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_online_percent, /* value is % of online CPUs */
100 zti_mode_batch, /* cpu-intensive; value is ignored */
101 zti_mode_null, /* don't create a taskq */
105 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
106 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
107 #define ZTI_BATCH { zti_mode_batch, 0 }
108 #define ZTI_NULL { zti_mode_null, 0 }
110 #define ZTI_ONE ZTI_FIX(1)
112 typedef struct zio_taskq_info {
113 enum zti_modes zti_mode;
117 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
118 "issue", "issue_high", "intr", "intr_high"
122 * Define the taskq threads for the following I/O types:
123 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
125 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
126 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
127 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
128 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
129 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
130 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
131 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
132 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
135 static void spa_sync_version(void *arg, dmu_tx_t *tx);
136 static void spa_sync_props(void *arg, dmu_tx_t *tx);
137 static boolean_t spa_has_active_shared_spare(spa_t *spa);
138 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
139 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
141 static void spa_vdev_resilver_done(spa_t *spa);
143 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
145 id_t zio_taskq_psrset_bind = PS_NONE;
148 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
150 uint_t zio_taskq_basedc = 80; /* base duty cycle */
152 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
153 extern int zfs_sync_pass_deferred_free;
156 extern void spa_deadman(void *arg);
160 * This (illegal) pool name is used when temporarily importing a spa_t in order
161 * to get the vdev stats associated with the imported devices.
163 #define TRYIMPORT_NAME "$import"
166 * ==========================================================================
167 * SPA properties routines
168 * ==========================================================================
172 * Add a (source=src, propname=propval) list to an nvlist.
175 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
176 uint64_t intval, zprop_source_t src)
178 const char *propname = zpool_prop_to_name(prop);
181 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
182 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
185 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
187 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
189 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
190 nvlist_free(propval);
194 * Get property values from the spa configuration.
197 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
199 vdev_t *rvd = spa->spa_root_vdev;
200 dsl_pool_t *pool = spa->spa_dsl_pool;
204 uint64_t cap, version;
205 zprop_source_t src = ZPROP_SRC_NONE;
206 spa_config_dirent_t *dp;
208 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
211 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
212 size = metaslab_class_get_space(spa_normal_class(spa));
213 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
214 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
215 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
216 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
220 for (int c = 0; c < rvd->vdev_children; c++) {
221 vdev_t *tvd = rvd->vdev_child[c];
222 space += tvd->vdev_max_asize - tvd->vdev_asize;
224 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
227 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
228 (spa_mode(spa) == FREAD), src);
230 cap = (size == 0) ? 0 : (alloc * 100 / size);
231 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
233 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
234 ddt_get_pool_dedup_ratio(spa), src);
236 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
237 rvd->vdev_state, src);
239 version = spa_version(spa);
240 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
241 src = ZPROP_SRC_DEFAULT;
243 src = ZPROP_SRC_LOCAL;
244 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
248 dsl_dir_t *freedir = pool->dp_free_dir;
251 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
252 * when opening pools before this version freedir will be NULL.
254 if (freedir != NULL) {
255 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
256 freedir->dd_phys->dd_used_bytes, src);
258 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
263 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
265 if (spa->spa_comment != NULL) {
266 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
270 if (spa->spa_root != NULL)
271 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
274 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
275 if (dp->scd_path == NULL) {
276 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
277 "none", 0, ZPROP_SRC_LOCAL);
278 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
279 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
280 dp->scd_path, 0, ZPROP_SRC_LOCAL);
286 * Get zpool property values.
289 spa_prop_get(spa_t *spa, nvlist_t **nvp)
291 objset_t *mos = spa->spa_meta_objset;
296 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
298 mutex_enter(&spa->spa_props_lock);
301 * Get properties from the spa config.
303 spa_prop_get_config(spa, nvp);
305 /* If no pool property object, no more prop to get. */
306 if (mos == NULL || spa->spa_pool_props_object == 0) {
307 mutex_exit(&spa->spa_props_lock);
312 * Get properties from the MOS pool property object.
314 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
315 (err = zap_cursor_retrieve(&zc, &za)) == 0;
316 zap_cursor_advance(&zc)) {
319 zprop_source_t src = ZPROP_SRC_DEFAULT;
322 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
325 switch (za.za_integer_length) {
327 /* integer property */
328 if (za.za_first_integer !=
329 zpool_prop_default_numeric(prop))
330 src = ZPROP_SRC_LOCAL;
332 if (prop == ZPOOL_PROP_BOOTFS) {
334 dsl_dataset_t *ds = NULL;
336 dp = spa_get_dsl(spa);
337 dsl_pool_config_enter(dp, FTAG);
338 if (err = dsl_dataset_hold_obj(dp,
339 za.za_first_integer, FTAG, &ds)) {
340 dsl_pool_config_exit(dp, FTAG);
345 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
347 dsl_dataset_name(ds, strval);
348 dsl_dataset_rele(ds, FTAG);
349 dsl_pool_config_exit(dp, FTAG);
352 intval = za.za_first_integer;
355 spa_prop_add_list(*nvp, prop, strval, intval, src);
359 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
364 /* string property */
365 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
366 err = zap_lookup(mos, spa->spa_pool_props_object,
367 za.za_name, 1, za.za_num_integers, strval);
369 kmem_free(strval, za.za_num_integers);
372 spa_prop_add_list(*nvp, prop, strval, 0, src);
373 kmem_free(strval, za.za_num_integers);
380 zap_cursor_fini(&zc);
381 mutex_exit(&spa->spa_props_lock);
383 if (err && err != ENOENT) {
393 * Validate the given pool properties nvlist and modify the list
394 * for the property values to be set.
397 spa_prop_validate(spa_t *spa, nvlist_t *props)
400 int error = 0, reset_bootfs = 0;
402 boolean_t has_feature = B_FALSE;
405 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
407 char *strval, *slash, *check, *fname;
408 const char *propname = nvpair_name(elem);
409 zpool_prop_t prop = zpool_name_to_prop(propname);
413 if (!zpool_prop_feature(propname)) {
414 error = SET_ERROR(EINVAL);
419 * Sanitize the input.
421 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
422 error = SET_ERROR(EINVAL);
426 if (nvpair_value_uint64(elem, &intval) != 0) {
427 error = SET_ERROR(EINVAL);
432 error = SET_ERROR(EINVAL);
436 fname = strchr(propname, '@') + 1;
437 if (zfeature_lookup_name(fname, NULL) != 0) {
438 error = SET_ERROR(EINVAL);
442 has_feature = B_TRUE;
445 case ZPOOL_PROP_VERSION:
446 error = nvpair_value_uint64(elem, &intval);
448 (intval < spa_version(spa) ||
449 intval > SPA_VERSION_BEFORE_FEATURES ||
451 error = SET_ERROR(EINVAL);
454 case ZPOOL_PROP_DELEGATION:
455 case ZPOOL_PROP_AUTOREPLACE:
456 case ZPOOL_PROP_LISTSNAPS:
457 case ZPOOL_PROP_AUTOEXPAND:
458 error = nvpair_value_uint64(elem, &intval);
459 if (!error && intval > 1)
460 error = SET_ERROR(EINVAL);
463 case ZPOOL_PROP_BOOTFS:
465 * If the pool version is less than SPA_VERSION_BOOTFS,
466 * or the pool is still being created (version == 0),
467 * the bootfs property cannot be set.
469 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
470 error = SET_ERROR(ENOTSUP);
475 * Make sure the vdev config is bootable
477 if (!vdev_is_bootable(spa->spa_root_vdev)) {
478 error = SET_ERROR(ENOTSUP);
484 error = nvpair_value_string(elem, &strval);
490 if (strval == NULL || strval[0] == '\0') {
491 objnum = zpool_prop_default_numeric(
496 if (error = dmu_objset_hold(strval, FTAG, &os))
499 /* Must be ZPL and not gzip compressed. */
501 if (dmu_objset_type(os) != DMU_OST_ZFS) {
502 error = SET_ERROR(ENOTSUP);
504 dsl_prop_get_int_ds(dmu_objset_ds(os),
505 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
507 !BOOTFS_COMPRESS_VALID(compress)) {
508 error = SET_ERROR(ENOTSUP);
510 objnum = dmu_objset_id(os);
512 dmu_objset_rele(os, FTAG);
516 case ZPOOL_PROP_FAILUREMODE:
517 error = nvpair_value_uint64(elem, &intval);
518 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
519 intval > ZIO_FAILURE_MODE_PANIC))
520 error = SET_ERROR(EINVAL);
523 * This is a special case which only occurs when
524 * the pool has completely failed. This allows
525 * the user to change the in-core failmode property
526 * without syncing it out to disk (I/Os might
527 * currently be blocked). We do this by returning
528 * EIO to the caller (spa_prop_set) to trick it
529 * into thinking we encountered a property validation
532 if (!error && spa_suspended(spa)) {
533 spa->spa_failmode = intval;
534 error = SET_ERROR(EIO);
538 case ZPOOL_PROP_CACHEFILE:
539 if ((error = nvpair_value_string(elem, &strval)) != 0)
542 if (strval[0] == '\0')
545 if (strcmp(strval, "none") == 0)
548 if (strval[0] != '/') {
549 error = SET_ERROR(EINVAL);
553 slash = strrchr(strval, '/');
554 ASSERT(slash != NULL);
556 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
557 strcmp(slash, "/..") == 0)
558 error = SET_ERROR(EINVAL);
561 case ZPOOL_PROP_COMMENT:
562 if ((error = nvpair_value_string(elem, &strval)) != 0)
564 for (check = strval; *check != '\0'; check++) {
566 * The kernel doesn't have an easy isprint()
567 * check. For this kernel check, we merely
568 * check ASCII apart from DEL. Fix this if
569 * there is an easy-to-use kernel isprint().
571 if (*check >= 0x7f) {
572 error = SET_ERROR(EINVAL);
577 if (strlen(strval) > ZPROP_MAX_COMMENT)
581 case ZPOOL_PROP_DEDUPDITTO:
582 if (spa_version(spa) < SPA_VERSION_DEDUP)
583 error = SET_ERROR(ENOTSUP);
585 error = nvpair_value_uint64(elem, &intval);
587 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
588 error = SET_ERROR(EINVAL);
596 if (!error && reset_bootfs) {
597 error = nvlist_remove(props,
598 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
601 error = nvlist_add_uint64(props,
602 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
610 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
613 spa_config_dirent_t *dp;
615 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
619 dp = kmem_alloc(sizeof (spa_config_dirent_t),
622 if (cachefile[0] == '\0')
623 dp->scd_path = spa_strdup(spa_config_path);
624 else if (strcmp(cachefile, "none") == 0)
627 dp->scd_path = spa_strdup(cachefile);
629 list_insert_head(&spa->spa_config_list, dp);
631 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
635 spa_prop_set(spa_t *spa, nvlist_t *nvp)
638 nvpair_t *elem = NULL;
639 boolean_t need_sync = B_FALSE;
641 if ((error = spa_prop_validate(spa, nvp)) != 0)
644 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
645 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
647 if (prop == ZPOOL_PROP_CACHEFILE ||
648 prop == ZPOOL_PROP_ALTROOT ||
649 prop == ZPOOL_PROP_READONLY)
652 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
655 if (prop == ZPOOL_PROP_VERSION) {
656 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
658 ASSERT(zpool_prop_feature(nvpair_name(elem)));
659 ver = SPA_VERSION_FEATURES;
663 /* Save time if the version is already set. */
664 if (ver == spa_version(spa))
668 * In addition to the pool directory object, we might
669 * create the pool properties object, the features for
670 * read object, the features for write object, or the
671 * feature descriptions object.
673 error = dsl_sync_task(spa->spa_name, NULL,
674 spa_sync_version, &ver, 6);
685 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
693 * If the bootfs property value is dsobj, clear it.
696 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
698 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
699 VERIFY(zap_remove(spa->spa_meta_objset,
700 spa->spa_pool_props_object,
701 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
708 spa_change_guid_check(void *arg, dmu_tx_t *tx)
710 uint64_t *newguid = arg;
711 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
712 vdev_t *rvd = spa->spa_root_vdev;
715 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
716 vdev_state = rvd->vdev_state;
717 spa_config_exit(spa, SCL_STATE, FTAG);
719 if (vdev_state != VDEV_STATE_HEALTHY)
720 return (SET_ERROR(ENXIO));
722 ASSERT3U(spa_guid(spa), !=, *newguid);
728 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
730 uint64_t *newguid = arg;
731 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
733 vdev_t *rvd = spa->spa_root_vdev;
735 oldguid = spa_guid(spa);
737 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
738 rvd->vdev_guid = *newguid;
739 rvd->vdev_guid_sum += (*newguid - oldguid);
740 vdev_config_dirty(rvd);
741 spa_config_exit(spa, SCL_STATE, FTAG);
743 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
748 * Change the GUID for the pool. This is done so that we can later
749 * re-import a pool built from a clone of our own vdevs. We will modify
750 * the root vdev's guid, our own pool guid, and then mark all of our
751 * vdevs dirty. Note that we must make sure that all our vdevs are
752 * online when we do this, or else any vdevs that weren't present
753 * would be orphaned from our pool. We are also going to issue a
754 * sysevent to update any watchers.
757 spa_change_guid(spa_t *spa)
762 mutex_enter(&spa_namespace_lock);
763 guid = spa_generate_guid(NULL);
765 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
766 spa_change_guid_sync, &guid, 5);
769 spa_config_sync(spa, B_FALSE, B_TRUE);
770 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
773 mutex_exit(&spa_namespace_lock);
779 * ==========================================================================
780 * SPA state manipulation (open/create/destroy/import/export)
781 * ==========================================================================
785 spa_error_entry_compare(const void *a, const void *b)
787 spa_error_entry_t *sa = (spa_error_entry_t *)a;
788 spa_error_entry_t *sb = (spa_error_entry_t *)b;
791 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
792 sizeof (zbookmark_t));
803 * Utility function which retrieves copies of the current logs and
804 * re-initializes them in the process.
807 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
809 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
811 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
812 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
814 avl_create(&spa->spa_errlist_scrub,
815 spa_error_entry_compare, sizeof (spa_error_entry_t),
816 offsetof(spa_error_entry_t, se_avl));
817 avl_create(&spa->spa_errlist_last,
818 spa_error_entry_compare, sizeof (spa_error_entry_t),
819 offsetof(spa_error_entry_t, se_avl));
823 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
826 uint_t flags = TASKQ_PREPOPULATE;
827 boolean_t batch = B_FALSE;
831 return (NULL); /* no taskq needed */
834 ASSERT3U(value, >=, 1);
835 value = MAX(value, 1);
840 flags |= TASKQ_THREADS_CPU_PCT;
841 value = zio_taskq_batch_pct;
844 case zti_mode_online_percent:
845 flags |= TASKQ_THREADS_CPU_PCT;
849 panic("unrecognized mode for %s taskq (%u:%u) in "
856 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
858 flags |= TASKQ_DC_BATCH;
860 return (taskq_create_sysdc(name, value, 50, INT_MAX,
861 spa->spa_proc, zio_taskq_basedc, flags));
864 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
865 spa->spa_proc, flags));
869 spa_create_zio_taskqs(spa_t *spa)
871 for (int t = 0; t < ZIO_TYPES; t++) {
872 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
873 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
874 enum zti_modes mode = ztip->zti_mode;
875 uint_t value = ztip->zti_value;
878 (void) snprintf(name, sizeof (name),
879 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
881 spa->spa_zio_taskq[t][q] =
882 spa_taskq_create(spa, name, mode, value);
890 spa_thread(void *arg)
895 user_t *pu = PTOU(curproc);
897 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
900 ASSERT(curproc != &p0);
901 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
902 "zpool-%s", spa->spa_name);
903 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
906 /* bind this thread to the requested psrset */
907 if (zio_taskq_psrset_bind != PS_NONE) {
909 mutex_enter(&cpu_lock);
910 mutex_enter(&pidlock);
911 mutex_enter(&curproc->p_lock);
913 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
914 0, NULL, NULL) == 0) {
915 curthread->t_bind_pset = zio_taskq_psrset_bind;
918 "Couldn't bind process for zfs pool \"%s\" to "
919 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
922 mutex_exit(&curproc->p_lock);
923 mutex_exit(&pidlock);
924 mutex_exit(&cpu_lock);
930 if (zio_taskq_sysdc) {
931 sysdc_thread_enter(curthread, 100, 0);
935 spa->spa_proc = curproc;
936 spa->spa_did = curthread->t_did;
938 spa_create_zio_taskqs(spa);
940 mutex_enter(&spa->spa_proc_lock);
941 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
943 spa->spa_proc_state = SPA_PROC_ACTIVE;
944 cv_broadcast(&spa->spa_proc_cv);
946 CALLB_CPR_SAFE_BEGIN(&cprinfo);
947 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
948 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
949 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
951 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
952 spa->spa_proc_state = SPA_PROC_GONE;
954 cv_broadcast(&spa->spa_proc_cv);
955 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
957 mutex_enter(&curproc->p_lock);
960 #endif /* SPA_PROCESS */
964 * Activate an uninitialized pool.
967 spa_activate(spa_t *spa, int mode)
969 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
971 spa->spa_state = POOL_STATE_ACTIVE;
972 spa->spa_mode = mode;
974 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
975 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
977 /* Try to create a covering process */
978 mutex_enter(&spa->spa_proc_lock);
979 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
980 ASSERT(spa->spa_proc == &p0);
984 /* Only create a process if we're going to be around a while. */
985 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
986 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
988 spa->spa_proc_state = SPA_PROC_CREATED;
989 while (spa->spa_proc_state == SPA_PROC_CREATED) {
990 cv_wait(&spa->spa_proc_cv,
991 &spa->spa_proc_lock);
993 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
994 ASSERT(spa->spa_proc != &p0);
995 ASSERT(spa->spa_did != 0);
999 "Couldn't create process for zfs pool \"%s\"\n",
1004 #endif /* SPA_PROCESS */
1005 mutex_exit(&spa->spa_proc_lock);
1007 /* If we didn't create a process, we need to create our taskqs. */
1008 ASSERT(spa->spa_proc == &p0);
1009 if (spa->spa_proc == &p0) {
1010 spa_create_zio_taskqs(spa);
1014 * Start TRIM thread.
1016 trim_thread_create(spa);
1018 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1019 offsetof(vdev_t, vdev_config_dirty_node));
1020 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1021 offsetof(vdev_t, vdev_state_dirty_node));
1023 txg_list_create(&spa->spa_vdev_txg_list,
1024 offsetof(struct vdev, vdev_txg_node));
1026 avl_create(&spa->spa_errlist_scrub,
1027 spa_error_entry_compare, sizeof (spa_error_entry_t),
1028 offsetof(spa_error_entry_t, se_avl));
1029 avl_create(&spa->spa_errlist_last,
1030 spa_error_entry_compare, sizeof (spa_error_entry_t),
1031 offsetof(spa_error_entry_t, se_avl));
1035 * Opposite of spa_activate().
1038 spa_deactivate(spa_t *spa)
1040 ASSERT(spa->spa_sync_on == B_FALSE);
1041 ASSERT(spa->spa_dsl_pool == NULL);
1042 ASSERT(spa->spa_root_vdev == NULL);
1043 ASSERT(spa->spa_async_zio_root == NULL);
1044 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1047 * Stop TRIM thread in case spa_unload() wasn't called directly
1048 * before spa_deactivate().
1050 trim_thread_destroy(spa);
1052 txg_list_destroy(&spa->spa_vdev_txg_list);
1054 list_destroy(&spa->spa_config_dirty_list);
1055 list_destroy(&spa->spa_state_dirty_list);
1057 for (int t = 0; t < ZIO_TYPES; t++) {
1058 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1059 if (spa->spa_zio_taskq[t][q] != NULL)
1060 taskq_destroy(spa->spa_zio_taskq[t][q]);
1061 spa->spa_zio_taskq[t][q] = NULL;
1065 metaslab_class_destroy(spa->spa_normal_class);
1066 spa->spa_normal_class = NULL;
1068 metaslab_class_destroy(spa->spa_log_class);
1069 spa->spa_log_class = NULL;
1072 * If this was part of an import or the open otherwise failed, we may
1073 * still have errors left in the queues. Empty them just in case.
1075 spa_errlog_drain(spa);
1077 avl_destroy(&spa->spa_errlist_scrub);
1078 avl_destroy(&spa->spa_errlist_last);
1080 spa->spa_state = POOL_STATE_UNINITIALIZED;
1082 mutex_enter(&spa->spa_proc_lock);
1083 if (spa->spa_proc_state != SPA_PROC_NONE) {
1084 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1085 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1086 cv_broadcast(&spa->spa_proc_cv);
1087 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1088 ASSERT(spa->spa_proc != &p0);
1089 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1091 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1092 spa->spa_proc_state = SPA_PROC_NONE;
1094 ASSERT(spa->spa_proc == &p0);
1095 mutex_exit(&spa->spa_proc_lock);
1099 * We want to make sure spa_thread() has actually exited the ZFS
1100 * module, so that the module can't be unloaded out from underneath
1103 if (spa->spa_did != 0) {
1104 thread_join(spa->spa_did);
1107 #endif /* SPA_PROCESS */
1111 * Verify a pool configuration, and construct the vdev tree appropriately. This
1112 * will create all the necessary vdevs in the appropriate layout, with each vdev
1113 * in the CLOSED state. This will prep the pool before open/creation/import.
1114 * All vdev validation is done by the vdev_alloc() routine.
1117 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1118 uint_t id, int atype)
1124 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1127 if ((*vdp)->vdev_ops->vdev_op_leaf)
1130 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1133 if (error == ENOENT)
1139 return (SET_ERROR(EINVAL));
1142 for (int c = 0; c < children; c++) {
1144 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1152 ASSERT(*vdp != NULL);
1158 * Opposite of spa_load().
1161 spa_unload(spa_t *spa)
1165 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1170 trim_thread_destroy(spa);
1175 spa_async_suspend(spa);
1180 if (spa->spa_sync_on) {
1181 txg_sync_stop(spa->spa_dsl_pool);
1182 spa->spa_sync_on = B_FALSE;
1186 * Wait for any outstanding async I/O to complete.
1188 if (spa->spa_async_zio_root != NULL) {
1189 (void) zio_wait(spa->spa_async_zio_root);
1190 spa->spa_async_zio_root = NULL;
1193 bpobj_close(&spa->spa_deferred_bpobj);
1196 * Close the dsl pool.
1198 if (spa->spa_dsl_pool) {
1199 dsl_pool_close(spa->spa_dsl_pool);
1200 spa->spa_dsl_pool = NULL;
1201 spa->spa_meta_objset = NULL;
1206 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1209 * Drop and purge level 2 cache
1211 spa_l2cache_drop(spa);
1216 if (spa->spa_root_vdev)
1217 vdev_free(spa->spa_root_vdev);
1218 ASSERT(spa->spa_root_vdev == NULL);
1220 for (i = 0; i < spa->spa_spares.sav_count; i++)
1221 vdev_free(spa->spa_spares.sav_vdevs[i]);
1222 if (spa->spa_spares.sav_vdevs) {
1223 kmem_free(spa->spa_spares.sav_vdevs,
1224 spa->spa_spares.sav_count * sizeof (void *));
1225 spa->spa_spares.sav_vdevs = NULL;
1227 if (spa->spa_spares.sav_config) {
1228 nvlist_free(spa->spa_spares.sav_config);
1229 spa->spa_spares.sav_config = NULL;
1231 spa->spa_spares.sav_count = 0;
1233 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1234 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1235 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1237 if (spa->spa_l2cache.sav_vdevs) {
1238 kmem_free(spa->spa_l2cache.sav_vdevs,
1239 spa->spa_l2cache.sav_count * sizeof (void *));
1240 spa->spa_l2cache.sav_vdevs = NULL;
1242 if (spa->spa_l2cache.sav_config) {
1243 nvlist_free(spa->spa_l2cache.sav_config);
1244 spa->spa_l2cache.sav_config = NULL;
1246 spa->spa_l2cache.sav_count = 0;
1248 spa->spa_async_suspended = 0;
1250 if (spa->spa_comment != NULL) {
1251 spa_strfree(spa->spa_comment);
1252 spa->spa_comment = NULL;
1255 spa_config_exit(spa, SCL_ALL, FTAG);
1259 * Load (or re-load) the current list of vdevs describing the active spares for
1260 * this pool. When this is called, we have some form of basic information in
1261 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1262 * then re-generate a more complete list including status information.
1265 spa_load_spares(spa_t *spa)
1272 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1275 * First, close and free any existing spare vdevs.
1277 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1278 vd = spa->spa_spares.sav_vdevs[i];
1280 /* Undo the call to spa_activate() below */
1281 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1282 B_FALSE)) != NULL && tvd->vdev_isspare)
1283 spa_spare_remove(tvd);
1288 if (spa->spa_spares.sav_vdevs)
1289 kmem_free(spa->spa_spares.sav_vdevs,
1290 spa->spa_spares.sav_count * sizeof (void *));
1292 if (spa->spa_spares.sav_config == NULL)
1295 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1296 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1298 spa->spa_spares.sav_count = (int)nspares;
1299 spa->spa_spares.sav_vdevs = NULL;
1305 * Construct the array of vdevs, opening them to get status in the
1306 * process. For each spare, there is potentially two different vdev_t
1307 * structures associated with it: one in the list of spares (used only
1308 * for basic validation purposes) and one in the active vdev
1309 * configuration (if it's spared in). During this phase we open and
1310 * validate each vdev on the spare list. If the vdev also exists in the
1311 * active configuration, then we also mark this vdev as an active spare.
1313 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1315 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1316 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1317 VDEV_ALLOC_SPARE) == 0);
1320 spa->spa_spares.sav_vdevs[i] = vd;
1322 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1323 B_FALSE)) != NULL) {
1324 if (!tvd->vdev_isspare)
1328 * We only mark the spare active if we were successfully
1329 * able to load the vdev. Otherwise, importing a pool
1330 * with a bad active spare would result in strange
1331 * behavior, because multiple pool would think the spare
1332 * is actively in use.
1334 * There is a vulnerability here to an equally bizarre
1335 * circumstance, where a dead active spare is later
1336 * brought back to life (onlined or otherwise). Given
1337 * the rarity of this scenario, and the extra complexity
1338 * it adds, we ignore the possibility.
1340 if (!vdev_is_dead(tvd))
1341 spa_spare_activate(tvd);
1345 vd->vdev_aux = &spa->spa_spares;
1347 if (vdev_open(vd) != 0)
1350 if (vdev_validate_aux(vd) == 0)
1355 * Recompute the stashed list of spares, with status information
1358 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1359 DATA_TYPE_NVLIST_ARRAY) == 0);
1361 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1363 for (i = 0; i < spa->spa_spares.sav_count; i++)
1364 spares[i] = vdev_config_generate(spa,
1365 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1366 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1367 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1368 for (i = 0; i < spa->spa_spares.sav_count; i++)
1369 nvlist_free(spares[i]);
1370 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1374 * Load (or re-load) the current list of vdevs describing the active l2cache for
1375 * this pool. When this is called, we have some form of basic information in
1376 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1377 * then re-generate a more complete list including status information.
1378 * Devices which are already active have their details maintained, and are
1382 spa_load_l2cache(spa_t *spa)
1386 int i, j, oldnvdevs;
1388 vdev_t *vd, **oldvdevs, **newvdevs;
1389 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1391 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1393 if (sav->sav_config != NULL) {
1394 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1395 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1396 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1402 oldvdevs = sav->sav_vdevs;
1403 oldnvdevs = sav->sav_count;
1404 sav->sav_vdevs = NULL;
1408 * Process new nvlist of vdevs.
1410 for (i = 0; i < nl2cache; i++) {
1411 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1415 for (j = 0; j < oldnvdevs; j++) {
1417 if (vd != NULL && guid == vd->vdev_guid) {
1419 * Retain previous vdev for add/remove ops.
1427 if (newvdevs[i] == NULL) {
1431 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1432 VDEV_ALLOC_L2CACHE) == 0);
1437 * Commit this vdev as an l2cache device,
1438 * even if it fails to open.
1440 spa_l2cache_add(vd);
1445 spa_l2cache_activate(vd);
1447 if (vdev_open(vd) != 0)
1450 (void) vdev_validate_aux(vd);
1452 if (!vdev_is_dead(vd))
1453 l2arc_add_vdev(spa, vd);
1458 * Purge vdevs that were dropped
1460 for (i = 0; i < oldnvdevs; i++) {
1465 ASSERT(vd->vdev_isl2cache);
1467 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1468 pool != 0ULL && l2arc_vdev_present(vd))
1469 l2arc_remove_vdev(vd);
1470 vdev_clear_stats(vd);
1476 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1478 if (sav->sav_config == NULL)
1481 sav->sav_vdevs = newvdevs;
1482 sav->sav_count = (int)nl2cache;
1485 * Recompute the stashed list of l2cache devices, with status
1486 * information this time.
1488 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1489 DATA_TYPE_NVLIST_ARRAY) == 0);
1491 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1492 for (i = 0; i < sav->sav_count; i++)
1493 l2cache[i] = vdev_config_generate(spa,
1494 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1495 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1496 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1498 for (i = 0; i < sav->sav_count; i++)
1499 nvlist_free(l2cache[i]);
1501 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1505 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1508 char *packed = NULL;
1513 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1514 nvsize = *(uint64_t *)db->db_data;
1515 dmu_buf_rele(db, FTAG);
1517 packed = kmem_alloc(nvsize, KM_SLEEP);
1518 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1521 error = nvlist_unpack(packed, nvsize, value, 0);
1522 kmem_free(packed, nvsize);
1528 * Checks to see if the given vdev could not be opened, in which case we post a
1529 * sysevent to notify the autoreplace code that the device has been removed.
1532 spa_check_removed(vdev_t *vd)
1534 for (int c = 0; c < vd->vdev_children; c++)
1535 spa_check_removed(vd->vdev_child[c]);
1537 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1539 zfs_post_autoreplace(vd->vdev_spa, vd);
1540 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1545 * Validate the current config against the MOS config
1548 spa_config_valid(spa_t *spa, nvlist_t *config)
1550 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1553 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1555 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1556 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1558 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1561 * If we're doing a normal import, then build up any additional
1562 * diagnostic information about missing devices in this config.
1563 * We'll pass this up to the user for further processing.
1565 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1566 nvlist_t **child, *nv;
1569 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1571 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1573 for (int c = 0; c < rvd->vdev_children; c++) {
1574 vdev_t *tvd = rvd->vdev_child[c];
1575 vdev_t *mtvd = mrvd->vdev_child[c];
1577 if (tvd->vdev_ops == &vdev_missing_ops &&
1578 mtvd->vdev_ops != &vdev_missing_ops &&
1580 child[idx++] = vdev_config_generate(spa, mtvd,
1585 VERIFY(nvlist_add_nvlist_array(nv,
1586 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1587 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1588 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1590 for (int i = 0; i < idx; i++)
1591 nvlist_free(child[i]);
1594 kmem_free(child, rvd->vdev_children * sizeof (char **));
1598 * Compare the root vdev tree with the information we have
1599 * from the MOS config (mrvd). Check each top-level vdev
1600 * with the corresponding MOS config top-level (mtvd).
1602 for (int c = 0; c < rvd->vdev_children; c++) {
1603 vdev_t *tvd = rvd->vdev_child[c];
1604 vdev_t *mtvd = mrvd->vdev_child[c];
1607 * Resolve any "missing" vdevs in the current configuration.
1608 * If we find that the MOS config has more accurate information
1609 * about the top-level vdev then use that vdev instead.
1611 if (tvd->vdev_ops == &vdev_missing_ops &&
1612 mtvd->vdev_ops != &vdev_missing_ops) {
1614 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1618 * Device specific actions.
1620 if (mtvd->vdev_islog) {
1621 spa_set_log_state(spa, SPA_LOG_CLEAR);
1624 * XXX - once we have 'readonly' pool
1625 * support we should be able to handle
1626 * missing data devices by transitioning
1627 * the pool to readonly.
1633 * Swap the missing vdev with the data we were
1634 * able to obtain from the MOS config.
1636 vdev_remove_child(rvd, tvd);
1637 vdev_remove_child(mrvd, mtvd);
1639 vdev_add_child(rvd, mtvd);
1640 vdev_add_child(mrvd, tvd);
1642 spa_config_exit(spa, SCL_ALL, FTAG);
1644 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1647 } else if (mtvd->vdev_islog) {
1649 * Load the slog device's state from the MOS config
1650 * since it's possible that the label does not
1651 * contain the most up-to-date information.
1653 vdev_load_log_state(tvd, mtvd);
1658 spa_config_exit(spa, SCL_ALL, FTAG);
1661 * Ensure we were able to validate the config.
1663 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1667 * Check for missing log devices
1670 spa_check_logs(spa_t *spa)
1672 boolean_t rv = B_FALSE;
1674 switch (spa->spa_log_state) {
1675 case SPA_LOG_MISSING:
1676 /* need to recheck in case slog has been restored */
1677 case SPA_LOG_UNKNOWN:
1678 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1679 NULL, DS_FIND_CHILDREN) != 0);
1681 spa_set_log_state(spa, SPA_LOG_MISSING);
1688 spa_passivate_log(spa_t *spa)
1690 vdev_t *rvd = spa->spa_root_vdev;
1691 boolean_t slog_found = B_FALSE;
1693 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1695 if (!spa_has_slogs(spa))
1698 for (int c = 0; c < rvd->vdev_children; c++) {
1699 vdev_t *tvd = rvd->vdev_child[c];
1700 metaslab_group_t *mg = tvd->vdev_mg;
1702 if (tvd->vdev_islog) {
1703 metaslab_group_passivate(mg);
1704 slog_found = B_TRUE;
1708 return (slog_found);
1712 spa_activate_log(spa_t *spa)
1714 vdev_t *rvd = spa->spa_root_vdev;
1716 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1718 for (int c = 0; c < rvd->vdev_children; c++) {
1719 vdev_t *tvd = rvd->vdev_child[c];
1720 metaslab_group_t *mg = tvd->vdev_mg;
1722 if (tvd->vdev_islog)
1723 metaslab_group_activate(mg);
1728 spa_offline_log(spa_t *spa)
1732 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1733 NULL, DS_FIND_CHILDREN);
1736 * We successfully offlined the log device, sync out the
1737 * current txg so that the "stubby" block can be removed
1740 txg_wait_synced(spa->spa_dsl_pool, 0);
1746 spa_aux_check_removed(spa_aux_vdev_t *sav)
1750 for (i = 0; i < sav->sav_count; i++)
1751 spa_check_removed(sav->sav_vdevs[i]);
1755 spa_claim_notify(zio_t *zio)
1757 spa_t *spa = zio->io_spa;
1762 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1763 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1764 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1765 mutex_exit(&spa->spa_props_lock);
1768 typedef struct spa_load_error {
1769 uint64_t sle_meta_count;
1770 uint64_t sle_data_count;
1774 spa_load_verify_done(zio_t *zio)
1776 blkptr_t *bp = zio->io_bp;
1777 spa_load_error_t *sle = zio->io_private;
1778 dmu_object_type_t type = BP_GET_TYPE(bp);
1779 int error = zio->io_error;
1782 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1783 type != DMU_OT_INTENT_LOG)
1784 atomic_add_64(&sle->sle_meta_count, 1);
1786 atomic_add_64(&sle->sle_data_count, 1);
1788 zio_data_buf_free(zio->io_data, zio->io_size);
1793 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1794 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1798 size_t size = BP_GET_PSIZE(bp);
1799 void *data = zio_data_buf_alloc(size);
1801 zio_nowait(zio_read(rio, spa, bp, data, size,
1802 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1803 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1804 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1810 spa_load_verify(spa_t *spa)
1813 spa_load_error_t sle = { 0 };
1814 zpool_rewind_policy_t policy;
1815 boolean_t verify_ok = B_FALSE;
1818 zpool_get_rewind_policy(spa->spa_config, &policy);
1820 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1823 rio = zio_root(spa, NULL, &sle,
1824 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1826 error = traverse_pool(spa, spa->spa_verify_min_txg,
1827 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1829 (void) zio_wait(rio);
1831 spa->spa_load_meta_errors = sle.sle_meta_count;
1832 spa->spa_load_data_errors = sle.sle_data_count;
1834 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1835 sle.sle_data_count <= policy.zrp_maxdata) {
1839 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1840 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1842 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1843 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1844 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1845 VERIFY(nvlist_add_int64(spa->spa_load_info,
1846 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1847 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1848 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1850 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1854 if (error != ENXIO && error != EIO)
1855 error = SET_ERROR(EIO);
1859 return (verify_ok ? 0 : EIO);
1863 * Find a value in the pool props object.
1866 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1868 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1869 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1873 * Find a value in the pool directory object.
1876 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1878 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1879 name, sizeof (uint64_t), 1, val));
1883 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1885 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1890 * Fix up config after a partly-completed split. This is done with the
1891 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1892 * pool have that entry in their config, but only the splitting one contains
1893 * a list of all the guids of the vdevs that are being split off.
1895 * This function determines what to do with that list: either rejoin
1896 * all the disks to the pool, or complete the splitting process. To attempt
1897 * the rejoin, each disk that is offlined is marked online again, and
1898 * we do a reopen() call. If the vdev label for every disk that was
1899 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1900 * then we call vdev_split() on each disk, and complete the split.
1902 * Otherwise we leave the config alone, with all the vdevs in place in
1903 * the original pool.
1906 spa_try_repair(spa_t *spa, nvlist_t *config)
1913 boolean_t attempt_reopen;
1915 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1918 /* check that the config is complete */
1919 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1920 &glist, &gcount) != 0)
1923 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1925 /* attempt to online all the vdevs & validate */
1926 attempt_reopen = B_TRUE;
1927 for (i = 0; i < gcount; i++) {
1928 if (glist[i] == 0) /* vdev is hole */
1931 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1932 if (vd[i] == NULL) {
1934 * Don't bother attempting to reopen the disks;
1935 * just do the split.
1937 attempt_reopen = B_FALSE;
1939 /* attempt to re-online it */
1940 vd[i]->vdev_offline = B_FALSE;
1944 if (attempt_reopen) {
1945 vdev_reopen(spa->spa_root_vdev);
1947 /* check each device to see what state it's in */
1948 for (extracted = 0, i = 0; i < gcount; i++) {
1949 if (vd[i] != NULL &&
1950 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1957 * If every disk has been moved to the new pool, or if we never
1958 * even attempted to look at them, then we split them off for
1961 if (!attempt_reopen || gcount == extracted) {
1962 for (i = 0; i < gcount; i++)
1965 vdev_reopen(spa->spa_root_vdev);
1968 kmem_free(vd, gcount * sizeof (vdev_t *));
1972 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1973 boolean_t mosconfig)
1975 nvlist_t *config = spa->spa_config;
1976 char *ereport = FM_EREPORT_ZFS_POOL;
1982 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1983 return (SET_ERROR(EINVAL));
1985 ASSERT(spa->spa_comment == NULL);
1986 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1987 spa->spa_comment = spa_strdup(comment);
1990 * Versioning wasn't explicitly added to the label until later, so if
1991 * it's not present treat it as the initial version.
1993 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1994 &spa->spa_ubsync.ub_version) != 0)
1995 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1997 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1998 &spa->spa_config_txg);
2000 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2001 spa_guid_exists(pool_guid, 0)) {
2002 error = SET_ERROR(EEXIST);
2004 spa->spa_config_guid = pool_guid;
2006 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2008 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2012 nvlist_free(spa->spa_load_info);
2013 spa->spa_load_info = fnvlist_alloc();
2015 gethrestime(&spa->spa_loaded_ts);
2016 error = spa_load_impl(spa, pool_guid, config, state, type,
2017 mosconfig, &ereport);
2020 spa->spa_minref = refcount_count(&spa->spa_refcount);
2022 if (error != EEXIST) {
2023 spa->spa_loaded_ts.tv_sec = 0;
2024 spa->spa_loaded_ts.tv_nsec = 0;
2026 if (error != EBADF) {
2027 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2030 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2037 * Load an existing storage pool, using the pool's builtin spa_config as a
2038 * source of configuration information.
2041 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2042 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2046 nvlist_t *nvroot = NULL;
2049 uberblock_t *ub = &spa->spa_uberblock;
2050 uint64_t children, config_cache_txg = spa->spa_config_txg;
2051 int orig_mode = spa->spa_mode;
2054 boolean_t missing_feat_write = B_FALSE;
2057 * If this is an untrusted config, access the pool in read-only mode.
2058 * This prevents things like resilvering recently removed devices.
2061 spa->spa_mode = FREAD;
2063 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2065 spa->spa_load_state = state;
2067 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2068 return (SET_ERROR(EINVAL));
2070 parse = (type == SPA_IMPORT_EXISTING ?
2071 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2074 * Create "The Godfather" zio to hold all async IOs
2076 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2077 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2080 * Parse the configuration into a vdev tree. We explicitly set the
2081 * value that will be returned by spa_version() since parsing the
2082 * configuration requires knowing the version number.
2084 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2085 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2086 spa_config_exit(spa, SCL_ALL, FTAG);
2091 ASSERT(spa->spa_root_vdev == rvd);
2093 if (type != SPA_IMPORT_ASSEMBLE) {
2094 ASSERT(spa_guid(spa) == pool_guid);
2098 * Try to open all vdevs, loading each label in the process.
2100 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2101 error = vdev_open(rvd);
2102 spa_config_exit(spa, SCL_ALL, FTAG);
2107 * We need to validate the vdev labels against the configuration that
2108 * we have in hand, which is dependent on the setting of mosconfig. If
2109 * mosconfig is true then we're validating the vdev labels based on
2110 * that config. Otherwise, we're validating against the cached config
2111 * (zpool.cache) that was read when we loaded the zfs module, and then
2112 * later we will recursively call spa_load() and validate against
2115 * If we're assembling a new pool that's been split off from an
2116 * existing pool, the labels haven't yet been updated so we skip
2117 * validation for now.
2119 if (type != SPA_IMPORT_ASSEMBLE) {
2120 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2121 error = vdev_validate(rvd, mosconfig);
2122 spa_config_exit(spa, SCL_ALL, FTAG);
2127 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2128 return (SET_ERROR(ENXIO));
2132 * Find the best uberblock.
2134 vdev_uberblock_load(rvd, ub, &label);
2137 * If we weren't able to find a single valid uberblock, return failure.
2139 if (ub->ub_txg == 0) {
2141 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2145 * If the pool has an unsupported version we can't open it.
2147 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2149 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2152 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2156 * If we weren't able to find what's necessary for reading the
2157 * MOS in the label, return failure.
2159 if (label == NULL || nvlist_lookup_nvlist(label,
2160 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2162 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2167 * Update our in-core representation with the definitive values
2170 nvlist_free(spa->spa_label_features);
2171 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2177 * Look through entries in the label nvlist's features_for_read. If
2178 * there is a feature listed there which we don't understand then we
2179 * cannot open a pool.
2181 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2182 nvlist_t *unsup_feat;
2184 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2187 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2189 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2190 if (!zfeature_is_supported(nvpair_name(nvp))) {
2191 VERIFY(nvlist_add_string(unsup_feat,
2192 nvpair_name(nvp), "") == 0);
2196 if (!nvlist_empty(unsup_feat)) {
2197 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2198 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2199 nvlist_free(unsup_feat);
2200 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2204 nvlist_free(unsup_feat);
2208 * If the vdev guid sum doesn't match the uberblock, we have an
2209 * incomplete configuration. We first check to see if the pool
2210 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2211 * If it is, defer the vdev_guid_sum check till later so we
2212 * can handle missing vdevs.
2214 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2215 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2216 rvd->vdev_guid_sum != ub->ub_guid_sum)
2217 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2219 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2220 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2221 spa_try_repair(spa, config);
2222 spa_config_exit(spa, SCL_ALL, FTAG);
2223 nvlist_free(spa->spa_config_splitting);
2224 spa->spa_config_splitting = NULL;
2228 * Initialize internal SPA structures.
2230 spa->spa_state = POOL_STATE_ACTIVE;
2231 spa->spa_ubsync = spa->spa_uberblock;
2232 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2233 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2234 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2235 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2236 spa->spa_claim_max_txg = spa->spa_first_txg;
2237 spa->spa_prev_software_version = ub->ub_software_version;
2239 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2241 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2242 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2244 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2245 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2247 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2248 boolean_t missing_feat_read = B_FALSE;
2249 nvlist_t *unsup_feat, *enabled_feat;
2251 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2252 &spa->spa_feat_for_read_obj) != 0) {
2253 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2256 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2257 &spa->spa_feat_for_write_obj) != 0) {
2258 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2261 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2262 &spa->spa_feat_desc_obj) != 0) {
2263 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2266 enabled_feat = fnvlist_alloc();
2267 unsup_feat = fnvlist_alloc();
2269 if (!feature_is_supported(spa->spa_meta_objset,
2270 spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj,
2271 unsup_feat, enabled_feat))
2272 missing_feat_read = B_TRUE;
2274 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2275 if (!feature_is_supported(spa->spa_meta_objset,
2276 spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj,
2277 unsup_feat, enabled_feat)) {
2278 missing_feat_write = B_TRUE;
2282 fnvlist_add_nvlist(spa->spa_load_info,
2283 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2285 if (!nvlist_empty(unsup_feat)) {
2286 fnvlist_add_nvlist(spa->spa_load_info,
2287 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2290 fnvlist_free(enabled_feat);
2291 fnvlist_free(unsup_feat);
2293 if (!missing_feat_read) {
2294 fnvlist_add_boolean(spa->spa_load_info,
2295 ZPOOL_CONFIG_CAN_RDONLY);
2299 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2300 * twofold: to determine whether the pool is available for
2301 * import in read-write mode and (if it is not) whether the
2302 * pool is available for import in read-only mode. If the pool
2303 * is available for import in read-write mode, it is displayed
2304 * as available in userland; if it is not available for import
2305 * in read-only mode, it is displayed as unavailable in
2306 * userland. If the pool is available for import in read-only
2307 * mode but not read-write mode, it is displayed as unavailable
2308 * in userland with a special note that the pool is actually
2309 * available for open in read-only mode.
2311 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2312 * missing a feature for write, we must first determine whether
2313 * the pool can be opened read-only before returning to
2314 * userland in order to know whether to display the
2315 * abovementioned note.
2317 if (missing_feat_read || (missing_feat_write &&
2318 spa_writeable(spa))) {
2319 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2324 spa->spa_is_initializing = B_TRUE;
2325 error = dsl_pool_open(spa->spa_dsl_pool);
2326 spa->spa_is_initializing = B_FALSE;
2328 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2332 nvlist_t *policy = NULL, *nvconfig;
2334 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2335 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2337 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2338 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2340 unsigned long myhostid = 0;
2342 VERIFY(nvlist_lookup_string(nvconfig,
2343 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2346 myhostid = zone_get_hostid(NULL);
2349 * We're emulating the system's hostid in userland, so
2350 * we can't use zone_get_hostid().
2352 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2353 #endif /* _KERNEL */
2354 if (check_hostid && hostid != 0 && myhostid != 0 &&
2355 hostid != myhostid) {
2356 nvlist_free(nvconfig);
2357 cmn_err(CE_WARN, "pool '%s' could not be "
2358 "loaded as it was last accessed by "
2359 "another system (host: %s hostid: 0x%lx). "
2360 "See: http://illumos.org/msg/ZFS-8000-EY",
2361 spa_name(spa), hostname,
2362 (unsigned long)hostid);
2363 return (SET_ERROR(EBADF));
2366 if (nvlist_lookup_nvlist(spa->spa_config,
2367 ZPOOL_REWIND_POLICY, &policy) == 0)
2368 VERIFY(nvlist_add_nvlist(nvconfig,
2369 ZPOOL_REWIND_POLICY, policy) == 0);
2371 spa_config_set(spa, nvconfig);
2373 spa_deactivate(spa);
2374 spa_activate(spa, orig_mode);
2376 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2379 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2380 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2381 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2383 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2386 * Load the bit that tells us to use the new accounting function
2387 * (raid-z deflation). If we have an older pool, this will not
2390 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2391 if (error != 0 && error != ENOENT)
2392 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2394 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2395 &spa->spa_creation_version);
2396 if (error != 0 && error != ENOENT)
2397 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2400 * Load the persistent error log. If we have an older pool, this will
2403 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2404 if (error != 0 && error != ENOENT)
2405 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2407 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2408 &spa->spa_errlog_scrub);
2409 if (error != 0 && error != ENOENT)
2410 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2413 * Load the history object. If we have an older pool, this
2414 * will not be present.
2416 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2417 if (error != 0 && error != ENOENT)
2418 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2421 * If we're assembling the pool from the split-off vdevs of
2422 * an existing pool, we don't want to attach the spares & cache
2427 * Load any hot spares for this pool.
2429 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2430 if (error != 0 && error != ENOENT)
2431 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2432 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2433 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2434 if (load_nvlist(spa, spa->spa_spares.sav_object,
2435 &spa->spa_spares.sav_config) != 0)
2436 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2438 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2439 spa_load_spares(spa);
2440 spa_config_exit(spa, SCL_ALL, FTAG);
2441 } else if (error == 0) {
2442 spa->spa_spares.sav_sync = B_TRUE;
2446 * Load any level 2 ARC devices for this pool.
2448 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2449 &spa->spa_l2cache.sav_object);
2450 if (error != 0 && error != ENOENT)
2451 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2452 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2453 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2454 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2455 &spa->spa_l2cache.sav_config) != 0)
2456 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2458 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2459 spa_load_l2cache(spa);
2460 spa_config_exit(spa, SCL_ALL, FTAG);
2461 } else if (error == 0) {
2462 spa->spa_l2cache.sav_sync = B_TRUE;
2465 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2467 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2468 if (error && error != ENOENT)
2469 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2472 uint64_t autoreplace;
2474 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2475 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2476 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2477 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2478 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2479 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2480 &spa->spa_dedup_ditto);
2482 spa->spa_autoreplace = (autoreplace != 0);
2486 * If the 'autoreplace' property is set, then post a resource notifying
2487 * the ZFS DE that it should not issue any faults for unopenable
2488 * devices. We also iterate over the vdevs, and post a sysevent for any
2489 * unopenable vdevs so that the normal autoreplace handler can take
2492 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2493 spa_check_removed(spa->spa_root_vdev);
2495 * For the import case, this is done in spa_import(), because
2496 * at this point we're using the spare definitions from
2497 * the MOS config, not necessarily from the userland config.
2499 if (state != SPA_LOAD_IMPORT) {
2500 spa_aux_check_removed(&spa->spa_spares);
2501 spa_aux_check_removed(&spa->spa_l2cache);
2506 * Load the vdev state for all toplevel vdevs.
2511 * Propagate the leaf DTLs we just loaded all the way up the tree.
2513 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2514 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2515 spa_config_exit(spa, SCL_ALL, FTAG);
2518 * Load the DDTs (dedup tables).
2520 error = ddt_load(spa);
2522 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2524 spa_update_dspace(spa);
2527 * Validate the config, using the MOS config to fill in any
2528 * information which might be missing. If we fail to validate
2529 * the config then declare the pool unfit for use. If we're
2530 * assembling a pool from a split, the log is not transferred
2533 if (type != SPA_IMPORT_ASSEMBLE) {
2536 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2537 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2539 if (!spa_config_valid(spa, nvconfig)) {
2540 nvlist_free(nvconfig);
2541 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2544 nvlist_free(nvconfig);
2547 * Now that we've validated the config, check the state of the
2548 * root vdev. If it can't be opened, it indicates one or
2549 * more toplevel vdevs are faulted.
2551 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2552 return (SET_ERROR(ENXIO));
2554 if (spa_check_logs(spa)) {
2555 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2556 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2560 if (missing_feat_write) {
2561 ASSERT(state == SPA_LOAD_TRYIMPORT);
2564 * At this point, we know that we can open the pool in
2565 * read-only mode but not read-write mode. We now have enough
2566 * information and can return to userland.
2568 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2572 * We've successfully opened the pool, verify that we're ready
2573 * to start pushing transactions.
2575 if (state != SPA_LOAD_TRYIMPORT) {
2576 if (error = spa_load_verify(spa))
2577 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2581 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2582 spa->spa_load_max_txg == UINT64_MAX)) {
2584 int need_update = B_FALSE;
2586 ASSERT(state != SPA_LOAD_TRYIMPORT);
2589 * Claim log blocks that haven't been committed yet.
2590 * This must all happen in a single txg.
2591 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2592 * invoked from zil_claim_log_block()'s i/o done callback.
2593 * Price of rollback is that we abandon the log.
2595 spa->spa_claiming = B_TRUE;
2597 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2598 spa_first_txg(spa));
2599 (void) dmu_objset_find(spa_name(spa),
2600 zil_claim, tx, DS_FIND_CHILDREN);
2603 spa->spa_claiming = B_FALSE;
2605 spa_set_log_state(spa, SPA_LOG_GOOD);
2606 spa->spa_sync_on = B_TRUE;
2607 txg_sync_start(spa->spa_dsl_pool);
2610 * Wait for all claims to sync. We sync up to the highest
2611 * claimed log block birth time so that claimed log blocks
2612 * don't appear to be from the future. spa_claim_max_txg
2613 * will have been set for us by either zil_check_log_chain()
2614 * (invoked from spa_check_logs()) or zil_claim() above.
2616 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2619 * If the config cache is stale, or we have uninitialized
2620 * metaslabs (see spa_vdev_add()), then update the config.
2622 * If this is a verbatim import, trust the current
2623 * in-core spa_config and update the disk labels.
2625 if (config_cache_txg != spa->spa_config_txg ||
2626 state == SPA_LOAD_IMPORT ||
2627 state == SPA_LOAD_RECOVER ||
2628 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2629 need_update = B_TRUE;
2631 for (int c = 0; c < rvd->vdev_children; c++)
2632 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2633 need_update = B_TRUE;
2636 * Update the config cache asychronously in case we're the
2637 * root pool, in which case the config cache isn't writable yet.
2640 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2643 * Check all DTLs to see if anything needs resilvering.
2645 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2646 vdev_resilver_needed(rvd, NULL, NULL))
2647 spa_async_request(spa, SPA_ASYNC_RESILVER);
2650 * Log the fact that we booted up (so that we can detect if
2651 * we rebooted in the middle of an operation).
2653 spa_history_log_version(spa, "open");
2656 * Delete any inconsistent datasets.
2658 (void) dmu_objset_find(spa_name(spa),
2659 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2662 * Clean up any stale temporary dataset userrefs.
2664 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2671 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2673 int mode = spa->spa_mode;
2676 spa_deactivate(spa);
2678 spa->spa_load_max_txg--;
2680 spa_activate(spa, mode);
2681 spa_async_suspend(spa);
2683 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2687 * If spa_load() fails this function will try loading prior txg's. If
2688 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2689 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2690 * function will not rewind the pool and will return the same error as
2694 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2695 uint64_t max_request, int rewind_flags)
2697 nvlist_t *loadinfo = NULL;
2698 nvlist_t *config = NULL;
2699 int load_error, rewind_error;
2700 uint64_t safe_rewind_txg;
2703 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2704 spa->spa_load_max_txg = spa->spa_load_txg;
2705 spa_set_log_state(spa, SPA_LOG_CLEAR);
2707 spa->spa_load_max_txg = max_request;
2710 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2712 if (load_error == 0)
2715 if (spa->spa_root_vdev != NULL)
2716 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2718 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2719 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2721 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2722 nvlist_free(config);
2723 return (load_error);
2726 if (state == SPA_LOAD_RECOVER) {
2727 /* Price of rolling back is discarding txgs, including log */
2728 spa_set_log_state(spa, SPA_LOG_CLEAR);
2731 * If we aren't rolling back save the load info from our first
2732 * import attempt so that we can restore it after attempting
2735 loadinfo = spa->spa_load_info;
2736 spa->spa_load_info = fnvlist_alloc();
2739 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2740 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2741 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2742 TXG_INITIAL : safe_rewind_txg;
2745 * Continue as long as we're finding errors, we're still within
2746 * the acceptable rewind range, and we're still finding uberblocks
2748 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2749 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2750 if (spa->spa_load_max_txg < safe_rewind_txg)
2751 spa->spa_extreme_rewind = B_TRUE;
2752 rewind_error = spa_load_retry(spa, state, mosconfig);
2755 spa->spa_extreme_rewind = B_FALSE;
2756 spa->spa_load_max_txg = UINT64_MAX;
2758 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2759 spa_config_set(spa, config);
2761 if (state == SPA_LOAD_RECOVER) {
2762 ASSERT3P(loadinfo, ==, NULL);
2763 return (rewind_error);
2765 /* Store the rewind info as part of the initial load info */
2766 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2767 spa->spa_load_info);
2769 /* Restore the initial load info */
2770 fnvlist_free(spa->spa_load_info);
2771 spa->spa_load_info = loadinfo;
2773 return (load_error);
2780 * The import case is identical to an open except that the configuration is sent
2781 * down from userland, instead of grabbed from the configuration cache. For the
2782 * case of an open, the pool configuration will exist in the
2783 * POOL_STATE_UNINITIALIZED state.
2785 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2786 * the same time open the pool, without having to keep around the spa_t in some
2790 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2794 spa_load_state_t state = SPA_LOAD_OPEN;
2796 int locked = B_FALSE;
2797 int firstopen = B_FALSE;
2802 * As disgusting as this is, we need to support recursive calls to this
2803 * function because dsl_dir_open() is called during spa_load(), and ends
2804 * up calling spa_open() again. The real fix is to figure out how to
2805 * avoid dsl_dir_open() calling this in the first place.
2807 if (mutex_owner(&spa_namespace_lock) != curthread) {
2808 mutex_enter(&spa_namespace_lock);
2812 if ((spa = spa_lookup(pool)) == NULL) {
2814 mutex_exit(&spa_namespace_lock);
2815 return (SET_ERROR(ENOENT));
2818 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2819 zpool_rewind_policy_t policy;
2823 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2825 if (policy.zrp_request & ZPOOL_DO_REWIND)
2826 state = SPA_LOAD_RECOVER;
2828 spa_activate(spa, spa_mode_global);
2830 if (state != SPA_LOAD_RECOVER)
2831 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2833 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2834 policy.zrp_request);
2836 if (error == EBADF) {
2838 * If vdev_validate() returns failure (indicated by
2839 * EBADF), it indicates that one of the vdevs indicates
2840 * that the pool has been exported or destroyed. If
2841 * this is the case, the config cache is out of sync and
2842 * we should remove the pool from the namespace.
2845 spa_deactivate(spa);
2846 spa_config_sync(spa, B_TRUE, B_TRUE);
2849 mutex_exit(&spa_namespace_lock);
2850 return (SET_ERROR(ENOENT));
2855 * We can't open the pool, but we still have useful
2856 * information: the state of each vdev after the
2857 * attempted vdev_open(). Return this to the user.
2859 if (config != NULL && spa->spa_config) {
2860 VERIFY(nvlist_dup(spa->spa_config, config,
2862 VERIFY(nvlist_add_nvlist(*config,
2863 ZPOOL_CONFIG_LOAD_INFO,
2864 spa->spa_load_info) == 0);
2867 spa_deactivate(spa);
2868 spa->spa_last_open_failed = error;
2870 mutex_exit(&spa_namespace_lock);
2876 spa_open_ref(spa, tag);
2879 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2882 * If we've recovered the pool, pass back any information we
2883 * gathered while doing the load.
2885 if (state == SPA_LOAD_RECOVER) {
2886 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2887 spa->spa_load_info) == 0);
2891 spa->spa_last_open_failed = 0;
2892 spa->spa_last_ubsync_txg = 0;
2893 spa->spa_load_txg = 0;
2894 mutex_exit(&spa_namespace_lock);
2898 zvol_create_minors(spa->spa_name);
2909 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2912 return (spa_open_common(name, spapp, tag, policy, config));
2916 spa_open(const char *name, spa_t **spapp, void *tag)
2918 return (spa_open_common(name, spapp, tag, NULL, NULL));
2922 * Lookup the given spa_t, incrementing the inject count in the process,
2923 * preventing it from being exported or destroyed.
2926 spa_inject_addref(char *name)
2930 mutex_enter(&spa_namespace_lock);
2931 if ((spa = spa_lookup(name)) == NULL) {
2932 mutex_exit(&spa_namespace_lock);
2935 spa->spa_inject_ref++;
2936 mutex_exit(&spa_namespace_lock);
2942 spa_inject_delref(spa_t *spa)
2944 mutex_enter(&spa_namespace_lock);
2945 spa->spa_inject_ref--;
2946 mutex_exit(&spa_namespace_lock);
2950 * Add spares device information to the nvlist.
2953 spa_add_spares(spa_t *spa, nvlist_t *config)
2963 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2965 if (spa->spa_spares.sav_count == 0)
2968 VERIFY(nvlist_lookup_nvlist(config,
2969 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2970 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2971 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2973 VERIFY(nvlist_add_nvlist_array(nvroot,
2974 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2975 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2976 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2979 * Go through and find any spares which have since been
2980 * repurposed as an active spare. If this is the case, update
2981 * their status appropriately.
2983 for (i = 0; i < nspares; i++) {
2984 VERIFY(nvlist_lookup_uint64(spares[i],
2985 ZPOOL_CONFIG_GUID, &guid) == 0);
2986 if (spa_spare_exists(guid, &pool, NULL) &&
2988 VERIFY(nvlist_lookup_uint64_array(
2989 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2990 (uint64_t **)&vs, &vsc) == 0);
2991 vs->vs_state = VDEV_STATE_CANT_OPEN;
2992 vs->vs_aux = VDEV_AUX_SPARED;
2999 * Add l2cache device information to the nvlist, including vdev stats.
3002 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3005 uint_t i, j, nl2cache;
3012 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3014 if (spa->spa_l2cache.sav_count == 0)
3017 VERIFY(nvlist_lookup_nvlist(config,
3018 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3019 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3020 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3021 if (nl2cache != 0) {
3022 VERIFY(nvlist_add_nvlist_array(nvroot,
3023 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3024 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3025 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3028 * Update level 2 cache device stats.
3031 for (i = 0; i < nl2cache; i++) {
3032 VERIFY(nvlist_lookup_uint64(l2cache[i],
3033 ZPOOL_CONFIG_GUID, &guid) == 0);
3036 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3038 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3039 vd = spa->spa_l2cache.sav_vdevs[j];
3045 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3046 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3048 vdev_get_stats(vd, vs);
3054 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3060 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3061 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3063 if (spa->spa_feat_for_read_obj != 0) {
3064 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3065 spa->spa_feat_for_read_obj);
3066 zap_cursor_retrieve(&zc, &za) == 0;
3067 zap_cursor_advance(&zc)) {
3068 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3069 za.za_num_integers == 1);
3070 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3071 za.za_first_integer));
3073 zap_cursor_fini(&zc);
3076 if (spa->spa_feat_for_write_obj != 0) {
3077 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3078 spa->spa_feat_for_write_obj);
3079 zap_cursor_retrieve(&zc, &za) == 0;
3080 zap_cursor_advance(&zc)) {
3081 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3082 za.za_num_integers == 1);
3083 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3084 za.za_first_integer));
3086 zap_cursor_fini(&zc);
3089 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3091 nvlist_free(features);
3095 spa_get_stats(const char *name, nvlist_t **config,
3096 char *altroot, size_t buflen)
3102 error = spa_open_common(name, &spa, FTAG, NULL, config);
3106 * This still leaves a window of inconsistency where the spares
3107 * or l2cache devices could change and the config would be
3108 * self-inconsistent.
3110 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3112 if (*config != NULL) {
3113 uint64_t loadtimes[2];
3115 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3116 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3117 VERIFY(nvlist_add_uint64_array(*config,
3118 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3120 VERIFY(nvlist_add_uint64(*config,
3121 ZPOOL_CONFIG_ERRCOUNT,
3122 spa_get_errlog_size(spa)) == 0);
3124 if (spa_suspended(spa))
3125 VERIFY(nvlist_add_uint64(*config,
3126 ZPOOL_CONFIG_SUSPENDED,
3127 spa->spa_failmode) == 0);
3129 spa_add_spares(spa, *config);
3130 spa_add_l2cache(spa, *config);
3131 spa_add_feature_stats(spa, *config);
3136 * We want to get the alternate root even for faulted pools, so we cheat
3137 * and call spa_lookup() directly.
3141 mutex_enter(&spa_namespace_lock);
3142 spa = spa_lookup(name);
3144 spa_altroot(spa, altroot, buflen);
3148 mutex_exit(&spa_namespace_lock);
3150 spa_altroot(spa, altroot, buflen);
3155 spa_config_exit(spa, SCL_CONFIG, FTAG);
3156 spa_close(spa, FTAG);
3163 * Validate that the auxiliary device array is well formed. We must have an
3164 * array of nvlists, each which describes a valid leaf vdev. If this is an
3165 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3166 * specified, as long as they are well-formed.
3169 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3170 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3171 vdev_labeltype_t label)
3178 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3181 * It's acceptable to have no devs specified.
3183 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3187 return (SET_ERROR(EINVAL));
3190 * Make sure the pool is formatted with a version that supports this
3193 if (spa_version(spa) < version)
3194 return (SET_ERROR(ENOTSUP));
3197 * Set the pending device list so we correctly handle device in-use
3200 sav->sav_pending = dev;
3201 sav->sav_npending = ndev;
3203 for (i = 0; i < ndev; i++) {
3204 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3208 if (!vd->vdev_ops->vdev_op_leaf) {
3210 error = SET_ERROR(EINVAL);
3215 * The L2ARC currently only supports disk devices in
3216 * kernel context. For user-level testing, we allow it.
3219 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3220 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3221 error = SET_ERROR(ENOTBLK);
3228 if ((error = vdev_open(vd)) == 0 &&
3229 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3230 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3231 vd->vdev_guid) == 0);
3237 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3244 sav->sav_pending = NULL;
3245 sav->sav_npending = 0;
3250 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3254 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3256 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3257 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3258 VDEV_LABEL_SPARE)) != 0) {
3262 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3263 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3264 VDEV_LABEL_L2CACHE));
3268 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3273 if (sav->sav_config != NULL) {
3279 * Generate new dev list by concatentating with the
3282 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3283 &olddevs, &oldndevs) == 0);
3285 newdevs = kmem_alloc(sizeof (void *) *
3286 (ndevs + oldndevs), KM_SLEEP);
3287 for (i = 0; i < oldndevs; i++)
3288 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3290 for (i = 0; i < ndevs; i++)
3291 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3294 VERIFY(nvlist_remove(sav->sav_config, config,
3295 DATA_TYPE_NVLIST_ARRAY) == 0);
3297 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3298 config, newdevs, ndevs + oldndevs) == 0);
3299 for (i = 0; i < oldndevs + ndevs; i++)
3300 nvlist_free(newdevs[i]);
3301 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3304 * Generate a new dev list.
3306 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3308 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3314 * Stop and drop level 2 ARC devices
3317 spa_l2cache_drop(spa_t *spa)
3321 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3323 for (i = 0; i < sav->sav_count; i++) {
3326 vd = sav->sav_vdevs[i];
3329 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3330 pool != 0ULL && l2arc_vdev_present(vd))
3331 l2arc_remove_vdev(vd);
3339 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3343 char *altroot = NULL;
3348 uint64_t txg = TXG_INITIAL;
3349 nvlist_t **spares, **l2cache;
3350 uint_t nspares, nl2cache;
3351 uint64_t version, obj;
3352 boolean_t has_features;
3355 * If this pool already exists, return failure.
3357 mutex_enter(&spa_namespace_lock);
3358 if (spa_lookup(pool) != NULL) {
3359 mutex_exit(&spa_namespace_lock);
3360 return (SET_ERROR(EEXIST));
3364 * Allocate a new spa_t structure.
3366 (void) nvlist_lookup_string(props,
3367 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3368 spa = spa_add(pool, NULL, altroot);
3369 spa_activate(spa, spa_mode_global);
3371 if (props && (error = spa_prop_validate(spa, props))) {
3372 spa_deactivate(spa);
3374 mutex_exit(&spa_namespace_lock);
3378 has_features = B_FALSE;
3379 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3380 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3381 if (zpool_prop_feature(nvpair_name(elem)))
3382 has_features = B_TRUE;
3385 if (has_features || nvlist_lookup_uint64(props,
3386 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3387 version = SPA_VERSION;
3389 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3391 spa->spa_first_txg = txg;
3392 spa->spa_uberblock.ub_txg = txg - 1;
3393 spa->spa_uberblock.ub_version = version;
3394 spa->spa_ubsync = spa->spa_uberblock;
3397 * Create "The Godfather" zio to hold all async IOs
3399 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3400 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3403 * Create the root vdev.
3405 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3407 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3409 ASSERT(error != 0 || rvd != NULL);
3410 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3412 if (error == 0 && !zfs_allocatable_devs(nvroot))
3413 error = SET_ERROR(EINVAL);
3416 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3417 (error = spa_validate_aux(spa, nvroot, txg,
3418 VDEV_ALLOC_ADD)) == 0) {
3419 for (int c = 0; c < rvd->vdev_children; c++) {
3420 vdev_metaslab_set_size(rvd->vdev_child[c]);
3421 vdev_expand(rvd->vdev_child[c], txg);
3425 spa_config_exit(spa, SCL_ALL, FTAG);
3429 spa_deactivate(spa);
3431 mutex_exit(&spa_namespace_lock);
3436 * Get the list of spares, if specified.
3438 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3439 &spares, &nspares) == 0) {
3440 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3442 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3443 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3444 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3445 spa_load_spares(spa);
3446 spa_config_exit(spa, SCL_ALL, FTAG);
3447 spa->spa_spares.sav_sync = B_TRUE;
3451 * Get the list of level 2 cache devices, if specified.
3453 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3454 &l2cache, &nl2cache) == 0) {
3455 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3456 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3457 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3458 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3459 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3460 spa_load_l2cache(spa);
3461 spa_config_exit(spa, SCL_ALL, FTAG);
3462 spa->spa_l2cache.sav_sync = B_TRUE;
3465 spa->spa_is_initializing = B_TRUE;
3466 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3467 spa->spa_meta_objset = dp->dp_meta_objset;
3468 spa->spa_is_initializing = B_FALSE;
3471 * Create DDTs (dedup tables).
3475 spa_update_dspace(spa);
3477 tx = dmu_tx_create_assigned(dp, txg);
3480 * Create the pool config object.
3482 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3483 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3484 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3486 if (zap_add(spa->spa_meta_objset,
3487 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3488 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3489 cmn_err(CE_PANIC, "failed to add pool config");
3492 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3493 spa_feature_create_zap_objects(spa, tx);
3495 if (zap_add(spa->spa_meta_objset,
3496 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3497 sizeof (uint64_t), 1, &version, tx) != 0) {
3498 cmn_err(CE_PANIC, "failed to add pool version");
3501 /* Newly created pools with the right version are always deflated. */
3502 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3503 spa->spa_deflate = TRUE;
3504 if (zap_add(spa->spa_meta_objset,
3505 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3506 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3507 cmn_err(CE_PANIC, "failed to add deflate");
3512 * Create the deferred-free bpobj. Turn off compression
3513 * because sync-to-convergence takes longer if the blocksize
3516 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3517 dmu_object_set_compress(spa->spa_meta_objset, obj,
3518 ZIO_COMPRESS_OFF, tx);
3519 if (zap_add(spa->spa_meta_objset,
3520 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3521 sizeof (uint64_t), 1, &obj, tx) != 0) {
3522 cmn_err(CE_PANIC, "failed to add bpobj");
3524 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3525 spa->spa_meta_objset, obj));
3528 * Create the pool's history object.
3530 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3531 spa_history_create_obj(spa, tx);
3534 * Set pool properties.
3536 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3537 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3538 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3539 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3541 if (props != NULL) {
3542 spa_configfile_set(spa, props, B_FALSE);
3543 spa_sync_props(props, tx);
3548 spa->spa_sync_on = B_TRUE;
3549 txg_sync_start(spa->spa_dsl_pool);
3552 * We explicitly wait for the first transaction to complete so that our
3553 * bean counters are appropriately updated.
3555 txg_wait_synced(spa->spa_dsl_pool, txg);
3557 spa_config_sync(spa, B_FALSE, B_TRUE);
3559 spa_history_log_version(spa, "create");
3561 spa->spa_minref = refcount_count(&spa->spa_refcount);
3563 mutex_exit(&spa_namespace_lock);
3571 * Get the root pool information from the root disk, then import the root pool
3572 * during the system boot up time.
3574 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3577 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3580 nvlist_t *nvtop, *nvroot;
3583 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3587 * Add this top-level vdev to the child array.
3589 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3591 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3593 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3596 * Put this pool's top-level vdevs into a root vdev.
3598 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3599 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3600 VDEV_TYPE_ROOT) == 0);
3601 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3602 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3603 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3607 * Replace the existing vdev_tree with the new root vdev in
3608 * this pool's configuration (remove the old, add the new).
3610 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3611 nvlist_free(nvroot);
3616 * Walk the vdev tree and see if we can find a device with "better"
3617 * configuration. A configuration is "better" if the label on that
3618 * device has a more recent txg.
3621 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3623 for (int c = 0; c < vd->vdev_children; c++)
3624 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3626 if (vd->vdev_ops->vdev_op_leaf) {
3630 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3634 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3638 * Do we have a better boot device?
3640 if (label_txg > *txg) {
3649 * Import a root pool.
3651 * For x86. devpath_list will consist of devid and/or physpath name of
3652 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3653 * The GRUB "findroot" command will return the vdev we should boot.
3655 * For Sparc, devpath_list consists the physpath name of the booting device
3656 * no matter the rootpool is a single device pool or a mirrored pool.
3658 * "/pci@1f,0/ide@d/disk@0,0:a"
3661 spa_import_rootpool(char *devpath, char *devid)
3664 vdev_t *rvd, *bvd, *avd = NULL;
3665 nvlist_t *config, *nvtop;
3671 * Read the label from the boot device and generate a configuration.
3673 config = spa_generate_rootconf(devpath, devid, &guid);
3674 #if defined(_OBP) && defined(_KERNEL)
3675 if (config == NULL) {
3676 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3678 get_iscsi_bootpath_phy(devpath);
3679 config = spa_generate_rootconf(devpath, devid, &guid);
3683 if (config == NULL) {
3684 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3686 return (SET_ERROR(EIO));
3689 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3691 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3693 mutex_enter(&spa_namespace_lock);
3694 if ((spa = spa_lookup(pname)) != NULL) {
3696 * Remove the existing root pool from the namespace so that we
3697 * can replace it with the correct config we just read in.
3702 spa = spa_add(pname, config, NULL);
3703 spa->spa_is_root = B_TRUE;
3704 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3707 * Build up a vdev tree based on the boot device's label config.
3709 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3711 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3712 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3713 VDEV_ALLOC_ROOTPOOL);
3714 spa_config_exit(spa, SCL_ALL, FTAG);
3716 mutex_exit(&spa_namespace_lock);
3717 nvlist_free(config);
3718 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3724 * Get the boot vdev.
3726 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3727 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3728 (u_longlong_t)guid);
3729 error = SET_ERROR(ENOENT);
3734 * Determine if there is a better boot device.
3737 spa_alt_rootvdev(rvd, &avd, &txg);
3739 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3740 "try booting from '%s'", avd->vdev_path);
3741 error = SET_ERROR(EINVAL);
3746 * If the boot device is part of a spare vdev then ensure that
3747 * we're booting off the active spare.
3749 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3750 !bvd->vdev_isspare) {
3751 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3752 "try booting from '%s'",
3754 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3755 error = SET_ERROR(EINVAL);
3761 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3763 spa_config_exit(spa, SCL_ALL, FTAG);
3764 mutex_exit(&spa_namespace_lock);
3766 nvlist_free(config);
3772 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3776 spa_generate_rootconf(const char *name)
3778 nvlist_t **configs, **tops;
3780 nvlist_t *best_cfg, *nvtop, *nvroot;
3789 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3792 ASSERT3U(count, !=, 0);
3794 for (i = 0; i < count; i++) {
3797 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
3799 if (txg > best_txg) {
3801 best_cfg = configs[i];
3806 * Multi-vdev root pool configuration discovery is not supported yet.
3809 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3811 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
3814 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
3815 for (i = 0; i < nchildren; i++) {
3818 if (configs[i] == NULL)
3820 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
3822 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
3824 for (i = 0; holes != NULL && i < nholes; i++) {
3827 if (tops[holes[i]] != NULL)
3829 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
3830 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
3831 VDEV_TYPE_HOLE) == 0);
3832 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
3834 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
3837 for (i = 0; i < nchildren; i++) {
3838 if (tops[i] != NULL)
3840 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
3841 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
3842 VDEV_TYPE_MISSING) == 0);
3843 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
3845 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
3850 * Create pool config based on the best vdev config.
3852 nvlist_dup(best_cfg, &config, KM_SLEEP);
3855 * Put this pool's top-level vdevs into a root vdev.
3857 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3859 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3860 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3861 VDEV_TYPE_ROOT) == 0);
3862 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3863 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3864 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3865 tops, nchildren) == 0);
3868 * Replace the existing vdev_tree with the new root vdev in
3869 * this pool's configuration (remove the old, add the new).
3871 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3874 * Drop vdev config elements that should not be present at pool level.
3876 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
3877 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
3879 for (i = 0; i < count; i++)
3880 nvlist_free(configs[i]);
3881 kmem_free(configs, count * sizeof(void *));
3882 for (i = 0; i < nchildren; i++)
3883 nvlist_free(tops[i]);
3884 kmem_free(tops, nchildren * sizeof(void *));
3885 nvlist_free(nvroot);
3890 spa_import_rootpool(const char *name)
3893 vdev_t *rvd, *bvd, *avd = NULL;
3894 nvlist_t *config, *nvtop;
3900 * Read the label from the boot device and generate a configuration.
3902 config = spa_generate_rootconf(name);
3904 mutex_enter(&spa_namespace_lock);
3905 if (config != NULL) {
3906 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3907 &pname) == 0 && strcmp(name, pname) == 0);
3908 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
3911 if ((spa = spa_lookup(pname)) != NULL) {
3913 * Remove the existing root pool from the namespace so
3914 * that we can replace it with the correct config
3919 spa = spa_add(pname, config, NULL);
3922 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
3923 * via spa_version().
3925 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3926 &spa->spa_ubsync.ub_version) != 0)
3927 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
3928 } else if ((spa = spa_lookup(name)) == NULL) {
3929 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
3933 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
3935 spa->spa_is_root = B_TRUE;
3936 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3939 * Build up a vdev tree based on the boot device's label config.
3941 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3943 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3944 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3945 VDEV_ALLOC_ROOTPOOL);
3946 spa_config_exit(spa, SCL_ALL, FTAG);
3948 mutex_exit(&spa_namespace_lock);
3949 nvlist_free(config);
3950 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3955 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3957 spa_config_exit(spa, SCL_ALL, FTAG);
3958 mutex_exit(&spa_namespace_lock);
3960 nvlist_free(config);
3968 * Import a non-root pool into the system.
3971 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3974 char *altroot = NULL;
3975 spa_load_state_t state = SPA_LOAD_IMPORT;
3976 zpool_rewind_policy_t policy;
3977 uint64_t mode = spa_mode_global;
3978 uint64_t readonly = B_FALSE;
3981 nvlist_t **spares, **l2cache;
3982 uint_t nspares, nl2cache;
3985 * If a pool with this name exists, return failure.
3987 mutex_enter(&spa_namespace_lock);
3988 if (spa_lookup(pool) != NULL) {
3989 mutex_exit(&spa_namespace_lock);
3990 return (SET_ERROR(EEXIST));
3994 * Create and initialize the spa structure.
3996 (void) nvlist_lookup_string(props,
3997 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3998 (void) nvlist_lookup_uint64(props,
3999 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4002 spa = spa_add(pool, config, altroot);
4003 spa->spa_import_flags = flags;
4006 * Verbatim import - Take a pool and insert it into the namespace
4007 * as if it had been loaded at boot.
4009 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4011 spa_configfile_set(spa, props, B_FALSE);
4013 spa_config_sync(spa, B_FALSE, B_TRUE);
4015 mutex_exit(&spa_namespace_lock);
4016 spa_history_log_version(spa, "import");
4021 spa_activate(spa, mode);
4024 * Don't start async tasks until we know everything is healthy.
4026 spa_async_suspend(spa);
4028 zpool_get_rewind_policy(config, &policy);
4029 if (policy.zrp_request & ZPOOL_DO_REWIND)
4030 state = SPA_LOAD_RECOVER;
4033 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4034 * because the user-supplied config is actually the one to trust when
4037 if (state != SPA_LOAD_RECOVER)
4038 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4040 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4041 policy.zrp_request);
4044 * Propagate anything learned while loading the pool and pass it
4045 * back to caller (i.e. rewind info, missing devices, etc).
4047 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4048 spa->spa_load_info) == 0);
4050 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4052 * Toss any existing sparelist, as it doesn't have any validity
4053 * anymore, and conflicts with spa_has_spare().
4055 if (spa->spa_spares.sav_config) {
4056 nvlist_free(spa->spa_spares.sav_config);
4057 spa->spa_spares.sav_config = NULL;
4058 spa_load_spares(spa);
4060 if (spa->spa_l2cache.sav_config) {
4061 nvlist_free(spa->spa_l2cache.sav_config);
4062 spa->spa_l2cache.sav_config = NULL;
4063 spa_load_l2cache(spa);
4066 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4069 error = spa_validate_aux(spa, nvroot, -1ULL,
4072 error = spa_validate_aux(spa, nvroot, -1ULL,
4073 VDEV_ALLOC_L2CACHE);
4074 spa_config_exit(spa, SCL_ALL, FTAG);
4077 spa_configfile_set(spa, props, B_FALSE);
4079 if (error != 0 || (props && spa_writeable(spa) &&
4080 (error = spa_prop_set(spa, props)))) {
4082 spa_deactivate(spa);
4084 mutex_exit(&spa_namespace_lock);
4088 spa_async_resume(spa);
4091 * Override any spares and level 2 cache devices as specified by
4092 * the user, as these may have correct device names/devids, etc.
4094 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4095 &spares, &nspares) == 0) {
4096 if (spa->spa_spares.sav_config)
4097 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4098 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4100 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4101 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4102 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4103 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4104 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4105 spa_load_spares(spa);
4106 spa_config_exit(spa, SCL_ALL, FTAG);
4107 spa->spa_spares.sav_sync = B_TRUE;
4109 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4110 &l2cache, &nl2cache) == 0) {
4111 if (spa->spa_l2cache.sav_config)
4112 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4113 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4115 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4116 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4117 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4118 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4119 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4120 spa_load_l2cache(spa);
4121 spa_config_exit(spa, SCL_ALL, FTAG);
4122 spa->spa_l2cache.sav_sync = B_TRUE;
4126 * Check for any removed devices.
4128 if (spa->spa_autoreplace) {
4129 spa_aux_check_removed(&spa->spa_spares);
4130 spa_aux_check_removed(&spa->spa_l2cache);
4133 if (spa_writeable(spa)) {
4135 * Update the config cache to include the newly-imported pool.
4137 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4141 * It's possible that the pool was expanded while it was exported.
4142 * We kick off an async task to handle this for us.
4144 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4146 mutex_exit(&spa_namespace_lock);
4147 spa_history_log_version(spa, "import");
4151 zvol_create_minors(pool);
4158 spa_tryimport(nvlist_t *tryconfig)
4160 nvlist_t *config = NULL;
4166 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4169 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4173 * Create and initialize the spa structure.
4175 mutex_enter(&spa_namespace_lock);
4176 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4177 spa_activate(spa, FREAD);
4180 * Pass off the heavy lifting to spa_load().
4181 * Pass TRUE for mosconfig because the user-supplied config
4182 * is actually the one to trust when doing an import.
4184 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4187 * If 'tryconfig' was at least parsable, return the current config.
4189 if (spa->spa_root_vdev != NULL) {
4190 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4191 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4193 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4195 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4196 spa->spa_uberblock.ub_timestamp) == 0);
4197 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4198 spa->spa_load_info) == 0);
4201 * If the bootfs property exists on this pool then we
4202 * copy it out so that external consumers can tell which
4203 * pools are bootable.
4205 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4206 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4209 * We have to play games with the name since the
4210 * pool was opened as TRYIMPORT_NAME.
4212 if (dsl_dsobj_to_dsname(spa_name(spa),
4213 spa->spa_bootfs, tmpname) == 0) {
4215 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4217 cp = strchr(tmpname, '/');
4219 (void) strlcpy(dsname, tmpname,
4222 (void) snprintf(dsname, MAXPATHLEN,
4223 "%s/%s", poolname, ++cp);
4225 VERIFY(nvlist_add_string(config,
4226 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4227 kmem_free(dsname, MAXPATHLEN);
4229 kmem_free(tmpname, MAXPATHLEN);
4233 * Add the list of hot spares and level 2 cache devices.
4235 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4236 spa_add_spares(spa, config);
4237 spa_add_l2cache(spa, config);
4238 spa_config_exit(spa, SCL_CONFIG, FTAG);
4242 spa_deactivate(spa);
4244 mutex_exit(&spa_namespace_lock);
4250 * Pool export/destroy
4252 * The act of destroying or exporting a pool is very simple. We make sure there
4253 * is no more pending I/O and any references to the pool are gone. Then, we
4254 * update the pool state and sync all the labels to disk, removing the
4255 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4256 * we don't sync the labels or remove the configuration cache.
4259 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4260 boolean_t force, boolean_t hardforce)
4267 if (!(spa_mode_global & FWRITE))
4268 return (SET_ERROR(EROFS));
4270 mutex_enter(&spa_namespace_lock);
4271 if ((spa = spa_lookup(pool)) == NULL) {
4272 mutex_exit(&spa_namespace_lock);
4273 return (SET_ERROR(ENOENT));
4277 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4278 * reacquire the namespace lock, and see if we can export.
4280 spa_open_ref(spa, FTAG);
4281 mutex_exit(&spa_namespace_lock);
4282 spa_async_suspend(spa);
4283 mutex_enter(&spa_namespace_lock);
4284 spa_close(spa, FTAG);
4287 * The pool will be in core if it's openable,
4288 * in which case we can modify its state.
4290 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4292 * Objsets may be open only because they're dirty, so we
4293 * have to force it to sync before checking spa_refcnt.
4295 txg_wait_synced(spa->spa_dsl_pool, 0);
4298 * A pool cannot be exported or destroyed if there are active
4299 * references. If we are resetting a pool, allow references by
4300 * fault injection handlers.
4302 if (!spa_refcount_zero(spa) ||
4303 (spa->spa_inject_ref != 0 &&
4304 new_state != POOL_STATE_UNINITIALIZED)) {
4305 spa_async_resume(spa);
4306 mutex_exit(&spa_namespace_lock);
4307 return (SET_ERROR(EBUSY));
4311 * A pool cannot be exported if it has an active shared spare.
4312 * This is to prevent other pools stealing the active spare
4313 * from an exported pool. At user's own will, such pool can
4314 * be forcedly exported.
4316 if (!force && new_state == POOL_STATE_EXPORTED &&
4317 spa_has_active_shared_spare(spa)) {
4318 spa_async_resume(spa);
4319 mutex_exit(&spa_namespace_lock);
4320 return (SET_ERROR(EXDEV));
4324 * We want this to be reflected on every label,
4325 * so mark them all dirty. spa_unload() will do the
4326 * final sync that pushes these changes out.
4328 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4329 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4330 spa->spa_state = new_state;
4331 spa->spa_final_txg = spa_last_synced_txg(spa) +
4333 vdev_config_dirty(spa->spa_root_vdev);
4334 spa_config_exit(spa, SCL_ALL, FTAG);
4338 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4340 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4342 spa_deactivate(spa);
4345 if (oldconfig && spa->spa_config)
4346 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4348 if (new_state != POOL_STATE_UNINITIALIZED) {
4350 spa_config_sync(spa, B_TRUE, B_TRUE);
4353 mutex_exit(&spa_namespace_lock);
4359 * Destroy a storage pool.
4362 spa_destroy(char *pool)
4364 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4369 * Export a storage pool.
4372 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4373 boolean_t hardforce)
4375 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4380 * Similar to spa_export(), this unloads the spa_t without actually removing it
4381 * from the namespace in any way.
4384 spa_reset(char *pool)
4386 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4391 * ==========================================================================
4392 * Device manipulation
4393 * ==========================================================================
4397 * Add a device to a storage pool.
4400 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4404 vdev_t *rvd = spa->spa_root_vdev;
4406 nvlist_t **spares, **l2cache;
4407 uint_t nspares, nl2cache;
4409 ASSERT(spa_writeable(spa));
4411 txg = spa_vdev_enter(spa);
4413 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4414 VDEV_ALLOC_ADD)) != 0)
4415 return (spa_vdev_exit(spa, NULL, txg, error));
4417 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4419 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4423 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4427 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4428 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4430 if (vd->vdev_children != 0 &&
4431 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4432 return (spa_vdev_exit(spa, vd, txg, error));
4435 * We must validate the spares and l2cache devices after checking the
4436 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4438 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4439 return (spa_vdev_exit(spa, vd, txg, error));
4442 * Transfer each new top-level vdev from vd to rvd.
4444 for (int c = 0; c < vd->vdev_children; c++) {
4447 * Set the vdev id to the first hole, if one exists.
4449 for (id = 0; id < rvd->vdev_children; id++) {
4450 if (rvd->vdev_child[id]->vdev_ishole) {
4451 vdev_free(rvd->vdev_child[id]);
4455 tvd = vd->vdev_child[c];
4456 vdev_remove_child(vd, tvd);
4458 vdev_add_child(rvd, tvd);
4459 vdev_config_dirty(tvd);
4463 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4464 ZPOOL_CONFIG_SPARES);
4465 spa_load_spares(spa);
4466 spa->spa_spares.sav_sync = B_TRUE;
4469 if (nl2cache != 0) {
4470 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4471 ZPOOL_CONFIG_L2CACHE);
4472 spa_load_l2cache(spa);
4473 spa->spa_l2cache.sav_sync = B_TRUE;
4477 * We have to be careful when adding new vdevs to an existing pool.
4478 * If other threads start allocating from these vdevs before we
4479 * sync the config cache, and we lose power, then upon reboot we may
4480 * fail to open the pool because there are DVAs that the config cache
4481 * can't translate. Therefore, we first add the vdevs without
4482 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4483 * and then let spa_config_update() initialize the new metaslabs.
4485 * spa_load() checks for added-but-not-initialized vdevs, so that
4486 * if we lose power at any point in this sequence, the remaining
4487 * steps will be completed the next time we load the pool.
4489 (void) spa_vdev_exit(spa, vd, txg, 0);
4491 mutex_enter(&spa_namespace_lock);
4492 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4493 mutex_exit(&spa_namespace_lock);
4499 * Attach a device to a mirror. The arguments are the path to any device
4500 * in the mirror, and the nvroot for the new device. If the path specifies
4501 * a device that is not mirrored, we automatically insert the mirror vdev.
4503 * If 'replacing' is specified, the new device is intended to replace the
4504 * existing device; in this case the two devices are made into their own
4505 * mirror using the 'replacing' vdev, which is functionally identical to
4506 * the mirror vdev (it actually reuses all the same ops) but has a few
4507 * extra rules: you can't attach to it after it's been created, and upon
4508 * completion of resilvering, the first disk (the one being replaced)
4509 * is automatically detached.
4512 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4514 uint64_t txg, dtl_max_txg;
4515 vdev_t *rvd = spa->spa_root_vdev;
4516 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4518 char *oldvdpath, *newvdpath;
4522 ASSERT(spa_writeable(spa));
4524 txg = spa_vdev_enter(spa);
4526 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4529 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4531 if (!oldvd->vdev_ops->vdev_op_leaf)
4532 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4534 pvd = oldvd->vdev_parent;
4536 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4537 VDEV_ALLOC_ATTACH)) != 0)
4538 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4540 if (newrootvd->vdev_children != 1)
4541 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4543 newvd = newrootvd->vdev_child[0];
4545 if (!newvd->vdev_ops->vdev_op_leaf)
4546 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4548 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4549 return (spa_vdev_exit(spa, newrootvd, txg, error));
4552 * Spares can't replace logs
4554 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4555 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4559 * For attach, the only allowable parent is a mirror or the root
4562 if (pvd->vdev_ops != &vdev_mirror_ops &&
4563 pvd->vdev_ops != &vdev_root_ops)
4564 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4566 pvops = &vdev_mirror_ops;
4569 * Active hot spares can only be replaced by inactive hot
4572 if (pvd->vdev_ops == &vdev_spare_ops &&
4573 oldvd->vdev_isspare &&
4574 !spa_has_spare(spa, newvd->vdev_guid))
4575 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4578 * If the source is a hot spare, and the parent isn't already a
4579 * spare, then we want to create a new hot spare. Otherwise, we
4580 * want to create a replacing vdev. The user is not allowed to
4581 * attach to a spared vdev child unless the 'isspare' state is
4582 * the same (spare replaces spare, non-spare replaces
4585 if (pvd->vdev_ops == &vdev_replacing_ops &&
4586 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4587 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4588 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4589 newvd->vdev_isspare != oldvd->vdev_isspare) {
4590 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4593 if (newvd->vdev_isspare)
4594 pvops = &vdev_spare_ops;
4596 pvops = &vdev_replacing_ops;
4600 * Make sure the new device is big enough.
4602 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4603 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4606 * The new device cannot have a higher alignment requirement
4607 * than the top-level vdev.
4609 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4610 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4613 * If this is an in-place replacement, update oldvd's path and devid
4614 * to make it distinguishable from newvd, and unopenable from now on.
4616 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4617 spa_strfree(oldvd->vdev_path);
4618 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4620 (void) sprintf(oldvd->vdev_path, "%s/%s",
4621 newvd->vdev_path, "old");
4622 if (oldvd->vdev_devid != NULL) {
4623 spa_strfree(oldvd->vdev_devid);
4624 oldvd->vdev_devid = NULL;
4628 /* mark the device being resilvered */
4629 newvd->vdev_resilvering = B_TRUE;
4632 * If the parent is not a mirror, or if we're replacing, insert the new
4633 * mirror/replacing/spare vdev above oldvd.
4635 if (pvd->vdev_ops != pvops)
4636 pvd = vdev_add_parent(oldvd, pvops);
4638 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4639 ASSERT(pvd->vdev_ops == pvops);
4640 ASSERT(oldvd->vdev_parent == pvd);
4643 * Extract the new device from its root and add it to pvd.
4645 vdev_remove_child(newrootvd, newvd);
4646 newvd->vdev_id = pvd->vdev_children;
4647 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4648 vdev_add_child(pvd, newvd);
4650 tvd = newvd->vdev_top;
4651 ASSERT(pvd->vdev_top == tvd);
4652 ASSERT(tvd->vdev_parent == rvd);
4654 vdev_config_dirty(tvd);
4657 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4658 * for any dmu_sync-ed blocks. It will propagate upward when
4659 * spa_vdev_exit() calls vdev_dtl_reassess().
4661 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4663 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4664 dtl_max_txg - TXG_INITIAL);
4666 if (newvd->vdev_isspare) {
4667 spa_spare_activate(newvd);
4668 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4671 oldvdpath = spa_strdup(oldvd->vdev_path);
4672 newvdpath = spa_strdup(newvd->vdev_path);
4673 newvd_isspare = newvd->vdev_isspare;
4676 * Mark newvd's DTL dirty in this txg.
4678 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4681 * Restart the resilver
4683 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4688 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4690 spa_history_log_internal(spa, "vdev attach", NULL,
4691 "%s vdev=%s %s vdev=%s",
4692 replacing && newvd_isspare ? "spare in" :
4693 replacing ? "replace" : "attach", newvdpath,
4694 replacing ? "for" : "to", oldvdpath);
4696 spa_strfree(oldvdpath);
4697 spa_strfree(newvdpath);
4699 if (spa->spa_bootfs)
4700 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4706 * Detach a device from a mirror or replacing vdev.
4708 * If 'replace_done' is specified, only detach if the parent
4709 * is a replacing vdev.
4712 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4716 vdev_t *rvd = spa->spa_root_vdev;
4717 vdev_t *vd, *pvd, *cvd, *tvd;
4718 boolean_t unspare = B_FALSE;
4719 uint64_t unspare_guid = 0;
4722 ASSERT(spa_writeable(spa));
4724 txg = spa_vdev_enter(spa);
4726 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4729 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4731 if (!vd->vdev_ops->vdev_op_leaf)
4732 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4734 pvd = vd->vdev_parent;
4737 * If the parent/child relationship is not as expected, don't do it.
4738 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4739 * vdev that's replacing B with C. The user's intent in replacing
4740 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4741 * the replace by detaching C, the expected behavior is to end up
4742 * M(A,B). But suppose that right after deciding to detach C,
4743 * the replacement of B completes. We would have M(A,C), and then
4744 * ask to detach C, which would leave us with just A -- not what
4745 * the user wanted. To prevent this, we make sure that the
4746 * parent/child relationship hasn't changed -- in this example,
4747 * that C's parent is still the replacing vdev R.
4749 if (pvd->vdev_guid != pguid && pguid != 0)
4750 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4753 * Only 'replacing' or 'spare' vdevs can be replaced.
4755 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4756 pvd->vdev_ops != &vdev_spare_ops)
4757 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4759 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4760 spa_version(spa) >= SPA_VERSION_SPARES);
4763 * Only mirror, replacing, and spare vdevs support detach.
4765 if (pvd->vdev_ops != &vdev_replacing_ops &&
4766 pvd->vdev_ops != &vdev_mirror_ops &&
4767 pvd->vdev_ops != &vdev_spare_ops)
4768 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4771 * If this device has the only valid copy of some data,
4772 * we cannot safely detach it.
4774 if (vdev_dtl_required(vd))
4775 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4777 ASSERT(pvd->vdev_children >= 2);
4780 * If we are detaching the second disk from a replacing vdev, then
4781 * check to see if we changed the original vdev's path to have "/old"
4782 * at the end in spa_vdev_attach(). If so, undo that change now.
4784 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4785 vd->vdev_path != NULL) {
4786 size_t len = strlen(vd->vdev_path);
4788 for (int c = 0; c < pvd->vdev_children; c++) {
4789 cvd = pvd->vdev_child[c];
4791 if (cvd == vd || cvd->vdev_path == NULL)
4794 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4795 strcmp(cvd->vdev_path + len, "/old") == 0) {
4796 spa_strfree(cvd->vdev_path);
4797 cvd->vdev_path = spa_strdup(vd->vdev_path);
4804 * If we are detaching the original disk from a spare, then it implies
4805 * that the spare should become a real disk, and be removed from the
4806 * active spare list for the pool.
4808 if (pvd->vdev_ops == &vdev_spare_ops &&
4810 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4814 * Erase the disk labels so the disk can be used for other things.
4815 * This must be done after all other error cases are handled,
4816 * but before we disembowel vd (so we can still do I/O to it).
4817 * But if we can't do it, don't treat the error as fatal --
4818 * it may be that the unwritability of the disk is the reason
4819 * it's being detached!
4821 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4824 * Remove vd from its parent and compact the parent's children.
4826 vdev_remove_child(pvd, vd);
4827 vdev_compact_children(pvd);
4830 * Remember one of the remaining children so we can get tvd below.
4832 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4835 * If we need to remove the remaining child from the list of hot spares,
4836 * do it now, marking the vdev as no longer a spare in the process.
4837 * We must do this before vdev_remove_parent(), because that can
4838 * change the GUID if it creates a new toplevel GUID. For a similar
4839 * reason, we must remove the spare now, in the same txg as the detach;
4840 * otherwise someone could attach a new sibling, change the GUID, and
4841 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4844 ASSERT(cvd->vdev_isspare);
4845 spa_spare_remove(cvd);
4846 unspare_guid = cvd->vdev_guid;
4847 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4848 cvd->vdev_unspare = B_TRUE;
4852 * If the parent mirror/replacing vdev only has one child,
4853 * the parent is no longer needed. Remove it from the tree.
4855 if (pvd->vdev_children == 1) {
4856 if (pvd->vdev_ops == &vdev_spare_ops)
4857 cvd->vdev_unspare = B_FALSE;
4858 vdev_remove_parent(cvd);
4859 cvd->vdev_resilvering = B_FALSE;
4864 * We don't set tvd until now because the parent we just removed
4865 * may have been the previous top-level vdev.
4867 tvd = cvd->vdev_top;
4868 ASSERT(tvd->vdev_parent == rvd);
4871 * Reevaluate the parent vdev state.
4873 vdev_propagate_state(cvd);
4876 * If the 'autoexpand' property is set on the pool then automatically
4877 * try to expand the size of the pool. For example if the device we
4878 * just detached was smaller than the others, it may be possible to
4879 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4880 * first so that we can obtain the updated sizes of the leaf vdevs.
4882 if (spa->spa_autoexpand) {
4884 vdev_expand(tvd, txg);
4887 vdev_config_dirty(tvd);
4890 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4891 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4892 * But first make sure we're not on any *other* txg's DTL list, to
4893 * prevent vd from being accessed after it's freed.
4895 vdpath = spa_strdup(vd->vdev_path);
4896 for (int t = 0; t < TXG_SIZE; t++)
4897 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4898 vd->vdev_detached = B_TRUE;
4899 vdev_dirty(tvd, VDD_DTL, vd, txg);
4901 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4903 /* hang on to the spa before we release the lock */
4904 spa_open_ref(spa, FTAG);
4906 error = spa_vdev_exit(spa, vd, txg, 0);
4908 spa_history_log_internal(spa, "detach", NULL,
4910 spa_strfree(vdpath);
4913 * If this was the removal of the original device in a hot spare vdev,
4914 * then we want to go through and remove the device from the hot spare
4915 * list of every other pool.
4918 spa_t *altspa = NULL;
4920 mutex_enter(&spa_namespace_lock);
4921 while ((altspa = spa_next(altspa)) != NULL) {
4922 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4926 spa_open_ref(altspa, FTAG);
4927 mutex_exit(&spa_namespace_lock);
4928 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4929 mutex_enter(&spa_namespace_lock);
4930 spa_close(altspa, FTAG);
4932 mutex_exit(&spa_namespace_lock);
4934 /* search the rest of the vdevs for spares to remove */
4935 spa_vdev_resilver_done(spa);
4938 /* all done with the spa; OK to release */
4939 mutex_enter(&spa_namespace_lock);
4940 spa_close(spa, FTAG);
4941 mutex_exit(&spa_namespace_lock);
4947 * Split a set of devices from their mirrors, and create a new pool from them.
4950 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4951 nvlist_t *props, boolean_t exp)
4954 uint64_t txg, *glist;
4956 uint_t c, children, lastlog;
4957 nvlist_t **child, *nvl, *tmp;
4959 char *altroot = NULL;
4960 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4961 boolean_t activate_slog;
4963 ASSERT(spa_writeable(spa));
4965 txg = spa_vdev_enter(spa);
4967 /* clear the log and flush everything up to now */
4968 activate_slog = spa_passivate_log(spa);
4969 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4970 error = spa_offline_log(spa);
4971 txg = spa_vdev_config_enter(spa);
4974 spa_activate_log(spa);
4977 return (spa_vdev_exit(spa, NULL, txg, error));
4979 /* check new spa name before going any further */
4980 if (spa_lookup(newname) != NULL)
4981 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4984 * scan through all the children to ensure they're all mirrors
4986 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4987 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4989 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4991 /* first, check to ensure we've got the right child count */
4992 rvd = spa->spa_root_vdev;
4994 for (c = 0; c < rvd->vdev_children; c++) {
4995 vdev_t *vd = rvd->vdev_child[c];
4997 /* don't count the holes & logs as children */
4998 if (vd->vdev_islog || vd->vdev_ishole) {
5006 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5007 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5009 /* next, ensure no spare or cache devices are part of the split */
5010 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5011 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5012 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5014 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5015 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5017 /* then, loop over each vdev and validate it */
5018 for (c = 0; c < children; c++) {
5019 uint64_t is_hole = 0;
5021 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5025 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5026 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5029 error = SET_ERROR(EINVAL);
5034 /* which disk is going to be split? */
5035 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5037 error = SET_ERROR(EINVAL);
5041 /* look it up in the spa */
5042 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5043 if (vml[c] == NULL) {
5044 error = SET_ERROR(ENODEV);
5048 /* make sure there's nothing stopping the split */
5049 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5050 vml[c]->vdev_islog ||
5051 vml[c]->vdev_ishole ||
5052 vml[c]->vdev_isspare ||
5053 vml[c]->vdev_isl2cache ||
5054 !vdev_writeable(vml[c]) ||
5055 vml[c]->vdev_children != 0 ||
5056 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5057 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5058 error = SET_ERROR(EINVAL);
5062 if (vdev_dtl_required(vml[c])) {
5063 error = SET_ERROR(EBUSY);
5067 /* we need certain info from the top level */
5068 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5069 vml[c]->vdev_top->vdev_ms_array) == 0);
5070 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5071 vml[c]->vdev_top->vdev_ms_shift) == 0);
5072 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5073 vml[c]->vdev_top->vdev_asize) == 0);
5074 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5075 vml[c]->vdev_top->vdev_ashift) == 0);
5079 kmem_free(vml, children * sizeof (vdev_t *));
5080 kmem_free(glist, children * sizeof (uint64_t));
5081 return (spa_vdev_exit(spa, NULL, txg, error));
5084 /* stop writers from using the disks */
5085 for (c = 0; c < children; c++) {
5087 vml[c]->vdev_offline = B_TRUE;
5089 vdev_reopen(spa->spa_root_vdev);
5092 * Temporarily record the splitting vdevs in the spa config. This
5093 * will disappear once the config is regenerated.
5095 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5096 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5097 glist, children) == 0);
5098 kmem_free(glist, children * sizeof (uint64_t));
5100 mutex_enter(&spa->spa_props_lock);
5101 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5103 mutex_exit(&spa->spa_props_lock);
5104 spa->spa_config_splitting = nvl;
5105 vdev_config_dirty(spa->spa_root_vdev);
5107 /* configure and create the new pool */
5108 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5109 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5110 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5111 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5112 spa_version(spa)) == 0);
5113 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5114 spa->spa_config_txg) == 0);
5115 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5116 spa_generate_guid(NULL)) == 0);
5117 (void) nvlist_lookup_string(props,
5118 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5120 /* add the new pool to the namespace */
5121 newspa = spa_add(newname, config, altroot);
5122 newspa->spa_config_txg = spa->spa_config_txg;
5123 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5125 /* release the spa config lock, retaining the namespace lock */
5126 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5128 if (zio_injection_enabled)
5129 zio_handle_panic_injection(spa, FTAG, 1);
5131 spa_activate(newspa, spa_mode_global);
5132 spa_async_suspend(newspa);
5135 /* mark that we are creating new spa by splitting */
5136 newspa->spa_splitting_newspa = B_TRUE;
5138 /* create the new pool from the disks of the original pool */
5139 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5141 newspa->spa_splitting_newspa = B_FALSE;
5146 /* if that worked, generate a real config for the new pool */
5147 if (newspa->spa_root_vdev != NULL) {
5148 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5149 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5150 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5151 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5152 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5157 if (props != NULL) {
5158 spa_configfile_set(newspa, props, B_FALSE);
5159 error = spa_prop_set(newspa, props);
5164 /* flush everything */
5165 txg = spa_vdev_config_enter(newspa);
5166 vdev_config_dirty(newspa->spa_root_vdev);
5167 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5169 if (zio_injection_enabled)
5170 zio_handle_panic_injection(spa, FTAG, 2);
5172 spa_async_resume(newspa);
5174 /* finally, update the original pool's config */
5175 txg = spa_vdev_config_enter(spa);
5176 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5177 error = dmu_tx_assign(tx, TXG_WAIT);
5180 for (c = 0; c < children; c++) {
5181 if (vml[c] != NULL) {
5184 spa_history_log_internal(spa, "detach", tx,
5185 "vdev=%s", vml[c]->vdev_path);
5189 vdev_config_dirty(spa->spa_root_vdev);
5190 spa->spa_config_splitting = NULL;
5194 (void) spa_vdev_exit(spa, NULL, txg, 0);
5196 if (zio_injection_enabled)
5197 zio_handle_panic_injection(spa, FTAG, 3);
5199 /* split is complete; log a history record */
5200 spa_history_log_internal(newspa, "split", NULL,
5201 "from pool %s", spa_name(spa));
5203 kmem_free(vml, children * sizeof (vdev_t *));
5205 /* if we're not going to mount the filesystems in userland, export */
5207 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5214 spa_deactivate(newspa);
5217 txg = spa_vdev_config_enter(spa);
5219 /* re-online all offlined disks */
5220 for (c = 0; c < children; c++) {
5222 vml[c]->vdev_offline = B_FALSE;
5224 vdev_reopen(spa->spa_root_vdev);
5226 nvlist_free(spa->spa_config_splitting);
5227 spa->spa_config_splitting = NULL;
5228 (void) spa_vdev_exit(spa, NULL, txg, error);
5230 kmem_free(vml, children * sizeof (vdev_t *));
5235 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5237 for (int i = 0; i < count; i++) {
5240 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5243 if (guid == target_guid)
5251 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5252 nvlist_t *dev_to_remove)
5254 nvlist_t **newdev = NULL;
5257 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5259 for (int i = 0, j = 0; i < count; i++) {
5260 if (dev[i] == dev_to_remove)
5262 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5265 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5266 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5268 for (int i = 0; i < count - 1; i++)
5269 nvlist_free(newdev[i]);
5272 kmem_free(newdev, (count - 1) * sizeof (void *));
5276 * Evacuate the device.
5279 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5284 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5285 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5286 ASSERT(vd == vd->vdev_top);
5289 * Evacuate the device. We don't hold the config lock as writer
5290 * since we need to do I/O but we do keep the
5291 * spa_namespace_lock held. Once this completes the device
5292 * should no longer have any blocks allocated on it.
5294 if (vd->vdev_islog) {
5295 if (vd->vdev_stat.vs_alloc != 0)
5296 error = spa_offline_log(spa);
5298 error = SET_ERROR(ENOTSUP);
5305 * The evacuation succeeded. Remove any remaining MOS metadata
5306 * associated with this vdev, and wait for these changes to sync.
5308 ASSERT0(vd->vdev_stat.vs_alloc);
5309 txg = spa_vdev_config_enter(spa);
5310 vd->vdev_removing = B_TRUE;
5311 vdev_dirty(vd, 0, NULL, txg);
5312 vdev_config_dirty(vd);
5313 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5319 * Complete the removal by cleaning up the namespace.
5322 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5324 vdev_t *rvd = spa->spa_root_vdev;
5325 uint64_t id = vd->vdev_id;
5326 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5328 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5329 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5330 ASSERT(vd == vd->vdev_top);
5333 * Only remove any devices which are empty.
5335 if (vd->vdev_stat.vs_alloc != 0)
5338 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5340 if (list_link_active(&vd->vdev_state_dirty_node))
5341 vdev_state_clean(vd);
5342 if (list_link_active(&vd->vdev_config_dirty_node))
5343 vdev_config_clean(vd);
5348 vdev_compact_children(rvd);
5350 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5351 vdev_add_child(rvd, vd);
5353 vdev_config_dirty(rvd);
5356 * Reassess the health of our root vdev.
5362 * Remove a device from the pool -
5364 * Removing a device from the vdev namespace requires several steps
5365 * and can take a significant amount of time. As a result we use
5366 * the spa_vdev_config_[enter/exit] functions which allow us to
5367 * grab and release the spa_config_lock while still holding the namespace
5368 * lock. During each step the configuration is synced out.
5370 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5374 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5377 metaslab_group_t *mg;
5378 nvlist_t **spares, **l2cache, *nv;
5380 uint_t nspares, nl2cache;
5382 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5384 ASSERT(spa_writeable(spa));
5387 txg = spa_vdev_enter(spa);
5389 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5391 if (spa->spa_spares.sav_vdevs != NULL &&
5392 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5393 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5394 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5396 * Only remove the hot spare if it's not currently in use
5399 if (vd == NULL || unspare) {
5400 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5401 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5402 spa_load_spares(spa);
5403 spa->spa_spares.sav_sync = B_TRUE;
5405 error = SET_ERROR(EBUSY);
5407 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5408 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5409 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5410 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5412 * Cache devices can always be removed.
5414 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5415 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5416 spa_load_l2cache(spa);
5417 spa->spa_l2cache.sav_sync = B_TRUE;
5418 } else if (vd != NULL && vd->vdev_islog) {
5420 ASSERT(vd == vd->vdev_top);
5423 * XXX - Once we have bp-rewrite this should
5424 * become the common case.
5430 * Stop allocating from this vdev.
5432 metaslab_group_passivate(mg);
5435 * Wait for the youngest allocations and frees to sync,
5436 * and then wait for the deferral of those frees to finish.
5438 spa_vdev_config_exit(spa, NULL,
5439 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5442 * Attempt to evacuate the vdev.
5444 error = spa_vdev_remove_evacuate(spa, vd);
5446 txg = spa_vdev_config_enter(spa);
5449 * If we couldn't evacuate the vdev, unwind.
5452 metaslab_group_activate(mg);
5453 return (spa_vdev_exit(spa, NULL, txg, error));
5457 * Clean up the vdev namespace.
5459 spa_vdev_remove_from_namespace(spa, vd);
5461 } else if (vd != NULL) {
5463 * Normal vdevs cannot be removed (yet).
5465 error = SET_ERROR(ENOTSUP);
5468 * There is no vdev of any kind with the specified guid.
5470 error = SET_ERROR(ENOENT);
5474 return (spa_vdev_exit(spa, NULL, txg, error));
5480 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5481 * currently spared, so we can detach it.
5484 spa_vdev_resilver_done_hunt(vdev_t *vd)
5486 vdev_t *newvd, *oldvd;
5488 for (int c = 0; c < vd->vdev_children; c++) {
5489 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5495 * Check for a completed replacement. We always consider the first
5496 * vdev in the list to be the oldest vdev, and the last one to be
5497 * the newest (see spa_vdev_attach() for how that works). In
5498 * the case where the newest vdev is faulted, we will not automatically
5499 * remove it after a resilver completes. This is OK as it will require
5500 * user intervention to determine which disk the admin wishes to keep.
5502 if (vd->vdev_ops == &vdev_replacing_ops) {
5503 ASSERT(vd->vdev_children > 1);
5505 newvd = vd->vdev_child[vd->vdev_children - 1];
5506 oldvd = vd->vdev_child[0];
5508 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5509 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5510 !vdev_dtl_required(oldvd))
5515 * Check for a completed resilver with the 'unspare' flag set.
5517 if (vd->vdev_ops == &vdev_spare_ops) {
5518 vdev_t *first = vd->vdev_child[0];
5519 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5521 if (last->vdev_unspare) {
5524 } else if (first->vdev_unspare) {
5531 if (oldvd != NULL &&
5532 vdev_dtl_empty(newvd, DTL_MISSING) &&
5533 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5534 !vdev_dtl_required(oldvd))
5538 * If there are more than two spares attached to a disk,
5539 * and those spares are not required, then we want to
5540 * attempt to free them up now so that they can be used
5541 * by other pools. Once we're back down to a single
5542 * disk+spare, we stop removing them.
5544 if (vd->vdev_children > 2) {
5545 newvd = vd->vdev_child[1];
5547 if (newvd->vdev_isspare && last->vdev_isspare &&
5548 vdev_dtl_empty(last, DTL_MISSING) &&
5549 vdev_dtl_empty(last, DTL_OUTAGE) &&
5550 !vdev_dtl_required(newvd))
5559 spa_vdev_resilver_done(spa_t *spa)
5561 vdev_t *vd, *pvd, *ppvd;
5562 uint64_t guid, sguid, pguid, ppguid;
5564 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5566 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5567 pvd = vd->vdev_parent;
5568 ppvd = pvd->vdev_parent;
5569 guid = vd->vdev_guid;
5570 pguid = pvd->vdev_guid;
5571 ppguid = ppvd->vdev_guid;
5574 * If we have just finished replacing a hot spared device, then
5575 * we need to detach the parent's first child (the original hot
5578 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5579 ppvd->vdev_children == 2) {
5580 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5581 sguid = ppvd->vdev_child[1]->vdev_guid;
5583 spa_config_exit(spa, SCL_ALL, FTAG);
5584 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5586 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5588 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5591 spa_config_exit(spa, SCL_ALL, FTAG);
5595 * Update the stored path or FRU for this vdev.
5598 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5602 boolean_t sync = B_FALSE;
5604 ASSERT(spa_writeable(spa));
5606 spa_vdev_state_enter(spa, SCL_ALL);
5608 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5609 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5611 if (!vd->vdev_ops->vdev_op_leaf)
5612 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5615 if (strcmp(value, vd->vdev_path) != 0) {
5616 spa_strfree(vd->vdev_path);
5617 vd->vdev_path = spa_strdup(value);
5621 if (vd->vdev_fru == NULL) {
5622 vd->vdev_fru = spa_strdup(value);
5624 } else if (strcmp(value, vd->vdev_fru) != 0) {
5625 spa_strfree(vd->vdev_fru);
5626 vd->vdev_fru = spa_strdup(value);
5631 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5635 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5637 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5641 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5643 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5647 * ==========================================================================
5649 * ==========================================================================
5653 spa_scan_stop(spa_t *spa)
5655 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5656 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5657 return (SET_ERROR(EBUSY));
5658 return (dsl_scan_cancel(spa->spa_dsl_pool));
5662 spa_scan(spa_t *spa, pool_scan_func_t func)
5664 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5666 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5667 return (SET_ERROR(ENOTSUP));
5670 * If a resilver was requested, but there is no DTL on a
5671 * writeable leaf device, we have nothing to do.
5673 if (func == POOL_SCAN_RESILVER &&
5674 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5675 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5679 return (dsl_scan(spa->spa_dsl_pool, func));
5683 * ==========================================================================
5684 * SPA async task processing
5685 * ==========================================================================
5689 spa_async_remove(spa_t *spa, vdev_t *vd)
5691 if (vd->vdev_remove_wanted) {
5692 vd->vdev_remove_wanted = B_FALSE;
5693 vd->vdev_delayed_close = B_FALSE;
5694 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5697 * We want to clear the stats, but we don't want to do a full
5698 * vdev_clear() as that will cause us to throw away
5699 * degraded/faulted state as well as attempt to reopen the
5700 * device, all of which is a waste.
5702 vd->vdev_stat.vs_read_errors = 0;
5703 vd->vdev_stat.vs_write_errors = 0;
5704 vd->vdev_stat.vs_checksum_errors = 0;
5706 vdev_state_dirty(vd->vdev_top);
5709 for (int c = 0; c < vd->vdev_children; c++)
5710 spa_async_remove(spa, vd->vdev_child[c]);
5714 spa_async_probe(spa_t *spa, vdev_t *vd)
5716 if (vd->vdev_probe_wanted) {
5717 vd->vdev_probe_wanted = B_FALSE;
5718 vdev_reopen(vd); /* vdev_open() does the actual probe */
5721 for (int c = 0; c < vd->vdev_children; c++)
5722 spa_async_probe(spa, vd->vdev_child[c]);
5726 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5732 if (!spa->spa_autoexpand)
5735 for (int c = 0; c < vd->vdev_children; c++) {
5736 vdev_t *cvd = vd->vdev_child[c];
5737 spa_async_autoexpand(spa, cvd);
5740 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5743 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5744 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5746 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5747 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5749 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5750 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5753 kmem_free(physpath, MAXPATHLEN);
5757 spa_async_thread(void *arg)
5762 ASSERT(spa->spa_sync_on);
5764 mutex_enter(&spa->spa_async_lock);
5765 tasks = spa->spa_async_tasks;
5766 spa->spa_async_tasks = 0;
5767 mutex_exit(&spa->spa_async_lock);
5770 * See if the config needs to be updated.
5772 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5773 uint64_t old_space, new_space;
5775 mutex_enter(&spa_namespace_lock);
5776 old_space = metaslab_class_get_space(spa_normal_class(spa));
5777 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5778 new_space = metaslab_class_get_space(spa_normal_class(spa));
5779 mutex_exit(&spa_namespace_lock);
5782 * If the pool grew as a result of the config update,
5783 * then log an internal history event.
5785 if (new_space != old_space) {
5786 spa_history_log_internal(spa, "vdev online", NULL,
5787 "pool '%s' size: %llu(+%llu)",
5788 spa_name(spa), new_space, new_space - old_space);
5793 * See if any devices need to be marked REMOVED.
5795 if (tasks & SPA_ASYNC_REMOVE) {
5796 spa_vdev_state_enter(spa, SCL_NONE);
5797 spa_async_remove(spa, spa->spa_root_vdev);
5798 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5799 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5800 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5801 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5802 (void) spa_vdev_state_exit(spa, NULL, 0);
5805 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5806 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5807 spa_async_autoexpand(spa, spa->spa_root_vdev);
5808 spa_config_exit(spa, SCL_CONFIG, FTAG);
5812 * See if any devices need to be probed.
5814 if (tasks & SPA_ASYNC_PROBE) {
5815 spa_vdev_state_enter(spa, SCL_NONE);
5816 spa_async_probe(spa, spa->spa_root_vdev);
5817 (void) spa_vdev_state_exit(spa, NULL, 0);
5821 * If any devices are done replacing, detach them.
5823 if (tasks & SPA_ASYNC_RESILVER_DONE)
5824 spa_vdev_resilver_done(spa);
5827 * Kick off a resilver.
5829 if (tasks & SPA_ASYNC_RESILVER)
5830 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5833 * Let the world know that we're done.
5835 mutex_enter(&spa->spa_async_lock);
5836 spa->spa_async_thread = NULL;
5837 cv_broadcast(&spa->spa_async_cv);
5838 mutex_exit(&spa->spa_async_lock);
5843 spa_async_suspend(spa_t *spa)
5845 mutex_enter(&spa->spa_async_lock);
5846 spa->spa_async_suspended++;
5847 while (spa->spa_async_thread != NULL)
5848 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5849 mutex_exit(&spa->spa_async_lock);
5853 spa_async_resume(spa_t *spa)
5855 mutex_enter(&spa->spa_async_lock);
5856 ASSERT(spa->spa_async_suspended != 0);
5857 spa->spa_async_suspended--;
5858 mutex_exit(&spa->spa_async_lock);
5862 spa_async_tasks_pending(spa_t *spa)
5864 uint_t non_config_tasks;
5866 boolean_t config_task_suspended;
5868 non_config_tasks = spa->spa_async_tasks & ~SPA_ASYNC_CONFIG_UPDATE;
5869 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
5870 if (spa->spa_ccw_fail_time == 0) {
5871 config_task_suspended = B_FALSE;
5873 config_task_suspended =
5874 (gethrtime() - spa->spa_ccw_fail_time) <
5875 (zfs_ccw_retry_interval * NANOSEC);
5878 return (non_config_tasks || (config_task && !config_task_suspended));
5882 spa_async_dispatch(spa_t *spa)
5884 mutex_enter(&spa->spa_async_lock);
5885 if (spa_async_tasks_pending(spa) &&
5886 !spa->spa_async_suspended &&
5887 spa->spa_async_thread == NULL &&
5889 spa->spa_async_thread = thread_create(NULL, 0,
5890 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5891 mutex_exit(&spa->spa_async_lock);
5895 spa_async_request(spa_t *spa, int task)
5897 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5898 mutex_enter(&spa->spa_async_lock);
5899 spa->spa_async_tasks |= task;
5900 mutex_exit(&spa->spa_async_lock);
5904 * ==========================================================================
5905 * SPA syncing routines
5906 * ==========================================================================
5910 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5913 bpobj_enqueue(bpo, bp, tx);
5918 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5922 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5923 BP_GET_PSIZE(bp), zio->io_flags));
5928 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5930 char *packed = NULL;
5935 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5938 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5939 * information. This avoids the dbuf_will_dirty() path and
5940 * saves us a pre-read to get data we don't actually care about.
5942 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
5943 packed = kmem_alloc(bufsize, KM_SLEEP);
5945 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5947 bzero(packed + nvsize, bufsize - nvsize);
5949 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5951 kmem_free(packed, bufsize);
5953 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5954 dmu_buf_will_dirty(db, tx);
5955 *(uint64_t *)db->db_data = nvsize;
5956 dmu_buf_rele(db, FTAG);
5960 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5961 const char *config, const char *entry)
5971 * Update the MOS nvlist describing the list of available devices.
5972 * spa_validate_aux() will have already made sure this nvlist is
5973 * valid and the vdevs are labeled appropriately.
5975 if (sav->sav_object == 0) {
5976 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5977 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5978 sizeof (uint64_t), tx);
5979 VERIFY(zap_update(spa->spa_meta_objset,
5980 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5981 &sav->sav_object, tx) == 0);
5984 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5985 if (sav->sav_count == 0) {
5986 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5988 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5989 for (i = 0; i < sav->sav_count; i++)
5990 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5991 B_FALSE, VDEV_CONFIG_L2CACHE);
5992 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5993 sav->sav_count) == 0);
5994 for (i = 0; i < sav->sav_count; i++)
5995 nvlist_free(list[i]);
5996 kmem_free(list, sav->sav_count * sizeof (void *));
5999 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6000 nvlist_free(nvroot);
6002 sav->sav_sync = B_FALSE;
6006 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6010 if (list_is_empty(&spa->spa_config_dirty_list))
6013 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6015 config = spa_config_generate(spa, spa->spa_root_vdev,
6016 dmu_tx_get_txg(tx), B_FALSE);
6019 * If we're upgrading the spa version then make sure that
6020 * the config object gets updated with the correct version.
6022 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6023 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6024 spa->spa_uberblock.ub_version);
6026 spa_config_exit(spa, SCL_STATE, FTAG);
6028 if (spa->spa_config_syncing)
6029 nvlist_free(spa->spa_config_syncing);
6030 spa->spa_config_syncing = config;
6032 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6036 spa_sync_version(void *arg, dmu_tx_t *tx)
6038 uint64_t *versionp = arg;
6039 uint64_t version = *versionp;
6040 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6043 * Setting the version is special cased when first creating the pool.
6045 ASSERT(tx->tx_txg != TXG_INITIAL);
6047 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6048 ASSERT(version >= spa_version(spa));
6050 spa->spa_uberblock.ub_version = version;
6051 vdev_config_dirty(spa->spa_root_vdev);
6052 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6056 * Set zpool properties.
6059 spa_sync_props(void *arg, dmu_tx_t *tx)
6061 nvlist_t *nvp = arg;
6062 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6063 objset_t *mos = spa->spa_meta_objset;
6064 nvpair_t *elem = NULL;
6066 mutex_enter(&spa->spa_props_lock);
6068 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6070 char *strval, *fname;
6072 const char *propname;
6073 zprop_type_t proptype;
6074 zfeature_info_t *feature;
6076 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6079 * We checked this earlier in spa_prop_validate().
6081 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6083 fname = strchr(nvpair_name(elem), '@') + 1;
6084 VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature));
6086 spa_feature_enable(spa, feature, tx);
6087 spa_history_log_internal(spa, "set", tx,
6088 "%s=enabled", nvpair_name(elem));
6091 case ZPOOL_PROP_VERSION:
6092 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
6094 * The version is synced seperatly before other
6095 * properties and should be correct by now.
6097 ASSERT3U(spa_version(spa), >=, intval);
6100 case ZPOOL_PROP_ALTROOT:
6102 * 'altroot' is a non-persistent property. It should
6103 * have been set temporarily at creation or import time.
6105 ASSERT(spa->spa_root != NULL);
6108 case ZPOOL_PROP_READONLY:
6109 case ZPOOL_PROP_CACHEFILE:
6111 * 'readonly' and 'cachefile' are also non-persisitent
6115 case ZPOOL_PROP_COMMENT:
6116 VERIFY(nvpair_value_string(elem, &strval) == 0);
6117 if (spa->spa_comment != NULL)
6118 spa_strfree(spa->spa_comment);
6119 spa->spa_comment = spa_strdup(strval);
6121 * We need to dirty the configuration on all the vdevs
6122 * so that their labels get updated. It's unnecessary
6123 * to do this for pool creation since the vdev's
6124 * configuratoin has already been dirtied.
6126 if (tx->tx_txg != TXG_INITIAL)
6127 vdev_config_dirty(spa->spa_root_vdev);
6128 spa_history_log_internal(spa, "set", tx,
6129 "%s=%s", nvpair_name(elem), strval);
6133 * Set pool property values in the poolprops mos object.
6135 if (spa->spa_pool_props_object == 0) {
6136 spa->spa_pool_props_object =
6137 zap_create_link(mos, DMU_OT_POOL_PROPS,
6138 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6142 /* normalize the property name */
6143 propname = zpool_prop_to_name(prop);
6144 proptype = zpool_prop_get_type(prop);
6146 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6147 ASSERT(proptype == PROP_TYPE_STRING);
6148 VERIFY(nvpair_value_string(elem, &strval) == 0);
6149 VERIFY(zap_update(mos,
6150 spa->spa_pool_props_object, propname,
6151 1, strlen(strval) + 1, strval, tx) == 0);
6152 spa_history_log_internal(spa, "set", tx,
6153 "%s=%s", nvpair_name(elem), strval);
6154 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6155 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
6157 if (proptype == PROP_TYPE_INDEX) {
6159 VERIFY(zpool_prop_index_to_string(
6160 prop, intval, &unused) == 0);
6162 VERIFY(zap_update(mos,
6163 spa->spa_pool_props_object, propname,
6164 8, 1, &intval, tx) == 0);
6165 spa_history_log_internal(spa, "set", tx,
6166 "%s=%lld", nvpair_name(elem), intval);
6168 ASSERT(0); /* not allowed */
6172 case ZPOOL_PROP_DELEGATION:
6173 spa->spa_delegation = intval;
6175 case ZPOOL_PROP_BOOTFS:
6176 spa->spa_bootfs = intval;
6178 case ZPOOL_PROP_FAILUREMODE:
6179 spa->spa_failmode = intval;
6181 case ZPOOL_PROP_AUTOEXPAND:
6182 spa->spa_autoexpand = intval;
6183 if (tx->tx_txg != TXG_INITIAL)
6184 spa_async_request(spa,
6185 SPA_ASYNC_AUTOEXPAND);
6187 case ZPOOL_PROP_DEDUPDITTO:
6188 spa->spa_dedup_ditto = intval;
6197 mutex_exit(&spa->spa_props_lock);
6201 * Perform one-time upgrade on-disk changes. spa_version() does not
6202 * reflect the new version this txg, so there must be no changes this
6203 * txg to anything that the upgrade code depends on after it executes.
6204 * Therefore this must be called after dsl_pool_sync() does the sync
6208 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6210 dsl_pool_t *dp = spa->spa_dsl_pool;
6212 ASSERT(spa->spa_sync_pass == 1);
6214 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6216 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6217 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6218 dsl_pool_create_origin(dp, tx);
6220 /* Keeping the origin open increases spa_minref */
6221 spa->spa_minref += 3;
6224 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6225 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6226 dsl_pool_upgrade_clones(dp, tx);
6229 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6230 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6231 dsl_pool_upgrade_dir_clones(dp, tx);
6233 /* Keeping the freedir open increases spa_minref */
6234 spa->spa_minref += 3;
6237 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6238 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6239 spa_feature_create_zap_objects(spa, tx);
6241 rrw_exit(&dp->dp_config_rwlock, FTAG);
6245 * Sync the specified transaction group. New blocks may be dirtied as
6246 * part of the process, so we iterate until it converges.
6249 spa_sync(spa_t *spa, uint64_t txg)
6251 dsl_pool_t *dp = spa->spa_dsl_pool;
6252 objset_t *mos = spa->spa_meta_objset;
6253 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
6254 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6255 vdev_t *rvd = spa->spa_root_vdev;
6260 VERIFY(spa_writeable(spa));
6263 * Lock out configuration changes.
6265 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6267 spa->spa_syncing_txg = txg;
6268 spa->spa_sync_pass = 0;
6271 * If there are any pending vdev state changes, convert them
6272 * into config changes that go out with this transaction group.
6274 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6275 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6277 * We need the write lock here because, for aux vdevs,
6278 * calling vdev_config_dirty() modifies sav_config.
6279 * This is ugly and will become unnecessary when we
6280 * eliminate the aux vdev wart by integrating all vdevs
6281 * into the root vdev tree.
6283 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6284 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6285 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6286 vdev_state_clean(vd);
6287 vdev_config_dirty(vd);
6289 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6290 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6292 spa_config_exit(spa, SCL_STATE, FTAG);
6294 tx = dmu_tx_create_assigned(dp, txg);
6296 spa->spa_sync_starttime = gethrtime();
6298 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6299 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6302 callout_reset(&spa->spa_deadman_cycid,
6303 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6308 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6309 * set spa_deflate if we have no raid-z vdevs.
6311 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6312 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6315 for (i = 0; i < rvd->vdev_children; i++) {
6316 vd = rvd->vdev_child[i];
6317 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6320 if (i == rvd->vdev_children) {
6321 spa->spa_deflate = TRUE;
6322 VERIFY(0 == zap_add(spa->spa_meta_objset,
6323 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6324 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6329 * If anything has changed in this txg, or if someone is waiting
6330 * for this txg to sync (eg, spa_vdev_remove()), push the
6331 * deferred frees from the previous txg. If not, leave them
6332 * alone so that we don't generate work on an otherwise idle
6335 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6336 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6337 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6338 ((dsl_scan_active(dp->dp_scan) ||
6339 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6340 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6341 VERIFY3U(bpobj_iterate(defer_bpo,
6342 spa_free_sync_cb, zio, tx), ==, 0);
6343 VERIFY0(zio_wait(zio));
6347 * Iterate to convergence.
6350 int pass = ++spa->spa_sync_pass;
6352 spa_sync_config_object(spa, tx);
6353 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6354 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6355 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6356 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6357 spa_errlog_sync(spa, txg);
6358 dsl_pool_sync(dp, txg);
6360 if (pass < zfs_sync_pass_deferred_free) {
6361 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6362 bplist_iterate(free_bpl, spa_free_sync_cb,
6364 VERIFY(zio_wait(zio) == 0);
6366 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6371 dsl_scan_sync(dp, tx);
6373 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6377 spa_sync_upgrades(spa, tx);
6379 } while (dmu_objset_is_dirty(mos, txg));
6382 * Rewrite the vdev configuration (which includes the uberblock)
6383 * to commit the transaction group.
6385 * If there are no dirty vdevs, we sync the uberblock to a few
6386 * random top-level vdevs that are known to be visible in the
6387 * config cache (see spa_vdev_add() for a complete description).
6388 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6392 * We hold SCL_STATE to prevent vdev open/close/etc.
6393 * while we're attempting to write the vdev labels.
6395 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6397 if (list_is_empty(&spa->spa_config_dirty_list)) {
6398 vdev_t *svd[SPA_DVAS_PER_BP];
6400 int children = rvd->vdev_children;
6401 int c0 = spa_get_random(children);
6403 for (int c = 0; c < children; c++) {
6404 vd = rvd->vdev_child[(c0 + c) % children];
6405 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6407 svd[svdcount++] = vd;
6408 if (svdcount == SPA_DVAS_PER_BP)
6411 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6413 error = vdev_config_sync(svd, svdcount, txg,
6416 error = vdev_config_sync(rvd->vdev_child,
6417 rvd->vdev_children, txg, B_FALSE);
6419 error = vdev_config_sync(rvd->vdev_child,
6420 rvd->vdev_children, txg, B_TRUE);
6424 spa->spa_last_synced_guid = rvd->vdev_guid;
6426 spa_config_exit(spa, SCL_STATE, FTAG);
6430 zio_suspend(spa, NULL);
6431 zio_resume_wait(spa);
6436 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6439 callout_drain(&spa->spa_deadman_cycid);
6444 * Clear the dirty config list.
6446 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6447 vdev_config_clean(vd);
6450 * Now that the new config has synced transactionally,
6451 * let it become visible to the config cache.
6453 if (spa->spa_config_syncing != NULL) {
6454 spa_config_set(spa, spa->spa_config_syncing);
6455 spa->spa_config_txg = txg;
6456 spa->spa_config_syncing = NULL;
6459 spa->spa_ubsync = spa->spa_uberblock;
6461 dsl_pool_sync_done(dp, txg);
6464 * Update usable space statistics.
6466 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6467 vdev_sync_done(vd, txg);
6469 spa_update_dspace(spa);
6472 * It had better be the case that we didn't dirty anything
6473 * since vdev_config_sync().
6475 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6476 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6477 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6479 spa->spa_sync_pass = 0;
6481 spa_config_exit(spa, SCL_CONFIG, FTAG);
6483 spa_handle_ignored_writes(spa);
6486 * If any async tasks have been requested, kick them off.
6488 spa_async_dispatch(spa);
6492 * Sync all pools. We don't want to hold the namespace lock across these
6493 * operations, so we take a reference on the spa_t and drop the lock during the
6497 spa_sync_allpools(void)
6500 mutex_enter(&spa_namespace_lock);
6501 while ((spa = spa_next(spa)) != NULL) {
6502 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6503 !spa_writeable(spa) || spa_suspended(spa))
6505 spa_open_ref(spa, FTAG);
6506 mutex_exit(&spa_namespace_lock);
6507 txg_wait_synced(spa_get_dsl(spa), 0);
6508 mutex_enter(&spa_namespace_lock);
6509 spa_close(spa, FTAG);
6511 mutex_exit(&spa_namespace_lock);
6515 * ==========================================================================
6516 * Miscellaneous routines
6517 * ==========================================================================
6521 * Remove all pools in the system.
6529 * Remove all cached state. All pools should be closed now,
6530 * so every spa in the AVL tree should be unreferenced.
6532 mutex_enter(&spa_namespace_lock);
6533 while ((spa = spa_next(NULL)) != NULL) {
6535 * Stop async tasks. The async thread may need to detach
6536 * a device that's been replaced, which requires grabbing
6537 * spa_namespace_lock, so we must drop it here.
6539 spa_open_ref(spa, FTAG);
6540 mutex_exit(&spa_namespace_lock);
6541 spa_async_suspend(spa);
6542 mutex_enter(&spa_namespace_lock);
6543 spa_close(spa, FTAG);
6545 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6547 spa_deactivate(spa);
6551 mutex_exit(&spa_namespace_lock);
6555 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6560 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6564 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6565 vd = spa->spa_l2cache.sav_vdevs[i];
6566 if (vd->vdev_guid == guid)
6570 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6571 vd = spa->spa_spares.sav_vdevs[i];
6572 if (vd->vdev_guid == guid)
6581 spa_upgrade(spa_t *spa, uint64_t version)
6583 ASSERT(spa_writeable(spa));
6585 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6588 * This should only be called for a non-faulted pool, and since a
6589 * future version would result in an unopenable pool, this shouldn't be
6592 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6593 ASSERT(version >= spa->spa_uberblock.ub_version);
6595 spa->spa_uberblock.ub_version = version;
6596 vdev_config_dirty(spa->spa_root_vdev);
6598 spa_config_exit(spa, SCL_ALL, FTAG);
6600 txg_wait_synced(spa_get_dsl(spa), 0);
6604 spa_has_spare(spa_t *spa, uint64_t guid)
6608 spa_aux_vdev_t *sav = &spa->spa_spares;
6610 for (i = 0; i < sav->sav_count; i++)
6611 if (sav->sav_vdevs[i]->vdev_guid == guid)
6614 for (i = 0; i < sav->sav_npending; i++) {
6615 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6616 &spareguid) == 0 && spareguid == guid)
6624 * Check if a pool has an active shared spare device.
6625 * Note: reference count of an active spare is 2, as a spare and as a replace
6628 spa_has_active_shared_spare(spa_t *spa)
6632 spa_aux_vdev_t *sav = &spa->spa_spares;
6634 for (i = 0; i < sav->sav_count; i++) {
6635 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6636 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6645 * Post a sysevent corresponding to the given event. The 'name' must be one of
6646 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6647 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6648 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6649 * or zdb as real changes.
6652 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6656 sysevent_attr_list_t *attr = NULL;
6657 sysevent_value_t value;
6660 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6663 value.value_type = SE_DATA_TYPE_STRING;
6664 value.value.sv_string = spa_name(spa);
6665 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6668 value.value_type = SE_DATA_TYPE_UINT64;
6669 value.value.sv_uint64 = spa_guid(spa);
6670 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6674 value.value_type = SE_DATA_TYPE_UINT64;
6675 value.value.sv_uint64 = vd->vdev_guid;
6676 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6680 if (vd->vdev_path) {
6681 value.value_type = SE_DATA_TYPE_STRING;
6682 value.value.sv_string = vd->vdev_path;
6683 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6684 &value, SE_SLEEP) != 0)
6689 if (sysevent_attach_attributes(ev, attr) != 0)
6693 (void) log_sysevent(ev, SE_SLEEP, &eid);
6697 sysevent_free_attr(attr);