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 * This file contains all the routines used when modifying on-disk SPA state.
31 * This includes opening, importing, destroying, exporting a pool, and syncing a
35 #include <sys/zfs_context.h>
36 #include <sys/fm/fs/zfs.h>
37 #include <sys/spa_impl.h>
39 #include <sys/zio_checksum.h>
41 #include <sys/dmu_tx.h>
45 #include <sys/vdev_impl.h>
46 #include <sys/metaslab.h>
47 #include <sys/metaslab_impl.h>
48 #include <sys/uberblock_impl.h>
51 #include <sys/dmu_traverse.h>
52 #include <sys/dmu_objset.h>
53 #include <sys/unique.h>
54 #include <sys/dsl_pool.h>
55 #include <sys/dsl_dataset.h>
56 #include <sys/dsl_dir.h>
57 #include <sys/dsl_prop.h>
58 #include <sys/dsl_synctask.h>
59 #include <sys/fs/zfs.h>
61 #include <sys/callb.h>
62 #include <sys/spa_boot.h>
63 #include <sys/zfs_ioctl.h>
64 #include <sys/dsl_scan.h>
65 #include <sys/dmu_send.h>
66 #include <sys/dsl_destroy.h>
67 #include <sys/dsl_userhold.h>
68 #include <sys/zfeature.h>
70 #include <sys/trim_map.h>
73 #include <sys/callb.h>
74 #include <sys/cpupart.h>
79 #include "zfs_comutil.h"
81 /* Check hostid on import? */
82 static int check_hostid = 1;
84 SYSCTL_DECL(_vfs_zfs);
85 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
86 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
87 "Check hostid on import?");
89 typedef enum zti_modes {
90 zti_mode_fixed, /* value is # of threads (min 1) */
91 zti_mode_online_percent, /* value is % of online CPUs */
92 zti_mode_batch, /* cpu-intensive; value is ignored */
93 zti_mode_null, /* don't create a taskq */
97 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
98 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
99 #define ZTI_BATCH { zti_mode_batch, 0 }
100 #define ZTI_NULL { zti_mode_null, 0 }
102 #define ZTI_ONE ZTI_FIX(1)
104 typedef struct zio_taskq_info {
105 enum zti_modes zti_mode;
109 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
110 "issue", "issue_high", "intr", "intr_high"
114 * Define the taskq threads for the following I/O types:
115 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
117 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
118 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
119 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
120 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
121 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
122 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
123 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
124 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
127 static void spa_sync_version(void *arg, dmu_tx_t *tx);
128 static void spa_sync_props(void *arg, dmu_tx_t *tx);
129 static boolean_t spa_has_active_shared_spare(spa_t *spa);
130 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
131 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
133 static void spa_vdev_resilver_done(spa_t *spa);
135 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
137 id_t zio_taskq_psrset_bind = PS_NONE;
140 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
142 uint_t zio_taskq_basedc = 80; /* base duty cycle */
144 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
145 extern int zfs_sync_pass_deferred_free;
148 extern void spa_deadman(void *arg);
152 * This (illegal) pool name is used when temporarily importing a spa_t in order
153 * to get the vdev stats associated with the imported devices.
155 #define TRYIMPORT_NAME "$import"
158 * ==========================================================================
159 * SPA properties routines
160 * ==========================================================================
164 * Add a (source=src, propname=propval) list to an nvlist.
167 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
168 uint64_t intval, zprop_source_t src)
170 const char *propname = zpool_prop_to_name(prop);
173 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
174 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
177 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
179 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
181 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
182 nvlist_free(propval);
186 * Get property values from the spa configuration.
189 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
191 vdev_t *rvd = spa->spa_root_vdev;
192 dsl_pool_t *pool = spa->spa_dsl_pool;
196 uint64_t cap, version;
197 zprop_source_t src = ZPROP_SRC_NONE;
198 spa_config_dirent_t *dp;
200 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
203 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
204 size = metaslab_class_get_space(spa_normal_class(spa));
205 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
206 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
207 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
208 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
212 for (int c = 0; c < rvd->vdev_children; c++) {
213 vdev_t *tvd = rvd->vdev_child[c];
214 space += tvd->vdev_max_asize - tvd->vdev_asize;
216 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
219 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
220 (spa_mode(spa) == FREAD), src);
222 cap = (size == 0) ? 0 : (alloc * 100 / size);
223 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
225 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
226 ddt_get_pool_dedup_ratio(spa), src);
228 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
229 rvd->vdev_state, src);
231 version = spa_version(spa);
232 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
233 src = ZPROP_SRC_DEFAULT;
235 src = ZPROP_SRC_LOCAL;
236 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
240 dsl_dir_t *freedir = pool->dp_free_dir;
243 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
244 * when opening pools before this version freedir will be NULL.
246 if (freedir != NULL) {
247 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
248 freedir->dd_phys->dd_used_bytes, src);
250 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
255 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
257 if (spa->spa_comment != NULL) {
258 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
262 if (spa->spa_root != NULL)
263 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
266 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
267 if (dp->scd_path == NULL) {
268 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
269 "none", 0, ZPROP_SRC_LOCAL);
270 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
271 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
272 dp->scd_path, 0, ZPROP_SRC_LOCAL);
278 * Get zpool property values.
281 spa_prop_get(spa_t *spa, nvlist_t **nvp)
283 objset_t *mos = spa->spa_meta_objset;
288 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
290 mutex_enter(&spa->spa_props_lock);
293 * Get properties from the spa config.
295 spa_prop_get_config(spa, nvp);
297 /* If no pool property object, no more prop to get. */
298 if (mos == NULL || spa->spa_pool_props_object == 0) {
299 mutex_exit(&spa->spa_props_lock);
304 * Get properties from the MOS pool property object.
306 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
307 (err = zap_cursor_retrieve(&zc, &za)) == 0;
308 zap_cursor_advance(&zc)) {
311 zprop_source_t src = ZPROP_SRC_DEFAULT;
314 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
317 switch (za.za_integer_length) {
319 /* integer property */
320 if (za.za_first_integer !=
321 zpool_prop_default_numeric(prop))
322 src = ZPROP_SRC_LOCAL;
324 if (prop == ZPOOL_PROP_BOOTFS) {
326 dsl_dataset_t *ds = NULL;
328 dp = spa_get_dsl(spa);
329 dsl_pool_config_enter(dp, FTAG);
330 if (err = dsl_dataset_hold_obj(dp,
331 za.za_first_integer, FTAG, &ds)) {
332 dsl_pool_config_exit(dp, FTAG);
337 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
339 dsl_dataset_name(ds, strval);
340 dsl_dataset_rele(ds, FTAG);
341 dsl_pool_config_exit(dp, FTAG);
344 intval = za.za_first_integer;
347 spa_prop_add_list(*nvp, prop, strval, intval, src);
351 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
356 /* string property */
357 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
358 err = zap_lookup(mos, spa->spa_pool_props_object,
359 za.za_name, 1, za.za_num_integers, strval);
361 kmem_free(strval, za.za_num_integers);
364 spa_prop_add_list(*nvp, prop, strval, 0, src);
365 kmem_free(strval, za.za_num_integers);
372 zap_cursor_fini(&zc);
373 mutex_exit(&spa->spa_props_lock);
375 if (err && err != ENOENT) {
385 * Validate the given pool properties nvlist and modify the list
386 * for the property values to be set.
389 spa_prop_validate(spa_t *spa, nvlist_t *props)
392 int error = 0, reset_bootfs = 0;
394 boolean_t has_feature = B_FALSE;
397 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
399 char *strval, *slash, *check, *fname;
400 const char *propname = nvpair_name(elem);
401 zpool_prop_t prop = zpool_name_to_prop(propname);
405 if (!zpool_prop_feature(propname)) {
406 error = SET_ERROR(EINVAL);
411 * Sanitize the input.
413 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
414 error = SET_ERROR(EINVAL);
418 if (nvpair_value_uint64(elem, &intval) != 0) {
419 error = SET_ERROR(EINVAL);
424 error = SET_ERROR(EINVAL);
428 fname = strchr(propname, '@') + 1;
429 if (zfeature_lookup_name(fname, NULL) != 0) {
430 error = SET_ERROR(EINVAL);
434 has_feature = B_TRUE;
437 case ZPOOL_PROP_VERSION:
438 error = nvpair_value_uint64(elem, &intval);
440 (intval < spa_version(spa) ||
441 intval > SPA_VERSION_BEFORE_FEATURES ||
443 error = SET_ERROR(EINVAL);
446 case ZPOOL_PROP_DELEGATION:
447 case ZPOOL_PROP_AUTOREPLACE:
448 case ZPOOL_PROP_LISTSNAPS:
449 case ZPOOL_PROP_AUTOEXPAND:
450 error = nvpair_value_uint64(elem, &intval);
451 if (!error && intval > 1)
452 error = SET_ERROR(EINVAL);
455 case ZPOOL_PROP_BOOTFS:
457 * If the pool version is less than SPA_VERSION_BOOTFS,
458 * or the pool is still being created (version == 0),
459 * the bootfs property cannot be set.
461 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
462 error = SET_ERROR(ENOTSUP);
467 * Make sure the vdev config is bootable
469 if (!vdev_is_bootable(spa->spa_root_vdev)) {
470 error = SET_ERROR(ENOTSUP);
476 error = nvpair_value_string(elem, &strval);
482 if (strval == NULL || strval[0] == '\0') {
483 objnum = zpool_prop_default_numeric(
488 if (error = dmu_objset_hold(strval, FTAG, &os))
491 /* Must be ZPL and not gzip compressed. */
493 if (dmu_objset_type(os) != DMU_OST_ZFS) {
494 error = SET_ERROR(ENOTSUP);
496 dsl_prop_get_int_ds(dmu_objset_ds(os),
497 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
499 !BOOTFS_COMPRESS_VALID(compress)) {
500 error = SET_ERROR(ENOTSUP);
502 objnum = dmu_objset_id(os);
504 dmu_objset_rele(os, FTAG);
508 case ZPOOL_PROP_FAILUREMODE:
509 error = nvpair_value_uint64(elem, &intval);
510 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
511 intval > ZIO_FAILURE_MODE_PANIC))
512 error = SET_ERROR(EINVAL);
515 * This is a special case which only occurs when
516 * the pool has completely failed. This allows
517 * the user to change the in-core failmode property
518 * without syncing it out to disk (I/Os might
519 * currently be blocked). We do this by returning
520 * EIO to the caller (spa_prop_set) to trick it
521 * into thinking we encountered a property validation
524 if (!error && spa_suspended(spa)) {
525 spa->spa_failmode = intval;
526 error = SET_ERROR(EIO);
530 case ZPOOL_PROP_CACHEFILE:
531 if ((error = nvpair_value_string(elem, &strval)) != 0)
534 if (strval[0] == '\0')
537 if (strcmp(strval, "none") == 0)
540 if (strval[0] != '/') {
541 error = SET_ERROR(EINVAL);
545 slash = strrchr(strval, '/');
546 ASSERT(slash != NULL);
548 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
549 strcmp(slash, "/..") == 0)
550 error = SET_ERROR(EINVAL);
553 case ZPOOL_PROP_COMMENT:
554 if ((error = nvpair_value_string(elem, &strval)) != 0)
556 for (check = strval; *check != '\0'; check++) {
558 * The kernel doesn't have an easy isprint()
559 * check. For this kernel check, we merely
560 * check ASCII apart from DEL. Fix this if
561 * there is an easy-to-use kernel isprint().
563 if (*check >= 0x7f) {
564 error = SET_ERROR(EINVAL);
569 if (strlen(strval) > ZPROP_MAX_COMMENT)
573 case ZPOOL_PROP_DEDUPDITTO:
574 if (spa_version(spa) < SPA_VERSION_DEDUP)
575 error = SET_ERROR(ENOTSUP);
577 error = nvpair_value_uint64(elem, &intval);
579 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
580 error = SET_ERROR(EINVAL);
588 if (!error && reset_bootfs) {
589 error = nvlist_remove(props,
590 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
593 error = nvlist_add_uint64(props,
594 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
602 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
605 spa_config_dirent_t *dp;
607 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
611 dp = kmem_alloc(sizeof (spa_config_dirent_t),
614 if (cachefile[0] == '\0')
615 dp->scd_path = spa_strdup(spa_config_path);
616 else if (strcmp(cachefile, "none") == 0)
619 dp->scd_path = spa_strdup(cachefile);
621 list_insert_head(&spa->spa_config_list, dp);
623 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
627 spa_prop_set(spa_t *spa, nvlist_t *nvp)
630 nvpair_t *elem = NULL;
631 boolean_t need_sync = B_FALSE;
633 if ((error = spa_prop_validate(spa, nvp)) != 0)
636 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
637 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
639 if (prop == ZPOOL_PROP_CACHEFILE ||
640 prop == ZPOOL_PROP_ALTROOT ||
641 prop == ZPOOL_PROP_READONLY)
644 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
647 if (prop == ZPOOL_PROP_VERSION) {
648 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
650 ASSERT(zpool_prop_feature(nvpair_name(elem)));
651 ver = SPA_VERSION_FEATURES;
655 /* Save time if the version is already set. */
656 if (ver == spa_version(spa))
660 * In addition to the pool directory object, we might
661 * create the pool properties object, the features for
662 * read object, the features for write object, or the
663 * feature descriptions object.
665 error = dsl_sync_task(spa->spa_name, NULL,
666 spa_sync_version, &ver, 6);
677 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
685 * If the bootfs property value is dsobj, clear it.
688 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
690 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
691 VERIFY(zap_remove(spa->spa_meta_objset,
692 spa->spa_pool_props_object,
693 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
700 spa_change_guid_check(void *arg, dmu_tx_t *tx)
702 uint64_t *newguid = arg;
703 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
704 vdev_t *rvd = spa->spa_root_vdev;
707 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
708 vdev_state = rvd->vdev_state;
709 spa_config_exit(spa, SCL_STATE, FTAG);
711 if (vdev_state != VDEV_STATE_HEALTHY)
712 return (SET_ERROR(ENXIO));
714 ASSERT3U(spa_guid(spa), !=, *newguid);
720 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
722 uint64_t *newguid = arg;
723 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
725 vdev_t *rvd = spa->spa_root_vdev;
727 oldguid = spa_guid(spa);
729 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
730 rvd->vdev_guid = *newguid;
731 rvd->vdev_guid_sum += (*newguid - oldguid);
732 vdev_config_dirty(rvd);
733 spa_config_exit(spa, SCL_STATE, FTAG);
735 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
740 * Change the GUID for the pool. This is done so that we can later
741 * re-import a pool built from a clone of our own vdevs. We will modify
742 * the root vdev's guid, our own pool guid, and then mark all of our
743 * vdevs dirty. Note that we must make sure that all our vdevs are
744 * online when we do this, or else any vdevs that weren't present
745 * would be orphaned from our pool. We are also going to issue a
746 * sysevent to update any watchers.
749 spa_change_guid(spa_t *spa)
754 mutex_enter(&spa_namespace_lock);
755 guid = spa_generate_guid(NULL);
757 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
758 spa_change_guid_sync, &guid, 5);
761 spa_config_sync(spa, B_FALSE, B_TRUE);
762 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
765 mutex_exit(&spa_namespace_lock);
771 * ==========================================================================
772 * SPA state manipulation (open/create/destroy/import/export)
773 * ==========================================================================
777 spa_error_entry_compare(const void *a, const void *b)
779 spa_error_entry_t *sa = (spa_error_entry_t *)a;
780 spa_error_entry_t *sb = (spa_error_entry_t *)b;
783 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
784 sizeof (zbookmark_t));
795 * Utility function which retrieves copies of the current logs and
796 * re-initializes them in the process.
799 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
801 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
803 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
804 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
806 avl_create(&spa->spa_errlist_scrub,
807 spa_error_entry_compare, sizeof (spa_error_entry_t),
808 offsetof(spa_error_entry_t, se_avl));
809 avl_create(&spa->spa_errlist_last,
810 spa_error_entry_compare, sizeof (spa_error_entry_t),
811 offsetof(spa_error_entry_t, se_avl));
815 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
818 uint_t flags = TASKQ_PREPOPULATE;
819 boolean_t batch = B_FALSE;
823 return (NULL); /* no taskq needed */
826 ASSERT3U(value, >=, 1);
827 value = MAX(value, 1);
832 flags |= TASKQ_THREADS_CPU_PCT;
833 value = zio_taskq_batch_pct;
836 case zti_mode_online_percent:
837 flags |= TASKQ_THREADS_CPU_PCT;
841 panic("unrecognized mode for %s taskq (%u:%u) in "
848 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
850 flags |= TASKQ_DC_BATCH;
852 return (taskq_create_sysdc(name, value, 50, INT_MAX,
853 spa->spa_proc, zio_taskq_basedc, flags));
856 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
857 spa->spa_proc, flags));
861 spa_create_zio_taskqs(spa_t *spa)
863 for (int t = 0; t < ZIO_TYPES; t++) {
864 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
865 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
866 enum zti_modes mode = ztip->zti_mode;
867 uint_t value = ztip->zti_value;
870 (void) snprintf(name, sizeof (name),
871 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
873 spa->spa_zio_taskq[t][q] =
874 spa_taskq_create(spa, name, mode, value);
882 spa_thread(void *arg)
887 user_t *pu = PTOU(curproc);
889 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
892 ASSERT(curproc != &p0);
893 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
894 "zpool-%s", spa->spa_name);
895 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
898 /* bind this thread to the requested psrset */
899 if (zio_taskq_psrset_bind != PS_NONE) {
901 mutex_enter(&cpu_lock);
902 mutex_enter(&pidlock);
903 mutex_enter(&curproc->p_lock);
905 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
906 0, NULL, NULL) == 0) {
907 curthread->t_bind_pset = zio_taskq_psrset_bind;
910 "Couldn't bind process for zfs pool \"%s\" to "
911 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
914 mutex_exit(&curproc->p_lock);
915 mutex_exit(&pidlock);
916 mutex_exit(&cpu_lock);
922 if (zio_taskq_sysdc) {
923 sysdc_thread_enter(curthread, 100, 0);
927 spa->spa_proc = curproc;
928 spa->spa_did = curthread->t_did;
930 spa_create_zio_taskqs(spa);
932 mutex_enter(&spa->spa_proc_lock);
933 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
935 spa->spa_proc_state = SPA_PROC_ACTIVE;
936 cv_broadcast(&spa->spa_proc_cv);
938 CALLB_CPR_SAFE_BEGIN(&cprinfo);
939 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
940 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
941 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
943 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
944 spa->spa_proc_state = SPA_PROC_GONE;
946 cv_broadcast(&spa->spa_proc_cv);
947 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
949 mutex_enter(&curproc->p_lock);
952 #endif /* SPA_PROCESS */
956 * Activate an uninitialized pool.
959 spa_activate(spa_t *spa, int mode)
961 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
963 spa->spa_state = POOL_STATE_ACTIVE;
964 spa->spa_mode = mode;
966 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
967 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
969 /* Try to create a covering process */
970 mutex_enter(&spa->spa_proc_lock);
971 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
972 ASSERT(spa->spa_proc == &p0);
976 /* Only create a process if we're going to be around a while. */
977 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
978 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
980 spa->spa_proc_state = SPA_PROC_CREATED;
981 while (spa->spa_proc_state == SPA_PROC_CREATED) {
982 cv_wait(&spa->spa_proc_cv,
983 &spa->spa_proc_lock);
985 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
986 ASSERT(spa->spa_proc != &p0);
987 ASSERT(spa->spa_did != 0);
991 "Couldn't create process for zfs pool \"%s\"\n",
996 #endif /* SPA_PROCESS */
997 mutex_exit(&spa->spa_proc_lock);
999 /* If we didn't create a process, we need to create our taskqs. */
1000 ASSERT(spa->spa_proc == &p0);
1001 if (spa->spa_proc == &p0) {
1002 spa_create_zio_taskqs(spa);
1006 * Start TRIM thread.
1008 trim_thread_create(spa);
1010 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1011 offsetof(vdev_t, vdev_config_dirty_node));
1012 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1013 offsetof(vdev_t, vdev_state_dirty_node));
1015 txg_list_create(&spa->spa_vdev_txg_list,
1016 offsetof(struct vdev, vdev_txg_node));
1018 avl_create(&spa->spa_errlist_scrub,
1019 spa_error_entry_compare, sizeof (spa_error_entry_t),
1020 offsetof(spa_error_entry_t, se_avl));
1021 avl_create(&spa->spa_errlist_last,
1022 spa_error_entry_compare, sizeof (spa_error_entry_t),
1023 offsetof(spa_error_entry_t, se_avl));
1027 * Opposite of spa_activate().
1030 spa_deactivate(spa_t *spa)
1032 ASSERT(spa->spa_sync_on == B_FALSE);
1033 ASSERT(spa->spa_dsl_pool == NULL);
1034 ASSERT(spa->spa_root_vdev == NULL);
1035 ASSERT(spa->spa_async_zio_root == NULL);
1036 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1039 * Stop TRIM thread in case spa_unload() wasn't called directly
1040 * before spa_deactivate().
1042 trim_thread_destroy(spa);
1044 txg_list_destroy(&spa->spa_vdev_txg_list);
1046 list_destroy(&spa->spa_config_dirty_list);
1047 list_destroy(&spa->spa_state_dirty_list);
1049 for (int t = 0; t < ZIO_TYPES; t++) {
1050 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1051 if (spa->spa_zio_taskq[t][q] != NULL)
1052 taskq_destroy(spa->spa_zio_taskq[t][q]);
1053 spa->spa_zio_taskq[t][q] = NULL;
1057 metaslab_class_destroy(spa->spa_normal_class);
1058 spa->spa_normal_class = NULL;
1060 metaslab_class_destroy(spa->spa_log_class);
1061 spa->spa_log_class = NULL;
1064 * If this was part of an import or the open otherwise failed, we may
1065 * still have errors left in the queues. Empty them just in case.
1067 spa_errlog_drain(spa);
1069 avl_destroy(&spa->spa_errlist_scrub);
1070 avl_destroy(&spa->spa_errlist_last);
1072 spa->spa_state = POOL_STATE_UNINITIALIZED;
1074 mutex_enter(&spa->spa_proc_lock);
1075 if (spa->spa_proc_state != SPA_PROC_NONE) {
1076 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1077 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1078 cv_broadcast(&spa->spa_proc_cv);
1079 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1080 ASSERT(spa->spa_proc != &p0);
1081 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1083 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1084 spa->spa_proc_state = SPA_PROC_NONE;
1086 ASSERT(spa->spa_proc == &p0);
1087 mutex_exit(&spa->spa_proc_lock);
1091 * We want to make sure spa_thread() has actually exited the ZFS
1092 * module, so that the module can't be unloaded out from underneath
1095 if (spa->spa_did != 0) {
1096 thread_join(spa->spa_did);
1099 #endif /* SPA_PROCESS */
1103 * Verify a pool configuration, and construct the vdev tree appropriately. This
1104 * will create all the necessary vdevs in the appropriate layout, with each vdev
1105 * in the CLOSED state. This will prep the pool before open/creation/import.
1106 * All vdev validation is done by the vdev_alloc() routine.
1109 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1110 uint_t id, int atype)
1116 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1119 if ((*vdp)->vdev_ops->vdev_op_leaf)
1122 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1125 if (error == ENOENT)
1131 return (SET_ERROR(EINVAL));
1134 for (int c = 0; c < children; c++) {
1136 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1144 ASSERT(*vdp != NULL);
1150 * Opposite of spa_load().
1153 spa_unload(spa_t *spa)
1157 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1162 trim_thread_destroy(spa);
1167 spa_async_suspend(spa);
1172 if (spa->spa_sync_on) {
1173 txg_sync_stop(spa->spa_dsl_pool);
1174 spa->spa_sync_on = B_FALSE;
1178 * Wait for any outstanding async I/O to complete.
1180 if (spa->spa_async_zio_root != NULL) {
1181 (void) zio_wait(spa->spa_async_zio_root);
1182 spa->spa_async_zio_root = NULL;
1185 bpobj_close(&spa->spa_deferred_bpobj);
1188 * Close the dsl pool.
1190 if (spa->spa_dsl_pool) {
1191 dsl_pool_close(spa->spa_dsl_pool);
1192 spa->spa_dsl_pool = NULL;
1193 spa->spa_meta_objset = NULL;
1198 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1201 * Drop and purge level 2 cache
1203 spa_l2cache_drop(spa);
1208 if (spa->spa_root_vdev)
1209 vdev_free(spa->spa_root_vdev);
1210 ASSERT(spa->spa_root_vdev == NULL);
1212 for (i = 0; i < spa->spa_spares.sav_count; i++)
1213 vdev_free(spa->spa_spares.sav_vdevs[i]);
1214 if (spa->spa_spares.sav_vdevs) {
1215 kmem_free(spa->spa_spares.sav_vdevs,
1216 spa->spa_spares.sav_count * sizeof (void *));
1217 spa->spa_spares.sav_vdevs = NULL;
1219 if (spa->spa_spares.sav_config) {
1220 nvlist_free(spa->spa_spares.sav_config);
1221 spa->spa_spares.sav_config = NULL;
1223 spa->spa_spares.sav_count = 0;
1225 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1226 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1227 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1229 if (spa->spa_l2cache.sav_vdevs) {
1230 kmem_free(spa->spa_l2cache.sav_vdevs,
1231 spa->spa_l2cache.sav_count * sizeof (void *));
1232 spa->spa_l2cache.sav_vdevs = NULL;
1234 if (spa->spa_l2cache.sav_config) {
1235 nvlist_free(spa->spa_l2cache.sav_config);
1236 spa->spa_l2cache.sav_config = NULL;
1238 spa->spa_l2cache.sav_count = 0;
1240 spa->spa_async_suspended = 0;
1242 if (spa->spa_comment != NULL) {
1243 spa_strfree(spa->spa_comment);
1244 spa->spa_comment = NULL;
1247 spa_config_exit(spa, SCL_ALL, FTAG);
1251 * Load (or re-load) the current list of vdevs describing the active spares for
1252 * this pool. When this is called, we have some form of basic information in
1253 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1254 * then re-generate a more complete list including status information.
1257 spa_load_spares(spa_t *spa)
1264 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1267 * First, close and free any existing spare vdevs.
1269 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1270 vd = spa->spa_spares.sav_vdevs[i];
1272 /* Undo the call to spa_activate() below */
1273 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1274 B_FALSE)) != NULL && tvd->vdev_isspare)
1275 spa_spare_remove(tvd);
1280 if (spa->spa_spares.sav_vdevs)
1281 kmem_free(spa->spa_spares.sav_vdevs,
1282 spa->spa_spares.sav_count * sizeof (void *));
1284 if (spa->spa_spares.sav_config == NULL)
1287 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1288 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1290 spa->spa_spares.sav_count = (int)nspares;
1291 spa->spa_spares.sav_vdevs = NULL;
1297 * Construct the array of vdevs, opening them to get status in the
1298 * process. For each spare, there is potentially two different vdev_t
1299 * structures associated with it: one in the list of spares (used only
1300 * for basic validation purposes) and one in the active vdev
1301 * configuration (if it's spared in). During this phase we open and
1302 * validate each vdev on the spare list. If the vdev also exists in the
1303 * active configuration, then we also mark this vdev as an active spare.
1305 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1307 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1308 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1309 VDEV_ALLOC_SPARE) == 0);
1312 spa->spa_spares.sav_vdevs[i] = vd;
1314 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1315 B_FALSE)) != NULL) {
1316 if (!tvd->vdev_isspare)
1320 * We only mark the spare active if we were successfully
1321 * able to load the vdev. Otherwise, importing a pool
1322 * with a bad active spare would result in strange
1323 * behavior, because multiple pool would think the spare
1324 * is actively in use.
1326 * There is a vulnerability here to an equally bizarre
1327 * circumstance, where a dead active spare is later
1328 * brought back to life (onlined or otherwise). Given
1329 * the rarity of this scenario, and the extra complexity
1330 * it adds, we ignore the possibility.
1332 if (!vdev_is_dead(tvd))
1333 spa_spare_activate(tvd);
1337 vd->vdev_aux = &spa->spa_spares;
1339 if (vdev_open(vd) != 0)
1342 if (vdev_validate_aux(vd) == 0)
1347 * Recompute the stashed list of spares, with status information
1350 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1351 DATA_TYPE_NVLIST_ARRAY) == 0);
1353 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1355 for (i = 0; i < spa->spa_spares.sav_count; i++)
1356 spares[i] = vdev_config_generate(spa,
1357 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1358 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1359 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1360 for (i = 0; i < spa->spa_spares.sav_count; i++)
1361 nvlist_free(spares[i]);
1362 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1366 * Load (or re-load) the current list of vdevs describing the active l2cache for
1367 * this pool. When this is called, we have some form of basic information in
1368 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1369 * then re-generate a more complete list including status information.
1370 * Devices which are already active have their details maintained, and are
1374 spa_load_l2cache(spa_t *spa)
1378 int i, j, oldnvdevs;
1380 vdev_t *vd, **oldvdevs, **newvdevs;
1381 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1383 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1385 if (sav->sav_config != NULL) {
1386 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1387 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1388 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1394 oldvdevs = sav->sav_vdevs;
1395 oldnvdevs = sav->sav_count;
1396 sav->sav_vdevs = NULL;
1400 * Process new nvlist of vdevs.
1402 for (i = 0; i < nl2cache; i++) {
1403 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1407 for (j = 0; j < oldnvdevs; j++) {
1409 if (vd != NULL && guid == vd->vdev_guid) {
1411 * Retain previous vdev for add/remove ops.
1419 if (newvdevs[i] == NULL) {
1423 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1424 VDEV_ALLOC_L2CACHE) == 0);
1429 * Commit this vdev as an l2cache device,
1430 * even if it fails to open.
1432 spa_l2cache_add(vd);
1437 spa_l2cache_activate(vd);
1439 if (vdev_open(vd) != 0)
1442 (void) vdev_validate_aux(vd);
1444 if (!vdev_is_dead(vd))
1445 l2arc_add_vdev(spa, vd);
1450 * Purge vdevs that were dropped
1452 for (i = 0; i < oldnvdevs; i++) {
1457 ASSERT(vd->vdev_isl2cache);
1459 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1460 pool != 0ULL && l2arc_vdev_present(vd))
1461 l2arc_remove_vdev(vd);
1462 vdev_clear_stats(vd);
1468 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1470 if (sav->sav_config == NULL)
1473 sav->sav_vdevs = newvdevs;
1474 sav->sav_count = (int)nl2cache;
1477 * Recompute the stashed list of l2cache devices, with status
1478 * information this time.
1480 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1481 DATA_TYPE_NVLIST_ARRAY) == 0);
1483 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1484 for (i = 0; i < sav->sav_count; i++)
1485 l2cache[i] = vdev_config_generate(spa,
1486 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1487 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1488 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1490 for (i = 0; i < sav->sav_count; i++)
1491 nvlist_free(l2cache[i]);
1493 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1497 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1500 char *packed = NULL;
1505 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1506 nvsize = *(uint64_t *)db->db_data;
1507 dmu_buf_rele(db, FTAG);
1509 packed = kmem_alloc(nvsize, KM_SLEEP);
1510 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1513 error = nvlist_unpack(packed, nvsize, value, 0);
1514 kmem_free(packed, nvsize);
1520 * Checks to see if the given vdev could not be opened, in which case we post a
1521 * sysevent to notify the autoreplace code that the device has been removed.
1524 spa_check_removed(vdev_t *vd)
1526 for (int c = 0; c < vd->vdev_children; c++)
1527 spa_check_removed(vd->vdev_child[c]);
1529 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1531 zfs_post_autoreplace(vd->vdev_spa, vd);
1532 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1537 * Validate the current config against the MOS config
1540 spa_config_valid(spa_t *spa, nvlist_t *config)
1542 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1545 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1547 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1548 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1550 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1553 * If we're doing a normal import, then build up any additional
1554 * diagnostic information about missing devices in this config.
1555 * We'll pass this up to the user for further processing.
1557 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1558 nvlist_t **child, *nv;
1561 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1563 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1565 for (int c = 0; c < rvd->vdev_children; c++) {
1566 vdev_t *tvd = rvd->vdev_child[c];
1567 vdev_t *mtvd = mrvd->vdev_child[c];
1569 if (tvd->vdev_ops == &vdev_missing_ops &&
1570 mtvd->vdev_ops != &vdev_missing_ops &&
1572 child[idx++] = vdev_config_generate(spa, mtvd,
1577 VERIFY(nvlist_add_nvlist_array(nv,
1578 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1579 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1580 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1582 for (int i = 0; i < idx; i++)
1583 nvlist_free(child[i]);
1586 kmem_free(child, rvd->vdev_children * sizeof (char **));
1590 * Compare the root vdev tree with the information we have
1591 * from the MOS config (mrvd). Check each top-level vdev
1592 * with the corresponding MOS config top-level (mtvd).
1594 for (int c = 0; c < rvd->vdev_children; c++) {
1595 vdev_t *tvd = rvd->vdev_child[c];
1596 vdev_t *mtvd = mrvd->vdev_child[c];
1599 * Resolve any "missing" vdevs in the current configuration.
1600 * If we find that the MOS config has more accurate information
1601 * about the top-level vdev then use that vdev instead.
1603 if (tvd->vdev_ops == &vdev_missing_ops &&
1604 mtvd->vdev_ops != &vdev_missing_ops) {
1606 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1610 * Device specific actions.
1612 if (mtvd->vdev_islog) {
1613 spa_set_log_state(spa, SPA_LOG_CLEAR);
1616 * XXX - once we have 'readonly' pool
1617 * support we should be able to handle
1618 * missing data devices by transitioning
1619 * the pool to readonly.
1625 * Swap the missing vdev with the data we were
1626 * able to obtain from the MOS config.
1628 vdev_remove_child(rvd, tvd);
1629 vdev_remove_child(mrvd, mtvd);
1631 vdev_add_child(rvd, mtvd);
1632 vdev_add_child(mrvd, tvd);
1634 spa_config_exit(spa, SCL_ALL, FTAG);
1636 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1639 } else if (mtvd->vdev_islog) {
1641 * Load the slog device's state from the MOS config
1642 * since it's possible that the label does not
1643 * contain the most up-to-date information.
1645 vdev_load_log_state(tvd, mtvd);
1650 spa_config_exit(spa, SCL_ALL, FTAG);
1653 * Ensure we were able to validate the config.
1655 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1659 * Check for missing log devices
1662 spa_check_logs(spa_t *spa)
1664 boolean_t rv = B_FALSE;
1666 switch (spa->spa_log_state) {
1667 case SPA_LOG_MISSING:
1668 /* need to recheck in case slog has been restored */
1669 case SPA_LOG_UNKNOWN:
1670 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1671 NULL, DS_FIND_CHILDREN) != 0);
1673 spa_set_log_state(spa, SPA_LOG_MISSING);
1680 spa_passivate_log(spa_t *spa)
1682 vdev_t *rvd = spa->spa_root_vdev;
1683 boolean_t slog_found = B_FALSE;
1685 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1687 if (!spa_has_slogs(spa))
1690 for (int c = 0; c < rvd->vdev_children; c++) {
1691 vdev_t *tvd = rvd->vdev_child[c];
1692 metaslab_group_t *mg = tvd->vdev_mg;
1694 if (tvd->vdev_islog) {
1695 metaslab_group_passivate(mg);
1696 slog_found = B_TRUE;
1700 return (slog_found);
1704 spa_activate_log(spa_t *spa)
1706 vdev_t *rvd = spa->spa_root_vdev;
1708 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1710 for (int c = 0; c < rvd->vdev_children; c++) {
1711 vdev_t *tvd = rvd->vdev_child[c];
1712 metaslab_group_t *mg = tvd->vdev_mg;
1714 if (tvd->vdev_islog)
1715 metaslab_group_activate(mg);
1720 spa_offline_log(spa_t *spa)
1724 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1725 NULL, DS_FIND_CHILDREN);
1728 * We successfully offlined the log device, sync out the
1729 * current txg so that the "stubby" block can be removed
1732 txg_wait_synced(spa->spa_dsl_pool, 0);
1738 spa_aux_check_removed(spa_aux_vdev_t *sav)
1742 for (i = 0; i < sav->sav_count; i++)
1743 spa_check_removed(sav->sav_vdevs[i]);
1747 spa_claim_notify(zio_t *zio)
1749 spa_t *spa = zio->io_spa;
1754 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1755 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1756 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1757 mutex_exit(&spa->spa_props_lock);
1760 typedef struct spa_load_error {
1761 uint64_t sle_meta_count;
1762 uint64_t sle_data_count;
1766 spa_load_verify_done(zio_t *zio)
1768 blkptr_t *bp = zio->io_bp;
1769 spa_load_error_t *sle = zio->io_private;
1770 dmu_object_type_t type = BP_GET_TYPE(bp);
1771 int error = zio->io_error;
1774 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1775 type != DMU_OT_INTENT_LOG)
1776 atomic_add_64(&sle->sle_meta_count, 1);
1778 atomic_add_64(&sle->sle_data_count, 1);
1780 zio_data_buf_free(zio->io_data, zio->io_size);
1785 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1786 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1790 size_t size = BP_GET_PSIZE(bp);
1791 void *data = zio_data_buf_alloc(size);
1793 zio_nowait(zio_read(rio, spa, bp, data, size,
1794 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1795 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1796 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1802 spa_load_verify(spa_t *spa)
1805 spa_load_error_t sle = { 0 };
1806 zpool_rewind_policy_t policy;
1807 boolean_t verify_ok = B_FALSE;
1810 zpool_get_rewind_policy(spa->spa_config, &policy);
1812 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1815 rio = zio_root(spa, NULL, &sle,
1816 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1818 error = traverse_pool(spa, spa->spa_verify_min_txg,
1819 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1821 (void) zio_wait(rio);
1823 spa->spa_load_meta_errors = sle.sle_meta_count;
1824 spa->spa_load_data_errors = sle.sle_data_count;
1826 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1827 sle.sle_data_count <= policy.zrp_maxdata) {
1831 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1832 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1834 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1835 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1836 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1837 VERIFY(nvlist_add_int64(spa->spa_load_info,
1838 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1839 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1840 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1842 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1846 if (error != ENXIO && error != EIO)
1847 error = SET_ERROR(EIO);
1851 return (verify_ok ? 0 : EIO);
1855 * Find a value in the pool props object.
1858 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1860 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1861 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1865 * Find a value in the pool directory object.
1868 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1870 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1871 name, sizeof (uint64_t), 1, val));
1875 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1877 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1882 * Fix up config after a partly-completed split. This is done with the
1883 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1884 * pool have that entry in their config, but only the splitting one contains
1885 * a list of all the guids of the vdevs that are being split off.
1887 * This function determines what to do with that list: either rejoin
1888 * all the disks to the pool, or complete the splitting process. To attempt
1889 * the rejoin, each disk that is offlined is marked online again, and
1890 * we do a reopen() call. If the vdev label for every disk that was
1891 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1892 * then we call vdev_split() on each disk, and complete the split.
1894 * Otherwise we leave the config alone, with all the vdevs in place in
1895 * the original pool.
1898 spa_try_repair(spa_t *spa, nvlist_t *config)
1905 boolean_t attempt_reopen;
1907 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1910 /* check that the config is complete */
1911 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1912 &glist, &gcount) != 0)
1915 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1917 /* attempt to online all the vdevs & validate */
1918 attempt_reopen = B_TRUE;
1919 for (i = 0; i < gcount; i++) {
1920 if (glist[i] == 0) /* vdev is hole */
1923 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1924 if (vd[i] == NULL) {
1926 * Don't bother attempting to reopen the disks;
1927 * just do the split.
1929 attempt_reopen = B_FALSE;
1931 /* attempt to re-online it */
1932 vd[i]->vdev_offline = B_FALSE;
1936 if (attempt_reopen) {
1937 vdev_reopen(spa->spa_root_vdev);
1939 /* check each device to see what state it's in */
1940 for (extracted = 0, i = 0; i < gcount; i++) {
1941 if (vd[i] != NULL &&
1942 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1949 * If every disk has been moved to the new pool, or if we never
1950 * even attempted to look at them, then we split them off for
1953 if (!attempt_reopen || gcount == extracted) {
1954 for (i = 0; i < gcount; i++)
1957 vdev_reopen(spa->spa_root_vdev);
1960 kmem_free(vd, gcount * sizeof (vdev_t *));
1964 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1965 boolean_t mosconfig)
1967 nvlist_t *config = spa->spa_config;
1968 char *ereport = FM_EREPORT_ZFS_POOL;
1974 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1975 return (SET_ERROR(EINVAL));
1977 ASSERT(spa->spa_comment == NULL);
1978 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1979 spa->spa_comment = spa_strdup(comment);
1982 * Versioning wasn't explicitly added to the label until later, so if
1983 * it's not present treat it as the initial version.
1985 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1986 &spa->spa_ubsync.ub_version) != 0)
1987 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1989 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1990 &spa->spa_config_txg);
1992 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1993 spa_guid_exists(pool_guid, 0)) {
1994 error = SET_ERROR(EEXIST);
1996 spa->spa_config_guid = pool_guid;
1998 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2000 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2004 nvlist_free(spa->spa_load_info);
2005 spa->spa_load_info = fnvlist_alloc();
2007 gethrestime(&spa->spa_loaded_ts);
2008 error = spa_load_impl(spa, pool_guid, config, state, type,
2009 mosconfig, &ereport);
2012 spa->spa_minref = refcount_count(&spa->spa_refcount);
2014 if (error != EEXIST) {
2015 spa->spa_loaded_ts.tv_sec = 0;
2016 spa->spa_loaded_ts.tv_nsec = 0;
2018 if (error != EBADF) {
2019 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2022 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2029 * Load an existing storage pool, using the pool's builtin spa_config as a
2030 * source of configuration information.
2033 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2034 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2038 nvlist_t *nvroot = NULL;
2041 uberblock_t *ub = &spa->spa_uberblock;
2042 uint64_t children, config_cache_txg = spa->spa_config_txg;
2043 int orig_mode = spa->spa_mode;
2046 boolean_t missing_feat_write = B_FALSE;
2049 * If this is an untrusted config, access the pool in read-only mode.
2050 * This prevents things like resilvering recently removed devices.
2053 spa->spa_mode = FREAD;
2055 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2057 spa->spa_load_state = state;
2059 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2060 return (SET_ERROR(EINVAL));
2062 parse = (type == SPA_IMPORT_EXISTING ?
2063 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2066 * Create "The Godfather" zio to hold all async IOs
2068 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2069 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2072 * Parse the configuration into a vdev tree. We explicitly set the
2073 * value that will be returned by spa_version() since parsing the
2074 * configuration requires knowing the version number.
2076 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2077 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2078 spa_config_exit(spa, SCL_ALL, FTAG);
2083 ASSERT(spa->spa_root_vdev == rvd);
2085 if (type != SPA_IMPORT_ASSEMBLE) {
2086 ASSERT(spa_guid(spa) == pool_guid);
2090 * Try to open all vdevs, loading each label in the process.
2092 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2093 error = vdev_open(rvd);
2094 spa_config_exit(spa, SCL_ALL, FTAG);
2099 * We need to validate the vdev labels against the configuration that
2100 * we have in hand, which is dependent on the setting of mosconfig. If
2101 * mosconfig is true then we're validating the vdev labels based on
2102 * that config. Otherwise, we're validating against the cached config
2103 * (zpool.cache) that was read when we loaded the zfs module, and then
2104 * later we will recursively call spa_load() and validate against
2107 * If we're assembling a new pool that's been split off from an
2108 * existing pool, the labels haven't yet been updated so we skip
2109 * validation for now.
2111 if (type != SPA_IMPORT_ASSEMBLE) {
2112 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2113 error = vdev_validate(rvd, mosconfig);
2114 spa_config_exit(spa, SCL_ALL, FTAG);
2119 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2120 return (SET_ERROR(ENXIO));
2124 * Find the best uberblock.
2126 vdev_uberblock_load(rvd, ub, &label);
2129 * If we weren't able to find a single valid uberblock, return failure.
2131 if (ub->ub_txg == 0) {
2133 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2137 * If the pool has an unsupported version we can't open it.
2139 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2141 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2144 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2148 * If we weren't able to find what's necessary for reading the
2149 * MOS in the label, return failure.
2151 if (label == NULL || nvlist_lookup_nvlist(label,
2152 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2154 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2159 * Update our in-core representation with the definitive values
2162 nvlist_free(spa->spa_label_features);
2163 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2169 * Look through entries in the label nvlist's features_for_read. If
2170 * there is a feature listed there which we don't understand then we
2171 * cannot open a pool.
2173 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2174 nvlist_t *unsup_feat;
2176 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2179 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2181 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2182 if (!zfeature_is_supported(nvpair_name(nvp))) {
2183 VERIFY(nvlist_add_string(unsup_feat,
2184 nvpair_name(nvp), "") == 0);
2188 if (!nvlist_empty(unsup_feat)) {
2189 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2190 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2191 nvlist_free(unsup_feat);
2192 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2196 nvlist_free(unsup_feat);
2200 * If the vdev guid sum doesn't match the uberblock, we have an
2201 * incomplete configuration. We first check to see if the pool
2202 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2203 * If it is, defer the vdev_guid_sum check till later so we
2204 * can handle missing vdevs.
2206 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2207 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2208 rvd->vdev_guid_sum != ub->ub_guid_sum)
2209 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2211 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2212 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2213 spa_try_repair(spa, config);
2214 spa_config_exit(spa, SCL_ALL, FTAG);
2215 nvlist_free(spa->spa_config_splitting);
2216 spa->spa_config_splitting = NULL;
2220 * Initialize internal SPA structures.
2222 spa->spa_state = POOL_STATE_ACTIVE;
2223 spa->spa_ubsync = spa->spa_uberblock;
2224 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2225 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2226 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2227 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2228 spa->spa_claim_max_txg = spa->spa_first_txg;
2229 spa->spa_prev_software_version = ub->ub_software_version;
2231 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2233 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2234 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2236 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2237 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2239 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2240 boolean_t missing_feat_read = B_FALSE;
2241 nvlist_t *unsup_feat, *enabled_feat;
2243 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2244 &spa->spa_feat_for_read_obj) != 0) {
2245 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2248 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2249 &spa->spa_feat_for_write_obj) != 0) {
2250 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2253 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2254 &spa->spa_feat_desc_obj) != 0) {
2255 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2258 enabled_feat = fnvlist_alloc();
2259 unsup_feat = fnvlist_alloc();
2261 if (!feature_is_supported(spa->spa_meta_objset,
2262 spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj,
2263 unsup_feat, enabled_feat))
2264 missing_feat_read = B_TRUE;
2266 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2267 if (!feature_is_supported(spa->spa_meta_objset,
2268 spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj,
2269 unsup_feat, enabled_feat)) {
2270 missing_feat_write = B_TRUE;
2274 fnvlist_add_nvlist(spa->spa_load_info,
2275 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2277 if (!nvlist_empty(unsup_feat)) {
2278 fnvlist_add_nvlist(spa->spa_load_info,
2279 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2282 fnvlist_free(enabled_feat);
2283 fnvlist_free(unsup_feat);
2285 if (!missing_feat_read) {
2286 fnvlist_add_boolean(spa->spa_load_info,
2287 ZPOOL_CONFIG_CAN_RDONLY);
2291 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2292 * twofold: to determine whether the pool is available for
2293 * import in read-write mode and (if it is not) whether the
2294 * pool is available for import in read-only mode. If the pool
2295 * is available for import in read-write mode, it is displayed
2296 * as available in userland; if it is not available for import
2297 * in read-only mode, it is displayed as unavailable in
2298 * userland. If the pool is available for import in read-only
2299 * mode but not read-write mode, it is displayed as unavailable
2300 * in userland with a special note that the pool is actually
2301 * available for open in read-only mode.
2303 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2304 * missing a feature for write, we must first determine whether
2305 * the pool can be opened read-only before returning to
2306 * userland in order to know whether to display the
2307 * abovementioned note.
2309 if (missing_feat_read || (missing_feat_write &&
2310 spa_writeable(spa))) {
2311 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2316 spa->spa_is_initializing = B_TRUE;
2317 error = dsl_pool_open(spa->spa_dsl_pool);
2318 spa->spa_is_initializing = B_FALSE;
2320 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2324 nvlist_t *policy = NULL, *nvconfig;
2326 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2327 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2329 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2330 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2332 unsigned long myhostid = 0;
2334 VERIFY(nvlist_lookup_string(nvconfig,
2335 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2338 myhostid = zone_get_hostid(NULL);
2341 * We're emulating the system's hostid in userland, so
2342 * we can't use zone_get_hostid().
2344 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2345 #endif /* _KERNEL */
2346 if (check_hostid && hostid != 0 && myhostid != 0 &&
2347 hostid != myhostid) {
2348 nvlist_free(nvconfig);
2349 cmn_err(CE_WARN, "pool '%s' could not be "
2350 "loaded as it was last accessed by "
2351 "another system (host: %s hostid: 0x%lx). "
2352 "See: http://illumos.org/msg/ZFS-8000-EY",
2353 spa_name(spa), hostname,
2354 (unsigned long)hostid);
2355 return (SET_ERROR(EBADF));
2358 if (nvlist_lookup_nvlist(spa->spa_config,
2359 ZPOOL_REWIND_POLICY, &policy) == 0)
2360 VERIFY(nvlist_add_nvlist(nvconfig,
2361 ZPOOL_REWIND_POLICY, policy) == 0);
2363 spa_config_set(spa, nvconfig);
2365 spa_deactivate(spa);
2366 spa_activate(spa, orig_mode);
2368 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2371 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2372 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2373 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2375 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2378 * Load the bit that tells us to use the new accounting function
2379 * (raid-z deflation). If we have an older pool, this will not
2382 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2383 if (error != 0 && error != ENOENT)
2384 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2386 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2387 &spa->spa_creation_version);
2388 if (error != 0 && error != ENOENT)
2389 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2392 * Load the persistent error log. If we have an older pool, this will
2395 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2396 if (error != 0 && error != ENOENT)
2397 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2399 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2400 &spa->spa_errlog_scrub);
2401 if (error != 0 && error != ENOENT)
2402 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2405 * Load the history object. If we have an older pool, this
2406 * will not be present.
2408 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2409 if (error != 0 && error != ENOENT)
2410 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2413 * If we're assembling the pool from the split-off vdevs of
2414 * an existing pool, we don't want to attach the spares & cache
2419 * Load any hot spares for this pool.
2421 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2422 if (error != 0 && error != ENOENT)
2423 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2424 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2425 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2426 if (load_nvlist(spa, spa->spa_spares.sav_object,
2427 &spa->spa_spares.sav_config) != 0)
2428 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2430 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2431 spa_load_spares(spa);
2432 spa_config_exit(spa, SCL_ALL, FTAG);
2433 } else if (error == 0) {
2434 spa->spa_spares.sav_sync = B_TRUE;
2438 * Load any level 2 ARC devices for this pool.
2440 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2441 &spa->spa_l2cache.sav_object);
2442 if (error != 0 && error != ENOENT)
2443 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2444 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2445 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2446 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2447 &spa->spa_l2cache.sav_config) != 0)
2448 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2450 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2451 spa_load_l2cache(spa);
2452 spa_config_exit(spa, SCL_ALL, FTAG);
2453 } else if (error == 0) {
2454 spa->spa_l2cache.sav_sync = B_TRUE;
2457 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2459 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2460 if (error && error != ENOENT)
2461 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2464 uint64_t autoreplace;
2466 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2467 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2468 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2469 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2470 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2471 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2472 &spa->spa_dedup_ditto);
2474 spa->spa_autoreplace = (autoreplace != 0);
2478 * If the 'autoreplace' property is set, then post a resource notifying
2479 * the ZFS DE that it should not issue any faults for unopenable
2480 * devices. We also iterate over the vdevs, and post a sysevent for any
2481 * unopenable vdevs so that the normal autoreplace handler can take
2484 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2485 spa_check_removed(spa->spa_root_vdev);
2487 * For the import case, this is done in spa_import(), because
2488 * at this point we're using the spare definitions from
2489 * the MOS config, not necessarily from the userland config.
2491 if (state != SPA_LOAD_IMPORT) {
2492 spa_aux_check_removed(&spa->spa_spares);
2493 spa_aux_check_removed(&spa->spa_l2cache);
2498 * Load the vdev state for all toplevel vdevs.
2503 * Propagate the leaf DTLs we just loaded all the way up the tree.
2505 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2506 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2507 spa_config_exit(spa, SCL_ALL, FTAG);
2510 * Load the DDTs (dedup tables).
2512 error = ddt_load(spa);
2514 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2516 spa_update_dspace(spa);
2519 * Validate the config, using the MOS config to fill in any
2520 * information which might be missing. If we fail to validate
2521 * the config then declare the pool unfit for use. If we're
2522 * assembling a pool from a split, the log is not transferred
2525 if (type != SPA_IMPORT_ASSEMBLE) {
2528 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2529 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2531 if (!spa_config_valid(spa, nvconfig)) {
2532 nvlist_free(nvconfig);
2533 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2536 nvlist_free(nvconfig);
2539 * Now that we've validated the config, check the state of the
2540 * root vdev. If it can't be opened, it indicates one or
2541 * more toplevel vdevs are faulted.
2543 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2544 return (SET_ERROR(ENXIO));
2546 if (spa_check_logs(spa)) {
2547 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2548 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2552 if (missing_feat_write) {
2553 ASSERT(state == SPA_LOAD_TRYIMPORT);
2556 * At this point, we know that we can open the pool in
2557 * read-only mode but not read-write mode. We now have enough
2558 * information and can return to userland.
2560 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2564 * We've successfully opened the pool, verify that we're ready
2565 * to start pushing transactions.
2567 if (state != SPA_LOAD_TRYIMPORT) {
2568 if (error = spa_load_verify(spa))
2569 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2573 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2574 spa->spa_load_max_txg == UINT64_MAX)) {
2576 int need_update = B_FALSE;
2578 ASSERT(state != SPA_LOAD_TRYIMPORT);
2581 * Claim log blocks that haven't been committed yet.
2582 * This must all happen in a single txg.
2583 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2584 * invoked from zil_claim_log_block()'s i/o done callback.
2585 * Price of rollback is that we abandon the log.
2587 spa->spa_claiming = B_TRUE;
2589 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2590 spa_first_txg(spa));
2591 (void) dmu_objset_find(spa_name(spa),
2592 zil_claim, tx, DS_FIND_CHILDREN);
2595 spa->spa_claiming = B_FALSE;
2597 spa_set_log_state(spa, SPA_LOG_GOOD);
2598 spa->spa_sync_on = B_TRUE;
2599 txg_sync_start(spa->spa_dsl_pool);
2602 * Wait for all claims to sync. We sync up to the highest
2603 * claimed log block birth time so that claimed log blocks
2604 * don't appear to be from the future. spa_claim_max_txg
2605 * will have been set for us by either zil_check_log_chain()
2606 * (invoked from spa_check_logs()) or zil_claim() above.
2608 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2611 * If the config cache is stale, or we have uninitialized
2612 * metaslabs (see spa_vdev_add()), then update the config.
2614 * If this is a verbatim import, trust the current
2615 * in-core spa_config and update the disk labels.
2617 if (config_cache_txg != spa->spa_config_txg ||
2618 state == SPA_LOAD_IMPORT ||
2619 state == SPA_LOAD_RECOVER ||
2620 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2621 need_update = B_TRUE;
2623 for (int c = 0; c < rvd->vdev_children; c++)
2624 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2625 need_update = B_TRUE;
2628 * Update the config cache asychronously in case we're the
2629 * root pool, in which case the config cache isn't writable yet.
2632 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2635 * Check all DTLs to see if anything needs resilvering.
2637 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2638 vdev_resilver_needed(rvd, NULL, NULL))
2639 spa_async_request(spa, SPA_ASYNC_RESILVER);
2642 * Log the fact that we booted up (so that we can detect if
2643 * we rebooted in the middle of an operation).
2645 spa_history_log_version(spa, "open");
2648 * Delete any inconsistent datasets.
2650 (void) dmu_objset_find(spa_name(spa),
2651 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2654 * Clean up any stale temporary dataset userrefs.
2656 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2663 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2665 int mode = spa->spa_mode;
2668 spa_deactivate(spa);
2670 spa->spa_load_max_txg--;
2672 spa_activate(spa, mode);
2673 spa_async_suspend(spa);
2675 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2679 * If spa_load() fails this function will try loading prior txg's. If
2680 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2681 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2682 * function will not rewind the pool and will return the same error as
2686 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2687 uint64_t max_request, int rewind_flags)
2689 nvlist_t *loadinfo = NULL;
2690 nvlist_t *config = NULL;
2691 int load_error, rewind_error;
2692 uint64_t safe_rewind_txg;
2695 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2696 spa->spa_load_max_txg = spa->spa_load_txg;
2697 spa_set_log_state(spa, SPA_LOG_CLEAR);
2699 spa->spa_load_max_txg = max_request;
2702 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2704 if (load_error == 0)
2707 if (spa->spa_root_vdev != NULL)
2708 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2710 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2711 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2713 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2714 nvlist_free(config);
2715 return (load_error);
2718 if (state == SPA_LOAD_RECOVER) {
2719 /* Price of rolling back is discarding txgs, including log */
2720 spa_set_log_state(spa, SPA_LOG_CLEAR);
2723 * If we aren't rolling back save the load info from our first
2724 * import attempt so that we can restore it after attempting
2727 loadinfo = spa->spa_load_info;
2728 spa->spa_load_info = fnvlist_alloc();
2731 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2732 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2733 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2734 TXG_INITIAL : safe_rewind_txg;
2737 * Continue as long as we're finding errors, we're still within
2738 * the acceptable rewind range, and we're still finding uberblocks
2740 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2741 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2742 if (spa->spa_load_max_txg < safe_rewind_txg)
2743 spa->spa_extreme_rewind = B_TRUE;
2744 rewind_error = spa_load_retry(spa, state, mosconfig);
2747 spa->spa_extreme_rewind = B_FALSE;
2748 spa->spa_load_max_txg = UINT64_MAX;
2750 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2751 spa_config_set(spa, config);
2753 if (state == SPA_LOAD_RECOVER) {
2754 ASSERT3P(loadinfo, ==, NULL);
2755 return (rewind_error);
2757 /* Store the rewind info as part of the initial load info */
2758 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2759 spa->spa_load_info);
2761 /* Restore the initial load info */
2762 fnvlist_free(spa->spa_load_info);
2763 spa->spa_load_info = loadinfo;
2765 return (load_error);
2772 * The import case is identical to an open except that the configuration is sent
2773 * down from userland, instead of grabbed from the configuration cache. For the
2774 * case of an open, the pool configuration will exist in the
2775 * POOL_STATE_UNINITIALIZED state.
2777 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2778 * the same time open the pool, without having to keep around the spa_t in some
2782 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2786 spa_load_state_t state = SPA_LOAD_OPEN;
2788 int locked = B_FALSE;
2789 int firstopen = B_FALSE;
2794 * As disgusting as this is, we need to support recursive calls to this
2795 * function because dsl_dir_open() is called during spa_load(), and ends
2796 * up calling spa_open() again. The real fix is to figure out how to
2797 * avoid dsl_dir_open() calling this in the first place.
2799 if (mutex_owner(&spa_namespace_lock) != curthread) {
2800 mutex_enter(&spa_namespace_lock);
2804 if ((spa = spa_lookup(pool)) == NULL) {
2806 mutex_exit(&spa_namespace_lock);
2807 return (SET_ERROR(ENOENT));
2810 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2811 zpool_rewind_policy_t policy;
2815 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2817 if (policy.zrp_request & ZPOOL_DO_REWIND)
2818 state = SPA_LOAD_RECOVER;
2820 spa_activate(spa, spa_mode_global);
2822 if (state != SPA_LOAD_RECOVER)
2823 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2825 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2826 policy.zrp_request);
2828 if (error == EBADF) {
2830 * If vdev_validate() returns failure (indicated by
2831 * EBADF), it indicates that one of the vdevs indicates
2832 * that the pool has been exported or destroyed. If
2833 * this is the case, the config cache is out of sync and
2834 * we should remove the pool from the namespace.
2837 spa_deactivate(spa);
2838 spa_config_sync(spa, B_TRUE, B_TRUE);
2841 mutex_exit(&spa_namespace_lock);
2842 return (SET_ERROR(ENOENT));
2847 * We can't open the pool, but we still have useful
2848 * information: the state of each vdev after the
2849 * attempted vdev_open(). Return this to the user.
2851 if (config != NULL && spa->spa_config) {
2852 VERIFY(nvlist_dup(spa->spa_config, config,
2854 VERIFY(nvlist_add_nvlist(*config,
2855 ZPOOL_CONFIG_LOAD_INFO,
2856 spa->spa_load_info) == 0);
2859 spa_deactivate(spa);
2860 spa->spa_last_open_failed = error;
2862 mutex_exit(&spa_namespace_lock);
2868 spa_open_ref(spa, tag);
2871 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2874 * If we've recovered the pool, pass back any information we
2875 * gathered while doing the load.
2877 if (state == SPA_LOAD_RECOVER) {
2878 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2879 spa->spa_load_info) == 0);
2883 spa->spa_last_open_failed = 0;
2884 spa->spa_last_ubsync_txg = 0;
2885 spa->spa_load_txg = 0;
2886 mutex_exit(&spa_namespace_lock);
2890 zvol_create_minors(spa->spa_name);
2901 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2904 return (spa_open_common(name, spapp, tag, policy, config));
2908 spa_open(const char *name, spa_t **spapp, void *tag)
2910 return (spa_open_common(name, spapp, tag, NULL, NULL));
2914 * Lookup the given spa_t, incrementing the inject count in the process,
2915 * preventing it from being exported or destroyed.
2918 spa_inject_addref(char *name)
2922 mutex_enter(&spa_namespace_lock);
2923 if ((spa = spa_lookup(name)) == NULL) {
2924 mutex_exit(&spa_namespace_lock);
2927 spa->spa_inject_ref++;
2928 mutex_exit(&spa_namespace_lock);
2934 spa_inject_delref(spa_t *spa)
2936 mutex_enter(&spa_namespace_lock);
2937 spa->spa_inject_ref--;
2938 mutex_exit(&spa_namespace_lock);
2942 * Add spares device information to the nvlist.
2945 spa_add_spares(spa_t *spa, nvlist_t *config)
2955 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2957 if (spa->spa_spares.sav_count == 0)
2960 VERIFY(nvlist_lookup_nvlist(config,
2961 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2962 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2963 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2965 VERIFY(nvlist_add_nvlist_array(nvroot,
2966 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2967 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2968 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2971 * Go through and find any spares which have since been
2972 * repurposed as an active spare. If this is the case, update
2973 * their status appropriately.
2975 for (i = 0; i < nspares; i++) {
2976 VERIFY(nvlist_lookup_uint64(spares[i],
2977 ZPOOL_CONFIG_GUID, &guid) == 0);
2978 if (spa_spare_exists(guid, &pool, NULL) &&
2980 VERIFY(nvlist_lookup_uint64_array(
2981 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2982 (uint64_t **)&vs, &vsc) == 0);
2983 vs->vs_state = VDEV_STATE_CANT_OPEN;
2984 vs->vs_aux = VDEV_AUX_SPARED;
2991 * Add l2cache device information to the nvlist, including vdev stats.
2994 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2997 uint_t i, j, nl2cache;
3004 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3006 if (spa->spa_l2cache.sav_count == 0)
3009 VERIFY(nvlist_lookup_nvlist(config,
3010 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3011 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3012 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3013 if (nl2cache != 0) {
3014 VERIFY(nvlist_add_nvlist_array(nvroot,
3015 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3016 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3017 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3020 * Update level 2 cache device stats.
3023 for (i = 0; i < nl2cache; i++) {
3024 VERIFY(nvlist_lookup_uint64(l2cache[i],
3025 ZPOOL_CONFIG_GUID, &guid) == 0);
3028 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3030 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3031 vd = spa->spa_l2cache.sav_vdevs[j];
3037 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3038 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3040 vdev_get_stats(vd, vs);
3046 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3052 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3053 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3055 if (spa->spa_feat_for_read_obj != 0) {
3056 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3057 spa->spa_feat_for_read_obj);
3058 zap_cursor_retrieve(&zc, &za) == 0;
3059 zap_cursor_advance(&zc)) {
3060 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3061 za.za_num_integers == 1);
3062 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3063 za.za_first_integer));
3065 zap_cursor_fini(&zc);
3068 if (spa->spa_feat_for_write_obj != 0) {
3069 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3070 spa->spa_feat_for_write_obj);
3071 zap_cursor_retrieve(&zc, &za) == 0;
3072 zap_cursor_advance(&zc)) {
3073 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3074 za.za_num_integers == 1);
3075 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3076 za.za_first_integer));
3078 zap_cursor_fini(&zc);
3081 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3083 nvlist_free(features);
3087 spa_get_stats(const char *name, nvlist_t **config,
3088 char *altroot, size_t buflen)
3094 error = spa_open_common(name, &spa, FTAG, NULL, config);
3098 * This still leaves a window of inconsistency where the spares
3099 * or l2cache devices could change and the config would be
3100 * self-inconsistent.
3102 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3104 if (*config != NULL) {
3105 uint64_t loadtimes[2];
3107 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3108 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3109 VERIFY(nvlist_add_uint64_array(*config,
3110 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3112 VERIFY(nvlist_add_uint64(*config,
3113 ZPOOL_CONFIG_ERRCOUNT,
3114 spa_get_errlog_size(spa)) == 0);
3116 if (spa_suspended(spa))
3117 VERIFY(nvlist_add_uint64(*config,
3118 ZPOOL_CONFIG_SUSPENDED,
3119 spa->spa_failmode) == 0);
3121 spa_add_spares(spa, *config);
3122 spa_add_l2cache(spa, *config);
3123 spa_add_feature_stats(spa, *config);
3128 * We want to get the alternate root even for faulted pools, so we cheat
3129 * and call spa_lookup() directly.
3133 mutex_enter(&spa_namespace_lock);
3134 spa = spa_lookup(name);
3136 spa_altroot(spa, altroot, buflen);
3140 mutex_exit(&spa_namespace_lock);
3142 spa_altroot(spa, altroot, buflen);
3147 spa_config_exit(spa, SCL_CONFIG, FTAG);
3148 spa_close(spa, FTAG);
3155 * Validate that the auxiliary device array is well formed. We must have an
3156 * array of nvlists, each which describes a valid leaf vdev. If this is an
3157 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3158 * specified, as long as they are well-formed.
3161 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3162 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3163 vdev_labeltype_t label)
3170 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3173 * It's acceptable to have no devs specified.
3175 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3179 return (SET_ERROR(EINVAL));
3182 * Make sure the pool is formatted with a version that supports this
3185 if (spa_version(spa) < version)
3186 return (SET_ERROR(ENOTSUP));
3189 * Set the pending device list so we correctly handle device in-use
3192 sav->sav_pending = dev;
3193 sav->sav_npending = ndev;
3195 for (i = 0; i < ndev; i++) {
3196 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3200 if (!vd->vdev_ops->vdev_op_leaf) {
3202 error = SET_ERROR(EINVAL);
3207 * The L2ARC currently only supports disk devices in
3208 * kernel context. For user-level testing, we allow it.
3211 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3212 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3213 error = SET_ERROR(ENOTBLK);
3220 if ((error = vdev_open(vd)) == 0 &&
3221 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3222 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3223 vd->vdev_guid) == 0);
3229 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3236 sav->sav_pending = NULL;
3237 sav->sav_npending = 0;
3242 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3246 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3248 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3249 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3250 VDEV_LABEL_SPARE)) != 0) {
3254 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3255 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3256 VDEV_LABEL_L2CACHE));
3260 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3265 if (sav->sav_config != NULL) {
3271 * Generate new dev list by concatentating with the
3274 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3275 &olddevs, &oldndevs) == 0);
3277 newdevs = kmem_alloc(sizeof (void *) *
3278 (ndevs + oldndevs), KM_SLEEP);
3279 for (i = 0; i < oldndevs; i++)
3280 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3282 for (i = 0; i < ndevs; i++)
3283 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3286 VERIFY(nvlist_remove(sav->sav_config, config,
3287 DATA_TYPE_NVLIST_ARRAY) == 0);
3289 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3290 config, newdevs, ndevs + oldndevs) == 0);
3291 for (i = 0; i < oldndevs + ndevs; i++)
3292 nvlist_free(newdevs[i]);
3293 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3296 * Generate a new dev list.
3298 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3300 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3306 * Stop and drop level 2 ARC devices
3309 spa_l2cache_drop(spa_t *spa)
3313 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3315 for (i = 0; i < sav->sav_count; i++) {
3318 vd = sav->sav_vdevs[i];
3321 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3322 pool != 0ULL && l2arc_vdev_present(vd))
3323 l2arc_remove_vdev(vd);
3331 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3335 char *altroot = NULL;
3340 uint64_t txg = TXG_INITIAL;
3341 nvlist_t **spares, **l2cache;
3342 uint_t nspares, nl2cache;
3343 uint64_t version, obj;
3344 boolean_t has_features;
3347 * If this pool already exists, return failure.
3349 mutex_enter(&spa_namespace_lock);
3350 if (spa_lookup(pool) != NULL) {
3351 mutex_exit(&spa_namespace_lock);
3352 return (SET_ERROR(EEXIST));
3356 * Allocate a new spa_t structure.
3358 (void) nvlist_lookup_string(props,
3359 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3360 spa = spa_add(pool, NULL, altroot);
3361 spa_activate(spa, spa_mode_global);
3363 if (props && (error = spa_prop_validate(spa, props))) {
3364 spa_deactivate(spa);
3366 mutex_exit(&spa_namespace_lock);
3370 has_features = B_FALSE;
3371 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3372 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3373 if (zpool_prop_feature(nvpair_name(elem)))
3374 has_features = B_TRUE;
3377 if (has_features || nvlist_lookup_uint64(props,
3378 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3379 version = SPA_VERSION;
3381 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3383 spa->spa_first_txg = txg;
3384 spa->spa_uberblock.ub_txg = txg - 1;
3385 spa->spa_uberblock.ub_version = version;
3386 spa->spa_ubsync = spa->spa_uberblock;
3389 * Create "The Godfather" zio to hold all async IOs
3391 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3392 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3395 * Create the root vdev.
3397 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3399 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3401 ASSERT(error != 0 || rvd != NULL);
3402 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3404 if (error == 0 && !zfs_allocatable_devs(nvroot))
3405 error = SET_ERROR(EINVAL);
3408 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3409 (error = spa_validate_aux(spa, nvroot, txg,
3410 VDEV_ALLOC_ADD)) == 0) {
3411 for (int c = 0; c < rvd->vdev_children; c++) {
3412 vdev_metaslab_set_size(rvd->vdev_child[c]);
3413 vdev_expand(rvd->vdev_child[c], txg);
3417 spa_config_exit(spa, SCL_ALL, FTAG);
3421 spa_deactivate(spa);
3423 mutex_exit(&spa_namespace_lock);
3428 * Get the list of spares, if specified.
3430 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3431 &spares, &nspares) == 0) {
3432 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3434 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3435 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3436 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3437 spa_load_spares(spa);
3438 spa_config_exit(spa, SCL_ALL, FTAG);
3439 spa->spa_spares.sav_sync = B_TRUE;
3443 * Get the list of level 2 cache devices, if specified.
3445 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3446 &l2cache, &nl2cache) == 0) {
3447 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3448 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3449 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3450 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3451 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3452 spa_load_l2cache(spa);
3453 spa_config_exit(spa, SCL_ALL, FTAG);
3454 spa->spa_l2cache.sav_sync = B_TRUE;
3457 spa->spa_is_initializing = B_TRUE;
3458 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3459 spa->spa_meta_objset = dp->dp_meta_objset;
3460 spa->spa_is_initializing = B_FALSE;
3463 * Create DDTs (dedup tables).
3467 spa_update_dspace(spa);
3469 tx = dmu_tx_create_assigned(dp, txg);
3472 * Create the pool config object.
3474 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3475 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3476 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3478 if (zap_add(spa->spa_meta_objset,
3479 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3480 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3481 cmn_err(CE_PANIC, "failed to add pool config");
3484 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3485 spa_feature_create_zap_objects(spa, tx);
3487 if (zap_add(spa->spa_meta_objset,
3488 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3489 sizeof (uint64_t), 1, &version, tx) != 0) {
3490 cmn_err(CE_PANIC, "failed to add pool version");
3493 /* Newly created pools with the right version are always deflated. */
3494 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3495 spa->spa_deflate = TRUE;
3496 if (zap_add(spa->spa_meta_objset,
3497 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3498 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3499 cmn_err(CE_PANIC, "failed to add deflate");
3504 * Create the deferred-free bpobj. Turn off compression
3505 * because sync-to-convergence takes longer if the blocksize
3508 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3509 dmu_object_set_compress(spa->spa_meta_objset, obj,
3510 ZIO_COMPRESS_OFF, tx);
3511 if (zap_add(spa->spa_meta_objset,
3512 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3513 sizeof (uint64_t), 1, &obj, tx) != 0) {
3514 cmn_err(CE_PANIC, "failed to add bpobj");
3516 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3517 spa->spa_meta_objset, obj));
3520 * Create the pool's history object.
3522 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3523 spa_history_create_obj(spa, tx);
3526 * Set pool properties.
3528 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3529 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3530 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3531 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3533 if (props != NULL) {
3534 spa_configfile_set(spa, props, B_FALSE);
3535 spa_sync_props(props, tx);
3540 spa->spa_sync_on = B_TRUE;
3541 txg_sync_start(spa->spa_dsl_pool);
3544 * We explicitly wait for the first transaction to complete so that our
3545 * bean counters are appropriately updated.
3547 txg_wait_synced(spa->spa_dsl_pool, txg);
3549 spa_config_sync(spa, B_FALSE, B_TRUE);
3551 spa_history_log_version(spa, "create");
3553 spa->spa_minref = refcount_count(&spa->spa_refcount);
3555 mutex_exit(&spa_namespace_lock);
3563 * Get the root pool information from the root disk, then import the root pool
3564 * during the system boot up time.
3566 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3569 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3572 nvlist_t *nvtop, *nvroot;
3575 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3579 * Add this top-level vdev to the child array.
3581 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3583 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3585 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3588 * Put this pool's top-level vdevs into a root vdev.
3590 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3591 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3592 VDEV_TYPE_ROOT) == 0);
3593 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3594 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3595 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3599 * Replace the existing vdev_tree with the new root vdev in
3600 * this pool's configuration (remove the old, add the new).
3602 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3603 nvlist_free(nvroot);
3608 * Walk the vdev tree and see if we can find a device with "better"
3609 * configuration. A configuration is "better" if the label on that
3610 * device has a more recent txg.
3613 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3615 for (int c = 0; c < vd->vdev_children; c++)
3616 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3618 if (vd->vdev_ops->vdev_op_leaf) {
3622 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3626 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3630 * Do we have a better boot device?
3632 if (label_txg > *txg) {
3641 * Import a root pool.
3643 * For x86. devpath_list will consist of devid and/or physpath name of
3644 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3645 * The GRUB "findroot" command will return the vdev we should boot.
3647 * For Sparc, devpath_list consists the physpath name of the booting device
3648 * no matter the rootpool is a single device pool or a mirrored pool.
3650 * "/pci@1f,0/ide@d/disk@0,0:a"
3653 spa_import_rootpool(char *devpath, char *devid)
3656 vdev_t *rvd, *bvd, *avd = NULL;
3657 nvlist_t *config, *nvtop;
3663 * Read the label from the boot device and generate a configuration.
3665 config = spa_generate_rootconf(devpath, devid, &guid);
3666 #if defined(_OBP) && defined(_KERNEL)
3667 if (config == NULL) {
3668 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3670 get_iscsi_bootpath_phy(devpath);
3671 config = spa_generate_rootconf(devpath, devid, &guid);
3675 if (config == NULL) {
3676 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3678 return (SET_ERROR(EIO));
3681 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3683 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3685 mutex_enter(&spa_namespace_lock);
3686 if ((spa = spa_lookup(pname)) != NULL) {
3688 * Remove the existing root pool from the namespace so that we
3689 * can replace it with the correct config we just read in.
3694 spa = spa_add(pname, config, NULL);
3695 spa->spa_is_root = B_TRUE;
3696 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3699 * Build up a vdev tree based on the boot device's label config.
3701 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3703 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3704 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3705 VDEV_ALLOC_ROOTPOOL);
3706 spa_config_exit(spa, SCL_ALL, FTAG);
3708 mutex_exit(&spa_namespace_lock);
3709 nvlist_free(config);
3710 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3716 * Get the boot vdev.
3718 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3719 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3720 (u_longlong_t)guid);
3721 error = SET_ERROR(ENOENT);
3726 * Determine if there is a better boot device.
3729 spa_alt_rootvdev(rvd, &avd, &txg);
3731 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3732 "try booting from '%s'", avd->vdev_path);
3733 error = SET_ERROR(EINVAL);
3738 * If the boot device is part of a spare vdev then ensure that
3739 * we're booting off the active spare.
3741 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3742 !bvd->vdev_isspare) {
3743 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3744 "try booting from '%s'",
3746 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3747 error = SET_ERROR(EINVAL);
3753 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3755 spa_config_exit(spa, SCL_ALL, FTAG);
3756 mutex_exit(&spa_namespace_lock);
3758 nvlist_free(config);
3764 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3768 spa_generate_rootconf(const char *name)
3770 nvlist_t **configs, **tops;
3772 nvlist_t *best_cfg, *nvtop, *nvroot;
3781 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3784 ASSERT3U(count, !=, 0);
3786 for (i = 0; i < count; i++) {
3789 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
3791 if (txg > best_txg) {
3793 best_cfg = configs[i];
3798 * Multi-vdev root pool configuration discovery is not supported yet.
3801 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3803 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
3806 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
3807 for (i = 0; i < nchildren; i++) {
3810 if (configs[i] == NULL)
3812 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
3814 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
3816 for (i = 0; holes != NULL && i < nholes; i++) {
3819 if (tops[holes[i]] != NULL)
3821 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
3822 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
3823 VDEV_TYPE_HOLE) == 0);
3824 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
3826 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
3829 for (i = 0; i < nchildren; i++) {
3830 if (tops[i] != NULL)
3832 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
3833 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
3834 VDEV_TYPE_MISSING) == 0);
3835 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
3837 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
3842 * Create pool config based on the best vdev config.
3844 nvlist_dup(best_cfg, &config, KM_SLEEP);
3847 * Put this pool's top-level vdevs into a root vdev.
3849 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3851 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3852 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3853 VDEV_TYPE_ROOT) == 0);
3854 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3855 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3856 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3857 tops, nchildren) == 0);
3860 * Replace the existing vdev_tree with the new root vdev in
3861 * this pool's configuration (remove the old, add the new).
3863 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3866 * Drop vdev config elements that should not be present at pool level.
3868 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
3869 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
3871 for (i = 0; i < count; i++)
3872 nvlist_free(configs[i]);
3873 kmem_free(configs, count * sizeof(void *));
3874 for (i = 0; i < nchildren; i++)
3875 nvlist_free(tops[i]);
3876 kmem_free(tops, nchildren * sizeof(void *));
3877 nvlist_free(nvroot);
3882 spa_import_rootpool(const char *name)
3885 vdev_t *rvd, *bvd, *avd = NULL;
3886 nvlist_t *config, *nvtop;
3892 * Read the label from the boot device and generate a configuration.
3894 config = spa_generate_rootconf(name);
3896 mutex_enter(&spa_namespace_lock);
3897 if (config != NULL) {
3898 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3899 &pname) == 0 && strcmp(name, pname) == 0);
3900 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
3903 if ((spa = spa_lookup(pname)) != NULL) {
3905 * Remove the existing root pool from the namespace so
3906 * that we can replace it with the correct config
3911 spa = spa_add(pname, config, NULL);
3914 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
3915 * via spa_version().
3917 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3918 &spa->spa_ubsync.ub_version) != 0)
3919 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
3920 } else if ((spa = spa_lookup(name)) == NULL) {
3921 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
3925 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
3927 spa->spa_is_root = B_TRUE;
3928 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3931 * Build up a vdev tree based on the boot device's label config.
3933 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3935 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3936 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3937 VDEV_ALLOC_ROOTPOOL);
3938 spa_config_exit(spa, SCL_ALL, FTAG);
3940 mutex_exit(&spa_namespace_lock);
3941 nvlist_free(config);
3942 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3947 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3949 spa_config_exit(spa, SCL_ALL, FTAG);
3950 mutex_exit(&spa_namespace_lock);
3952 nvlist_free(config);
3960 * Import a non-root pool into the system.
3963 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3966 char *altroot = NULL;
3967 spa_load_state_t state = SPA_LOAD_IMPORT;
3968 zpool_rewind_policy_t policy;
3969 uint64_t mode = spa_mode_global;
3970 uint64_t readonly = B_FALSE;
3973 nvlist_t **spares, **l2cache;
3974 uint_t nspares, nl2cache;
3977 * If a pool with this name exists, return failure.
3979 mutex_enter(&spa_namespace_lock);
3980 if (spa_lookup(pool) != NULL) {
3981 mutex_exit(&spa_namespace_lock);
3982 return (SET_ERROR(EEXIST));
3986 * Create and initialize the spa structure.
3988 (void) nvlist_lookup_string(props,
3989 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3990 (void) nvlist_lookup_uint64(props,
3991 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3994 spa = spa_add(pool, config, altroot);
3995 spa->spa_import_flags = flags;
3998 * Verbatim import - Take a pool and insert it into the namespace
3999 * as if it had been loaded at boot.
4001 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4003 spa_configfile_set(spa, props, B_FALSE);
4005 spa_config_sync(spa, B_FALSE, B_TRUE);
4007 mutex_exit(&spa_namespace_lock);
4008 spa_history_log_version(spa, "import");
4013 spa_activate(spa, mode);
4016 * Don't start async tasks until we know everything is healthy.
4018 spa_async_suspend(spa);
4020 zpool_get_rewind_policy(config, &policy);
4021 if (policy.zrp_request & ZPOOL_DO_REWIND)
4022 state = SPA_LOAD_RECOVER;
4025 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4026 * because the user-supplied config is actually the one to trust when
4029 if (state != SPA_LOAD_RECOVER)
4030 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4032 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4033 policy.zrp_request);
4036 * Propagate anything learned while loading the pool and pass it
4037 * back to caller (i.e. rewind info, missing devices, etc).
4039 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4040 spa->spa_load_info) == 0);
4042 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4044 * Toss any existing sparelist, as it doesn't have any validity
4045 * anymore, and conflicts with spa_has_spare().
4047 if (spa->spa_spares.sav_config) {
4048 nvlist_free(spa->spa_spares.sav_config);
4049 spa->spa_spares.sav_config = NULL;
4050 spa_load_spares(spa);
4052 if (spa->spa_l2cache.sav_config) {
4053 nvlist_free(spa->spa_l2cache.sav_config);
4054 spa->spa_l2cache.sav_config = NULL;
4055 spa_load_l2cache(spa);
4058 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4061 error = spa_validate_aux(spa, nvroot, -1ULL,
4064 error = spa_validate_aux(spa, nvroot, -1ULL,
4065 VDEV_ALLOC_L2CACHE);
4066 spa_config_exit(spa, SCL_ALL, FTAG);
4069 spa_configfile_set(spa, props, B_FALSE);
4071 if (error != 0 || (props && spa_writeable(spa) &&
4072 (error = spa_prop_set(spa, props)))) {
4074 spa_deactivate(spa);
4076 mutex_exit(&spa_namespace_lock);
4080 spa_async_resume(spa);
4083 * Override any spares and level 2 cache devices as specified by
4084 * the user, as these may have correct device names/devids, etc.
4086 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4087 &spares, &nspares) == 0) {
4088 if (spa->spa_spares.sav_config)
4089 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4090 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4092 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4093 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4094 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4095 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4096 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4097 spa_load_spares(spa);
4098 spa_config_exit(spa, SCL_ALL, FTAG);
4099 spa->spa_spares.sav_sync = B_TRUE;
4101 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4102 &l2cache, &nl2cache) == 0) {
4103 if (spa->spa_l2cache.sav_config)
4104 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4105 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4107 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4108 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4109 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4110 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4111 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4112 spa_load_l2cache(spa);
4113 spa_config_exit(spa, SCL_ALL, FTAG);
4114 spa->spa_l2cache.sav_sync = B_TRUE;
4118 * Check for any removed devices.
4120 if (spa->spa_autoreplace) {
4121 spa_aux_check_removed(&spa->spa_spares);
4122 spa_aux_check_removed(&spa->spa_l2cache);
4125 if (spa_writeable(spa)) {
4127 * Update the config cache to include the newly-imported pool.
4129 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4133 * It's possible that the pool was expanded while it was exported.
4134 * We kick off an async task to handle this for us.
4136 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4138 mutex_exit(&spa_namespace_lock);
4139 spa_history_log_version(spa, "import");
4143 zvol_create_minors(pool);
4150 spa_tryimport(nvlist_t *tryconfig)
4152 nvlist_t *config = NULL;
4158 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4161 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4165 * Create and initialize the spa structure.
4167 mutex_enter(&spa_namespace_lock);
4168 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4169 spa_activate(spa, FREAD);
4172 * Pass off the heavy lifting to spa_load().
4173 * Pass TRUE for mosconfig because the user-supplied config
4174 * is actually the one to trust when doing an import.
4176 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4179 * If 'tryconfig' was at least parsable, return the current config.
4181 if (spa->spa_root_vdev != NULL) {
4182 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4183 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4185 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4187 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4188 spa->spa_uberblock.ub_timestamp) == 0);
4189 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4190 spa->spa_load_info) == 0);
4193 * If the bootfs property exists on this pool then we
4194 * copy it out so that external consumers can tell which
4195 * pools are bootable.
4197 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4198 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4201 * We have to play games with the name since the
4202 * pool was opened as TRYIMPORT_NAME.
4204 if (dsl_dsobj_to_dsname(spa_name(spa),
4205 spa->spa_bootfs, tmpname) == 0) {
4207 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4209 cp = strchr(tmpname, '/');
4211 (void) strlcpy(dsname, tmpname,
4214 (void) snprintf(dsname, MAXPATHLEN,
4215 "%s/%s", poolname, ++cp);
4217 VERIFY(nvlist_add_string(config,
4218 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4219 kmem_free(dsname, MAXPATHLEN);
4221 kmem_free(tmpname, MAXPATHLEN);
4225 * Add the list of hot spares and level 2 cache devices.
4227 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4228 spa_add_spares(spa, config);
4229 spa_add_l2cache(spa, config);
4230 spa_config_exit(spa, SCL_CONFIG, FTAG);
4234 spa_deactivate(spa);
4236 mutex_exit(&spa_namespace_lock);
4242 * Pool export/destroy
4244 * The act of destroying or exporting a pool is very simple. We make sure there
4245 * is no more pending I/O and any references to the pool are gone. Then, we
4246 * update the pool state and sync all the labels to disk, removing the
4247 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4248 * we don't sync the labels or remove the configuration cache.
4251 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4252 boolean_t force, boolean_t hardforce)
4259 if (!(spa_mode_global & FWRITE))
4260 return (SET_ERROR(EROFS));
4262 mutex_enter(&spa_namespace_lock);
4263 if ((spa = spa_lookup(pool)) == NULL) {
4264 mutex_exit(&spa_namespace_lock);
4265 return (SET_ERROR(ENOENT));
4269 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4270 * reacquire the namespace lock, and see if we can export.
4272 spa_open_ref(spa, FTAG);
4273 mutex_exit(&spa_namespace_lock);
4274 spa_async_suspend(spa);
4275 mutex_enter(&spa_namespace_lock);
4276 spa_close(spa, FTAG);
4279 * The pool will be in core if it's openable,
4280 * in which case we can modify its state.
4282 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4284 * Objsets may be open only because they're dirty, so we
4285 * have to force it to sync before checking spa_refcnt.
4287 txg_wait_synced(spa->spa_dsl_pool, 0);
4290 * A pool cannot be exported or destroyed if there are active
4291 * references. If we are resetting a pool, allow references by
4292 * fault injection handlers.
4294 if (!spa_refcount_zero(spa) ||
4295 (spa->spa_inject_ref != 0 &&
4296 new_state != POOL_STATE_UNINITIALIZED)) {
4297 spa_async_resume(spa);
4298 mutex_exit(&spa_namespace_lock);
4299 return (SET_ERROR(EBUSY));
4303 * A pool cannot be exported if it has an active shared spare.
4304 * This is to prevent other pools stealing the active spare
4305 * from an exported pool. At user's own will, such pool can
4306 * be forcedly exported.
4308 if (!force && new_state == POOL_STATE_EXPORTED &&
4309 spa_has_active_shared_spare(spa)) {
4310 spa_async_resume(spa);
4311 mutex_exit(&spa_namespace_lock);
4312 return (SET_ERROR(EXDEV));
4316 * We want this to be reflected on every label,
4317 * so mark them all dirty. spa_unload() will do the
4318 * final sync that pushes these changes out.
4320 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4321 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4322 spa->spa_state = new_state;
4323 spa->spa_final_txg = spa_last_synced_txg(spa) +
4325 vdev_config_dirty(spa->spa_root_vdev);
4326 spa_config_exit(spa, SCL_ALL, FTAG);
4330 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4332 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4334 spa_deactivate(spa);
4337 if (oldconfig && spa->spa_config)
4338 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4340 if (new_state != POOL_STATE_UNINITIALIZED) {
4342 spa_config_sync(spa, B_TRUE, B_TRUE);
4345 mutex_exit(&spa_namespace_lock);
4351 * Destroy a storage pool.
4354 spa_destroy(char *pool)
4356 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4361 * Export a storage pool.
4364 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4365 boolean_t hardforce)
4367 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4372 * Similar to spa_export(), this unloads the spa_t without actually removing it
4373 * from the namespace in any way.
4376 spa_reset(char *pool)
4378 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4383 * ==========================================================================
4384 * Device manipulation
4385 * ==========================================================================
4389 * Add a device to a storage pool.
4392 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4396 vdev_t *rvd = spa->spa_root_vdev;
4398 nvlist_t **spares, **l2cache;
4399 uint_t nspares, nl2cache;
4401 ASSERT(spa_writeable(spa));
4403 txg = spa_vdev_enter(spa);
4405 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4406 VDEV_ALLOC_ADD)) != 0)
4407 return (spa_vdev_exit(spa, NULL, txg, error));
4409 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4411 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4415 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4419 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4420 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4422 if (vd->vdev_children != 0 &&
4423 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4424 return (spa_vdev_exit(spa, vd, txg, error));
4427 * We must validate the spares and l2cache devices after checking the
4428 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4430 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4431 return (spa_vdev_exit(spa, vd, txg, error));
4434 * Transfer each new top-level vdev from vd to rvd.
4436 for (int c = 0; c < vd->vdev_children; c++) {
4439 * Set the vdev id to the first hole, if one exists.
4441 for (id = 0; id < rvd->vdev_children; id++) {
4442 if (rvd->vdev_child[id]->vdev_ishole) {
4443 vdev_free(rvd->vdev_child[id]);
4447 tvd = vd->vdev_child[c];
4448 vdev_remove_child(vd, tvd);
4450 vdev_add_child(rvd, tvd);
4451 vdev_config_dirty(tvd);
4455 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4456 ZPOOL_CONFIG_SPARES);
4457 spa_load_spares(spa);
4458 spa->spa_spares.sav_sync = B_TRUE;
4461 if (nl2cache != 0) {
4462 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4463 ZPOOL_CONFIG_L2CACHE);
4464 spa_load_l2cache(spa);
4465 spa->spa_l2cache.sav_sync = B_TRUE;
4469 * We have to be careful when adding new vdevs to an existing pool.
4470 * If other threads start allocating from these vdevs before we
4471 * sync the config cache, and we lose power, then upon reboot we may
4472 * fail to open the pool because there are DVAs that the config cache
4473 * can't translate. Therefore, we first add the vdevs without
4474 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4475 * and then let spa_config_update() initialize the new metaslabs.
4477 * spa_load() checks for added-but-not-initialized vdevs, so that
4478 * if we lose power at any point in this sequence, the remaining
4479 * steps will be completed the next time we load the pool.
4481 (void) spa_vdev_exit(spa, vd, txg, 0);
4483 mutex_enter(&spa_namespace_lock);
4484 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4485 mutex_exit(&spa_namespace_lock);
4491 * Attach a device to a mirror. The arguments are the path to any device
4492 * in the mirror, and the nvroot for the new device. If the path specifies
4493 * a device that is not mirrored, we automatically insert the mirror vdev.
4495 * If 'replacing' is specified, the new device is intended to replace the
4496 * existing device; in this case the two devices are made into their own
4497 * mirror using the 'replacing' vdev, which is functionally identical to
4498 * the mirror vdev (it actually reuses all the same ops) but has a few
4499 * extra rules: you can't attach to it after it's been created, and upon
4500 * completion of resilvering, the first disk (the one being replaced)
4501 * is automatically detached.
4504 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4506 uint64_t txg, dtl_max_txg;
4507 vdev_t *rvd = spa->spa_root_vdev;
4508 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4510 char *oldvdpath, *newvdpath;
4514 ASSERT(spa_writeable(spa));
4516 txg = spa_vdev_enter(spa);
4518 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4521 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4523 if (!oldvd->vdev_ops->vdev_op_leaf)
4524 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4526 pvd = oldvd->vdev_parent;
4528 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4529 VDEV_ALLOC_ATTACH)) != 0)
4530 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4532 if (newrootvd->vdev_children != 1)
4533 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4535 newvd = newrootvd->vdev_child[0];
4537 if (!newvd->vdev_ops->vdev_op_leaf)
4538 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4540 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4541 return (spa_vdev_exit(spa, newrootvd, txg, error));
4544 * Spares can't replace logs
4546 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4547 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4551 * For attach, the only allowable parent is a mirror or the root
4554 if (pvd->vdev_ops != &vdev_mirror_ops &&
4555 pvd->vdev_ops != &vdev_root_ops)
4556 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4558 pvops = &vdev_mirror_ops;
4561 * Active hot spares can only be replaced by inactive hot
4564 if (pvd->vdev_ops == &vdev_spare_ops &&
4565 oldvd->vdev_isspare &&
4566 !spa_has_spare(spa, newvd->vdev_guid))
4567 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4570 * If the source is a hot spare, and the parent isn't already a
4571 * spare, then we want to create a new hot spare. Otherwise, we
4572 * want to create a replacing vdev. The user is not allowed to
4573 * attach to a spared vdev child unless the 'isspare' state is
4574 * the same (spare replaces spare, non-spare replaces
4577 if (pvd->vdev_ops == &vdev_replacing_ops &&
4578 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4579 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4580 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4581 newvd->vdev_isspare != oldvd->vdev_isspare) {
4582 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4585 if (newvd->vdev_isspare)
4586 pvops = &vdev_spare_ops;
4588 pvops = &vdev_replacing_ops;
4592 * Make sure the new device is big enough.
4594 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4595 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4598 * The new device cannot have a higher alignment requirement
4599 * than the top-level vdev.
4601 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4602 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4605 * If this is an in-place replacement, update oldvd's path and devid
4606 * to make it distinguishable from newvd, and unopenable from now on.
4608 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4609 spa_strfree(oldvd->vdev_path);
4610 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4612 (void) sprintf(oldvd->vdev_path, "%s/%s",
4613 newvd->vdev_path, "old");
4614 if (oldvd->vdev_devid != NULL) {
4615 spa_strfree(oldvd->vdev_devid);
4616 oldvd->vdev_devid = NULL;
4620 /* mark the device being resilvered */
4621 newvd->vdev_resilvering = B_TRUE;
4624 * If the parent is not a mirror, or if we're replacing, insert the new
4625 * mirror/replacing/spare vdev above oldvd.
4627 if (pvd->vdev_ops != pvops)
4628 pvd = vdev_add_parent(oldvd, pvops);
4630 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4631 ASSERT(pvd->vdev_ops == pvops);
4632 ASSERT(oldvd->vdev_parent == pvd);
4635 * Extract the new device from its root and add it to pvd.
4637 vdev_remove_child(newrootvd, newvd);
4638 newvd->vdev_id = pvd->vdev_children;
4639 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4640 vdev_add_child(pvd, newvd);
4642 tvd = newvd->vdev_top;
4643 ASSERT(pvd->vdev_top == tvd);
4644 ASSERT(tvd->vdev_parent == rvd);
4646 vdev_config_dirty(tvd);
4649 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4650 * for any dmu_sync-ed blocks. It will propagate upward when
4651 * spa_vdev_exit() calls vdev_dtl_reassess().
4653 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4655 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4656 dtl_max_txg - TXG_INITIAL);
4658 if (newvd->vdev_isspare) {
4659 spa_spare_activate(newvd);
4660 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4663 oldvdpath = spa_strdup(oldvd->vdev_path);
4664 newvdpath = spa_strdup(newvd->vdev_path);
4665 newvd_isspare = newvd->vdev_isspare;
4668 * Mark newvd's DTL dirty in this txg.
4670 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4673 * Restart the resilver
4675 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4680 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4682 spa_history_log_internal(spa, "vdev attach", NULL,
4683 "%s vdev=%s %s vdev=%s",
4684 replacing && newvd_isspare ? "spare in" :
4685 replacing ? "replace" : "attach", newvdpath,
4686 replacing ? "for" : "to", oldvdpath);
4688 spa_strfree(oldvdpath);
4689 spa_strfree(newvdpath);
4691 if (spa->spa_bootfs)
4692 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4698 * Detach a device from a mirror or replacing vdev.
4699 * If 'replace_done' is specified, only detach if the parent
4700 * is a replacing vdev.
4703 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4707 vdev_t *rvd = spa->spa_root_vdev;
4708 vdev_t *vd, *pvd, *cvd, *tvd;
4709 boolean_t unspare = B_FALSE;
4710 uint64_t unspare_guid = 0;
4713 ASSERT(spa_writeable(spa));
4715 txg = spa_vdev_enter(spa);
4717 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4720 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4722 if (!vd->vdev_ops->vdev_op_leaf)
4723 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4725 pvd = vd->vdev_parent;
4728 * If the parent/child relationship is not as expected, don't do it.
4729 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4730 * vdev that's replacing B with C. The user's intent in replacing
4731 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4732 * the replace by detaching C, the expected behavior is to end up
4733 * M(A,B). But suppose that right after deciding to detach C,
4734 * the replacement of B completes. We would have M(A,C), and then
4735 * ask to detach C, which would leave us with just A -- not what
4736 * the user wanted. To prevent this, we make sure that the
4737 * parent/child relationship hasn't changed -- in this example,
4738 * that C's parent is still the replacing vdev R.
4740 if (pvd->vdev_guid != pguid && pguid != 0)
4741 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4744 * Only 'replacing' or 'spare' vdevs can be replaced.
4746 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4747 pvd->vdev_ops != &vdev_spare_ops)
4748 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4750 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4751 spa_version(spa) >= SPA_VERSION_SPARES);
4754 * Only mirror, replacing, and spare vdevs support detach.
4756 if (pvd->vdev_ops != &vdev_replacing_ops &&
4757 pvd->vdev_ops != &vdev_mirror_ops &&
4758 pvd->vdev_ops != &vdev_spare_ops)
4759 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4762 * If this device has the only valid copy of some data,
4763 * we cannot safely detach it.
4765 if (vdev_dtl_required(vd))
4766 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4768 ASSERT(pvd->vdev_children >= 2);
4771 * If we are detaching the second disk from a replacing vdev, then
4772 * check to see if we changed the original vdev's path to have "/old"
4773 * at the end in spa_vdev_attach(). If so, undo that change now.
4775 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4776 vd->vdev_path != NULL) {
4777 size_t len = strlen(vd->vdev_path);
4779 for (int c = 0; c < pvd->vdev_children; c++) {
4780 cvd = pvd->vdev_child[c];
4782 if (cvd == vd || cvd->vdev_path == NULL)
4785 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4786 strcmp(cvd->vdev_path + len, "/old") == 0) {
4787 spa_strfree(cvd->vdev_path);
4788 cvd->vdev_path = spa_strdup(vd->vdev_path);
4795 * If we are detaching the original disk from a spare, then it implies
4796 * that the spare should become a real disk, and be removed from the
4797 * active spare list for the pool.
4799 if (pvd->vdev_ops == &vdev_spare_ops &&
4801 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4805 * Erase the disk labels so the disk can be used for other things.
4806 * This must be done after all other error cases are handled,
4807 * but before we disembowel vd (so we can still do I/O to it).
4808 * But if we can't do it, don't treat the error as fatal --
4809 * it may be that the unwritability of the disk is the reason
4810 * it's being detached!
4812 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4815 * Remove vd from its parent and compact the parent's children.
4817 vdev_remove_child(pvd, vd);
4818 vdev_compact_children(pvd);
4821 * Remember one of the remaining children so we can get tvd below.
4823 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4826 * If we need to remove the remaining child from the list of hot spares,
4827 * do it now, marking the vdev as no longer a spare in the process.
4828 * We must do this before vdev_remove_parent(), because that can
4829 * change the GUID if it creates a new toplevel GUID. For a similar
4830 * reason, we must remove the spare now, in the same txg as the detach;
4831 * otherwise someone could attach a new sibling, change the GUID, and
4832 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4835 ASSERT(cvd->vdev_isspare);
4836 spa_spare_remove(cvd);
4837 unspare_guid = cvd->vdev_guid;
4838 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4839 cvd->vdev_unspare = B_TRUE;
4843 * If the parent mirror/replacing vdev only has one child,
4844 * the parent is no longer needed. Remove it from the tree.
4846 if (pvd->vdev_children == 1) {
4847 if (pvd->vdev_ops == &vdev_spare_ops)
4848 cvd->vdev_unspare = B_FALSE;
4849 vdev_remove_parent(cvd);
4850 cvd->vdev_resilvering = B_FALSE;
4855 * We don't set tvd until now because the parent we just removed
4856 * may have been the previous top-level vdev.
4858 tvd = cvd->vdev_top;
4859 ASSERT(tvd->vdev_parent == rvd);
4862 * Reevaluate the parent vdev state.
4864 vdev_propagate_state(cvd);
4867 * If the 'autoexpand' property is set on the pool then automatically
4868 * try to expand the size of the pool. For example if the device we
4869 * just detached was smaller than the others, it may be possible to
4870 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4871 * first so that we can obtain the updated sizes of the leaf vdevs.
4873 if (spa->spa_autoexpand) {
4875 vdev_expand(tvd, txg);
4878 vdev_config_dirty(tvd);
4881 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4882 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4883 * But first make sure we're not on any *other* txg's DTL list, to
4884 * prevent vd from being accessed after it's freed.
4886 vdpath = spa_strdup(vd->vdev_path);
4887 for (int t = 0; t < TXG_SIZE; t++)
4888 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4889 vd->vdev_detached = B_TRUE;
4890 vdev_dirty(tvd, VDD_DTL, vd, txg);
4892 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4894 /* hang on to the spa before we release the lock */
4895 spa_open_ref(spa, FTAG);
4897 error = spa_vdev_exit(spa, vd, txg, 0);
4899 spa_history_log_internal(spa, "detach", NULL,
4901 spa_strfree(vdpath);
4904 * If this was the removal of the original device in a hot spare vdev,
4905 * then we want to go through and remove the device from the hot spare
4906 * list of every other pool.
4909 spa_t *altspa = NULL;
4911 mutex_enter(&spa_namespace_lock);
4912 while ((altspa = spa_next(altspa)) != NULL) {
4913 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4917 spa_open_ref(altspa, FTAG);
4918 mutex_exit(&spa_namespace_lock);
4919 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4920 mutex_enter(&spa_namespace_lock);
4921 spa_close(altspa, FTAG);
4923 mutex_exit(&spa_namespace_lock);
4925 /* search the rest of the vdevs for spares to remove */
4926 spa_vdev_resilver_done(spa);
4929 /* all done with the spa; OK to release */
4930 mutex_enter(&spa_namespace_lock);
4931 spa_close(spa, FTAG);
4932 mutex_exit(&spa_namespace_lock);
4938 * Split a set of devices from their mirrors, and create a new pool from them.
4941 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4942 nvlist_t *props, boolean_t exp)
4945 uint64_t txg, *glist;
4947 uint_t c, children, lastlog;
4948 nvlist_t **child, *nvl, *tmp;
4950 char *altroot = NULL;
4951 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4952 boolean_t activate_slog;
4954 ASSERT(spa_writeable(spa));
4956 txg = spa_vdev_enter(spa);
4958 /* clear the log and flush everything up to now */
4959 activate_slog = spa_passivate_log(spa);
4960 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4961 error = spa_offline_log(spa);
4962 txg = spa_vdev_config_enter(spa);
4965 spa_activate_log(spa);
4968 return (spa_vdev_exit(spa, NULL, txg, error));
4970 /* check new spa name before going any further */
4971 if (spa_lookup(newname) != NULL)
4972 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4975 * scan through all the children to ensure they're all mirrors
4977 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4978 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4980 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4982 /* first, check to ensure we've got the right child count */
4983 rvd = spa->spa_root_vdev;
4985 for (c = 0; c < rvd->vdev_children; c++) {
4986 vdev_t *vd = rvd->vdev_child[c];
4988 /* don't count the holes & logs as children */
4989 if (vd->vdev_islog || vd->vdev_ishole) {
4997 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4998 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5000 /* next, ensure no spare or cache devices are part of the split */
5001 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5002 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5003 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5005 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5006 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5008 /* then, loop over each vdev and validate it */
5009 for (c = 0; c < children; c++) {
5010 uint64_t is_hole = 0;
5012 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5016 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5017 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5020 error = SET_ERROR(EINVAL);
5025 /* which disk is going to be split? */
5026 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5028 error = SET_ERROR(EINVAL);
5032 /* look it up in the spa */
5033 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5034 if (vml[c] == NULL) {
5035 error = SET_ERROR(ENODEV);
5039 /* make sure there's nothing stopping the split */
5040 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5041 vml[c]->vdev_islog ||
5042 vml[c]->vdev_ishole ||
5043 vml[c]->vdev_isspare ||
5044 vml[c]->vdev_isl2cache ||
5045 !vdev_writeable(vml[c]) ||
5046 vml[c]->vdev_children != 0 ||
5047 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5048 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5049 error = SET_ERROR(EINVAL);
5053 if (vdev_dtl_required(vml[c])) {
5054 error = SET_ERROR(EBUSY);
5058 /* we need certain info from the top level */
5059 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5060 vml[c]->vdev_top->vdev_ms_array) == 0);
5061 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5062 vml[c]->vdev_top->vdev_ms_shift) == 0);
5063 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5064 vml[c]->vdev_top->vdev_asize) == 0);
5065 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5066 vml[c]->vdev_top->vdev_ashift) == 0);
5070 kmem_free(vml, children * sizeof (vdev_t *));
5071 kmem_free(glist, children * sizeof (uint64_t));
5072 return (spa_vdev_exit(spa, NULL, txg, error));
5075 /* stop writers from using the disks */
5076 for (c = 0; c < children; c++) {
5078 vml[c]->vdev_offline = B_TRUE;
5080 vdev_reopen(spa->spa_root_vdev);
5083 * Temporarily record the splitting vdevs in the spa config. This
5084 * will disappear once the config is regenerated.
5086 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5087 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5088 glist, children) == 0);
5089 kmem_free(glist, children * sizeof (uint64_t));
5091 mutex_enter(&spa->spa_props_lock);
5092 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5094 mutex_exit(&spa->spa_props_lock);
5095 spa->spa_config_splitting = nvl;
5096 vdev_config_dirty(spa->spa_root_vdev);
5098 /* configure and create the new pool */
5099 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5100 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5101 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5102 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5103 spa_version(spa)) == 0);
5104 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5105 spa->spa_config_txg) == 0);
5106 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5107 spa_generate_guid(NULL)) == 0);
5108 (void) nvlist_lookup_string(props,
5109 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5111 /* add the new pool to the namespace */
5112 newspa = spa_add(newname, config, altroot);
5113 newspa->spa_config_txg = spa->spa_config_txg;
5114 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5116 /* release the spa config lock, retaining the namespace lock */
5117 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5119 if (zio_injection_enabled)
5120 zio_handle_panic_injection(spa, FTAG, 1);
5122 spa_activate(newspa, spa_mode_global);
5123 spa_async_suspend(newspa);
5126 /* mark that we are creating new spa by splitting */
5127 newspa->spa_splitting_newspa = B_TRUE;
5129 /* create the new pool from the disks of the original pool */
5130 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5132 newspa->spa_splitting_newspa = B_FALSE;
5137 /* if that worked, generate a real config for the new pool */
5138 if (newspa->spa_root_vdev != NULL) {
5139 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5140 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5141 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5142 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5143 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5148 if (props != NULL) {
5149 spa_configfile_set(newspa, props, B_FALSE);
5150 error = spa_prop_set(newspa, props);
5155 /* flush everything */
5156 txg = spa_vdev_config_enter(newspa);
5157 vdev_config_dirty(newspa->spa_root_vdev);
5158 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5160 if (zio_injection_enabled)
5161 zio_handle_panic_injection(spa, FTAG, 2);
5163 spa_async_resume(newspa);
5165 /* finally, update the original pool's config */
5166 txg = spa_vdev_config_enter(spa);
5167 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5168 error = dmu_tx_assign(tx, TXG_WAIT);
5171 for (c = 0; c < children; c++) {
5172 if (vml[c] != NULL) {
5175 spa_history_log_internal(spa, "detach", tx,
5176 "vdev=%s", vml[c]->vdev_path);
5180 vdev_config_dirty(spa->spa_root_vdev);
5181 spa->spa_config_splitting = NULL;
5185 (void) spa_vdev_exit(spa, NULL, txg, 0);
5187 if (zio_injection_enabled)
5188 zio_handle_panic_injection(spa, FTAG, 3);
5190 /* split is complete; log a history record */
5191 spa_history_log_internal(newspa, "split", NULL,
5192 "from pool %s", spa_name(spa));
5194 kmem_free(vml, children * sizeof (vdev_t *));
5196 /* if we're not going to mount the filesystems in userland, export */
5198 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5205 spa_deactivate(newspa);
5208 txg = spa_vdev_config_enter(spa);
5210 /* re-online all offlined disks */
5211 for (c = 0; c < children; c++) {
5213 vml[c]->vdev_offline = B_FALSE;
5215 vdev_reopen(spa->spa_root_vdev);
5217 nvlist_free(spa->spa_config_splitting);
5218 spa->spa_config_splitting = NULL;
5219 (void) spa_vdev_exit(spa, NULL, txg, error);
5221 kmem_free(vml, children * sizeof (vdev_t *));
5226 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5228 for (int i = 0; i < count; i++) {
5231 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5234 if (guid == target_guid)
5242 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5243 nvlist_t *dev_to_remove)
5245 nvlist_t **newdev = NULL;
5248 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5250 for (int i = 0, j = 0; i < count; i++) {
5251 if (dev[i] == dev_to_remove)
5253 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5256 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5257 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5259 for (int i = 0; i < count - 1; i++)
5260 nvlist_free(newdev[i]);
5263 kmem_free(newdev, (count - 1) * sizeof (void *));
5267 * Evacuate the device.
5270 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5275 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5276 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5277 ASSERT(vd == vd->vdev_top);
5280 * Evacuate the device. We don't hold the config lock as writer
5281 * since we need to do I/O but we do keep the
5282 * spa_namespace_lock held. Once this completes the device
5283 * should no longer have any blocks allocated on it.
5285 if (vd->vdev_islog) {
5286 if (vd->vdev_stat.vs_alloc != 0)
5287 error = spa_offline_log(spa);
5289 error = SET_ERROR(ENOTSUP);
5296 * The evacuation succeeded. Remove any remaining MOS metadata
5297 * associated with this vdev, and wait for these changes to sync.
5299 ASSERT0(vd->vdev_stat.vs_alloc);
5300 txg = spa_vdev_config_enter(spa);
5301 vd->vdev_removing = B_TRUE;
5302 vdev_dirty(vd, 0, NULL, txg);
5303 vdev_config_dirty(vd);
5304 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5310 * Complete the removal by cleaning up the namespace.
5313 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5315 vdev_t *rvd = spa->spa_root_vdev;
5316 uint64_t id = vd->vdev_id;
5317 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5319 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5320 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5321 ASSERT(vd == vd->vdev_top);
5324 * Only remove any devices which are empty.
5326 if (vd->vdev_stat.vs_alloc != 0)
5329 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5331 if (list_link_active(&vd->vdev_state_dirty_node))
5332 vdev_state_clean(vd);
5333 if (list_link_active(&vd->vdev_config_dirty_node))
5334 vdev_config_clean(vd);
5339 vdev_compact_children(rvd);
5341 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5342 vdev_add_child(rvd, vd);
5344 vdev_config_dirty(rvd);
5347 * Reassess the health of our root vdev.
5353 * Remove a device from the pool -
5355 * Removing a device from the vdev namespace requires several steps
5356 * and can take a significant amount of time. As a result we use
5357 * the spa_vdev_config_[enter/exit] functions which allow us to
5358 * grab and release the spa_config_lock while still holding the namespace
5359 * lock. During each step the configuration is synced out.
5363 * Remove a device from the pool. Currently, this supports removing only hot
5364 * spares, slogs, and level 2 ARC devices.
5367 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5370 metaslab_group_t *mg;
5371 nvlist_t **spares, **l2cache, *nv;
5373 uint_t nspares, nl2cache;
5375 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5377 ASSERT(spa_writeable(spa));
5380 txg = spa_vdev_enter(spa);
5382 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5384 if (spa->spa_spares.sav_vdevs != NULL &&
5385 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5386 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5387 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5389 * Only remove the hot spare if it's not currently in use
5392 if (vd == NULL || unspare) {
5393 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5394 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5395 spa_load_spares(spa);
5396 spa->spa_spares.sav_sync = B_TRUE;
5398 error = SET_ERROR(EBUSY);
5400 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5401 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5402 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5403 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5405 * Cache devices can always be removed.
5407 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5408 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5409 spa_load_l2cache(spa);
5410 spa->spa_l2cache.sav_sync = B_TRUE;
5411 } else if (vd != NULL && vd->vdev_islog) {
5413 ASSERT(vd == vd->vdev_top);
5416 * XXX - Once we have bp-rewrite this should
5417 * become the common case.
5423 * Stop allocating from this vdev.
5425 metaslab_group_passivate(mg);
5428 * Wait for the youngest allocations and frees to sync,
5429 * and then wait for the deferral of those frees to finish.
5431 spa_vdev_config_exit(spa, NULL,
5432 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5435 * Attempt to evacuate the vdev.
5437 error = spa_vdev_remove_evacuate(spa, vd);
5439 txg = spa_vdev_config_enter(spa);
5442 * If we couldn't evacuate the vdev, unwind.
5445 metaslab_group_activate(mg);
5446 return (spa_vdev_exit(spa, NULL, txg, error));
5450 * Clean up the vdev namespace.
5452 spa_vdev_remove_from_namespace(spa, vd);
5454 } else if (vd != NULL) {
5456 * Normal vdevs cannot be removed (yet).
5458 error = SET_ERROR(ENOTSUP);
5461 * There is no vdev of any kind with the specified guid.
5463 error = SET_ERROR(ENOENT);
5467 return (spa_vdev_exit(spa, NULL, txg, error));
5473 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5474 * current spared, so we can detach it.
5477 spa_vdev_resilver_done_hunt(vdev_t *vd)
5479 vdev_t *newvd, *oldvd;
5481 for (int c = 0; c < vd->vdev_children; c++) {
5482 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5488 * Check for a completed replacement. We always consider the first
5489 * vdev in the list to be the oldest vdev, and the last one to be
5490 * the newest (see spa_vdev_attach() for how that works). In
5491 * the case where the newest vdev is faulted, we will not automatically
5492 * remove it after a resilver completes. This is OK as it will require
5493 * user intervention to determine which disk the admin wishes to keep.
5495 if (vd->vdev_ops == &vdev_replacing_ops) {
5496 ASSERT(vd->vdev_children > 1);
5498 newvd = vd->vdev_child[vd->vdev_children - 1];
5499 oldvd = vd->vdev_child[0];
5501 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5502 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5503 !vdev_dtl_required(oldvd))
5508 * Check for a completed resilver with the 'unspare' flag set.
5510 if (vd->vdev_ops == &vdev_spare_ops) {
5511 vdev_t *first = vd->vdev_child[0];
5512 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5514 if (last->vdev_unspare) {
5517 } else if (first->vdev_unspare) {
5524 if (oldvd != NULL &&
5525 vdev_dtl_empty(newvd, DTL_MISSING) &&
5526 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5527 !vdev_dtl_required(oldvd))
5531 * If there are more than two spares attached to a disk,
5532 * and those spares are not required, then we want to
5533 * attempt to free them up now so that they can be used
5534 * by other pools. Once we're back down to a single
5535 * disk+spare, we stop removing them.
5537 if (vd->vdev_children > 2) {
5538 newvd = vd->vdev_child[1];
5540 if (newvd->vdev_isspare && last->vdev_isspare &&
5541 vdev_dtl_empty(last, DTL_MISSING) &&
5542 vdev_dtl_empty(last, DTL_OUTAGE) &&
5543 !vdev_dtl_required(newvd))
5552 spa_vdev_resilver_done(spa_t *spa)
5554 vdev_t *vd, *pvd, *ppvd;
5555 uint64_t guid, sguid, pguid, ppguid;
5557 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5559 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5560 pvd = vd->vdev_parent;
5561 ppvd = pvd->vdev_parent;
5562 guid = vd->vdev_guid;
5563 pguid = pvd->vdev_guid;
5564 ppguid = ppvd->vdev_guid;
5567 * If we have just finished replacing a hot spared device, then
5568 * we need to detach the parent's first child (the original hot
5571 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5572 ppvd->vdev_children == 2) {
5573 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5574 sguid = ppvd->vdev_child[1]->vdev_guid;
5576 spa_config_exit(spa, SCL_ALL, FTAG);
5577 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5579 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5581 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5584 spa_config_exit(spa, SCL_ALL, FTAG);
5588 * Update the stored path or FRU for this vdev.
5591 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5595 boolean_t sync = B_FALSE;
5597 ASSERT(spa_writeable(spa));
5599 spa_vdev_state_enter(spa, SCL_ALL);
5601 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5602 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5604 if (!vd->vdev_ops->vdev_op_leaf)
5605 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5608 if (strcmp(value, vd->vdev_path) != 0) {
5609 spa_strfree(vd->vdev_path);
5610 vd->vdev_path = spa_strdup(value);
5614 if (vd->vdev_fru == NULL) {
5615 vd->vdev_fru = spa_strdup(value);
5617 } else if (strcmp(value, vd->vdev_fru) != 0) {
5618 spa_strfree(vd->vdev_fru);
5619 vd->vdev_fru = spa_strdup(value);
5624 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5628 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5630 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5634 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5636 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5640 * ==========================================================================
5642 * ==========================================================================
5646 spa_scan_stop(spa_t *spa)
5648 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5649 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5650 return (SET_ERROR(EBUSY));
5651 return (dsl_scan_cancel(spa->spa_dsl_pool));
5655 spa_scan(spa_t *spa, pool_scan_func_t func)
5657 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5659 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5660 return (SET_ERROR(ENOTSUP));
5663 * If a resilver was requested, but there is no DTL on a
5664 * writeable leaf device, we have nothing to do.
5666 if (func == POOL_SCAN_RESILVER &&
5667 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5668 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5672 return (dsl_scan(spa->spa_dsl_pool, func));
5676 * ==========================================================================
5677 * SPA async task processing
5678 * ==========================================================================
5682 spa_async_remove(spa_t *spa, vdev_t *vd)
5684 if (vd->vdev_remove_wanted) {
5685 vd->vdev_remove_wanted = B_FALSE;
5686 vd->vdev_delayed_close = B_FALSE;
5687 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5690 * We want to clear the stats, but we don't want to do a full
5691 * vdev_clear() as that will cause us to throw away
5692 * degraded/faulted state as well as attempt to reopen the
5693 * device, all of which is a waste.
5695 vd->vdev_stat.vs_read_errors = 0;
5696 vd->vdev_stat.vs_write_errors = 0;
5697 vd->vdev_stat.vs_checksum_errors = 0;
5699 vdev_state_dirty(vd->vdev_top);
5702 for (int c = 0; c < vd->vdev_children; c++)
5703 spa_async_remove(spa, vd->vdev_child[c]);
5707 spa_async_probe(spa_t *spa, vdev_t *vd)
5709 if (vd->vdev_probe_wanted) {
5710 vd->vdev_probe_wanted = B_FALSE;
5711 vdev_reopen(vd); /* vdev_open() does the actual probe */
5714 for (int c = 0; c < vd->vdev_children; c++)
5715 spa_async_probe(spa, vd->vdev_child[c]);
5719 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5725 if (!spa->spa_autoexpand)
5728 for (int c = 0; c < vd->vdev_children; c++) {
5729 vdev_t *cvd = vd->vdev_child[c];
5730 spa_async_autoexpand(spa, cvd);
5733 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5736 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5737 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5739 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5740 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5742 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5743 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5746 kmem_free(physpath, MAXPATHLEN);
5750 spa_async_thread(void *arg)
5755 ASSERT(spa->spa_sync_on);
5757 mutex_enter(&spa->spa_async_lock);
5758 tasks = spa->spa_async_tasks;
5759 spa->spa_async_tasks = 0;
5760 mutex_exit(&spa->spa_async_lock);
5763 * See if the config needs to be updated.
5765 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5766 uint64_t old_space, new_space;
5768 mutex_enter(&spa_namespace_lock);
5769 old_space = metaslab_class_get_space(spa_normal_class(spa));
5770 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5771 new_space = metaslab_class_get_space(spa_normal_class(spa));
5772 mutex_exit(&spa_namespace_lock);
5775 * If the pool grew as a result of the config update,
5776 * then log an internal history event.
5778 if (new_space != old_space) {
5779 spa_history_log_internal(spa, "vdev online", NULL,
5780 "pool '%s' size: %llu(+%llu)",
5781 spa_name(spa), new_space, new_space - old_space);
5786 * See if any devices need to be marked REMOVED.
5788 if (tasks & SPA_ASYNC_REMOVE) {
5789 spa_vdev_state_enter(spa, SCL_NONE);
5790 spa_async_remove(spa, spa->spa_root_vdev);
5791 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5792 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5793 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5794 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5795 (void) spa_vdev_state_exit(spa, NULL, 0);
5798 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5799 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5800 spa_async_autoexpand(spa, spa->spa_root_vdev);
5801 spa_config_exit(spa, SCL_CONFIG, FTAG);
5805 * See if any devices need to be probed.
5807 if (tasks & SPA_ASYNC_PROBE) {
5808 spa_vdev_state_enter(spa, SCL_NONE);
5809 spa_async_probe(spa, spa->spa_root_vdev);
5810 (void) spa_vdev_state_exit(spa, NULL, 0);
5814 * If any devices are done replacing, detach them.
5816 if (tasks & SPA_ASYNC_RESILVER_DONE)
5817 spa_vdev_resilver_done(spa);
5820 * Kick off a resilver.
5822 if (tasks & SPA_ASYNC_RESILVER)
5823 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5826 * Let the world know that we're done.
5828 mutex_enter(&spa->spa_async_lock);
5829 spa->spa_async_thread = NULL;
5830 cv_broadcast(&spa->spa_async_cv);
5831 mutex_exit(&spa->spa_async_lock);
5836 spa_async_suspend(spa_t *spa)
5838 mutex_enter(&spa->spa_async_lock);
5839 spa->spa_async_suspended++;
5840 while (spa->spa_async_thread != NULL)
5841 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5842 mutex_exit(&spa->spa_async_lock);
5846 spa_async_resume(spa_t *spa)
5848 mutex_enter(&spa->spa_async_lock);
5849 ASSERT(spa->spa_async_suspended != 0);
5850 spa->spa_async_suspended--;
5851 mutex_exit(&spa->spa_async_lock);
5855 spa_async_dispatch(spa_t *spa)
5857 mutex_enter(&spa->spa_async_lock);
5858 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5859 spa->spa_async_thread == NULL &&
5860 rootdir != NULL && !vn_is_readonly(rootdir))
5861 spa->spa_async_thread = thread_create(NULL, 0,
5862 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5863 mutex_exit(&spa->spa_async_lock);
5867 spa_async_request(spa_t *spa, int task)
5869 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5870 mutex_enter(&spa->spa_async_lock);
5871 spa->spa_async_tasks |= task;
5872 mutex_exit(&spa->spa_async_lock);
5876 * ==========================================================================
5877 * SPA syncing routines
5878 * ==========================================================================
5882 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5885 bpobj_enqueue(bpo, bp, tx);
5890 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5894 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5895 BP_GET_PSIZE(bp), zio->io_flags));
5900 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5902 char *packed = NULL;
5907 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5910 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5911 * information. This avoids the dbuf_will_dirty() path and
5912 * saves us a pre-read to get data we don't actually care about.
5914 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
5915 packed = kmem_alloc(bufsize, KM_SLEEP);
5917 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5919 bzero(packed + nvsize, bufsize - nvsize);
5921 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5923 kmem_free(packed, bufsize);
5925 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5926 dmu_buf_will_dirty(db, tx);
5927 *(uint64_t *)db->db_data = nvsize;
5928 dmu_buf_rele(db, FTAG);
5932 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5933 const char *config, const char *entry)
5943 * Update the MOS nvlist describing the list of available devices.
5944 * spa_validate_aux() will have already made sure this nvlist is
5945 * valid and the vdevs are labeled appropriately.
5947 if (sav->sav_object == 0) {
5948 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5949 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5950 sizeof (uint64_t), tx);
5951 VERIFY(zap_update(spa->spa_meta_objset,
5952 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5953 &sav->sav_object, tx) == 0);
5956 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5957 if (sav->sav_count == 0) {
5958 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5960 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5961 for (i = 0; i < sav->sav_count; i++)
5962 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5963 B_FALSE, VDEV_CONFIG_L2CACHE);
5964 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5965 sav->sav_count) == 0);
5966 for (i = 0; i < sav->sav_count; i++)
5967 nvlist_free(list[i]);
5968 kmem_free(list, sav->sav_count * sizeof (void *));
5971 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5972 nvlist_free(nvroot);
5974 sav->sav_sync = B_FALSE;
5978 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5982 if (list_is_empty(&spa->spa_config_dirty_list))
5985 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5987 config = spa_config_generate(spa, spa->spa_root_vdev,
5988 dmu_tx_get_txg(tx), B_FALSE);
5991 * If we're upgrading the spa version then make sure that
5992 * the config object gets updated with the correct version.
5994 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
5995 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5996 spa->spa_uberblock.ub_version);
5998 spa_config_exit(spa, SCL_STATE, FTAG);
6000 if (spa->spa_config_syncing)
6001 nvlist_free(spa->spa_config_syncing);
6002 spa->spa_config_syncing = config;
6004 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6008 spa_sync_version(void *arg, dmu_tx_t *tx)
6010 uint64_t *versionp = arg;
6011 uint64_t version = *versionp;
6012 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6015 * Setting the version is special cased when first creating the pool.
6017 ASSERT(tx->tx_txg != TXG_INITIAL);
6019 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6020 ASSERT(version >= spa_version(spa));
6022 spa->spa_uberblock.ub_version = version;
6023 vdev_config_dirty(spa->spa_root_vdev);
6024 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6028 * Set zpool properties.
6031 spa_sync_props(void *arg, dmu_tx_t *tx)
6033 nvlist_t *nvp = arg;
6034 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6035 objset_t *mos = spa->spa_meta_objset;
6036 nvpair_t *elem = NULL;
6038 mutex_enter(&spa->spa_props_lock);
6040 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6042 char *strval, *fname;
6044 const char *propname;
6045 zprop_type_t proptype;
6046 zfeature_info_t *feature;
6048 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6051 * We checked this earlier in spa_prop_validate().
6053 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6055 fname = strchr(nvpair_name(elem), '@') + 1;
6056 VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature));
6058 spa_feature_enable(spa, feature, tx);
6059 spa_history_log_internal(spa, "set", tx,
6060 "%s=enabled", nvpair_name(elem));
6063 case ZPOOL_PROP_VERSION:
6064 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
6066 * The version is synced seperatly before other
6067 * properties and should be correct by now.
6069 ASSERT3U(spa_version(spa), >=, intval);
6072 case ZPOOL_PROP_ALTROOT:
6074 * 'altroot' is a non-persistent property. It should
6075 * have been set temporarily at creation or import time.
6077 ASSERT(spa->spa_root != NULL);
6080 case ZPOOL_PROP_READONLY:
6081 case ZPOOL_PROP_CACHEFILE:
6083 * 'readonly' and 'cachefile' are also non-persisitent
6087 case ZPOOL_PROP_COMMENT:
6088 VERIFY(nvpair_value_string(elem, &strval) == 0);
6089 if (spa->spa_comment != NULL)
6090 spa_strfree(spa->spa_comment);
6091 spa->spa_comment = spa_strdup(strval);
6093 * We need to dirty the configuration on all the vdevs
6094 * so that their labels get updated. It's unnecessary
6095 * to do this for pool creation since the vdev's
6096 * configuratoin has already been dirtied.
6098 if (tx->tx_txg != TXG_INITIAL)
6099 vdev_config_dirty(spa->spa_root_vdev);
6100 spa_history_log_internal(spa, "set", tx,
6101 "%s=%s", nvpair_name(elem), strval);
6105 * Set pool property values in the poolprops mos object.
6107 if (spa->spa_pool_props_object == 0) {
6108 spa->spa_pool_props_object =
6109 zap_create_link(mos, DMU_OT_POOL_PROPS,
6110 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6114 /* normalize the property name */
6115 propname = zpool_prop_to_name(prop);
6116 proptype = zpool_prop_get_type(prop);
6118 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6119 ASSERT(proptype == PROP_TYPE_STRING);
6120 VERIFY(nvpair_value_string(elem, &strval) == 0);
6121 VERIFY(zap_update(mos,
6122 spa->spa_pool_props_object, propname,
6123 1, strlen(strval) + 1, strval, tx) == 0);
6124 spa_history_log_internal(spa, "set", tx,
6125 "%s=%s", nvpair_name(elem), strval);
6126 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6127 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
6129 if (proptype == PROP_TYPE_INDEX) {
6131 VERIFY(zpool_prop_index_to_string(
6132 prop, intval, &unused) == 0);
6134 VERIFY(zap_update(mos,
6135 spa->spa_pool_props_object, propname,
6136 8, 1, &intval, tx) == 0);
6137 spa_history_log_internal(spa, "set", tx,
6138 "%s=%lld", nvpair_name(elem), intval);
6140 ASSERT(0); /* not allowed */
6144 case ZPOOL_PROP_DELEGATION:
6145 spa->spa_delegation = intval;
6147 case ZPOOL_PROP_BOOTFS:
6148 spa->spa_bootfs = intval;
6150 case ZPOOL_PROP_FAILUREMODE:
6151 spa->spa_failmode = intval;
6153 case ZPOOL_PROP_AUTOEXPAND:
6154 spa->spa_autoexpand = intval;
6155 if (tx->tx_txg != TXG_INITIAL)
6156 spa_async_request(spa,
6157 SPA_ASYNC_AUTOEXPAND);
6159 case ZPOOL_PROP_DEDUPDITTO:
6160 spa->spa_dedup_ditto = intval;
6169 mutex_exit(&spa->spa_props_lock);
6173 * Perform one-time upgrade on-disk changes. spa_version() does not
6174 * reflect the new version this txg, so there must be no changes this
6175 * txg to anything that the upgrade code depends on after it executes.
6176 * Therefore this must be called after dsl_pool_sync() does the sync
6180 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6182 dsl_pool_t *dp = spa->spa_dsl_pool;
6184 ASSERT(spa->spa_sync_pass == 1);
6186 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6188 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6189 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6190 dsl_pool_create_origin(dp, tx);
6192 /* Keeping the origin open increases spa_minref */
6193 spa->spa_minref += 3;
6196 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6197 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6198 dsl_pool_upgrade_clones(dp, tx);
6201 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6202 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6203 dsl_pool_upgrade_dir_clones(dp, tx);
6205 /* Keeping the freedir open increases spa_minref */
6206 spa->spa_minref += 3;
6209 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6210 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6211 spa_feature_create_zap_objects(spa, tx);
6213 rrw_exit(&dp->dp_config_rwlock, FTAG);
6217 * Sync the specified transaction group. New blocks may be dirtied as
6218 * part of the process, so we iterate until it converges.
6221 spa_sync(spa_t *spa, uint64_t txg)
6223 dsl_pool_t *dp = spa->spa_dsl_pool;
6224 objset_t *mos = spa->spa_meta_objset;
6225 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
6226 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6227 vdev_t *rvd = spa->spa_root_vdev;
6232 VERIFY(spa_writeable(spa));
6235 * Lock out configuration changes.
6237 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6239 spa->spa_syncing_txg = txg;
6240 spa->spa_sync_pass = 0;
6243 * If there are any pending vdev state changes, convert them
6244 * into config changes that go out with this transaction group.
6246 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6247 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6249 * We need the write lock here because, for aux vdevs,
6250 * calling vdev_config_dirty() modifies sav_config.
6251 * This is ugly and will become unnecessary when we
6252 * eliminate the aux vdev wart by integrating all vdevs
6253 * into the root vdev tree.
6255 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6256 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6257 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6258 vdev_state_clean(vd);
6259 vdev_config_dirty(vd);
6261 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6262 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6264 spa_config_exit(spa, SCL_STATE, FTAG);
6266 tx = dmu_tx_create_assigned(dp, txg);
6268 spa->spa_sync_starttime = gethrtime();
6270 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6271 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6274 callout_reset(&spa->spa_deadman_cycid,
6275 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6280 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6281 * set spa_deflate if we have no raid-z vdevs.
6283 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6284 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6287 for (i = 0; i < rvd->vdev_children; i++) {
6288 vd = rvd->vdev_child[i];
6289 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6292 if (i == rvd->vdev_children) {
6293 spa->spa_deflate = TRUE;
6294 VERIFY(0 == zap_add(spa->spa_meta_objset,
6295 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6296 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6301 * If anything has changed in this txg, or if someone is waiting
6302 * for this txg to sync (eg, spa_vdev_remove()), push the
6303 * deferred frees from the previous txg. If not, leave them
6304 * alone so that we don't generate work on an otherwise idle
6307 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6308 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6309 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6310 ((dsl_scan_active(dp->dp_scan) ||
6311 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6312 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6313 VERIFY3U(bpobj_iterate(defer_bpo,
6314 spa_free_sync_cb, zio, tx), ==, 0);
6315 VERIFY0(zio_wait(zio));
6319 * Iterate to convergence.
6322 int pass = ++spa->spa_sync_pass;
6324 spa_sync_config_object(spa, tx);
6325 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6326 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6327 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6328 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6329 spa_errlog_sync(spa, txg);
6330 dsl_pool_sync(dp, txg);
6332 if (pass < zfs_sync_pass_deferred_free) {
6333 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6334 bplist_iterate(free_bpl, spa_free_sync_cb,
6336 VERIFY(zio_wait(zio) == 0);
6338 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6343 dsl_scan_sync(dp, tx);
6345 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6349 spa_sync_upgrades(spa, tx);
6351 } while (dmu_objset_is_dirty(mos, txg));
6354 * Rewrite the vdev configuration (which includes the uberblock)
6355 * to commit the transaction group.
6357 * If there are no dirty vdevs, we sync the uberblock to a few
6358 * random top-level vdevs that are known to be visible in the
6359 * config cache (see spa_vdev_add() for a complete description).
6360 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6364 * We hold SCL_STATE to prevent vdev open/close/etc.
6365 * while we're attempting to write the vdev labels.
6367 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6369 if (list_is_empty(&spa->spa_config_dirty_list)) {
6370 vdev_t *svd[SPA_DVAS_PER_BP];
6372 int children = rvd->vdev_children;
6373 int c0 = spa_get_random(children);
6375 for (int c = 0; c < children; c++) {
6376 vd = rvd->vdev_child[(c0 + c) % children];
6377 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6379 svd[svdcount++] = vd;
6380 if (svdcount == SPA_DVAS_PER_BP)
6383 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6385 error = vdev_config_sync(svd, svdcount, txg,
6388 error = vdev_config_sync(rvd->vdev_child,
6389 rvd->vdev_children, txg, B_FALSE);
6391 error = vdev_config_sync(rvd->vdev_child,
6392 rvd->vdev_children, txg, B_TRUE);
6396 spa->spa_last_synced_guid = rvd->vdev_guid;
6398 spa_config_exit(spa, SCL_STATE, FTAG);
6402 zio_suspend(spa, NULL);
6403 zio_resume_wait(spa);
6408 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6411 callout_drain(&spa->spa_deadman_cycid);
6416 * Clear the dirty config list.
6418 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6419 vdev_config_clean(vd);
6422 * Now that the new config has synced transactionally,
6423 * let it become visible to the config cache.
6425 if (spa->spa_config_syncing != NULL) {
6426 spa_config_set(spa, spa->spa_config_syncing);
6427 spa->spa_config_txg = txg;
6428 spa->spa_config_syncing = NULL;
6431 spa->spa_ubsync = spa->spa_uberblock;
6433 dsl_pool_sync_done(dp, txg);
6436 * Update usable space statistics.
6438 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6439 vdev_sync_done(vd, txg);
6441 spa_update_dspace(spa);
6444 * It had better be the case that we didn't dirty anything
6445 * since vdev_config_sync().
6447 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6448 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6449 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6451 spa->spa_sync_pass = 0;
6453 spa_config_exit(spa, SCL_CONFIG, FTAG);
6455 spa_handle_ignored_writes(spa);
6458 * If any async tasks have been requested, kick them off.
6460 spa_async_dispatch(spa);
6464 * Sync all pools. We don't want to hold the namespace lock across these
6465 * operations, so we take a reference on the spa_t and drop the lock during the
6469 spa_sync_allpools(void)
6472 mutex_enter(&spa_namespace_lock);
6473 while ((spa = spa_next(spa)) != NULL) {
6474 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6475 !spa_writeable(spa) || spa_suspended(spa))
6477 spa_open_ref(spa, FTAG);
6478 mutex_exit(&spa_namespace_lock);
6479 txg_wait_synced(spa_get_dsl(spa), 0);
6480 mutex_enter(&spa_namespace_lock);
6481 spa_close(spa, FTAG);
6483 mutex_exit(&spa_namespace_lock);
6487 * ==========================================================================
6488 * Miscellaneous routines
6489 * ==========================================================================
6493 * Remove all pools in the system.
6501 * Remove all cached state. All pools should be closed now,
6502 * so every spa in the AVL tree should be unreferenced.
6504 mutex_enter(&spa_namespace_lock);
6505 while ((spa = spa_next(NULL)) != NULL) {
6507 * Stop async tasks. The async thread may need to detach
6508 * a device that's been replaced, which requires grabbing
6509 * spa_namespace_lock, so we must drop it here.
6511 spa_open_ref(spa, FTAG);
6512 mutex_exit(&spa_namespace_lock);
6513 spa_async_suspend(spa);
6514 mutex_enter(&spa_namespace_lock);
6515 spa_close(spa, FTAG);
6517 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6519 spa_deactivate(spa);
6523 mutex_exit(&spa_namespace_lock);
6527 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6532 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6536 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6537 vd = spa->spa_l2cache.sav_vdevs[i];
6538 if (vd->vdev_guid == guid)
6542 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6543 vd = spa->spa_spares.sav_vdevs[i];
6544 if (vd->vdev_guid == guid)
6553 spa_upgrade(spa_t *spa, uint64_t version)
6555 ASSERT(spa_writeable(spa));
6557 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6560 * This should only be called for a non-faulted pool, and since a
6561 * future version would result in an unopenable pool, this shouldn't be
6564 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6565 ASSERT(version >= spa->spa_uberblock.ub_version);
6567 spa->spa_uberblock.ub_version = version;
6568 vdev_config_dirty(spa->spa_root_vdev);
6570 spa_config_exit(spa, SCL_ALL, FTAG);
6572 txg_wait_synced(spa_get_dsl(spa), 0);
6576 spa_has_spare(spa_t *spa, uint64_t guid)
6580 spa_aux_vdev_t *sav = &spa->spa_spares;
6582 for (i = 0; i < sav->sav_count; i++)
6583 if (sav->sav_vdevs[i]->vdev_guid == guid)
6586 for (i = 0; i < sav->sav_npending; i++) {
6587 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6588 &spareguid) == 0 && spareguid == guid)
6596 * Check if a pool has an active shared spare device.
6597 * Note: reference count of an active spare is 2, as a spare and as a replace
6600 spa_has_active_shared_spare(spa_t *spa)
6604 spa_aux_vdev_t *sav = &spa->spa_spares;
6606 for (i = 0; i < sav->sav_count; i++) {
6607 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6608 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6617 * Post a sysevent corresponding to the given event. The 'name' must be one of
6618 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6619 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6620 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6621 * or zdb as real changes.
6624 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6628 sysevent_attr_list_t *attr = NULL;
6629 sysevent_value_t value;
6632 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6635 value.value_type = SE_DATA_TYPE_STRING;
6636 value.value.sv_string = spa_name(spa);
6637 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6640 value.value_type = SE_DATA_TYPE_UINT64;
6641 value.value.sv_uint64 = spa_guid(spa);
6642 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6646 value.value_type = SE_DATA_TYPE_UINT64;
6647 value.value.sv_uint64 = vd->vdev_guid;
6648 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6652 if (vd->vdev_path) {
6653 value.value_type = SE_DATA_TYPE_STRING;
6654 value.value.sv_string = vd->vdev_path;
6655 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6656 &value, SE_SLEEP) != 0)
6661 if (sysevent_attach_attributes(ev, attr) != 0)
6665 (void) log_sysevent(ev, SE_SLEEP, &eid);
6669 sysevent_free_attr(attr);