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 boolean_t spa_has_active_shared_spare(spa_t *spa);
121 static inline int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
122 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
124 static void spa_vdev_resilver_done(spa_t *spa);
126 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
127 id_t zio_taskq_psrset_bind = PS_NONE;
128 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
129 uint_t zio_taskq_basedc = 80; /* base duty cycle */
131 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
134 * This (illegal) pool name is used when temporarily importing a spa_t in order
135 * to get the vdev stats associated with the imported devices.
137 #define TRYIMPORT_NAME "$import"
140 * ==========================================================================
141 * SPA properties routines
142 * ==========================================================================
146 * Add a (source=src, propname=propval) list to an nvlist.
149 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
150 uint64_t intval, zprop_source_t src)
152 const char *propname = zpool_prop_to_name(prop);
155 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
156 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
159 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
161 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
163 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
164 nvlist_free(propval);
168 * Get property values from the spa configuration.
171 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
173 vdev_t *rvd = spa->spa_root_vdev;
174 dsl_pool_t *pool = spa->spa_dsl_pool;
178 uint64_t cap, version;
179 zprop_source_t src = ZPROP_SRC_NONE;
180 spa_config_dirent_t *dp;
183 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
186 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
187 size = metaslab_class_get_space(spa_normal_class(spa));
188 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
189 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
190 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
191 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
195 for (c = 0; c < rvd->vdev_children; c++) {
196 vdev_t *tvd = rvd->vdev_child[c];
197 space += tvd->vdev_max_asize - tvd->vdev_asize;
199 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
202 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
203 (spa_mode(spa) == FREAD), src);
205 cap = (size == 0) ? 0 : (alloc * 100 / size);
206 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
208 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
209 ddt_get_pool_dedup_ratio(spa), src);
211 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
212 rvd->vdev_state, src);
214 version = spa_version(spa);
215 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
216 src = ZPROP_SRC_DEFAULT;
218 src = ZPROP_SRC_LOCAL;
219 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
223 dsl_dir_t *freedir = pool->dp_free_dir;
226 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
227 * when opening pools before this version freedir will be NULL.
229 if (freedir != NULL) {
230 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
231 freedir->dd_phys->dd_used_bytes, src);
233 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
238 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
240 if (spa->spa_comment != NULL) {
241 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
245 if (spa->spa_root != NULL)
246 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
249 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
250 if (dp->scd_path == NULL) {
251 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
252 "none", 0, ZPROP_SRC_LOCAL);
253 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
254 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
255 dp->scd_path, 0, ZPROP_SRC_LOCAL);
261 * Get zpool property values.
264 spa_prop_get(spa_t *spa, nvlist_t **nvp)
266 objset_t *mos = spa->spa_meta_objset;
271 err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_PUSHPAGE);
275 mutex_enter(&spa->spa_props_lock);
278 * Get properties from the spa config.
280 spa_prop_get_config(spa, nvp);
282 /* If no pool property object, no more prop to get. */
283 if (mos == NULL || spa->spa_pool_props_object == 0) {
284 mutex_exit(&spa->spa_props_lock);
289 * Get properties from the MOS pool property object.
291 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
292 (err = zap_cursor_retrieve(&zc, &za)) == 0;
293 zap_cursor_advance(&zc)) {
296 zprop_source_t src = ZPROP_SRC_DEFAULT;
299 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
302 switch (za.za_integer_length) {
304 /* integer property */
305 if (za.za_first_integer !=
306 zpool_prop_default_numeric(prop))
307 src = ZPROP_SRC_LOCAL;
309 if (prop == ZPOOL_PROP_BOOTFS) {
311 dsl_dataset_t *ds = NULL;
313 dp = spa_get_dsl(spa);
314 rw_enter(&dp->dp_config_rwlock, RW_READER);
315 if ((err = dsl_dataset_hold_obj(dp,
316 za.za_first_integer, FTAG, &ds))) {
317 rw_exit(&dp->dp_config_rwlock);
322 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
324 dsl_dataset_name(ds, strval);
325 dsl_dataset_rele(ds, FTAG);
326 rw_exit(&dp->dp_config_rwlock);
329 intval = za.za_first_integer;
332 spa_prop_add_list(*nvp, prop, strval, intval, src);
336 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
341 /* string property */
342 strval = kmem_alloc(za.za_num_integers, KM_PUSHPAGE);
343 err = zap_lookup(mos, spa->spa_pool_props_object,
344 za.za_name, 1, za.za_num_integers, strval);
346 kmem_free(strval, za.za_num_integers);
349 spa_prop_add_list(*nvp, prop, strval, 0, src);
350 kmem_free(strval, za.za_num_integers);
357 zap_cursor_fini(&zc);
358 mutex_exit(&spa->spa_props_lock);
360 if (err && err != ENOENT) {
370 * Validate the given pool properties nvlist and modify the list
371 * for the property values to be set.
374 spa_prop_validate(spa_t *spa, nvlist_t *props)
377 int error = 0, reset_bootfs = 0;
379 boolean_t has_feature = B_FALSE;
382 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
384 char *strval, *slash, *check, *fname;
385 const char *propname = nvpair_name(elem);
386 zpool_prop_t prop = zpool_name_to_prop(propname);
390 if (!zpool_prop_feature(propname)) {
396 * Sanitize the input.
398 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
403 if (nvpair_value_uint64(elem, &intval) != 0) {
413 fname = strchr(propname, '@') + 1;
414 if (zfeature_lookup_name(fname, NULL) != 0) {
419 has_feature = B_TRUE;
422 case ZPOOL_PROP_VERSION:
423 error = nvpair_value_uint64(elem, &intval);
425 (intval < spa_version(spa) ||
426 intval > SPA_VERSION_BEFORE_FEATURES ||
431 case ZPOOL_PROP_DELEGATION:
432 case ZPOOL_PROP_AUTOREPLACE:
433 case ZPOOL_PROP_LISTSNAPS:
434 case ZPOOL_PROP_AUTOEXPAND:
435 error = nvpair_value_uint64(elem, &intval);
436 if (!error && intval > 1)
440 case ZPOOL_PROP_BOOTFS:
442 * If the pool version is less than SPA_VERSION_BOOTFS,
443 * or the pool is still being created (version == 0),
444 * the bootfs property cannot be set.
446 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
452 * Make sure the vdev config is bootable
454 if (!vdev_is_bootable(spa->spa_root_vdev)) {
461 error = nvpair_value_string(elem, &strval);
467 if (strval == NULL || strval[0] == '\0') {
468 objnum = zpool_prop_default_numeric(
473 if ((error = dmu_objset_hold(strval,FTAG,&os)))
476 /* Must be ZPL and not gzip compressed. */
478 if (dmu_objset_type(os) != DMU_OST_ZFS) {
480 } else if ((error = dsl_prop_get_integer(strval,
481 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
482 &compress, NULL)) == 0 &&
483 !BOOTFS_COMPRESS_VALID(compress)) {
486 objnum = dmu_objset_id(os);
488 dmu_objset_rele(os, FTAG);
492 case ZPOOL_PROP_FAILUREMODE:
493 error = nvpair_value_uint64(elem, &intval);
494 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
495 intval > ZIO_FAILURE_MODE_PANIC))
499 * This is a special case which only occurs when
500 * the pool has completely failed. This allows
501 * the user to change the in-core failmode property
502 * without syncing it out to disk (I/Os might
503 * currently be blocked). We do this by returning
504 * EIO to the caller (spa_prop_set) to trick it
505 * into thinking we encountered a property validation
508 if (!error && spa_suspended(spa)) {
509 spa->spa_failmode = intval;
514 case ZPOOL_PROP_CACHEFILE:
515 if ((error = nvpair_value_string(elem, &strval)) != 0)
518 if (strval[0] == '\0')
521 if (strcmp(strval, "none") == 0)
524 if (strval[0] != '/') {
529 slash = strrchr(strval, '/');
530 ASSERT(slash != NULL);
532 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
533 strcmp(slash, "/..") == 0)
537 case ZPOOL_PROP_COMMENT:
538 if ((error = nvpair_value_string(elem, &strval)) != 0)
540 for (check = strval; *check != '\0'; check++) {
541 if (!isprint(*check)) {
547 if (strlen(strval) > ZPROP_MAX_COMMENT)
551 case ZPOOL_PROP_DEDUPDITTO:
552 if (spa_version(spa) < SPA_VERSION_DEDUP)
555 error = nvpair_value_uint64(elem, &intval);
557 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
569 if (!error && reset_bootfs) {
570 error = nvlist_remove(props,
571 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
574 error = nvlist_add_uint64(props,
575 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
583 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
586 spa_config_dirent_t *dp;
588 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
592 dp = kmem_alloc(sizeof (spa_config_dirent_t),
595 if (cachefile[0] == '\0')
596 dp->scd_path = spa_strdup(spa_config_path);
597 else if (strcmp(cachefile, "none") == 0)
600 dp->scd_path = spa_strdup(cachefile);
602 list_insert_head(&spa->spa_config_list, dp);
604 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
608 spa_prop_set(spa_t *spa, nvlist_t *nvp)
611 nvpair_t *elem = NULL;
612 boolean_t need_sync = B_FALSE;
614 if ((error = spa_prop_validate(spa, nvp)) != 0)
617 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
618 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
620 if (prop == ZPOOL_PROP_CACHEFILE ||
621 prop == ZPOOL_PROP_ALTROOT ||
622 prop == ZPOOL_PROP_READONLY)
625 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
628 if (prop == ZPOOL_PROP_VERSION) {
629 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
631 ASSERT(zpool_prop_feature(nvpair_name(elem)));
632 ver = SPA_VERSION_FEATURES;
636 /* Save time if the version is already set. */
637 if (ver == spa_version(spa))
641 * In addition to the pool directory object, we might
642 * create the pool properties object, the features for
643 * read object, the features for write object, or the
644 * feature descriptions object.
646 error = dsl_sync_task_do(spa_get_dsl(spa), NULL,
647 spa_sync_version, spa, &ver, 6);
658 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
666 * If the bootfs property value is dsobj, clear it.
669 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
671 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
672 VERIFY(zap_remove(spa->spa_meta_objset,
673 spa->spa_pool_props_object,
674 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
680 * Change the GUID for the pool. This is done so that we can later
681 * re-import a pool built from a clone of our own vdevs. We will modify
682 * the root vdev's guid, our own pool guid, and then mark all of our
683 * vdevs dirty. Note that we must make sure that all our vdevs are
684 * online when we do this, or else any vdevs that weren't present
685 * would be orphaned from our pool. We are also going to issue a
686 * sysevent to update any watchers.
689 spa_change_guid(spa_t *spa)
691 uint64_t oldguid, newguid;
694 if (!(spa_mode_global & FWRITE))
697 txg = spa_vdev_enter(spa);
699 if (spa->spa_root_vdev->vdev_state != VDEV_STATE_HEALTHY)
700 return (spa_vdev_exit(spa, NULL, txg, ENXIO));
702 oldguid = spa_guid(spa);
703 newguid = spa_generate_guid(NULL);
704 ASSERT3U(oldguid, !=, newguid);
706 spa->spa_root_vdev->vdev_guid = newguid;
707 spa->spa_root_vdev->vdev_guid_sum += (newguid - oldguid);
709 vdev_config_dirty(spa->spa_root_vdev);
711 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_REGUID);
713 return (spa_vdev_exit(spa, NULL, txg, 0));
717 * ==========================================================================
718 * SPA state manipulation (open/create/destroy/import/export)
719 * ==========================================================================
723 spa_error_entry_compare(const void *a, const void *b)
725 spa_error_entry_t *sa = (spa_error_entry_t *)a;
726 spa_error_entry_t *sb = (spa_error_entry_t *)b;
729 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
730 sizeof (zbookmark_t));
741 * Utility function which retrieves copies of the current logs and
742 * re-initializes them in the process.
745 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
747 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
749 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
750 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
752 avl_create(&spa->spa_errlist_scrub,
753 spa_error_entry_compare, sizeof (spa_error_entry_t),
754 offsetof(spa_error_entry_t, se_avl));
755 avl_create(&spa->spa_errlist_last,
756 spa_error_entry_compare, sizeof (spa_error_entry_t),
757 offsetof(spa_error_entry_t, se_avl));
761 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
764 uint_t flags = TASKQ_PREPOPULATE;
765 boolean_t batch = B_FALSE;
769 return (NULL); /* no taskq needed */
772 ASSERT3U(value, >=, 1);
773 value = MAX(value, 1);
778 flags |= TASKQ_THREADS_CPU_PCT;
779 value = zio_taskq_batch_pct;
782 case zti_mode_online_percent:
783 flags |= TASKQ_THREADS_CPU_PCT;
787 panic("unrecognized mode for %s taskq (%u:%u) in "
793 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
795 flags |= TASKQ_DC_BATCH;
797 return (taskq_create_sysdc(name, value, 50, INT_MAX,
798 spa->spa_proc, zio_taskq_basedc, flags));
800 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
801 spa->spa_proc, flags));
805 spa_create_zio_taskqs(spa_t *spa)
809 for (t = 0; t < ZIO_TYPES; t++) {
810 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
811 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
812 enum zti_modes mode = ztip->zti_mode;
813 uint_t value = ztip->zti_value;
816 (void) snprintf(name, sizeof (name),
817 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
819 spa->spa_zio_taskq[t][q] =
820 spa_taskq_create(spa, name, mode, value);
825 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
827 spa_thread(void *arg)
832 user_t *pu = PTOU(curproc);
834 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
837 ASSERT(curproc != &p0);
838 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
839 "zpool-%s", spa->spa_name);
840 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
842 /* bind this thread to the requested psrset */
843 if (zio_taskq_psrset_bind != PS_NONE) {
845 mutex_enter(&cpu_lock);
846 mutex_enter(&pidlock);
847 mutex_enter(&curproc->p_lock);
849 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
850 0, NULL, NULL) == 0) {
851 curthread->t_bind_pset = zio_taskq_psrset_bind;
854 "Couldn't bind process for zfs pool \"%s\" to "
855 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
858 mutex_exit(&curproc->p_lock);
859 mutex_exit(&pidlock);
860 mutex_exit(&cpu_lock);
864 if (zio_taskq_sysdc) {
865 sysdc_thread_enter(curthread, 100, 0);
868 spa->spa_proc = curproc;
869 spa->spa_did = curthread->t_did;
871 spa_create_zio_taskqs(spa);
873 mutex_enter(&spa->spa_proc_lock);
874 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
876 spa->spa_proc_state = SPA_PROC_ACTIVE;
877 cv_broadcast(&spa->spa_proc_cv);
879 CALLB_CPR_SAFE_BEGIN(&cprinfo);
880 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
881 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
882 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
884 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
885 spa->spa_proc_state = SPA_PROC_GONE;
887 cv_broadcast(&spa->spa_proc_cv);
888 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
890 mutex_enter(&curproc->p_lock);
896 * Activate an uninitialized pool.
899 spa_activate(spa_t *spa, int mode)
901 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
903 spa->spa_state = POOL_STATE_ACTIVE;
904 spa->spa_mode = mode;
906 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
907 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
909 /* Try to create a covering process */
910 mutex_enter(&spa->spa_proc_lock);
911 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
912 ASSERT(spa->spa_proc == &p0);
915 #ifdef HAVE_SPA_THREAD
916 /* Only create a process if we're going to be around a while. */
917 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
918 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
920 spa->spa_proc_state = SPA_PROC_CREATED;
921 while (spa->spa_proc_state == SPA_PROC_CREATED) {
922 cv_wait(&spa->spa_proc_cv,
923 &spa->spa_proc_lock);
925 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
926 ASSERT(spa->spa_proc != &p0);
927 ASSERT(spa->spa_did != 0);
931 "Couldn't create process for zfs pool \"%s\"\n",
936 #endif /* HAVE_SPA_THREAD */
937 mutex_exit(&spa->spa_proc_lock);
939 /* If we didn't create a process, we need to create our taskqs. */
940 if (spa->spa_proc == &p0) {
941 spa_create_zio_taskqs(spa);
944 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
945 offsetof(vdev_t, vdev_config_dirty_node));
946 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
947 offsetof(vdev_t, vdev_state_dirty_node));
949 txg_list_create(&spa->spa_vdev_txg_list,
950 offsetof(struct vdev, vdev_txg_node));
952 avl_create(&spa->spa_errlist_scrub,
953 spa_error_entry_compare, sizeof (spa_error_entry_t),
954 offsetof(spa_error_entry_t, se_avl));
955 avl_create(&spa->spa_errlist_last,
956 spa_error_entry_compare, sizeof (spa_error_entry_t),
957 offsetof(spa_error_entry_t, se_avl));
961 * Opposite of spa_activate().
964 spa_deactivate(spa_t *spa)
968 ASSERT(spa->spa_sync_on == B_FALSE);
969 ASSERT(spa->spa_dsl_pool == NULL);
970 ASSERT(spa->spa_root_vdev == NULL);
971 ASSERT(spa->spa_async_zio_root == NULL);
972 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
974 txg_list_destroy(&spa->spa_vdev_txg_list);
976 list_destroy(&spa->spa_config_dirty_list);
977 list_destroy(&spa->spa_state_dirty_list);
979 for (t = 0; t < ZIO_TYPES; t++) {
980 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
981 if (spa->spa_zio_taskq[t][q] != NULL)
982 taskq_destroy(spa->spa_zio_taskq[t][q]);
983 spa->spa_zio_taskq[t][q] = NULL;
987 metaslab_class_destroy(spa->spa_normal_class);
988 spa->spa_normal_class = NULL;
990 metaslab_class_destroy(spa->spa_log_class);
991 spa->spa_log_class = NULL;
994 * If this was part of an import or the open otherwise failed, we may
995 * still have errors left in the queues. Empty them just in case.
997 spa_errlog_drain(spa);
999 avl_destroy(&spa->spa_errlist_scrub);
1000 avl_destroy(&spa->spa_errlist_last);
1002 spa->spa_state = POOL_STATE_UNINITIALIZED;
1004 mutex_enter(&spa->spa_proc_lock);
1005 if (spa->spa_proc_state != SPA_PROC_NONE) {
1006 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1007 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1008 cv_broadcast(&spa->spa_proc_cv);
1009 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1010 ASSERT(spa->spa_proc != &p0);
1011 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1013 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1014 spa->spa_proc_state = SPA_PROC_NONE;
1016 ASSERT(spa->spa_proc == &p0);
1017 mutex_exit(&spa->spa_proc_lock);
1020 * We want to make sure spa_thread() has actually exited the ZFS
1021 * module, so that the module can't be unloaded out from underneath
1024 if (spa->spa_did != 0) {
1025 thread_join(spa->spa_did);
1031 * Verify a pool configuration, and construct the vdev tree appropriately. This
1032 * will create all the necessary vdevs in the appropriate layout, with each vdev
1033 * in the CLOSED state. This will prep the pool before open/creation/import.
1034 * All vdev validation is done by the vdev_alloc() routine.
1037 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1038 uint_t id, int atype)
1045 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1048 if ((*vdp)->vdev_ops->vdev_op_leaf)
1051 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1054 if (error == ENOENT)
1063 for (c = 0; c < children; c++) {
1065 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1073 ASSERT(*vdp != NULL);
1079 * Opposite of spa_load().
1082 spa_unload(spa_t *spa)
1086 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1091 spa_async_suspend(spa);
1096 if (spa->spa_sync_on) {
1097 txg_sync_stop(spa->spa_dsl_pool);
1098 spa->spa_sync_on = B_FALSE;
1102 * Wait for any outstanding async I/O to complete.
1104 if (spa->spa_async_zio_root != NULL) {
1105 (void) zio_wait(spa->spa_async_zio_root);
1106 spa->spa_async_zio_root = NULL;
1109 bpobj_close(&spa->spa_deferred_bpobj);
1112 * Close the dsl pool.
1114 if (spa->spa_dsl_pool) {
1115 dsl_pool_close(spa->spa_dsl_pool);
1116 spa->spa_dsl_pool = NULL;
1117 spa->spa_meta_objset = NULL;
1122 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1125 * Drop and purge level 2 cache
1127 spa_l2cache_drop(spa);
1132 if (spa->spa_root_vdev)
1133 vdev_free(spa->spa_root_vdev);
1134 ASSERT(spa->spa_root_vdev == NULL);
1136 for (i = 0; i < spa->spa_spares.sav_count; i++)
1137 vdev_free(spa->spa_spares.sav_vdevs[i]);
1138 if (spa->spa_spares.sav_vdevs) {
1139 kmem_free(spa->spa_spares.sav_vdevs,
1140 spa->spa_spares.sav_count * sizeof (void *));
1141 spa->spa_spares.sav_vdevs = NULL;
1143 if (spa->spa_spares.sav_config) {
1144 nvlist_free(spa->spa_spares.sav_config);
1145 spa->spa_spares.sav_config = NULL;
1147 spa->spa_spares.sav_count = 0;
1149 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1150 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1151 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1153 if (spa->spa_l2cache.sav_vdevs) {
1154 kmem_free(spa->spa_l2cache.sav_vdevs,
1155 spa->spa_l2cache.sav_count * sizeof (void *));
1156 spa->spa_l2cache.sav_vdevs = NULL;
1158 if (spa->spa_l2cache.sav_config) {
1159 nvlist_free(spa->spa_l2cache.sav_config);
1160 spa->spa_l2cache.sav_config = NULL;
1162 spa->spa_l2cache.sav_count = 0;
1164 spa->spa_async_suspended = 0;
1166 if (spa->spa_comment != NULL) {
1167 spa_strfree(spa->spa_comment);
1168 spa->spa_comment = NULL;
1171 spa_config_exit(spa, SCL_ALL, FTAG);
1175 * Load (or re-load) the current list of vdevs describing the active spares for
1176 * this pool. When this is called, we have some form of basic information in
1177 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1178 * then re-generate a more complete list including status information.
1181 spa_load_spares(spa_t *spa)
1188 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1191 * First, close and free any existing spare vdevs.
1193 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1194 vd = spa->spa_spares.sav_vdevs[i];
1196 /* Undo the call to spa_activate() below */
1197 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1198 B_FALSE)) != NULL && tvd->vdev_isspare)
1199 spa_spare_remove(tvd);
1204 if (spa->spa_spares.sav_vdevs)
1205 kmem_free(spa->spa_spares.sav_vdevs,
1206 spa->spa_spares.sav_count * sizeof (void *));
1208 if (spa->spa_spares.sav_config == NULL)
1211 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1212 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1214 spa->spa_spares.sav_count = (int)nspares;
1215 spa->spa_spares.sav_vdevs = NULL;
1221 * Construct the array of vdevs, opening them to get status in the
1222 * process. For each spare, there is potentially two different vdev_t
1223 * structures associated with it: one in the list of spares (used only
1224 * for basic validation purposes) and one in the active vdev
1225 * configuration (if it's spared in). During this phase we open and
1226 * validate each vdev on the spare list. If the vdev also exists in the
1227 * active configuration, then we also mark this vdev as an active spare.
1229 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1231 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1232 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1233 VDEV_ALLOC_SPARE) == 0);
1236 spa->spa_spares.sav_vdevs[i] = vd;
1238 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1239 B_FALSE)) != NULL) {
1240 if (!tvd->vdev_isspare)
1244 * We only mark the spare active if we were successfully
1245 * able to load the vdev. Otherwise, importing a pool
1246 * with a bad active spare would result in strange
1247 * behavior, because multiple pool would think the spare
1248 * is actively in use.
1250 * There is a vulnerability here to an equally bizarre
1251 * circumstance, where a dead active spare is later
1252 * brought back to life (onlined or otherwise). Given
1253 * the rarity of this scenario, and the extra complexity
1254 * it adds, we ignore the possibility.
1256 if (!vdev_is_dead(tvd))
1257 spa_spare_activate(tvd);
1261 vd->vdev_aux = &spa->spa_spares;
1263 if (vdev_open(vd) != 0)
1266 if (vdev_validate_aux(vd) == 0)
1271 * Recompute the stashed list of spares, with status information
1274 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1275 DATA_TYPE_NVLIST_ARRAY) == 0);
1277 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1279 for (i = 0; i < spa->spa_spares.sav_count; i++)
1280 spares[i] = vdev_config_generate(spa,
1281 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1282 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1283 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1284 for (i = 0; i < spa->spa_spares.sav_count; i++)
1285 nvlist_free(spares[i]);
1286 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1290 * Load (or re-load) the current list of vdevs describing the active l2cache for
1291 * this pool. When this is called, we have some form of basic information in
1292 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1293 * then re-generate a more complete list including status information.
1294 * Devices which are already active have their details maintained, and are
1298 spa_load_l2cache(spa_t *spa)
1302 int i, j, oldnvdevs;
1304 vdev_t *vd, **oldvdevs, **newvdevs = NULL;
1305 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1307 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1309 if (sav->sav_config != NULL) {
1310 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1311 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1312 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_PUSHPAGE);
1317 oldvdevs = sav->sav_vdevs;
1318 oldnvdevs = sav->sav_count;
1319 sav->sav_vdevs = NULL;
1323 * Process new nvlist of vdevs.
1325 for (i = 0; i < nl2cache; i++) {
1326 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1330 for (j = 0; j < oldnvdevs; j++) {
1332 if (vd != NULL && guid == vd->vdev_guid) {
1334 * Retain previous vdev for add/remove ops.
1342 if (newvdevs[i] == NULL) {
1346 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1347 VDEV_ALLOC_L2CACHE) == 0);
1352 * Commit this vdev as an l2cache device,
1353 * even if it fails to open.
1355 spa_l2cache_add(vd);
1360 spa_l2cache_activate(vd);
1362 if (vdev_open(vd) != 0)
1365 (void) vdev_validate_aux(vd);
1367 if (!vdev_is_dead(vd))
1368 l2arc_add_vdev(spa, vd);
1373 * Purge vdevs that were dropped
1375 for (i = 0; i < oldnvdevs; i++) {
1380 ASSERT(vd->vdev_isl2cache);
1382 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1383 pool != 0ULL && l2arc_vdev_present(vd))
1384 l2arc_remove_vdev(vd);
1385 vdev_clear_stats(vd);
1391 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1393 if (sav->sav_config == NULL)
1396 sav->sav_vdevs = newvdevs;
1397 sav->sav_count = (int)nl2cache;
1400 * Recompute the stashed list of l2cache devices, with status
1401 * information this time.
1403 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1404 DATA_TYPE_NVLIST_ARRAY) == 0);
1406 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE);
1407 for (i = 0; i < sav->sav_count; i++)
1408 l2cache[i] = vdev_config_generate(spa,
1409 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1410 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1411 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1413 for (i = 0; i < sav->sav_count; i++)
1414 nvlist_free(l2cache[i]);
1416 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1420 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1423 char *packed = NULL;
1428 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1432 nvsize = *(uint64_t *)db->db_data;
1433 dmu_buf_rele(db, FTAG);
1435 packed = kmem_alloc(nvsize, KM_PUSHPAGE | KM_NODEBUG);
1436 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1439 error = nvlist_unpack(packed, nvsize, value, 0);
1440 kmem_free(packed, nvsize);
1446 * Checks to see if the given vdev could not be opened, in which case we post a
1447 * sysevent to notify the autoreplace code that the device has been removed.
1450 spa_check_removed(vdev_t *vd)
1454 for (c = 0; c < vd->vdev_children; c++)
1455 spa_check_removed(vd->vdev_child[c]);
1457 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1458 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE,
1459 vd->vdev_spa, vd, NULL, 0, 0);
1460 spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_CHECK);
1465 * Validate the current config against the MOS config
1468 spa_config_valid(spa_t *spa, nvlist_t *config)
1470 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1474 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1476 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1477 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1479 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1482 * If we're doing a normal import, then build up any additional
1483 * diagnostic information about missing devices in this config.
1484 * We'll pass this up to the user for further processing.
1486 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1487 nvlist_t **child, *nv;
1490 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1492 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
1494 for (c = 0; c < rvd->vdev_children; c++) {
1495 vdev_t *tvd = rvd->vdev_child[c];
1496 vdev_t *mtvd = mrvd->vdev_child[c];
1498 if (tvd->vdev_ops == &vdev_missing_ops &&
1499 mtvd->vdev_ops != &vdev_missing_ops &&
1501 child[idx++] = vdev_config_generate(spa, mtvd,
1506 VERIFY(nvlist_add_nvlist_array(nv,
1507 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1508 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1509 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1511 for (i = 0; i < idx; i++)
1512 nvlist_free(child[i]);
1515 kmem_free(child, rvd->vdev_children * sizeof (char **));
1519 * Compare the root vdev tree with the information we have
1520 * from the MOS config (mrvd). Check each top-level vdev
1521 * with the corresponding MOS config top-level (mtvd).
1523 for (c = 0; c < rvd->vdev_children; c++) {
1524 vdev_t *tvd = rvd->vdev_child[c];
1525 vdev_t *mtvd = mrvd->vdev_child[c];
1528 * Resolve any "missing" vdevs in the current configuration.
1529 * If we find that the MOS config has more accurate information
1530 * about the top-level vdev then use that vdev instead.
1532 if (tvd->vdev_ops == &vdev_missing_ops &&
1533 mtvd->vdev_ops != &vdev_missing_ops) {
1535 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1539 * Device specific actions.
1541 if (mtvd->vdev_islog) {
1542 spa_set_log_state(spa, SPA_LOG_CLEAR);
1545 * XXX - once we have 'readonly' pool
1546 * support we should be able to handle
1547 * missing data devices by transitioning
1548 * the pool to readonly.
1554 * Swap the missing vdev with the data we were
1555 * able to obtain from the MOS config.
1557 vdev_remove_child(rvd, tvd);
1558 vdev_remove_child(mrvd, mtvd);
1560 vdev_add_child(rvd, mtvd);
1561 vdev_add_child(mrvd, tvd);
1563 spa_config_exit(spa, SCL_ALL, FTAG);
1565 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1568 } else if (mtvd->vdev_islog) {
1570 * Load the slog device's state from the MOS config
1571 * since it's possible that the label does not
1572 * contain the most up-to-date information.
1574 vdev_load_log_state(tvd, mtvd);
1579 spa_config_exit(spa, SCL_ALL, FTAG);
1582 * Ensure we were able to validate the config.
1584 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1588 * Check for missing log devices
1591 spa_check_logs(spa_t *spa)
1593 switch (spa->spa_log_state) {
1596 case SPA_LOG_MISSING:
1597 /* need to recheck in case slog has been restored */
1598 case SPA_LOG_UNKNOWN:
1599 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1600 DS_FIND_CHILDREN)) {
1601 spa_set_log_state(spa, SPA_LOG_MISSING);
1610 spa_passivate_log(spa_t *spa)
1612 vdev_t *rvd = spa->spa_root_vdev;
1613 boolean_t slog_found = B_FALSE;
1616 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1618 if (!spa_has_slogs(spa))
1621 for (c = 0; c < rvd->vdev_children; c++) {
1622 vdev_t *tvd = rvd->vdev_child[c];
1623 metaslab_group_t *mg = tvd->vdev_mg;
1625 if (tvd->vdev_islog) {
1626 metaslab_group_passivate(mg);
1627 slog_found = B_TRUE;
1631 return (slog_found);
1635 spa_activate_log(spa_t *spa)
1637 vdev_t *rvd = spa->spa_root_vdev;
1640 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1642 for (c = 0; c < rvd->vdev_children; c++) {
1643 vdev_t *tvd = rvd->vdev_child[c];
1644 metaslab_group_t *mg = tvd->vdev_mg;
1646 if (tvd->vdev_islog)
1647 metaslab_group_activate(mg);
1652 spa_offline_log(spa_t *spa)
1656 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1657 NULL, DS_FIND_CHILDREN)) == 0) {
1660 * We successfully offlined the log device, sync out the
1661 * current txg so that the "stubby" block can be removed
1664 txg_wait_synced(spa->spa_dsl_pool, 0);
1670 spa_aux_check_removed(spa_aux_vdev_t *sav)
1674 for (i = 0; i < sav->sav_count; i++)
1675 spa_check_removed(sav->sav_vdevs[i]);
1679 spa_claim_notify(zio_t *zio)
1681 spa_t *spa = zio->io_spa;
1686 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1687 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1688 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1689 mutex_exit(&spa->spa_props_lock);
1692 typedef struct spa_load_error {
1693 uint64_t sle_meta_count;
1694 uint64_t sle_data_count;
1698 spa_load_verify_done(zio_t *zio)
1700 blkptr_t *bp = zio->io_bp;
1701 spa_load_error_t *sle = zio->io_private;
1702 dmu_object_type_t type = BP_GET_TYPE(bp);
1703 int error = zio->io_error;
1706 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1707 type != DMU_OT_INTENT_LOG)
1708 atomic_add_64(&sle->sle_meta_count, 1);
1710 atomic_add_64(&sle->sle_data_count, 1);
1712 zio_data_buf_free(zio->io_data, zio->io_size);
1717 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1718 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1722 size_t size = BP_GET_PSIZE(bp);
1723 void *data = zio_data_buf_alloc(size);
1725 zio_nowait(zio_read(rio, spa, bp, data, size,
1726 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1727 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1728 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1734 spa_load_verify(spa_t *spa)
1737 spa_load_error_t sle = { 0 };
1738 zpool_rewind_policy_t policy;
1739 boolean_t verify_ok = B_FALSE;
1742 zpool_get_rewind_policy(spa->spa_config, &policy);
1744 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1747 rio = zio_root(spa, NULL, &sle,
1748 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1750 error = traverse_pool(spa, spa->spa_verify_min_txg,
1751 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1753 (void) zio_wait(rio);
1755 spa->spa_load_meta_errors = sle.sle_meta_count;
1756 spa->spa_load_data_errors = sle.sle_data_count;
1758 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1759 sle.sle_data_count <= policy.zrp_maxdata) {
1763 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1764 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1766 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1767 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1768 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1769 VERIFY(nvlist_add_int64(spa->spa_load_info,
1770 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1771 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1772 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1774 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1778 if (error != ENXIO && error != EIO)
1783 return (verify_ok ? 0 : EIO);
1787 * Find a value in the pool props object.
1790 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1792 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1793 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1797 * Find a value in the pool directory object.
1800 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1802 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1803 name, sizeof (uint64_t), 1, val));
1807 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1809 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1814 * Fix up config after a partly-completed split. This is done with the
1815 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1816 * pool have that entry in their config, but only the splitting one contains
1817 * a list of all the guids of the vdevs that are being split off.
1819 * This function determines what to do with that list: either rejoin
1820 * all the disks to the pool, or complete the splitting process. To attempt
1821 * the rejoin, each disk that is offlined is marked online again, and
1822 * we do a reopen() call. If the vdev label for every disk that was
1823 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1824 * then we call vdev_split() on each disk, and complete the split.
1826 * Otherwise we leave the config alone, with all the vdevs in place in
1827 * the original pool.
1830 spa_try_repair(spa_t *spa, nvlist_t *config)
1837 boolean_t attempt_reopen;
1839 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1842 /* check that the config is complete */
1843 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1844 &glist, &gcount) != 0)
1847 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_PUSHPAGE);
1849 /* attempt to online all the vdevs & validate */
1850 attempt_reopen = B_TRUE;
1851 for (i = 0; i < gcount; i++) {
1852 if (glist[i] == 0) /* vdev is hole */
1855 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1856 if (vd[i] == NULL) {
1858 * Don't bother attempting to reopen the disks;
1859 * just do the split.
1861 attempt_reopen = B_FALSE;
1863 /* attempt to re-online it */
1864 vd[i]->vdev_offline = B_FALSE;
1868 if (attempt_reopen) {
1869 vdev_reopen(spa->spa_root_vdev);
1871 /* check each device to see what state it's in */
1872 for (extracted = 0, i = 0; i < gcount; i++) {
1873 if (vd[i] != NULL &&
1874 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1881 * If every disk has been moved to the new pool, or if we never
1882 * even attempted to look at them, then we split them off for
1885 if (!attempt_reopen || gcount == extracted) {
1886 for (i = 0; i < gcount; i++)
1889 vdev_reopen(spa->spa_root_vdev);
1892 kmem_free(vd, gcount * sizeof (vdev_t *));
1896 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1897 boolean_t mosconfig)
1899 nvlist_t *config = spa->spa_config;
1900 char *ereport = FM_EREPORT_ZFS_POOL;
1906 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1909 ASSERT(spa->spa_comment == NULL);
1910 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1911 spa->spa_comment = spa_strdup(comment);
1914 * Versioning wasn't explicitly added to the label until later, so if
1915 * it's not present treat it as the initial version.
1917 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1918 &spa->spa_ubsync.ub_version) != 0)
1919 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1921 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1922 &spa->spa_config_txg);
1924 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1925 spa_guid_exists(pool_guid, 0)) {
1928 spa->spa_config_guid = pool_guid;
1930 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1932 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1936 nvlist_free(spa->spa_load_info);
1937 spa->spa_load_info = fnvlist_alloc();
1939 gethrestime(&spa->spa_loaded_ts);
1940 error = spa_load_impl(spa, pool_guid, config, state, type,
1941 mosconfig, &ereport);
1944 spa->spa_minref = refcount_count(&spa->spa_refcount);
1946 if (error != EEXIST) {
1947 spa->spa_loaded_ts.tv_sec = 0;
1948 spa->spa_loaded_ts.tv_nsec = 0;
1950 if (error != EBADF) {
1951 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1954 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
1961 * Load an existing storage pool, using the pool's builtin spa_config as a
1962 * source of configuration information.
1964 __attribute__((always_inline))
1966 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
1967 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1971 nvlist_t *nvroot = NULL;
1974 uberblock_t *ub = &spa->spa_uberblock;
1975 uint64_t children, config_cache_txg = spa->spa_config_txg;
1976 int orig_mode = spa->spa_mode;
1979 boolean_t missing_feat_write = B_FALSE;
1982 * If this is an untrusted config, access the pool in read-only mode.
1983 * This prevents things like resilvering recently removed devices.
1986 spa->spa_mode = FREAD;
1988 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1990 spa->spa_load_state = state;
1992 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
1995 parse = (type == SPA_IMPORT_EXISTING ?
1996 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
1999 * Create "The Godfather" zio to hold all async IOs
2001 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2002 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2005 * Parse the configuration into a vdev tree. We explicitly set the
2006 * value that will be returned by spa_version() since parsing the
2007 * configuration requires knowing the version number.
2009 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2010 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2011 spa_config_exit(spa, SCL_ALL, FTAG);
2016 ASSERT(spa->spa_root_vdev == rvd);
2018 if (type != SPA_IMPORT_ASSEMBLE) {
2019 ASSERT(spa_guid(spa) == pool_guid);
2023 * Try to open all vdevs, loading each label in the process.
2025 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2026 error = vdev_open(rvd);
2027 spa_config_exit(spa, SCL_ALL, FTAG);
2032 * We need to validate the vdev labels against the configuration that
2033 * we have in hand, which is dependent on the setting of mosconfig. If
2034 * mosconfig is true then we're validating the vdev labels based on
2035 * that config. Otherwise, we're validating against the cached config
2036 * (zpool.cache) that was read when we loaded the zfs module, and then
2037 * later we will recursively call spa_load() and validate against
2040 * If we're assembling a new pool that's been split off from an
2041 * existing pool, the labels haven't yet been updated so we skip
2042 * validation for now.
2044 if (type != SPA_IMPORT_ASSEMBLE) {
2045 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2046 error = vdev_validate(rvd, mosconfig);
2047 spa_config_exit(spa, SCL_ALL, FTAG);
2052 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2057 * Find the best uberblock.
2059 vdev_uberblock_load(rvd, ub, &label);
2062 * If we weren't able to find a single valid uberblock, return failure.
2064 if (ub->ub_txg == 0) {
2066 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2070 * If the pool has an unsupported version we can't open it.
2072 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2074 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2077 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2081 * If we weren't able to find what's necessary for reading the
2082 * MOS in the label, return failure.
2084 if (label == NULL || nvlist_lookup_nvlist(label,
2085 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2087 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2092 * Update our in-core representation with the definitive values
2095 nvlist_free(spa->spa_label_features);
2096 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2102 * Look through entries in the label nvlist's features_for_read. If
2103 * there is a feature listed there which we don't understand then we
2104 * cannot open a pool.
2106 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2107 nvlist_t *unsup_feat;
2110 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2113 for (nvp = nvlist_next_nvpair(spa->spa_label_features, NULL);
2115 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2116 if (!zfeature_is_supported(nvpair_name(nvp))) {
2117 VERIFY(nvlist_add_string(unsup_feat,
2118 nvpair_name(nvp), "") == 0);
2122 if (!nvlist_empty(unsup_feat)) {
2123 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2124 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2125 nvlist_free(unsup_feat);
2126 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2130 nvlist_free(unsup_feat);
2134 * If the vdev guid sum doesn't match the uberblock, we have an
2135 * incomplete configuration. We first check to see if the pool
2136 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2137 * If it is, defer the vdev_guid_sum check till later so we
2138 * can handle missing vdevs.
2140 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2141 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2142 rvd->vdev_guid_sum != ub->ub_guid_sum)
2143 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2145 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2146 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2147 spa_try_repair(spa, config);
2148 spa_config_exit(spa, SCL_ALL, FTAG);
2149 nvlist_free(spa->spa_config_splitting);
2150 spa->spa_config_splitting = NULL;
2154 * Initialize internal SPA structures.
2156 spa->spa_state = POOL_STATE_ACTIVE;
2157 spa->spa_ubsync = spa->spa_uberblock;
2158 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2159 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2160 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2161 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2162 spa->spa_claim_max_txg = spa->spa_first_txg;
2163 spa->spa_prev_software_version = ub->ub_software_version;
2165 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2167 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2168 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2170 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2171 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2173 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2174 boolean_t missing_feat_read = B_FALSE;
2175 nvlist_t *unsup_feat;
2177 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2178 &spa->spa_feat_for_read_obj) != 0) {
2179 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2182 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2183 &spa->spa_feat_for_write_obj) != 0) {
2184 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2187 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2188 &spa->spa_feat_desc_obj) != 0) {
2189 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2192 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2195 if (!feature_is_supported(spa->spa_meta_objset,
2196 spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj,
2198 missing_feat_read = B_TRUE;
2200 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2201 if (!feature_is_supported(spa->spa_meta_objset,
2202 spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj,
2204 missing_feat_write = B_TRUE;
2207 if (!nvlist_empty(unsup_feat)) {
2208 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2209 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2212 nvlist_free(unsup_feat);
2214 if (!missing_feat_read) {
2215 fnvlist_add_boolean(spa->spa_load_info,
2216 ZPOOL_CONFIG_CAN_RDONLY);
2220 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2221 * twofold: to determine whether the pool is available for
2222 * import in read-write mode and (if it is not) whether the
2223 * pool is available for import in read-only mode. If the pool
2224 * is available for import in read-write mode, it is displayed
2225 * as available in userland; if it is not available for import
2226 * in read-only mode, it is displayed as unavailable in
2227 * userland. If the pool is available for import in read-only
2228 * mode but not read-write mode, it is displayed as unavailable
2229 * in userland with a special note that the pool is actually
2230 * available for open in read-only mode.
2232 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2233 * missing a feature for write, we must first determine whether
2234 * the pool can be opened read-only before returning to
2235 * userland in order to know whether to display the
2236 * abovementioned note.
2238 if (missing_feat_read || (missing_feat_write &&
2239 spa_writeable(spa))) {
2240 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2245 spa->spa_is_initializing = B_TRUE;
2246 error = dsl_pool_open(spa->spa_dsl_pool);
2247 spa->spa_is_initializing = B_FALSE;
2249 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2253 nvlist_t *policy = NULL, *nvconfig;
2255 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2256 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2258 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2259 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2261 unsigned long myhostid = 0;
2263 VERIFY(nvlist_lookup_string(nvconfig,
2264 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2267 myhostid = zone_get_hostid(NULL);
2270 * We're emulating the system's hostid in userland, so
2271 * we can't use zone_get_hostid().
2273 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2274 #endif /* _KERNEL */
2275 if (hostid != 0 && myhostid != 0 &&
2276 hostid != myhostid) {
2277 nvlist_free(nvconfig);
2278 cmn_err(CE_WARN, "pool '%s' could not be "
2279 "loaded as it was last accessed by "
2280 "another system (host: %s hostid: 0x%lx). "
2281 "See: http://zfsonlinux.org/msg/ZFS-8000-EY",
2282 spa_name(spa), hostname,
2283 (unsigned long)hostid);
2287 if (nvlist_lookup_nvlist(spa->spa_config,
2288 ZPOOL_REWIND_POLICY, &policy) == 0)
2289 VERIFY(nvlist_add_nvlist(nvconfig,
2290 ZPOOL_REWIND_POLICY, policy) == 0);
2292 spa_config_set(spa, nvconfig);
2294 spa_deactivate(spa);
2295 spa_activate(spa, orig_mode);
2297 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2300 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2301 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2302 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2304 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2307 * Load the bit that tells us to use the new accounting function
2308 * (raid-z deflation). If we have an older pool, this will not
2311 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2312 if (error != 0 && error != ENOENT)
2313 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2315 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2316 &spa->spa_creation_version);
2317 if (error != 0 && error != ENOENT)
2318 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2321 * Load the persistent error log. If we have an older pool, this will
2324 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2325 if (error != 0 && error != ENOENT)
2326 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2328 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2329 &spa->spa_errlog_scrub);
2330 if (error != 0 && error != ENOENT)
2331 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2334 * Load the history object. If we have an older pool, this
2335 * will not be present.
2337 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2338 if (error != 0 && error != ENOENT)
2339 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2342 * If we're assembling the pool from the split-off vdevs of
2343 * an existing pool, we don't want to attach the spares & cache
2348 * Load any hot spares for this pool.
2350 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2351 if (error != 0 && error != ENOENT)
2352 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2353 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2354 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2355 if (load_nvlist(spa, spa->spa_spares.sav_object,
2356 &spa->spa_spares.sav_config) != 0)
2357 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2359 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2360 spa_load_spares(spa);
2361 spa_config_exit(spa, SCL_ALL, FTAG);
2362 } else if (error == 0) {
2363 spa->spa_spares.sav_sync = B_TRUE;
2367 * Load any level 2 ARC devices for this pool.
2369 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2370 &spa->spa_l2cache.sav_object);
2371 if (error != 0 && error != ENOENT)
2372 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2373 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2374 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2375 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2376 &spa->spa_l2cache.sav_config) != 0)
2377 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2379 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2380 spa_load_l2cache(spa);
2381 spa_config_exit(spa, SCL_ALL, FTAG);
2382 } else if (error == 0) {
2383 spa->spa_l2cache.sav_sync = B_TRUE;
2386 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2388 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2389 if (error && error != ENOENT)
2390 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2393 uint64_t autoreplace;
2395 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2396 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2397 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2398 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2399 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2400 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2401 &spa->spa_dedup_ditto);
2403 spa->spa_autoreplace = (autoreplace != 0);
2407 * If the 'autoreplace' property is set, then post a resource notifying
2408 * the ZFS DE that it should not issue any faults for unopenable
2409 * devices. We also iterate over the vdevs, and post a sysevent for any
2410 * unopenable vdevs so that the normal autoreplace handler can take
2413 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2414 spa_check_removed(spa->spa_root_vdev);
2416 * For the import case, this is done in spa_import(), because
2417 * at this point we're using the spare definitions from
2418 * the MOS config, not necessarily from the userland config.
2420 if (state != SPA_LOAD_IMPORT) {
2421 spa_aux_check_removed(&spa->spa_spares);
2422 spa_aux_check_removed(&spa->spa_l2cache);
2427 * Load the vdev state for all toplevel vdevs.
2432 * Propagate the leaf DTLs we just loaded all the way up the tree.
2434 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2435 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2436 spa_config_exit(spa, SCL_ALL, FTAG);
2439 * Load the DDTs (dedup tables).
2441 error = ddt_load(spa);
2443 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2445 spa_update_dspace(spa);
2448 * Validate the config, using the MOS config to fill in any
2449 * information which might be missing. If we fail to validate
2450 * the config then declare the pool unfit for use. If we're
2451 * assembling a pool from a split, the log is not transferred
2454 if (type != SPA_IMPORT_ASSEMBLE) {
2457 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2458 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2460 if (!spa_config_valid(spa, nvconfig)) {
2461 nvlist_free(nvconfig);
2462 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2465 nvlist_free(nvconfig);
2468 * Now that we've validated the config, check the state of the
2469 * root vdev. If it can't be opened, it indicates one or
2470 * more toplevel vdevs are faulted.
2472 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2475 if (spa_check_logs(spa)) {
2476 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2477 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2481 if (missing_feat_write) {
2482 ASSERT(state == SPA_LOAD_TRYIMPORT);
2485 * At this point, we know that we can open the pool in
2486 * read-only mode but not read-write mode. We now have enough
2487 * information and can return to userland.
2489 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2493 * We've successfully opened the pool, verify that we're ready
2494 * to start pushing transactions.
2496 if (state != SPA_LOAD_TRYIMPORT) {
2497 if ((error = spa_load_verify(spa)))
2498 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2502 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2503 spa->spa_load_max_txg == UINT64_MAX)) {
2505 int need_update = B_FALSE;
2508 ASSERT(state != SPA_LOAD_TRYIMPORT);
2511 * Claim log blocks that haven't been committed yet.
2512 * This must all happen in a single txg.
2513 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2514 * invoked from zil_claim_log_block()'s i/o done callback.
2515 * Price of rollback is that we abandon the log.
2517 spa->spa_claiming = B_TRUE;
2519 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2520 spa_first_txg(spa));
2521 (void) dmu_objset_find(spa_name(spa),
2522 zil_claim, tx, DS_FIND_CHILDREN);
2525 spa->spa_claiming = B_FALSE;
2527 spa_set_log_state(spa, SPA_LOG_GOOD);
2528 spa->spa_sync_on = B_TRUE;
2529 txg_sync_start(spa->spa_dsl_pool);
2532 * Wait for all claims to sync. We sync up to the highest
2533 * claimed log block birth time so that claimed log blocks
2534 * don't appear to be from the future. spa_claim_max_txg
2535 * will have been set for us by either zil_check_log_chain()
2536 * (invoked from spa_check_logs()) or zil_claim() above.
2538 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2541 * If the config cache is stale, or we have uninitialized
2542 * metaslabs (see spa_vdev_add()), then update the config.
2544 * If this is a verbatim import, trust the current
2545 * in-core spa_config and update the disk labels.
2547 if (config_cache_txg != spa->spa_config_txg ||
2548 state == SPA_LOAD_IMPORT ||
2549 state == SPA_LOAD_RECOVER ||
2550 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2551 need_update = B_TRUE;
2553 for (c = 0; c < rvd->vdev_children; c++)
2554 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2555 need_update = B_TRUE;
2558 * Update the config cache asychronously in case we're the
2559 * root pool, in which case the config cache isn't writable yet.
2562 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2565 * Check all DTLs to see if anything needs resilvering.
2567 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2568 vdev_resilver_needed(rvd, NULL, NULL))
2569 spa_async_request(spa, SPA_ASYNC_RESILVER);
2572 * Delete any inconsistent datasets.
2574 (void) dmu_objset_find(spa_name(spa),
2575 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2578 * Clean up any stale temporary dataset userrefs.
2580 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2587 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2589 int mode = spa->spa_mode;
2592 spa_deactivate(spa);
2594 spa->spa_load_max_txg--;
2596 spa_activate(spa, mode);
2597 spa_async_suspend(spa);
2599 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2603 * If spa_load() fails this function will try loading prior txg's. If
2604 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2605 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2606 * function will not rewind the pool and will return the same error as
2610 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2611 uint64_t max_request, int rewind_flags)
2613 nvlist_t *loadinfo = NULL;
2614 nvlist_t *config = NULL;
2615 int load_error, rewind_error;
2616 uint64_t safe_rewind_txg;
2619 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2620 spa->spa_load_max_txg = spa->spa_load_txg;
2621 spa_set_log_state(spa, SPA_LOG_CLEAR);
2623 spa->spa_load_max_txg = max_request;
2626 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2628 if (load_error == 0)
2631 if (spa->spa_root_vdev != NULL)
2632 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2634 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2635 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2637 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2638 nvlist_free(config);
2639 return (load_error);
2642 if (state == SPA_LOAD_RECOVER) {
2643 /* Price of rolling back is discarding txgs, including log */
2644 spa_set_log_state(spa, SPA_LOG_CLEAR);
2647 * If we aren't rolling back save the load info from our first
2648 * import attempt so that we can restore it after attempting
2651 loadinfo = spa->spa_load_info;
2652 spa->spa_load_info = fnvlist_alloc();
2655 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2656 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2657 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2658 TXG_INITIAL : safe_rewind_txg;
2661 * Continue as long as we're finding errors, we're still within
2662 * the acceptable rewind range, and we're still finding uberblocks
2664 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2665 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2666 if (spa->spa_load_max_txg < safe_rewind_txg)
2667 spa->spa_extreme_rewind = B_TRUE;
2668 rewind_error = spa_load_retry(spa, state, mosconfig);
2671 spa->spa_extreme_rewind = B_FALSE;
2672 spa->spa_load_max_txg = UINT64_MAX;
2674 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2675 spa_config_set(spa, config);
2677 if (state == SPA_LOAD_RECOVER) {
2678 ASSERT3P(loadinfo, ==, NULL);
2679 return (rewind_error);
2681 /* Store the rewind info as part of the initial load info */
2682 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2683 spa->spa_load_info);
2685 /* Restore the initial load info */
2686 fnvlist_free(spa->spa_load_info);
2687 spa->spa_load_info = loadinfo;
2689 return (load_error);
2696 * The import case is identical to an open except that the configuration is sent
2697 * down from userland, instead of grabbed from the configuration cache. For the
2698 * case of an open, the pool configuration will exist in the
2699 * POOL_STATE_UNINITIALIZED state.
2701 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2702 * the same time open the pool, without having to keep around the spa_t in some
2706 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2710 spa_load_state_t state = SPA_LOAD_OPEN;
2712 int locked = B_FALSE;
2717 * As disgusting as this is, we need to support recursive calls to this
2718 * function because dsl_dir_open() is called during spa_load(), and ends
2719 * up calling spa_open() again. The real fix is to figure out how to
2720 * avoid dsl_dir_open() calling this in the first place.
2722 if (mutex_owner(&spa_namespace_lock) != curthread) {
2723 mutex_enter(&spa_namespace_lock);
2727 if ((spa = spa_lookup(pool)) == NULL) {
2729 mutex_exit(&spa_namespace_lock);
2733 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2734 zpool_rewind_policy_t policy;
2736 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2738 if (policy.zrp_request & ZPOOL_DO_REWIND)
2739 state = SPA_LOAD_RECOVER;
2741 spa_activate(spa, spa_mode_global);
2743 if (state != SPA_LOAD_RECOVER)
2744 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2746 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2747 policy.zrp_request);
2749 if (error == EBADF) {
2751 * If vdev_validate() returns failure (indicated by
2752 * EBADF), it indicates that one of the vdevs indicates
2753 * that the pool has been exported or destroyed. If
2754 * this is the case, the config cache is out of sync and
2755 * we should remove the pool from the namespace.
2758 spa_deactivate(spa);
2759 spa_config_sync(spa, B_TRUE, B_TRUE);
2762 mutex_exit(&spa_namespace_lock);
2768 * We can't open the pool, but we still have useful
2769 * information: the state of each vdev after the
2770 * attempted vdev_open(). Return this to the user.
2772 if (config != NULL && spa->spa_config) {
2773 VERIFY(nvlist_dup(spa->spa_config, config,
2775 VERIFY(nvlist_add_nvlist(*config,
2776 ZPOOL_CONFIG_LOAD_INFO,
2777 spa->spa_load_info) == 0);
2780 spa_deactivate(spa);
2781 spa->spa_last_open_failed = error;
2783 mutex_exit(&spa_namespace_lock);
2789 spa_open_ref(spa, tag);
2792 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2795 * If we've recovered the pool, pass back any information we
2796 * gathered while doing the load.
2798 if (state == SPA_LOAD_RECOVER) {
2799 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2800 spa->spa_load_info) == 0);
2804 spa->spa_last_open_failed = 0;
2805 spa->spa_last_ubsync_txg = 0;
2806 spa->spa_load_txg = 0;
2807 mutex_exit(&spa_namespace_lock);
2816 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2819 return (spa_open_common(name, spapp, tag, policy, config));
2823 spa_open(const char *name, spa_t **spapp, void *tag)
2825 return (spa_open_common(name, spapp, tag, NULL, NULL));
2829 * Lookup the given spa_t, incrementing the inject count in the process,
2830 * preventing it from being exported or destroyed.
2833 spa_inject_addref(char *name)
2837 mutex_enter(&spa_namespace_lock);
2838 if ((spa = spa_lookup(name)) == NULL) {
2839 mutex_exit(&spa_namespace_lock);
2842 spa->spa_inject_ref++;
2843 mutex_exit(&spa_namespace_lock);
2849 spa_inject_delref(spa_t *spa)
2851 mutex_enter(&spa_namespace_lock);
2852 spa->spa_inject_ref--;
2853 mutex_exit(&spa_namespace_lock);
2857 * Add spares device information to the nvlist.
2860 spa_add_spares(spa_t *spa, nvlist_t *config)
2870 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2872 if (spa->spa_spares.sav_count == 0)
2875 VERIFY(nvlist_lookup_nvlist(config,
2876 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2877 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2878 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2880 VERIFY(nvlist_add_nvlist_array(nvroot,
2881 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2882 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2883 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2886 * Go through and find any spares which have since been
2887 * repurposed as an active spare. If this is the case, update
2888 * their status appropriately.
2890 for (i = 0; i < nspares; i++) {
2891 VERIFY(nvlist_lookup_uint64(spares[i],
2892 ZPOOL_CONFIG_GUID, &guid) == 0);
2893 if (spa_spare_exists(guid, &pool, NULL) &&
2895 VERIFY(nvlist_lookup_uint64_array(
2896 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2897 (uint64_t **)&vs, &vsc) == 0);
2898 vs->vs_state = VDEV_STATE_CANT_OPEN;
2899 vs->vs_aux = VDEV_AUX_SPARED;
2906 * Add l2cache device information to the nvlist, including vdev stats.
2909 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2912 uint_t i, j, nl2cache;
2919 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2921 if (spa->spa_l2cache.sav_count == 0)
2924 VERIFY(nvlist_lookup_nvlist(config,
2925 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2926 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2927 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2928 if (nl2cache != 0) {
2929 VERIFY(nvlist_add_nvlist_array(nvroot,
2930 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2931 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2932 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2935 * Update level 2 cache device stats.
2938 for (i = 0; i < nl2cache; i++) {
2939 VERIFY(nvlist_lookup_uint64(l2cache[i],
2940 ZPOOL_CONFIG_GUID, &guid) == 0);
2943 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2945 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2946 vd = spa->spa_l2cache.sav_vdevs[j];
2952 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2953 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2955 vdev_get_stats(vd, vs);
2961 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
2967 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2968 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2970 if (spa->spa_feat_for_read_obj != 0) {
2971 for (zap_cursor_init(&zc, spa->spa_meta_objset,
2972 spa->spa_feat_for_read_obj);
2973 zap_cursor_retrieve(&zc, &za) == 0;
2974 zap_cursor_advance(&zc)) {
2975 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
2976 za.za_num_integers == 1);
2977 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
2978 za.za_first_integer));
2980 zap_cursor_fini(&zc);
2983 if (spa->spa_feat_for_write_obj != 0) {
2984 for (zap_cursor_init(&zc, spa->spa_meta_objset,
2985 spa->spa_feat_for_write_obj);
2986 zap_cursor_retrieve(&zc, &za) == 0;
2987 zap_cursor_advance(&zc)) {
2988 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
2989 za.za_num_integers == 1);
2990 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
2991 za.za_first_integer));
2993 zap_cursor_fini(&zc);
2996 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
2998 nvlist_free(features);
3002 spa_get_stats(const char *name, nvlist_t **config,
3003 char *altroot, size_t buflen)
3009 error = spa_open_common(name, &spa, FTAG, NULL, config);
3013 * This still leaves a window of inconsistency where the spares
3014 * or l2cache devices could change and the config would be
3015 * self-inconsistent.
3017 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3019 if (*config != NULL) {
3020 uint64_t loadtimes[2];
3022 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3023 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3024 VERIFY(nvlist_add_uint64_array(*config,
3025 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3027 VERIFY(nvlist_add_uint64(*config,
3028 ZPOOL_CONFIG_ERRCOUNT,
3029 spa_get_errlog_size(spa)) == 0);
3031 if (spa_suspended(spa))
3032 VERIFY(nvlist_add_uint64(*config,
3033 ZPOOL_CONFIG_SUSPENDED,
3034 spa->spa_failmode) == 0);
3036 spa_add_spares(spa, *config);
3037 spa_add_l2cache(spa, *config);
3038 spa_add_feature_stats(spa, *config);
3043 * We want to get the alternate root even for faulted pools, so we cheat
3044 * and call spa_lookup() directly.
3048 mutex_enter(&spa_namespace_lock);
3049 spa = spa_lookup(name);
3051 spa_altroot(spa, altroot, buflen);
3055 mutex_exit(&spa_namespace_lock);
3057 spa_altroot(spa, altroot, buflen);
3062 spa_config_exit(spa, SCL_CONFIG, FTAG);
3063 spa_close(spa, FTAG);
3070 * Validate that the auxiliary device array is well formed. We must have an
3071 * array of nvlists, each which describes a valid leaf vdev. If this is an
3072 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3073 * specified, as long as they are well-formed.
3076 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3077 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3078 vdev_labeltype_t label)
3085 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3088 * It's acceptable to have no devs specified.
3090 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3097 * Make sure the pool is formatted with a version that supports this
3100 if (spa_version(spa) < version)
3104 * Set the pending device list so we correctly handle device in-use
3107 sav->sav_pending = dev;
3108 sav->sav_npending = ndev;
3110 for (i = 0; i < ndev; i++) {
3111 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3115 if (!vd->vdev_ops->vdev_op_leaf) {
3122 * The L2ARC currently only supports disk devices in
3123 * kernel context. For user-level testing, we allow it.
3126 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3127 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3135 if ((error = vdev_open(vd)) == 0 &&
3136 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3137 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3138 vd->vdev_guid) == 0);
3144 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3151 sav->sav_pending = NULL;
3152 sav->sav_npending = 0;
3157 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3161 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3163 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3164 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3165 VDEV_LABEL_SPARE)) != 0) {
3169 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3170 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3171 VDEV_LABEL_L2CACHE));
3175 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3180 if (sav->sav_config != NULL) {
3186 * Generate new dev list by concatentating with the
3189 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3190 &olddevs, &oldndevs) == 0);
3192 newdevs = kmem_alloc(sizeof (void *) *
3193 (ndevs + oldndevs), KM_PUSHPAGE);
3194 for (i = 0; i < oldndevs; i++)
3195 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3197 for (i = 0; i < ndevs; i++)
3198 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3201 VERIFY(nvlist_remove(sav->sav_config, config,
3202 DATA_TYPE_NVLIST_ARRAY) == 0);
3204 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3205 config, newdevs, ndevs + oldndevs) == 0);
3206 for (i = 0; i < oldndevs + ndevs; i++)
3207 nvlist_free(newdevs[i]);
3208 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3211 * Generate a new dev list.
3213 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3215 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3221 * Stop and drop level 2 ARC devices
3224 spa_l2cache_drop(spa_t *spa)
3228 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3230 for (i = 0; i < sav->sav_count; i++) {
3233 vd = sav->sav_vdevs[i];
3236 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3237 pool != 0ULL && l2arc_vdev_present(vd))
3238 l2arc_remove_vdev(vd);
3246 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3247 const char *history_str, nvlist_t *zplprops)
3250 char *altroot = NULL;
3255 uint64_t txg = TXG_INITIAL;
3256 nvlist_t **spares, **l2cache;
3257 uint_t nspares, nl2cache;
3258 uint64_t version, obj;
3259 boolean_t has_features;
3264 * If this pool already exists, return failure.
3266 mutex_enter(&spa_namespace_lock);
3267 if (spa_lookup(pool) != NULL) {
3268 mutex_exit(&spa_namespace_lock);
3273 * Allocate a new spa_t structure.
3275 (void) nvlist_lookup_string(props,
3276 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3277 spa = spa_add(pool, NULL, altroot);
3278 spa_activate(spa, spa_mode_global);
3280 if (props && (error = spa_prop_validate(spa, props))) {
3281 spa_deactivate(spa);
3283 mutex_exit(&spa_namespace_lock);
3287 has_features = B_FALSE;
3288 for (elem = nvlist_next_nvpair(props, NULL);
3289 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3290 if (zpool_prop_feature(nvpair_name(elem)))
3291 has_features = B_TRUE;
3294 if (has_features || nvlist_lookup_uint64(props,
3295 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3296 version = SPA_VERSION;
3298 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3300 spa->spa_first_txg = txg;
3301 spa->spa_uberblock.ub_txg = txg - 1;
3302 spa->spa_uberblock.ub_version = version;
3303 spa->spa_ubsync = spa->spa_uberblock;
3306 * Create "The Godfather" zio to hold all async IOs
3308 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3309 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3312 * Create the root vdev.
3314 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3316 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3318 ASSERT(error != 0 || rvd != NULL);
3319 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3321 if (error == 0 && !zfs_allocatable_devs(nvroot))
3325 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3326 (error = spa_validate_aux(spa, nvroot, txg,
3327 VDEV_ALLOC_ADD)) == 0) {
3328 for (c = 0; c < rvd->vdev_children; c++) {
3329 vdev_metaslab_set_size(rvd->vdev_child[c]);
3330 vdev_expand(rvd->vdev_child[c], txg);
3334 spa_config_exit(spa, SCL_ALL, FTAG);
3338 spa_deactivate(spa);
3340 mutex_exit(&spa_namespace_lock);
3345 * Get the list of spares, if specified.
3347 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3348 &spares, &nspares) == 0) {
3349 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3351 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3352 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3353 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3354 spa_load_spares(spa);
3355 spa_config_exit(spa, SCL_ALL, FTAG);
3356 spa->spa_spares.sav_sync = B_TRUE;
3360 * Get the list of level 2 cache devices, if specified.
3362 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3363 &l2cache, &nl2cache) == 0) {
3364 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3365 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3366 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3367 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3368 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3369 spa_load_l2cache(spa);
3370 spa_config_exit(spa, SCL_ALL, FTAG);
3371 spa->spa_l2cache.sav_sync = B_TRUE;
3374 spa->spa_is_initializing = B_TRUE;
3375 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3376 spa->spa_meta_objset = dp->dp_meta_objset;
3377 spa->spa_is_initializing = B_FALSE;
3380 * Create DDTs (dedup tables).
3384 spa_update_dspace(spa);
3386 tx = dmu_tx_create_assigned(dp, txg);
3389 * Create the pool config object.
3391 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3392 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3393 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3395 if (zap_add(spa->spa_meta_objset,
3396 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3397 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3398 cmn_err(CE_PANIC, "failed to add pool config");
3401 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3402 spa_feature_create_zap_objects(spa, tx);
3404 if (zap_add(spa->spa_meta_objset,
3405 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3406 sizeof (uint64_t), 1, &version, tx) != 0) {
3407 cmn_err(CE_PANIC, "failed to add pool version");
3410 /* Newly created pools with the right version are always deflated. */
3411 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3412 spa->spa_deflate = TRUE;
3413 if (zap_add(spa->spa_meta_objset,
3414 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3415 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3416 cmn_err(CE_PANIC, "failed to add deflate");
3421 * Create the deferred-free bpobj. Turn off compression
3422 * because sync-to-convergence takes longer if the blocksize
3425 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3426 dmu_object_set_compress(spa->spa_meta_objset, obj,
3427 ZIO_COMPRESS_OFF, tx);
3428 if (zap_add(spa->spa_meta_objset,
3429 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3430 sizeof (uint64_t), 1, &obj, tx) != 0) {
3431 cmn_err(CE_PANIC, "failed to add bpobj");
3433 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3434 spa->spa_meta_objset, obj));
3437 * Create the pool's history object.
3439 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3440 spa_history_create_obj(spa, tx);
3443 * Set pool properties.
3445 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3446 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3447 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3448 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3450 if (props != NULL) {
3451 spa_configfile_set(spa, props, B_FALSE);
3452 spa_sync_props(spa, props, tx);
3457 spa->spa_sync_on = B_TRUE;
3458 txg_sync_start(spa->spa_dsl_pool);
3461 * We explicitly wait for the first transaction to complete so that our
3462 * bean counters are appropriately updated.
3464 txg_wait_synced(spa->spa_dsl_pool, txg);
3466 spa_config_sync(spa, B_FALSE, B_TRUE);
3468 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3469 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3470 spa_history_log_version(spa, LOG_POOL_CREATE);
3472 spa->spa_minref = refcount_count(&spa->spa_refcount);
3474 mutex_exit(&spa_namespace_lock);
3481 * Get the root pool information from the root disk, then import the root pool
3482 * during the system boot up time.
3484 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3487 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3490 nvlist_t *nvtop, *nvroot;
3493 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3497 * Add this top-level vdev to the child array.
3499 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3501 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3503 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3506 * Put this pool's top-level vdevs into a root vdev.
3508 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3509 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3510 VDEV_TYPE_ROOT) == 0);
3511 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3512 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3513 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3517 * Replace the existing vdev_tree with the new root vdev in
3518 * this pool's configuration (remove the old, add the new).
3520 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3521 nvlist_free(nvroot);
3526 * Walk the vdev tree and see if we can find a device with "better"
3527 * configuration. A configuration is "better" if the label on that
3528 * device has a more recent txg.
3531 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3535 for (c = 0; c < vd->vdev_children; c++)
3536 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3538 if (vd->vdev_ops->vdev_op_leaf) {
3542 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3546 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3550 * Do we have a better boot device?
3552 if (label_txg > *txg) {
3561 * Import a root pool.
3563 * For x86. devpath_list will consist of devid and/or physpath name of
3564 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3565 * The GRUB "findroot" command will return the vdev we should boot.
3567 * For Sparc, devpath_list consists the physpath name of the booting device
3568 * no matter the rootpool is a single device pool or a mirrored pool.
3570 * "/pci@1f,0/ide@d/disk@0,0:a"
3573 spa_import_rootpool(char *devpath, char *devid)
3576 vdev_t *rvd, *bvd, *avd = NULL;
3577 nvlist_t *config, *nvtop;
3583 * Read the label from the boot device and generate a configuration.
3585 config = spa_generate_rootconf(devpath, devid, &guid);
3586 #if defined(_OBP) && defined(_KERNEL)
3587 if (config == NULL) {
3588 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3590 get_iscsi_bootpath_phy(devpath);
3591 config = spa_generate_rootconf(devpath, devid, &guid);
3595 if (config == NULL) {
3596 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3601 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3603 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3605 mutex_enter(&spa_namespace_lock);
3606 if ((spa = spa_lookup(pname)) != NULL) {
3608 * Remove the existing root pool from the namespace so that we
3609 * can replace it with the correct config we just read in.
3614 spa = spa_add(pname, config, NULL);
3615 spa->spa_is_root = B_TRUE;
3616 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3619 * Build up a vdev tree based on the boot device's label config.
3621 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3623 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3624 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3625 VDEV_ALLOC_ROOTPOOL);
3626 spa_config_exit(spa, SCL_ALL, FTAG);
3628 mutex_exit(&spa_namespace_lock);
3629 nvlist_free(config);
3630 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3636 * Get the boot vdev.
3638 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3639 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3640 (u_longlong_t)guid);
3646 * Determine if there is a better boot device.
3649 spa_alt_rootvdev(rvd, &avd, &txg);
3651 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3652 "try booting from '%s'", avd->vdev_path);
3658 * If the boot device is part of a spare vdev then ensure that
3659 * we're booting off the active spare.
3661 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3662 !bvd->vdev_isspare) {
3663 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3664 "try booting from '%s'",
3666 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3672 spa_history_log_version(spa, LOG_POOL_IMPORT);
3674 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3676 spa_config_exit(spa, SCL_ALL, FTAG);
3677 mutex_exit(&spa_namespace_lock);
3679 nvlist_free(config);
3686 * Import a non-root pool into the system.
3689 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3692 char *altroot = NULL;
3693 spa_load_state_t state = SPA_LOAD_IMPORT;
3694 zpool_rewind_policy_t policy;
3695 uint64_t mode = spa_mode_global;
3696 uint64_t readonly = B_FALSE;
3699 nvlist_t **spares, **l2cache;
3700 uint_t nspares, nl2cache;
3703 * If a pool with this name exists, return failure.
3705 mutex_enter(&spa_namespace_lock);
3706 if (spa_lookup(pool) != NULL) {
3707 mutex_exit(&spa_namespace_lock);
3712 * Create and initialize the spa structure.
3714 (void) nvlist_lookup_string(props,
3715 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3716 (void) nvlist_lookup_uint64(props,
3717 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3720 spa = spa_add(pool, config, altroot);
3721 spa->spa_import_flags = flags;
3724 * Verbatim import - Take a pool and insert it into the namespace
3725 * as if it had been loaded at boot.
3727 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3729 spa_configfile_set(spa, props, B_FALSE);
3731 spa_config_sync(spa, B_FALSE, B_TRUE);
3733 mutex_exit(&spa_namespace_lock);
3734 spa_history_log_version(spa, LOG_POOL_IMPORT);
3739 spa_activate(spa, mode);
3742 * Don't start async tasks until we know everything is healthy.
3744 spa_async_suspend(spa);
3746 zpool_get_rewind_policy(config, &policy);
3747 if (policy.zrp_request & ZPOOL_DO_REWIND)
3748 state = SPA_LOAD_RECOVER;
3751 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3752 * because the user-supplied config is actually the one to trust when
3755 if (state != SPA_LOAD_RECOVER)
3756 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3758 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3759 policy.zrp_request);
3762 * Propagate anything learned while loading the pool and pass it
3763 * back to caller (i.e. rewind info, missing devices, etc).
3765 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3766 spa->spa_load_info) == 0);
3768 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3770 * Toss any existing sparelist, as it doesn't have any validity
3771 * anymore, and conflicts with spa_has_spare().
3773 if (spa->spa_spares.sav_config) {
3774 nvlist_free(spa->spa_spares.sav_config);
3775 spa->spa_spares.sav_config = NULL;
3776 spa_load_spares(spa);
3778 if (spa->spa_l2cache.sav_config) {
3779 nvlist_free(spa->spa_l2cache.sav_config);
3780 spa->spa_l2cache.sav_config = NULL;
3781 spa_load_l2cache(spa);
3784 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3787 error = spa_validate_aux(spa, nvroot, -1ULL,
3790 error = spa_validate_aux(spa, nvroot, -1ULL,
3791 VDEV_ALLOC_L2CACHE);
3792 spa_config_exit(spa, SCL_ALL, FTAG);
3795 spa_configfile_set(spa, props, B_FALSE);
3797 if (error != 0 || (props && spa_writeable(spa) &&
3798 (error = spa_prop_set(spa, props)))) {
3800 spa_deactivate(spa);
3802 mutex_exit(&spa_namespace_lock);
3806 spa_async_resume(spa);
3809 * Override any spares and level 2 cache devices as specified by
3810 * the user, as these may have correct device names/devids, etc.
3812 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3813 &spares, &nspares) == 0) {
3814 if (spa->spa_spares.sav_config)
3815 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3816 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3818 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3819 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3820 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3821 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3822 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3823 spa_load_spares(spa);
3824 spa_config_exit(spa, SCL_ALL, FTAG);
3825 spa->spa_spares.sav_sync = B_TRUE;
3827 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3828 &l2cache, &nl2cache) == 0) {
3829 if (spa->spa_l2cache.sav_config)
3830 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3831 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3833 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3834 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3835 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3836 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3837 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3838 spa_load_l2cache(spa);
3839 spa_config_exit(spa, SCL_ALL, FTAG);
3840 spa->spa_l2cache.sav_sync = B_TRUE;
3844 * Check for any removed devices.
3846 if (spa->spa_autoreplace) {
3847 spa_aux_check_removed(&spa->spa_spares);
3848 spa_aux_check_removed(&spa->spa_l2cache);
3851 if (spa_writeable(spa)) {
3853 * Update the config cache to include the newly-imported pool.
3855 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3859 * It's possible that the pool was expanded while it was exported.
3860 * We kick off an async task to handle this for us.
3862 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3864 mutex_exit(&spa_namespace_lock);
3865 spa_history_log_version(spa, LOG_POOL_IMPORT);
3871 spa_tryimport(nvlist_t *tryconfig)
3873 nvlist_t *config = NULL;
3879 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3882 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3886 * Create and initialize the spa structure.
3888 mutex_enter(&spa_namespace_lock);
3889 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3890 spa_activate(spa, FREAD);
3893 * Pass off the heavy lifting to spa_load().
3894 * Pass TRUE for mosconfig because the user-supplied config
3895 * is actually the one to trust when doing an import.
3897 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3900 * If 'tryconfig' was at least parsable, return the current config.
3902 if (spa->spa_root_vdev != NULL) {
3903 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3904 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3906 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3908 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3909 spa->spa_uberblock.ub_timestamp) == 0);
3910 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3911 spa->spa_load_info) == 0);
3914 * If the bootfs property exists on this pool then we
3915 * copy it out so that external consumers can tell which
3916 * pools are bootable.
3918 if ((!error || error == EEXIST) && spa->spa_bootfs) {
3919 char *tmpname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
3922 * We have to play games with the name since the
3923 * pool was opened as TRYIMPORT_NAME.
3925 if (dsl_dsobj_to_dsname(spa_name(spa),
3926 spa->spa_bootfs, tmpname) == 0) {
3928 char *dsname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
3930 cp = strchr(tmpname, '/');
3932 (void) strlcpy(dsname, tmpname,
3935 (void) snprintf(dsname, MAXPATHLEN,
3936 "%s/%s", poolname, ++cp);
3938 VERIFY(nvlist_add_string(config,
3939 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3940 kmem_free(dsname, MAXPATHLEN);
3942 kmem_free(tmpname, MAXPATHLEN);
3946 * Add the list of hot spares and level 2 cache devices.
3948 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3949 spa_add_spares(spa, config);
3950 spa_add_l2cache(spa, config);
3951 spa_config_exit(spa, SCL_CONFIG, FTAG);
3955 spa_deactivate(spa);
3957 mutex_exit(&spa_namespace_lock);
3963 * Pool export/destroy
3965 * The act of destroying or exporting a pool is very simple. We make sure there
3966 * is no more pending I/O and any references to the pool are gone. Then, we
3967 * update the pool state and sync all the labels to disk, removing the
3968 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3969 * we don't sync the labels or remove the configuration cache.
3972 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
3973 boolean_t force, boolean_t hardforce)
3980 if (!(spa_mode_global & FWRITE))
3983 mutex_enter(&spa_namespace_lock);
3984 if ((spa = spa_lookup(pool)) == NULL) {
3985 mutex_exit(&spa_namespace_lock);
3990 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3991 * reacquire the namespace lock, and see if we can export.
3993 spa_open_ref(spa, FTAG);
3994 mutex_exit(&spa_namespace_lock);
3995 spa_async_suspend(spa);
3996 mutex_enter(&spa_namespace_lock);
3997 spa_close(spa, FTAG);
4000 * The pool will be in core if it's openable,
4001 * in which case we can modify its state.
4003 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4005 * Objsets may be open only because they're dirty, so we
4006 * have to force it to sync before checking spa_refcnt.
4008 txg_wait_synced(spa->spa_dsl_pool, 0);
4011 * A pool cannot be exported or destroyed if there are active
4012 * references. If we are resetting a pool, allow references by
4013 * fault injection handlers.
4015 if (!spa_refcount_zero(spa) ||
4016 (spa->spa_inject_ref != 0 &&
4017 new_state != POOL_STATE_UNINITIALIZED)) {
4018 spa_async_resume(spa);
4019 mutex_exit(&spa_namespace_lock);
4024 * A pool cannot be exported if it has an active shared spare.
4025 * This is to prevent other pools stealing the active spare
4026 * from an exported pool. At user's own will, such pool can
4027 * be forcedly exported.
4029 if (!force && new_state == POOL_STATE_EXPORTED &&
4030 spa_has_active_shared_spare(spa)) {
4031 spa_async_resume(spa);
4032 mutex_exit(&spa_namespace_lock);
4037 * We want this to be reflected on every label,
4038 * so mark them all dirty. spa_unload() will do the
4039 * final sync that pushes these changes out.
4041 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4042 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4043 spa->spa_state = new_state;
4044 spa->spa_final_txg = spa_last_synced_txg(spa) +
4046 vdev_config_dirty(spa->spa_root_vdev);
4047 spa_config_exit(spa, SCL_ALL, FTAG);
4051 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_DESTROY);
4053 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4055 spa_deactivate(spa);
4058 if (oldconfig && spa->spa_config)
4059 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4061 if (new_state != POOL_STATE_UNINITIALIZED) {
4063 spa_config_sync(spa, B_TRUE, B_TRUE);
4066 mutex_exit(&spa_namespace_lock);
4072 * Destroy a storage pool.
4075 spa_destroy(char *pool)
4077 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4082 * Export a storage pool.
4085 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4086 boolean_t hardforce)
4088 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4093 * Similar to spa_export(), this unloads the spa_t without actually removing it
4094 * from the namespace in any way.
4097 spa_reset(char *pool)
4099 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4104 * ==========================================================================
4105 * Device manipulation
4106 * ==========================================================================
4110 * Add a device to a storage pool.
4113 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4117 vdev_t *rvd = spa->spa_root_vdev;
4119 nvlist_t **spares, **l2cache;
4120 uint_t nspares, nl2cache;
4123 ASSERT(spa_writeable(spa));
4125 txg = spa_vdev_enter(spa);
4127 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4128 VDEV_ALLOC_ADD)) != 0)
4129 return (spa_vdev_exit(spa, NULL, txg, error));
4131 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4133 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4137 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4141 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4142 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4144 if (vd->vdev_children != 0 &&
4145 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4146 return (spa_vdev_exit(spa, vd, txg, error));
4149 * We must validate the spares and l2cache devices after checking the
4150 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4152 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4153 return (spa_vdev_exit(spa, vd, txg, error));
4156 * Transfer each new top-level vdev from vd to rvd.
4158 for (c = 0; c < vd->vdev_children; c++) {
4161 * Set the vdev id to the first hole, if one exists.
4163 for (id = 0; id < rvd->vdev_children; id++) {
4164 if (rvd->vdev_child[id]->vdev_ishole) {
4165 vdev_free(rvd->vdev_child[id]);
4169 tvd = vd->vdev_child[c];
4170 vdev_remove_child(vd, tvd);
4172 vdev_add_child(rvd, tvd);
4173 vdev_config_dirty(tvd);
4177 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4178 ZPOOL_CONFIG_SPARES);
4179 spa_load_spares(spa);
4180 spa->spa_spares.sav_sync = B_TRUE;
4183 if (nl2cache != 0) {
4184 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4185 ZPOOL_CONFIG_L2CACHE);
4186 spa_load_l2cache(spa);
4187 spa->spa_l2cache.sav_sync = B_TRUE;
4191 * We have to be careful when adding new vdevs to an existing pool.
4192 * If other threads start allocating from these vdevs before we
4193 * sync the config cache, and we lose power, then upon reboot we may
4194 * fail to open the pool because there are DVAs that the config cache
4195 * can't translate. Therefore, we first add the vdevs without
4196 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4197 * and then let spa_config_update() initialize the new metaslabs.
4199 * spa_load() checks for added-but-not-initialized vdevs, so that
4200 * if we lose power at any point in this sequence, the remaining
4201 * steps will be completed the next time we load the pool.
4203 (void) spa_vdev_exit(spa, vd, txg, 0);
4205 mutex_enter(&spa_namespace_lock);
4206 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4207 mutex_exit(&spa_namespace_lock);
4213 * Attach a device to a mirror. The arguments are the path to any device
4214 * in the mirror, and the nvroot for the new device. If the path specifies
4215 * a device that is not mirrored, we automatically insert the mirror vdev.
4217 * If 'replacing' is specified, the new device is intended to replace the
4218 * existing device; in this case the two devices are made into their own
4219 * mirror using the 'replacing' vdev, which is functionally identical to
4220 * the mirror vdev (it actually reuses all the same ops) but has a few
4221 * extra rules: you can't attach to it after it's been created, and upon
4222 * completion of resilvering, the first disk (the one being replaced)
4223 * is automatically detached.
4226 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4228 uint64_t txg, dtl_max_txg;
4229 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
4230 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4232 char *oldvdpath, *newvdpath;
4236 ASSERT(spa_writeable(spa));
4238 txg = spa_vdev_enter(spa);
4240 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4243 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4245 if (!oldvd->vdev_ops->vdev_op_leaf)
4246 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4248 pvd = oldvd->vdev_parent;
4250 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4251 VDEV_ALLOC_ATTACH)) != 0)
4252 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4254 if (newrootvd->vdev_children != 1)
4255 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4257 newvd = newrootvd->vdev_child[0];
4259 if (!newvd->vdev_ops->vdev_op_leaf)
4260 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4262 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4263 return (spa_vdev_exit(spa, newrootvd, txg, error));
4266 * Spares can't replace logs
4268 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4269 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4273 * For attach, the only allowable parent is a mirror or the root
4276 if (pvd->vdev_ops != &vdev_mirror_ops &&
4277 pvd->vdev_ops != &vdev_root_ops)
4278 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4280 pvops = &vdev_mirror_ops;
4283 * Active hot spares can only be replaced by inactive hot
4286 if (pvd->vdev_ops == &vdev_spare_ops &&
4287 oldvd->vdev_isspare &&
4288 !spa_has_spare(spa, newvd->vdev_guid))
4289 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4292 * If the source is a hot spare, and the parent isn't already a
4293 * spare, then we want to create a new hot spare. Otherwise, we
4294 * want to create a replacing vdev. The user is not allowed to
4295 * attach to a spared vdev child unless the 'isspare' state is
4296 * the same (spare replaces spare, non-spare replaces
4299 if (pvd->vdev_ops == &vdev_replacing_ops &&
4300 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4301 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4302 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4303 newvd->vdev_isspare != oldvd->vdev_isspare) {
4304 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4307 if (newvd->vdev_isspare)
4308 pvops = &vdev_spare_ops;
4310 pvops = &vdev_replacing_ops;
4314 * Make sure the new device is big enough.
4316 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4317 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4320 * The new device cannot have a higher alignment requirement
4321 * than the top-level vdev.
4323 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4324 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4327 * If this is an in-place replacement, update oldvd's path and devid
4328 * to make it distinguishable from newvd, and unopenable from now on.
4330 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4331 spa_strfree(oldvd->vdev_path);
4332 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4334 (void) sprintf(oldvd->vdev_path, "%s/%s",
4335 newvd->vdev_path, "old");
4336 if (oldvd->vdev_devid != NULL) {
4337 spa_strfree(oldvd->vdev_devid);
4338 oldvd->vdev_devid = NULL;
4342 /* mark the device being resilvered */
4343 newvd->vdev_resilvering = B_TRUE;
4346 * If the parent is not a mirror, or if we're replacing, insert the new
4347 * mirror/replacing/spare vdev above oldvd.
4349 if (pvd->vdev_ops != pvops)
4350 pvd = vdev_add_parent(oldvd, pvops);
4352 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4353 ASSERT(pvd->vdev_ops == pvops);
4354 ASSERT(oldvd->vdev_parent == pvd);
4357 * Extract the new device from its root and add it to pvd.
4359 vdev_remove_child(newrootvd, newvd);
4360 newvd->vdev_id = pvd->vdev_children;
4361 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4362 vdev_add_child(pvd, newvd);
4364 tvd = newvd->vdev_top;
4365 ASSERT(pvd->vdev_top == tvd);
4366 ASSERT(tvd->vdev_parent == rvd);
4368 vdev_config_dirty(tvd);
4371 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4372 * for any dmu_sync-ed blocks. It will propagate upward when
4373 * spa_vdev_exit() calls vdev_dtl_reassess().
4375 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4377 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4378 dtl_max_txg - TXG_INITIAL);
4380 if (newvd->vdev_isspare) {
4381 spa_spare_activate(newvd);
4382 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_DEVICE_SPARE);
4385 oldvdpath = spa_strdup(oldvd->vdev_path);
4386 newvdpath = spa_strdup(newvd->vdev_path);
4387 newvd_isspare = newvd->vdev_isspare;
4390 * Mark newvd's DTL dirty in this txg.
4392 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4395 * Restart the resilver
4397 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4402 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4404 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
4405 "%s vdev=%s %s vdev=%s",
4406 replacing && newvd_isspare ? "spare in" :
4407 replacing ? "replace" : "attach", newvdpath,
4408 replacing ? "for" : "to", oldvdpath);
4410 spa_strfree(oldvdpath);
4411 spa_strfree(newvdpath);
4413 if (spa->spa_bootfs)
4414 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH);
4420 * Detach a device from a mirror or replacing vdev.
4421 * If 'replace_done' is specified, only detach if the parent
4422 * is a replacing vdev.
4425 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4429 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
4430 vdev_t *vd, *pvd, *cvd, *tvd;
4431 boolean_t unspare = B_FALSE;
4432 uint64_t unspare_guid = 0;
4436 ASSERT(spa_writeable(spa));
4438 txg = spa_vdev_enter(spa);
4440 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4443 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4445 if (!vd->vdev_ops->vdev_op_leaf)
4446 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4448 pvd = vd->vdev_parent;
4451 * If the parent/child relationship is not as expected, don't do it.
4452 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4453 * vdev that's replacing B with C. The user's intent in replacing
4454 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4455 * the replace by detaching C, the expected behavior is to end up
4456 * M(A,B). But suppose that right after deciding to detach C,
4457 * the replacement of B completes. We would have M(A,C), and then
4458 * ask to detach C, which would leave us with just A -- not what
4459 * the user wanted. To prevent this, we make sure that the
4460 * parent/child relationship hasn't changed -- in this example,
4461 * that C's parent is still the replacing vdev R.
4463 if (pvd->vdev_guid != pguid && pguid != 0)
4464 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4467 * Only 'replacing' or 'spare' vdevs can be replaced.
4469 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4470 pvd->vdev_ops != &vdev_spare_ops)
4471 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4473 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4474 spa_version(spa) >= SPA_VERSION_SPARES);
4477 * Only mirror, replacing, and spare vdevs support detach.
4479 if (pvd->vdev_ops != &vdev_replacing_ops &&
4480 pvd->vdev_ops != &vdev_mirror_ops &&
4481 pvd->vdev_ops != &vdev_spare_ops)
4482 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4485 * If this device has the only valid copy of some data,
4486 * we cannot safely detach it.
4488 if (vdev_dtl_required(vd))
4489 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4491 ASSERT(pvd->vdev_children >= 2);
4494 * If we are detaching the second disk from a replacing vdev, then
4495 * check to see if we changed the original vdev's path to have "/old"
4496 * at the end in spa_vdev_attach(). If so, undo that change now.
4498 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4499 vd->vdev_path != NULL) {
4500 size_t len = strlen(vd->vdev_path);
4502 for (c = 0; c < pvd->vdev_children; c++) {
4503 cvd = pvd->vdev_child[c];
4505 if (cvd == vd || cvd->vdev_path == NULL)
4508 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4509 strcmp(cvd->vdev_path + len, "/old") == 0) {
4510 spa_strfree(cvd->vdev_path);
4511 cvd->vdev_path = spa_strdup(vd->vdev_path);
4518 * If we are detaching the original disk from a spare, then it implies
4519 * that the spare should become a real disk, and be removed from the
4520 * active spare list for the pool.
4522 if (pvd->vdev_ops == &vdev_spare_ops &&
4524 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4528 * Erase the disk labels so the disk can be used for other things.
4529 * This must be done after all other error cases are handled,
4530 * but before we disembowel vd (so we can still do I/O to it).
4531 * But if we can't do it, don't treat the error as fatal --
4532 * it may be that the unwritability of the disk is the reason
4533 * it's being detached!
4535 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4538 * Remove vd from its parent and compact the parent's children.
4540 vdev_remove_child(pvd, vd);
4541 vdev_compact_children(pvd);
4544 * Remember one of the remaining children so we can get tvd below.
4546 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4549 * If we need to remove the remaining child from the list of hot spares,
4550 * do it now, marking the vdev as no longer a spare in the process.
4551 * We must do this before vdev_remove_parent(), because that can
4552 * change the GUID if it creates a new toplevel GUID. For a similar
4553 * reason, we must remove the spare now, in the same txg as the detach;
4554 * otherwise someone could attach a new sibling, change the GUID, and
4555 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4558 ASSERT(cvd->vdev_isspare);
4559 spa_spare_remove(cvd);
4560 unspare_guid = cvd->vdev_guid;
4561 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4562 cvd->vdev_unspare = B_TRUE;
4566 * If the parent mirror/replacing vdev only has one child,
4567 * the parent is no longer needed. Remove it from the tree.
4569 if (pvd->vdev_children == 1) {
4570 if (pvd->vdev_ops == &vdev_spare_ops)
4571 cvd->vdev_unspare = B_FALSE;
4572 vdev_remove_parent(cvd);
4573 cvd->vdev_resilvering = B_FALSE;
4578 * We don't set tvd until now because the parent we just removed
4579 * may have been the previous top-level vdev.
4581 tvd = cvd->vdev_top;
4582 ASSERT(tvd->vdev_parent == rvd);
4585 * Reevaluate the parent vdev state.
4587 vdev_propagate_state(cvd);
4590 * If the 'autoexpand' property is set on the pool then automatically
4591 * try to expand the size of the pool. For example if the device we
4592 * just detached was smaller than the others, it may be possible to
4593 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4594 * first so that we can obtain the updated sizes of the leaf vdevs.
4596 if (spa->spa_autoexpand) {
4598 vdev_expand(tvd, txg);
4601 vdev_config_dirty(tvd);
4604 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4605 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4606 * But first make sure we're not on any *other* txg's DTL list, to
4607 * prevent vd from being accessed after it's freed.
4609 vdpath = spa_strdup(vd->vdev_path);
4610 for (t = 0; t < TXG_SIZE; t++)
4611 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4612 vd->vdev_detached = B_TRUE;
4613 vdev_dirty(tvd, VDD_DTL, vd, txg);
4615 spa_event_notify(spa, vd, FM_EREPORT_ZFS_DEVICE_REMOVE);
4617 /* hang on to the spa before we release the lock */
4618 spa_open_ref(spa, FTAG);
4620 error = spa_vdev_exit(spa, vd, txg, 0);
4622 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4624 spa_strfree(vdpath);
4627 * If this was the removal of the original device in a hot spare vdev,
4628 * then we want to go through and remove the device from the hot spare
4629 * list of every other pool.
4632 spa_t *altspa = NULL;
4634 mutex_enter(&spa_namespace_lock);
4635 while ((altspa = spa_next(altspa)) != NULL) {
4636 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4640 spa_open_ref(altspa, FTAG);
4641 mutex_exit(&spa_namespace_lock);
4642 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4643 mutex_enter(&spa_namespace_lock);
4644 spa_close(altspa, FTAG);
4646 mutex_exit(&spa_namespace_lock);
4648 /* search the rest of the vdevs for spares to remove */
4649 spa_vdev_resilver_done(spa);
4652 /* all done with the spa; OK to release */
4653 mutex_enter(&spa_namespace_lock);
4654 spa_close(spa, FTAG);
4655 mutex_exit(&spa_namespace_lock);
4661 * Split a set of devices from their mirrors, and create a new pool from them.
4664 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4665 nvlist_t *props, boolean_t exp)
4668 uint64_t txg, *glist;
4670 uint_t c, children, lastlog;
4671 nvlist_t **child, *nvl, *tmp;
4673 char *altroot = NULL;
4674 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4675 boolean_t activate_slog;
4677 ASSERT(spa_writeable(spa));
4679 txg = spa_vdev_enter(spa);
4681 /* clear the log and flush everything up to now */
4682 activate_slog = spa_passivate_log(spa);
4683 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4684 error = spa_offline_log(spa);
4685 txg = spa_vdev_config_enter(spa);
4688 spa_activate_log(spa);
4691 return (spa_vdev_exit(spa, NULL, txg, error));
4693 /* check new spa name before going any further */
4694 if (spa_lookup(newname) != NULL)
4695 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4698 * scan through all the children to ensure they're all mirrors
4700 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4701 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4703 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4705 /* first, check to ensure we've got the right child count */
4706 rvd = spa->spa_root_vdev;
4708 for (c = 0; c < rvd->vdev_children; c++) {
4709 vdev_t *vd = rvd->vdev_child[c];
4711 /* don't count the holes & logs as children */
4712 if (vd->vdev_islog || vd->vdev_ishole) {
4720 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4721 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4723 /* next, ensure no spare or cache devices are part of the split */
4724 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4725 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4726 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4728 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_PUSHPAGE);
4729 glist = kmem_zalloc(children * sizeof (uint64_t), KM_PUSHPAGE);
4731 /* then, loop over each vdev and validate it */
4732 for (c = 0; c < children; c++) {
4733 uint64_t is_hole = 0;
4735 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4739 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4740 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4748 /* which disk is going to be split? */
4749 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4755 /* look it up in the spa */
4756 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4757 if (vml[c] == NULL) {
4762 /* make sure there's nothing stopping the split */
4763 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4764 vml[c]->vdev_islog ||
4765 vml[c]->vdev_ishole ||
4766 vml[c]->vdev_isspare ||
4767 vml[c]->vdev_isl2cache ||
4768 !vdev_writeable(vml[c]) ||
4769 vml[c]->vdev_children != 0 ||
4770 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4771 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4776 if (vdev_dtl_required(vml[c])) {
4781 /* we need certain info from the top level */
4782 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4783 vml[c]->vdev_top->vdev_ms_array) == 0);
4784 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4785 vml[c]->vdev_top->vdev_ms_shift) == 0);
4786 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4787 vml[c]->vdev_top->vdev_asize) == 0);
4788 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4789 vml[c]->vdev_top->vdev_ashift) == 0);
4793 kmem_free(vml, children * sizeof (vdev_t *));
4794 kmem_free(glist, children * sizeof (uint64_t));
4795 return (spa_vdev_exit(spa, NULL, txg, error));
4798 /* stop writers from using the disks */
4799 for (c = 0; c < children; c++) {
4801 vml[c]->vdev_offline = B_TRUE;
4803 vdev_reopen(spa->spa_root_vdev);
4806 * Temporarily record the splitting vdevs in the spa config. This
4807 * will disappear once the config is regenerated.
4809 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
4810 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4811 glist, children) == 0);
4812 kmem_free(glist, children * sizeof (uint64_t));
4814 mutex_enter(&spa->spa_props_lock);
4815 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4817 mutex_exit(&spa->spa_props_lock);
4818 spa->spa_config_splitting = nvl;
4819 vdev_config_dirty(spa->spa_root_vdev);
4821 /* configure and create the new pool */
4822 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4823 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4824 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4825 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4826 spa_version(spa)) == 0);
4827 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4828 spa->spa_config_txg) == 0);
4829 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4830 spa_generate_guid(NULL)) == 0);
4831 (void) nvlist_lookup_string(props,
4832 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4834 /* add the new pool to the namespace */
4835 newspa = spa_add(newname, config, altroot);
4836 newspa->spa_config_txg = spa->spa_config_txg;
4837 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4839 /* release the spa config lock, retaining the namespace lock */
4840 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4842 if (zio_injection_enabled)
4843 zio_handle_panic_injection(spa, FTAG, 1);
4845 spa_activate(newspa, spa_mode_global);
4846 spa_async_suspend(newspa);
4848 /* create the new pool from the disks of the original pool */
4849 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4853 /* if that worked, generate a real config for the new pool */
4854 if (newspa->spa_root_vdev != NULL) {
4855 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4856 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
4857 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4858 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4859 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4864 if (props != NULL) {
4865 spa_configfile_set(newspa, props, B_FALSE);
4866 error = spa_prop_set(newspa, props);
4871 /* flush everything */
4872 txg = spa_vdev_config_enter(newspa);
4873 vdev_config_dirty(newspa->spa_root_vdev);
4874 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4876 if (zio_injection_enabled)
4877 zio_handle_panic_injection(spa, FTAG, 2);
4879 spa_async_resume(newspa);
4881 /* finally, update the original pool's config */
4882 txg = spa_vdev_config_enter(spa);
4883 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4884 error = dmu_tx_assign(tx, TXG_WAIT);
4887 for (c = 0; c < children; c++) {
4888 if (vml[c] != NULL) {
4891 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4897 vdev_config_dirty(spa->spa_root_vdev);
4898 spa->spa_config_splitting = NULL;
4902 (void) spa_vdev_exit(spa, NULL, txg, 0);
4904 if (zio_injection_enabled)
4905 zio_handle_panic_injection(spa, FTAG, 3);
4907 /* split is complete; log a history record */
4908 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
4909 "split new pool %s from pool %s", newname, spa_name(spa));
4911 kmem_free(vml, children * sizeof (vdev_t *));
4913 /* if we're not going to mount the filesystems in userland, export */
4915 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
4922 spa_deactivate(newspa);
4925 txg = spa_vdev_config_enter(spa);
4927 /* re-online all offlined disks */
4928 for (c = 0; c < children; c++) {
4930 vml[c]->vdev_offline = B_FALSE;
4932 vdev_reopen(spa->spa_root_vdev);
4934 nvlist_free(spa->spa_config_splitting);
4935 spa->spa_config_splitting = NULL;
4936 (void) spa_vdev_exit(spa, NULL, txg, error);
4938 kmem_free(vml, children * sizeof (vdev_t *));
4943 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
4947 for (i = 0; i < count; i++) {
4950 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
4953 if (guid == target_guid)
4961 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
4962 nvlist_t *dev_to_remove)
4964 nvlist_t **newdev = NULL;
4968 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_PUSHPAGE);
4970 for (i = 0, j = 0; i < count; i++) {
4971 if (dev[i] == dev_to_remove)
4973 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_PUSHPAGE) == 0);
4976 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
4977 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
4979 for (i = 0; i < count - 1; i++)
4980 nvlist_free(newdev[i]);
4983 kmem_free(newdev, (count - 1) * sizeof (void *));
4987 * Evacuate the device.
4990 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
4995 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4996 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4997 ASSERT(vd == vd->vdev_top);
5000 * Evacuate the device. We don't hold the config lock as writer
5001 * since we need to do I/O but we do keep the
5002 * spa_namespace_lock held. Once this completes the device
5003 * should no longer have any blocks allocated on it.
5005 if (vd->vdev_islog) {
5006 if (vd->vdev_stat.vs_alloc != 0)
5007 error = spa_offline_log(spa);
5016 * The evacuation succeeded. Remove any remaining MOS metadata
5017 * associated with this vdev, and wait for these changes to sync.
5019 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
5020 txg = spa_vdev_config_enter(spa);
5021 vd->vdev_removing = B_TRUE;
5022 vdev_dirty(vd, 0, NULL, txg);
5023 vdev_config_dirty(vd);
5024 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5030 * Complete the removal by cleaning up the namespace.
5033 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5035 vdev_t *rvd = spa->spa_root_vdev;
5036 uint64_t id = vd->vdev_id;
5037 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5039 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5040 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5041 ASSERT(vd == vd->vdev_top);
5044 * Only remove any devices which are empty.
5046 if (vd->vdev_stat.vs_alloc != 0)
5049 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5051 if (list_link_active(&vd->vdev_state_dirty_node))
5052 vdev_state_clean(vd);
5053 if (list_link_active(&vd->vdev_config_dirty_node))
5054 vdev_config_clean(vd);
5059 vdev_compact_children(rvd);
5061 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5062 vdev_add_child(rvd, vd);
5064 vdev_config_dirty(rvd);
5067 * Reassess the health of our root vdev.
5073 * Remove a device from the pool -
5075 * Removing a device from the vdev namespace requires several steps
5076 * and can take a significant amount of time. As a result we use
5077 * the spa_vdev_config_[enter/exit] functions which allow us to
5078 * grab and release the spa_config_lock while still holding the namespace
5079 * lock. During each step the configuration is synced out.
5083 * Remove a device from the pool. Currently, this supports removing only hot
5084 * spares, slogs, and level 2 ARC devices.
5087 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5090 metaslab_group_t *mg;
5091 nvlist_t **spares, **l2cache, *nv;
5093 uint_t nspares, nl2cache;
5095 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5097 ASSERT(spa_writeable(spa));
5100 txg = spa_vdev_enter(spa);
5102 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5104 if (spa->spa_spares.sav_vdevs != NULL &&
5105 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5106 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5107 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5109 * Only remove the hot spare if it's not currently in use
5112 if (vd == NULL || unspare) {
5113 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5114 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5115 spa_load_spares(spa);
5116 spa->spa_spares.sav_sync = B_TRUE;
5120 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5121 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5122 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5123 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5125 * Cache devices can always be removed.
5127 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5128 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5129 spa_load_l2cache(spa);
5130 spa->spa_l2cache.sav_sync = B_TRUE;
5131 } else if (vd != NULL && vd->vdev_islog) {
5133 ASSERT(vd == vd->vdev_top);
5136 * XXX - Once we have bp-rewrite this should
5137 * become the common case.
5143 * Stop allocating from this vdev.
5145 metaslab_group_passivate(mg);
5148 * Wait for the youngest allocations and frees to sync,
5149 * and then wait for the deferral of those frees to finish.
5151 spa_vdev_config_exit(spa, NULL,
5152 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5155 * Attempt to evacuate the vdev.
5157 error = spa_vdev_remove_evacuate(spa, vd);
5159 txg = spa_vdev_config_enter(spa);
5162 * If we couldn't evacuate the vdev, unwind.
5165 metaslab_group_activate(mg);
5166 return (spa_vdev_exit(spa, NULL, txg, error));
5170 * Clean up the vdev namespace.
5172 spa_vdev_remove_from_namespace(spa, vd);
5174 } else if (vd != NULL) {
5176 * Normal vdevs cannot be removed (yet).
5181 * There is no vdev of any kind with the specified guid.
5187 return (spa_vdev_exit(spa, NULL, txg, error));
5193 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5194 * current spared, so we can detach it.
5197 spa_vdev_resilver_done_hunt(vdev_t *vd)
5199 vdev_t *newvd, *oldvd;
5202 for (c = 0; c < vd->vdev_children; c++) {
5203 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5209 * Check for a completed replacement. We always consider the first
5210 * vdev in the list to be the oldest vdev, and the last one to be
5211 * the newest (see spa_vdev_attach() for how that works). In
5212 * the case where the newest vdev is faulted, we will not automatically
5213 * remove it after a resilver completes. This is OK as it will require
5214 * user intervention to determine which disk the admin wishes to keep.
5216 if (vd->vdev_ops == &vdev_replacing_ops) {
5217 ASSERT(vd->vdev_children > 1);
5219 newvd = vd->vdev_child[vd->vdev_children - 1];
5220 oldvd = vd->vdev_child[0];
5222 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5223 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5224 !vdev_dtl_required(oldvd))
5229 * Check for a completed resilver with the 'unspare' flag set.
5231 if (vd->vdev_ops == &vdev_spare_ops) {
5232 vdev_t *first = vd->vdev_child[0];
5233 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5235 if (last->vdev_unspare) {
5238 } else if (first->vdev_unspare) {
5245 if (oldvd != NULL &&
5246 vdev_dtl_empty(newvd, DTL_MISSING) &&
5247 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5248 !vdev_dtl_required(oldvd))
5252 * If there are more than two spares attached to a disk,
5253 * and those spares are not required, then we want to
5254 * attempt to free them up now so that they can be used
5255 * by other pools. Once we're back down to a single
5256 * disk+spare, we stop removing them.
5258 if (vd->vdev_children > 2) {
5259 newvd = vd->vdev_child[1];
5261 if (newvd->vdev_isspare && last->vdev_isspare &&
5262 vdev_dtl_empty(last, DTL_MISSING) &&
5263 vdev_dtl_empty(last, DTL_OUTAGE) &&
5264 !vdev_dtl_required(newvd))
5273 spa_vdev_resilver_done(spa_t *spa)
5275 vdev_t *vd, *pvd, *ppvd;
5276 uint64_t guid, sguid, pguid, ppguid;
5278 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5280 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5281 pvd = vd->vdev_parent;
5282 ppvd = pvd->vdev_parent;
5283 guid = vd->vdev_guid;
5284 pguid = pvd->vdev_guid;
5285 ppguid = ppvd->vdev_guid;
5288 * If we have just finished replacing a hot spared device, then
5289 * we need to detach the parent's first child (the original hot
5292 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5293 ppvd->vdev_children == 2) {
5294 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5295 sguid = ppvd->vdev_child[1]->vdev_guid;
5297 spa_config_exit(spa, SCL_ALL, FTAG);
5298 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5300 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5302 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5305 spa_config_exit(spa, SCL_ALL, FTAG);
5309 * Update the stored path or FRU for this vdev.
5312 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5316 boolean_t sync = B_FALSE;
5318 ASSERT(spa_writeable(spa));
5320 spa_vdev_state_enter(spa, SCL_ALL);
5322 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5323 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5325 if (!vd->vdev_ops->vdev_op_leaf)
5326 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5329 if (strcmp(value, vd->vdev_path) != 0) {
5330 spa_strfree(vd->vdev_path);
5331 vd->vdev_path = spa_strdup(value);
5335 if (vd->vdev_fru == NULL) {
5336 vd->vdev_fru = spa_strdup(value);
5338 } else if (strcmp(value, vd->vdev_fru) != 0) {
5339 spa_strfree(vd->vdev_fru);
5340 vd->vdev_fru = spa_strdup(value);
5345 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5349 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5351 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5355 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5357 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5361 * ==========================================================================
5363 * ==========================================================================
5367 spa_scan_stop(spa_t *spa)
5369 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5370 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5372 return (dsl_scan_cancel(spa->spa_dsl_pool));
5376 spa_scan(spa_t *spa, pool_scan_func_t func)
5378 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5380 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5384 * If a resilver was requested, but there is no DTL on a
5385 * writeable leaf device, we have nothing to do.
5387 if (func == POOL_SCAN_RESILVER &&
5388 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5389 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5393 return (dsl_scan(spa->spa_dsl_pool, func));
5397 * ==========================================================================
5398 * SPA async task processing
5399 * ==========================================================================
5403 spa_async_remove(spa_t *spa, vdev_t *vd)
5407 if (vd->vdev_remove_wanted) {
5408 vd->vdev_remove_wanted = B_FALSE;
5409 vd->vdev_delayed_close = B_FALSE;
5410 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5413 * We want to clear the stats, but we don't want to do a full
5414 * vdev_clear() as that will cause us to throw away
5415 * degraded/faulted state as well as attempt to reopen the
5416 * device, all of which is a waste.
5418 vd->vdev_stat.vs_read_errors = 0;
5419 vd->vdev_stat.vs_write_errors = 0;
5420 vd->vdev_stat.vs_checksum_errors = 0;
5422 vdev_state_dirty(vd->vdev_top);
5425 for (c = 0; c < vd->vdev_children; c++)
5426 spa_async_remove(spa, vd->vdev_child[c]);
5430 spa_async_probe(spa_t *spa, vdev_t *vd)
5434 if (vd->vdev_probe_wanted) {
5435 vd->vdev_probe_wanted = B_FALSE;
5436 vdev_reopen(vd); /* vdev_open() does the actual probe */
5439 for (c = 0; c < vd->vdev_children; c++)
5440 spa_async_probe(spa, vd->vdev_child[c]);
5444 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5448 if (!spa->spa_autoexpand)
5451 for (c = 0; c < vd->vdev_children; c++) {
5452 vdev_t *cvd = vd->vdev_child[c];
5453 spa_async_autoexpand(spa, cvd);
5456 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5459 spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND);
5463 spa_async_thread(spa_t *spa)
5467 ASSERT(spa->spa_sync_on);
5469 mutex_enter(&spa->spa_async_lock);
5470 tasks = spa->spa_async_tasks;
5471 spa->spa_async_tasks = 0;
5472 mutex_exit(&spa->spa_async_lock);
5475 * See if the config needs to be updated.
5477 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5478 uint64_t old_space, new_space;
5480 mutex_enter(&spa_namespace_lock);
5481 old_space = metaslab_class_get_space(spa_normal_class(spa));
5482 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5483 new_space = metaslab_class_get_space(spa_normal_class(spa));
5484 mutex_exit(&spa_namespace_lock);
5487 * If the pool grew as a result of the config update,
5488 * then log an internal history event.
5490 if (new_space != old_space) {
5491 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5493 "pool '%s' size: %llu(+%llu)",
5494 spa_name(spa), new_space, new_space - old_space);
5499 * See if any devices need to be marked REMOVED.
5501 if (tasks & SPA_ASYNC_REMOVE) {
5502 spa_vdev_state_enter(spa, SCL_NONE);
5503 spa_async_remove(spa, spa->spa_root_vdev);
5504 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
5505 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5506 for (i = 0; i < spa->spa_spares.sav_count; i++)
5507 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5508 (void) spa_vdev_state_exit(spa, NULL, 0);
5511 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5512 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5513 spa_async_autoexpand(spa, spa->spa_root_vdev);
5514 spa_config_exit(spa, SCL_CONFIG, FTAG);
5518 * See if any devices need to be probed.
5520 if (tasks & SPA_ASYNC_PROBE) {
5521 spa_vdev_state_enter(spa, SCL_NONE);
5522 spa_async_probe(spa, spa->spa_root_vdev);
5523 (void) spa_vdev_state_exit(spa, NULL, 0);
5527 * If any devices are done replacing, detach them.
5529 if (tasks & SPA_ASYNC_RESILVER_DONE)
5530 spa_vdev_resilver_done(spa);
5533 * Kick off a resilver.
5535 if (tasks & SPA_ASYNC_RESILVER)
5536 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5539 * Let the world know that we're done.
5541 mutex_enter(&spa->spa_async_lock);
5542 spa->spa_async_thread = NULL;
5543 cv_broadcast(&spa->spa_async_cv);
5544 mutex_exit(&spa->spa_async_lock);
5549 spa_async_suspend(spa_t *spa)
5551 mutex_enter(&spa->spa_async_lock);
5552 spa->spa_async_suspended++;
5553 while (spa->spa_async_thread != NULL)
5554 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5555 mutex_exit(&spa->spa_async_lock);
5559 spa_async_resume(spa_t *spa)
5561 mutex_enter(&spa->spa_async_lock);
5562 ASSERT(spa->spa_async_suspended != 0);
5563 spa->spa_async_suspended--;
5564 mutex_exit(&spa->spa_async_lock);
5568 spa_async_dispatch(spa_t *spa)
5570 mutex_enter(&spa->spa_async_lock);
5571 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5572 spa->spa_async_thread == NULL &&
5573 rootdir != NULL && !vn_is_readonly(rootdir))
5574 spa->spa_async_thread = thread_create(NULL, 0,
5575 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5576 mutex_exit(&spa->spa_async_lock);
5580 spa_async_request(spa_t *spa, int task)
5582 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5583 mutex_enter(&spa->spa_async_lock);
5584 spa->spa_async_tasks |= task;
5585 mutex_exit(&spa->spa_async_lock);
5589 * ==========================================================================
5590 * SPA syncing routines
5591 * ==========================================================================
5595 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5598 bpobj_enqueue(bpo, bp, tx);
5603 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5607 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5613 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5615 char *packed = NULL;
5620 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5623 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5624 * information. This avoids the dbuf_will_dirty() path and
5625 * saves us a pre-read to get data we don't actually care about.
5627 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
5628 packed = vmem_alloc(bufsize, KM_PUSHPAGE);
5630 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5632 bzero(packed + nvsize, bufsize - nvsize);
5634 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5636 vmem_free(packed, bufsize);
5638 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5639 dmu_buf_will_dirty(db, tx);
5640 *(uint64_t *)db->db_data = nvsize;
5641 dmu_buf_rele(db, FTAG);
5645 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5646 const char *config, const char *entry)
5656 * Update the MOS nvlist describing the list of available devices.
5657 * spa_validate_aux() will have already made sure this nvlist is
5658 * valid and the vdevs are labeled appropriately.
5660 if (sav->sav_object == 0) {
5661 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5662 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5663 sizeof (uint64_t), tx);
5664 VERIFY(zap_update(spa->spa_meta_objset,
5665 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5666 &sav->sav_object, tx) == 0);
5669 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
5670 if (sav->sav_count == 0) {
5671 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5673 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE);
5674 for (i = 0; i < sav->sav_count; i++)
5675 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5676 B_FALSE, VDEV_CONFIG_L2CACHE);
5677 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5678 sav->sav_count) == 0);
5679 for (i = 0; i < sav->sav_count; i++)
5680 nvlist_free(list[i]);
5681 kmem_free(list, sav->sav_count * sizeof (void *));
5684 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5685 nvlist_free(nvroot);
5687 sav->sav_sync = B_FALSE;
5691 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5695 if (list_is_empty(&spa->spa_config_dirty_list))
5698 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5700 config = spa_config_generate(spa, spa->spa_root_vdev,
5701 dmu_tx_get_txg(tx), B_FALSE);
5703 spa_config_exit(spa, SCL_STATE, FTAG);
5705 if (spa->spa_config_syncing)
5706 nvlist_free(spa->spa_config_syncing);
5707 spa->spa_config_syncing = config;
5709 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5713 spa_sync_version(void *arg1, void *arg2, dmu_tx_t *tx)
5716 uint64_t version = *(uint64_t *)arg2;
5719 * Setting the version is special cased when first creating the pool.
5721 ASSERT(tx->tx_txg != TXG_INITIAL);
5723 ASSERT(version <= SPA_VERSION);
5724 ASSERT(version >= spa_version(spa));
5726 spa->spa_uberblock.ub_version = version;
5727 vdev_config_dirty(spa->spa_root_vdev);
5731 * Set zpool properties.
5734 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5737 objset_t *mos = spa->spa_meta_objset;
5738 nvlist_t *nvp = arg2;
5739 nvpair_t *elem = NULL;
5741 mutex_enter(&spa->spa_props_lock);
5743 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5745 char *strval, *fname;
5747 const char *propname;
5748 zprop_type_t proptype;
5749 zfeature_info_t *feature;
5751 prop = zpool_name_to_prop(nvpair_name(elem));
5752 switch ((int)prop) {
5755 * We checked this earlier in spa_prop_validate().
5757 ASSERT(zpool_prop_feature(nvpair_name(elem)));
5759 fname = strchr(nvpair_name(elem), '@') + 1;
5760 VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature));
5762 spa_feature_enable(spa, feature, tx);
5765 case ZPOOL_PROP_VERSION:
5766 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5768 * The version is synced seperatly before other
5769 * properties and should be correct by now.
5771 ASSERT3U(spa_version(spa), >=, intval);
5774 case ZPOOL_PROP_ALTROOT:
5776 * 'altroot' is a non-persistent property. It should
5777 * have been set temporarily at creation or import time.
5779 ASSERT(spa->spa_root != NULL);
5782 case ZPOOL_PROP_READONLY:
5783 case ZPOOL_PROP_CACHEFILE:
5785 * 'readonly' and 'cachefile' are also non-persisitent
5789 case ZPOOL_PROP_COMMENT:
5790 VERIFY(nvpair_value_string(elem, &strval) == 0);
5791 if (spa->spa_comment != NULL)
5792 spa_strfree(spa->spa_comment);
5793 spa->spa_comment = spa_strdup(strval);
5795 * We need to dirty the configuration on all the vdevs
5796 * so that their labels get updated. It's unnecessary
5797 * to do this for pool creation since the vdev's
5798 * configuratoin has already been dirtied.
5800 if (tx->tx_txg != TXG_INITIAL)
5801 vdev_config_dirty(spa->spa_root_vdev);
5805 * Set pool property values in the poolprops mos object.
5807 if (spa->spa_pool_props_object == 0) {
5808 spa->spa_pool_props_object =
5809 zap_create_link(mos, DMU_OT_POOL_PROPS,
5810 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5814 /* normalize the property name */
5815 propname = zpool_prop_to_name(prop);
5816 proptype = zpool_prop_get_type(prop);
5818 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5819 ASSERT(proptype == PROP_TYPE_STRING);
5820 VERIFY(nvpair_value_string(elem, &strval) == 0);
5821 VERIFY(zap_update(mos,
5822 spa->spa_pool_props_object, propname,
5823 1, strlen(strval) + 1, strval, tx) == 0);
5825 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5826 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5828 if (proptype == PROP_TYPE_INDEX) {
5830 VERIFY(zpool_prop_index_to_string(
5831 prop, intval, &unused) == 0);
5833 VERIFY(zap_update(mos,
5834 spa->spa_pool_props_object, propname,
5835 8, 1, &intval, tx) == 0);
5837 ASSERT(0); /* not allowed */
5841 case ZPOOL_PROP_DELEGATION:
5842 spa->spa_delegation = intval;
5844 case ZPOOL_PROP_BOOTFS:
5845 spa->spa_bootfs = intval;
5847 case ZPOOL_PROP_FAILUREMODE:
5848 spa->spa_failmode = intval;
5850 case ZPOOL_PROP_AUTOEXPAND:
5851 spa->spa_autoexpand = intval;
5852 if (tx->tx_txg != TXG_INITIAL)
5853 spa_async_request(spa,
5854 SPA_ASYNC_AUTOEXPAND);
5856 case ZPOOL_PROP_DEDUPDITTO:
5857 spa->spa_dedup_ditto = intval;
5864 /* log internal history if this is not a zpool create */
5865 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5866 tx->tx_txg != TXG_INITIAL) {
5867 spa_history_log_internal(LOG_POOL_PROPSET,
5868 spa, tx, "%s %lld %s",
5869 nvpair_name(elem), intval, spa_name(spa));
5873 mutex_exit(&spa->spa_props_lock);
5877 * Perform one-time upgrade on-disk changes. spa_version() does not
5878 * reflect the new version this txg, so there must be no changes this
5879 * txg to anything that the upgrade code depends on after it executes.
5880 * Therefore this must be called after dsl_pool_sync() does the sync
5884 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5886 dsl_pool_t *dp = spa->spa_dsl_pool;
5888 ASSERT(spa->spa_sync_pass == 1);
5890 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5891 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5892 dsl_pool_create_origin(dp, tx);
5894 /* Keeping the origin open increases spa_minref */
5895 spa->spa_minref += 3;
5898 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5899 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5900 dsl_pool_upgrade_clones(dp, tx);
5903 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5904 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5905 dsl_pool_upgrade_dir_clones(dp, tx);
5907 /* Keeping the freedir open increases spa_minref */
5908 spa->spa_minref += 3;
5911 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
5912 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
5913 spa_feature_create_zap_objects(spa, tx);
5918 * Sync the specified transaction group. New blocks may be dirtied as
5919 * part of the process, so we iterate until it converges.
5922 spa_sync(spa_t *spa, uint64_t txg)
5924 dsl_pool_t *dp = spa->spa_dsl_pool;
5925 objset_t *mos = spa->spa_meta_objset;
5926 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
5927 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
5928 vdev_t *rvd = spa->spa_root_vdev;
5934 VERIFY(spa_writeable(spa));
5937 * Lock out configuration changes.
5939 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5941 spa->spa_syncing_txg = txg;
5942 spa->spa_sync_pass = 0;
5945 * If there are any pending vdev state changes, convert them
5946 * into config changes that go out with this transaction group.
5948 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5949 while (list_head(&spa->spa_state_dirty_list) != NULL) {
5951 * We need the write lock here because, for aux vdevs,
5952 * calling vdev_config_dirty() modifies sav_config.
5953 * This is ugly and will become unnecessary when we
5954 * eliminate the aux vdev wart by integrating all vdevs
5955 * into the root vdev tree.
5957 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5958 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
5959 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
5960 vdev_state_clean(vd);
5961 vdev_config_dirty(vd);
5963 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5964 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
5966 spa_config_exit(spa, SCL_STATE, FTAG);
5968 tx = dmu_tx_create_assigned(dp, txg);
5971 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5972 * set spa_deflate if we have no raid-z vdevs.
5974 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
5975 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
5978 for (i = 0; i < rvd->vdev_children; i++) {
5979 vd = rvd->vdev_child[i];
5980 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
5983 if (i == rvd->vdev_children) {
5984 spa->spa_deflate = TRUE;
5985 VERIFY(0 == zap_add(spa->spa_meta_objset,
5986 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
5987 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
5992 * If anything has changed in this txg, or if someone is waiting
5993 * for this txg to sync (eg, spa_vdev_remove()), push the
5994 * deferred frees from the previous txg. If not, leave them
5995 * alone so that we don't generate work on an otherwise idle
5998 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
5999 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6000 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6001 ((dsl_scan_active(dp->dp_scan) ||
6002 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6003 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6004 VERIFY3U(bpobj_iterate(defer_bpo,
6005 spa_free_sync_cb, zio, tx), ==, 0);
6006 VERIFY3U(zio_wait(zio), ==, 0);
6010 * Iterate to convergence.
6013 int pass = ++spa->spa_sync_pass;
6015 spa_sync_config_object(spa, tx);
6016 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6017 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6018 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6019 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6020 spa_errlog_sync(spa, txg);
6021 dsl_pool_sync(dp, txg);
6023 if (pass <= SYNC_PASS_DEFERRED_FREE) {
6024 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6025 bplist_iterate(free_bpl, spa_free_sync_cb,
6027 VERIFY(zio_wait(zio) == 0);
6029 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6034 dsl_scan_sync(dp, tx);
6036 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)))
6040 spa_sync_upgrades(spa, tx);
6042 } while (dmu_objset_is_dirty(mos, txg));
6045 * Rewrite the vdev configuration (which includes the uberblock)
6046 * to commit the transaction group.
6048 * If there are no dirty vdevs, we sync the uberblock to a few
6049 * random top-level vdevs that are known to be visible in the
6050 * config cache (see spa_vdev_add() for a complete description).
6051 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6055 * We hold SCL_STATE to prevent vdev open/close/etc.
6056 * while we're attempting to write the vdev labels.
6058 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6060 if (list_is_empty(&spa->spa_config_dirty_list)) {
6061 vdev_t *svd[SPA_DVAS_PER_BP];
6063 int children = rvd->vdev_children;
6064 int c0 = spa_get_random(children);
6066 for (c = 0; c < children; c++) {
6067 vd = rvd->vdev_child[(c0 + c) % children];
6068 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6070 svd[svdcount++] = vd;
6071 if (svdcount == SPA_DVAS_PER_BP)
6074 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6076 error = vdev_config_sync(svd, svdcount, txg,
6079 error = vdev_config_sync(rvd->vdev_child,
6080 rvd->vdev_children, txg, B_FALSE);
6082 error = vdev_config_sync(rvd->vdev_child,
6083 rvd->vdev_children, txg, B_TRUE);
6086 spa_config_exit(spa, SCL_STATE, FTAG);
6090 zio_suspend(spa, NULL);
6091 zio_resume_wait(spa);
6096 * Clear the dirty config list.
6098 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6099 vdev_config_clean(vd);
6102 * Now that the new config has synced transactionally,
6103 * let it become visible to the config cache.
6105 if (spa->spa_config_syncing != NULL) {
6106 spa_config_set(spa, spa->spa_config_syncing);
6107 spa->spa_config_txg = txg;
6108 spa->spa_config_syncing = NULL;
6111 spa->spa_ubsync = spa->spa_uberblock;
6113 dsl_pool_sync_done(dp, txg);
6116 * Update usable space statistics.
6118 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))))
6119 vdev_sync_done(vd, txg);
6121 spa_update_dspace(spa);
6124 * It had better be the case that we didn't dirty anything
6125 * since vdev_config_sync().
6127 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6128 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6129 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6131 spa->spa_sync_pass = 0;
6133 spa_config_exit(spa, SCL_CONFIG, FTAG);
6135 spa_handle_ignored_writes(spa);
6138 * If any async tasks have been requested, kick them off.
6140 spa_async_dispatch(spa);
6144 * Sync all pools. We don't want to hold the namespace lock across these
6145 * operations, so we take a reference on the spa_t and drop the lock during the
6149 spa_sync_allpools(void)
6152 mutex_enter(&spa_namespace_lock);
6153 while ((spa = spa_next(spa)) != NULL) {
6154 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6155 !spa_writeable(spa) || spa_suspended(spa))
6157 spa_open_ref(spa, FTAG);
6158 mutex_exit(&spa_namespace_lock);
6159 txg_wait_synced(spa_get_dsl(spa), 0);
6160 mutex_enter(&spa_namespace_lock);
6161 spa_close(spa, FTAG);
6163 mutex_exit(&spa_namespace_lock);
6167 * ==========================================================================
6168 * Miscellaneous routines
6169 * ==========================================================================
6173 * Remove all pools in the system.
6181 * Remove all cached state. All pools should be closed now,
6182 * so every spa in the AVL tree should be unreferenced.
6184 mutex_enter(&spa_namespace_lock);
6185 while ((spa = spa_next(NULL)) != NULL) {
6187 * Stop async tasks. The async thread may need to detach
6188 * a device that's been replaced, which requires grabbing
6189 * spa_namespace_lock, so we must drop it here.
6191 spa_open_ref(spa, FTAG);
6192 mutex_exit(&spa_namespace_lock);
6193 spa_async_suspend(spa);
6194 mutex_enter(&spa_namespace_lock);
6195 spa_close(spa, FTAG);
6197 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6199 spa_deactivate(spa);
6203 mutex_exit(&spa_namespace_lock);
6207 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6212 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6216 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6217 vd = spa->spa_l2cache.sav_vdevs[i];
6218 if (vd->vdev_guid == guid)
6222 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6223 vd = spa->spa_spares.sav_vdevs[i];
6224 if (vd->vdev_guid == guid)
6233 spa_upgrade(spa_t *spa, uint64_t version)
6235 ASSERT(spa_writeable(spa));
6237 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6240 * This should only be called for a non-faulted pool, and since a
6241 * future version would result in an unopenable pool, this shouldn't be
6244 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
6245 ASSERT(version >= spa->spa_uberblock.ub_version);
6247 spa->spa_uberblock.ub_version = version;
6248 vdev_config_dirty(spa->spa_root_vdev);
6250 spa_config_exit(spa, SCL_ALL, FTAG);
6252 txg_wait_synced(spa_get_dsl(spa), 0);
6256 spa_has_spare(spa_t *spa, uint64_t guid)
6260 spa_aux_vdev_t *sav = &spa->spa_spares;
6262 for (i = 0; i < sav->sav_count; i++)
6263 if (sav->sav_vdevs[i]->vdev_guid == guid)
6266 for (i = 0; i < sav->sav_npending; i++) {
6267 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6268 &spareguid) == 0 && spareguid == guid)
6276 * Check if a pool has an active shared spare device.
6277 * Note: reference count of an active spare is 2, as a spare and as a replace
6280 spa_has_active_shared_spare(spa_t *spa)
6284 spa_aux_vdev_t *sav = &spa->spa_spares;
6286 for (i = 0; i < sav->sav_count; i++) {
6287 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6288 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6297 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
6298 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6299 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6300 * or zdb as real changes.
6303 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6306 zfs_ereport_post(name, spa, vd, NULL, 0, 0);
6310 #if defined(_KERNEL) && defined(HAVE_SPL)
6311 /* state manipulation functions */
6312 EXPORT_SYMBOL(spa_open);
6313 EXPORT_SYMBOL(spa_open_rewind);
6314 EXPORT_SYMBOL(spa_get_stats);
6315 EXPORT_SYMBOL(spa_create);
6316 EXPORT_SYMBOL(spa_import_rootpool);
6317 EXPORT_SYMBOL(spa_import);
6318 EXPORT_SYMBOL(spa_tryimport);
6319 EXPORT_SYMBOL(spa_destroy);
6320 EXPORT_SYMBOL(spa_export);
6321 EXPORT_SYMBOL(spa_reset);
6322 EXPORT_SYMBOL(spa_async_request);
6323 EXPORT_SYMBOL(spa_async_suspend);
6324 EXPORT_SYMBOL(spa_async_resume);
6325 EXPORT_SYMBOL(spa_inject_addref);
6326 EXPORT_SYMBOL(spa_inject_delref);
6327 EXPORT_SYMBOL(spa_scan_stat_init);
6328 EXPORT_SYMBOL(spa_scan_get_stats);
6330 /* device maniion */
6331 EXPORT_SYMBOL(spa_vdev_add);
6332 EXPORT_SYMBOL(spa_vdev_attach);
6333 EXPORT_SYMBOL(spa_vdev_detach);
6334 EXPORT_SYMBOL(spa_vdev_remove);
6335 EXPORT_SYMBOL(spa_vdev_setpath);
6336 EXPORT_SYMBOL(spa_vdev_setfru);
6337 EXPORT_SYMBOL(spa_vdev_split_mirror);
6339 /* spare statech is global across all pools) */
6340 EXPORT_SYMBOL(spa_spare_add);
6341 EXPORT_SYMBOL(spa_spare_remove);
6342 EXPORT_SYMBOL(spa_spare_exists);
6343 EXPORT_SYMBOL(spa_spare_activate);
6345 /* L2ARC statech is global across all pools) */
6346 EXPORT_SYMBOL(spa_l2cache_add);
6347 EXPORT_SYMBOL(spa_l2cache_remove);
6348 EXPORT_SYMBOL(spa_l2cache_exists);
6349 EXPORT_SYMBOL(spa_l2cache_activate);
6350 EXPORT_SYMBOL(spa_l2cache_drop);
6353 EXPORT_SYMBOL(spa_scan);
6354 EXPORT_SYMBOL(spa_scan_stop);
6357 EXPORT_SYMBOL(spa_sync); /* only for DMU use */
6358 EXPORT_SYMBOL(spa_sync_allpools);
6361 EXPORT_SYMBOL(spa_prop_set);
6362 EXPORT_SYMBOL(spa_prop_get);
6363 EXPORT_SYMBOL(spa_prop_clear_bootfs);
6365 /* asynchronous event notification */
6366 EXPORT_SYMBOL(spa_event_notify);