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 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
29 * This file contains all the routines used when modifying on-disk SPA state.
30 * This includes opening, importing, destroying, exporting a pool, and syncing a
34 #include <sys/zfs_context.h>
35 #include <sys/fm/fs/zfs.h>
36 #include <sys/spa_impl.h>
38 #include <sys/zio_checksum.h>
40 #include <sys/dmu_tx.h>
44 #include <sys/vdev_impl.h>
45 #include <sys/vdev_disk.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/systeminfo.h>
63 #include <sys/spa_boot.h>
64 #include <sys/zfs_ioctl.h>
65 #include <sys/dsl_scan.h>
66 #include <sys/zfeature.h>
69 #include <sys/bootprops.h>
70 #include <sys/callb.h>
71 #include <sys/cpupart.h>
73 #include <sys/sysdc.h>
78 #include "zfs_comutil.h"
80 typedef enum zti_modes {
81 zti_mode_fixed, /* value is # of threads (min 1) */
82 zti_mode_online_percent, /* value is % of online CPUs */
83 zti_mode_batch, /* cpu-intensive; value is ignored */
84 zti_mode_null, /* don't create a taskq */
88 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
89 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
90 #define ZTI_BATCH { zti_mode_batch, 0 }
91 #define ZTI_NULL { zti_mode_null, 0 }
93 #define ZTI_ONE ZTI_FIX(1)
95 typedef struct zio_taskq_info {
96 enum zti_modes zti_mode;
100 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
101 "iss", "iss_h", "int", "int_h"
105 * Define the taskq threads for the following I/O types:
106 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
108 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
109 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
110 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
111 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
112 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(16), ZTI_FIX(5) },
113 { ZTI_PCT(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
114 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
115 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
118 static dsl_syncfunc_t spa_sync_version;
119 static dsl_syncfunc_t spa_sync_props;
120 static dsl_checkfunc_t spa_change_guid_check;
121 static dsl_syncfunc_t spa_change_guid_sync;
122 static boolean_t spa_has_active_shared_spare(spa_t *spa);
123 static inline int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
124 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
126 static void spa_vdev_resilver_done(spa_t *spa);
128 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
129 id_t zio_taskq_psrset_bind = PS_NONE;
130 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
131 uint_t zio_taskq_basedc = 80; /* base duty cycle */
133 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
136 * This (illegal) pool name is used when temporarily importing a spa_t in order
137 * to get the vdev stats associated with the imported devices.
139 #define TRYIMPORT_NAME "$import"
142 * ==========================================================================
143 * SPA properties routines
144 * ==========================================================================
148 * Add a (source=src, propname=propval) list to an nvlist.
151 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
152 uint64_t intval, zprop_source_t src)
154 const char *propname = zpool_prop_to_name(prop);
157 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
158 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
161 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
163 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
165 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
166 nvlist_free(propval);
170 * Get property values from the spa configuration.
173 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
175 vdev_t *rvd = spa->spa_root_vdev;
176 dsl_pool_t *pool = spa->spa_dsl_pool;
180 uint64_t cap, version;
181 zprop_source_t src = ZPROP_SRC_NONE;
182 spa_config_dirent_t *dp;
185 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
188 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
189 size = metaslab_class_get_space(spa_normal_class(spa));
190 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
191 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
192 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
193 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
197 for (c = 0; c < rvd->vdev_children; c++) {
198 vdev_t *tvd = rvd->vdev_child[c];
199 space += tvd->vdev_max_asize - tvd->vdev_asize;
201 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
204 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
205 (spa_mode(spa) == FREAD), src);
207 cap = (size == 0) ? 0 : (alloc * 100 / size);
208 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
210 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
211 ddt_get_pool_dedup_ratio(spa), src);
213 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
214 rvd->vdev_state, src);
216 version = spa_version(spa);
217 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
218 src = ZPROP_SRC_DEFAULT;
220 src = ZPROP_SRC_LOCAL;
221 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
225 dsl_dir_t *freedir = pool->dp_free_dir;
228 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
229 * when opening pools before this version freedir will be NULL.
231 if (freedir != NULL) {
232 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
233 freedir->dd_phys->dd_used_bytes, src);
235 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
240 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
242 if (spa->spa_comment != NULL) {
243 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
247 if (spa->spa_root != NULL)
248 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
251 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
252 if (dp->scd_path == NULL) {
253 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
254 "none", 0, ZPROP_SRC_LOCAL);
255 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
256 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
257 dp->scd_path, 0, ZPROP_SRC_LOCAL);
263 * Get zpool property values.
266 spa_prop_get(spa_t *spa, nvlist_t **nvp)
268 objset_t *mos = spa->spa_meta_objset;
273 err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_PUSHPAGE);
277 mutex_enter(&spa->spa_props_lock);
280 * Get properties from the spa config.
282 spa_prop_get_config(spa, nvp);
284 /* If no pool property object, no more prop to get. */
285 if (mos == NULL || spa->spa_pool_props_object == 0) {
286 mutex_exit(&spa->spa_props_lock);
291 * Get properties from the MOS pool property object.
293 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
294 (err = zap_cursor_retrieve(&zc, &za)) == 0;
295 zap_cursor_advance(&zc)) {
298 zprop_source_t src = ZPROP_SRC_DEFAULT;
301 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
304 switch (za.za_integer_length) {
306 /* integer property */
307 if (za.za_first_integer !=
308 zpool_prop_default_numeric(prop))
309 src = ZPROP_SRC_LOCAL;
311 if (prop == ZPOOL_PROP_BOOTFS) {
313 dsl_dataset_t *ds = NULL;
315 dp = spa_get_dsl(spa);
316 rw_enter(&dp->dp_config_rwlock, RW_READER);
317 if ((err = dsl_dataset_hold_obj(dp,
318 za.za_first_integer, FTAG, &ds))) {
319 rw_exit(&dp->dp_config_rwlock);
324 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
326 dsl_dataset_name(ds, strval);
327 dsl_dataset_rele(ds, FTAG);
328 rw_exit(&dp->dp_config_rwlock);
331 intval = za.za_first_integer;
334 spa_prop_add_list(*nvp, prop, strval, intval, src);
338 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
343 /* string property */
344 strval = kmem_alloc(za.za_num_integers, KM_PUSHPAGE);
345 err = zap_lookup(mos, spa->spa_pool_props_object,
346 za.za_name, 1, za.za_num_integers, strval);
348 kmem_free(strval, za.za_num_integers);
351 spa_prop_add_list(*nvp, prop, strval, 0, src);
352 kmem_free(strval, za.za_num_integers);
359 zap_cursor_fini(&zc);
360 mutex_exit(&spa->spa_props_lock);
362 if (err && err != ENOENT) {
372 * Validate the given pool properties nvlist and modify the list
373 * for the property values to be set.
376 spa_prop_validate(spa_t *spa, nvlist_t *props)
379 int error = 0, reset_bootfs = 0;
381 boolean_t has_feature = B_FALSE;
384 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
386 char *strval, *slash, *check, *fname;
387 const char *propname = nvpair_name(elem);
388 zpool_prop_t prop = zpool_name_to_prop(propname);
392 if (!zpool_prop_feature(propname)) {
398 * Sanitize the input.
400 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
405 if (nvpair_value_uint64(elem, &intval) != 0) {
415 fname = strchr(propname, '@') + 1;
416 if (zfeature_lookup_name(fname, NULL) != 0) {
421 has_feature = B_TRUE;
424 case ZPOOL_PROP_VERSION:
425 error = nvpair_value_uint64(elem, &intval);
427 (intval < spa_version(spa) ||
428 intval > SPA_VERSION_BEFORE_FEATURES ||
433 case ZPOOL_PROP_DELEGATION:
434 case ZPOOL_PROP_AUTOREPLACE:
435 case ZPOOL_PROP_LISTSNAPS:
436 case ZPOOL_PROP_AUTOEXPAND:
437 error = nvpair_value_uint64(elem, &intval);
438 if (!error && intval > 1)
442 case ZPOOL_PROP_BOOTFS:
444 * If the pool version is less than SPA_VERSION_BOOTFS,
445 * or the pool is still being created (version == 0),
446 * the bootfs property cannot be set.
448 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
454 * Make sure the vdev config is bootable
456 if (!vdev_is_bootable(spa->spa_root_vdev)) {
463 error = nvpair_value_string(elem, &strval);
469 if (strval == NULL || strval[0] == '\0') {
470 objnum = zpool_prop_default_numeric(
475 if ((error = dmu_objset_hold(strval,FTAG,&os)))
478 /* Must be ZPL and not gzip compressed. */
480 if (dmu_objset_type(os) != DMU_OST_ZFS) {
482 } else if ((error = dsl_prop_get_integer(strval,
483 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
484 &compress, NULL)) == 0 &&
485 !BOOTFS_COMPRESS_VALID(compress)) {
488 objnum = dmu_objset_id(os);
490 dmu_objset_rele(os, FTAG);
494 case ZPOOL_PROP_FAILUREMODE:
495 error = nvpair_value_uint64(elem, &intval);
496 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
497 intval > ZIO_FAILURE_MODE_PANIC))
501 * This is a special case which only occurs when
502 * the pool has completely failed. This allows
503 * the user to change the in-core failmode property
504 * without syncing it out to disk (I/Os might
505 * currently be blocked). We do this by returning
506 * EIO to the caller (spa_prop_set) to trick it
507 * into thinking we encountered a property validation
510 if (!error && spa_suspended(spa)) {
511 spa->spa_failmode = intval;
516 case ZPOOL_PROP_CACHEFILE:
517 if ((error = nvpair_value_string(elem, &strval)) != 0)
520 if (strval[0] == '\0')
523 if (strcmp(strval, "none") == 0)
526 if (strval[0] != '/') {
531 slash = strrchr(strval, '/');
532 ASSERT(slash != NULL);
534 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
535 strcmp(slash, "/..") == 0)
539 case ZPOOL_PROP_COMMENT:
540 if ((error = nvpair_value_string(elem, &strval)) != 0)
542 for (check = strval; *check != '\0'; check++) {
543 if (!isprint(*check)) {
549 if (strlen(strval) > ZPROP_MAX_COMMENT)
553 case ZPOOL_PROP_DEDUPDITTO:
554 if (spa_version(spa) < SPA_VERSION_DEDUP)
557 error = nvpair_value_uint64(elem, &intval);
559 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
571 if (!error && reset_bootfs) {
572 error = nvlist_remove(props,
573 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
576 error = nvlist_add_uint64(props,
577 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
585 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
588 spa_config_dirent_t *dp;
590 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
594 dp = kmem_alloc(sizeof (spa_config_dirent_t),
597 if (cachefile[0] == '\0')
598 dp->scd_path = spa_strdup(spa_config_path);
599 else if (strcmp(cachefile, "none") == 0)
602 dp->scd_path = spa_strdup(cachefile);
604 list_insert_head(&spa->spa_config_list, dp);
606 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
610 spa_prop_set(spa_t *spa, nvlist_t *nvp)
613 nvpair_t *elem = NULL;
614 boolean_t need_sync = B_FALSE;
616 if ((error = spa_prop_validate(spa, nvp)) != 0)
619 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
620 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
622 if (prop == ZPOOL_PROP_CACHEFILE ||
623 prop == ZPOOL_PROP_ALTROOT ||
624 prop == ZPOOL_PROP_READONLY)
627 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
630 if (prop == ZPOOL_PROP_VERSION) {
631 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
633 ASSERT(zpool_prop_feature(nvpair_name(elem)));
634 ver = SPA_VERSION_FEATURES;
638 /* Save time if the version is already set. */
639 if (ver == spa_version(spa))
643 * In addition to the pool directory object, we might
644 * create the pool properties object, the features for
645 * read object, the features for write object, or the
646 * feature descriptions object.
648 error = dsl_sync_task_do(spa_get_dsl(spa), NULL,
649 spa_sync_version, spa, &ver, 6);
660 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
668 * If the bootfs property value is dsobj, clear it.
671 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
673 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
674 VERIFY(zap_remove(spa->spa_meta_objset,
675 spa->spa_pool_props_object,
676 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
683 spa_change_guid_check(void *arg1, void *arg2, dmu_tx_t *tx)
686 vdev_t *rvd = spa->spa_root_vdev;
688 ASSERTV(uint64_t *newguid = arg2);
690 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
691 vdev_state = rvd->vdev_state;
692 spa_config_exit(spa, SCL_STATE, FTAG);
694 if (vdev_state != VDEV_STATE_HEALTHY)
697 ASSERT3U(spa_guid(spa), !=, *newguid);
703 spa_change_guid_sync(void *arg1, void *arg2, dmu_tx_t *tx)
706 uint64_t *newguid = arg2;
708 vdev_t *rvd = spa->spa_root_vdev;
710 oldguid = spa_guid(spa);
712 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
713 rvd->vdev_guid = *newguid;
714 rvd->vdev_guid_sum += (*newguid - oldguid);
715 vdev_config_dirty(rvd);
716 spa_config_exit(spa, SCL_STATE, FTAG);
718 spa_history_log_internal(LOG_POOL_GUID_CHANGE, spa, tx,
719 "old=%lld new=%lld", oldguid, *newguid);
723 * Change the GUID for the pool. This is done so that we can later
724 * re-import a pool built from a clone of our own vdevs. We will modify
725 * the root vdev's guid, our own pool guid, and then mark all of our
726 * vdevs dirty. Note that we must make sure that all our vdevs are
727 * online when we do this, or else any vdevs that weren't present
728 * would be orphaned from our pool. We are also going to issue a
729 * sysevent to update any watchers.
732 spa_change_guid(spa_t *spa)
737 mutex_enter(&spa_namespace_lock);
738 guid = spa_generate_guid(NULL);
740 error = dsl_sync_task_do(spa_get_dsl(spa), spa_change_guid_check,
741 spa_change_guid_sync, spa, &guid, 5);
744 spa_config_sync(spa, B_FALSE, B_TRUE);
745 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_REGUID);
748 mutex_exit(&spa_namespace_lock);
754 * ==========================================================================
755 * SPA state manipulation (open/create/destroy/import/export)
756 * ==========================================================================
760 spa_error_entry_compare(const void *a, const void *b)
762 spa_error_entry_t *sa = (spa_error_entry_t *)a;
763 spa_error_entry_t *sb = (spa_error_entry_t *)b;
766 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
767 sizeof (zbookmark_t));
778 * Utility function which retrieves copies of the current logs and
779 * re-initializes them in the process.
782 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
784 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
786 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
787 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
789 avl_create(&spa->spa_errlist_scrub,
790 spa_error_entry_compare, sizeof (spa_error_entry_t),
791 offsetof(spa_error_entry_t, se_avl));
792 avl_create(&spa->spa_errlist_last,
793 spa_error_entry_compare, sizeof (spa_error_entry_t),
794 offsetof(spa_error_entry_t, se_avl));
798 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
801 uint_t flags = TASKQ_PREPOPULATE;
802 boolean_t batch = B_FALSE;
806 return (NULL); /* no taskq needed */
809 ASSERT3U(value, >=, 1);
810 value = MAX(value, 1);
815 flags |= TASKQ_THREADS_CPU_PCT;
816 value = zio_taskq_batch_pct;
819 case zti_mode_online_percent:
820 flags |= TASKQ_THREADS_CPU_PCT;
824 panic("unrecognized mode for %s taskq (%u:%u) in "
830 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
832 flags |= TASKQ_DC_BATCH;
834 return (taskq_create_sysdc(name, value, 50, INT_MAX,
835 spa->spa_proc, zio_taskq_basedc, flags));
837 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
838 spa->spa_proc, flags));
842 spa_create_zio_taskqs(spa_t *spa)
846 for (t = 0; t < ZIO_TYPES; t++) {
847 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
848 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
849 enum zti_modes mode = ztip->zti_mode;
850 uint_t value = ztip->zti_value;
853 (void) snprintf(name, sizeof (name),
854 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
856 spa->spa_zio_taskq[t][q] =
857 spa_taskq_create(spa, name, mode, value);
862 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
864 spa_thread(void *arg)
869 user_t *pu = PTOU(curproc);
871 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
874 ASSERT(curproc != &p0);
875 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
876 "zpool-%s", spa->spa_name);
877 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
879 /* bind this thread to the requested psrset */
880 if (zio_taskq_psrset_bind != PS_NONE) {
882 mutex_enter(&cpu_lock);
883 mutex_enter(&pidlock);
884 mutex_enter(&curproc->p_lock);
886 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
887 0, NULL, NULL) == 0) {
888 curthread->t_bind_pset = zio_taskq_psrset_bind;
891 "Couldn't bind process for zfs pool \"%s\" to "
892 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
895 mutex_exit(&curproc->p_lock);
896 mutex_exit(&pidlock);
897 mutex_exit(&cpu_lock);
901 if (zio_taskq_sysdc) {
902 sysdc_thread_enter(curthread, 100, 0);
905 spa->spa_proc = curproc;
906 spa->spa_did = curthread->t_did;
908 spa_create_zio_taskqs(spa);
910 mutex_enter(&spa->spa_proc_lock);
911 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
913 spa->spa_proc_state = SPA_PROC_ACTIVE;
914 cv_broadcast(&spa->spa_proc_cv);
916 CALLB_CPR_SAFE_BEGIN(&cprinfo);
917 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
918 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
919 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
921 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
922 spa->spa_proc_state = SPA_PROC_GONE;
924 cv_broadcast(&spa->spa_proc_cv);
925 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
927 mutex_enter(&curproc->p_lock);
933 * Activate an uninitialized pool.
936 spa_activate(spa_t *spa, int mode)
938 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
940 spa->spa_state = POOL_STATE_ACTIVE;
941 spa->spa_mode = mode;
943 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
944 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
946 /* Try to create a covering process */
947 mutex_enter(&spa->spa_proc_lock);
948 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
949 ASSERT(spa->spa_proc == &p0);
952 #ifdef HAVE_SPA_THREAD
953 /* Only create a process if we're going to be around a while. */
954 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
955 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
957 spa->spa_proc_state = SPA_PROC_CREATED;
958 while (spa->spa_proc_state == SPA_PROC_CREATED) {
959 cv_wait(&spa->spa_proc_cv,
960 &spa->spa_proc_lock);
962 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
963 ASSERT(spa->spa_proc != &p0);
964 ASSERT(spa->spa_did != 0);
968 "Couldn't create process for zfs pool \"%s\"\n",
973 #endif /* HAVE_SPA_THREAD */
974 mutex_exit(&spa->spa_proc_lock);
976 /* If we didn't create a process, we need to create our taskqs. */
977 if (spa->spa_proc == &p0) {
978 spa_create_zio_taskqs(spa);
981 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
982 offsetof(vdev_t, vdev_config_dirty_node));
983 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
984 offsetof(vdev_t, vdev_state_dirty_node));
986 txg_list_create(&spa->spa_vdev_txg_list,
987 offsetof(struct vdev, vdev_txg_node));
989 avl_create(&spa->spa_errlist_scrub,
990 spa_error_entry_compare, sizeof (spa_error_entry_t),
991 offsetof(spa_error_entry_t, se_avl));
992 avl_create(&spa->spa_errlist_last,
993 spa_error_entry_compare, sizeof (spa_error_entry_t),
994 offsetof(spa_error_entry_t, se_avl));
998 * Opposite of spa_activate().
1001 spa_deactivate(spa_t *spa)
1005 ASSERT(spa->spa_sync_on == B_FALSE);
1006 ASSERT(spa->spa_dsl_pool == NULL);
1007 ASSERT(spa->spa_root_vdev == NULL);
1008 ASSERT(spa->spa_async_zio_root == NULL);
1009 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1011 txg_list_destroy(&spa->spa_vdev_txg_list);
1013 list_destroy(&spa->spa_config_dirty_list);
1014 list_destroy(&spa->spa_state_dirty_list);
1016 for (t = 0; t < ZIO_TYPES; t++) {
1017 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
1018 if (spa->spa_zio_taskq[t][q] != NULL)
1019 taskq_destroy(spa->spa_zio_taskq[t][q]);
1020 spa->spa_zio_taskq[t][q] = NULL;
1024 metaslab_class_destroy(spa->spa_normal_class);
1025 spa->spa_normal_class = NULL;
1027 metaslab_class_destroy(spa->spa_log_class);
1028 spa->spa_log_class = NULL;
1031 * If this was part of an import or the open otherwise failed, we may
1032 * still have errors left in the queues. Empty them just in case.
1034 spa_errlog_drain(spa);
1036 avl_destroy(&spa->spa_errlist_scrub);
1037 avl_destroy(&spa->spa_errlist_last);
1039 spa->spa_state = POOL_STATE_UNINITIALIZED;
1041 mutex_enter(&spa->spa_proc_lock);
1042 if (spa->spa_proc_state != SPA_PROC_NONE) {
1043 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1044 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1045 cv_broadcast(&spa->spa_proc_cv);
1046 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1047 ASSERT(spa->spa_proc != &p0);
1048 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1050 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1051 spa->spa_proc_state = SPA_PROC_NONE;
1053 ASSERT(spa->spa_proc == &p0);
1054 mutex_exit(&spa->spa_proc_lock);
1057 * We want to make sure spa_thread() has actually exited the ZFS
1058 * module, so that the module can't be unloaded out from underneath
1061 if (spa->spa_did != 0) {
1062 thread_join(spa->spa_did);
1068 * Verify a pool configuration, and construct the vdev tree appropriately. This
1069 * will create all the necessary vdevs in the appropriate layout, with each vdev
1070 * in the CLOSED state. This will prep the pool before open/creation/import.
1071 * All vdev validation is done by the vdev_alloc() routine.
1074 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1075 uint_t id, int atype)
1082 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1085 if ((*vdp)->vdev_ops->vdev_op_leaf)
1088 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1091 if (error == ENOENT)
1100 for (c = 0; c < children; c++) {
1102 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1110 ASSERT(*vdp != NULL);
1116 * Opposite of spa_load().
1119 spa_unload(spa_t *spa)
1123 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1128 spa_async_suspend(spa);
1133 if (spa->spa_sync_on) {
1134 txg_sync_stop(spa->spa_dsl_pool);
1135 spa->spa_sync_on = B_FALSE;
1139 * Wait for any outstanding async I/O to complete.
1141 if (spa->spa_async_zio_root != NULL) {
1142 (void) zio_wait(spa->spa_async_zio_root);
1143 spa->spa_async_zio_root = NULL;
1146 bpobj_close(&spa->spa_deferred_bpobj);
1149 * Close the dsl pool.
1151 if (spa->spa_dsl_pool) {
1152 dsl_pool_close(spa->spa_dsl_pool);
1153 spa->spa_dsl_pool = NULL;
1154 spa->spa_meta_objset = NULL;
1159 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1162 * Drop and purge level 2 cache
1164 spa_l2cache_drop(spa);
1169 if (spa->spa_root_vdev)
1170 vdev_free(spa->spa_root_vdev);
1171 ASSERT(spa->spa_root_vdev == NULL);
1173 for (i = 0; i < spa->spa_spares.sav_count; i++)
1174 vdev_free(spa->spa_spares.sav_vdevs[i]);
1175 if (spa->spa_spares.sav_vdevs) {
1176 kmem_free(spa->spa_spares.sav_vdevs,
1177 spa->spa_spares.sav_count * sizeof (void *));
1178 spa->spa_spares.sav_vdevs = NULL;
1180 if (spa->spa_spares.sav_config) {
1181 nvlist_free(spa->spa_spares.sav_config);
1182 spa->spa_spares.sav_config = NULL;
1184 spa->spa_spares.sav_count = 0;
1186 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1187 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1188 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1190 if (spa->spa_l2cache.sav_vdevs) {
1191 kmem_free(spa->spa_l2cache.sav_vdevs,
1192 spa->spa_l2cache.sav_count * sizeof (void *));
1193 spa->spa_l2cache.sav_vdevs = NULL;
1195 if (spa->spa_l2cache.sav_config) {
1196 nvlist_free(spa->spa_l2cache.sav_config);
1197 spa->spa_l2cache.sav_config = NULL;
1199 spa->spa_l2cache.sav_count = 0;
1201 spa->spa_async_suspended = 0;
1203 if (spa->spa_comment != NULL) {
1204 spa_strfree(spa->spa_comment);
1205 spa->spa_comment = NULL;
1208 spa_config_exit(spa, SCL_ALL, FTAG);
1212 * Load (or re-load) the current list of vdevs describing the active spares for
1213 * this pool. When this is called, we have some form of basic information in
1214 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1215 * then re-generate a more complete list including status information.
1218 spa_load_spares(spa_t *spa)
1225 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1228 * First, close and free any existing spare vdevs.
1230 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1231 vd = spa->spa_spares.sav_vdevs[i];
1233 /* Undo the call to spa_activate() below */
1234 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1235 B_FALSE)) != NULL && tvd->vdev_isspare)
1236 spa_spare_remove(tvd);
1241 if (spa->spa_spares.sav_vdevs)
1242 kmem_free(spa->spa_spares.sav_vdevs,
1243 spa->spa_spares.sav_count * sizeof (void *));
1245 if (spa->spa_spares.sav_config == NULL)
1248 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1249 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1251 spa->spa_spares.sav_count = (int)nspares;
1252 spa->spa_spares.sav_vdevs = NULL;
1258 * Construct the array of vdevs, opening them to get status in the
1259 * process. For each spare, there is potentially two different vdev_t
1260 * structures associated with it: one in the list of spares (used only
1261 * for basic validation purposes) and one in the active vdev
1262 * configuration (if it's spared in). During this phase we open and
1263 * validate each vdev on the spare list. If the vdev also exists in the
1264 * active configuration, then we also mark this vdev as an active spare.
1266 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1268 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1269 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1270 VDEV_ALLOC_SPARE) == 0);
1273 spa->spa_spares.sav_vdevs[i] = vd;
1275 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1276 B_FALSE)) != NULL) {
1277 if (!tvd->vdev_isspare)
1281 * We only mark the spare active if we were successfully
1282 * able to load the vdev. Otherwise, importing a pool
1283 * with a bad active spare would result in strange
1284 * behavior, because multiple pool would think the spare
1285 * is actively in use.
1287 * There is a vulnerability here to an equally bizarre
1288 * circumstance, where a dead active spare is later
1289 * brought back to life (onlined or otherwise). Given
1290 * the rarity of this scenario, and the extra complexity
1291 * it adds, we ignore the possibility.
1293 if (!vdev_is_dead(tvd))
1294 spa_spare_activate(tvd);
1298 vd->vdev_aux = &spa->spa_spares;
1300 if (vdev_open(vd) != 0)
1303 if (vdev_validate_aux(vd) == 0)
1308 * Recompute the stashed list of spares, with status information
1311 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1312 DATA_TYPE_NVLIST_ARRAY) == 0);
1314 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1316 for (i = 0; i < spa->spa_spares.sav_count; i++)
1317 spares[i] = vdev_config_generate(spa,
1318 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1319 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1320 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1321 for (i = 0; i < spa->spa_spares.sav_count; i++)
1322 nvlist_free(spares[i]);
1323 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1327 * Load (or re-load) the current list of vdevs describing the active l2cache for
1328 * this pool. When this is called, we have some form of basic information in
1329 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1330 * then re-generate a more complete list including status information.
1331 * Devices which are already active have their details maintained, and are
1335 spa_load_l2cache(spa_t *spa)
1339 int i, j, oldnvdevs;
1341 vdev_t *vd, **oldvdevs, **newvdevs = NULL;
1342 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1344 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1346 if (sav->sav_config != NULL) {
1347 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1348 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1349 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_PUSHPAGE);
1354 oldvdevs = sav->sav_vdevs;
1355 oldnvdevs = sav->sav_count;
1356 sav->sav_vdevs = NULL;
1360 * Process new nvlist of vdevs.
1362 for (i = 0; i < nl2cache; i++) {
1363 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1367 for (j = 0; j < oldnvdevs; j++) {
1369 if (vd != NULL && guid == vd->vdev_guid) {
1371 * Retain previous vdev for add/remove ops.
1379 if (newvdevs[i] == NULL) {
1383 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1384 VDEV_ALLOC_L2CACHE) == 0);
1389 * Commit this vdev as an l2cache device,
1390 * even if it fails to open.
1392 spa_l2cache_add(vd);
1397 spa_l2cache_activate(vd);
1399 if (vdev_open(vd) != 0)
1402 (void) vdev_validate_aux(vd);
1404 if (!vdev_is_dead(vd))
1405 l2arc_add_vdev(spa, vd);
1410 * Purge vdevs that were dropped
1412 for (i = 0; i < oldnvdevs; i++) {
1417 ASSERT(vd->vdev_isl2cache);
1419 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1420 pool != 0ULL && l2arc_vdev_present(vd))
1421 l2arc_remove_vdev(vd);
1422 vdev_clear_stats(vd);
1428 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1430 if (sav->sav_config == NULL)
1433 sav->sav_vdevs = newvdevs;
1434 sav->sav_count = (int)nl2cache;
1437 * Recompute the stashed list of l2cache devices, with status
1438 * information this time.
1440 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1441 DATA_TYPE_NVLIST_ARRAY) == 0);
1443 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE);
1444 for (i = 0; i < sav->sav_count; i++)
1445 l2cache[i] = vdev_config_generate(spa,
1446 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1447 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1448 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1450 for (i = 0; i < sav->sav_count; i++)
1451 nvlist_free(l2cache[i]);
1453 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1457 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1460 char *packed = NULL;
1465 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1469 nvsize = *(uint64_t *)db->db_data;
1470 dmu_buf_rele(db, FTAG);
1472 packed = kmem_alloc(nvsize, KM_PUSHPAGE | KM_NODEBUG);
1473 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1476 error = nvlist_unpack(packed, nvsize, value, 0);
1477 kmem_free(packed, nvsize);
1483 * Checks to see if the given vdev could not be opened, in which case we post a
1484 * sysevent to notify the autoreplace code that the device has been removed.
1487 spa_check_removed(vdev_t *vd)
1491 for (c = 0; c < vd->vdev_children; c++)
1492 spa_check_removed(vd->vdev_child[c]);
1494 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1495 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE,
1496 vd->vdev_spa, vd, NULL, 0, 0);
1497 spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_CHECK);
1502 * Validate the current config against the MOS config
1505 spa_config_valid(spa_t *spa, nvlist_t *config)
1507 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1511 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1513 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1514 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1516 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1519 * If we're doing a normal import, then build up any additional
1520 * diagnostic information about missing devices in this config.
1521 * We'll pass this up to the user for further processing.
1523 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1524 nvlist_t **child, *nv;
1527 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1529 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
1531 for (c = 0; c < rvd->vdev_children; c++) {
1532 vdev_t *tvd = rvd->vdev_child[c];
1533 vdev_t *mtvd = mrvd->vdev_child[c];
1535 if (tvd->vdev_ops == &vdev_missing_ops &&
1536 mtvd->vdev_ops != &vdev_missing_ops &&
1538 child[idx++] = vdev_config_generate(spa, mtvd,
1543 VERIFY(nvlist_add_nvlist_array(nv,
1544 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1545 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1546 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1548 for (i = 0; i < idx; i++)
1549 nvlist_free(child[i]);
1552 kmem_free(child, rvd->vdev_children * sizeof (char **));
1556 * Compare the root vdev tree with the information we have
1557 * from the MOS config (mrvd). Check each top-level vdev
1558 * with the corresponding MOS config top-level (mtvd).
1560 for (c = 0; c < rvd->vdev_children; c++) {
1561 vdev_t *tvd = rvd->vdev_child[c];
1562 vdev_t *mtvd = mrvd->vdev_child[c];
1565 * Resolve any "missing" vdevs in the current configuration.
1566 * If we find that the MOS config has more accurate information
1567 * about the top-level vdev then use that vdev instead.
1569 if (tvd->vdev_ops == &vdev_missing_ops &&
1570 mtvd->vdev_ops != &vdev_missing_ops) {
1572 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1576 * Device specific actions.
1578 if (mtvd->vdev_islog) {
1579 spa_set_log_state(spa, SPA_LOG_CLEAR);
1582 * XXX - once we have 'readonly' pool
1583 * support we should be able to handle
1584 * missing data devices by transitioning
1585 * the pool to readonly.
1591 * Swap the missing vdev with the data we were
1592 * able to obtain from the MOS config.
1594 vdev_remove_child(rvd, tvd);
1595 vdev_remove_child(mrvd, mtvd);
1597 vdev_add_child(rvd, mtvd);
1598 vdev_add_child(mrvd, tvd);
1600 spa_config_exit(spa, SCL_ALL, FTAG);
1602 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1605 } else if (mtvd->vdev_islog) {
1607 * Load the slog device's state from the MOS config
1608 * since it's possible that the label does not
1609 * contain the most up-to-date information.
1611 vdev_load_log_state(tvd, mtvd);
1616 spa_config_exit(spa, SCL_ALL, FTAG);
1619 * Ensure we were able to validate the config.
1621 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1625 * Check for missing log devices
1628 spa_check_logs(spa_t *spa)
1630 switch (spa->spa_log_state) {
1633 case SPA_LOG_MISSING:
1634 /* need to recheck in case slog has been restored */
1635 case SPA_LOG_UNKNOWN:
1636 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1637 DS_FIND_CHILDREN)) {
1638 spa_set_log_state(spa, SPA_LOG_MISSING);
1647 spa_passivate_log(spa_t *spa)
1649 vdev_t *rvd = spa->spa_root_vdev;
1650 boolean_t slog_found = B_FALSE;
1653 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1655 if (!spa_has_slogs(spa))
1658 for (c = 0; c < rvd->vdev_children; c++) {
1659 vdev_t *tvd = rvd->vdev_child[c];
1660 metaslab_group_t *mg = tvd->vdev_mg;
1662 if (tvd->vdev_islog) {
1663 metaslab_group_passivate(mg);
1664 slog_found = B_TRUE;
1668 return (slog_found);
1672 spa_activate_log(spa_t *spa)
1674 vdev_t *rvd = spa->spa_root_vdev;
1677 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1679 for (c = 0; c < rvd->vdev_children; c++) {
1680 vdev_t *tvd = rvd->vdev_child[c];
1681 metaslab_group_t *mg = tvd->vdev_mg;
1683 if (tvd->vdev_islog)
1684 metaslab_group_activate(mg);
1689 spa_offline_log(spa_t *spa)
1693 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1694 NULL, DS_FIND_CHILDREN)) == 0) {
1697 * We successfully offlined the log device, sync out the
1698 * current txg so that the "stubby" block can be removed
1701 txg_wait_synced(spa->spa_dsl_pool, 0);
1707 spa_aux_check_removed(spa_aux_vdev_t *sav)
1711 for (i = 0; i < sav->sav_count; i++)
1712 spa_check_removed(sav->sav_vdevs[i]);
1716 spa_claim_notify(zio_t *zio)
1718 spa_t *spa = zio->io_spa;
1723 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1724 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1725 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1726 mutex_exit(&spa->spa_props_lock);
1729 typedef struct spa_load_error {
1730 uint64_t sle_meta_count;
1731 uint64_t sle_data_count;
1735 spa_load_verify_done(zio_t *zio)
1737 blkptr_t *bp = zio->io_bp;
1738 spa_load_error_t *sle = zio->io_private;
1739 dmu_object_type_t type = BP_GET_TYPE(bp);
1740 int error = zio->io_error;
1743 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1744 type != DMU_OT_INTENT_LOG)
1745 atomic_add_64(&sle->sle_meta_count, 1);
1747 atomic_add_64(&sle->sle_data_count, 1);
1749 zio_data_buf_free(zio->io_data, zio->io_size);
1754 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1755 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1759 size_t size = BP_GET_PSIZE(bp);
1760 void *data = zio_data_buf_alloc(size);
1762 zio_nowait(zio_read(rio, spa, bp, data, size,
1763 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1764 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1765 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1771 spa_load_verify(spa_t *spa)
1774 spa_load_error_t sle = { 0 };
1775 zpool_rewind_policy_t policy;
1776 boolean_t verify_ok = B_FALSE;
1779 zpool_get_rewind_policy(spa->spa_config, &policy);
1781 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1784 rio = zio_root(spa, NULL, &sle,
1785 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1787 error = traverse_pool(spa, spa->spa_verify_min_txg,
1788 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1790 (void) zio_wait(rio);
1792 spa->spa_load_meta_errors = sle.sle_meta_count;
1793 spa->spa_load_data_errors = sle.sle_data_count;
1795 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1796 sle.sle_data_count <= policy.zrp_maxdata) {
1800 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1801 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1803 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1804 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1805 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1806 VERIFY(nvlist_add_int64(spa->spa_load_info,
1807 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1808 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1809 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1811 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1815 if (error != ENXIO && error != EIO)
1820 return (verify_ok ? 0 : EIO);
1824 * Find a value in the pool props object.
1827 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1829 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1830 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1834 * Find a value in the pool directory object.
1837 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1839 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1840 name, sizeof (uint64_t), 1, val));
1844 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1846 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1851 * Fix up config after a partly-completed split. This is done with the
1852 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1853 * pool have that entry in their config, but only the splitting one contains
1854 * a list of all the guids of the vdevs that are being split off.
1856 * This function determines what to do with that list: either rejoin
1857 * all the disks to the pool, or complete the splitting process. To attempt
1858 * the rejoin, each disk that is offlined is marked online again, and
1859 * we do a reopen() call. If the vdev label for every disk that was
1860 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1861 * then we call vdev_split() on each disk, and complete the split.
1863 * Otherwise we leave the config alone, with all the vdevs in place in
1864 * the original pool.
1867 spa_try_repair(spa_t *spa, nvlist_t *config)
1874 boolean_t attempt_reopen;
1876 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1879 /* check that the config is complete */
1880 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1881 &glist, &gcount) != 0)
1884 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_PUSHPAGE);
1886 /* attempt to online all the vdevs & validate */
1887 attempt_reopen = B_TRUE;
1888 for (i = 0; i < gcount; i++) {
1889 if (glist[i] == 0) /* vdev is hole */
1892 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1893 if (vd[i] == NULL) {
1895 * Don't bother attempting to reopen the disks;
1896 * just do the split.
1898 attempt_reopen = B_FALSE;
1900 /* attempt to re-online it */
1901 vd[i]->vdev_offline = B_FALSE;
1905 if (attempt_reopen) {
1906 vdev_reopen(spa->spa_root_vdev);
1908 /* check each device to see what state it's in */
1909 for (extracted = 0, i = 0; i < gcount; i++) {
1910 if (vd[i] != NULL &&
1911 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1918 * If every disk has been moved to the new pool, or if we never
1919 * even attempted to look at them, then we split them off for
1922 if (!attempt_reopen || gcount == extracted) {
1923 for (i = 0; i < gcount; i++)
1926 vdev_reopen(spa->spa_root_vdev);
1929 kmem_free(vd, gcount * sizeof (vdev_t *));
1933 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1934 boolean_t mosconfig)
1936 nvlist_t *config = spa->spa_config;
1937 char *ereport = FM_EREPORT_ZFS_POOL;
1943 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1946 ASSERT(spa->spa_comment == NULL);
1947 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1948 spa->spa_comment = spa_strdup(comment);
1951 * Versioning wasn't explicitly added to the label until later, so if
1952 * it's not present treat it as the initial version.
1954 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1955 &spa->spa_ubsync.ub_version) != 0)
1956 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1958 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1959 &spa->spa_config_txg);
1961 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1962 spa_guid_exists(pool_guid, 0)) {
1965 spa->spa_config_guid = pool_guid;
1967 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1969 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1973 nvlist_free(spa->spa_load_info);
1974 spa->spa_load_info = fnvlist_alloc();
1976 gethrestime(&spa->spa_loaded_ts);
1977 error = spa_load_impl(spa, pool_guid, config, state, type,
1978 mosconfig, &ereport);
1981 spa->spa_minref = refcount_count(&spa->spa_refcount);
1983 if (error != EEXIST) {
1984 spa->spa_loaded_ts.tv_sec = 0;
1985 spa->spa_loaded_ts.tv_nsec = 0;
1987 if (error != EBADF) {
1988 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1991 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
1998 * Load an existing storage pool, using the pool's builtin spa_config as a
1999 * source of configuration information.
2001 __attribute__((always_inline))
2003 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2004 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2008 nvlist_t *nvroot = NULL;
2011 uberblock_t *ub = &spa->spa_uberblock;
2012 uint64_t children, config_cache_txg = spa->spa_config_txg;
2013 int orig_mode = spa->spa_mode;
2016 boolean_t missing_feat_write = B_FALSE;
2019 * If this is an untrusted config, access the pool in read-only mode.
2020 * This prevents things like resilvering recently removed devices.
2023 spa->spa_mode = FREAD;
2025 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2027 spa->spa_load_state = state;
2029 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2032 parse = (type == SPA_IMPORT_EXISTING ?
2033 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2036 * Create "The Godfather" zio to hold all async IOs
2038 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2039 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2042 * Parse the configuration into a vdev tree. We explicitly set the
2043 * value that will be returned by spa_version() since parsing the
2044 * configuration requires knowing the version number.
2046 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2047 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2048 spa_config_exit(spa, SCL_ALL, FTAG);
2053 ASSERT(spa->spa_root_vdev == rvd);
2055 if (type != SPA_IMPORT_ASSEMBLE) {
2056 ASSERT(spa_guid(spa) == pool_guid);
2060 * Try to open all vdevs, loading each label in the process.
2062 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2063 error = vdev_open(rvd);
2064 spa_config_exit(spa, SCL_ALL, FTAG);
2069 * We need to validate the vdev labels against the configuration that
2070 * we have in hand, which is dependent on the setting of mosconfig. If
2071 * mosconfig is true then we're validating the vdev labels based on
2072 * that config. Otherwise, we're validating against the cached config
2073 * (zpool.cache) that was read when we loaded the zfs module, and then
2074 * later we will recursively call spa_load() and validate against
2077 * If we're assembling a new pool that's been split off from an
2078 * existing pool, the labels haven't yet been updated so we skip
2079 * validation for now.
2081 if (type != SPA_IMPORT_ASSEMBLE) {
2082 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2083 error = vdev_validate(rvd, mosconfig);
2084 spa_config_exit(spa, SCL_ALL, FTAG);
2089 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2094 * Find the best uberblock.
2096 vdev_uberblock_load(rvd, ub, &label);
2099 * If we weren't able to find a single valid uberblock, return failure.
2101 if (ub->ub_txg == 0) {
2103 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2107 * If the pool has an unsupported version we can't open it.
2109 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2111 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2114 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2118 * If we weren't able to find what's necessary for reading the
2119 * MOS in the label, return failure.
2121 if (label == NULL || nvlist_lookup_nvlist(label,
2122 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2124 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2129 * Update our in-core representation with the definitive values
2132 nvlist_free(spa->spa_label_features);
2133 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2139 * Look through entries in the label nvlist's features_for_read. If
2140 * there is a feature listed there which we don't understand then we
2141 * cannot open a pool.
2143 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2144 nvlist_t *unsup_feat;
2147 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2150 for (nvp = nvlist_next_nvpair(spa->spa_label_features, NULL);
2152 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2153 if (!zfeature_is_supported(nvpair_name(nvp))) {
2154 VERIFY(nvlist_add_string(unsup_feat,
2155 nvpair_name(nvp), "") == 0);
2159 if (!nvlist_empty(unsup_feat)) {
2160 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2161 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2162 nvlist_free(unsup_feat);
2163 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2167 nvlist_free(unsup_feat);
2171 * If the vdev guid sum doesn't match the uberblock, we have an
2172 * incomplete configuration. We first check to see if the pool
2173 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2174 * If it is, defer the vdev_guid_sum check till later so we
2175 * can handle missing vdevs.
2177 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2178 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2179 rvd->vdev_guid_sum != ub->ub_guid_sum)
2180 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2182 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2183 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2184 spa_try_repair(spa, config);
2185 spa_config_exit(spa, SCL_ALL, FTAG);
2186 nvlist_free(spa->spa_config_splitting);
2187 spa->spa_config_splitting = NULL;
2191 * Initialize internal SPA structures.
2193 spa->spa_state = POOL_STATE_ACTIVE;
2194 spa->spa_ubsync = spa->spa_uberblock;
2195 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2196 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2197 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2198 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2199 spa->spa_claim_max_txg = spa->spa_first_txg;
2200 spa->spa_prev_software_version = ub->ub_software_version;
2202 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2204 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2205 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2207 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2208 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2210 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2211 boolean_t missing_feat_read = B_FALSE;
2212 nvlist_t *unsup_feat;
2214 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2215 &spa->spa_feat_for_read_obj) != 0) {
2216 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2219 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2220 &spa->spa_feat_for_write_obj) != 0) {
2221 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2224 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2225 &spa->spa_feat_desc_obj) != 0) {
2226 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2229 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2232 if (!feature_is_supported(spa->spa_meta_objset,
2233 spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj,
2235 missing_feat_read = B_TRUE;
2237 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2238 if (!feature_is_supported(spa->spa_meta_objset,
2239 spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj,
2241 missing_feat_write = B_TRUE;
2244 if (!nvlist_empty(unsup_feat)) {
2245 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2246 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2249 nvlist_free(unsup_feat);
2251 if (!missing_feat_read) {
2252 fnvlist_add_boolean(spa->spa_load_info,
2253 ZPOOL_CONFIG_CAN_RDONLY);
2257 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2258 * twofold: to determine whether the pool is available for
2259 * import in read-write mode and (if it is not) whether the
2260 * pool is available for import in read-only mode. If the pool
2261 * is available for import in read-write mode, it is displayed
2262 * as available in userland; if it is not available for import
2263 * in read-only mode, it is displayed as unavailable in
2264 * userland. If the pool is available for import in read-only
2265 * mode but not read-write mode, it is displayed as unavailable
2266 * in userland with a special note that the pool is actually
2267 * available for open in read-only mode.
2269 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2270 * missing a feature for write, we must first determine whether
2271 * the pool can be opened read-only before returning to
2272 * userland in order to know whether to display the
2273 * abovementioned note.
2275 if (missing_feat_read || (missing_feat_write &&
2276 spa_writeable(spa))) {
2277 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2282 spa->spa_is_initializing = B_TRUE;
2283 error = dsl_pool_open(spa->spa_dsl_pool);
2284 spa->spa_is_initializing = B_FALSE;
2286 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2290 nvlist_t *policy = NULL, *nvconfig;
2292 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2293 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2295 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2296 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2298 unsigned long myhostid = 0;
2300 VERIFY(nvlist_lookup_string(nvconfig,
2301 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2304 myhostid = zone_get_hostid(NULL);
2307 * We're emulating the system's hostid in userland, so
2308 * we can't use zone_get_hostid().
2310 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2311 #endif /* _KERNEL */
2312 if (hostid != 0 && myhostid != 0 &&
2313 hostid != myhostid) {
2314 nvlist_free(nvconfig);
2315 cmn_err(CE_WARN, "pool '%s' could not be "
2316 "loaded as it was last accessed by "
2317 "another system (host: %s hostid: 0x%lx). "
2318 "See: http://zfsonlinux.org/msg/ZFS-8000-EY",
2319 spa_name(spa), hostname,
2320 (unsigned long)hostid);
2324 if (nvlist_lookup_nvlist(spa->spa_config,
2325 ZPOOL_REWIND_POLICY, &policy) == 0)
2326 VERIFY(nvlist_add_nvlist(nvconfig,
2327 ZPOOL_REWIND_POLICY, policy) == 0);
2329 spa_config_set(spa, nvconfig);
2331 spa_deactivate(spa);
2332 spa_activate(spa, orig_mode);
2334 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2337 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2338 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2339 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2341 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2344 * Load the bit that tells us to use the new accounting function
2345 * (raid-z deflation). If we have an older pool, this will not
2348 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2349 if (error != 0 && error != ENOENT)
2350 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2352 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2353 &spa->spa_creation_version);
2354 if (error != 0 && error != ENOENT)
2355 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2358 * Load the persistent error log. If we have an older pool, this will
2361 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2362 if (error != 0 && error != ENOENT)
2363 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2365 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2366 &spa->spa_errlog_scrub);
2367 if (error != 0 && error != ENOENT)
2368 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2371 * Load the history object. If we have an older pool, this
2372 * will not be present.
2374 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2375 if (error != 0 && error != ENOENT)
2376 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2379 * If we're assembling the pool from the split-off vdevs of
2380 * an existing pool, we don't want to attach the spares & cache
2385 * Load any hot spares for this pool.
2387 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2388 if (error != 0 && error != ENOENT)
2389 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2390 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2391 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2392 if (load_nvlist(spa, spa->spa_spares.sav_object,
2393 &spa->spa_spares.sav_config) != 0)
2394 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2396 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2397 spa_load_spares(spa);
2398 spa_config_exit(spa, SCL_ALL, FTAG);
2399 } else if (error == 0) {
2400 spa->spa_spares.sav_sync = B_TRUE;
2404 * Load any level 2 ARC devices for this pool.
2406 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2407 &spa->spa_l2cache.sav_object);
2408 if (error != 0 && error != ENOENT)
2409 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2410 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2411 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2412 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2413 &spa->spa_l2cache.sav_config) != 0)
2414 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2416 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2417 spa_load_l2cache(spa);
2418 spa_config_exit(spa, SCL_ALL, FTAG);
2419 } else if (error == 0) {
2420 spa->spa_l2cache.sav_sync = B_TRUE;
2423 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2425 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2426 if (error && error != ENOENT)
2427 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2430 uint64_t autoreplace;
2432 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2433 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2434 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2435 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2436 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2437 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2438 &spa->spa_dedup_ditto);
2440 spa->spa_autoreplace = (autoreplace != 0);
2444 * If the 'autoreplace' property is set, then post a resource notifying
2445 * the ZFS DE that it should not issue any faults for unopenable
2446 * devices. We also iterate over the vdevs, and post a sysevent for any
2447 * unopenable vdevs so that the normal autoreplace handler can take
2450 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2451 spa_check_removed(spa->spa_root_vdev);
2453 * For the import case, this is done in spa_import(), because
2454 * at this point we're using the spare definitions from
2455 * the MOS config, not necessarily from the userland config.
2457 if (state != SPA_LOAD_IMPORT) {
2458 spa_aux_check_removed(&spa->spa_spares);
2459 spa_aux_check_removed(&spa->spa_l2cache);
2464 * Load the vdev state for all toplevel vdevs.
2469 * Propagate the leaf DTLs we just loaded all the way up the tree.
2471 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2472 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2473 spa_config_exit(spa, SCL_ALL, FTAG);
2476 * Load the DDTs (dedup tables).
2478 error = ddt_load(spa);
2480 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2482 spa_update_dspace(spa);
2485 * Validate the config, using the MOS config to fill in any
2486 * information which might be missing. If we fail to validate
2487 * the config then declare the pool unfit for use. If we're
2488 * assembling a pool from a split, the log is not transferred
2491 if (type != SPA_IMPORT_ASSEMBLE) {
2494 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2495 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2497 if (!spa_config_valid(spa, nvconfig)) {
2498 nvlist_free(nvconfig);
2499 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2502 nvlist_free(nvconfig);
2505 * Now that we've validated the config, check the state of the
2506 * root vdev. If it can't be opened, it indicates one or
2507 * more toplevel vdevs are faulted.
2509 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2512 if (spa_check_logs(spa)) {
2513 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2514 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2518 if (missing_feat_write) {
2519 ASSERT(state == SPA_LOAD_TRYIMPORT);
2522 * At this point, we know that we can open the pool in
2523 * read-only mode but not read-write mode. We now have enough
2524 * information and can return to userland.
2526 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2530 * We've successfully opened the pool, verify that we're ready
2531 * to start pushing transactions.
2533 if (state != SPA_LOAD_TRYIMPORT) {
2534 if ((error = spa_load_verify(spa)))
2535 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2539 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2540 spa->spa_load_max_txg == UINT64_MAX)) {
2542 int need_update = B_FALSE;
2545 ASSERT(state != SPA_LOAD_TRYIMPORT);
2548 * Claim log blocks that haven't been committed yet.
2549 * This must all happen in a single txg.
2550 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2551 * invoked from zil_claim_log_block()'s i/o done callback.
2552 * Price of rollback is that we abandon the log.
2554 spa->spa_claiming = B_TRUE;
2556 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2557 spa_first_txg(spa));
2558 (void) dmu_objset_find(spa_name(spa),
2559 zil_claim, tx, DS_FIND_CHILDREN);
2562 spa->spa_claiming = B_FALSE;
2564 spa_set_log_state(spa, SPA_LOG_GOOD);
2565 spa->spa_sync_on = B_TRUE;
2566 txg_sync_start(spa->spa_dsl_pool);
2569 * Wait for all claims to sync. We sync up to the highest
2570 * claimed log block birth time so that claimed log blocks
2571 * don't appear to be from the future. spa_claim_max_txg
2572 * will have been set for us by either zil_check_log_chain()
2573 * (invoked from spa_check_logs()) or zil_claim() above.
2575 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2578 * If the config cache is stale, or we have uninitialized
2579 * metaslabs (see spa_vdev_add()), then update the config.
2581 * If this is a verbatim import, trust the current
2582 * in-core spa_config and update the disk labels.
2584 if (config_cache_txg != spa->spa_config_txg ||
2585 state == SPA_LOAD_IMPORT ||
2586 state == SPA_LOAD_RECOVER ||
2587 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2588 need_update = B_TRUE;
2590 for (c = 0; c < rvd->vdev_children; c++)
2591 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2592 need_update = B_TRUE;
2595 * Update the config cache asychronously in case we're the
2596 * root pool, in which case the config cache isn't writable yet.
2599 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2602 * Check all DTLs to see if anything needs resilvering.
2604 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2605 vdev_resilver_needed(rvd, NULL, NULL))
2606 spa_async_request(spa, SPA_ASYNC_RESILVER);
2609 * Delete any inconsistent datasets.
2611 (void) dmu_objset_find(spa_name(spa),
2612 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2615 * Clean up any stale temporary dataset userrefs.
2617 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2624 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2626 int mode = spa->spa_mode;
2629 spa_deactivate(spa);
2631 spa->spa_load_max_txg--;
2633 spa_activate(spa, mode);
2634 spa_async_suspend(spa);
2636 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2640 * If spa_load() fails this function will try loading prior txg's. If
2641 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2642 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2643 * function will not rewind the pool and will return the same error as
2647 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2648 uint64_t max_request, int rewind_flags)
2650 nvlist_t *loadinfo = NULL;
2651 nvlist_t *config = NULL;
2652 int load_error, rewind_error;
2653 uint64_t safe_rewind_txg;
2656 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2657 spa->spa_load_max_txg = spa->spa_load_txg;
2658 spa_set_log_state(spa, SPA_LOG_CLEAR);
2660 spa->spa_load_max_txg = max_request;
2663 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2665 if (load_error == 0)
2668 if (spa->spa_root_vdev != NULL)
2669 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2671 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2672 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2674 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2675 nvlist_free(config);
2676 return (load_error);
2679 if (state == SPA_LOAD_RECOVER) {
2680 /* Price of rolling back is discarding txgs, including log */
2681 spa_set_log_state(spa, SPA_LOG_CLEAR);
2684 * If we aren't rolling back save the load info from our first
2685 * import attempt so that we can restore it after attempting
2688 loadinfo = spa->spa_load_info;
2689 spa->spa_load_info = fnvlist_alloc();
2692 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2693 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2694 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2695 TXG_INITIAL : safe_rewind_txg;
2698 * Continue as long as we're finding errors, we're still within
2699 * the acceptable rewind range, and we're still finding uberblocks
2701 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2702 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2703 if (spa->spa_load_max_txg < safe_rewind_txg)
2704 spa->spa_extreme_rewind = B_TRUE;
2705 rewind_error = spa_load_retry(spa, state, mosconfig);
2708 spa->spa_extreme_rewind = B_FALSE;
2709 spa->spa_load_max_txg = UINT64_MAX;
2711 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2712 spa_config_set(spa, config);
2714 if (state == SPA_LOAD_RECOVER) {
2715 ASSERT3P(loadinfo, ==, NULL);
2716 return (rewind_error);
2718 /* Store the rewind info as part of the initial load info */
2719 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2720 spa->spa_load_info);
2722 /* Restore the initial load info */
2723 fnvlist_free(spa->spa_load_info);
2724 spa->spa_load_info = loadinfo;
2726 return (load_error);
2733 * The import case is identical to an open except that the configuration is sent
2734 * down from userland, instead of grabbed from the configuration cache. For the
2735 * case of an open, the pool configuration will exist in the
2736 * POOL_STATE_UNINITIALIZED state.
2738 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2739 * the same time open the pool, without having to keep around the spa_t in some
2743 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2747 spa_load_state_t state = SPA_LOAD_OPEN;
2749 int locked = B_FALSE;
2754 * As disgusting as this is, we need to support recursive calls to this
2755 * function because dsl_dir_open() is called during spa_load(), and ends
2756 * up calling spa_open() again. The real fix is to figure out how to
2757 * avoid dsl_dir_open() calling this in the first place.
2759 if (mutex_owner(&spa_namespace_lock) != curthread) {
2760 mutex_enter(&spa_namespace_lock);
2764 if ((spa = spa_lookup(pool)) == NULL) {
2766 mutex_exit(&spa_namespace_lock);
2770 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2771 zpool_rewind_policy_t policy;
2773 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2775 if (policy.zrp_request & ZPOOL_DO_REWIND)
2776 state = SPA_LOAD_RECOVER;
2778 spa_activate(spa, spa_mode_global);
2780 if (state != SPA_LOAD_RECOVER)
2781 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2783 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2784 policy.zrp_request);
2786 if (error == EBADF) {
2788 * If vdev_validate() returns failure (indicated by
2789 * EBADF), it indicates that one of the vdevs indicates
2790 * that the pool has been exported or destroyed. If
2791 * this is the case, the config cache is out of sync and
2792 * we should remove the pool from the namespace.
2795 spa_deactivate(spa);
2796 spa_config_sync(spa, B_TRUE, B_TRUE);
2799 mutex_exit(&spa_namespace_lock);
2805 * We can't open the pool, but we still have useful
2806 * information: the state of each vdev after the
2807 * attempted vdev_open(). Return this to the user.
2809 if (config != NULL && spa->spa_config) {
2810 VERIFY(nvlist_dup(spa->spa_config, config,
2812 VERIFY(nvlist_add_nvlist(*config,
2813 ZPOOL_CONFIG_LOAD_INFO,
2814 spa->spa_load_info) == 0);
2817 spa_deactivate(spa);
2818 spa->spa_last_open_failed = error;
2820 mutex_exit(&spa_namespace_lock);
2826 spa_open_ref(spa, tag);
2829 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2832 * If we've recovered the pool, pass back any information we
2833 * gathered while doing the load.
2835 if (state == SPA_LOAD_RECOVER) {
2836 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2837 spa->spa_load_info) == 0);
2841 spa->spa_last_open_failed = 0;
2842 spa->spa_last_ubsync_txg = 0;
2843 spa->spa_load_txg = 0;
2844 mutex_exit(&spa_namespace_lock);
2853 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2856 return (spa_open_common(name, spapp, tag, policy, config));
2860 spa_open(const char *name, spa_t **spapp, void *tag)
2862 return (spa_open_common(name, spapp, tag, NULL, NULL));
2866 * Lookup the given spa_t, incrementing the inject count in the process,
2867 * preventing it from being exported or destroyed.
2870 spa_inject_addref(char *name)
2874 mutex_enter(&spa_namespace_lock);
2875 if ((spa = spa_lookup(name)) == NULL) {
2876 mutex_exit(&spa_namespace_lock);
2879 spa->spa_inject_ref++;
2880 mutex_exit(&spa_namespace_lock);
2886 spa_inject_delref(spa_t *spa)
2888 mutex_enter(&spa_namespace_lock);
2889 spa->spa_inject_ref--;
2890 mutex_exit(&spa_namespace_lock);
2894 * Add spares device information to the nvlist.
2897 spa_add_spares(spa_t *spa, nvlist_t *config)
2907 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2909 if (spa->spa_spares.sav_count == 0)
2912 VERIFY(nvlist_lookup_nvlist(config,
2913 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2914 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2915 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2917 VERIFY(nvlist_add_nvlist_array(nvroot,
2918 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2919 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2920 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2923 * Go through and find any spares which have since been
2924 * repurposed as an active spare. If this is the case, update
2925 * their status appropriately.
2927 for (i = 0; i < nspares; i++) {
2928 VERIFY(nvlist_lookup_uint64(spares[i],
2929 ZPOOL_CONFIG_GUID, &guid) == 0);
2930 if (spa_spare_exists(guid, &pool, NULL) &&
2932 VERIFY(nvlist_lookup_uint64_array(
2933 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2934 (uint64_t **)&vs, &vsc) == 0);
2935 vs->vs_state = VDEV_STATE_CANT_OPEN;
2936 vs->vs_aux = VDEV_AUX_SPARED;
2943 * Add l2cache device information to the nvlist, including vdev stats.
2946 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2949 uint_t i, j, nl2cache;
2956 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2958 if (spa->spa_l2cache.sav_count == 0)
2961 VERIFY(nvlist_lookup_nvlist(config,
2962 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2963 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2964 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2965 if (nl2cache != 0) {
2966 VERIFY(nvlist_add_nvlist_array(nvroot,
2967 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2968 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2969 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2972 * Update level 2 cache device stats.
2975 for (i = 0; i < nl2cache; i++) {
2976 VERIFY(nvlist_lookup_uint64(l2cache[i],
2977 ZPOOL_CONFIG_GUID, &guid) == 0);
2980 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2982 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2983 vd = spa->spa_l2cache.sav_vdevs[j];
2989 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2990 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2992 vdev_get_stats(vd, vs);
2998 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3004 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3005 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3007 if (spa->spa_feat_for_read_obj != 0) {
3008 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3009 spa->spa_feat_for_read_obj);
3010 zap_cursor_retrieve(&zc, &za) == 0;
3011 zap_cursor_advance(&zc)) {
3012 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3013 za.za_num_integers == 1);
3014 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3015 za.za_first_integer));
3017 zap_cursor_fini(&zc);
3020 if (spa->spa_feat_for_write_obj != 0) {
3021 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3022 spa->spa_feat_for_write_obj);
3023 zap_cursor_retrieve(&zc, &za) == 0;
3024 zap_cursor_advance(&zc)) {
3025 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3026 za.za_num_integers == 1);
3027 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3028 za.za_first_integer));
3030 zap_cursor_fini(&zc);
3033 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3035 nvlist_free(features);
3039 spa_get_stats(const char *name, nvlist_t **config,
3040 char *altroot, size_t buflen)
3046 error = spa_open_common(name, &spa, FTAG, NULL, config);
3050 * This still leaves a window of inconsistency where the spares
3051 * or l2cache devices could change and the config would be
3052 * self-inconsistent.
3054 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3056 if (*config != NULL) {
3057 uint64_t loadtimes[2];
3059 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3060 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3061 VERIFY(nvlist_add_uint64_array(*config,
3062 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3064 VERIFY(nvlist_add_uint64(*config,
3065 ZPOOL_CONFIG_ERRCOUNT,
3066 spa_get_errlog_size(spa)) == 0);
3068 if (spa_suspended(spa))
3069 VERIFY(nvlist_add_uint64(*config,
3070 ZPOOL_CONFIG_SUSPENDED,
3071 spa->spa_failmode) == 0);
3073 spa_add_spares(spa, *config);
3074 spa_add_l2cache(spa, *config);
3075 spa_add_feature_stats(spa, *config);
3080 * We want to get the alternate root even for faulted pools, so we cheat
3081 * and call spa_lookup() directly.
3085 mutex_enter(&spa_namespace_lock);
3086 spa = spa_lookup(name);
3088 spa_altroot(spa, altroot, buflen);
3092 mutex_exit(&spa_namespace_lock);
3094 spa_altroot(spa, altroot, buflen);
3099 spa_config_exit(spa, SCL_CONFIG, FTAG);
3100 spa_close(spa, FTAG);
3107 * Validate that the auxiliary device array is well formed. We must have an
3108 * array of nvlists, each which describes a valid leaf vdev. If this is an
3109 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3110 * specified, as long as they are well-formed.
3113 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3114 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3115 vdev_labeltype_t label)
3122 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3125 * It's acceptable to have no devs specified.
3127 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3134 * Make sure the pool is formatted with a version that supports this
3137 if (spa_version(spa) < version)
3141 * Set the pending device list so we correctly handle device in-use
3144 sav->sav_pending = dev;
3145 sav->sav_npending = ndev;
3147 for (i = 0; i < ndev; i++) {
3148 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3152 if (!vd->vdev_ops->vdev_op_leaf) {
3159 * The L2ARC currently only supports disk devices in
3160 * kernel context. For user-level testing, we allow it.
3163 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3164 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3172 if ((error = vdev_open(vd)) == 0 &&
3173 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3174 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3175 vd->vdev_guid) == 0);
3181 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3188 sav->sav_pending = NULL;
3189 sav->sav_npending = 0;
3194 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3198 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3200 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3201 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3202 VDEV_LABEL_SPARE)) != 0) {
3206 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3207 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3208 VDEV_LABEL_L2CACHE));
3212 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3217 if (sav->sav_config != NULL) {
3223 * Generate new dev list by concatentating with the
3226 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3227 &olddevs, &oldndevs) == 0);
3229 newdevs = kmem_alloc(sizeof (void *) *
3230 (ndevs + oldndevs), KM_PUSHPAGE);
3231 for (i = 0; i < oldndevs; i++)
3232 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3234 for (i = 0; i < ndevs; i++)
3235 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3238 VERIFY(nvlist_remove(sav->sav_config, config,
3239 DATA_TYPE_NVLIST_ARRAY) == 0);
3241 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3242 config, newdevs, ndevs + oldndevs) == 0);
3243 for (i = 0; i < oldndevs + ndevs; i++)
3244 nvlist_free(newdevs[i]);
3245 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3248 * Generate a new dev list.
3250 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3252 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3258 * Stop and drop level 2 ARC devices
3261 spa_l2cache_drop(spa_t *spa)
3265 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3267 for (i = 0; i < sav->sav_count; i++) {
3270 vd = sav->sav_vdevs[i];
3273 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3274 pool != 0ULL && l2arc_vdev_present(vd))
3275 l2arc_remove_vdev(vd);
3283 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3284 const char *history_str, nvlist_t *zplprops)
3287 char *altroot = NULL;
3292 uint64_t txg = TXG_INITIAL;
3293 nvlist_t **spares, **l2cache;
3294 uint_t nspares, nl2cache;
3295 uint64_t version, obj;
3296 boolean_t has_features;
3301 * If this pool already exists, return failure.
3303 mutex_enter(&spa_namespace_lock);
3304 if (spa_lookup(pool) != NULL) {
3305 mutex_exit(&spa_namespace_lock);
3310 * Allocate a new spa_t structure.
3312 (void) nvlist_lookup_string(props,
3313 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3314 spa = spa_add(pool, NULL, altroot);
3315 spa_activate(spa, spa_mode_global);
3317 if (props && (error = spa_prop_validate(spa, props))) {
3318 spa_deactivate(spa);
3320 mutex_exit(&spa_namespace_lock);
3324 has_features = B_FALSE;
3325 for (elem = nvlist_next_nvpair(props, NULL);
3326 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3327 if (zpool_prop_feature(nvpair_name(elem)))
3328 has_features = B_TRUE;
3331 if (has_features || nvlist_lookup_uint64(props,
3332 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3333 version = SPA_VERSION;
3335 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3337 spa->spa_first_txg = txg;
3338 spa->spa_uberblock.ub_txg = txg - 1;
3339 spa->spa_uberblock.ub_version = version;
3340 spa->spa_ubsync = spa->spa_uberblock;
3343 * Create "The Godfather" zio to hold all async IOs
3345 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3346 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3349 * Create the root vdev.
3351 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3353 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3355 ASSERT(error != 0 || rvd != NULL);
3356 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3358 if (error == 0 && !zfs_allocatable_devs(nvroot))
3362 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3363 (error = spa_validate_aux(spa, nvroot, txg,
3364 VDEV_ALLOC_ADD)) == 0) {
3365 for (c = 0; c < rvd->vdev_children; c++) {
3366 vdev_metaslab_set_size(rvd->vdev_child[c]);
3367 vdev_expand(rvd->vdev_child[c], txg);
3371 spa_config_exit(spa, SCL_ALL, FTAG);
3375 spa_deactivate(spa);
3377 mutex_exit(&spa_namespace_lock);
3382 * Get the list of spares, if specified.
3384 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3385 &spares, &nspares) == 0) {
3386 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3388 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3389 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3390 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3391 spa_load_spares(spa);
3392 spa_config_exit(spa, SCL_ALL, FTAG);
3393 spa->spa_spares.sav_sync = B_TRUE;
3397 * Get the list of level 2 cache devices, if specified.
3399 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3400 &l2cache, &nl2cache) == 0) {
3401 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3402 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3403 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3404 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3405 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3406 spa_load_l2cache(spa);
3407 spa_config_exit(spa, SCL_ALL, FTAG);
3408 spa->spa_l2cache.sav_sync = B_TRUE;
3411 spa->spa_is_initializing = B_TRUE;
3412 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3413 spa->spa_meta_objset = dp->dp_meta_objset;
3414 spa->spa_is_initializing = B_FALSE;
3417 * Create DDTs (dedup tables).
3421 spa_update_dspace(spa);
3423 tx = dmu_tx_create_assigned(dp, txg);
3426 * Create the pool config object.
3428 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3429 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3430 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3432 if (zap_add(spa->spa_meta_objset,
3433 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3434 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3435 cmn_err(CE_PANIC, "failed to add pool config");
3438 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3439 spa_feature_create_zap_objects(spa, tx);
3441 if (zap_add(spa->spa_meta_objset,
3442 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3443 sizeof (uint64_t), 1, &version, tx) != 0) {
3444 cmn_err(CE_PANIC, "failed to add pool version");
3447 /* Newly created pools with the right version are always deflated. */
3448 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3449 spa->spa_deflate = TRUE;
3450 if (zap_add(spa->spa_meta_objset,
3451 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3452 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3453 cmn_err(CE_PANIC, "failed to add deflate");
3458 * Create the deferred-free bpobj. Turn off compression
3459 * because sync-to-convergence takes longer if the blocksize
3462 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3463 dmu_object_set_compress(spa->spa_meta_objset, obj,
3464 ZIO_COMPRESS_OFF, tx);
3465 if (zap_add(spa->spa_meta_objset,
3466 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3467 sizeof (uint64_t), 1, &obj, tx) != 0) {
3468 cmn_err(CE_PANIC, "failed to add bpobj");
3470 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3471 spa->spa_meta_objset, obj));
3474 * Create the pool's history object.
3476 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3477 spa_history_create_obj(spa, tx);
3480 * Set pool properties.
3482 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3483 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3484 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3485 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3487 if (props != NULL) {
3488 spa_configfile_set(spa, props, B_FALSE);
3489 spa_sync_props(spa, props, tx);
3494 spa->spa_sync_on = B_TRUE;
3495 txg_sync_start(spa->spa_dsl_pool);
3498 * We explicitly wait for the first transaction to complete so that our
3499 * bean counters are appropriately updated.
3501 txg_wait_synced(spa->spa_dsl_pool, txg);
3503 spa_config_sync(spa, B_FALSE, B_TRUE);
3505 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3506 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3507 spa_history_log_version(spa, LOG_POOL_CREATE);
3509 spa->spa_minref = refcount_count(&spa->spa_refcount);
3511 mutex_exit(&spa_namespace_lock);
3518 * Get the root pool information from the root disk, then import the root pool
3519 * during the system boot up time.
3521 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3524 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3527 nvlist_t *nvtop, *nvroot;
3530 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3534 * Add this top-level vdev to the child array.
3536 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3538 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3540 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3543 * Put this pool's top-level vdevs into a root vdev.
3545 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3546 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3547 VDEV_TYPE_ROOT) == 0);
3548 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3549 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3550 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3554 * Replace the existing vdev_tree with the new root vdev in
3555 * this pool's configuration (remove the old, add the new).
3557 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3558 nvlist_free(nvroot);
3563 * Walk the vdev tree and see if we can find a device with "better"
3564 * configuration. A configuration is "better" if the label on that
3565 * device has a more recent txg.
3568 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3572 for (c = 0; c < vd->vdev_children; c++)
3573 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3575 if (vd->vdev_ops->vdev_op_leaf) {
3579 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3583 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3587 * Do we have a better boot device?
3589 if (label_txg > *txg) {
3598 * Import a root pool.
3600 * For x86. devpath_list will consist of devid and/or physpath name of
3601 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3602 * The GRUB "findroot" command will return the vdev we should boot.
3604 * For Sparc, devpath_list consists the physpath name of the booting device
3605 * no matter the rootpool is a single device pool or a mirrored pool.
3607 * "/pci@1f,0/ide@d/disk@0,0:a"
3610 spa_import_rootpool(char *devpath, char *devid)
3613 vdev_t *rvd, *bvd, *avd = NULL;
3614 nvlist_t *config, *nvtop;
3620 * Read the label from the boot device and generate a configuration.
3622 config = spa_generate_rootconf(devpath, devid, &guid);
3623 #if defined(_OBP) && defined(_KERNEL)
3624 if (config == NULL) {
3625 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3627 get_iscsi_bootpath_phy(devpath);
3628 config = spa_generate_rootconf(devpath, devid, &guid);
3632 if (config == NULL) {
3633 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3638 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3640 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3642 mutex_enter(&spa_namespace_lock);
3643 if ((spa = spa_lookup(pname)) != NULL) {
3645 * Remove the existing root pool from the namespace so that we
3646 * can replace it with the correct config we just read in.
3651 spa = spa_add(pname, config, NULL);
3652 spa->spa_is_root = B_TRUE;
3653 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3656 * Build up a vdev tree based on the boot device's label config.
3658 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3660 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3661 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3662 VDEV_ALLOC_ROOTPOOL);
3663 spa_config_exit(spa, SCL_ALL, FTAG);
3665 mutex_exit(&spa_namespace_lock);
3666 nvlist_free(config);
3667 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3673 * Get the boot vdev.
3675 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3676 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3677 (u_longlong_t)guid);
3683 * Determine if there is a better boot device.
3686 spa_alt_rootvdev(rvd, &avd, &txg);
3688 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3689 "try booting from '%s'", avd->vdev_path);
3695 * If the boot device is part of a spare vdev then ensure that
3696 * we're booting off the active spare.
3698 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3699 !bvd->vdev_isspare) {
3700 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3701 "try booting from '%s'",
3703 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3709 spa_history_log_version(spa, LOG_POOL_IMPORT);
3711 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3713 spa_config_exit(spa, SCL_ALL, FTAG);
3714 mutex_exit(&spa_namespace_lock);
3716 nvlist_free(config);
3723 * Import a non-root pool into the system.
3726 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3729 char *altroot = NULL;
3730 spa_load_state_t state = SPA_LOAD_IMPORT;
3731 zpool_rewind_policy_t policy;
3732 uint64_t mode = spa_mode_global;
3733 uint64_t readonly = B_FALSE;
3736 nvlist_t **spares, **l2cache;
3737 uint_t nspares, nl2cache;
3740 * If a pool with this name exists, return failure.
3742 mutex_enter(&spa_namespace_lock);
3743 if (spa_lookup(pool) != NULL) {
3744 mutex_exit(&spa_namespace_lock);
3749 * Create and initialize the spa structure.
3751 (void) nvlist_lookup_string(props,
3752 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3753 (void) nvlist_lookup_uint64(props,
3754 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3757 spa = spa_add(pool, config, altroot);
3758 spa->spa_import_flags = flags;
3761 * Verbatim import - Take a pool and insert it into the namespace
3762 * as if it had been loaded at boot.
3764 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3766 spa_configfile_set(spa, props, B_FALSE);
3768 spa_config_sync(spa, B_FALSE, B_TRUE);
3770 mutex_exit(&spa_namespace_lock);
3771 spa_history_log_version(spa, LOG_POOL_IMPORT);
3776 spa_activate(spa, mode);
3779 * Don't start async tasks until we know everything is healthy.
3781 spa_async_suspend(spa);
3783 zpool_get_rewind_policy(config, &policy);
3784 if (policy.zrp_request & ZPOOL_DO_REWIND)
3785 state = SPA_LOAD_RECOVER;
3788 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3789 * because the user-supplied config is actually the one to trust when
3792 if (state != SPA_LOAD_RECOVER)
3793 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3795 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3796 policy.zrp_request);
3799 * Propagate anything learned while loading the pool and pass it
3800 * back to caller (i.e. rewind info, missing devices, etc).
3802 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3803 spa->spa_load_info) == 0);
3805 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3807 * Toss any existing sparelist, as it doesn't have any validity
3808 * anymore, and conflicts with spa_has_spare().
3810 if (spa->spa_spares.sav_config) {
3811 nvlist_free(spa->spa_spares.sav_config);
3812 spa->spa_spares.sav_config = NULL;
3813 spa_load_spares(spa);
3815 if (spa->spa_l2cache.sav_config) {
3816 nvlist_free(spa->spa_l2cache.sav_config);
3817 spa->spa_l2cache.sav_config = NULL;
3818 spa_load_l2cache(spa);
3821 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3824 error = spa_validate_aux(spa, nvroot, -1ULL,
3827 error = spa_validate_aux(spa, nvroot, -1ULL,
3828 VDEV_ALLOC_L2CACHE);
3829 spa_config_exit(spa, SCL_ALL, FTAG);
3832 spa_configfile_set(spa, props, B_FALSE);
3834 if (error != 0 || (props && spa_writeable(spa) &&
3835 (error = spa_prop_set(spa, props)))) {
3837 spa_deactivate(spa);
3839 mutex_exit(&spa_namespace_lock);
3843 spa_async_resume(spa);
3846 * Override any spares and level 2 cache devices as specified by
3847 * the user, as these may have correct device names/devids, etc.
3849 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3850 &spares, &nspares) == 0) {
3851 if (spa->spa_spares.sav_config)
3852 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3853 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3855 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3856 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3857 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3858 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3859 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3860 spa_load_spares(spa);
3861 spa_config_exit(spa, SCL_ALL, FTAG);
3862 spa->spa_spares.sav_sync = B_TRUE;
3864 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3865 &l2cache, &nl2cache) == 0) {
3866 if (spa->spa_l2cache.sav_config)
3867 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3868 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3870 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3871 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3872 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3873 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3874 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3875 spa_load_l2cache(spa);
3876 spa_config_exit(spa, SCL_ALL, FTAG);
3877 spa->spa_l2cache.sav_sync = B_TRUE;
3881 * Check for any removed devices.
3883 if (spa->spa_autoreplace) {
3884 spa_aux_check_removed(&spa->spa_spares);
3885 spa_aux_check_removed(&spa->spa_l2cache);
3888 if (spa_writeable(spa)) {
3890 * Update the config cache to include the newly-imported pool.
3892 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3896 * It's possible that the pool was expanded while it was exported.
3897 * We kick off an async task to handle this for us.
3899 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3901 mutex_exit(&spa_namespace_lock);
3902 spa_history_log_version(spa, LOG_POOL_IMPORT);
3908 spa_tryimport(nvlist_t *tryconfig)
3910 nvlist_t *config = NULL;
3916 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3919 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3923 * Create and initialize the spa structure.
3925 mutex_enter(&spa_namespace_lock);
3926 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3927 spa_activate(spa, FREAD);
3930 * Pass off the heavy lifting to spa_load().
3931 * Pass TRUE for mosconfig because the user-supplied config
3932 * is actually the one to trust when doing an import.
3934 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3937 * If 'tryconfig' was at least parsable, return the current config.
3939 if (spa->spa_root_vdev != NULL) {
3940 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3941 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3943 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3945 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3946 spa->spa_uberblock.ub_timestamp) == 0);
3947 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3948 spa->spa_load_info) == 0);
3951 * If the bootfs property exists on this pool then we
3952 * copy it out so that external consumers can tell which
3953 * pools are bootable.
3955 if ((!error || error == EEXIST) && spa->spa_bootfs) {
3956 char *tmpname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
3959 * We have to play games with the name since the
3960 * pool was opened as TRYIMPORT_NAME.
3962 if (dsl_dsobj_to_dsname(spa_name(spa),
3963 spa->spa_bootfs, tmpname) == 0) {
3965 char *dsname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
3967 cp = strchr(tmpname, '/');
3969 (void) strlcpy(dsname, tmpname,
3972 (void) snprintf(dsname, MAXPATHLEN,
3973 "%s/%s", poolname, ++cp);
3975 VERIFY(nvlist_add_string(config,
3976 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3977 kmem_free(dsname, MAXPATHLEN);
3979 kmem_free(tmpname, MAXPATHLEN);
3983 * Add the list of hot spares and level 2 cache devices.
3985 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3986 spa_add_spares(spa, config);
3987 spa_add_l2cache(spa, config);
3988 spa_config_exit(spa, SCL_CONFIG, FTAG);
3992 spa_deactivate(spa);
3994 mutex_exit(&spa_namespace_lock);
4000 * Pool export/destroy
4002 * The act of destroying or exporting a pool is very simple. We make sure there
4003 * is no more pending I/O and any references to the pool are gone. Then, we
4004 * update the pool state and sync all the labels to disk, removing the
4005 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4006 * we don't sync the labels or remove the configuration cache.
4009 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4010 boolean_t force, boolean_t hardforce)
4017 if (!(spa_mode_global & FWRITE))
4020 mutex_enter(&spa_namespace_lock);
4021 if ((spa = spa_lookup(pool)) == NULL) {
4022 mutex_exit(&spa_namespace_lock);
4027 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4028 * reacquire the namespace lock, and see if we can export.
4030 spa_open_ref(spa, FTAG);
4031 mutex_exit(&spa_namespace_lock);
4032 spa_async_suspend(spa);
4033 mutex_enter(&spa_namespace_lock);
4034 spa_close(spa, FTAG);
4037 * The pool will be in core if it's openable,
4038 * in which case we can modify its state.
4040 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4042 * Objsets may be open only because they're dirty, so we
4043 * have to force it to sync before checking spa_refcnt.
4045 txg_wait_synced(spa->spa_dsl_pool, 0);
4048 * A pool cannot be exported or destroyed if there are active
4049 * references. If we are resetting a pool, allow references by
4050 * fault injection handlers.
4052 if (!spa_refcount_zero(spa) ||
4053 (spa->spa_inject_ref != 0 &&
4054 new_state != POOL_STATE_UNINITIALIZED)) {
4055 spa_async_resume(spa);
4056 mutex_exit(&spa_namespace_lock);
4061 * A pool cannot be exported if it has an active shared spare.
4062 * This is to prevent other pools stealing the active spare
4063 * from an exported pool. At user's own will, such pool can
4064 * be forcedly exported.
4066 if (!force && new_state == POOL_STATE_EXPORTED &&
4067 spa_has_active_shared_spare(spa)) {
4068 spa_async_resume(spa);
4069 mutex_exit(&spa_namespace_lock);
4074 * We want this to be reflected on every label,
4075 * so mark them all dirty. spa_unload() will do the
4076 * final sync that pushes these changes out.
4078 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4079 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4080 spa->spa_state = new_state;
4081 spa->spa_final_txg = spa_last_synced_txg(spa) +
4083 vdev_config_dirty(spa->spa_root_vdev);
4084 spa_config_exit(spa, SCL_ALL, FTAG);
4088 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_DESTROY);
4090 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4092 spa_deactivate(spa);
4095 if (oldconfig && spa->spa_config)
4096 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4098 if (new_state != POOL_STATE_UNINITIALIZED) {
4100 spa_config_sync(spa, B_TRUE, B_TRUE);
4103 mutex_exit(&spa_namespace_lock);
4109 * Destroy a storage pool.
4112 spa_destroy(char *pool)
4114 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4119 * Export a storage pool.
4122 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4123 boolean_t hardforce)
4125 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4130 * Similar to spa_export(), this unloads the spa_t without actually removing it
4131 * from the namespace in any way.
4134 spa_reset(char *pool)
4136 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4141 * ==========================================================================
4142 * Device manipulation
4143 * ==========================================================================
4147 * Add a device to a storage pool.
4150 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4154 vdev_t *rvd = spa->spa_root_vdev;
4156 nvlist_t **spares, **l2cache;
4157 uint_t nspares, nl2cache;
4160 ASSERT(spa_writeable(spa));
4162 txg = spa_vdev_enter(spa);
4164 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4165 VDEV_ALLOC_ADD)) != 0)
4166 return (spa_vdev_exit(spa, NULL, txg, error));
4168 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4170 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4174 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4178 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4179 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4181 if (vd->vdev_children != 0 &&
4182 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4183 return (spa_vdev_exit(spa, vd, txg, error));
4186 * We must validate the spares and l2cache devices after checking the
4187 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4189 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4190 return (spa_vdev_exit(spa, vd, txg, error));
4193 * Transfer each new top-level vdev from vd to rvd.
4195 for (c = 0; c < vd->vdev_children; c++) {
4198 * Set the vdev id to the first hole, if one exists.
4200 for (id = 0; id < rvd->vdev_children; id++) {
4201 if (rvd->vdev_child[id]->vdev_ishole) {
4202 vdev_free(rvd->vdev_child[id]);
4206 tvd = vd->vdev_child[c];
4207 vdev_remove_child(vd, tvd);
4209 vdev_add_child(rvd, tvd);
4210 vdev_config_dirty(tvd);
4214 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4215 ZPOOL_CONFIG_SPARES);
4216 spa_load_spares(spa);
4217 spa->spa_spares.sav_sync = B_TRUE;
4220 if (nl2cache != 0) {
4221 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4222 ZPOOL_CONFIG_L2CACHE);
4223 spa_load_l2cache(spa);
4224 spa->spa_l2cache.sav_sync = B_TRUE;
4228 * We have to be careful when adding new vdevs to an existing pool.
4229 * If other threads start allocating from these vdevs before we
4230 * sync the config cache, and we lose power, then upon reboot we may
4231 * fail to open the pool because there are DVAs that the config cache
4232 * can't translate. Therefore, we first add the vdevs without
4233 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4234 * and then let spa_config_update() initialize the new metaslabs.
4236 * spa_load() checks for added-but-not-initialized vdevs, so that
4237 * if we lose power at any point in this sequence, the remaining
4238 * steps will be completed the next time we load the pool.
4240 (void) spa_vdev_exit(spa, vd, txg, 0);
4242 mutex_enter(&spa_namespace_lock);
4243 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4244 mutex_exit(&spa_namespace_lock);
4250 * Attach a device to a mirror. The arguments are the path to any device
4251 * in the mirror, and the nvroot for the new device. If the path specifies
4252 * a device that is not mirrored, we automatically insert the mirror vdev.
4254 * If 'replacing' is specified, the new device is intended to replace the
4255 * existing device; in this case the two devices are made into their own
4256 * mirror using the 'replacing' vdev, which is functionally identical to
4257 * the mirror vdev (it actually reuses all the same ops) but has a few
4258 * extra rules: you can't attach to it after it's been created, and upon
4259 * completion of resilvering, the first disk (the one being replaced)
4260 * is automatically detached.
4263 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4265 uint64_t txg, dtl_max_txg;
4266 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
4267 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4269 char *oldvdpath, *newvdpath;
4273 ASSERT(spa_writeable(spa));
4275 txg = spa_vdev_enter(spa);
4277 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4280 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4282 if (!oldvd->vdev_ops->vdev_op_leaf)
4283 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4285 pvd = oldvd->vdev_parent;
4287 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4288 VDEV_ALLOC_ATTACH)) != 0)
4289 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4291 if (newrootvd->vdev_children != 1)
4292 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4294 newvd = newrootvd->vdev_child[0];
4296 if (!newvd->vdev_ops->vdev_op_leaf)
4297 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4299 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4300 return (spa_vdev_exit(spa, newrootvd, txg, error));
4303 * Spares can't replace logs
4305 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4306 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4310 * For attach, the only allowable parent is a mirror or the root
4313 if (pvd->vdev_ops != &vdev_mirror_ops &&
4314 pvd->vdev_ops != &vdev_root_ops)
4315 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4317 pvops = &vdev_mirror_ops;
4320 * Active hot spares can only be replaced by inactive hot
4323 if (pvd->vdev_ops == &vdev_spare_ops &&
4324 oldvd->vdev_isspare &&
4325 !spa_has_spare(spa, newvd->vdev_guid))
4326 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4329 * If the source is a hot spare, and the parent isn't already a
4330 * spare, then we want to create a new hot spare. Otherwise, we
4331 * want to create a replacing vdev. The user is not allowed to
4332 * attach to a spared vdev child unless the 'isspare' state is
4333 * the same (spare replaces spare, non-spare replaces
4336 if (pvd->vdev_ops == &vdev_replacing_ops &&
4337 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4338 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4339 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4340 newvd->vdev_isspare != oldvd->vdev_isspare) {
4341 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4344 if (newvd->vdev_isspare)
4345 pvops = &vdev_spare_ops;
4347 pvops = &vdev_replacing_ops;
4351 * Make sure the new device is big enough.
4353 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4354 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4357 * The new device cannot have a higher alignment requirement
4358 * than the top-level vdev.
4360 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4361 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4364 * If this is an in-place replacement, update oldvd's path and devid
4365 * to make it distinguishable from newvd, and unopenable from now on.
4367 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4368 spa_strfree(oldvd->vdev_path);
4369 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4371 (void) sprintf(oldvd->vdev_path, "%s/%s",
4372 newvd->vdev_path, "old");
4373 if (oldvd->vdev_devid != NULL) {
4374 spa_strfree(oldvd->vdev_devid);
4375 oldvd->vdev_devid = NULL;
4379 /* mark the device being resilvered */
4380 newvd->vdev_resilvering = B_TRUE;
4383 * If the parent is not a mirror, or if we're replacing, insert the new
4384 * mirror/replacing/spare vdev above oldvd.
4386 if (pvd->vdev_ops != pvops)
4387 pvd = vdev_add_parent(oldvd, pvops);
4389 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4390 ASSERT(pvd->vdev_ops == pvops);
4391 ASSERT(oldvd->vdev_parent == pvd);
4394 * Extract the new device from its root and add it to pvd.
4396 vdev_remove_child(newrootvd, newvd);
4397 newvd->vdev_id = pvd->vdev_children;
4398 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4399 vdev_add_child(pvd, newvd);
4401 tvd = newvd->vdev_top;
4402 ASSERT(pvd->vdev_top == tvd);
4403 ASSERT(tvd->vdev_parent == rvd);
4405 vdev_config_dirty(tvd);
4408 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4409 * for any dmu_sync-ed blocks. It will propagate upward when
4410 * spa_vdev_exit() calls vdev_dtl_reassess().
4412 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4414 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4415 dtl_max_txg - TXG_INITIAL);
4417 if (newvd->vdev_isspare) {
4418 spa_spare_activate(newvd);
4419 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_DEVICE_SPARE);
4422 oldvdpath = spa_strdup(oldvd->vdev_path);
4423 newvdpath = spa_strdup(newvd->vdev_path);
4424 newvd_isspare = newvd->vdev_isspare;
4427 * Mark newvd's DTL dirty in this txg.
4429 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4432 * Restart the resilver
4434 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4439 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4441 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
4442 "%s vdev=%s %s vdev=%s",
4443 replacing && newvd_isspare ? "spare in" :
4444 replacing ? "replace" : "attach", newvdpath,
4445 replacing ? "for" : "to", oldvdpath);
4447 spa_strfree(oldvdpath);
4448 spa_strfree(newvdpath);
4450 if (spa->spa_bootfs)
4451 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH);
4457 * Detach a device from a mirror or replacing vdev.
4458 * If 'replace_done' is specified, only detach if the parent
4459 * is a replacing vdev.
4462 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4466 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
4467 vdev_t *vd, *pvd, *cvd, *tvd;
4468 boolean_t unspare = B_FALSE;
4469 uint64_t unspare_guid = 0;
4473 ASSERT(spa_writeable(spa));
4475 txg = spa_vdev_enter(spa);
4477 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4480 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4482 if (!vd->vdev_ops->vdev_op_leaf)
4483 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4485 pvd = vd->vdev_parent;
4488 * If the parent/child relationship is not as expected, don't do it.
4489 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4490 * vdev that's replacing B with C. The user's intent in replacing
4491 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4492 * the replace by detaching C, the expected behavior is to end up
4493 * M(A,B). But suppose that right after deciding to detach C,
4494 * the replacement of B completes. We would have M(A,C), and then
4495 * ask to detach C, which would leave us with just A -- not what
4496 * the user wanted. To prevent this, we make sure that the
4497 * parent/child relationship hasn't changed -- in this example,
4498 * that C's parent is still the replacing vdev R.
4500 if (pvd->vdev_guid != pguid && pguid != 0)
4501 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4504 * Only 'replacing' or 'spare' vdevs can be replaced.
4506 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4507 pvd->vdev_ops != &vdev_spare_ops)
4508 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4510 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4511 spa_version(spa) >= SPA_VERSION_SPARES);
4514 * Only mirror, replacing, and spare vdevs support detach.
4516 if (pvd->vdev_ops != &vdev_replacing_ops &&
4517 pvd->vdev_ops != &vdev_mirror_ops &&
4518 pvd->vdev_ops != &vdev_spare_ops)
4519 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4522 * If this device has the only valid copy of some data,
4523 * we cannot safely detach it.
4525 if (vdev_dtl_required(vd))
4526 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4528 ASSERT(pvd->vdev_children >= 2);
4531 * If we are detaching the second disk from a replacing vdev, then
4532 * check to see if we changed the original vdev's path to have "/old"
4533 * at the end in spa_vdev_attach(). If so, undo that change now.
4535 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4536 vd->vdev_path != NULL) {
4537 size_t len = strlen(vd->vdev_path);
4539 for (c = 0; c < pvd->vdev_children; c++) {
4540 cvd = pvd->vdev_child[c];
4542 if (cvd == vd || cvd->vdev_path == NULL)
4545 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4546 strcmp(cvd->vdev_path + len, "/old") == 0) {
4547 spa_strfree(cvd->vdev_path);
4548 cvd->vdev_path = spa_strdup(vd->vdev_path);
4555 * If we are detaching the original disk from a spare, then it implies
4556 * that the spare should become a real disk, and be removed from the
4557 * active spare list for the pool.
4559 if (pvd->vdev_ops == &vdev_spare_ops &&
4561 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4565 * Erase the disk labels so the disk can be used for other things.
4566 * This must be done after all other error cases are handled,
4567 * but before we disembowel vd (so we can still do I/O to it).
4568 * But if we can't do it, don't treat the error as fatal --
4569 * it may be that the unwritability of the disk is the reason
4570 * it's being detached!
4572 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4575 * Remove vd from its parent and compact the parent's children.
4577 vdev_remove_child(pvd, vd);
4578 vdev_compact_children(pvd);
4581 * Remember one of the remaining children so we can get tvd below.
4583 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4586 * If we need to remove the remaining child from the list of hot spares,
4587 * do it now, marking the vdev as no longer a spare in the process.
4588 * We must do this before vdev_remove_parent(), because that can
4589 * change the GUID if it creates a new toplevel GUID. For a similar
4590 * reason, we must remove the spare now, in the same txg as the detach;
4591 * otherwise someone could attach a new sibling, change the GUID, and
4592 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4595 ASSERT(cvd->vdev_isspare);
4596 spa_spare_remove(cvd);
4597 unspare_guid = cvd->vdev_guid;
4598 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4599 cvd->vdev_unspare = B_TRUE;
4603 * If the parent mirror/replacing vdev only has one child,
4604 * the parent is no longer needed. Remove it from the tree.
4606 if (pvd->vdev_children == 1) {
4607 if (pvd->vdev_ops == &vdev_spare_ops)
4608 cvd->vdev_unspare = B_FALSE;
4609 vdev_remove_parent(cvd);
4610 cvd->vdev_resilvering = B_FALSE;
4615 * We don't set tvd until now because the parent we just removed
4616 * may have been the previous top-level vdev.
4618 tvd = cvd->vdev_top;
4619 ASSERT(tvd->vdev_parent == rvd);
4622 * Reevaluate the parent vdev state.
4624 vdev_propagate_state(cvd);
4627 * If the 'autoexpand' property is set on the pool then automatically
4628 * try to expand the size of the pool. For example if the device we
4629 * just detached was smaller than the others, it may be possible to
4630 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4631 * first so that we can obtain the updated sizes of the leaf vdevs.
4633 if (spa->spa_autoexpand) {
4635 vdev_expand(tvd, txg);
4638 vdev_config_dirty(tvd);
4641 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4642 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4643 * But first make sure we're not on any *other* txg's DTL list, to
4644 * prevent vd from being accessed after it's freed.
4646 vdpath = spa_strdup(vd->vdev_path);
4647 for (t = 0; t < TXG_SIZE; t++)
4648 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4649 vd->vdev_detached = B_TRUE;
4650 vdev_dirty(tvd, VDD_DTL, vd, txg);
4652 spa_event_notify(spa, vd, FM_EREPORT_ZFS_DEVICE_REMOVE);
4654 /* hang on to the spa before we release the lock */
4655 spa_open_ref(spa, FTAG);
4657 error = spa_vdev_exit(spa, vd, txg, 0);
4659 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4661 spa_strfree(vdpath);
4664 * If this was the removal of the original device in a hot spare vdev,
4665 * then we want to go through and remove the device from the hot spare
4666 * list of every other pool.
4669 spa_t *altspa = NULL;
4671 mutex_enter(&spa_namespace_lock);
4672 while ((altspa = spa_next(altspa)) != NULL) {
4673 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4677 spa_open_ref(altspa, FTAG);
4678 mutex_exit(&spa_namespace_lock);
4679 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4680 mutex_enter(&spa_namespace_lock);
4681 spa_close(altspa, FTAG);
4683 mutex_exit(&spa_namespace_lock);
4685 /* search the rest of the vdevs for spares to remove */
4686 spa_vdev_resilver_done(spa);
4689 /* all done with the spa; OK to release */
4690 mutex_enter(&spa_namespace_lock);
4691 spa_close(spa, FTAG);
4692 mutex_exit(&spa_namespace_lock);
4698 * Split a set of devices from their mirrors, and create a new pool from them.
4701 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4702 nvlist_t *props, boolean_t exp)
4705 uint64_t txg, *glist;
4707 uint_t c, children, lastlog;
4708 nvlist_t **child, *nvl, *tmp;
4710 char *altroot = NULL;
4711 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4712 boolean_t activate_slog;
4714 ASSERT(spa_writeable(spa));
4716 txg = spa_vdev_enter(spa);
4718 /* clear the log and flush everything up to now */
4719 activate_slog = spa_passivate_log(spa);
4720 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4721 error = spa_offline_log(spa);
4722 txg = spa_vdev_config_enter(spa);
4725 spa_activate_log(spa);
4728 return (spa_vdev_exit(spa, NULL, txg, error));
4730 /* check new spa name before going any further */
4731 if (spa_lookup(newname) != NULL)
4732 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4735 * scan through all the children to ensure they're all mirrors
4737 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4738 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4740 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4742 /* first, check to ensure we've got the right child count */
4743 rvd = spa->spa_root_vdev;
4745 for (c = 0; c < rvd->vdev_children; c++) {
4746 vdev_t *vd = rvd->vdev_child[c];
4748 /* don't count the holes & logs as children */
4749 if (vd->vdev_islog || vd->vdev_ishole) {
4757 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4758 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4760 /* next, ensure no spare or cache devices are part of the split */
4761 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4762 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4763 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4765 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_PUSHPAGE);
4766 glist = kmem_zalloc(children * sizeof (uint64_t), KM_PUSHPAGE);
4768 /* then, loop over each vdev and validate it */
4769 for (c = 0; c < children; c++) {
4770 uint64_t is_hole = 0;
4772 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4776 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4777 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4785 /* which disk is going to be split? */
4786 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4792 /* look it up in the spa */
4793 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4794 if (vml[c] == NULL) {
4799 /* make sure there's nothing stopping the split */
4800 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4801 vml[c]->vdev_islog ||
4802 vml[c]->vdev_ishole ||
4803 vml[c]->vdev_isspare ||
4804 vml[c]->vdev_isl2cache ||
4805 !vdev_writeable(vml[c]) ||
4806 vml[c]->vdev_children != 0 ||
4807 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4808 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4813 if (vdev_dtl_required(vml[c])) {
4818 /* we need certain info from the top level */
4819 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4820 vml[c]->vdev_top->vdev_ms_array) == 0);
4821 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4822 vml[c]->vdev_top->vdev_ms_shift) == 0);
4823 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4824 vml[c]->vdev_top->vdev_asize) == 0);
4825 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4826 vml[c]->vdev_top->vdev_ashift) == 0);
4830 kmem_free(vml, children * sizeof (vdev_t *));
4831 kmem_free(glist, children * sizeof (uint64_t));
4832 return (spa_vdev_exit(spa, NULL, txg, error));
4835 /* stop writers from using the disks */
4836 for (c = 0; c < children; c++) {
4838 vml[c]->vdev_offline = B_TRUE;
4840 vdev_reopen(spa->spa_root_vdev);
4843 * Temporarily record the splitting vdevs in the spa config. This
4844 * will disappear once the config is regenerated.
4846 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
4847 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4848 glist, children) == 0);
4849 kmem_free(glist, children * sizeof (uint64_t));
4851 mutex_enter(&spa->spa_props_lock);
4852 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4854 mutex_exit(&spa->spa_props_lock);
4855 spa->spa_config_splitting = nvl;
4856 vdev_config_dirty(spa->spa_root_vdev);
4858 /* configure and create the new pool */
4859 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4860 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4861 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4862 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4863 spa_version(spa)) == 0);
4864 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4865 spa->spa_config_txg) == 0);
4866 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4867 spa_generate_guid(NULL)) == 0);
4868 (void) nvlist_lookup_string(props,
4869 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4871 /* add the new pool to the namespace */
4872 newspa = spa_add(newname, config, altroot);
4873 newspa->spa_config_txg = spa->spa_config_txg;
4874 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4876 /* release the spa config lock, retaining the namespace lock */
4877 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4879 if (zio_injection_enabled)
4880 zio_handle_panic_injection(spa, FTAG, 1);
4882 spa_activate(newspa, spa_mode_global);
4883 spa_async_suspend(newspa);
4885 /* create the new pool from the disks of the original pool */
4886 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4890 /* if that worked, generate a real config for the new pool */
4891 if (newspa->spa_root_vdev != NULL) {
4892 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4893 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
4894 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4895 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4896 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4901 if (props != NULL) {
4902 spa_configfile_set(newspa, props, B_FALSE);
4903 error = spa_prop_set(newspa, props);
4908 /* flush everything */
4909 txg = spa_vdev_config_enter(newspa);
4910 vdev_config_dirty(newspa->spa_root_vdev);
4911 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4913 if (zio_injection_enabled)
4914 zio_handle_panic_injection(spa, FTAG, 2);
4916 spa_async_resume(newspa);
4918 /* finally, update the original pool's config */
4919 txg = spa_vdev_config_enter(spa);
4920 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4921 error = dmu_tx_assign(tx, TXG_WAIT);
4924 for (c = 0; c < children; c++) {
4925 if (vml[c] != NULL) {
4928 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4934 vdev_config_dirty(spa->spa_root_vdev);
4935 spa->spa_config_splitting = NULL;
4939 (void) spa_vdev_exit(spa, NULL, txg, 0);
4941 if (zio_injection_enabled)
4942 zio_handle_panic_injection(spa, FTAG, 3);
4944 /* split is complete; log a history record */
4945 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
4946 "split new pool %s from pool %s", newname, spa_name(spa));
4948 kmem_free(vml, children * sizeof (vdev_t *));
4950 /* if we're not going to mount the filesystems in userland, export */
4952 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
4959 spa_deactivate(newspa);
4962 txg = spa_vdev_config_enter(spa);
4964 /* re-online all offlined disks */
4965 for (c = 0; c < children; c++) {
4967 vml[c]->vdev_offline = B_FALSE;
4969 vdev_reopen(spa->spa_root_vdev);
4971 nvlist_free(spa->spa_config_splitting);
4972 spa->spa_config_splitting = NULL;
4973 (void) spa_vdev_exit(spa, NULL, txg, error);
4975 kmem_free(vml, children * sizeof (vdev_t *));
4980 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
4984 for (i = 0; i < count; i++) {
4987 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
4990 if (guid == target_guid)
4998 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
4999 nvlist_t *dev_to_remove)
5001 nvlist_t **newdev = NULL;
5005 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_PUSHPAGE);
5007 for (i = 0, j = 0; i < count; i++) {
5008 if (dev[i] == dev_to_remove)
5010 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_PUSHPAGE) == 0);
5013 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5014 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5016 for (i = 0; i < count - 1; i++)
5017 nvlist_free(newdev[i]);
5020 kmem_free(newdev, (count - 1) * sizeof (void *));
5024 * Evacuate the device.
5027 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5032 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5033 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5034 ASSERT(vd == vd->vdev_top);
5037 * Evacuate the device. We don't hold the config lock as writer
5038 * since we need to do I/O but we do keep the
5039 * spa_namespace_lock held. Once this completes the device
5040 * should no longer have any blocks allocated on it.
5042 if (vd->vdev_islog) {
5043 if (vd->vdev_stat.vs_alloc != 0)
5044 error = spa_offline_log(spa);
5053 * The evacuation succeeded. Remove any remaining MOS metadata
5054 * associated with this vdev, and wait for these changes to sync.
5056 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
5057 txg = spa_vdev_config_enter(spa);
5058 vd->vdev_removing = B_TRUE;
5059 vdev_dirty(vd, 0, NULL, txg);
5060 vdev_config_dirty(vd);
5061 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5067 * Complete the removal by cleaning up the namespace.
5070 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5072 vdev_t *rvd = spa->spa_root_vdev;
5073 uint64_t id = vd->vdev_id;
5074 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5076 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5077 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5078 ASSERT(vd == vd->vdev_top);
5081 * Only remove any devices which are empty.
5083 if (vd->vdev_stat.vs_alloc != 0)
5086 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5088 if (list_link_active(&vd->vdev_state_dirty_node))
5089 vdev_state_clean(vd);
5090 if (list_link_active(&vd->vdev_config_dirty_node))
5091 vdev_config_clean(vd);
5096 vdev_compact_children(rvd);
5098 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5099 vdev_add_child(rvd, vd);
5101 vdev_config_dirty(rvd);
5104 * Reassess the health of our root vdev.
5110 * Remove a device from the pool -
5112 * Removing a device from the vdev namespace requires several steps
5113 * and can take a significant amount of time. As a result we use
5114 * the spa_vdev_config_[enter/exit] functions which allow us to
5115 * grab and release the spa_config_lock while still holding the namespace
5116 * lock. During each step the configuration is synced out.
5120 * Remove a device from the pool. Currently, this supports removing only hot
5121 * spares, slogs, and level 2 ARC devices.
5124 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5127 metaslab_group_t *mg;
5128 nvlist_t **spares, **l2cache, *nv;
5130 uint_t nspares, nl2cache;
5132 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5134 ASSERT(spa_writeable(spa));
5137 txg = spa_vdev_enter(spa);
5139 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5141 if (spa->spa_spares.sav_vdevs != NULL &&
5142 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5143 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5144 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5146 * Only remove the hot spare if it's not currently in use
5149 if (vd == NULL || unspare) {
5150 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5151 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5152 spa_load_spares(spa);
5153 spa->spa_spares.sav_sync = B_TRUE;
5157 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5158 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5159 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5160 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5162 * Cache devices can always be removed.
5164 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5165 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5166 spa_load_l2cache(spa);
5167 spa->spa_l2cache.sav_sync = B_TRUE;
5168 } else if (vd != NULL && vd->vdev_islog) {
5170 ASSERT(vd == vd->vdev_top);
5173 * XXX - Once we have bp-rewrite this should
5174 * become the common case.
5180 * Stop allocating from this vdev.
5182 metaslab_group_passivate(mg);
5185 * Wait for the youngest allocations and frees to sync,
5186 * and then wait for the deferral of those frees to finish.
5188 spa_vdev_config_exit(spa, NULL,
5189 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5192 * Attempt to evacuate the vdev.
5194 error = spa_vdev_remove_evacuate(spa, vd);
5196 txg = spa_vdev_config_enter(spa);
5199 * If we couldn't evacuate the vdev, unwind.
5202 metaslab_group_activate(mg);
5203 return (spa_vdev_exit(spa, NULL, txg, error));
5207 * Clean up the vdev namespace.
5209 spa_vdev_remove_from_namespace(spa, vd);
5211 } else if (vd != NULL) {
5213 * Normal vdevs cannot be removed (yet).
5218 * There is no vdev of any kind with the specified guid.
5224 return (spa_vdev_exit(spa, NULL, txg, error));
5230 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5231 * current spared, so we can detach it.
5234 spa_vdev_resilver_done_hunt(vdev_t *vd)
5236 vdev_t *newvd, *oldvd;
5239 for (c = 0; c < vd->vdev_children; c++) {
5240 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5246 * Check for a completed replacement. We always consider the first
5247 * vdev in the list to be the oldest vdev, and the last one to be
5248 * the newest (see spa_vdev_attach() for how that works). In
5249 * the case where the newest vdev is faulted, we will not automatically
5250 * remove it after a resilver completes. This is OK as it will require
5251 * user intervention to determine which disk the admin wishes to keep.
5253 if (vd->vdev_ops == &vdev_replacing_ops) {
5254 ASSERT(vd->vdev_children > 1);
5256 newvd = vd->vdev_child[vd->vdev_children - 1];
5257 oldvd = vd->vdev_child[0];
5259 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5260 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5261 !vdev_dtl_required(oldvd))
5266 * Check for a completed resilver with the 'unspare' flag set.
5268 if (vd->vdev_ops == &vdev_spare_ops) {
5269 vdev_t *first = vd->vdev_child[0];
5270 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5272 if (last->vdev_unspare) {
5275 } else if (first->vdev_unspare) {
5282 if (oldvd != NULL &&
5283 vdev_dtl_empty(newvd, DTL_MISSING) &&
5284 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5285 !vdev_dtl_required(oldvd))
5289 * If there are more than two spares attached to a disk,
5290 * and those spares are not required, then we want to
5291 * attempt to free them up now so that they can be used
5292 * by other pools. Once we're back down to a single
5293 * disk+spare, we stop removing them.
5295 if (vd->vdev_children > 2) {
5296 newvd = vd->vdev_child[1];
5298 if (newvd->vdev_isspare && last->vdev_isspare &&
5299 vdev_dtl_empty(last, DTL_MISSING) &&
5300 vdev_dtl_empty(last, DTL_OUTAGE) &&
5301 !vdev_dtl_required(newvd))
5310 spa_vdev_resilver_done(spa_t *spa)
5312 vdev_t *vd, *pvd, *ppvd;
5313 uint64_t guid, sguid, pguid, ppguid;
5315 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5317 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5318 pvd = vd->vdev_parent;
5319 ppvd = pvd->vdev_parent;
5320 guid = vd->vdev_guid;
5321 pguid = pvd->vdev_guid;
5322 ppguid = ppvd->vdev_guid;
5325 * If we have just finished replacing a hot spared device, then
5326 * we need to detach the parent's first child (the original hot
5329 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5330 ppvd->vdev_children == 2) {
5331 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5332 sguid = ppvd->vdev_child[1]->vdev_guid;
5334 spa_config_exit(spa, SCL_ALL, FTAG);
5335 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5337 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5339 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5342 spa_config_exit(spa, SCL_ALL, FTAG);
5346 * Update the stored path or FRU for this vdev.
5349 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5353 boolean_t sync = B_FALSE;
5355 ASSERT(spa_writeable(spa));
5357 spa_vdev_state_enter(spa, SCL_ALL);
5359 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5360 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5362 if (!vd->vdev_ops->vdev_op_leaf)
5363 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5366 if (strcmp(value, vd->vdev_path) != 0) {
5367 spa_strfree(vd->vdev_path);
5368 vd->vdev_path = spa_strdup(value);
5372 if (vd->vdev_fru == NULL) {
5373 vd->vdev_fru = spa_strdup(value);
5375 } else if (strcmp(value, vd->vdev_fru) != 0) {
5376 spa_strfree(vd->vdev_fru);
5377 vd->vdev_fru = spa_strdup(value);
5382 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5386 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5388 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5392 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5394 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5398 * ==========================================================================
5400 * ==========================================================================
5404 spa_scan_stop(spa_t *spa)
5406 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5407 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5409 return (dsl_scan_cancel(spa->spa_dsl_pool));
5413 spa_scan(spa_t *spa, pool_scan_func_t func)
5415 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5417 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5421 * If a resilver was requested, but there is no DTL on a
5422 * writeable leaf device, we have nothing to do.
5424 if (func == POOL_SCAN_RESILVER &&
5425 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5426 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5430 return (dsl_scan(spa->spa_dsl_pool, func));
5434 * ==========================================================================
5435 * SPA async task processing
5436 * ==========================================================================
5440 spa_async_remove(spa_t *spa, vdev_t *vd)
5444 if (vd->vdev_remove_wanted) {
5445 vd->vdev_remove_wanted = B_FALSE;
5446 vd->vdev_delayed_close = B_FALSE;
5447 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5450 * We want to clear the stats, but we don't want to do a full
5451 * vdev_clear() as that will cause us to throw away
5452 * degraded/faulted state as well as attempt to reopen the
5453 * device, all of which is a waste.
5455 vd->vdev_stat.vs_read_errors = 0;
5456 vd->vdev_stat.vs_write_errors = 0;
5457 vd->vdev_stat.vs_checksum_errors = 0;
5459 vdev_state_dirty(vd->vdev_top);
5462 for (c = 0; c < vd->vdev_children; c++)
5463 spa_async_remove(spa, vd->vdev_child[c]);
5467 spa_async_probe(spa_t *spa, vdev_t *vd)
5471 if (vd->vdev_probe_wanted) {
5472 vd->vdev_probe_wanted = B_FALSE;
5473 vdev_reopen(vd); /* vdev_open() does the actual probe */
5476 for (c = 0; c < vd->vdev_children; c++)
5477 spa_async_probe(spa, vd->vdev_child[c]);
5481 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5485 if (!spa->spa_autoexpand)
5488 for (c = 0; c < vd->vdev_children; c++) {
5489 vdev_t *cvd = vd->vdev_child[c];
5490 spa_async_autoexpand(spa, cvd);
5493 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5496 spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND);
5500 spa_async_thread(spa_t *spa)
5504 ASSERT(spa->spa_sync_on);
5506 mutex_enter(&spa->spa_async_lock);
5507 tasks = spa->spa_async_tasks;
5508 spa->spa_async_tasks = 0;
5509 mutex_exit(&spa->spa_async_lock);
5512 * See if the config needs to be updated.
5514 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5515 uint64_t old_space, new_space;
5517 mutex_enter(&spa_namespace_lock);
5518 old_space = metaslab_class_get_space(spa_normal_class(spa));
5519 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5520 new_space = metaslab_class_get_space(spa_normal_class(spa));
5521 mutex_exit(&spa_namespace_lock);
5524 * If the pool grew as a result of the config update,
5525 * then log an internal history event.
5527 if (new_space != old_space) {
5528 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5530 "pool '%s' size: %llu(+%llu)",
5531 spa_name(spa), new_space, new_space - old_space);
5536 * See if any devices need to be marked REMOVED.
5538 if (tasks & SPA_ASYNC_REMOVE) {
5539 spa_vdev_state_enter(spa, SCL_NONE);
5540 spa_async_remove(spa, spa->spa_root_vdev);
5541 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
5542 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5543 for (i = 0; i < spa->spa_spares.sav_count; i++)
5544 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5545 (void) spa_vdev_state_exit(spa, NULL, 0);
5548 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5549 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5550 spa_async_autoexpand(spa, spa->spa_root_vdev);
5551 spa_config_exit(spa, SCL_CONFIG, FTAG);
5555 * See if any devices need to be probed.
5557 if (tasks & SPA_ASYNC_PROBE) {
5558 spa_vdev_state_enter(spa, SCL_NONE);
5559 spa_async_probe(spa, spa->spa_root_vdev);
5560 (void) spa_vdev_state_exit(spa, NULL, 0);
5564 * If any devices are done replacing, detach them.
5566 if (tasks & SPA_ASYNC_RESILVER_DONE)
5567 spa_vdev_resilver_done(spa);
5570 * Kick off a resilver.
5572 if (tasks & SPA_ASYNC_RESILVER)
5573 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5576 * Let the world know that we're done.
5578 mutex_enter(&spa->spa_async_lock);
5579 spa->spa_async_thread = NULL;
5580 cv_broadcast(&spa->spa_async_cv);
5581 mutex_exit(&spa->spa_async_lock);
5586 spa_async_suspend(spa_t *spa)
5588 mutex_enter(&spa->spa_async_lock);
5589 spa->spa_async_suspended++;
5590 while (spa->spa_async_thread != NULL)
5591 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5592 mutex_exit(&spa->spa_async_lock);
5596 spa_async_resume(spa_t *spa)
5598 mutex_enter(&spa->spa_async_lock);
5599 ASSERT(spa->spa_async_suspended != 0);
5600 spa->spa_async_suspended--;
5601 mutex_exit(&spa->spa_async_lock);
5605 spa_async_dispatch(spa_t *spa)
5607 mutex_enter(&spa->spa_async_lock);
5608 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5609 spa->spa_async_thread == NULL &&
5610 rootdir != NULL && !vn_is_readonly(rootdir))
5611 spa->spa_async_thread = thread_create(NULL, 0,
5612 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5613 mutex_exit(&spa->spa_async_lock);
5617 spa_async_request(spa_t *spa, int task)
5619 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5620 mutex_enter(&spa->spa_async_lock);
5621 spa->spa_async_tasks |= task;
5622 mutex_exit(&spa->spa_async_lock);
5626 * ==========================================================================
5627 * SPA syncing routines
5628 * ==========================================================================
5632 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5635 bpobj_enqueue(bpo, bp, tx);
5640 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5644 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5650 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5652 char *packed = NULL;
5657 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5660 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5661 * information. This avoids the dbuf_will_dirty() path and
5662 * saves us a pre-read to get data we don't actually care about.
5664 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
5665 packed = vmem_alloc(bufsize, KM_PUSHPAGE);
5667 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5669 bzero(packed + nvsize, bufsize - nvsize);
5671 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5673 vmem_free(packed, bufsize);
5675 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5676 dmu_buf_will_dirty(db, tx);
5677 *(uint64_t *)db->db_data = nvsize;
5678 dmu_buf_rele(db, FTAG);
5682 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5683 const char *config, const char *entry)
5693 * Update the MOS nvlist describing the list of available devices.
5694 * spa_validate_aux() will have already made sure this nvlist is
5695 * valid and the vdevs are labeled appropriately.
5697 if (sav->sav_object == 0) {
5698 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5699 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5700 sizeof (uint64_t), tx);
5701 VERIFY(zap_update(spa->spa_meta_objset,
5702 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5703 &sav->sav_object, tx) == 0);
5706 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
5707 if (sav->sav_count == 0) {
5708 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5710 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE);
5711 for (i = 0; i < sav->sav_count; i++)
5712 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5713 B_FALSE, VDEV_CONFIG_L2CACHE);
5714 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5715 sav->sav_count) == 0);
5716 for (i = 0; i < sav->sav_count; i++)
5717 nvlist_free(list[i]);
5718 kmem_free(list, sav->sav_count * sizeof (void *));
5721 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5722 nvlist_free(nvroot);
5724 sav->sav_sync = B_FALSE;
5728 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5732 if (list_is_empty(&spa->spa_config_dirty_list))
5735 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5737 config = spa_config_generate(spa, spa->spa_root_vdev,
5738 dmu_tx_get_txg(tx), B_FALSE);
5740 spa_config_exit(spa, SCL_STATE, FTAG);
5742 if (spa->spa_config_syncing)
5743 nvlist_free(spa->spa_config_syncing);
5744 spa->spa_config_syncing = config;
5746 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5750 spa_sync_version(void *arg1, void *arg2, dmu_tx_t *tx)
5753 uint64_t version = *(uint64_t *)arg2;
5756 * Setting the version is special cased when first creating the pool.
5758 ASSERT(tx->tx_txg != TXG_INITIAL);
5760 ASSERT(version <= SPA_VERSION);
5761 ASSERT(version >= spa_version(spa));
5763 spa->spa_uberblock.ub_version = version;
5764 vdev_config_dirty(spa->spa_root_vdev);
5768 * Set zpool properties.
5771 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5774 objset_t *mos = spa->spa_meta_objset;
5775 nvlist_t *nvp = arg2;
5776 nvpair_t *elem = NULL;
5778 mutex_enter(&spa->spa_props_lock);
5780 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5782 char *strval, *fname;
5784 const char *propname;
5785 zprop_type_t proptype;
5786 zfeature_info_t *feature;
5788 prop = zpool_name_to_prop(nvpair_name(elem));
5789 switch ((int)prop) {
5792 * We checked this earlier in spa_prop_validate().
5794 ASSERT(zpool_prop_feature(nvpair_name(elem)));
5796 fname = strchr(nvpair_name(elem), '@') + 1;
5797 VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature));
5799 spa_feature_enable(spa, feature, tx);
5802 case ZPOOL_PROP_VERSION:
5803 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5805 * The version is synced seperatly before other
5806 * properties and should be correct by now.
5808 ASSERT3U(spa_version(spa), >=, intval);
5811 case ZPOOL_PROP_ALTROOT:
5813 * 'altroot' is a non-persistent property. It should
5814 * have been set temporarily at creation or import time.
5816 ASSERT(spa->spa_root != NULL);
5819 case ZPOOL_PROP_READONLY:
5820 case ZPOOL_PROP_CACHEFILE:
5822 * 'readonly' and 'cachefile' are also non-persisitent
5826 case ZPOOL_PROP_COMMENT:
5827 VERIFY(nvpair_value_string(elem, &strval) == 0);
5828 if (spa->spa_comment != NULL)
5829 spa_strfree(spa->spa_comment);
5830 spa->spa_comment = spa_strdup(strval);
5832 * We need to dirty the configuration on all the vdevs
5833 * so that their labels get updated. It's unnecessary
5834 * to do this for pool creation since the vdev's
5835 * configuratoin has already been dirtied.
5837 if (tx->tx_txg != TXG_INITIAL)
5838 vdev_config_dirty(spa->spa_root_vdev);
5842 * Set pool property values in the poolprops mos object.
5844 if (spa->spa_pool_props_object == 0) {
5845 spa->spa_pool_props_object =
5846 zap_create_link(mos, DMU_OT_POOL_PROPS,
5847 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5851 /* normalize the property name */
5852 propname = zpool_prop_to_name(prop);
5853 proptype = zpool_prop_get_type(prop);
5855 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5856 ASSERT(proptype == PROP_TYPE_STRING);
5857 VERIFY(nvpair_value_string(elem, &strval) == 0);
5858 VERIFY(zap_update(mos,
5859 spa->spa_pool_props_object, propname,
5860 1, strlen(strval) + 1, strval, tx) == 0);
5862 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5863 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5865 if (proptype == PROP_TYPE_INDEX) {
5867 VERIFY(zpool_prop_index_to_string(
5868 prop, intval, &unused) == 0);
5870 VERIFY(zap_update(mos,
5871 spa->spa_pool_props_object, propname,
5872 8, 1, &intval, tx) == 0);
5874 ASSERT(0); /* not allowed */
5878 case ZPOOL_PROP_DELEGATION:
5879 spa->spa_delegation = intval;
5881 case ZPOOL_PROP_BOOTFS:
5882 spa->spa_bootfs = intval;
5884 case ZPOOL_PROP_FAILUREMODE:
5885 spa->spa_failmode = intval;
5887 case ZPOOL_PROP_AUTOEXPAND:
5888 spa->spa_autoexpand = intval;
5889 if (tx->tx_txg != TXG_INITIAL)
5890 spa_async_request(spa,
5891 SPA_ASYNC_AUTOEXPAND);
5893 case ZPOOL_PROP_DEDUPDITTO:
5894 spa->spa_dedup_ditto = intval;
5901 /* log internal history if this is not a zpool create */
5902 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5903 tx->tx_txg != TXG_INITIAL) {
5904 spa_history_log_internal(LOG_POOL_PROPSET,
5905 spa, tx, "%s %lld %s",
5906 nvpair_name(elem), intval, spa_name(spa));
5910 mutex_exit(&spa->spa_props_lock);
5914 * Perform one-time upgrade on-disk changes. spa_version() does not
5915 * reflect the new version this txg, so there must be no changes this
5916 * txg to anything that the upgrade code depends on after it executes.
5917 * Therefore this must be called after dsl_pool_sync() does the sync
5921 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5923 dsl_pool_t *dp = spa->spa_dsl_pool;
5925 ASSERT(spa->spa_sync_pass == 1);
5927 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5928 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5929 dsl_pool_create_origin(dp, tx);
5931 /* Keeping the origin open increases spa_minref */
5932 spa->spa_minref += 3;
5935 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5936 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5937 dsl_pool_upgrade_clones(dp, tx);
5940 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5941 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5942 dsl_pool_upgrade_dir_clones(dp, tx);
5944 /* Keeping the freedir open increases spa_minref */
5945 spa->spa_minref += 3;
5948 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
5949 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
5950 spa_feature_create_zap_objects(spa, tx);
5955 * Sync the specified transaction group. New blocks may be dirtied as
5956 * part of the process, so we iterate until it converges.
5959 spa_sync(spa_t *spa, uint64_t txg)
5961 dsl_pool_t *dp = spa->spa_dsl_pool;
5962 objset_t *mos = spa->spa_meta_objset;
5963 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
5964 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
5965 vdev_t *rvd = spa->spa_root_vdev;
5971 VERIFY(spa_writeable(spa));
5974 * Lock out configuration changes.
5976 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5978 spa->spa_syncing_txg = txg;
5979 spa->spa_sync_pass = 0;
5982 * If there are any pending vdev state changes, convert them
5983 * into config changes that go out with this transaction group.
5985 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5986 while (list_head(&spa->spa_state_dirty_list) != NULL) {
5988 * We need the write lock here because, for aux vdevs,
5989 * calling vdev_config_dirty() modifies sav_config.
5990 * This is ugly and will become unnecessary when we
5991 * eliminate the aux vdev wart by integrating all vdevs
5992 * into the root vdev tree.
5994 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5995 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
5996 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
5997 vdev_state_clean(vd);
5998 vdev_config_dirty(vd);
6000 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6001 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6003 spa_config_exit(spa, SCL_STATE, FTAG);
6005 tx = dmu_tx_create_assigned(dp, txg);
6008 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6009 * set spa_deflate if we have no raid-z vdevs.
6011 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6012 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6015 for (i = 0; i < rvd->vdev_children; i++) {
6016 vd = rvd->vdev_child[i];
6017 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6020 if (i == rvd->vdev_children) {
6021 spa->spa_deflate = TRUE;
6022 VERIFY(0 == zap_add(spa->spa_meta_objset,
6023 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6024 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6029 * If anything has changed in this txg, or if someone is waiting
6030 * for this txg to sync (eg, spa_vdev_remove()), push the
6031 * deferred frees from the previous txg. If not, leave them
6032 * alone so that we don't generate work on an otherwise idle
6035 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6036 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6037 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6038 ((dsl_scan_active(dp->dp_scan) ||
6039 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6040 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6041 VERIFY3U(bpobj_iterate(defer_bpo,
6042 spa_free_sync_cb, zio, tx), ==, 0);
6043 VERIFY3U(zio_wait(zio), ==, 0);
6047 * Iterate to convergence.
6050 int pass = ++spa->spa_sync_pass;
6052 spa_sync_config_object(spa, tx);
6053 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6054 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6055 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6056 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6057 spa_errlog_sync(spa, txg);
6058 dsl_pool_sync(dp, txg);
6060 if (pass <= SYNC_PASS_DEFERRED_FREE) {
6061 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6062 bplist_iterate(free_bpl, spa_free_sync_cb,
6064 VERIFY(zio_wait(zio) == 0);
6066 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6071 dsl_scan_sync(dp, tx);
6073 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)))
6077 spa_sync_upgrades(spa, tx);
6079 } while (dmu_objset_is_dirty(mos, txg));
6082 * Rewrite the vdev configuration (which includes the uberblock)
6083 * to commit the transaction group.
6085 * If there are no dirty vdevs, we sync the uberblock to a few
6086 * random top-level vdevs that are known to be visible in the
6087 * config cache (see spa_vdev_add() for a complete description).
6088 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6092 * We hold SCL_STATE to prevent vdev open/close/etc.
6093 * while we're attempting to write the vdev labels.
6095 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6097 if (list_is_empty(&spa->spa_config_dirty_list)) {
6098 vdev_t *svd[SPA_DVAS_PER_BP];
6100 int children = rvd->vdev_children;
6101 int c0 = spa_get_random(children);
6103 for (c = 0; c < children; c++) {
6104 vd = rvd->vdev_child[(c0 + c) % children];
6105 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6107 svd[svdcount++] = vd;
6108 if (svdcount == SPA_DVAS_PER_BP)
6111 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6113 error = vdev_config_sync(svd, svdcount, txg,
6116 error = vdev_config_sync(rvd->vdev_child,
6117 rvd->vdev_children, txg, B_FALSE);
6119 error = vdev_config_sync(rvd->vdev_child,
6120 rvd->vdev_children, txg, B_TRUE);
6124 spa->spa_last_synced_guid = rvd->vdev_guid;
6126 spa_config_exit(spa, SCL_STATE, FTAG);
6130 zio_suspend(spa, NULL);
6131 zio_resume_wait(spa);
6136 * Clear the dirty config list.
6138 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6139 vdev_config_clean(vd);
6142 * Now that the new config has synced transactionally,
6143 * let it become visible to the config cache.
6145 if (spa->spa_config_syncing != NULL) {
6146 spa_config_set(spa, spa->spa_config_syncing);
6147 spa->spa_config_txg = txg;
6148 spa->spa_config_syncing = NULL;
6151 spa->spa_ubsync = spa->spa_uberblock;
6153 dsl_pool_sync_done(dp, txg);
6156 * Update usable space statistics.
6158 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))))
6159 vdev_sync_done(vd, txg);
6161 spa_update_dspace(spa);
6164 * It had better be the case that we didn't dirty anything
6165 * since vdev_config_sync().
6167 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6168 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6169 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6171 spa->spa_sync_pass = 0;
6173 spa_config_exit(spa, SCL_CONFIG, FTAG);
6175 spa_handle_ignored_writes(spa);
6178 * If any async tasks have been requested, kick them off.
6180 spa_async_dispatch(spa);
6184 * Sync all pools. We don't want to hold the namespace lock across these
6185 * operations, so we take a reference on the spa_t and drop the lock during the
6189 spa_sync_allpools(void)
6192 mutex_enter(&spa_namespace_lock);
6193 while ((spa = spa_next(spa)) != NULL) {
6194 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6195 !spa_writeable(spa) || spa_suspended(spa))
6197 spa_open_ref(spa, FTAG);
6198 mutex_exit(&spa_namespace_lock);
6199 txg_wait_synced(spa_get_dsl(spa), 0);
6200 mutex_enter(&spa_namespace_lock);
6201 spa_close(spa, FTAG);
6203 mutex_exit(&spa_namespace_lock);
6207 * ==========================================================================
6208 * Miscellaneous routines
6209 * ==========================================================================
6213 * Remove all pools in the system.
6221 * Remove all cached state. All pools should be closed now,
6222 * so every spa in the AVL tree should be unreferenced.
6224 mutex_enter(&spa_namespace_lock);
6225 while ((spa = spa_next(NULL)) != NULL) {
6227 * Stop async tasks. The async thread may need to detach
6228 * a device that's been replaced, which requires grabbing
6229 * spa_namespace_lock, so we must drop it here.
6231 spa_open_ref(spa, FTAG);
6232 mutex_exit(&spa_namespace_lock);
6233 spa_async_suspend(spa);
6234 mutex_enter(&spa_namespace_lock);
6235 spa_close(spa, FTAG);
6237 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6239 spa_deactivate(spa);
6243 mutex_exit(&spa_namespace_lock);
6247 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6252 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6256 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6257 vd = spa->spa_l2cache.sav_vdevs[i];
6258 if (vd->vdev_guid == guid)
6262 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6263 vd = spa->spa_spares.sav_vdevs[i];
6264 if (vd->vdev_guid == guid)
6273 spa_upgrade(spa_t *spa, uint64_t version)
6275 ASSERT(spa_writeable(spa));
6277 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6280 * This should only be called for a non-faulted pool, and since a
6281 * future version would result in an unopenable pool, this shouldn't be
6284 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
6285 ASSERT(version >= spa->spa_uberblock.ub_version);
6287 spa->spa_uberblock.ub_version = version;
6288 vdev_config_dirty(spa->spa_root_vdev);
6290 spa_config_exit(spa, SCL_ALL, FTAG);
6292 txg_wait_synced(spa_get_dsl(spa), 0);
6296 spa_has_spare(spa_t *spa, uint64_t guid)
6300 spa_aux_vdev_t *sav = &spa->spa_spares;
6302 for (i = 0; i < sav->sav_count; i++)
6303 if (sav->sav_vdevs[i]->vdev_guid == guid)
6306 for (i = 0; i < sav->sav_npending; i++) {
6307 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6308 &spareguid) == 0 && spareguid == guid)
6316 * Check if a pool has an active shared spare device.
6317 * Note: reference count of an active spare is 2, as a spare and as a replace
6320 spa_has_active_shared_spare(spa_t *spa)
6324 spa_aux_vdev_t *sav = &spa->spa_spares;
6326 for (i = 0; i < sav->sav_count; i++) {
6327 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6328 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6337 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
6338 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6339 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6340 * or zdb as real changes.
6343 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6346 zfs_ereport_post(name, spa, vd, NULL, 0, 0);
6350 #if defined(_KERNEL) && defined(HAVE_SPL)
6351 /* state manipulation functions */
6352 EXPORT_SYMBOL(spa_open);
6353 EXPORT_SYMBOL(spa_open_rewind);
6354 EXPORT_SYMBOL(spa_get_stats);
6355 EXPORT_SYMBOL(spa_create);
6356 EXPORT_SYMBOL(spa_import_rootpool);
6357 EXPORT_SYMBOL(spa_import);
6358 EXPORT_SYMBOL(spa_tryimport);
6359 EXPORT_SYMBOL(spa_destroy);
6360 EXPORT_SYMBOL(spa_export);
6361 EXPORT_SYMBOL(spa_reset);
6362 EXPORT_SYMBOL(spa_async_request);
6363 EXPORT_SYMBOL(spa_async_suspend);
6364 EXPORT_SYMBOL(spa_async_resume);
6365 EXPORT_SYMBOL(spa_inject_addref);
6366 EXPORT_SYMBOL(spa_inject_delref);
6367 EXPORT_SYMBOL(spa_scan_stat_init);
6368 EXPORT_SYMBOL(spa_scan_get_stats);
6370 /* device maniion */
6371 EXPORT_SYMBOL(spa_vdev_add);
6372 EXPORT_SYMBOL(spa_vdev_attach);
6373 EXPORT_SYMBOL(spa_vdev_detach);
6374 EXPORT_SYMBOL(spa_vdev_remove);
6375 EXPORT_SYMBOL(spa_vdev_setpath);
6376 EXPORT_SYMBOL(spa_vdev_setfru);
6377 EXPORT_SYMBOL(spa_vdev_split_mirror);
6379 /* spare statech is global across all pools) */
6380 EXPORT_SYMBOL(spa_spare_add);
6381 EXPORT_SYMBOL(spa_spare_remove);
6382 EXPORT_SYMBOL(spa_spare_exists);
6383 EXPORT_SYMBOL(spa_spare_activate);
6385 /* L2ARC statech is global across all pools) */
6386 EXPORT_SYMBOL(spa_l2cache_add);
6387 EXPORT_SYMBOL(spa_l2cache_remove);
6388 EXPORT_SYMBOL(spa_l2cache_exists);
6389 EXPORT_SYMBOL(spa_l2cache_activate);
6390 EXPORT_SYMBOL(spa_l2cache_drop);
6393 EXPORT_SYMBOL(spa_scan);
6394 EXPORT_SYMBOL(spa_scan_stop);
6397 EXPORT_SYMBOL(spa_sync); /* only for DMU use */
6398 EXPORT_SYMBOL(spa_sync_allpools);
6401 EXPORT_SYMBOL(spa_prop_set);
6402 EXPORT_SYMBOL(spa_prop_get);
6403 EXPORT_SYMBOL(spa_prop_clear_bootfs);
6405 /* asynchronous event notification */
6406 EXPORT_SYMBOL(spa_event_notify);