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];
956 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
959 taskq_dispatch_ent(tq, func, arg, flags, ent);
963 spa_create_zio_taskqs(spa_t *spa)
965 for (int t = 0; t < ZIO_TYPES; t++) {
966 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
967 spa_taskqs_init(spa, t, q);
975 spa_thread(void *arg)
980 user_t *pu = PTOU(curproc);
982 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
985 ASSERT(curproc != &p0);
986 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
987 "zpool-%s", spa->spa_name);
988 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
991 /* bind this thread to the requested psrset */
992 if (zio_taskq_psrset_bind != PS_NONE) {
994 mutex_enter(&cpu_lock);
995 mutex_enter(&pidlock);
996 mutex_enter(&curproc->p_lock);
998 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
999 0, NULL, NULL) == 0) {
1000 curthread->t_bind_pset = zio_taskq_psrset_bind;
1003 "Couldn't bind process for zfs pool \"%s\" to "
1004 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1007 mutex_exit(&curproc->p_lock);
1008 mutex_exit(&pidlock);
1009 mutex_exit(&cpu_lock);
1015 if (zio_taskq_sysdc) {
1016 sysdc_thread_enter(curthread, 100, 0);
1020 spa->spa_proc = curproc;
1021 spa->spa_did = curthread->t_did;
1023 spa_create_zio_taskqs(spa);
1025 mutex_enter(&spa->spa_proc_lock);
1026 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1028 spa->spa_proc_state = SPA_PROC_ACTIVE;
1029 cv_broadcast(&spa->spa_proc_cv);
1031 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1032 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1033 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1034 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1036 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1037 spa->spa_proc_state = SPA_PROC_GONE;
1038 spa->spa_proc = &p0;
1039 cv_broadcast(&spa->spa_proc_cv);
1040 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1042 mutex_enter(&curproc->p_lock);
1045 #endif /* SPA_PROCESS */
1049 * Activate an uninitialized pool.
1052 spa_activate(spa_t *spa, int mode)
1054 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1056 spa->spa_state = POOL_STATE_ACTIVE;
1057 spa->spa_mode = mode;
1059 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1060 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1062 /* Try to create a covering process */
1063 mutex_enter(&spa->spa_proc_lock);
1064 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1065 ASSERT(spa->spa_proc == &p0);
1069 /* Only create a process if we're going to be around a while. */
1070 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1071 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1073 spa->spa_proc_state = SPA_PROC_CREATED;
1074 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1075 cv_wait(&spa->spa_proc_cv,
1076 &spa->spa_proc_lock);
1078 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1079 ASSERT(spa->spa_proc != &p0);
1080 ASSERT(spa->spa_did != 0);
1084 "Couldn't create process for zfs pool \"%s\"\n",
1089 #endif /* SPA_PROCESS */
1090 mutex_exit(&spa->spa_proc_lock);
1092 /* If we didn't create a process, we need to create our taskqs. */
1093 ASSERT(spa->spa_proc == &p0);
1094 if (spa->spa_proc == &p0) {
1095 spa_create_zio_taskqs(spa);
1099 * Start TRIM thread.
1101 trim_thread_create(spa);
1103 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1104 offsetof(vdev_t, vdev_config_dirty_node));
1105 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1106 offsetof(vdev_t, vdev_state_dirty_node));
1108 txg_list_create(&spa->spa_vdev_txg_list,
1109 offsetof(struct vdev, vdev_txg_node));
1111 avl_create(&spa->spa_errlist_scrub,
1112 spa_error_entry_compare, sizeof (spa_error_entry_t),
1113 offsetof(spa_error_entry_t, se_avl));
1114 avl_create(&spa->spa_errlist_last,
1115 spa_error_entry_compare, sizeof (spa_error_entry_t),
1116 offsetof(spa_error_entry_t, se_avl));
1120 * Opposite of spa_activate().
1123 spa_deactivate(spa_t *spa)
1125 ASSERT(spa->spa_sync_on == B_FALSE);
1126 ASSERT(spa->spa_dsl_pool == NULL);
1127 ASSERT(spa->spa_root_vdev == NULL);
1128 ASSERT(spa->spa_async_zio_root == NULL);
1129 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1132 * Stop TRIM thread in case spa_unload() wasn't called directly
1133 * before spa_deactivate().
1135 trim_thread_destroy(spa);
1137 txg_list_destroy(&spa->spa_vdev_txg_list);
1139 list_destroy(&spa->spa_config_dirty_list);
1140 list_destroy(&spa->spa_state_dirty_list);
1142 for (int t = 0; t < ZIO_TYPES; t++) {
1143 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1144 spa_taskqs_fini(spa, t, q);
1148 metaslab_class_destroy(spa->spa_normal_class);
1149 spa->spa_normal_class = NULL;
1151 metaslab_class_destroy(spa->spa_log_class);
1152 spa->spa_log_class = NULL;
1155 * If this was part of an import or the open otherwise failed, we may
1156 * still have errors left in the queues. Empty them just in case.
1158 spa_errlog_drain(spa);
1160 avl_destroy(&spa->spa_errlist_scrub);
1161 avl_destroy(&spa->spa_errlist_last);
1163 spa->spa_state = POOL_STATE_UNINITIALIZED;
1165 mutex_enter(&spa->spa_proc_lock);
1166 if (spa->spa_proc_state != SPA_PROC_NONE) {
1167 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1168 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1169 cv_broadcast(&spa->spa_proc_cv);
1170 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1171 ASSERT(spa->spa_proc != &p0);
1172 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1174 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1175 spa->spa_proc_state = SPA_PROC_NONE;
1177 ASSERT(spa->spa_proc == &p0);
1178 mutex_exit(&spa->spa_proc_lock);
1182 * We want to make sure spa_thread() has actually exited the ZFS
1183 * module, so that the module can't be unloaded out from underneath
1186 if (spa->spa_did != 0) {
1187 thread_join(spa->spa_did);
1190 #endif /* SPA_PROCESS */
1194 * Verify a pool configuration, and construct the vdev tree appropriately. This
1195 * will create all the necessary vdevs in the appropriate layout, with each vdev
1196 * in the CLOSED state. This will prep the pool before open/creation/import.
1197 * All vdev validation is done by the vdev_alloc() routine.
1200 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1201 uint_t id, int atype)
1207 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1210 if ((*vdp)->vdev_ops->vdev_op_leaf)
1213 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1216 if (error == ENOENT)
1222 return (SET_ERROR(EINVAL));
1225 for (int c = 0; c < children; c++) {
1227 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1235 ASSERT(*vdp != NULL);
1241 * Opposite of spa_load().
1244 spa_unload(spa_t *spa)
1248 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1253 trim_thread_destroy(spa);
1258 spa_async_suspend(spa);
1263 if (spa->spa_sync_on) {
1264 txg_sync_stop(spa->spa_dsl_pool);
1265 spa->spa_sync_on = B_FALSE;
1269 * Wait for any outstanding async I/O to complete.
1271 if (spa->spa_async_zio_root != NULL) {
1272 (void) zio_wait(spa->spa_async_zio_root);
1273 spa->spa_async_zio_root = NULL;
1276 bpobj_close(&spa->spa_deferred_bpobj);
1278 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1283 if (spa->spa_root_vdev)
1284 vdev_free(spa->spa_root_vdev);
1285 ASSERT(spa->spa_root_vdev == NULL);
1288 * Close the dsl pool.
1290 if (spa->spa_dsl_pool) {
1291 dsl_pool_close(spa->spa_dsl_pool);
1292 spa->spa_dsl_pool = NULL;
1293 spa->spa_meta_objset = NULL;
1300 * Drop and purge level 2 cache
1302 spa_l2cache_drop(spa);
1304 for (i = 0; i < spa->spa_spares.sav_count; i++)
1305 vdev_free(spa->spa_spares.sav_vdevs[i]);
1306 if (spa->spa_spares.sav_vdevs) {
1307 kmem_free(spa->spa_spares.sav_vdevs,
1308 spa->spa_spares.sav_count * sizeof (void *));
1309 spa->spa_spares.sav_vdevs = NULL;
1311 if (spa->spa_spares.sav_config) {
1312 nvlist_free(spa->spa_spares.sav_config);
1313 spa->spa_spares.sav_config = NULL;
1315 spa->spa_spares.sav_count = 0;
1317 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1318 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1319 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1321 if (spa->spa_l2cache.sav_vdevs) {
1322 kmem_free(spa->spa_l2cache.sav_vdevs,
1323 spa->spa_l2cache.sav_count * sizeof (void *));
1324 spa->spa_l2cache.sav_vdevs = NULL;
1326 if (spa->spa_l2cache.sav_config) {
1327 nvlist_free(spa->spa_l2cache.sav_config);
1328 spa->spa_l2cache.sav_config = NULL;
1330 spa->spa_l2cache.sav_count = 0;
1332 spa->spa_async_suspended = 0;
1334 if (spa->spa_comment != NULL) {
1335 spa_strfree(spa->spa_comment);
1336 spa->spa_comment = NULL;
1339 spa_config_exit(spa, SCL_ALL, FTAG);
1343 * Load (or re-load) the current list of vdevs describing the active spares for
1344 * this pool. When this is called, we have some form of basic information in
1345 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1346 * then re-generate a more complete list including status information.
1349 spa_load_spares(spa_t *spa)
1356 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1359 * First, close and free any existing spare vdevs.
1361 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1362 vd = spa->spa_spares.sav_vdevs[i];
1364 /* Undo the call to spa_activate() below */
1365 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1366 B_FALSE)) != NULL && tvd->vdev_isspare)
1367 spa_spare_remove(tvd);
1372 if (spa->spa_spares.sav_vdevs)
1373 kmem_free(spa->spa_spares.sav_vdevs,
1374 spa->spa_spares.sav_count * sizeof (void *));
1376 if (spa->spa_spares.sav_config == NULL)
1379 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1380 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1382 spa->spa_spares.sav_count = (int)nspares;
1383 spa->spa_spares.sav_vdevs = NULL;
1389 * Construct the array of vdevs, opening them to get status in the
1390 * process. For each spare, there is potentially two different vdev_t
1391 * structures associated with it: one in the list of spares (used only
1392 * for basic validation purposes) and one in the active vdev
1393 * configuration (if it's spared in). During this phase we open and
1394 * validate each vdev on the spare list. If the vdev also exists in the
1395 * active configuration, then we also mark this vdev as an active spare.
1397 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1399 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1400 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1401 VDEV_ALLOC_SPARE) == 0);
1404 spa->spa_spares.sav_vdevs[i] = vd;
1406 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1407 B_FALSE)) != NULL) {
1408 if (!tvd->vdev_isspare)
1412 * We only mark the spare active if we were successfully
1413 * able to load the vdev. Otherwise, importing a pool
1414 * with a bad active spare would result in strange
1415 * behavior, because multiple pool would think the spare
1416 * is actively in use.
1418 * There is a vulnerability here to an equally bizarre
1419 * circumstance, where a dead active spare is later
1420 * brought back to life (onlined or otherwise). Given
1421 * the rarity of this scenario, and the extra complexity
1422 * it adds, we ignore the possibility.
1424 if (!vdev_is_dead(tvd))
1425 spa_spare_activate(tvd);
1429 vd->vdev_aux = &spa->spa_spares;
1431 if (vdev_open(vd) != 0)
1434 if (vdev_validate_aux(vd) == 0)
1439 * Recompute the stashed list of spares, with status information
1442 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1443 DATA_TYPE_NVLIST_ARRAY) == 0);
1445 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1447 for (i = 0; i < spa->spa_spares.sav_count; i++)
1448 spares[i] = vdev_config_generate(spa,
1449 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1450 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1451 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1452 for (i = 0; i < spa->spa_spares.sav_count; i++)
1453 nvlist_free(spares[i]);
1454 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1458 * Load (or re-load) the current list of vdevs describing the active l2cache for
1459 * this pool. When this is called, we have some form of basic information in
1460 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1461 * then re-generate a more complete list including status information.
1462 * Devices which are already active have their details maintained, and are
1466 spa_load_l2cache(spa_t *spa)
1470 int i, j, oldnvdevs;
1472 vdev_t *vd, **oldvdevs, **newvdevs;
1473 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1475 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1477 if (sav->sav_config != NULL) {
1478 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1479 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1480 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1486 oldvdevs = sav->sav_vdevs;
1487 oldnvdevs = sav->sav_count;
1488 sav->sav_vdevs = NULL;
1492 * Process new nvlist of vdevs.
1494 for (i = 0; i < nl2cache; i++) {
1495 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1499 for (j = 0; j < oldnvdevs; j++) {
1501 if (vd != NULL && guid == vd->vdev_guid) {
1503 * Retain previous vdev for add/remove ops.
1511 if (newvdevs[i] == NULL) {
1515 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1516 VDEV_ALLOC_L2CACHE) == 0);
1521 * Commit this vdev as an l2cache device,
1522 * even if it fails to open.
1524 spa_l2cache_add(vd);
1529 spa_l2cache_activate(vd);
1531 if (vdev_open(vd) != 0)
1534 (void) vdev_validate_aux(vd);
1536 if (!vdev_is_dead(vd))
1537 l2arc_add_vdev(spa, vd);
1542 * Purge vdevs that were dropped
1544 for (i = 0; i < oldnvdevs; i++) {
1549 ASSERT(vd->vdev_isl2cache);
1551 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1552 pool != 0ULL && l2arc_vdev_present(vd))
1553 l2arc_remove_vdev(vd);
1554 vdev_clear_stats(vd);
1560 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1562 if (sav->sav_config == NULL)
1565 sav->sav_vdevs = newvdevs;
1566 sav->sav_count = (int)nl2cache;
1569 * Recompute the stashed list of l2cache devices, with status
1570 * information this time.
1572 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1573 DATA_TYPE_NVLIST_ARRAY) == 0);
1575 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1576 for (i = 0; i < sav->sav_count; i++)
1577 l2cache[i] = vdev_config_generate(spa,
1578 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1579 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1580 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1582 for (i = 0; i < sav->sav_count; i++)
1583 nvlist_free(l2cache[i]);
1585 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1589 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1592 char *packed = NULL;
1597 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1600 nvsize = *(uint64_t *)db->db_data;
1601 dmu_buf_rele(db, FTAG);
1603 packed = kmem_alloc(nvsize, KM_SLEEP);
1604 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1607 error = nvlist_unpack(packed, nvsize, value, 0);
1608 kmem_free(packed, nvsize);
1614 * Checks to see if the given vdev could not be opened, in which case we post a
1615 * sysevent to notify the autoreplace code that the device has been removed.
1618 spa_check_removed(vdev_t *vd)
1620 for (int c = 0; c < vd->vdev_children; c++)
1621 spa_check_removed(vd->vdev_child[c]);
1623 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1625 zfs_post_autoreplace(vd->vdev_spa, vd);
1626 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1631 * Validate the current config against the MOS config
1634 spa_config_valid(spa_t *spa, nvlist_t *config)
1636 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1639 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1641 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1642 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1644 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1647 * If we're doing a normal import, then build up any additional
1648 * diagnostic information about missing devices in this config.
1649 * We'll pass this up to the user for further processing.
1651 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1652 nvlist_t **child, *nv;
1655 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1657 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1659 for (int c = 0; c < rvd->vdev_children; c++) {
1660 vdev_t *tvd = rvd->vdev_child[c];
1661 vdev_t *mtvd = mrvd->vdev_child[c];
1663 if (tvd->vdev_ops == &vdev_missing_ops &&
1664 mtvd->vdev_ops != &vdev_missing_ops &&
1666 child[idx++] = vdev_config_generate(spa, mtvd,
1671 VERIFY(nvlist_add_nvlist_array(nv,
1672 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1673 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1674 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1676 for (int i = 0; i < idx; i++)
1677 nvlist_free(child[i]);
1680 kmem_free(child, rvd->vdev_children * sizeof (char **));
1684 * Compare the root vdev tree with the information we have
1685 * from the MOS config (mrvd). Check each top-level vdev
1686 * with the corresponding MOS config top-level (mtvd).
1688 for (int c = 0; c < rvd->vdev_children; c++) {
1689 vdev_t *tvd = rvd->vdev_child[c];
1690 vdev_t *mtvd = mrvd->vdev_child[c];
1693 * Resolve any "missing" vdevs in the current configuration.
1694 * If we find that the MOS config has more accurate information
1695 * about the top-level vdev then use that vdev instead.
1697 if (tvd->vdev_ops == &vdev_missing_ops &&
1698 mtvd->vdev_ops != &vdev_missing_ops) {
1700 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1704 * Device specific actions.
1706 if (mtvd->vdev_islog) {
1707 spa_set_log_state(spa, SPA_LOG_CLEAR);
1710 * XXX - once we have 'readonly' pool
1711 * support we should be able to handle
1712 * missing data devices by transitioning
1713 * the pool to readonly.
1719 * Swap the missing vdev with the data we were
1720 * able to obtain from the MOS config.
1722 vdev_remove_child(rvd, tvd);
1723 vdev_remove_child(mrvd, mtvd);
1725 vdev_add_child(rvd, mtvd);
1726 vdev_add_child(mrvd, tvd);
1728 spa_config_exit(spa, SCL_ALL, FTAG);
1730 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1733 } else if (mtvd->vdev_islog) {
1735 * Load the slog device's state from the MOS config
1736 * since it's possible that the label does not
1737 * contain the most up-to-date information.
1739 vdev_load_log_state(tvd, mtvd);
1744 spa_config_exit(spa, SCL_ALL, FTAG);
1747 * Ensure we were able to validate the config.
1749 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1753 * Check for missing log devices
1756 spa_check_logs(spa_t *spa)
1758 boolean_t rv = B_FALSE;
1760 switch (spa->spa_log_state) {
1761 case SPA_LOG_MISSING:
1762 /* need to recheck in case slog has been restored */
1763 case SPA_LOG_UNKNOWN:
1764 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1765 NULL, DS_FIND_CHILDREN) != 0);
1767 spa_set_log_state(spa, SPA_LOG_MISSING);
1774 spa_passivate_log(spa_t *spa)
1776 vdev_t *rvd = spa->spa_root_vdev;
1777 boolean_t slog_found = B_FALSE;
1779 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1781 if (!spa_has_slogs(spa))
1784 for (int c = 0; c < rvd->vdev_children; c++) {
1785 vdev_t *tvd = rvd->vdev_child[c];
1786 metaslab_group_t *mg = tvd->vdev_mg;
1788 if (tvd->vdev_islog) {
1789 metaslab_group_passivate(mg);
1790 slog_found = B_TRUE;
1794 return (slog_found);
1798 spa_activate_log(spa_t *spa)
1800 vdev_t *rvd = spa->spa_root_vdev;
1802 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1804 for (int c = 0; c < rvd->vdev_children; c++) {
1805 vdev_t *tvd = rvd->vdev_child[c];
1806 metaslab_group_t *mg = tvd->vdev_mg;
1808 if (tvd->vdev_islog)
1809 metaslab_group_activate(mg);
1814 spa_offline_log(spa_t *spa)
1818 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1819 NULL, DS_FIND_CHILDREN);
1822 * We successfully offlined the log device, sync out the
1823 * current txg so that the "stubby" block can be removed
1826 txg_wait_synced(spa->spa_dsl_pool, 0);
1832 spa_aux_check_removed(spa_aux_vdev_t *sav)
1836 for (i = 0; i < sav->sav_count; i++)
1837 spa_check_removed(sav->sav_vdevs[i]);
1841 spa_claim_notify(zio_t *zio)
1843 spa_t *spa = zio->io_spa;
1848 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1849 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1850 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1851 mutex_exit(&spa->spa_props_lock);
1854 typedef struct spa_load_error {
1855 uint64_t sle_meta_count;
1856 uint64_t sle_data_count;
1860 spa_load_verify_done(zio_t *zio)
1862 blkptr_t *bp = zio->io_bp;
1863 spa_load_error_t *sle = zio->io_private;
1864 dmu_object_type_t type = BP_GET_TYPE(bp);
1865 int error = zio->io_error;
1868 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1869 type != DMU_OT_INTENT_LOG)
1870 atomic_add_64(&sle->sle_meta_count, 1);
1872 atomic_add_64(&sle->sle_data_count, 1);
1874 zio_data_buf_free(zio->io_data, zio->io_size);
1879 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1880 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1882 if (!BP_IS_HOLE(bp)) {
1884 size_t size = BP_GET_PSIZE(bp);
1885 void *data = zio_data_buf_alloc(size);
1887 zio_nowait(zio_read(rio, spa, bp, data, size,
1888 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1889 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1890 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1896 spa_load_verify(spa_t *spa)
1899 spa_load_error_t sle = { 0 };
1900 zpool_rewind_policy_t policy;
1901 boolean_t verify_ok = B_FALSE;
1904 zpool_get_rewind_policy(spa->spa_config, &policy);
1906 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1909 rio = zio_root(spa, NULL, &sle,
1910 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1912 error = traverse_pool(spa, spa->spa_verify_min_txg,
1913 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1915 (void) zio_wait(rio);
1917 spa->spa_load_meta_errors = sle.sle_meta_count;
1918 spa->spa_load_data_errors = sle.sle_data_count;
1920 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1921 sle.sle_data_count <= policy.zrp_maxdata) {
1925 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1926 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1928 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1929 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1930 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1931 VERIFY(nvlist_add_int64(spa->spa_load_info,
1932 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1933 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1934 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1936 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1940 if (error != ENXIO && error != EIO)
1941 error = SET_ERROR(EIO);
1945 return (verify_ok ? 0 : EIO);
1949 * Find a value in the pool props object.
1952 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1954 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1955 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1959 * Find a value in the pool directory object.
1962 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1964 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1965 name, sizeof (uint64_t), 1, val));
1969 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1971 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1976 * Fix up config after a partly-completed split. This is done with the
1977 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1978 * pool have that entry in their config, but only the splitting one contains
1979 * a list of all the guids of the vdevs that are being split off.
1981 * This function determines what to do with that list: either rejoin
1982 * all the disks to the pool, or complete the splitting process. To attempt
1983 * the rejoin, each disk that is offlined is marked online again, and
1984 * we do a reopen() call. If the vdev label for every disk that was
1985 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1986 * then we call vdev_split() on each disk, and complete the split.
1988 * Otherwise we leave the config alone, with all the vdevs in place in
1989 * the original pool.
1992 spa_try_repair(spa_t *spa, nvlist_t *config)
1999 boolean_t attempt_reopen;
2001 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2004 /* check that the config is complete */
2005 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2006 &glist, &gcount) != 0)
2009 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2011 /* attempt to online all the vdevs & validate */
2012 attempt_reopen = B_TRUE;
2013 for (i = 0; i < gcount; i++) {
2014 if (glist[i] == 0) /* vdev is hole */
2017 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2018 if (vd[i] == NULL) {
2020 * Don't bother attempting to reopen the disks;
2021 * just do the split.
2023 attempt_reopen = B_FALSE;
2025 /* attempt to re-online it */
2026 vd[i]->vdev_offline = B_FALSE;
2030 if (attempt_reopen) {
2031 vdev_reopen(spa->spa_root_vdev);
2033 /* check each device to see what state it's in */
2034 for (extracted = 0, i = 0; i < gcount; i++) {
2035 if (vd[i] != NULL &&
2036 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2043 * If every disk has been moved to the new pool, or if we never
2044 * even attempted to look at them, then we split them off for
2047 if (!attempt_reopen || gcount == extracted) {
2048 for (i = 0; i < gcount; i++)
2051 vdev_reopen(spa->spa_root_vdev);
2054 kmem_free(vd, gcount * sizeof (vdev_t *));
2058 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2059 boolean_t mosconfig)
2061 nvlist_t *config = spa->spa_config;
2062 char *ereport = FM_EREPORT_ZFS_POOL;
2068 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2069 return (SET_ERROR(EINVAL));
2071 ASSERT(spa->spa_comment == NULL);
2072 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2073 spa->spa_comment = spa_strdup(comment);
2076 * Versioning wasn't explicitly added to the label until later, so if
2077 * it's not present treat it as the initial version.
2079 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2080 &spa->spa_ubsync.ub_version) != 0)
2081 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2083 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2084 &spa->spa_config_txg);
2086 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2087 spa_guid_exists(pool_guid, 0)) {
2088 error = SET_ERROR(EEXIST);
2090 spa->spa_config_guid = pool_guid;
2092 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2094 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2098 nvlist_free(spa->spa_load_info);
2099 spa->spa_load_info = fnvlist_alloc();
2101 gethrestime(&spa->spa_loaded_ts);
2102 error = spa_load_impl(spa, pool_guid, config, state, type,
2103 mosconfig, &ereport);
2106 spa->spa_minref = refcount_count(&spa->spa_refcount);
2108 if (error != EEXIST) {
2109 spa->spa_loaded_ts.tv_sec = 0;
2110 spa->spa_loaded_ts.tv_nsec = 0;
2112 if (error != EBADF) {
2113 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2116 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2123 * Load an existing storage pool, using the pool's builtin spa_config as a
2124 * source of configuration information.
2127 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2128 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2132 nvlist_t *nvroot = NULL;
2135 uberblock_t *ub = &spa->spa_uberblock;
2136 uint64_t children, config_cache_txg = spa->spa_config_txg;
2137 int orig_mode = spa->spa_mode;
2140 boolean_t missing_feat_write = B_FALSE;
2143 * If this is an untrusted config, access the pool in read-only mode.
2144 * This prevents things like resilvering recently removed devices.
2147 spa->spa_mode = FREAD;
2149 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2151 spa->spa_load_state = state;
2153 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2154 return (SET_ERROR(EINVAL));
2156 parse = (type == SPA_IMPORT_EXISTING ?
2157 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2160 * Create "The Godfather" zio to hold all async IOs
2162 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2163 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2166 * Parse the configuration into a vdev tree. We explicitly set the
2167 * value that will be returned by spa_version() since parsing the
2168 * configuration requires knowing the version number.
2170 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2171 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2172 spa_config_exit(spa, SCL_ALL, FTAG);
2177 ASSERT(spa->spa_root_vdev == rvd);
2179 if (type != SPA_IMPORT_ASSEMBLE) {
2180 ASSERT(spa_guid(spa) == pool_guid);
2184 * Try to open all vdevs, loading each label in the process.
2186 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2187 error = vdev_open(rvd);
2188 spa_config_exit(spa, SCL_ALL, FTAG);
2193 * We need to validate the vdev labels against the configuration that
2194 * we have in hand, which is dependent on the setting of mosconfig. If
2195 * mosconfig is true then we're validating the vdev labels based on
2196 * that config. Otherwise, we're validating against the cached config
2197 * (zpool.cache) that was read when we loaded the zfs module, and then
2198 * later we will recursively call spa_load() and validate against
2201 * If we're assembling a new pool that's been split off from an
2202 * existing pool, the labels haven't yet been updated so we skip
2203 * validation for now.
2205 if (type != SPA_IMPORT_ASSEMBLE) {
2206 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2207 error = vdev_validate(rvd, mosconfig);
2208 spa_config_exit(spa, SCL_ALL, FTAG);
2213 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2214 return (SET_ERROR(ENXIO));
2218 * Find the best uberblock.
2220 vdev_uberblock_load(rvd, ub, &label);
2223 * If we weren't able to find a single valid uberblock, return failure.
2225 if (ub->ub_txg == 0) {
2227 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2231 * If the pool has an unsupported version we can't open it.
2233 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2235 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2238 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2242 * If we weren't able to find what's necessary for reading the
2243 * MOS in the label, return failure.
2245 if (label == NULL || nvlist_lookup_nvlist(label,
2246 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2248 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2253 * Update our in-core representation with the definitive values
2256 nvlist_free(spa->spa_label_features);
2257 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2263 * Look through entries in the label nvlist's features_for_read. If
2264 * there is a feature listed there which we don't understand then we
2265 * cannot open a pool.
2267 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2268 nvlist_t *unsup_feat;
2270 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2273 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2275 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2276 if (!zfeature_is_supported(nvpair_name(nvp))) {
2277 VERIFY(nvlist_add_string(unsup_feat,
2278 nvpair_name(nvp), "") == 0);
2282 if (!nvlist_empty(unsup_feat)) {
2283 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2284 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2285 nvlist_free(unsup_feat);
2286 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2290 nvlist_free(unsup_feat);
2294 * If the vdev guid sum doesn't match the uberblock, we have an
2295 * incomplete configuration. We first check to see if the pool
2296 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2297 * If it is, defer the vdev_guid_sum check till later so we
2298 * can handle missing vdevs.
2300 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2301 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2302 rvd->vdev_guid_sum != ub->ub_guid_sum)
2303 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2305 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2306 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2307 spa_try_repair(spa, config);
2308 spa_config_exit(spa, SCL_ALL, FTAG);
2309 nvlist_free(spa->spa_config_splitting);
2310 spa->spa_config_splitting = NULL;
2314 * Initialize internal SPA structures.
2316 spa->spa_state = POOL_STATE_ACTIVE;
2317 spa->spa_ubsync = spa->spa_uberblock;
2318 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2319 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2320 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2321 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2322 spa->spa_claim_max_txg = spa->spa_first_txg;
2323 spa->spa_prev_software_version = ub->ub_software_version;
2325 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2327 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2328 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2330 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2331 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2333 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2334 boolean_t missing_feat_read = B_FALSE;
2335 nvlist_t *unsup_feat, *enabled_feat;
2337 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2338 &spa->spa_feat_for_read_obj) != 0) {
2339 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2342 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2343 &spa->spa_feat_for_write_obj) != 0) {
2344 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2347 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2348 &spa->spa_feat_desc_obj) != 0) {
2349 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2352 enabled_feat = fnvlist_alloc();
2353 unsup_feat = fnvlist_alloc();
2355 if (!spa_features_check(spa, B_FALSE,
2356 unsup_feat, enabled_feat))
2357 missing_feat_read = B_TRUE;
2359 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2360 if (!spa_features_check(spa, B_TRUE,
2361 unsup_feat, enabled_feat)) {
2362 missing_feat_write = B_TRUE;
2366 fnvlist_add_nvlist(spa->spa_load_info,
2367 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2369 if (!nvlist_empty(unsup_feat)) {
2370 fnvlist_add_nvlist(spa->spa_load_info,
2371 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2374 fnvlist_free(enabled_feat);
2375 fnvlist_free(unsup_feat);
2377 if (!missing_feat_read) {
2378 fnvlist_add_boolean(spa->spa_load_info,
2379 ZPOOL_CONFIG_CAN_RDONLY);
2383 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2384 * twofold: to determine whether the pool is available for
2385 * import in read-write mode and (if it is not) whether the
2386 * pool is available for import in read-only mode. If the pool
2387 * is available for import in read-write mode, it is displayed
2388 * as available in userland; if it is not available for import
2389 * in read-only mode, it is displayed as unavailable in
2390 * userland. If the pool is available for import in read-only
2391 * mode but not read-write mode, it is displayed as unavailable
2392 * in userland with a special note that the pool is actually
2393 * available for open in read-only mode.
2395 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2396 * missing a feature for write, we must first determine whether
2397 * the pool can be opened read-only before returning to
2398 * userland in order to know whether to display the
2399 * abovementioned note.
2401 if (missing_feat_read || (missing_feat_write &&
2402 spa_writeable(spa))) {
2403 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2408 * Load refcounts for ZFS features from disk into an in-memory
2409 * cache during SPA initialization.
2411 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2414 error = feature_get_refcount_from_disk(spa,
2415 &spa_feature_table[i], &refcount);
2417 spa->spa_feat_refcount_cache[i] = refcount;
2418 } else if (error == ENOTSUP) {
2419 spa->spa_feat_refcount_cache[i] =
2420 SPA_FEATURE_DISABLED;
2422 return (spa_vdev_err(rvd,
2423 VDEV_AUX_CORRUPT_DATA, EIO));
2428 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2429 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2430 &spa->spa_feat_enabled_txg_obj) != 0) {
2431 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2435 spa->spa_is_initializing = B_TRUE;
2436 error = dsl_pool_open(spa->spa_dsl_pool);
2437 spa->spa_is_initializing = B_FALSE;
2439 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2443 nvlist_t *policy = NULL, *nvconfig;
2445 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2446 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2448 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2449 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2451 unsigned long myhostid = 0;
2453 VERIFY(nvlist_lookup_string(nvconfig,
2454 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2457 myhostid = zone_get_hostid(NULL);
2460 * We're emulating the system's hostid in userland, so
2461 * we can't use zone_get_hostid().
2463 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2464 #endif /* _KERNEL */
2465 if (check_hostid && hostid != 0 && myhostid != 0 &&
2466 hostid != myhostid) {
2467 nvlist_free(nvconfig);
2468 cmn_err(CE_WARN, "pool '%s' could not be "
2469 "loaded as it was last accessed by "
2470 "another system (host: %s hostid: 0x%lx). "
2471 "See: http://illumos.org/msg/ZFS-8000-EY",
2472 spa_name(spa), hostname,
2473 (unsigned long)hostid);
2474 return (SET_ERROR(EBADF));
2477 if (nvlist_lookup_nvlist(spa->spa_config,
2478 ZPOOL_REWIND_POLICY, &policy) == 0)
2479 VERIFY(nvlist_add_nvlist(nvconfig,
2480 ZPOOL_REWIND_POLICY, policy) == 0);
2482 spa_config_set(spa, nvconfig);
2484 spa_deactivate(spa);
2485 spa_activate(spa, orig_mode);
2487 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2490 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2491 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2492 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2494 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2497 * Load the bit that tells us to use the new accounting function
2498 * (raid-z deflation). If we have an older pool, this will not
2501 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2502 if (error != 0 && error != ENOENT)
2503 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2505 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2506 &spa->spa_creation_version);
2507 if (error != 0 && error != ENOENT)
2508 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2511 * Load the persistent error log. If we have an older pool, this will
2514 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2515 if (error != 0 && error != ENOENT)
2516 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2518 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2519 &spa->spa_errlog_scrub);
2520 if (error != 0 && error != ENOENT)
2521 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2524 * Load the history object. If we have an older pool, this
2525 * will not be present.
2527 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2528 if (error != 0 && error != ENOENT)
2529 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2532 * If we're assembling the pool from the split-off vdevs of
2533 * an existing pool, we don't want to attach the spares & cache
2538 * Load any hot spares for this pool.
2540 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2541 if (error != 0 && error != ENOENT)
2542 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2543 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2544 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2545 if (load_nvlist(spa, spa->spa_spares.sav_object,
2546 &spa->spa_spares.sav_config) != 0)
2547 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2549 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2550 spa_load_spares(spa);
2551 spa_config_exit(spa, SCL_ALL, FTAG);
2552 } else if (error == 0) {
2553 spa->spa_spares.sav_sync = B_TRUE;
2557 * Load any level 2 ARC devices for this pool.
2559 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2560 &spa->spa_l2cache.sav_object);
2561 if (error != 0 && error != ENOENT)
2562 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2563 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2564 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2565 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2566 &spa->spa_l2cache.sav_config) != 0)
2567 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2569 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2570 spa_load_l2cache(spa);
2571 spa_config_exit(spa, SCL_ALL, FTAG);
2572 } else if (error == 0) {
2573 spa->spa_l2cache.sav_sync = B_TRUE;
2576 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2578 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2579 if (error && error != ENOENT)
2580 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2583 uint64_t autoreplace;
2585 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2586 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2587 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2588 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2589 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2590 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2591 &spa->spa_dedup_ditto);
2593 spa->spa_autoreplace = (autoreplace != 0);
2597 * If the 'autoreplace' property is set, then post a resource notifying
2598 * the ZFS DE that it should not issue any faults for unopenable
2599 * devices. We also iterate over the vdevs, and post a sysevent for any
2600 * unopenable vdevs so that the normal autoreplace handler can take
2603 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2604 spa_check_removed(spa->spa_root_vdev);
2606 * For the import case, this is done in spa_import(), because
2607 * at this point we're using the spare definitions from
2608 * the MOS config, not necessarily from the userland config.
2610 if (state != SPA_LOAD_IMPORT) {
2611 spa_aux_check_removed(&spa->spa_spares);
2612 spa_aux_check_removed(&spa->spa_l2cache);
2617 * Load the vdev state for all toplevel vdevs.
2622 * Propagate the leaf DTLs we just loaded all the way up the tree.
2624 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2625 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2626 spa_config_exit(spa, SCL_ALL, FTAG);
2629 * Load the DDTs (dedup tables).
2631 error = ddt_load(spa);
2633 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2635 spa_update_dspace(spa);
2638 * Validate the config, using the MOS config to fill in any
2639 * information which might be missing. If we fail to validate
2640 * the config then declare the pool unfit for use. If we're
2641 * assembling a pool from a split, the log is not transferred
2644 if (type != SPA_IMPORT_ASSEMBLE) {
2647 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2648 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2650 if (!spa_config_valid(spa, nvconfig)) {
2651 nvlist_free(nvconfig);
2652 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2655 nvlist_free(nvconfig);
2658 * Now that we've validated the config, check the state of the
2659 * root vdev. If it can't be opened, it indicates one or
2660 * more toplevel vdevs are faulted.
2662 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2663 return (SET_ERROR(ENXIO));
2665 if (spa_check_logs(spa)) {
2666 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2667 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2671 if (missing_feat_write) {
2672 ASSERT(state == SPA_LOAD_TRYIMPORT);
2675 * At this point, we know that we can open the pool in
2676 * read-only mode but not read-write mode. We now have enough
2677 * information and can return to userland.
2679 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2683 * We've successfully opened the pool, verify that we're ready
2684 * to start pushing transactions.
2686 if (state != SPA_LOAD_TRYIMPORT) {
2687 if (error = spa_load_verify(spa))
2688 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2692 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2693 spa->spa_load_max_txg == UINT64_MAX)) {
2695 int need_update = B_FALSE;
2697 ASSERT(state != SPA_LOAD_TRYIMPORT);
2700 * Claim log blocks that haven't been committed yet.
2701 * This must all happen in a single txg.
2702 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2703 * invoked from zil_claim_log_block()'s i/o done callback.
2704 * Price of rollback is that we abandon the log.
2706 spa->spa_claiming = B_TRUE;
2708 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2709 spa_first_txg(spa));
2710 (void) dmu_objset_find(spa_name(spa),
2711 zil_claim, tx, DS_FIND_CHILDREN);
2714 spa->spa_claiming = B_FALSE;
2716 spa_set_log_state(spa, SPA_LOG_GOOD);
2717 spa->spa_sync_on = B_TRUE;
2718 txg_sync_start(spa->spa_dsl_pool);
2721 * Wait for all claims to sync. We sync up to the highest
2722 * claimed log block birth time so that claimed log blocks
2723 * don't appear to be from the future. spa_claim_max_txg
2724 * will have been set for us by either zil_check_log_chain()
2725 * (invoked from spa_check_logs()) or zil_claim() above.
2727 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2730 * If the config cache is stale, or we have uninitialized
2731 * metaslabs (see spa_vdev_add()), then update the config.
2733 * If this is a verbatim import, trust the current
2734 * in-core spa_config and update the disk labels.
2736 if (config_cache_txg != spa->spa_config_txg ||
2737 state == SPA_LOAD_IMPORT ||
2738 state == SPA_LOAD_RECOVER ||
2739 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2740 need_update = B_TRUE;
2742 for (int c = 0; c < rvd->vdev_children; c++)
2743 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2744 need_update = B_TRUE;
2747 * Update the config cache asychronously in case we're the
2748 * root pool, in which case the config cache isn't writable yet.
2751 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2754 * Check all DTLs to see if anything needs resilvering.
2756 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2757 vdev_resilver_needed(rvd, NULL, NULL))
2758 spa_async_request(spa, SPA_ASYNC_RESILVER);
2761 * Log the fact that we booted up (so that we can detect if
2762 * we rebooted in the middle of an operation).
2764 spa_history_log_version(spa, "open");
2767 * Delete any inconsistent datasets.
2769 (void) dmu_objset_find(spa_name(spa),
2770 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2773 * Clean up any stale temporary dataset userrefs.
2775 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2782 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2784 int mode = spa->spa_mode;
2787 spa_deactivate(spa);
2789 spa->spa_load_max_txg--;
2791 spa_activate(spa, mode);
2792 spa_async_suspend(spa);
2794 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2798 * If spa_load() fails this function will try loading prior txg's. If
2799 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2800 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2801 * function will not rewind the pool and will return the same error as
2805 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2806 uint64_t max_request, int rewind_flags)
2808 nvlist_t *loadinfo = NULL;
2809 nvlist_t *config = NULL;
2810 int load_error, rewind_error;
2811 uint64_t safe_rewind_txg;
2814 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2815 spa->spa_load_max_txg = spa->spa_load_txg;
2816 spa_set_log_state(spa, SPA_LOG_CLEAR);
2818 spa->spa_load_max_txg = max_request;
2821 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2823 if (load_error == 0)
2826 if (spa->spa_root_vdev != NULL)
2827 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2829 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2830 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2832 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2833 nvlist_free(config);
2834 return (load_error);
2837 if (state == SPA_LOAD_RECOVER) {
2838 /* Price of rolling back is discarding txgs, including log */
2839 spa_set_log_state(spa, SPA_LOG_CLEAR);
2842 * If we aren't rolling back save the load info from our first
2843 * import attempt so that we can restore it after attempting
2846 loadinfo = spa->spa_load_info;
2847 spa->spa_load_info = fnvlist_alloc();
2850 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2851 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2852 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2853 TXG_INITIAL : safe_rewind_txg;
2856 * Continue as long as we're finding errors, we're still within
2857 * the acceptable rewind range, and we're still finding uberblocks
2859 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2860 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2861 if (spa->spa_load_max_txg < safe_rewind_txg)
2862 spa->spa_extreme_rewind = B_TRUE;
2863 rewind_error = spa_load_retry(spa, state, mosconfig);
2866 spa->spa_extreme_rewind = B_FALSE;
2867 spa->spa_load_max_txg = UINT64_MAX;
2869 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2870 spa_config_set(spa, config);
2872 if (state == SPA_LOAD_RECOVER) {
2873 ASSERT3P(loadinfo, ==, NULL);
2874 return (rewind_error);
2876 /* Store the rewind info as part of the initial load info */
2877 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2878 spa->spa_load_info);
2880 /* Restore the initial load info */
2881 fnvlist_free(spa->spa_load_info);
2882 spa->spa_load_info = loadinfo;
2884 return (load_error);
2891 * The import case is identical to an open except that the configuration is sent
2892 * down from userland, instead of grabbed from the configuration cache. For the
2893 * case of an open, the pool configuration will exist in the
2894 * POOL_STATE_UNINITIALIZED state.
2896 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2897 * the same time open the pool, without having to keep around the spa_t in some
2901 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2905 spa_load_state_t state = SPA_LOAD_OPEN;
2907 int locked = B_FALSE;
2908 int firstopen = B_FALSE;
2913 * As disgusting as this is, we need to support recursive calls to this
2914 * function because dsl_dir_open() is called during spa_load(), and ends
2915 * up calling spa_open() again. The real fix is to figure out how to
2916 * avoid dsl_dir_open() calling this in the first place.
2918 if (mutex_owner(&spa_namespace_lock) != curthread) {
2919 mutex_enter(&spa_namespace_lock);
2923 if ((spa = spa_lookup(pool)) == NULL) {
2925 mutex_exit(&spa_namespace_lock);
2926 return (SET_ERROR(ENOENT));
2929 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2930 zpool_rewind_policy_t policy;
2934 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2936 if (policy.zrp_request & ZPOOL_DO_REWIND)
2937 state = SPA_LOAD_RECOVER;
2939 spa_activate(spa, spa_mode_global);
2941 if (state != SPA_LOAD_RECOVER)
2942 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2944 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2945 policy.zrp_request);
2947 if (error == EBADF) {
2949 * If vdev_validate() returns failure (indicated by
2950 * EBADF), it indicates that one of the vdevs indicates
2951 * that the pool has been exported or destroyed. If
2952 * this is the case, the config cache is out of sync and
2953 * we should remove the pool from the namespace.
2956 spa_deactivate(spa);
2957 spa_config_sync(spa, B_TRUE, B_TRUE);
2960 mutex_exit(&spa_namespace_lock);
2961 return (SET_ERROR(ENOENT));
2966 * We can't open the pool, but we still have useful
2967 * information: the state of each vdev after the
2968 * attempted vdev_open(). Return this to the user.
2970 if (config != NULL && spa->spa_config) {
2971 VERIFY(nvlist_dup(spa->spa_config, config,
2973 VERIFY(nvlist_add_nvlist(*config,
2974 ZPOOL_CONFIG_LOAD_INFO,
2975 spa->spa_load_info) == 0);
2978 spa_deactivate(spa);
2979 spa->spa_last_open_failed = error;
2981 mutex_exit(&spa_namespace_lock);
2987 spa_open_ref(spa, tag);
2990 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2993 * If we've recovered the pool, pass back any information we
2994 * gathered while doing the load.
2996 if (state == SPA_LOAD_RECOVER) {
2997 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2998 spa->spa_load_info) == 0);
3002 spa->spa_last_open_failed = 0;
3003 spa->spa_last_ubsync_txg = 0;
3004 spa->spa_load_txg = 0;
3005 mutex_exit(&spa_namespace_lock);
3009 zvol_create_minors(spa->spa_name);
3020 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3023 return (spa_open_common(name, spapp, tag, policy, config));
3027 spa_open(const char *name, spa_t **spapp, void *tag)
3029 return (spa_open_common(name, spapp, tag, NULL, NULL));
3033 * Lookup the given spa_t, incrementing the inject count in the process,
3034 * preventing it from being exported or destroyed.
3037 spa_inject_addref(char *name)
3041 mutex_enter(&spa_namespace_lock);
3042 if ((spa = spa_lookup(name)) == NULL) {
3043 mutex_exit(&spa_namespace_lock);
3046 spa->spa_inject_ref++;
3047 mutex_exit(&spa_namespace_lock);
3053 spa_inject_delref(spa_t *spa)
3055 mutex_enter(&spa_namespace_lock);
3056 spa->spa_inject_ref--;
3057 mutex_exit(&spa_namespace_lock);
3061 * Add spares device information to the nvlist.
3064 spa_add_spares(spa_t *spa, nvlist_t *config)
3074 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3076 if (spa->spa_spares.sav_count == 0)
3079 VERIFY(nvlist_lookup_nvlist(config,
3080 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3081 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3082 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3084 VERIFY(nvlist_add_nvlist_array(nvroot,
3085 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3086 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3087 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3090 * Go through and find any spares which have since been
3091 * repurposed as an active spare. If this is the case, update
3092 * their status appropriately.
3094 for (i = 0; i < nspares; i++) {
3095 VERIFY(nvlist_lookup_uint64(spares[i],
3096 ZPOOL_CONFIG_GUID, &guid) == 0);
3097 if (spa_spare_exists(guid, &pool, NULL) &&
3099 VERIFY(nvlist_lookup_uint64_array(
3100 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3101 (uint64_t **)&vs, &vsc) == 0);
3102 vs->vs_state = VDEV_STATE_CANT_OPEN;
3103 vs->vs_aux = VDEV_AUX_SPARED;
3110 * Add l2cache device information to the nvlist, including vdev stats.
3113 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3116 uint_t i, j, nl2cache;
3123 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3125 if (spa->spa_l2cache.sav_count == 0)
3128 VERIFY(nvlist_lookup_nvlist(config,
3129 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3130 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3131 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3132 if (nl2cache != 0) {
3133 VERIFY(nvlist_add_nvlist_array(nvroot,
3134 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3135 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3136 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3139 * Update level 2 cache device stats.
3142 for (i = 0; i < nl2cache; i++) {
3143 VERIFY(nvlist_lookup_uint64(l2cache[i],
3144 ZPOOL_CONFIG_GUID, &guid) == 0);
3147 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3149 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3150 vd = spa->spa_l2cache.sav_vdevs[j];
3156 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3157 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3159 vdev_get_stats(vd, vs);
3165 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3171 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3172 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3174 /* We may be unable to read features if pool is suspended. */
3175 if (spa_suspended(spa))
3178 if (spa->spa_feat_for_read_obj != 0) {
3179 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3180 spa->spa_feat_for_read_obj);
3181 zap_cursor_retrieve(&zc, &za) == 0;
3182 zap_cursor_advance(&zc)) {
3183 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3184 za.za_num_integers == 1);
3185 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3186 za.za_first_integer));
3188 zap_cursor_fini(&zc);
3191 if (spa->spa_feat_for_write_obj != 0) {
3192 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3193 spa->spa_feat_for_write_obj);
3194 zap_cursor_retrieve(&zc, &za) == 0;
3195 zap_cursor_advance(&zc)) {
3196 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3197 za.za_num_integers == 1);
3198 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3199 za.za_first_integer));
3201 zap_cursor_fini(&zc);
3205 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3207 nvlist_free(features);
3211 spa_get_stats(const char *name, nvlist_t **config,
3212 char *altroot, size_t buflen)
3218 error = spa_open_common(name, &spa, FTAG, NULL, config);
3222 * This still leaves a window of inconsistency where the spares
3223 * or l2cache devices could change and the config would be
3224 * self-inconsistent.
3226 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3228 if (*config != NULL) {
3229 uint64_t loadtimes[2];
3231 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3232 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3233 VERIFY(nvlist_add_uint64_array(*config,
3234 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3236 VERIFY(nvlist_add_uint64(*config,
3237 ZPOOL_CONFIG_ERRCOUNT,
3238 spa_get_errlog_size(spa)) == 0);
3240 if (spa_suspended(spa))
3241 VERIFY(nvlist_add_uint64(*config,
3242 ZPOOL_CONFIG_SUSPENDED,
3243 spa->spa_failmode) == 0);
3245 spa_add_spares(spa, *config);
3246 spa_add_l2cache(spa, *config);
3247 spa_add_feature_stats(spa, *config);
3252 * We want to get the alternate root even for faulted pools, so we cheat
3253 * and call spa_lookup() directly.
3257 mutex_enter(&spa_namespace_lock);
3258 spa = spa_lookup(name);
3260 spa_altroot(spa, altroot, buflen);
3264 mutex_exit(&spa_namespace_lock);
3266 spa_altroot(spa, altroot, buflen);
3271 spa_config_exit(spa, SCL_CONFIG, FTAG);
3272 spa_close(spa, FTAG);
3279 * Validate that the auxiliary device array is well formed. We must have an
3280 * array of nvlists, each which describes a valid leaf vdev. If this is an
3281 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3282 * specified, as long as they are well-formed.
3285 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3286 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3287 vdev_labeltype_t label)
3294 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3297 * It's acceptable to have no devs specified.
3299 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3303 return (SET_ERROR(EINVAL));
3306 * Make sure the pool is formatted with a version that supports this
3309 if (spa_version(spa) < version)
3310 return (SET_ERROR(ENOTSUP));
3313 * Set the pending device list so we correctly handle device in-use
3316 sav->sav_pending = dev;
3317 sav->sav_npending = ndev;
3319 for (i = 0; i < ndev; i++) {
3320 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3324 if (!vd->vdev_ops->vdev_op_leaf) {
3326 error = SET_ERROR(EINVAL);
3331 * The L2ARC currently only supports disk devices in
3332 * kernel context. For user-level testing, we allow it.
3335 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3336 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3337 error = SET_ERROR(ENOTBLK);
3344 if ((error = vdev_open(vd)) == 0 &&
3345 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3346 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3347 vd->vdev_guid) == 0);
3353 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3360 sav->sav_pending = NULL;
3361 sav->sav_npending = 0;
3366 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3370 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3372 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3373 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3374 VDEV_LABEL_SPARE)) != 0) {
3378 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3379 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3380 VDEV_LABEL_L2CACHE));
3384 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3389 if (sav->sav_config != NULL) {
3395 * Generate new dev list by concatentating with the
3398 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3399 &olddevs, &oldndevs) == 0);
3401 newdevs = kmem_alloc(sizeof (void *) *
3402 (ndevs + oldndevs), KM_SLEEP);
3403 for (i = 0; i < oldndevs; i++)
3404 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3406 for (i = 0; i < ndevs; i++)
3407 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3410 VERIFY(nvlist_remove(sav->sav_config, config,
3411 DATA_TYPE_NVLIST_ARRAY) == 0);
3413 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3414 config, newdevs, ndevs + oldndevs) == 0);
3415 for (i = 0; i < oldndevs + ndevs; i++)
3416 nvlist_free(newdevs[i]);
3417 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3420 * Generate a new dev list.
3422 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3424 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3430 * Stop and drop level 2 ARC devices
3433 spa_l2cache_drop(spa_t *spa)
3437 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3439 for (i = 0; i < sav->sav_count; i++) {
3442 vd = sav->sav_vdevs[i];
3445 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3446 pool != 0ULL && l2arc_vdev_present(vd))
3447 l2arc_remove_vdev(vd);
3455 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3459 char *altroot = NULL;
3464 uint64_t txg = TXG_INITIAL;
3465 nvlist_t **spares, **l2cache;
3466 uint_t nspares, nl2cache;
3467 uint64_t version, obj;
3468 boolean_t has_features;
3471 * If this pool already exists, return failure.
3473 mutex_enter(&spa_namespace_lock);
3474 if (spa_lookup(pool) != NULL) {
3475 mutex_exit(&spa_namespace_lock);
3476 return (SET_ERROR(EEXIST));
3480 * Allocate a new spa_t structure.
3482 (void) nvlist_lookup_string(props,
3483 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3484 spa = spa_add(pool, NULL, altroot);
3485 spa_activate(spa, spa_mode_global);
3487 if (props && (error = spa_prop_validate(spa, props))) {
3488 spa_deactivate(spa);
3490 mutex_exit(&spa_namespace_lock);
3494 has_features = B_FALSE;
3495 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3496 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3497 if (zpool_prop_feature(nvpair_name(elem)))
3498 has_features = B_TRUE;
3501 if (has_features || nvlist_lookup_uint64(props,
3502 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3503 version = SPA_VERSION;
3505 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3507 spa->spa_first_txg = txg;
3508 spa->spa_uberblock.ub_txg = txg - 1;
3509 spa->spa_uberblock.ub_version = version;
3510 spa->spa_ubsync = spa->spa_uberblock;
3513 * Create "The Godfather" zio to hold all async IOs
3515 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3516 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3519 * Create the root vdev.
3521 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3523 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3525 ASSERT(error != 0 || rvd != NULL);
3526 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3528 if (error == 0 && !zfs_allocatable_devs(nvroot))
3529 error = SET_ERROR(EINVAL);
3532 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3533 (error = spa_validate_aux(spa, nvroot, txg,
3534 VDEV_ALLOC_ADD)) == 0) {
3535 for (int c = 0; c < rvd->vdev_children; c++) {
3536 vdev_ashift_optimize(rvd->vdev_child[c]);
3537 vdev_metaslab_set_size(rvd->vdev_child[c]);
3538 vdev_expand(rvd->vdev_child[c], txg);
3542 spa_config_exit(spa, SCL_ALL, FTAG);
3546 spa_deactivate(spa);
3548 mutex_exit(&spa_namespace_lock);
3553 * Get the list of spares, if specified.
3555 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3556 &spares, &nspares) == 0) {
3557 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3559 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3560 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3561 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3562 spa_load_spares(spa);
3563 spa_config_exit(spa, SCL_ALL, FTAG);
3564 spa->spa_spares.sav_sync = B_TRUE;
3568 * Get the list of level 2 cache devices, if specified.
3570 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3571 &l2cache, &nl2cache) == 0) {
3572 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3573 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3574 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3575 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3576 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3577 spa_load_l2cache(spa);
3578 spa_config_exit(spa, SCL_ALL, FTAG);
3579 spa->spa_l2cache.sav_sync = B_TRUE;
3582 spa->spa_is_initializing = B_TRUE;
3583 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3584 spa->spa_meta_objset = dp->dp_meta_objset;
3585 spa->spa_is_initializing = B_FALSE;
3588 * Create DDTs (dedup tables).
3592 spa_update_dspace(spa);
3594 tx = dmu_tx_create_assigned(dp, txg);
3597 * Create the pool config object.
3599 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3600 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3601 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3603 if (zap_add(spa->spa_meta_objset,
3604 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3605 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3606 cmn_err(CE_PANIC, "failed to add pool config");
3609 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3610 spa_feature_create_zap_objects(spa, tx);
3612 if (zap_add(spa->spa_meta_objset,
3613 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3614 sizeof (uint64_t), 1, &version, tx) != 0) {
3615 cmn_err(CE_PANIC, "failed to add pool version");
3618 /* Newly created pools with the right version are always deflated. */
3619 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3620 spa->spa_deflate = TRUE;
3621 if (zap_add(spa->spa_meta_objset,
3622 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3623 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3624 cmn_err(CE_PANIC, "failed to add deflate");
3629 * Create the deferred-free bpobj. Turn off compression
3630 * because sync-to-convergence takes longer if the blocksize
3633 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3634 dmu_object_set_compress(spa->spa_meta_objset, obj,
3635 ZIO_COMPRESS_OFF, tx);
3636 if (zap_add(spa->spa_meta_objset,
3637 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3638 sizeof (uint64_t), 1, &obj, tx) != 0) {
3639 cmn_err(CE_PANIC, "failed to add bpobj");
3641 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3642 spa->spa_meta_objset, obj));
3645 * Create the pool's history object.
3647 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3648 spa_history_create_obj(spa, tx);
3651 * Set pool properties.
3653 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3654 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3655 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3656 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3658 if (props != NULL) {
3659 spa_configfile_set(spa, props, B_FALSE);
3660 spa_sync_props(props, tx);
3665 spa->spa_sync_on = B_TRUE;
3666 txg_sync_start(spa->spa_dsl_pool);
3669 * We explicitly wait for the first transaction to complete so that our
3670 * bean counters are appropriately updated.
3672 txg_wait_synced(spa->spa_dsl_pool, txg);
3674 spa_config_sync(spa, B_FALSE, B_TRUE);
3676 spa_history_log_version(spa, "create");
3678 spa->spa_minref = refcount_count(&spa->spa_refcount);
3680 mutex_exit(&spa_namespace_lock);
3688 * Get the root pool information from the root disk, then import the root pool
3689 * during the system boot up time.
3691 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3694 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3697 nvlist_t *nvtop, *nvroot;
3700 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3704 * Add this top-level vdev to the child array.
3706 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3708 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3710 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3713 * Put this pool's top-level vdevs into a root vdev.
3715 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3716 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3717 VDEV_TYPE_ROOT) == 0);
3718 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3719 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3720 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3724 * Replace the existing vdev_tree with the new root vdev in
3725 * this pool's configuration (remove the old, add the new).
3727 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3728 nvlist_free(nvroot);
3733 * Walk the vdev tree and see if we can find a device with "better"
3734 * configuration. A configuration is "better" if the label on that
3735 * device has a more recent txg.
3738 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3740 for (int c = 0; c < vd->vdev_children; c++)
3741 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3743 if (vd->vdev_ops->vdev_op_leaf) {
3747 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3751 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3755 * Do we have a better boot device?
3757 if (label_txg > *txg) {
3766 * Import a root pool.
3768 * For x86. devpath_list will consist of devid and/or physpath name of
3769 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3770 * The GRUB "findroot" command will return the vdev we should boot.
3772 * For Sparc, devpath_list consists the physpath name of the booting device
3773 * no matter the rootpool is a single device pool or a mirrored pool.
3775 * "/pci@1f,0/ide@d/disk@0,0:a"
3778 spa_import_rootpool(char *devpath, char *devid)
3781 vdev_t *rvd, *bvd, *avd = NULL;
3782 nvlist_t *config, *nvtop;
3788 * Read the label from the boot device and generate a configuration.
3790 config = spa_generate_rootconf(devpath, devid, &guid);
3791 #if defined(_OBP) && defined(_KERNEL)
3792 if (config == NULL) {
3793 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3795 get_iscsi_bootpath_phy(devpath);
3796 config = spa_generate_rootconf(devpath, devid, &guid);
3800 if (config == NULL) {
3801 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3803 return (SET_ERROR(EIO));
3806 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3808 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3810 mutex_enter(&spa_namespace_lock);
3811 if ((spa = spa_lookup(pname)) != NULL) {
3813 * Remove the existing root pool from the namespace so that we
3814 * can replace it with the correct config we just read in.
3819 spa = spa_add(pname, config, NULL);
3820 spa->spa_is_root = B_TRUE;
3821 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3824 * Build up a vdev tree based on the boot device's label config.
3826 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3828 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3829 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3830 VDEV_ALLOC_ROOTPOOL);
3831 spa_config_exit(spa, SCL_ALL, FTAG);
3833 mutex_exit(&spa_namespace_lock);
3834 nvlist_free(config);
3835 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3841 * Get the boot vdev.
3843 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3844 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3845 (u_longlong_t)guid);
3846 error = SET_ERROR(ENOENT);
3851 * Determine if there is a better boot device.
3854 spa_alt_rootvdev(rvd, &avd, &txg);
3856 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3857 "try booting from '%s'", avd->vdev_path);
3858 error = SET_ERROR(EINVAL);
3863 * If the boot device is part of a spare vdev then ensure that
3864 * we're booting off the active spare.
3866 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3867 !bvd->vdev_isspare) {
3868 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3869 "try booting from '%s'",
3871 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3872 error = SET_ERROR(EINVAL);
3878 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3880 spa_config_exit(spa, SCL_ALL, FTAG);
3881 mutex_exit(&spa_namespace_lock);
3883 nvlist_free(config);
3889 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3893 spa_generate_rootconf(const char *name)
3895 nvlist_t **configs, **tops;
3897 nvlist_t *best_cfg, *nvtop, *nvroot;
3906 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3909 ASSERT3U(count, !=, 0);
3911 for (i = 0; i < count; i++) {
3914 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
3916 if (txg > best_txg) {
3918 best_cfg = configs[i];
3923 * Multi-vdev root pool configuration discovery is not supported yet.
3926 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3928 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
3931 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
3932 for (i = 0; i < nchildren; i++) {
3935 if (configs[i] == NULL)
3937 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
3939 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
3941 for (i = 0; holes != NULL && i < nholes; i++) {
3944 if (tops[holes[i]] != NULL)
3946 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
3947 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
3948 VDEV_TYPE_HOLE) == 0);
3949 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
3951 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
3954 for (i = 0; i < nchildren; i++) {
3955 if (tops[i] != NULL)
3957 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
3958 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
3959 VDEV_TYPE_MISSING) == 0);
3960 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
3962 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
3967 * Create pool config based on the best vdev config.
3969 nvlist_dup(best_cfg, &config, KM_SLEEP);
3972 * Put this pool's top-level vdevs into a root vdev.
3974 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3976 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3977 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3978 VDEV_TYPE_ROOT) == 0);
3979 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3980 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3981 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3982 tops, nchildren) == 0);
3985 * Replace the existing vdev_tree with the new root vdev in
3986 * this pool's configuration (remove the old, add the new).
3988 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3991 * Drop vdev config elements that should not be present at pool level.
3993 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
3994 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
3996 for (i = 0; i < count; i++)
3997 nvlist_free(configs[i]);
3998 kmem_free(configs, count * sizeof(void *));
3999 for (i = 0; i < nchildren; i++)
4000 nvlist_free(tops[i]);
4001 kmem_free(tops, nchildren * sizeof(void *));
4002 nvlist_free(nvroot);
4007 spa_import_rootpool(const char *name)
4010 vdev_t *rvd, *bvd, *avd = NULL;
4011 nvlist_t *config, *nvtop;
4017 * Read the label from the boot device and generate a configuration.
4019 config = spa_generate_rootconf(name);
4021 mutex_enter(&spa_namespace_lock);
4022 if (config != NULL) {
4023 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4024 &pname) == 0 && strcmp(name, pname) == 0);
4025 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4028 if ((spa = spa_lookup(pname)) != NULL) {
4030 * Remove the existing root pool from the namespace so
4031 * that we can replace it with the correct config
4036 spa = spa_add(pname, config, NULL);
4039 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4040 * via spa_version().
4042 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4043 &spa->spa_ubsync.ub_version) != 0)
4044 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4045 } else if ((spa = spa_lookup(name)) == NULL) {
4046 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4050 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4052 spa->spa_is_root = B_TRUE;
4053 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4056 * Build up a vdev tree based on the boot device's label config.
4058 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4060 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4061 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4062 VDEV_ALLOC_ROOTPOOL);
4063 spa_config_exit(spa, SCL_ALL, FTAG);
4065 mutex_exit(&spa_namespace_lock);
4066 nvlist_free(config);
4067 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4072 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4074 spa_config_exit(spa, SCL_ALL, FTAG);
4075 mutex_exit(&spa_namespace_lock);
4077 nvlist_free(config);
4085 * Import a non-root pool into the system.
4088 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4091 char *altroot = NULL;
4092 spa_load_state_t state = SPA_LOAD_IMPORT;
4093 zpool_rewind_policy_t policy;
4094 uint64_t mode = spa_mode_global;
4095 uint64_t readonly = B_FALSE;
4098 nvlist_t **spares, **l2cache;
4099 uint_t nspares, nl2cache;
4102 * If a pool with this name exists, return failure.
4104 mutex_enter(&spa_namespace_lock);
4105 if (spa_lookup(pool) != NULL) {
4106 mutex_exit(&spa_namespace_lock);
4107 return (SET_ERROR(EEXIST));
4111 * Create and initialize the spa structure.
4113 (void) nvlist_lookup_string(props,
4114 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4115 (void) nvlist_lookup_uint64(props,
4116 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4119 spa = spa_add(pool, config, altroot);
4120 spa->spa_import_flags = flags;
4123 * Verbatim import - Take a pool and insert it into the namespace
4124 * as if it had been loaded at boot.
4126 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4128 spa_configfile_set(spa, props, B_FALSE);
4130 spa_config_sync(spa, B_FALSE, B_TRUE);
4132 mutex_exit(&spa_namespace_lock);
4136 spa_activate(spa, mode);
4139 * Don't start async tasks until we know everything is healthy.
4141 spa_async_suspend(spa);
4143 zpool_get_rewind_policy(config, &policy);
4144 if (policy.zrp_request & ZPOOL_DO_REWIND)
4145 state = SPA_LOAD_RECOVER;
4148 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4149 * because the user-supplied config is actually the one to trust when
4152 if (state != SPA_LOAD_RECOVER)
4153 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4155 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4156 policy.zrp_request);
4159 * Propagate anything learned while loading the pool and pass it
4160 * back to caller (i.e. rewind info, missing devices, etc).
4162 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4163 spa->spa_load_info) == 0);
4165 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4167 * Toss any existing sparelist, as it doesn't have any validity
4168 * anymore, and conflicts with spa_has_spare().
4170 if (spa->spa_spares.sav_config) {
4171 nvlist_free(spa->spa_spares.sav_config);
4172 spa->spa_spares.sav_config = NULL;
4173 spa_load_spares(spa);
4175 if (spa->spa_l2cache.sav_config) {
4176 nvlist_free(spa->spa_l2cache.sav_config);
4177 spa->spa_l2cache.sav_config = NULL;
4178 spa_load_l2cache(spa);
4181 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4184 error = spa_validate_aux(spa, nvroot, -1ULL,
4187 error = spa_validate_aux(spa, nvroot, -1ULL,
4188 VDEV_ALLOC_L2CACHE);
4189 spa_config_exit(spa, SCL_ALL, FTAG);
4192 spa_configfile_set(spa, props, B_FALSE);
4194 if (error != 0 || (props && spa_writeable(spa) &&
4195 (error = spa_prop_set(spa, props)))) {
4197 spa_deactivate(spa);
4199 mutex_exit(&spa_namespace_lock);
4203 spa_async_resume(spa);
4206 * Override any spares and level 2 cache devices as specified by
4207 * the user, as these may have correct device names/devids, etc.
4209 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4210 &spares, &nspares) == 0) {
4211 if (spa->spa_spares.sav_config)
4212 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4213 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4215 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4216 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4217 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4218 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4219 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4220 spa_load_spares(spa);
4221 spa_config_exit(spa, SCL_ALL, FTAG);
4222 spa->spa_spares.sav_sync = B_TRUE;
4224 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4225 &l2cache, &nl2cache) == 0) {
4226 if (spa->spa_l2cache.sav_config)
4227 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4228 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4230 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4231 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4232 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4233 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4234 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4235 spa_load_l2cache(spa);
4236 spa_config_exit(spa, SCL_ALL, FTAG);
4237 spa->spa_l2cache.sav_sync = B_TRUE;
4241 * Check for any removed devices.
4243 if (spa->spa_autoreplace) {
4244 spa_aux_check_removed(&spa->spa_spares);
4245 spa_aux_check_removed(&spa->spa_l2cache);
4248 if (spa_writeable(spa)) {
4250 * Update the config cache to include the newly-imported pool.
4252 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4256 * It's possible that the pool was expanded while it was exported.
4257 * We kick off an async task to handle this for us.
4259 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4261 mutex_exit(&spa_namespace_lock);
4262 spa_history_log_version(spa, "import");
4266 zvol_create_minors(pool);
4273 spa_tryimport(nvlist_t *tryconfig)
4275 nvlist_t *config = NULL;
4281 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4284 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4288 * Create and initialize the spa structure.
4290 mutex_enter(&spa_namespace_lock);
4291 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4292 spa_activate(spa, FREAD);
4295 * Pass off the heavy lifting to spa_load().
4296 * Pass TRUE for mosconfig because the user-supplied config
4297 * is actually the one to trust when doing an import.
4299 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4302 * If 'tryconfig' was at least parsable, return the current config.
4304 if (spa->spa_root_vdev != NULL) {
4305 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4306 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4308 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4310 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4311 spa->spa_uberblock.ub_timestamp) == 0);
4312 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4313 spa->spa_load_info) == 0);
4316 * If the bootfs property exists on this pool then we
4317 * copy it out so that external consumers can tell which
4318 * pools are bootable.
4320 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4321 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4324 * We have to play games with the name since the
4325 * pool was opened as TRYIMPORT_NAME.
4327 if (dsl_dsobj_to_dsname(spa_name(spa),
4328 spa->spa_bootfs, tmpname) == 0) {
4330 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4332 cp = strchr(tmpname, '/');
4334 (void) strlcpy(dsname, tmpname,
4337 (void) snprintf(dsname, MAXPATHLEN,
4338 "%s/%s", poolname, ++cp);
4340 VERIFY(nvlist_add_string(config,
4341 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4342 kmem_free(dsname, MAXPATHLEN);
4344 kmem_free(tmpname, MAXPATHLEN);
4348 * Add the list of hot spares and level 2 cache devices.
4350 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4351 spa_add_spares(spa, config);
4352 spa_add_l2cache(spa, config);
4353 spa_config_exit(spa, SCL_CONFIG, FTAG);
4357 spa_deactivate(spa);
4359 mutex_exit(&spa_namespace_lock);
4365 * Pool export/destroy
4367 * The act of destroying or exporting a pool is very simple. We make sure there
4368 * is no more pending I/O and any references to the pool are gone. Then, we
4369 * update the pool state and sync all the labels to disk, removing the
4370 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4371 * we don't sync the labels or remove the configuration cache.
4374 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4375 boolean_t force, boolean_t hardforce)
4382 if (!(spa_mode_global & FWRITE))
4383 return (SET_ERROR(EROFS));
4385 mutex_enter(&spa_namespace_lock);
4386 if ((spa = spa_lookup(pool)) == NULL) {
4387 mutex_exit(&spa_namespace_lock);
4388 return (SET_ERROR(ENOENT));
4392 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4393 * reacquire the namespace lock, and see if we can export.
4395 spa_open_ref(spa, FTAG);
4396 mutex_exit(&spa_namespace_lock);
4397 spa_async_suspend(spa);
4398 mutex_enter(&spa_namespace_lock);
4399 spa_close(spa, FTAG);
4402 * The pool will be in core if it's openable,
4403 * in which case we can modify its state.
4405 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4407 * Objsets may be open only because they're dirty, so we
4408 * have to force it to sync before checking spa_refcnt.
4410 txg_wait_synced(spa->spa_dsl_pool, 0);
4413 * A pool cannot be exported or destroyed if there are active
4414 * references. If we are resetting a pool, allow references by
4415 * fault injection handlers.
4417 if (!spa_refcount_zero(spa) ||
4418 (spa->spa_inject_ref != 0 &&
4419 new_state != POOL_STATE_UNINITIALIZED)) {
4420 spa_async_resume(spa);
4421 mutex_exit(&spa_namespace_lock);
4422 return (SET_ERROR(EBUSY));
4426 * A pool cannot be exported if it has an active shared spare.
4427 * This is to prevent other pools stealing the active spare
4428 * from an exported pool. At user's own will, such pool can
4429 * be forcedly exported.
4431 if (!force && new_state == POOL_STATE_EXPORTED &&
4432 spa_has_active_shared_spare(spa)) {
4433 spa_async_resume(spa);
4434 mutex_exit(&spa_namespace_lock);
4435 return (SET_ERROR(EXDEV));
4439 * We want this to be reflected on every label,
4440 * so mark them all dirty. spa_unload() will do the
4441 * final sync that pushes these changes out.
4443 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4444 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4445 spa->spa_state = new_state;
4446 spa->spa_final_txg = spa_last_synced_txg(spa) +
4448 vdev_config_dirty(spa->spa_root_vdev);
4449 spa_config_exit(spa, SCL_ALL, FTAG);
4453 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4455 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4457 spa_deactivate(spa);
4460 if (oldconfig && spa->spa_config)
4461 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4463 if (new_state != POOL_STATE_UNINITIALIZED) {
4465 spa_config_sync(spa, B_TRUE, B_TRUE);
4468 mutex_exit(&spa_namespace_lock);
4474 * Destroy a storage pool.
4477 spa_destroy(char *pool)
4479 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4484 * Export a storage pool.
4487 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4488 boolean_t hardforce)
4490 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4495 * Similar to spa_export(), this unloads the spa_t without actually removing it
4496 * from the namespace in any way.
4499 spa_reset(char *pool)
4501 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4506 * ==========================================================================
4507 * Device manipulation
4508 * ==========================================================================
4512 * Add a device to a storage pool.
4515 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4519 vdev_t *rvd = spa->spa_root_vdev;
4521 nvlist_t **spares, **l2cache;
4522 uint_t nspares, nl2cache;
4524 ASSERT(spa_writeable(spa));
4526 txg = spa_vdev_enter(spa);
4528 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4529 VDEV_ALLOC_ADD)) != 0)
4530 return (spa_vdev_exit(spa, NULL, txg, error));
4532 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4534 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4538 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4542 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4543 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4545 if (vd->vdev_children != 0 &&
4546 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4547 return (spa_vdev_exit(spa, vd, txg, error));
4550 * We must validate the spares and l2cache devices after checking the
4551 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4553 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4554 return (spa_vdev_exit(spa, vd, txg, error));
4557 * Transfer each new top-level vdev from vd to rvd.
4559 for (int c = 0; c < vd->vdev_children; c++) {
4562 * Set the vdev id to the first hole, if one exists.
4564 for (id = 0; id < rvd->vdev_children; id++) {
4565 if (rvd->vdev_child[id]->vdev_ishole) {
4566 vdev_free(rvd->vdev_child[id]);
4570 tvd = vd->vdev_child[c];
4571 vdev_remove_child(vd, tvd);
4573 vdev_add_child(rvd, tvd);
4574 vdev_config_dirty(tvd);
4578 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4579 ZPOOL_CONFIG_SPARES);
4580 spa_load_spares(spa);
4581 spa->spa_spares.sav_sync = B_TRUE;
4584 if (nl2cache != 0) {
4585 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4586 ZPOOL_CONFIG_L2CACHE);
4587 spa_load_l2cache(spa);
4588 spa->spa_l2cache.sav_sync = B_TRUE;
4592 * We have to be careful when adding new vdevs to an existing pool.
4593 * If other threads start allocating from these vdevs before we
4594 * sync the config cache, and we lose power, then upon reboot we may
4595 * fail to open the pool because there are DVAs that the config cache
4596 * can't translate. Therefore, we first add the vdevs without
4597 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4598 * and then let spa_config_update() initialize the new metaslabs.
4600 * spa_load() checks for added-but-not-initialized vdevs, so that
4601 * if we lose power at any point in this sequence, the remaining
4602 * steps will be completed the next time we load the pool.
4604 (void) spa_vdev_exit(spa, vd, txg, 0);
4606 mutex_enter(&spa_namespace_lock);
4607 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4608 mutex_exit(&spa_namespace_lock);
4614 * Attach a device to a mirror. The arguments are the path to any device
4615 * in the mirror, and the nvroot for the new device. If the path specifies
4616 * a device that is not mirrored, we automatically insert the mirror vdev.
4618 * If 'replacing' is specified, the new device is intended to replace the
4619 * existing device; in this case the two devices are made into their own
4620 * mirror using the 'replacing' vdev, which is functionally identical to
4621 * the mirror vdev (it actually reuses all the same ops) but has a few
4622 * extra rules: you can't attach to it after it's been created, and upon
4623 * completion of resilvering, the first disk (the one being replaced)
4624 * is automatically detached.
4627 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4629 uint64_t txg, dtl_max_txg;
4630 vdev_t *rvd = spa->spa_root_vdev;
4631 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4633 char *oldvdpath, *newvdpath;
4637 ASSERT(spa_writeable(spa));
4639 txg = spa_vdev_enter(spa);
4641 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4644 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4646 if (!oldvd->vdev_ops->vdev_op_leaf)
4647 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4649 pvd = oldvd->vdev_parent;
4651 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4652 VDEV_ALLOC_ATTACH)) != 0)
4653 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4655 if (newrootvd->vdev_children != 1)
4656 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4658 newvd = newrootvd->vdev_child[0];
4660 if (!newvd->vdev_ops->vdev_op_leaf)
4661 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4663 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4664 return (spa_vdev_exit(spa, newrootvd, txg, error));
4667 * Spares can't replace logs
4669 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4670 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4674 * For attach, the only allowable parent is a mirror or the root
4677 if (pvd->vdev_ops != &vdev_mirror_ops &&
4678 pvd->vdev_ops != &vdev_root_ops)
4679 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4681 pvops = &vdev_mirror_ops;
4684 * Active hot spares can only be replaced by inactive hot
4687 if (pvd->vdev_ops == &vdev_spare_ops &&
4688 oldvd->vdev_isspare &&
4689 !spa_has_spare(spa, newvd->vdev_guid))
4690 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4693 * If the source is a hot spare, and the parent isn't already a
4694 * spare, then we want to create a new hot spare. Otherwise, we
4695 * want to create a replacing vdev. The user is not allowed to
4696 * attach to a spared vdev child unless the 'isspare' state is
4697 * the same (spare replaces spare, non-spare replaces
4700 if (pvd->vdev_ops == &vdev_replacing_ops &&
4701 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4702 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4703 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4704 newvd->vdev_isspare != oldvd->vdev_isspare) {
4705 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4708 if (newvd->vdev_isspare)
4709 pvops = &vdev_spare_ops;
4711 pvops = &vdev_replacing_ops;
4715 * Make sure the new device is big enough.
4717 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4718 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4721 * The new device cannot have a higher alignment requirement
4722 * than the top-level vdev.
4724 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4725 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4728 * If this is an in-place replacement, update oldvd's path and devid
4729 * to make it distinguishable from newvd, and unopenable from now on.
4731 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4732 spa_strfree(oldvd->vdev_path);
4733 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4735 (void) sprintf(oldvd->vdev_path, "%s/%s",
4736 newvd->vdev_path, "old");
4737 if (oldvd->vdev_devid != NULL) {
4738 spa_strfree(oldvd->vdev_devid);
4739 oldvd->vdev_devid = NULL;
4743 /* mark the device being resilvered */
4744 newvd->vdev_resilver_txg = txg;
4747 * If the parent is not a mirror, or if we're replacing, insert the new
4748 * mirror/replacing/spare vdev above oldvd.
4750 if (pvd->vdev_ops != pvops)
4751 pvd = vdev_add_parent(oldvd, pvops);
4753 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4754 ASSERT(pvd->vdev_ops == pvops);
4755 ASSERT(oldvd->vdev_parent == pvd);
4758 * Extract the new device from its root and add it to pvd.
4760 vdev_remove_child(newrootvd, newvd);
4761 newvd->vdev_id = pvd->vdev_children;
4762 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4763 vdev_add_child(pvd, newvd);
4765 tvd = newvd->vdev_top;
4766 ASSERT(pvd->vdev_top == tvd);
4767 ASSERT(tvd->vdev_parent == rvd);
4769 vdev_config_dirty(tvd);
4772 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4773 * for any dmu_sync-ed blocks. It will propagate upward when
4774 * spa_vdev_exit() calls vdev_dtl_reassess().
4776 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4778 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4779 dtl_max_txg - TXG_INITIAL);
4781 if (newvd->vdev_isspare) {
4782 spa_spare_activate(newvd);
4783 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4786 oldvdpath = spa_strdup(oldvd->vdev_path);
4787 newvdpath = spa_strdup(newvd->vdev_path);
4788 newvd_isspare = newvd->vdev_isspare;
4791 * Mark newvd's DTL dirty in this txg.
4793 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4796 * Schedule the resilver to restart in the future. We do this to
4797 * ensure that dmu_sync-ed blocks have been stitched into the
4798 * respective datasets.
4800 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4805 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4807 spa_history_log_internal(spa, "vdev attach", NULL,
4808 "%s vdev=%s %s vdev=%s",
4809 replacing && newvd_isspare ? "spare in" :
4810 replacing ? "replace" : "attach", newvdpath,
4811 replacing ? "for" : "to", oldvdpath);
4813 spa_strfree(oldvdpath);
4814 spa_strfree(newvdpath);
4816 if (spa->spa_bootfs)
4817 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4823 * Detach a device from a mirror or replacing vdev.
4825 * If 'replace_done' is specified, only detach if the parent
4826 * is a replacing vdev.
4829 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4833 vdev_t *rvd = spa->spa_root_vdev;
4834 vdev_t *vd, *pvd, *cvd, *tvd;
4835 boolean_t unspare = B_FALSE;
4836 uint64_t unspare_guid = 0;
4839 ASSERT(spa_writeable(spa));
4841 txg = spa_vdev_enter(spa);
4843 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4846 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4848 if (!vd->vdev_ops->vdev_op_leaf)
4849 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4851 pvd = vd->vdev_parent;
4854 * If the parent/child relationship is not as expected, don't do it.
4855 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4856 * vdev that's replacing B with C. The user's intent in replacing
4857 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4858 * the replace by detaching C, the expected behavior is to end up
4859 * M(A,B). But suppose that right after deciding to detach C,
4860 * the replacement of B completes. We would have M(A,C), and then
4861 * ask to detach C, which would leave us with just A -- not what
4862 * the user wanted. To prevent this, we make sure that the
4863 * parent/child relationship hasn't changed -- in this example,
4864 * that C's parent is still the replacing vdev R.
4866 if (pvd->vdev_guid != pguid && pguid != 0)
4867 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4870 * Only 'replacing' or 'spare' vdevs can be replaced.
4872 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4873 pvd->vdev_ops != &vdev_spare_ops)
4874 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4876 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4877 spa_version(spa) >= SPA_VERSION_SPARES);
4880 * Only mirror, replacing, and spare vdevs support detach.
4882 if (pvd->vdev_ops != &vdev_replacing_ops &&
4883 pvd->vdev_ops != &vdev_mirror_ops &&
4884 pvd->vdev_ops != &vdev_spare_ops)
4885 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4888 * If this device has the only valid copy of some data,
4889 * we cannot safely detach it.
4891 if (vdev_dtl_required(vd))
4892 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4894 ASSERT(pvd->vdev_children >= 2);
4897 * If we are detaching the second disk from a replacing vdev, then
4898 * check to see if we changed the original vdev's path to have "/old"
4899 * at the end in spa_vdev_attach(). If so, undo that change now.
4901 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4902 vd->vdev_path != NULL) {
4903 size_t len = strlen(vd->vdev_path);
4905 for (int c = 0; c < pvd->vdev_children; c++) {
4906 cvd = pvd->vdev_child[c];
4908 if (cvd == vd || cvd->vdev_path == NULL)
4911 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4912 strcmp(cvd->vdev_path + len, "/old") == 0) {
4913 spa_strfree(cvd->vdev_path);
4914 cvd->vdev_path = spa_strdup(vd->vdev_path);
4921 * If we are detaching the original disk from a spare, then it implies
4922 * that the spare should become a real disk, and be removed from the
4923 * active spare list for the pool.
4925 if (pvd->vdev_ops == &vdev_spare_ops &&
4927 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4931 * Erase the disk labels so the disk can be used for other things.
4932 * This must be done after all other error cases are handled,
4933 * but before we disembowel vd (so we can still do I/O to it).
4934 * But if we can't do it, don't treat the error as fatal --
4935 * it may be that the unwritability of the disk is the reason
4936 * it's being detached!
4938 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4941 * Remove vd from its parent and compact the parent's children.
4943 vdev_remove_child(pvd, vd);
4944 vdev_compact_children(pvd);
4947 * Remember one of the remaining children so we can get tvd below.
4949 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4952 * If we need to remove the remaining child from the list of hot spares,
4953 * do it now, marking the vdev as no longer a spare in the process.
4954 * We must do this before vdev_remove_parent(), because that can
4955 * change the GUID if it creates a new toplevel GUID. For a similar
4956 * reason, we must remove the spare now, in the same txg as the detach;
4957 * otherwise someone could attach a new sibling, change the GUID, and
4958 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4961 ASSERT(cvd->vdev_isspare);
4962 spa_spare_remove(cvd);
4963 unspare_guid = cvd->vdev_guid;
4964 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4965 cvd->vdev_unspare = B_TRUE;
4969 * If the parent mirror/replacing vdev only has one child,
4970 * the parent is no longer needed. Remove it from the tree.
4972 if (pvd->vdev_children == 1) {
4973 if (pvd->vdev_ops == &vdev_spare_ops)
4974 cvd->vdev_unspare = B_FALSE;
4975 vdev_remove_parent(cvd);
4980 * We don't set tvd until now because the parent we just removed
4981 * may have been the previous top-level vdev.
4983 tvd = cvd->vdev_top;
4984 ASSERT(tvd->vdev_parent == rvd);
4987 * Reevaluate the parent vdev state.
4989 vdev_propagate_state(cvd);
4992 * If the 'autoexpand' property is set on the pool then automatically
4993 * try to expand the size of the pool. For example if the device we
4994 * just detached was smaller than the others, it may be possible to
4995 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4996 * first so that we can obtain the updated sizes of the leaf vdevs.
4998 if (spa->spa_autoexpand) {
5000 vdev_expand(tvd, txg);
5003 vdev_config_dirty(tvd);
5006 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5007 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5008 * But first make sure we're not on any *other* txg's DTL list, to
5009 * prevent vd from being accessed after it's freed.
5011 vdpath = spa_strdup(vd->vdev_path);
5012 for (int t = 0; t < TXG_SIZE; t++)
5013 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5014 vd->vdev_detached = B_TRUE;
5015 vdev_dirty(tvd, VDD_DTL, vd, txg);
5017 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5019 /* hang on to the spa before we release the lock */
5020 spa_open_ref(spa, FTAG);
5022 error = spa_vdev_exit(spa, vd, txg, 0);
5024 spa_history_log_internal(spa, "detach", NULL,
5026 spa_strfree(vdpath);
5029 * If this was the removal of the original device in a hot spare vdev,
5030 * then we want to go through and remove the device from the hot spare
5031 * list of every other pool.
5034 spa_t *altspa = NULL;
5036 mutex_enter(&spa_namespace_lock);
5037 while ((altspa = spa_next(altspa)) != NULL) {
5038 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5042 spa_open_ref(altspa, FTAG);
5043 mutex_exit(&spa_namespace_lock);
5044 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5045 mutex_enter(&spa_namespace_lock);
5046 spa_close(altspa, FTAG);
5048 mutex_exit(&spa_namespace_lock);
5050 /* search the rest of the vdevs for spares to remove */
5051 spa_vdev_resilver_done(spa);
5054 /* all done with the spa; OK to release */
5055 mutex_enter(&spa_namespace_lock);
5056 spa_close(spa, FTAG);
5057 mutex_exit(&spa_namespace_lock);
5063 * Split a set of devices from their mirrors, and create a new pool from them.
5066 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5067 nvlist_t *props, boolean_t exp)
5070 uint64_t txg, *glist;
5072 uint_t c, children, lastlog;
5073 nvlist_t **child, *nvl, *tmp;
5075 char *altroot = NULL;
5076 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5077 boolean_t activate_slog;
5079 ASSERT(spa_writeable(spa));
5081 txg = spa_vdev_enter(spa);
5083 /* clear the log and flush everything up to now */
5084 activate_slog = spa_passivate_log(spa);
5085 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5086 error = spa_offline_log(spa);
5087 txg = spa_vdev_config_enter(spa);
5090 spa_activate_log(spa);
5093 return (spa_vdev_exit(spa, NULL, txg, error));
5095 /* check new spa name before going any further */
5096 if (spa_lookup(newname) != NULL)
5097 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5100 * scan through all the children to ensure they're all mirrors
5102 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5103 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5105 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5107 /* first, check to ensure we've got the right child count */
5108 rvd = spa->spa_root_vdev;
5110 for (c = 0; c < rvd->vdev_children; c++) {
5111 vdev_t *vd = rvd->vdev_child[c];
5113 /* don't count the holes & logs as children */
5114 if (vd->vdev_islog || vd->vdev_ishole) {
5122 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5123 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5125 /* next, ensure no spare or cache devices are part of the split */
5126 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5127 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5128 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5130 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5131 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5133 /* then, loop over each vdev and validate it */
5134 for (c = 0; c < children; c++) {
5135 uint64_t is_hole = 0;
5137 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5141 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5142 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5145 error = SET_ERROR(EINVAL);
5150 /* which disk is going to be split? */
5151 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5153 error = SET_ERROR(EINVAL);
5157 /* look it up in the spa */
5158 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5159 if (vml[c] == NULL) {
5160 error = SET_ERROR(ENODEV);
5164 /* make sure there's nothing stopping the split */
5165 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5166 vml[c]->vdev_islog ||
5167 vml[c]->vdev_ishole ||
5168 vml[c]->vdev_isspare ||
5169 vml[c]->vdev_isl2cache ||
5170 !vdev_writeable(vml[c]) ||
5171 vml[c]->vdev_children != 0 ||
5172 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5173 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5174 error = SET_ERROR(EINVAL);
5178 if (vdev_dtl_required(vml[c])) {
5179 error = SET_ERROR(EBUSY);
5183 /* we need certain info from the top level */
5184 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5185 vml[c]->vdev_top->vdev_ms_array) == 0);
5186 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5187 vml[c]->vdev_top->vdev_ms_shift) == 0);
5188 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5189 vml[c]->vdev_top->vdev_asize) == 0);
5190 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5191 vml[c]->vdev_top->vdev_ashift) == 0);
5195 kmem_free(vml, children * sizeof (vdev_t *));
5196 kmem_free(glist, children * sizeof (uint64_t));
5197 return (spa_vdev_exit(spa, NULL, txg, error));
5200 /* stop writers from using the disks */
5201 for (c = 0; c < children; c++) {
5203 vml[c]->vdev_offline = B_TRUE;
5205 vdev_reopen(spa->spa_root_vdev);
5208 * Temporarily record the splitting vdevs in the spa config. This
5209 * will disappear once the config is regenerated.
5211 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5212 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5213 glist, children) == 0);
5214 kmem_free(glist, children * sizeof (uint64_t));
5216 mutex_enter(&spa->spa_props_lock);
5217 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5219 mutex_exit(&spa->spa_props_lock);
5220 spa->spa_config_splitting = nvl;
5221 vdev_config_dirty(spa->spa_root_vdev);
5223 /* configure and create the new pool */
5224 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5225 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5226 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5227 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5228 spa_version(spa)) == 0);
5229 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5230 spa->spa_config_txg) == 0);
5231 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5232 spa_generate_guid(NULL)) == 0);
5233 (void) nvlist_lookup_string(props,
5234 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5236 /* add the new pool to the namespace */
5237 newspa = spa_add(newname, config, altroot);
5238 newspa->spa_config_txg = spa->spa_config_txg;
5239 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5241 /* release the spa config lock, retaining the namespace lock */
5242 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5244 if (zio_injection_enabled)
5245 zio_handle_panic_injection(spa, FTAG, 1);
5247 spa_activate(newspa, spa_mode_global);
5248 spa_async_suspend(newspa);
5251 /* mark that we are creating new spa by splitting */
5252 newspa->spa_splitting_newspa = B_TRUE;
5254 /* create the new pool from the disks of the original pool */
5255 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5257 newspa->spa_splitting_newspa = B_FALSE;
5262 /* if that worked, generate a real config for the new pool */
5263 if (newspa->spa_root_vdev != NULL) {
5264 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5265 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5266 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5267 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5268 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5273 if (props != NULL) {
5274 spa_configfile_set(newspa, props, B_FALSE);
5275 error = spa_prop_set(newspa, props);
5280 /* flush everything */
5281 txg = spa_vdev_config_enter(newspa);
5282 vdev_config_dirty(newspa->spa_root_vdev);
5283 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5285 if (zio_injection_enabled)
5286 zio_handle_panic_injection(spa, FTAG, 2);
5288 spa_async_resume(newspa);
5290 /* finally, update the original pool's config */
5291 txg = spa_vdev_config_enter(spa);
5292 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5293 error = dmu_tx_assign(tx, TXG_WAIT);
5296 for (c = 0; c < children; c++) {
5297 if (vml[c] != NULL) {
5300 spa_history_log_internal(spa, "detach", tx,
5301 "vdev=%s", vml[c]->vdev_path);
5305 vdev_config_dirty(spa->spa_root_vdev);
5306 spa->spa_config_splitting = NULL;
5310 (void) spa_vdev_exit(spa, NULL, txg, 0);
5312 if (zio_injection_enabled)
5313 zio_handle_panic_injection(spa, FTAG, 3);
5315 /* split is complete; log a history record */
5316 spa_history_log_internal(newspa, "split", NULL,
5317 "from pool %s", spa_name(spa));
5319 kmem_free(vml, children * sizeof (vdev_t *));
5321 /* if we're not going to mount the filesystems in userland, export */
5323 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5330 spa_deactivate(newspa);
5333 txg = spa_vdev_config_enter(spa);
5335 /* re-online all offlined disks */
5336 for (c = 0; c < children; c++) {
5338 vml[c]->vdev_offline = B_FALSE;
5340 vdev_reopen(spa->spa_root_vdev);
5342 nvlist_free(spa->spa_config_splitting);
5343 spa->spa_config_splitting = NULL;
5344 (void) spa_vdev_exit(spa, NULL, txg, error);
5346 kmem_free(vml, children * sizeof (vdev_t *));
5351 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5353 for (int i = 0; i < count; i++) {
5356 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5359 if (guid == target_guid)
5367 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5368 nvlist_t *dev_to_remove)
5370 nvlist_t **newdev = NULL;
5373 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5375 for (int i = 0, j = 0; i < count; i++) {
5376 if (dev[i] == dev_to_remove)
5378 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5381 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5382 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5384 for (int i = 0; i < count - 1; i++)
5385 nvlist_free(newdev[i]);
5388 kmem_free(newdev, (count - 1) * sizeof (void *));
5392 * Evacuate the device.
5395 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5400 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5401 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5402 ASSERT(vd == vd->vdev_top);
5405 * Evacuate the device. We don't hold the config lock as writer
5406 * since we need to do I/O but we do keep the
5407 * spa_namespace_lock held. Once this completes the device
5408 * should no longer have any blocks allocated on it.
5410 if (vd->vdev_islog) {
5411 if (vd->vdev_stat.vs_alloc != 0)
5412 error = spa_offline_log(spa);
5414 error = SET_ERROR(ENOTSUP);
5421 * The evacuation succeeded. Remove any remaining MOS metadata
5422 * associated with this vdev, and wait for these changes to sync.
5424 ASSERT0(vd->vdev_stat.vs_alloc);
5425 txg = spa_vdev_config_enter(spa);
5426 vd->vdev_removing = B_TRUE;
5427 vdev_dirty_leaves(vd, VDD_DTL, txg);
5428 vdev_config_dirty(vd);
5429 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5435 * Complete the removal by cleaning up the namespace.
5438 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5440 vdev_t *rvd = spa->spa_root_vdev;
5441 uint64_t id = vd->vdev_id;
5442 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5444 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5445 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5446 ASSERT(vd == vd->vdev_top);
5449 * Only remove any devices which are empty.
5451 if (vd->vdev_stat.vs_alloc != 0)
5454 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5456 if (list_link_active(&vd->vdev_state_dirty_node))
5457 vdev_state_clean(vd);
5458 if (list_link_active(&vd->vdev_config_dirty_node))
5459 vdev_config_clean(vd);
5464 vdev_compact_children(rvd);
5466 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5467 vdev_add_child(rvd, vd);
5469 vdev_config_dirty(rvd);
5472 * Reassess the health of our root vdev.
5478 * Remove a device from the pool -
5480 * Removing a device from the vdev namespace requires several steps
5481 * and can take a significant amount of time. As a result we use
5482 * the spa_vdev_config_[enter/exit] functions which allow us to
5483 * grab and release the spa_config_lock while still holding the namespace
5484 * lock. During each step the configuration is synced out.
5486 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5490 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5493 metaslab_group_t *mg;
5494 nvlist_t **spares, **l2cache, *nv;
5496 uint_t nspares, nl2cache;
5498 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5500 ASSERT(spa_writeable(spa));
5503 txg = spa_vdev_enter(spa);
5505 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5507 if (spa->spa_spares.sav_vdevs != NULL &&
5508 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5509 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5510 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5512 * Only remove the hot spare if it's not currently in use
5515 if (vd == NULL || unspare) {
5516 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5517 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5518 spa_load_spares(spa);
5519 spa->spa_spares.sav_sync = B_TRUE;
5521 error = SET_ERROR(EBUSY);
5523 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5524 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5525 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5526 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5528 * Cache devices can always be removed.
5530 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5531 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5532 spa_load_l2cache(spa);
5533 spa->spa_l2cache.sav_sync = B_TRUE;
5534 } else if (vd != NULL && vd->vdev_islog) {
5536 ASSERT(vd == vd->vdev_top);
5539 * XXX - Once we have bp-rewrite this should
5540 * become the common case.
5546 * Stop allocating from this vdev.
5548 metaslab_group_passivate(mg);
5551 * Wait for the youngest allocations and frees to sync,
5552 * and then wait for the deferral of those frees to finish.
5554 spa_vdev_config_exit(spa, NULL,
5555 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5558 * Attempt to evacuate the vdev.
5560 error = spa_vdev_remove_evacuate(spa, vd);
5562 txg = spa_vdev_config_enter(spa);
5565 * If we couldn't evacuate the vdev, unwind.
5568 metaslab_group_activate(mg);
5569 return (spa_vdev_exit(spa, NULL, txg, error));
5573 * Clean up the vdev namespace.
5575 spa_vdev_remove_from_namespace(spa, vd);
5577 } else if (vd != NULL) {
5579 * Normal vdevs cannot be removed (yet).
5581 error = SET_ERROR(ENOTSUP);
5584 * There is no vdev of any kind with the specified guid.
5586 error = SET_ERROR(ENOENT);
5590 return (spa_vdev_exit(spa, NULL, txg, error));
5596 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5597 * currently spared, so we can detach it.
5600 spa_vdev_resilver_done_hunt(vdev_t *vd)
5602 vdev_t *newvd, *oldvd;
5604 for (int c = 0; c < vd->vdev_children; c++) {
5605 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5611 * Check for a completed replacement. We always consider the first
5612 * vdev in the list to be the oldest vdev, and the last one to be
5613 * the newest (see spa_vdev_attach() for how that works). In
5614 * the case where the newest vdev is faulted, we will not automatically
5615 * remove it after a resilver completes. This is OK as it will require
5616 * user intervention to determine which disk the admin wishes to keep.
5618 if (vd->vdev_ops == &vdev_replacing_ops) {
5619 ASSERT(vd->vdev_children > 1);
5621 newvd = vd->vdev_child[vd->vdev_children - 1];
5622 oldvd = vd->vdev_child[0];
5624 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5625 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5626 !vdev_dtl_required(oldvd))
5631 * Check for a completed resilver with the 'unspare' flag set.
5633 if (vd->vdev_ops == &vdev_spare_ops) {
5634 vdev_t *first = vd->vdev_child[0];
5635 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5637 if (last->vdev_unspare) {
5640 } else if (first->vdev_unspare) {
5647 if (oldvd != NULL &&
5648 vdev_dtl_empty(newvd, DTL_MISSING) &&
5649 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5650 !vdev_dtl_required(oldvd))
5654 * If there are more than two spares attached to a disk,
5655 * and those spares are not required, then we want to
5656 * attempt to free them up now so that they can be used
5657 * by other pools. Once we're back down to a single
5658 * disk+spare, we stop removing them.
5660 if (vd->vdev_children > 2) {
5661 newvd = vd->vdev_child[1];
5663 if (newvd->vdev_isspare && last->vdev_isspare &&
5664 vdev_dtl_empty(last, DTL_MISSING) &&
5665 vdev_dtl_empty(last, DTL_OUTAGE) &&
5666 !vdev_dtl_required(newvd))
5675 spa_vdev_resilver_done(spa_t *spa)
5677 vdev_t *vd, *pvd, *ppvd;
5678 uint64_t guid, sguid, pguid, ppguid;
5680 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5682 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5683 pvd = vd->vdev_parent;
5684 ppvd = pvd->vdev_parent;
5685 guid = vd->vdev_guid;
5686 pguid = pvd->vdev_guid;
5687 ppguid = ppvd->vdev_guid;
5690 * If we have just finished replacing a hot spared device, then
5691 * we need to detach the parent's first child (the original hot
5694 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5695 ppvd->vdev_children == 2) {
5696 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5697 sguid = ppvd->vdev_child[1]->vdev_guid;
5699 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5701 spa_config_exit(spa, SCL_ALL, FTAG);
5702 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5704 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5706 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5709 spa_config_exit(spa, SCL_ALL, FTAG);
5713 * Update the stored path or FRU for this vdev.
5716 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5720 boolean_t sync = B_FALSE;
5722 ASSERT(spa_writeable(spa));
5724 spa_vdev_state_enter(spa, SCL_ALL);
5726 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5727 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5729 if (!vd->vdev_ops->vdev_op_leaf)
5730 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5733 if (strcmp(value, vd->vdev_path) != 0) {
5734 spa_strfree(vd->vdev_path);
5735 vd->vdev_path = spa_strdup(value);
5739 if (vd->vdev_fru == NULL) {
5740 vd->vdev_fru = spa_strdup(value);
5742 } else if (strcmp(value, vd->vdev_fru) != 0) {
5743 spa_strfree(vd->vdev_fru);
5744 vd->vdev_fru = spa_strdup(value);
5749 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5753 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5755 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5759 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5761 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5765 * ==========================================================================
5767 * ==========================================================================
5771 spa_scan_stop(spa_t *spa)
5773 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5774 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5775 return (SET_ERROR(EBUSY));
5776 return (dsl_scan_cancel(spa->spa_dsl_pool));
5780 spa_scan(spa_t *spa, pool_scan_func_t func)
5782 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5784 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5785 return (SET_ERROR(ENOTSUP));
5788 * If a resilver was requested, but there is no DTL on a
5789 * writeable leaf device, we have nothing to do.
5791 if (func == POOL_SCAN_RESILVER &&
5792 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5793 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5797 return (dsl_scan(spa->spa_dsl_pool, func));
5801 * ==========================================================================
5802 * SPA async task processing
5803 * ==========================================================================
5807 spa_async_remove(spa_t *spa, vdev_t *vd)
5809 if (vd->vdev_remove_wanted) {
5810 vd->vdev_remove_wanted = B_FALSE;
5811 vd->vdev_delayed_close = B_FALSE;
5812 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5815 * We want to clear the stats, but we don't want to do a full
5816 * vdev_clear() as that will cause us to throw away
5817 * degraded/faulted state as well as attempt to reopen the
5818 * device, all of which is a waste.
5820 vd->vdev_stat.vs_read_errors = 0;
5821 vd->vdev_stat.vs_write_errors = 0;
5822 vd->vdev_stat.vs_checksum_errors = 0;
5824 vdev_state_dirty(vd->vdev_top);
5827 for (int c = 0; c < vd->vdev_children; c++)
5828 spa_async_remove(spa, vd->vdev_child[c]);
5832 spa_async_probe(spa_t *spa, vdev_t *vd)
5834 if (vd->vdev_probe_wanted) {
5835 vd->vdev_probe_wanted = B_FALSE;
5836 vdev_reopen(vd); /* vdev_open() does the actual probe */
5839 for (int c = 0; c < vd->vdev_children; c++)
5840 spa_async_probe(spa, vd->vdev_child[c]);
5844 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5850 if (!spa->spa_autoexpand)
5853 for (int c = 0; c < vd->vdev_children; c++) {
5854 vdev_t *cvd = vd->vdev_child[c];
5855 spa_async_autoexpand(spa, cvd);
5858 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5861 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5862 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5864 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5865 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5867 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5868 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5871 kmem_free(physpath, MAXPATHLEN);
5875 spa_async_thread(void *arg)
5880 ASSERT(spa->spa_sync_on);
5882 mutex_enter(&spa->spa_async_lock);
5883 tasks = spa->spa_async_tasks;
5884 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
5885 mutex_exit(&spa->spa_async_lock);
5888 * See if the config needs to be updated.
5890 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5891 uint64_t old_space, new_space;
5893 mutex_enter(&spa_namespace_lock);
5894 old_space = metaslab_class_get_space(spa_normal_class(spa));
5895 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5896 new_space = metaslab_class_get_space(spa_normal_class(spa));
5897 mutex_exit(&spa_namespace_lock);
5900 * If the pool grew as a result of the config update,
5901 * then log an internal history event.
5903 if (new_space != old_space) {
5904 spa_history_log_internal(spa, "vdev online", NULL,
5905 "pool '%s' size: %llu(+%llu)",
5906 spa_name(spa), new_space, new_space - old_space);
5910 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5911 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5912 spa_async_autoexpand(spa, spa->spa_root_vdev);
5913 spa_config_exit(spa, SCL_CONFIG, FTAG);
5917 * See if any devices need to be probed.
5919 if (tasks & SPA_ASYNC_PROBE) {
5920 spa_vdev_state_enter(spa, SCL_NONE);
5921 spa_async_probe(spa, spa->spa_root_vdev);
5922 (void) spa_vdev_state_exit(spa, NULL, 0);
5926 * If any devices are done replacing, detach them.
5928 if (tasks & SPA_ASYNC_RESILVER_DONE)
5929 spa_vdev_resilver_done(spa);
5932 * Kick off a resilver.
5934 if (tasks & SPA_ASYNC_RESILVER)
5935 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5938 * Let the world know that we're done.
5940 mutex_enter(&spa->spa_async_lock);
5941 spa->spa_async_thread = NULL;
5942 cv_broadcast(&spa->spa_async_cv);
5943 mutex_exit(&spa->spa_async_lock);
5948 spa_async_thread_vd(void *arg)
5953 ASSERT(spa->spa_sync_on);
5955 mutex_enter(&spa->spa_async_lock);
5956 tasks = spa->spa_async_tasks;
5958 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
5959 mutex_exit(&spa->spa_async_lock);
5962 * See if any devices need to be marked REMOVED.
5964 if (tasks & SPA_ASYNC_REMOVE) {
5965 spa_vdev_state_enter(spa, SCL_NONE);
5966 spa_async_remove(spa, spa->spa_root_vdev);
5967 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5968 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5969 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5970 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5971 (void) spa_vdev_state_exit(spa, NULL, 0);
5975 * Let the world know that we're done.
5977 mutex_enter(&spa->spa_async_lock);
5978 tasks = spa->spa_async_tasks;
5979 if ((tasks & SPA_ASYNC_REMOVE) != 0)
5981 spa->spa_async_thread_vd = NULL;
5982 cv_broadcast(&spa->spa_async_cv);
5983 mutex_exit(&spa->spa_async_lock);
5988 spa_async_suspend(spa_t *spa)
5990 mutex_enter(&spa->spa_async_lock);
5991 spa->spa_async_suspended++;
5992 while (spa->spa_async_thread != NULL &&
5993 spa->spa_async_thread_vd != NULL)
5994 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5995 mutex_exit(&spa->spa_async_lock);
5999 spa_async_resume(spa_t *spa)
6001 mutex_enter(&spa->spa_async_lock);
6002 ASSERT(spa->spa_async_suspended != 0);
6003 spa->spa_async_suspended--;
6004 mutex_exit(&spa->spa_async_lock);
6008 spa_async_tasks_pending(spa_t *spa)
6010 uint_t non_config_tasks;
6012 boolean_t config_task_suspended;
6014 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6016 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6017 if (spa->spa_ccw_fail_time == 0) {
6018 config_task_suspended = B_FALSE;
6020 config_task_suspended =
6021 (gethrtime() - spa->spa_ccw_fail_time) <
6022 (zfs_ccw_retry_interval * NANOSEC);
6025 return (non_config_tasks || (config_task && !config_task_suspended));
6029 spa_async_dispatch(spa_t *spa)
6031 mutex_enter(&spa->spa_async_lock);
6032 if (spa_async_tasks_pending(spa) &&
6033 !spa->spa_async_suspended &&
6034 spa->spa_async_thread == NULL &&
6036 spa->spa_async_thread = thread_create(NULL, 0,
6037 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6038 mutex_exit(&spa->spa_async_lock);
6042 spa_async_dispatch_vd(spa_t *spa)
6044 mutex_enter(&spa->spa_async_lock);
6045 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6046 !spa->spa_async_suspended &&
6047 spa->spa_async_thread_vd == NULL &&
6049 spa->spa_async_thread_vd = thread_create(NULL, 0,
6050 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6051 mutex_exit(&spa->spa_async_lock);
6055 spa_async_request(spa_t *spa, int task)
6057 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6058 mutex_enter(&spa->spa_async_lock);
6059 spa->spa_async_tasks |= task;
6060 mutex_exit(&spa->spa_async_lock);
6061 spa_async_dispatch_vd(spa);
6065 * ==========================================================================
6066 * SPA syncing routines
6067 * ==========================================================================
6071 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6074 bpobj_enqueue(bpo, bp, tx);
6079 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6083 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6084 BP_GET_PSIZE(bp), zio->io_flags));
6089 * Note: this simple function is not inlined to make it easier to dtrace the
6090 * amount of time spent syncing frees.
6093 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6095 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6096 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6097 VERIFY(zio_wait(zio) == 0);
6101 * Note: this simple function is not inlined to make it easier to dtrace the
6102 * amount of time spent syncing deferred frees.
6105 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6107 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6108 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6109 spa_free_sync_cb, zio, tx), ==, 0);
6110 VERIFY0(zio_wait(zio));
6115 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6117 char *packed = NULL;
6122 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6125 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6126 * information. This avoids the dmu_buf_will_dirty() path and
6127 * saves us a pre-read to get data we don't actually care about.
6129 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6130 packed = kmem_alloc(bufsize, KM_SLEEP);
6132 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6134 bzero(packed + nvsize, bufsize - nvsize);
6136 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6138 kmem_free(packed, bufsize);
6140 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6141 dmu_buf_will_dirty(db, tx);
6142 *(uint64_t *)db->db_data = nvsize;
6143 dmu_buf_rele(db, FTAG);
6147 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6148 const char *config, const char *entry)
6158 * Update the MOS nvlist describing the list of available devices.
6159 * spa_validate_aux() will have already made sure this nvlist is
6160 * valid and the vdevs are labeled appropriately.
6162 if (sav->sav_object == 0) {
6163 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6164 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6165 sizeof (uint64_t), tx);
6166 VERIFY(zap_update(spa->spa_meta_objset,
6167 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6168 &sav->sav_object, tx) == 0);
6171 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6172 if (sav->sav_count == 0) {
6173 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6175 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6176 for (i = 0; i < sav->sav_count; i++)
6177 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6178 B_FALSE, VDEV_CONFIG_L2CACHE);
6179 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6180 sav->sav_count) == 0);
6181 for (i = 0; i < sav->sav_count; i++)
6182 nvlist_free(list[i]);
6183 kmem_free(list, sav->sav_count * sizeof (void *));
6186 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6187 nvlist_free(nvroot);
6189 sav->sav_sync = B_FALSE;
6193 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6197 if (list_is_empty(&spa->spa_config_dirty_list))
6200 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6202 config = spa_config_generate(spa, spa->spa_root_vdev,
6203 dmu_tx_get_txg(tx), B_FALSE);
6206 * If we're upgrading the spa version then make sure that
6207 * the config object gets updated with the correct version.
6209 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6210 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6211 spa->spa_uberblock.ub_version);
6213 spa_config_exit(spa, SCL_STATE, FTAG);
6215 if (spa->spa_config_syncing)
6216 nvlist_free(spa->spa_config_syncing);
6217 spa->spa_config_syncing = config;
6219 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6223 spa_sync_version(void *arg, dmu_tx_t *tx)
6225 uint64_t *versionp = arg;
6226 uint64_t version = *versionp;
6227 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6230 * Setting the version is special cased when first creating the pool.
6232 ASSERT(tx->tx_txg != TXG_INITIAL);
6234 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6235 ASSERT(version >= spa_version(spa));
6237 spa->spa_uberblock.ub_version = version;
6238 vdev_config_dirty(spa->spa_root_vdev);
6239 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6243 * Set zpool properties.
6246 spa_sync_props(void *arg, dmu_tx_t *tx)
6248 nvlist_t *nvp = arg;
6249 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6250 objset_t *mos = spa->spa_meta_objset;
6251 nvpair_t *elem = NULL;
6253 mutex_enter(&spa->spa_props_lock);
6255 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6257 char *strval, *fname;
6259 const char *propname;
6260 zprop_type_t proptype;
6263 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6266 * We checked this earlier in spa_prop_validate().
6268 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6270 fname = strchr(nvpair_name(elem), '@') + 1;
6271 VERIFY0(zfeature_lookup_name(fname, &fid));
6273 spa_feature_enable(spa, fid, tx);
6274 spa_history_log_internal(spa, "set", tx,
6275 "%s=enabled", nvpair_name(elem));
6278 case ZPOOL_PROP_VERSION:
6279 intval = fnvpair_value_uint64(elem);
6281 * The version is synced seperatly before other
6282 * properties and should be correct by now.
6284 ASSERT3U(spa_version(spa), >=, intval);
6287 case ZPOOL_PROP_ALTROOT:
6289 * 'altroot' is a non-persistent property. It should
6290 * have been set temporarily at creation or import time.
6292 ASSERT(spa->spa_root != NULL);
6295 case ZPOOL_PROP_READONLY:
6296 case ZPOOL_PROP_CACHEFILE:
6298 * 'readonly' and 'cachefile' are also non-persisitent
6302 case ZPOOL_PROP_COMMENT:
6303 strval = fnvpair_value_string(elem);
6304 if (spa->spa_comment != NULL)
6305 spa_strfree(spa->spa_comment);
6306 spa->spa_comment = spa_strdup(strval);
6308 * We need to dirty the configuration on all the vdevs
6309 * so that their labels get updated. It's unnecessary
6310 * to do this for pool creation since the vdev's
6311 * configuratoin has already been dirtied.
6313 if (tx->tx_txg != TXG_INITIAL)
6314 vdev_config_dirty(spa->spa_root_vdev);
6315 spa_history_log_internal(spa, "set", tx,
6316 "%s=%s", nvpair_name(elem), strval);
6320 * Set pool property values in the poolprops mos object.
6322 if (spa->spa_pool_props_object == 0) {
6323 spa->spa_pool_props_object =
6324 zap_create_link(mos, DMU_OT_POOL_PROPS,
6325 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6329 /* normalize the property name */
6330 propname = zpool_prop_to_name(prop);
6331 proptype = zpool_prop_get_type(prop);
6333 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6334 ASSERT(proptype == PROP_TYPE_STRING);
6335 strval = fnvpair_value_string(elem);
6336 VERIFY0(zap_update(mos,
6337 spa->spa_pool_props_object, propname,
6338 1, strlen(strval) + 1, strval, tx));
6339 spa_history_log_internal(spa, "set", tx,
6340 "%s=%s", nvpair_name(elem), strval);
6341 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6342 intval = fnvpair_value_uint64(elem);
6344 if (proptype == PROP_TYPE_INDEX) {
6346 VERIFY0(zpool_prop_index_to_string(
6347 prop, intval, &unused));
6349 VERIFY0(zap_update(mos,
6350 spa->spa_pool_props_object, propname,
6351 8, 1, &intval, tx));
6352 spa_history_log_internal(spa, "set", tx,
6353 "%s=%lld", nvpair_name(elem), intval);
6355 ASSERT(0); /* not allowed */
6359 case ZPOOL_PROP_DELEGATION:
6360 spa->spa_delegation = intval;
6362 case ZPOOL_PROP_BOOTFS:
6363 spa->spa_bootfs = intval;
6365 case ZPOOL_PROP_FAILUREMODE:
6366 spa->spa_failmode = intval;
6368 case ZPOOL_PROP_AUTOEXPAND:
6369 spa->spa_autoexpand = intval;
6370 if (tx->tx_txg != TXG_INITIAL)
6371 spa_async_request(spa,
6372 SPA_ASYNC_AUTOEXPAND);
6374 case ZPOOL_PROP_DEDUPDITTO:
6375 spa->spa_dedup_ditto = intval;
6384 mutex_exit(&spa->spa_props_lock);
6388 * Perform one-time upgrade on-disk changes. spa_version() does not
6389 * reflect the new version this txg, so there must be no changes this
6390 * txg to anything that the upgrade code depends on after it executes.
6391 * Therefore this must be called after dsl_pool_sync() does the sync
6395 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6397 dsl_pool_t *dp = spa->spa_dsl_pool;
6399 ASSERT(spa->spa_sync_pass == 1);
6401 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6403 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6404 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6405 dsl_pool_create_origin(dp, tx);
6407 /* Keeping the origin open increases spa_minref */
6408 spa->spa_minref += 3;
6411 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6412 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6413 dsl_pool_upgrade_clones(dp, tx);
6416 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6417 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6418 dsl_pool_upgrade_dir_clones(dp, tx);
6420 /* Keeping the freedir open increases spa_minref */
6421 spa->spa_minref += 3;
6424 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6425 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6426 spa_feature_create_zap_objects(spa, tx);
6428 rrw_exit(&dp->dp_config_rwlock, FTAG);
6432 * Sync the specified transaction group. New blocks may be dirtied as
6433 * part of the process, so we iterate until it converges.
6436 spa_sync(spa_t *spa, uint64_t txg)
6438 dsl_pool_t *dp = spa->spa_dsl_pool;
6439 objset_t *mos = spa->spa_meta_objset;
6440 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6441 vdev_t *rvd = spa->spa_root_vdev;
6446 VERIFY(spa_writeable(spa));
6449 * Lock out configuration changes.
6451 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6453 spa->spa_syncing_txg = txg;
6454 spa->spa_sync_pass = 0;
6457 * If there are any pending vdev state changes, convert them
6458 * into config changes that go out with this transaction group.
6460 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6461 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6463 * We need the write lock here because, for aux vdevs,
6464 * calling vdev_config_dirty() modifies sav_config.
6465 * This is ugly and will become unnecessary when we
6466 * eliminate the aux vdev wart by integrating all vdevs
6467 * into the root vdev tree.
6469 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6470 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6471 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6472 vdev_state_clean(vd);
6473 vdev_config_dirty(vd);
6475 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6476 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6478 spa_config_exit(spa, SCL_STATE, FTAG);
6480 tx = dmu_tx_create_assigned(dp, txg);
6482 spa->spa_sync_starttime = gethrtime();
6484 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6485 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6488 callout_reset(&spa->spa_deadman_cycid,
6489 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6494 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6495 * set spa_deflate if we have no raid-z vdevs.
6497 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6498 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6501 for (i = 0; i < rvd->vdev_children; i++) {
6502 vd = rvd->vdev_child[i];
6503 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6506 if (i == rvd->vdev_children) {
6507 spa->spa_deflate = TRUE;
6508 VERIFY(0 == zap_add(spa->spa_meta_objset,
6509 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6510 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6515 * If anything has changed in this txg, or if someone is waiting
6516 * for this txg to sync (eg, spa_vdev_remove()), push the
6517 * deferred frees from the previous txg. If not, leave them
6518 * alone so that we don't generate work on an otherwise idle
6521 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6522 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6523 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6524 ((dsl_scan_active(dp->dp_scan) ||
6525 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6526 spa_sync_deferred_frees(spa, tx);
6530 * Iterate to convergence.
6533 int pass = ++spa->spa_sync_pass;
6535 spa_sync_config_object(spa, tx);
6536 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6537 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6538 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6539 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6540 spa_errlog_sync(spa, txg);
6541 dsl_pool_sync(dp, txg);
6543 if (pass < zfs_sync_pass_deferred_free) {
6544 spa_sync_frees(spa, free_bpl, tx);
6546 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6547 &spa->spa_deferred_bpobj, tx);
6551 dsl_scan_sync(dp, tx);
6553 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6557 spa_sync_upgrades(spa, tx);
6559 } while (dmu_objset_is_dirty(mos, txg));
6562 * Rewrite the vdev configuration (which includes the uberblock)
6563 * to commit the transaction group.
6565 * If there are no dirty vdevs, we sync the uberblock to a few
6566 * random top-level vdevs that are known to be visible in the
6567 * config cache (see spa_vdev_add() for a complete description).
6568 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6572 * We hold SCL_STATE to prevent vdev open/close/etc.
6573 * while we're attempting to write the vdev labels.
6575 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6577 if (list_is_empty(&spa->spa_config_dirty_list)) {
6578 vdev_t *svd[SPA_DVAS_PER_BP];
6580 int children = rvd->vdev_children;
6581 int c0 = spa_get_random(children);
6583 for (int c = 0; c < children; c++) {
6584 vd = rvd->vdev_child[(c0 + c) % children];
6585 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6587 svd[svdcount++] = vd;
6588 if (svdcount == SPA_DVAS_PER_BP)
6591 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6593 error = vdev_config_sync(svd, svdcount, txg,
6596 error = vdev_config_sync(rvd->vdev_child,
6597 rvd->vdev_children, txg, B_FALSE);
6599 error = vdev_config_sync(rvd->vdev_child,
6600 rvd->vdev_children, txg, B_TRUE);
6604 spa->spa_last_synced_guid = rvd->vdev_guid;
6606 spa_config_exit(spa, SCL_STATE, FTAG);
6610 zio_suspend(spa, NULL);
6611 zio_resume_wait(spa);
6616 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6619 callout_drain(&spa->spa_deadman_cycid);
6624 * Clear the dirty config list.
6626 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6627 vdev_config_clean(vd);
6630 * Now that the new config has synced transactionally,
6631 * let it become visible to the config cache.
6633 if (spa->spa_config_syncing != NULL) {
6634 spa_config_set(spa, spa->spa_config_syncing);
6635 spa->spa_config_txg = txg;
6636 spa->spa_config_syncing = NULL;
6639 spa->spa_ubsync = spa->spa_uberblock;
6641 dsl_pool_sync_done(dp, txg);
6644 * Update usable space statistics.
6646 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6647 vdev_sync_done(vd, txg);
6649 spa_update_dspace(spa);
6652 * It had better be the case that we didn't dirty anything
6653 * since vdev_config_sync().
6655 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6656 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6657 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6659 spa->spa_sync_pass = 0;
6661 spa_config_exit(spa, SCL_CONFIG, FTAG);
6663 spa_handle_ignored_writes(spa);
6666 * If any async tasks have been requested, kick them off.
6668 spa_async_dispatch(spa);
6669 spa_async_dispatch_vd(spa);
6673 * Sync all pools. We don't want to hold the namespace lock across these
6674 * operations, so we take a reference on the spa_t and drop the lock during the
6678 spa_sync_allpools(void)
6681 mutex_enter(&spa_namespace_lock);
6682 while ((spa = spa_next(spa)) != NULL) {
6683 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6684 !spa_writeable(spa) || spa_suspended(spa))
6686 spa_open_ref(spa, FTAG);
6687 mutex_exit(&spa_namespace_lock);
6688 txg_wait_synced(spa_get_dsl(spa), 0);
6689 mutex_enter(&spa_namespace_lock);
6690 spa_close(spa, FTAG);
6692 mutex_exit(&spa_namespace_lock);
6696 * ==========================================================================
6697 * Miscellaneous routines
6698 * ==========================================================================
6702 * Remove all pools in the system.
6710 * Remove all cached state. All pools should be closed now,
6711 * so every spa in the AVL tree should be unreferenced.
6713 mutex_enter(&spa_namespace_lock);
6714 while ((spa = spa_next(NULL)) != NULL) {
6716 * Stop async tasks. The async thread may need to detach
6717 * a device that's been replaced, which requires grabbing
6718 * spa_namespace_lock, so we must drop it here.
6720 spa_open_ref(spa, FTAG);
6721 mutex_exit(&spa_namespace_lock);
6722 spa_async_suspend(spa);
6723 mutex_enter(&spa_namespace_lock);
6724 spa_close(spa, FTAG);
6726 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6728 spa_deactivate(spa);
6732 mutex_exit(&spa_namespace_lock);
6736 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6741 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6745 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6746 vd = spa->spa_l2cache.sav_vdevs[i];
6747 if (vd->vdev_guid == guid)
6751 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6752 vd = spa->spa_spares.sav_vdevs[i];
6753 if (vd->vdev_guid == guid)
6762 spa_upgrade(spa_t *spa, uint64_t version)
6764 ASSERT(spa_writeable(spa));
6766 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6769 * This should only be called for a non-faulted pool, and since a
6770 * future version would result in an unopenable pool, this shouldn't be
6773 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6774 ASSERT(version >= spa->spa_uberblock.ub_version);
6776 spa->spa_uberblock.ub_version = version;
6777 vdev_config_dirty(spa->spa_root_vdev);
6779 spa_config_exit(spa, SCL_ALL, FTAG);
6781 txg_wait_synced(spa_get_dsl(spa), 0);
6785 spa_has_spare(spa_t *spa, uint64_t guid)
6789 spa_aux_vdev_t *sav = &spa->spa_spares;
6791 for (i = 0; i < sav->sav_count; i++)
6792 if (sav->sav_vdevs[i]->vdev_guid == guid)
6795 for (i = 0; i < sav->sav_npending; i++) {
6796 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6797 &spareguid) == 0 && spareguid == guid)
6805 * Check if a pool has an active shared spare device.
6806 * Note: reference count of an active spare is 2, as a spare and as a replace
6809 spa_has_active_shared_spare(spa_t *spa)
6813 spa_aux_vdev_t *sav = &spa->spa_spares;
6815 for (i = 0; i < sav->sav_count; i++) {
6816 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6817 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6826 * Post a sysevent corresponding to the given event. The 'name' must be one of
6827 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6828 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6829 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6830 * or zdb as real changes.
6833 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6837 sysevent_attr_list_t *attr = NULL;
6838 sysevent_value_t value;
6841 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6844 value.value_type = SE_DATA_TYPE_STRING;
6845 value.value.sv_string = spa_name(spa);
6846 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6849 value.value_type = SE_DATA_TYPE_UINT64;
6850 value.value.sv_uint64 = spa_guid(spa);
6851 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6855 value.value_type = SE_DATA_TYPE_UINT64;
6856 value.value.sv_uint64 = vd->vdev_guid;
6857 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6861 if (vd->vdev_path) {
6862 value.value_type = SE_DATA_TYPE_STRING;
6863 value.value.sv_string = vd->vdev_path;
6864 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6865 &value, SE_SLEEP) != 0)
6870 if (sysevent_attach_attributes(ev, attr) != 0)
6874 (void) log_sysevent(ev, SE_SLEEP, &eid);
6878 sysevent_free_attr(attr);