4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
28 * This file contains all the routines used when modifying on-disk SPA state.
29 * This includes opening, importing, destroying, exporting a pool, and syncing a
33 #include <sys/zfs_context.h>
34 #include <sys/fm/fs/zfs.h>
35 #include <sys/spa_impl.h>
37 #include <sys/zio_checksum.h>
39 #include <sys/dmu_tx.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/metaslab.h>
45 #include <sys/metaslab_impl.h>
46 #include <sys/uberblock_impl.h>
49 #include <sys/dmu_traverse.h>
50 #include <sys/dmu_objset.h>
51 #include <sys/unique.h>
52 #include <sys/dsl_pool.h>
53 #include <sys/dsl_dataset.h>
54 #include <sys/dsl_dir.h>
55 #include <sys/dsl_prop.h>
56 #include <sys/dsl_synctask.h>
57 #include <sys/fs/zfs.h>
59 #include <sys/callb.h>
60 #include <sys/spa_boot.h>
61 #include <sys/zfs_ioctl.h>
62 #include <sys/dsl_scan.h>
66 #include <sys/callb.h>
67 #include <sys/cpupart.h>
72 #include "zfs_comutil.h"
74 /* Check hostid on import? */
75 static int check_hostid = 1;
77 SYSCTL_DECL(_vfs_zfs);
78 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
79 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
80 "Check hostid on import?");
82 typedef enum zti_modes {
83 zti_mode_fixed, /* value is # of threads (min 1) */
84 zti_mode_online_percent, /* value is % of online CPUs */
85 zti_mode_batch, /* cpu-intensive; value is ignored */
86 zti_mode_null, /* don't create a taskq */
90 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
91 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
92 #define ZTI_BATCH { zti_mode_batch, 0 }
93 #define ZTI_NULL { zti_mode_null, 0 }
95 #define ZTI_ONE ZTI_FIX(1)
97 typedef struct zio_taskq_info {
98 enum zti_modes zti_mode;
102 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
103 "issue", "issue_high", "intr", "intr_high"
107 * Define the taskq threads for the following I/O types:
108 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
110 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
111 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
112 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
113 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
114 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
115 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
116 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
117 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
120 static dsl_syncfunc_t spa_sync_props;
121 static boolean_t spa_has_active_shared_spare(spa_t *spa);
122 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
123 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
125 static void spa_vdev_resilver_done(spa_t *spa);
127 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
129 id_t zio_taskq_psrset_bind = PS_NONE;
132 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
134 uint_t zio_taskq_basedc = 80; /* base duty cycle */
136 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
139 * This (illegal) pool name is used when temporarily importing a spa_t in order
140 * to get the vdev stats associated with the imported devices.
142 #define TRYIMPORT_NAME "$import"
145 * ==========================================================================
146 * SPA properties routines
147 * ==========================================================================
151 * Add a (source=src, propname=propval) list to an nvlist.
154 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
155 uint64_t intval, zprop_source_t src)
157 const char *propname = zpool_prop_to_name(prop);
160 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
161 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
164 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
166 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
168 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
169 nvlist_free(propval);
173 * Get property values from the spa configuration.
176 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
178 vdev_t *rvd = spa->spa_root_vdev;
182 uint64_t cap, version;
183 zprop_source_t src = ZPROP_SRC_NONE;
184 spa_config_dirent_t *dp;
186 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
189 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
190 size = metaslab_class_get_space(spa_normal_class(spa));
191 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
192 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
193 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
194 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
198 for (int c = 0; c < rvd->vdev_children; c++) {
199 vdev_t *tvd = rvd->vdev_child[c];
200 space += tvd->vdev_max_asize - tvd->vdev_asize;
202 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
205 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
206 (spa_mode(spa) == FREAD), src);
208 cap = (size == 0) ? 0 : (alloc * 100 / size);
209 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
211 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
212 ddt_get_pool_dedup_ratio(spa), src);
214 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
215 rvd->vdev_state, src);
217 version = spa_version(spa);
218 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
219 src = ZPROP_SRC_DEFAULT;
221 src = ZPROP_SRC_LOCAL;
222 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
225 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
227 if (spa->spa_comment != NULL) {
228 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
232 if (spa->spa_root != NULL)
233 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
236 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
237 if (dp->scd_path == NULL) {
238 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
239 "none", 0, ZPROP_SRC_LOCAL);
240 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
241 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
242 dp->scd_path, 0, ZPROP_SRC_LOCAL);
248 * Get zpool property values.
251 spa_prop_get(spa_t *spa, nvlist_t **nvp)
253 objset_t *mos = spa->spa_meta_objset;
258 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
260 mutex_enter(&spa->spa_props_lock);
263 * Get properties from the spa config.
265 spa_prop_get_config(spa, nvp);
267 /* If no pool property object, no more prop to get. */
268 if (mos == NULL || spa->spa_pool_props_object == 0) {
269 mutex_exit(&spa->spa_props_lock);
274 * Get properties from the MOS pool property object.
276 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
277 (err = zap_cursor_retrieve(&zc, &za)) == 0;
278 zap_cursor_advance(&zc)) {
281 zprop_source_t src = ZPROP_SRC_DEFAULT;
284 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
287 switch (za.za_integer_length) {
289 /* integer property */
290 if (za.za_first_integer !=
291 zpool_prop_default_numeric(prop))
292 src = ZPROP_SRC_LOCAL;
294 if (prop == ZPOOL_PROP_BOOTFS) {
296 dsl_dataset_t *ds = NULL;
298 dp = spa_get_dsl(spa);
299 rw_enter(&dp->dp_config_rwlock, RW_READER);
300 if (err = dsl_dataset_hold_obj(dp,
301 za.za_first_integer, FTAG, &ds)) {
302 rw_exit(&dp->dp_config_rwlock);
307 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
309 dsl_dataset_name(ds, strval);
310 dsl_dataset_rele(ds, FTAG);
311 rw_exit(&dp->dp_config_rwlock);
314 intval = za.za_first_integer;
317 spa_prop_add_list(*nvp, prop, strval, intval, src);
321 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
326 /* string property */
327 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
328 err = zap_lookup(mos, spa->spa_pool_props_object,
329 za.za_name, 1, za.za_num_integers, strval);
331 kmem_free(strval, za.za_num_integers);
334 spa_prop_add_list(*nvp, prop, strval, 0, src);
335 kmem_free(strval, za.za_num_integers);
342 zap_cursor_fini(&zc);
343 mutex_exit(&spa->spa_props_lock);
345 if (err && err != ENOENT) {
355 * Validate the given pool properties nvlist and modify the list
356 * for the property values to be set.
359 spa_prop_validate(spa_t *spa, nvlist_t *props)
362 int error = 0, reset_bootfs = 0;
366 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
368 char *propname, *strval;
373 propname = nvpair_name(elem);
375 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
379 case ZPOOL_PROP_VERSION:
380 error = nvpair_value_uint64(elem, &intval);
382 (intval < spa_version(spa) || intval > SPA_VERSION))
386 case ZPOOL_PROP_DELEGATION:
387 case ZPOOL_PROP_AUTOREPLACE:
388 case ZPOOL_PROP_LISTSNAPS:
389 case ZPOOL_PROP_AUTOEXPAND:
390 error = nvpair_value_uint64(elem, &intval);
391 if (!error && intval > 1)
395 case ZPOOL_PROP_BOOTFS:
397 * If the pool version is less than SPA_VERSION_BOOTFS,
398 * or the pool is still being created (version == 0),
399 * the bootfs property cannot be set.
401 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
407 * Make sure the vdev config is bootable
409 if (!vdev_is_bootable(spa->spa_root_vdev)) {
416 error = nvpair_value_string(elem, &strval);
421 if (strval == NULL || strval[0] == '\0') {
422 objnum = zpool_prop_default_numeric(
427 if (error = dmu_objset_hold(strval, FTAG, &os))
430 /* Must be ZPL and not gzip compressed. */
432 if (dmu_objset_type(os) != DMU_OST_ZFS) {
434 } else if ((error = dsl_prop_get_integer(strval,
435 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
436 &compress, NULL)) == 0 &&
437 !BOOTFS_COMPRESS_VALID(compress)) {
440 objnum = dmu_objset_id(os);
442 dmu_objset_rele(os, FTAG);
446 case ZPOOL_PROP_FAILUREMODE:
447 error = nvpair_value_uint64(elem, &intval);
448 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
449 intval > ZIO_FAILURE_MODE_PANIC))
453 * This is a special case which only occurs when
454 * the pool has completely failed. This allows
455 * the user to change the in-core failmode property
456 * without syncing it out to disk (I/Os might
457 * currently be blocked). We do this by returning
458 * EIO to the caller (spa_prop_set) to trick it
459 * into thinking we encountered a property validation
462 if (!error && spa_suspended(spa)) {
463 spa->spa_failmode = intval;
468 case ZPOOL_PROP_CACHEFILE:
469 if ((error = nvpair_value_string(elem, &strval)) != 0)
472 if (strval[0] == '\0')
475 if (strcmp(strval, "none") == 0)
478 if (strval[0] != '/') {
483 slash = strrchr(strval, '/');
484 ASSERT(slash != NULL);
486 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
487 strcmp(slash, "/..") == 0)
491 case ZPOOL_PROP_COMMENT:
492 if ((error = nvpair_value_string(elem, &strval)) != 0)
494 for (check = strval; *check != '\0'; check++) {
496 * The kernel doesn't have an easy isprint()
497 * check. For this kernel check, we merely
498 * check ASCII apart from DEL. Fix this if
499 * there is an easy-to-use kernel isprint().
501 if (*check >= 0x7f) {
507 if (strlen(strval) > ZPROP_MAX_COMMENT)
511 case ZPOOL_PROP_DEDUPDITTO:
512 if (spa_version(spa) < SPA_VERSION_DEDUP)
515 error = nvpair_value_uint64(elem, &intval);
517 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
526 if (!error && reset_bootfs) {
527 error = nvlist_remove(props,
528 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
531 error = nvlist_add_uint64(props,
532 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
540 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
543 spa_config_dirent_t *dp;
545 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
549 dp = kmem_alloc(sizeof (spa_config_dirent_t),
552 if (cachefile[0] == '\0')
553 dp->scd_path = spa_strdup(spa_config_path);
554 else if (strcmp(cachefile, "none") == 0)
557 dp->scd_path = spa_strdup(cachefile);
559 list_insert_head(&spa->spa_config_list, dp);
561 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
565 spa_prop_set(spa_t *spa, nvlist_t *nvp)
569 boolean_t need_sync = B_FALSE;
572 if ((error = spa_prop_validate(spa, nvp)) != 0)
576 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
577 if ((prop = zpool_name_to_prop(
578 nvpair_name(elem))) == ZPROP_INVAL)
581 if (prop == ZPOOL_PROP_CACHEFILE ||
582 prop == ZPOOL_PROP_ALTROOT ||
583 prop == ZPOOL_PROP_READONLY)
591 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
598 * If the bootfs property value is dsobj, clear it.
601 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
603 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
604 VERIFY(zap_remove(spa->spa_meta_objset,
605 spa->spa_pool_props_object,
606 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
612 * Change the GUID for the pool. This is done so that we can later
613 * re-import a pool built from a clone of our own vdevs. We will modify
614 * the root vdev's guid, our own pool guid, and then mark all of our
615 * vdevs dirty. Note that we must make sure that all our vdevs are
616 * online when we do this, or else any vdevs that weren't present
617 * would be orphaned from our pool. We are also going to issue a
618 * sysevent to update any watchers.
621 spa_change_guid(spa_t *spa)
623 uint64_t oldguid, newguid;
626 if (!(spa_mode_global & FWRITE))
629 txg = spa_vdev_enter(spa);
631 if (spa->spa_root_vdev->vdev_state != VDEV_STATE_HEALTHY)
632 return (spa_vdev_exit(spa, NULL, txg, ENXIO));
634 oldguid = spa_guid(spa);
635 newguid = spa_generate_guid(NULL);
636 ASSERT3U(oldguid, !=, newguid);
638 spa->spa_root_vdev->vdev_guid = newguid;
639 spa->spa_root_vdev->vdev_guid_sum += (newguid - oldguid);
641 vdev_config_dirty(spa->spa_root_vdev);
643 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
645 return (spa_vdev_exit(spa, NULL, txg, 0));
649 * ==========================================================================
650 * SPA state manipulation (open/create/destroy/import/export)
651 * ==========================================================================
655 spa_error_entry_compare(const void *a, const void *b)
657 spa_error_entry_t *sa = (spa_error_entry_t *)a;
658 spa_error_entry_t *sb = (spa_error_entry_t *)b;
661 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
662 sizeof (zbookmark_t));
673 * Utility function which retrieves copies of the current logs and
674 * re-initializes them in the process.
677 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
679 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
681 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
682 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
684 avl_create(&spa->spa_errlist_scrub,
685 spa_error_entry_compare, sizeof (spa_error_entry_t),
686 offsetof(spa_error_entry_t, se_avl));
687 avl_create(&spa->spa_errlist_last,
688 spa_error_entry_compare, sizeof (spa_error_entry_t),
689 offsetof(spa_error_entry_t, se_avl));
693 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
696 uint_t flags = TASKQ_PREPOPULATE;
697 boolean_t batch = B_FALSE;
701 return (NULL); /* no taskq needed */
704 ASSERT3U(value, >=, 1);
705 value = MAX(value, 1);
710 flags |= TASKQ_THREADS_CPU_PCT;
711 value = zio_taskq_batch_pct;
714 case zti_mode_online_percent:
715 flags |= TASKQ_THREADS_CPU_PCT;
719 panic("unrecognized mode for %s taskq (%u:%u) in "
726 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
728 flags |= TASKQ_DC_BATCH;
730 return (taskq_create_sysdc(name, value, 50, INT_MAX,
731 spa->spa_proc, zio_taskq_basedc, flags));
734 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
735 spa->spa_proc, flags));
739 spa_create_zio_taskqs(spa_t *spa)
741 for (int t = 0; t < ZIO_TYPES; t++) {
742 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
743 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
744 enum zti_modes mode = ztip->zti_mode;
745 uint_t value = ztip->zti_value;
748 (void) snprintf(name, sizeof (name),
749 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
751 spa->spa_zio_taskq[t][q] =
752 spa_taskq_create(spa, name, mode, value);
760 spa_thread(void *arg)
765 user_t *pu = PTOU(curproc);
767 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
770 ASSERT(curproc != &p0);
771 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
772 "zpool-%s", spa->spa_name);
773 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
776 /* bind this thread to the requested psrset */
777 if (zio_taskq_psrset_bind != PS_NONE) {
779 mutex_enter(&cpu_lock);
780 mutex_enter(&pidlock);
781 mutex_enter(&curproc->p_lock);
783 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
784 0, NULL, NULL) == 0) {
785 curthread->t_bind_pset = zio_taskq_psrset_bind;
788 "Couldn't bind process for zfs pool \"%s\" to "
789 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
792 mutex_exit(&curproc->p_lock);
793 mutex_exit(&pidlock);
794 mutex_exit(&cpu_lock);
800 if (zio_taskq_sysdc) {
801 sysdc_thread_enter(curthread, 100, 0);
805 spa->spa_proc = curproc;
806 spa->spa_did = curthread->t_did;
808 spa_create_zio_taskqs(spa);
810 mutex_enter(&spa->spa_proc_lock);
811 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
813 spa->spa_proc_state = SPA_PROC_ACTIVE;
814 cv_broadcast(&spa->spa_proc_cv);
816 CALLB_CPR_SAFE_BEGIN(&cprinfo);
817 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
818 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
819 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
821 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
822 spa->spa_proc_state = SPA_PROC_GONE;
824 cv_broadcast(&spa->spa_proc_cv);
825 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
827 mutex_enter(&curproc->p_lock);
830 #endif /* SPA_PROCESS */
834 * Activate an uninitialized pool.
837 spa_activate(spa_t *spa, int mode)
839 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
841 spa->spa_state = POOL_STATE_ACTIVE;
842 spa->spa_mode = mode;
844 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
845 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
847 /* Try to create a covering process */
848 mutex_enter(&spa->spa_proc_lock);
849 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
850 ASSERT(spa->spa_proc == &p0);
854 /* Only create a process if we're going to be around a while. */
855 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
856 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
858 spa->spa_proc_state = SPA_PROC_CREATED;
859 while (spa->spa_proc_state == SPA_PROC_CREATED) {
860 cv_wait(&spa->spa_proc_cv,
861 &spa->spa_proc_lock);
863 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
864 ASSERT(spa->spa_proc != &p0);
865 ASSERT(spa->spa_did != 0);
869 "Couldn't create process for zfs pool \"%s\"\n",
874 #endif /* SPA_PROCESS */
875 mutex_exit(&spa->spa_proc_lock);
877 /* If we didn't create a process, we need to create our taskqs. */
878 ASSERT(spa->spa_proc == &p0);
879 if (spa->spa_proc == &p0) {
880 spa_create_zio_taskqs(spa);
883 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
884 offsetof(vdev_t, vdev_config_dirty_node));
885 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
886 offsetof(vdev_t, vdev_state_dirty_node));
888 txg_list_create(&spa->spa_vdev_txg_list,
889 offsetof(struct vdev, vdev_txg_node));
891 avl_create(&spa->spa_errlist_scrub,
892 spa_error_entry_compare, sizeof (spa_error_entry_t),
893 offsetof(spa_error_entry_t, se_avl));
894 avl_create(&spa->spa_errlist_last,
895 spa_error_entry_compare, sizeof (spa_error_entry_t),
896 offsetof(spa_error_entry_t, se_avl));
900 * Opposite of spa_activate().
903 spa_deactivate(spa_t *spa)
905 ASSERT(spa->spa_sync_on == B_FALSE);
906 ASSERT(spa->spa_dsl_pool == NULL);
907 ASSERT(spa->spa_root_vdev == NULL);
908 ASSERT(spa->spa_async_zio_root == NULL);
909 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
911 txg_list_destroy(&spa->spa_vdev_txg_list);
913 list_destroy(&spa->spa_config_dirty_list);
914 list_destroy(&spa->spa_state_dirty_list);
916 for (int t = 0; t < ZIO_TYPES; t++) {
917 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
918 if (spa->spa_zio_taskq[t][q] != NULL)
919 taskq_destroy(spa->spa_zio_taskq[t][q]);
920 spa->spa_zio_taskq[t][q] = NULL;
924 metaslab_class_destroy(spa->spa_normal_class);
925 spa->spa_normal_class = NULL;
927 metaslab_class_destroy(spa->spa_log_class);
928 spa->spa_log_class = NULL;
931 * If this was part of an import or the open otherwise failed, we may
932 * still have errors left in the queues. Empty them just in case.
934 spa_errlog_drain(spa);
936 avl_destroy(&spa->spa_errlist_scrub);
937 avl_destroy(&spa->spa_errlist_last);
939 spa->spa_state = POOL_STATE_UNINITIALIZED;
941 mutex_enter(&spa->spa_proc_lock);
942 if (spa->spa_proc_state != SPA_PROC_NONE) {
943 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
944 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
945 cv_broadcast(&spa->spa_proc_cv);
946 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
947 ASSERT(spa->spa_proc != &p0);
948 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
950 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
951 spa->spa_proc_state = SPA_PROC_NONE;
953 ASSERT(spa->spa_proc == &p0);
954 mutex_exit(&spa->spa_proc_lock);
958 * We want to make sure spa_thread() has actually exited the ZFS
959 * module, so that the module can't be unloaded out from underneath
962 if (spa->spa_did != 0) {
963 thread_join(spa->spa_did);
966 #endif /* SPA_PROCESS */
970 * Verify a pool configuration, and construct the vdev tree appropriately. This
971 * will create all the necessary vdevs in the appropriate layout, with each vdev
972 * in the CLOSED state. This will prep the pool before open/creation/import.
973 * All vdev validation is done by the vdev_alloc() routine.
976 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
977 uint_t id, int atype)
983 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
986 if ((*vdp)->vdev_ops->vdev_op_leaf)
989 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1001 for (int c = 0; c < children; c++) {
1003 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1011 ASSERT(*vdp != NULL);
1017 * Opposite of spa_load().
1020 spa_unload(spa_t *spa)
1024 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1029 spa_async_suspend(spa);
1034 if (spa->spa_sync_on) {
1035 txg_sync_stop(spa->spa_dsl_pool);
1036 spa->spa_sync_on = B_FALSE;
1040 * Wait for any outstanding async I/O to complete.
1042 if (spa->spa_async_zio_root != NULL) {
1043 (void) zio_wait(spa->spa_async_zio_root);
1044 spa->spa_async_zio_root = NULL;
1047 bpobj_close(&spa->spa_deferred_bpobj);
1050 * Close the dsl pool.
1052 if (spa->spa_dsl_pool) {
1053 dsl_pool_close(spa->spa_dsl_pool);
1054 spa->spa_dsl_pool = NULL;
1055 spa->spa_meta_objset = NULL;
1060 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1063 * Drop and purge level 2 cache
1065 spa_l2cache_drop(spa);
1070 if (spa->spa_root_vdev)
1071 vdev_free(spa->spa_root_vdev);
1072 ASSERT(spa->spa_root_vdev == NULL);
1074 for (i = 0; i < spa->spa_spares.sav_count; i++)
1075 vdev_free(spa->spa_spares.sav_vdevs[i]);
1076 if (spa->spa_spares.sav_vdevs) {
1077 kmem_free(spa->spa_spares.sav_vdevs,
1078 spa->spa_spares.sav_count * sizeof (void *));
1079 spa->spa_spares.sav_vdevs = NULL;
1081 if (spa->spa_spares.sav_config) {
1082 nvlist_free(spa->spa_spares.sav_config);
1083 spa->spa_spares.sav_config = NULL;
1085 spa->spa_spares.sav_count = 0;
1087 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1088 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1089 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1091 if (spa->spa_l2cache.sav_vdevs) {
1092 kmem_free(spa->spa_l2cache.sav_vdevs,
1093 spa->spa_l2cache.sav_count * sizeof (void *));
1094 spa->spa_l2cache.sav_vdevs = NULL;
1096 if (spa->spa_l2cache.sav_config) {
1097 nvlist_free(spa->spa_l2cache.sav_config);
1098 spa->spa_l2cache.sav_config = NULL;
1100 spa->spa_l2cache.sav_count = 0;
1102 spa->spa_async_suspended = 0;
1104 if (spa->spa_comment != NULL) {
1105 spa_strfree(spa->spa_comment);
1106 spa->spa_comment = NULL;
1109 spa_config_exit(spa, SCL_ALL, FTAG);
1113 * Load (or re-load) the current list of vdevs describing the active spares for
1114 * this pool. When this is called, we have some form of basic information in
1115 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1116 * then re-generate a more complete list including status information.
1119 spa_load_spares(spa_t *spa)
1126 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1129 * First, close and free any existing spare vdevs.
1131 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1132 vd = spa->spa_spares.sav_vdevs[i];
1134 /* Undo the call to spa_activate() below */
1135 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1136 B_FALSE)) != NULL && tvd->vdev_isspare)
1137 spa_spare_remove(tvd);
1142 if (spa->spa_spares.sav_vdevs)
1143 kmem_free(spa->spa_spares.sav_vdevs,
1144 spa->spa_spares.sav_count * sizeof (void *));
1146 if (spa->spa_spares.sav_config == NULL)
1149 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1150 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1152 spa->spa_spares.sav_count = (int)nspares;
1153 spa->spa_spares.sav_vdevs = NULL;
1159 * Construct the array of vdevs, opening them to get status in the
1160 * process. For each spare, there is potentially two different vdev_t
1161 * structures associated with it: one in the list of spares (used only
1162 * for basic validation purposes) and one in the active vdev
1163 * configuration (if it's spared in). During this phase we open and
1164 * validate each vdev on the spare list. If the vdev also exists in the
1165 * active configuration, then we also mark this vdev as an active spare.
1167 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1169 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1170 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1171 VDEV_ALLOC_SPARE) == 0);
1174 spa->spa_spares.sav_vdevs[i] = vd;
1176 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1177 B_FALSE)) != NULL) {
1178 if (!tvd->vdev_isspare)
1182 * We only mark the spare active if we were successfully
1183 * able to load the vdev. Otherwise, importing a pool
1184 * with a bad active spare would result in strange
1185 * behavior, because multiple pool would think the spare
1186 * is actively in use.
1188 * There is a vulnerability here to an equally bizarre
1189 * circumstance, where a dead active spare is later
1190 * brought back to life (onlined or otherwise). Given
1191 * the rarity of this scenario, and the extra complexity
1192 * it adds, we ignore the possibility.
1194 if (!vdev_is_dead(tvd))
1195 spa_spare_activate(tvd);
1199 vd->vdev_aux = &spa->spa_spares;
1201 if (vdev_open(vd) != 0)
1204 if (vdev_validate_aux(vd) == 0)
1209 * Recompute the stashed list of spares, with status information
1212 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1213 DATA_TYPE_NVLIST_ARRAY) == 0);
1215 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1217 for (i = 0; i < spa->spa_spares.sav_count; i++)
1218 spares[i] = vdev_config_generate(spa,
1219 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1220 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1221 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1222 for (i = 0; i < spa->spa_spares.sav_count; i++)
1223 nvlist_free(spares[i]);
1224 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1228 * Load (or re-load) the current list of vdevs describing the active l2cache for
1229 * this pool. When this is called, we have some form of basic information in
1230 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1231 * then re-generate a more complete list including status information.
1232 * Devices which are already active have their details maintained, and are
1236 spa_load_l2cache(spa_t *spa)
1240 int i, j, oldnvdevs;
1242 vdev_t *vd, **oldvdevs, **newvdevs;
1243 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1245 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1247 if (sav->sav_config != NULL) {
1248 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1249 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1250 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1255 oldvdevs = sav->sav_vdevs;
1256 oldnvdevs = sav->sav_count;
1257 sav->sav_vdevs = NULL;
1261 * Process new nvlist of vdevs.
1263 for (i = 0; i < nl2cache; i++) {
1264 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1268 for (j = 0; j < oldnvdevs; j++) {
1270 if (vd != NULL && guid == vd->vdev_guid) {
1272 * Retain previous vdev for add/remove ops.
1280 if (newvdevs[i] == NULL) {
1284 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1285 VDEV_ALLOC_L2CACHE) == 0);
1290 * Commit this vdev as an l2cache device,
1291 * even if it fails to open.
1293 spa_l2cache_add(vd);
1298 spa_l2cache_activate(vd);
1300 if (vdev_open(vd) != 0)
1303 (void) vdev_validate_aux(vd);
1305 if (!vdev_is_dead(vd))
1306 l2arc_add_vdev(spa, vd);
1311 * Purge vdevs that were dropped
1313 for (i = 0; i < oldnvdevs; i++) {
1318 ASSERT(vd->vdev_isl2cache);
1320 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1321 pool != 0ULL && l2arc_vdev_present(vd))
1322 l2arc_remove_vdev(vd);
1323 vdev_clear_stats(vd);
1329 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1331 if (sav->sav_config == NULL)
1334 sav->sav_vdevs = newvdevs;
1335 sav->sav_count = (int)nl2cache;
1338 * Recompute the stashed list of l2cache devices, with status
1339 * information this time.
1341 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1342 DATA_TYPE_NVLIST_ARRAY) == 0);
1344 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1345 for (i = 0; i < sav->sav_count; i++)
1346 l2cache[i] = vdev_config_generate(spa,
1347 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1348 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1349 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1351 for (i = 0; i < sav->sav_count; i++)
1352 nvlist_free(l2cache[i]);
1354 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1358 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1361 char *packed = NULL;
1366 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1367 nvsize = *(uint64_t *)db->db_data;
1368 dmu_buf_rele(db, FTAG);
1370 packed = kmem_alloc(nvsize, KM_SLEEP);
1371 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1374 error = nvlist_unpack(packed, nvsize, value, 0);
1375 kmem_free(packed, nvsize);
1381 * Checks to see if the given vdev could not be opened, in which case we post a
1382 * sysevent to notify the autoreplace code that the device has been removed.
1385 spa_check_removed(vdev_t *vd)
1387 for (int c = 0; c < vd->vdev_children; c++)
1388 spa_check_removed(vd->vdev_child[c]);
1390 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1391 zfs_post_autoreplace(vd->vdev_spa, vd);
1392 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1397 * Validate the current config against the MOS config
1400 spa_config_valid(spa_t *spa, nvlist_t *config)
1402 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1405 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1407 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1408 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1410 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1413 * If we're doing a normal import, then build up any additional
1414 * diagnostic information about missing devices in this config.
1415 * We'll pass this up to the user for further processing.
1417 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1418 nvlist_t **child, *nv;
1421 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1423 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1425 for (int c = 0; c < rvd->vdev_children; c++) {
1426 vdev_t *tvd = rvd->vdev_child[c];
1427 vdev_t *mtvd = mrvd->vdev_child[c];
1429 if (tvd->vdev_ops == &vdev_missing_ops &&
1430 mtvd->vdev_ops != &vdev_missing_ops &&
1432 child[idx++] = vdev_config_generate(spa, mtvd,
1437 VERIFY(nvlist_add_nvlist_array(nv,
1438 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1439 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1440 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1442 for (int i = 0; i < idx; i++)
1443 nvlist_free(child[i]);
1446 kmem_free(child, rvd->vdev_children * sizeof (char **));
1450 * Compare the root vdev tree with the information we have
1451 * from the MOS config (mrvd). Check each top-level vdev
1452 * with the corresponding MOS config top-level (mtvd).
1454 for (int c = 0; c < rvd->vdev_children; c++) {
1455 vdev_t *tvd = rvd->vdev_child[c];
1456 vdev_t *mtvd = mrvd->vdev_child[c];
1459 * Resolve any "missing" vdevs in the current configuration.
1460 * If we find that the MOS config has more accurate information
1461 * about the top-level vdev then use that vdev instead.
1463 if (tvd->vdev_ops == &vdev_missing_ops &&
1464 mtvd->vdev_ops != &vdev_missing_ops) {
1466 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1470 * Device specific actions.
1472 if (mtvd->vdev_islog) {
1473 spa_set_log_state(spa, SPA_LOG_CLEAR);
1476 * XXX - once we have 'readonly' pool
1477 * support we should be able to handle
1478 * missing data devices by transitioning
1479 * the pool to readonly.
1485 * Swap the missing vdev with the data we were
1486 * able to obtain from the MOS config.
1488 vdev_remove_child(rvd, tvd);
1489 vdev_remove_child(mrvd, mtvd);
1491 vdev_add_child(rvd, mtvd);
1492 vdev_add_child(mrvd, tvd);
1494 spa_config_exit(spa, SCL_ALL, FTAG);
1496 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1499 } else if (mtvd->vdev_islog) {
1501 * Load the slog device's state from the MOS config
1502 * since it's possible that the label does not
1503 * contain the most up-to-date information.
1505 vdev_load_log_state(tvd, mtvd);
1510 spa_config_exit(spa, SCL_ALL, FTAG);
1513 * Ensure we were able to validate the config.
1515 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1519 * Check for missing log devices
1522 spa_check_logs(spa_t *spa)
1524 switch (spa->spa_log_state) {
1525 case SPA_LOG_MISSING:
1526 /* need to recheck in case slog has been restored */
1527 case SPA_LOG_UNKNOWN:
1528 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1529 DS_FIND_CHILDREN)) {
1530 spa_set_log_state(spa, SPA_LOG_MISSING);
1539 spa_passivate_log(spa_t *spa)
1541 vdev_t *rvd = spa->spa_root_vdev;
1542 boolean_t slog_found = B_FALSE;
1544 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1546 if (!spa_has_slogs(spa))
1549 for (int c = 0; c < rvd->vdev_children; c++) {
1550 vdev_t *tvd = rvd->vdev_child[c];
1551 metaslab_group_t *mg = tvd->vdev_mg;
1553 if (tvd->vdev_islog) {
1554 metaslab_group_passivate(mg);
1555 slog_found = B_TRUE;
1559 return (slog_found);
1563 spa_activate_log(spa_t *spa)
1565 vdev_t *rvd = spa->spa_root_vdev;
1567 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1569 for (int c = 0; c < rvd->vdev_children; c++) {
1570 vdev_t *tvd = rvd->vdev_child[c];
1571 metaslab_group_t *mg = tvd->vdev_mg;
1573 if (tvd->vdev_islog)
1574 metaslab_group_activate(mg);
1579 spa_offline_log(spa_t *spa)
1583 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1584 NULL, DS_FIND_CHILDREN)) == 0) {
1587 * We successfully offlined the log device, sync out the
1588 * current txg so that the "stubby" block can be removed
1591 txg_wait_synced(spa->spa_dsl_pool, 0);
1597 spa_aux_check_removed(spa_aux_vdev_t *sav)
1601 for (i = 0; i < sav->sav_count; i++)
1602 spa_check_removed(sav->sav_vdevs[i]);
1606 spa_claim_notify(zio_t *zio)
1608 spa_t *spa = zio->io_spa;
1613 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1614 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1615 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1616 mutex_exit(&spa->spa_props_lock);
1619 typedef struct spa_load_error {
1620 uint64_t sle_meta_count;
1621 uint64_t sle_data_count;
1625 spa_load_verify_done(zio_t *zio)
1627 blkptr_t *bp = zio->io_bp;
1628 spa_load_error_t *sle = zio->io_private;
1629 dmu_object_type_t type = BP_GET_TYPE(bp);
1630 int error = zio->io_error;
1633 if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
1634 type != DMU_OT_INTENT_LOG)
1635 atomic_add_64(&sle->sle_meta_count, 1);
1637 atomic_add_64(&sle->sle_data_count, 1);
1639 zio_data_buf_free(zio->io_data, zio->io_size);
1644 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1645 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1649 size_t size = BP_GET_PSIZE(bp);
1650 void *data = zio_data_buf_alloc(size);
1652 zio_nowait(zio_read(rio, spa, bp, data, size,
1653 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1654 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1655 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1661 spa_load_verify(spa_t *spa)
1664 spa_load_error_t sle = { 0 };
1665 zpool_rewind_policy_t policy;
1666 boolean_t verify_ok = B_FALSE;
1669 zpool_get_rewind_policy(spa->spa_config, &policy);
1671 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1674 rio = zio_root(spa, NULL, &sle,
1675 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1677 error = traverse_pool(spa, spa->spa_verify_min_txg,
1678 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1680 (void) zio_wait(rio);
1682 spa->spa_load_meta_errors = sle.sle_meta_count;
1683 spa->spa_load_data_errors = sle.sle_data_count;
1685 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1686 sle.sle_data_count <= policy.zrp_maxdata) {
1690 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1691 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1693 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1694 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1695 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1696 VERIFY(nvlist_add_int64(spa->spa_load_info,
1697 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1698 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1699 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1701 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1705 if (error != ENXIO && error != EIO)
1710 return (verify_ok ? 0 : EIO);
1714 * Find a value in the pool props object.
1717 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1719 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1720 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1724 * Find a value in the pool directory object.
1727 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1729 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1730 name, sizeof (uint64_t), 1, val));
1734 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1736 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1741 * Fix up config after a partly-completed split. This is done with the
1742 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1743 * pool have that entry in their config, but only the splitting one contains
1744 * a list of all the guids of the vdevs that are being split off.
1746 * This function determines what to do with that list: either rejoin
1747 * all the disks to the pool, or complete the splitting process. To attempt
1748 * the rejoin, each disk that is offlined is marked online again, and
1749 * we do a reopen() call. If the vdev label for every disk that was
1750 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1751 * then we call vdev_split() on each disk, and complete the split.
1753 * Otherwise we leave the config alone, with all the vdevs in place in
1754 * the original pool.
1757 spa_try_repair(spa_t *spa, nvlist_t *config)
1764 boolean_t attempt_reopen;
1766 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1769 /* check that the config is complete */
1770 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1771 &glist, &gcount) != 0)
1774 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1776 /* attempt to online all the vdevs & validate */
1777 attempt_reopen = B_TRUE;
1778 for (i = 0; i < gcount; i++) {
1779 if (glist[i] == 0) /* vdev is hole */
1782 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1783 if (vd[i] == NULL) {
1785 * Don't bother attempting to reopen the disks;
1786 * just do the split.
1788 attempt_reopen = B_FALSE;
1790 /* attempt to re-online it */
1791 vd[i]->vdev_offline = B_FALSE;
1795 if (attempt_reopen) {
1796 vdev_reopen(spa->spa_root_vdev);
1798 /* check each device to see what state it's in */
1799 for (extracted = 0, i = 0; i < gcount; i++) {
1800 if (vd[i] != NULL &&
1801 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1808 * If every disk has been moved to the new pool, or if we never
1809 * even attempted to look at them, then we split them off for
1812 if (!attempt_reopen || gcount == extracted) {
1813 for (i = 0; i < gcount; i++)
1816 vdev_reopen(spa->spa_root_vdev);
1819 kmem_free(vd, gcount * sizeof (vdev_t *));
1823 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1824 boolean_t mosconfig)
1826 nvlist_t *config = spa->spa_config;
1827 char *ereport = FM_EREPORT_ZFS_POOL;
1833 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1836 ASSERT(spa->spa_comment == NULL);
1837 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1838 spa->spa_comment = spa_strdup(comment);
1841 * Versioning wasn't explicitly added to the label until later, so if
1842 * it's not present treat it as the initial version.
1844 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1845 &spa->spa_ubsync.ub_version) != 0)
1846 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1848 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1849 &spa->spa_config_txg);
1851 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1852 spa_guid_exists(pool_guid, 0)) {
1855 spa->spa_config_guid = pool_guid;
1857 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1859 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1863 gethrestime(&spa->spa_loaded_ts);
1864 error = spa_load_impl(spa, pool_guid, config, state, type,
1865 mosconfig, &ereport);
1868 spa->spa_minref = refcount_count(&spa->spa_refcount);
1870 if (error != EEXIST) {
1871 spa->spa_loaded_ts.tv_sec = 0;
1872 spa->spa_loaded_ts.tv_nsec = 0;
1874 if (error != EBADF) {
1875 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1878 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
1885 * Load an existing storage pool, using the pool's builtin spa_config as a
1886 * source of configuration information.
1889 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
1890 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1894 nvlist_t *nvroot = NULL;
1896 uberblock_t *ub = &spa->spa_uberblock;
1897 uint64_t children, config_cache_txg = spa->spa_config_txg;
1898 int orig_mode = spa->spa_mode;
1903 * If this is an untrusted config, access the pool in read-only mode.
1904 * This prevents things like resilvering recently removed devices.
1907 spa->spa_mode = FREAD;
1909 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1911 spa->spa_load_state = state;
1913 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
1916 parse = (type == SPA_IMPORT_EXISTING ?
1917 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
1920 * Create "The Godfather" zio to hold all async IOs
1922 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1923 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
1926 * Parse the configuration into a vdev tree. We explicitly set the
1927 * value that will be returned by spa_version() since parsing the
1928 * configuration requires knowing the version number.
1930 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1931 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
1932 spa_config_exit(spa, SCL_ALL, FTAG);
1937 ASSERT(spa->spa_root_vdev == rvd);
1939 if (type != SPA_IMPORT_ASSEMBLE) {
1940 ASSERT(spa_guid(spa) == pool_guid);
1944 * Try to open all vdevs, loading each label in the process.
1946 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1947 error = vdev_open(rvd);
1948 spa_config_exit(spa, SCL_ALL, FTAG);
1953 * We need to validate the vdev labels against the configuration that
1954 * we have in hand, which is dependent on the setting of mosconfig. If
1955 * mosconfig is true then we're validating the vdev labels based on
1956 * that config. Otherwise, we're validating against the cached config
1957 * (zpool.cache) that was read when we loaded the zfs module, and then
1958 * later we will recursively call spa_load() and validate against
1961 * If we're assembling a new pool that's been split off from an
1962 * existing pool, the labels haven't yet been updated so we skip
1963 * validation for now.
1965 if (type != SPA_IMPORT_ASSEMBLE) {
1966 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1967 error = vdev_validate(rvd, mosconfig);
1968 spa_config_exit(spa, SCL_ALL, FTAG);
1973 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
1978 * Find the best uberblock.
1980 vdev_uberblock_load(NULL, rvd, ub);
1983 * If we weren't able to find a single valid uberblock, return failure.
1985 if (ub->ub_txg == 0)
1986 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
1989 * If the pool is newer than the code, we can't open it.
1991 if (ub->ub_version > SPA_VERSION)
1992 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1995 * If the vdev guid sum doesn't match the uberblock, we have an
1996 * incomplete configuration. We first check to see if the pool
1997 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
1998 * If it is, defer the vdev_guid_sum check till later so we
1999 * can handle missing vdevs.
2001 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2002 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2003 rvd->vdev_guid_sum != ub->ub_guid_sum)
2004 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2006 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2007 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2008 spa_try_repair(spa, config);
2009 spa_config_exit(spa, SCL_ALL, FTAG);
2010 nvlist_free(spa->spa_config_splitting);
2011 spa->spa_config_splitting = NULL;
2015 * Initialize internal SPA structures.
2017 spa->spa_state = POOL_STATE_ACTIVE;
2018 spa->spa_ubsync = spa->spa_uberblock;
2019 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2020 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2021 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2022 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2023 spa->spa_claim_max_txg = spa->spa_first_txg;
2024 spa->spa_prev_software_version = ub->ub_software_version;
2026 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2028 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2029 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2031 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2032 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2036 nvlist_t *policy = NULL, *nvconfig;
2038 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2039 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2041 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2042 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2044 unsigned long myhostid = 0;
2046 VERIFY(nvlist_lookup_string(nvconfig,
2047 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2050 myhostid = zone_get_hostid(NULL);
2053 * We're emulating the system's hostid in userland, so
2054 * we can't use zone_get_hostid().
2056 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2057 #endif /* _KERNEL */
2058 if (check_hostid && hostid != 0 && myhostid != 0 &&
2059 hostid != myhostid) {
2060 nvlist_free(nvconfig);
2061 cmn_err(CE_WARN, "pool '%s' could not be "
2062 "loaded as it was last accessed by "
2063 "another system (host: %s hostid: 0x%lx). "
2064 "See: http://illumos.org/msg/ZFS-8000-EY",
2065 spa_name(spa), hostname,
2066 (unsigned long)hostid);
2070 if (nvlist_lookup_nvlist(spa->spa_config,
2071 ZPOOL_REWIND_POLICY, &policy) == 0)
2072 VERIFY(nvlist_add_nvlist(nvconfig,
2073 ZPOOL_REWIND_POLICY, policy) == 0);
2075 spa_config_set(spa, nvconfig);
2077 spa_deactivate(spa);
2078 spa_activate(spa, orig_mode);
2080 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2083 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2084 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2085 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2087 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2090 * Load the bit that tells us to use the new accounting function
2091 * (raid-z deflation). If we have an older pool, this will not
2094 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2095 if (error != 0 && error != ENOENT)
2096 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2098 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2099 &spa->spa_creation_version);
2100 if (error != 0 && error != ENOENT)
2101 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2104 * Load the persistent error log. If we have an older pool, this will
2107 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2108 if (error != 0 && error != ENOENT)
2109 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2111 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2112 &spa->spa_errlog_scrub);
2113 if (error != 0 && error != ENOENT)
2114 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2117 * Load the history object. If we have an older pool, this
2118 * will not be present.
2120 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2121 if (error != 0 && error != ENOENT)
2122 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2125 * If we're assembling the pool from the split-off vdevs of
2126 * an existing pool, we don't want to attach the spares & cache
2131 * Load any hot spares for this pool.
2133 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2134 if (error != 0 && error != ENOENT)
2135 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2136 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2137 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2138 if (load_nvlist(spa, spa->spa_spares.sav_object,
2139 &spa->spa_spares.sav_config) != 0)
2140 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2142 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2143 spa_load_spares(spa);
2144 spa_config_exit(spa, SCL_ALL, FTAG);
2145 } else if (error == 0) {
2146 spa->spa_spares.sav_sync = B_TRUE;
2150 * Load any level 2 ARC devices for this pool.
2152 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2153 &spa->spa_l2cache.sav_object);
2154 if (error != 0 && error != ENOENT)
2155 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2156 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2157 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2158 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2159 &spa->spa_l2cache.sav_config) != 0)
2160 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2162 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2163 spa_load_l2cache(spa);
2164 spa_config_exit(spa, SCL_ALL, FTAG);
2165 } else if (error == 0) {
2166 spa->spa_l2cache.sav_sync = B_TRUE;
2169 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2171 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2172 if (error && error != ENOENT)
2173 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2176 uint64_t autoreplace;
2178 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2179 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2180 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2181 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2182 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2183 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2184 &spa->spa_dedup_ditto);
2186 spa->spa_autoreplace = (autoreplace != 0);
2190 * If the 'autoreplace' property is set, then post a resource notifying
2191 * the ZFS DE that it should not issue any faults for unopenable
2192 * devices. We also iterate over the vdevs, and post a sysevent for any
2193 * unopenable vdevs so that the normal autoreplace handler can take
2196 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2197 spa_check_removed(spa->spa_root_vdev);
2199 * For the import case, this is done in spa_import(), because
2200 * at this point we're using the spare definitions from
2201 * the MOS config, not necessarily from the userland config.
2203 if (state != SPA_LOAD_IMPORT) {
2204 spa_aux_check_removed(&spa->spa_spares);
2205 spa_aux_check_removed(&spa->spa_l2cache);
2210 * Load the vdev state for all toplevel vdevs.
2215 * Propagate the leaf DTLs we just loaded all the way up the tree.
2217 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2218 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2219 spa_config_exit(spa, SCL_ALL, FTAG);
2222 * Load the DDTs (dedup tables).
2224 error = ddt_load(spa);
2226 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2228 spa_update_dspace(spa);
2231 * Validate the config, using the MOS config to fill in any
2232 * information which might be missing. If we fail to validate
2233 * the config then declare the pool unfit for use. If we're
2234 * assembling a pool from a split, the log is not transferred
2237 if (type != SPA_IMPORT_ASSEMBLE) {
2240 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2241 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2243 if (!spa_config_valid(spa, nvconfig)) {
2244 nvlist_free(nvconfig);
2245 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2248 nvlist_free(nvconfig);
2251 * Now that we've validate the config, check the state of the
2252 * root vdev. If it can't be opened, it indicates one or
2253 * more toplevel vdevs are faulted.
2255 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2258 if (spa_check_logs(spa)) {
2259 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2260 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2265 * We've successfully opened the pool, verify that we're ready
2266 * to start pushing transactions.
2268 if (state != SPA_LOAD_TRYIMPORT) {
2269 if (error = spa_load_verify(spa))
2270 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2274 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2275 spa->spa_load_max_txg == UINT64_MAX)) {
2277 int need_update = B_FALSE;
2279 ASSERT(state != SPA_LOAD_TRYIMPORT);
2282 * Claim log blocks that haven't been committed yet.
2283 * This must all happen in a single txg.
2284 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2285 * invoked from zil_claim_log_block()'s i/o done callback.
2286 * Price of rollback is that we abandon the log.
2288 spa->spa_claiming = B_TRUE;
2290 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2291 spa_first_txg(spa));
2292 (void) dmu_objset_find(spa_name(spa),
2293 zil_claim, tx, DS_FIND_CHILDREN);
2296 spa->spa_claiming = B_FALSE;
2298 spa_set_log_state(spa, SPA_LOG_GOOD);
2299 spa->spa_sync_on = B_TRUE;
2300 txg_sync_start(spa->spa_dsl_pool);
2303 * Wait for all claims to sync. We sync up to the highest
2304 * claimed log block birth time so that claimed log blocks
2305 * don't appear to be from the future. spa_claim_max_txg
2306 * will have been set for us by either zil_check_log_chain()
2307 * (invoked from spa_check_logs()) or zil_claim() above.
2309 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2312 * If the config cache is stale, or we have uninitialized
2313 * metaslabs (see spa_vdev_add()), then update the config.
2315 * If this is a verbatim import, trust the current
2316 * in-core spa_config and update the disk labels.
2318 if (config_cache_txg != spa->spa_config_txg ||
2319 state == SPA_LOAD_IMPORT ||
2320 state == SPA_LOAD_RECOVER ||
2321 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2322 need_update = B_TRUE;
2324 for (int c = 0; c < rvd->vdev_children; c++)
2325 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2326 need_update = B_TRUE;
2329 * Update the config cache asychronously in case we're the
2330 * root pool, in which case the config cache isn't writable yet.
2333 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2336 * Check all DTLs to see if anything needs resilvering.
2338 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2339 vdev_resilver_needed(rvd, NULL, NULL))
2340 spa_async_request(spa, SPA_ASYNC_RESILVER);
2343 * Delete any inconsistent datasets.
2345 (void) dmu_objset_find(spa_name(spa),
2346 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2349 * Clean up any stale temporary dataset userrefs.
2351 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2358 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2360 int mode = spa->spa_mode;
2363 spa_deactivate(spa);
2365 spa->spa_load_max_txg--;
2367 spa_activate(spa, mode);
2368 spa_async_suspend(spa);
2370 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2374 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2375 uint64_t max_request, int rewind_flags)
2377 nvlist_t *config = NULL;
2378 int load_error, rewind_error;
2379 uint64_t safe_rewind_txg;
2382 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2383 spa->spa_load_max_txg = spa->spa_load_txg;
2384 spa_set_log_state(spa, SPA_LOG_CLEAR);
2386 spa->spa_load_max_txg = max_request;
2389 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2391 if (load_error == 0)
2394 if (spa->spa_root_vdev != NULL)
2395 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2397 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2398 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2400 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2401 nvlist_free(config);
2402 return (load_error);
2405 /* Price of rolling back is discarding txgs, including log */
2406 if (state == SPA_LOAD_RECOVER)
2407 spa_set_log_state(spa, SPA_LOG_CLEAR);
2409 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2410 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2411 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2412 TXG_INITIAL : safe_rewind_txg;
2415 * Continue as long as we're finding errors, we're still within
2416 * the acceptable rewind range, and we're still finding uberblocks
2418 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2419 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2420 if (spa->spa_load_max_txg < safe_rewind_txg)
2421 spa->spa_extreme_rewind = B_TRUE;
2422 rewind_error = spa_load_retry(spa, state, mosconfig);
2425 spa->spa_extreme_rewind = B_FALSE;
2426 spa->spa_load_max_txg = UINT64_MAX;
2428 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2429 spa_config_set(spa, config);
2431 return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
2437 * The import case is identical to an open except that the configuration is sent
2438 * down from userland, instead of grabbed from the configuration cache. For the
2439 * case of an open, the pool configuration will exist in the
2440 * POOL_STATE_UNINITIALIZED state.
2442 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2443 * the same time open the pool, without having to keep around the spa_t in some
2447 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2451 spa_load_state_t state = SPA_LOAD_OPEN;
2453 int locked = B_FALSE;
2454 int firstopen = B_FALSE;
2459 * As disgusting as this is, we need to support recursive calls to this
2460 * function because dsl_dir_open() is called during spa_load(), and ends
2461 * up calling spa_open() again. The real fix is to figure out how to
2462 * avoid dsl_dir_open() calling this in the first place.
2464 if (mutex_owner(&spa_namespace_lock) != curthread) {
2465 mutex_enter(&spa_namespace_lock);
2469 if ((spa = spa_lookup(pool)) == NULL) {
2471 mutex_exit(&spa_namespace_lock);
2475 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2476 zpool_rewind_policy_t policy;
2480 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2482 if (policy.zrp_request & ZPOOL_DO_REWIND)
2483 state = SPA_LOAD_RECOVER;
2485 spa_activate(spa, spa_mode_global);
2487 if (state != SPA_LOAD_RECOVER)
2488 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2490 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2491 policy.zrp_request);
2493 if (error == EBADF) {
2495 * If vdev_validate() returns failure (indicated by
2496 * EBADF), it indicates that one of the vdevs indicates
2497 * that the pool has been exported or destroyed. If
2498 * this is the case, the config cache is out of sync and
2499 * we should remove the pool from the namespace.
2502 spa_deactivate(spa);
2503 spa_config_sync(spa, B_TRUE, B_TRUE);
2506 mutex_exit(&spa_namespace_lock);
2512 * We can't open the pool, but we still have useful
2513 * information: the state of each vdev after the
2514 * attempted vdev_open(). Return this to the user.
2516 if (config != NULL && spa->spa_config) {
2517 VERIFY(nvlist_dup(spa->spa_config, config,
2519 VERIFY(nvlist_add_nvlist(*config,
2520 ZPOOL_CONFIG_LOAD_INFO,
2521 spa->spa_load_info) == 0);
2524 spa_deactivate(spa);
2525 spa->spa_last_open_failed = error;
2527 mutex_exit(&spa_namespace_lock);
2533 spa_open_ref(spa, tag);
2536 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2539 * If we've recovered the pool, pass back any information we
2540 * gathered while doing the load.
2542 if (state == SPA_LOAD_RECOVER) {
2543 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2544 spa->spa_load_info) == 0);
2548 spa->spa_last_open_failed = 0;
2549 spa->spa_last_ubsync_txg = 0;
2550 spa->spa_load_txg = 0;
2551 mutex_exit(&spa_namespace_lock);
2555 zvol_create_minors(pool);
2566 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2569 return (spa_open_common(name, spapp, tag, policy, config));
2573 spa_open(const char *name, spa_t **spapp, void *tag)
2575 return (spa_open_common(name, spapp, tag, NULL, NULL));
2579 * Lookup the given spa_t, incrementing the inject count in the process,
2580 * preventing it from being exported or destroyed.
2583 spa_inject_addref(char *name)
2587 mutex_enter(&spa_namespace_lock);
2588 if ((spa = spa_lookup(name)) == NULL) {
2589 mutex_exit(&spa_namespace_lock);
2592 spa->spa_inject_ref++;
2593 mutex_exit(&spa_namespace_lock);
2599 spa_inject_delref(spa_t *spa)
2601 mutex_enter(&spa_namespace_lock);
2602 spa->spa_inject_ref--;
2603 mutex_exit(&spa_namespace_lock);
2607 * Add spares device information to the nvlist.
2610 spa_add_spares(spa_t *spa, nvlist_t *config)
2620 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2622 if (spa->spa_spares.sav_count == 0)
2625 VERIFY(nvlist_lookup_nvlist(config,
2626 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2627 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2628 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2630 VERIFY(nvlist_add_nvlist_array(nvroot,
2631 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2632 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2633 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2636 * Go through and find any spares which have since been
2637 * repurposed as an active spare. If this is the case, update
2638 * their status appropriately.
2640 for (i = 0; i < nspares; i++) {
2641 VERIFY(nvlist_lookup_uint64(spares[i],
2642 ZPOOL_CONFIG_GUID, &guid) == 0);
2643 if (spa_spare_exists(guid, &pool, NULL) &&
2645 VERIFY(nvlist_lookup_uint64_array(
2646 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2647 (uint64_t **)&vs, &vsc) == 0);
2648 vs->vs_state = VDEV_STATE_CANT_OPEN;
2649 vs->vs_aux = VDEV_AUX_SPARED;
2656 * Add l2cache device information to the nvlist, including vdev stats.
2659 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2662 uint_t i, j, nl2cache;
2669 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2671 if (spa->spa_l2cache.sav_count == 0)
2674 VERIFY(nvlist_lookup_nvlist(config,
2675 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2676 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2677 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2678 if (nl2cache != 0) {
2679 VERIFY(nvlist_add_nvlist_array(nvroot,
2680 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2681 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2682 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2685 * Update level 2 cache device stats.
2688 for (i = 0; i < nl2cache; i++) {
2689 VERIFY(nvlist_lookup_uint64(l2cache[i],
2690 ZPOOL_CONFIG_GUID, &guid) == 0);
2693 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2695 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2696 vd = spa->spa_l2cache.sav_vdevs[j];
2702 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2703 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2705 vdev_get_stats(vd, vs);
2711 spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2717 error = spa_open_common(name, &spa, FTAG, NULL, config);
2721 * This still leaves a window of inconsistency where the spares
2722 * or l2cache devices could change and the config would be
2723 * self-inconsistent.
2725 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2727 if (*config != NULL) {
2728 uint64_t loadtimes[2];
2730 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
2731 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
2732 VERIFY(nvlist_add_uint64_array(*config,
2733 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
2735 VERIFY(nvlist_add_uint64(*config,
2736 ZPOOL_CONFIG_ERRCOUNT,
2737 spa_get_errlog_size(spa)) == 0);
2739 if (spa_suspended(spa))
2740 VERIFY(nvlist_add_uint64(*config,
2741 ZPOOL_CONFIG_SUSPENDED,
2742 spa->spa_failmode) == 0);
2744 spa_add_spares(spa, *config);
2745 spa_add_l2cache(spa, *config);
2750 * We want to get the alternate root even for faulted pools, so we cheat
2751 * and call spa_lookup() directly.
2755 mutex_enter(&spa_namespace_lock);
2756 spa = spa_lookup(name);
2758 spa_altroot(spa, altroot, buflen);
2762 mutex_exit(&spa_namespace_lock);
2764 spa_altroot(spa, altroot, buflen);
2769 spa_config_exit(spa, SCL_CONFIG, FTAG);
2770 spa_close(spa, FTAG);
2777 * Validate that the auxiliary device array is well formed. We must have an
2778 * array of nvlists, each which describes a valid leaf vdev. If this is an
2779 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2780 * specified, as long as they are well-formed.
2783 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
2784 spa_aux_vdev_t *sav, const char *config, uint64_t version,
2785 vdev_labeltype_t label)
2792 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2795 * It's acceptable to have no devs specified.
2797 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
2804 * Make sure the pool is formatted with a version that supports this
2807 if (spa_version(spa) < version)
2811 * Set the pending device list so we correctly handle device in-use
2814 sav->sav_pending = dev;
2815 sav->sav_npending = ndev;
2817 for (i = 0; i < ndev; i++) {
2818 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
2822 if (!vd->vdev_ops->vdev_op_leaf) {
2829 * The L2ARC currently only supports disk devices in
2830 * kernel context. For user-level testing, we allow it.
2833 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
2834 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
2842 if ((error = vdev_open(vd)) == 0 &&
2843 (error = vdev_label_init(vd, crtxg, label)) == 0) {
2844 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
2845 vd->vdev_guid) == 0);
2851 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
2858 sav->sav_pending = NULL;
2859 sav->sav_npending = 0;
2864 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
2868 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2870 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2871 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
2872 VDEV_LABEL_SPARE)) != 0) {
2876 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2877 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
2878 VDEV_LABEL_L2CACHE));
2882 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
2887 if (sav->sav_config != NULL) {
2893 * Generate new dev list by concatentating with the
2896 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
2897 &olddevs, &oldndevs) == 0);
2899 newdevs = kmem_alloc(sizeof (void *) *
2900 (ndevs + oldndevs), KM_SLEEP);
2901 for (i = 0; i < oldndevs; i++)
2902 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
2904 for (i = 0; i < ndevs; i++)
2905 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
2908 VERIFY(nvlist_remove(sav->sav_config, config,
2909 DATA_TYPE_NVLIST_ARRAY) == 0);
2911 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2912 config, newdevs, ndevs + oldndevs) == 0);
2913 for (i = 0; i < oldndevs + ndevs; i++)
2914 nvlist_free(newdevs[i]);
2915 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
2918 * Generate a new dev list.
2920 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
2922 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
2928 * Stop and drop level 2 ARC devices
2931 spa_l2cache_drop(spa_t *spa)
2935 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2937 for (i = 0; i < sav->sav_count; i++) {
2940 vd = sav->sav_vdevs[i];
2943 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2944 pool != 0ULL && l2arc_vdev_present(vd))
2945 l2arc_remove_vdev(vd);
2953 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
2954 const char *history_str, nvlist_t *zplprops)
2957 char *altroot = NULL;
2962 uint64_t txg = TXG_INITIAL;
2963 nvlist_t **spares, **l2cache;
2964 uint_t nspares, nl2cache;
2965 uint64_t version, obj;
2968 * If this pool already exists, return failure.
2970 mutex_enter(&spa_namespace_lock);
2971 if (spa_lookup(pool) != NULL) {
2972 mutex_exit(&spa_namespace_lock);
2977 * Allocate a new spa_t structure.
2979 (void) nvlist_lookup_string(props,
2980 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2981 spa = spa_add(pool, NULL, altroot);
2982 spa_activate(spa, spa_mode_global);
2984 if (props && (error = spa_prop_validate(spa, props))) {
2985 spa_deactivate(spa);
2987 mutex_exit(&spa_namespace_lock);
2991 if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
2993 version = SPA_VERSION;
2994 ASSERT(version <= SPA_VERSION);
2996 spa->spa_first_txg = txg;
2997 spa->spa_uberblock.ub_txg = txg - 1;
2998 spa->spa_uberblock.ub_version = version;
2999 spa->spa_ubsync = spa->spa_uberblock;
3002 * Create "The Godfather" zio to hold all async IOs
3004 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3005 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3008 * Create the root vdev.
3010 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3012 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3014 ASSERT(error != 0 || rvd != NULL);
3015 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3017 if (error == 0 && !zfs_allocatable_devs(nvroot))
3021 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3022 (error = spa_validate_aux(spa, nvroot, txg,
3023 VDEV_ALLOC_ADD)) == 0) {
3024 for (int c = 0; c < rvd->vdev_children; c++) {
3025 vdev_metaslab_set_size(rvd->vdev_child[c]);
3026 vdev_expand(rvd->vdev_child[c], txg);
3030 spa_config_exit(spa, SCL_ALL, FTAG);
3034 spa_deactivate(spa);
3036 mutex_exit(&spa_namespace_lock);
3041 * Get the list of spares, if specified.
3043 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3044 &spares, &nspares) == 0) {
3045 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3047 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3048 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3049 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3050 spa_load_spares(spa);
3051 spa_config_exit(spa, SCL_ALL, FTAG);
3052 spa->spa_spares.sav_sync = B_TRUE;
3056 * Get the list of level 2 cache devices, if specified.
3058 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3059 &l2cache, &nl2cache) == 0) {
3060 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3061 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3062 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3063 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3064 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3065 spa_load_l2cache(spa);
3066 spa_config_exit(spa, SCL_ALL, FTAG);
3067 spa->spa_l2cache.sav_sync = B_TRUE;
3070 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3071 spa->spa_meta_objset = dp->dp_meta_objset;
3074 * Create DDTs (dedup tables).
3078 spa_update_dspace(spa);
3080 tx = dmu_tx_create_assigned(dp, txg);
3083 * Create the pool config object.
3085 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3086 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3087 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3089 if (zap_add(spa->spa_meta_objset,
3090 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3091 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3092 cmn_err(CE_PANIC, "failed to add pool config");
3095 if (zap_add(spa->spa_meta_objset,
3096 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3097 sizeof (uint64_t), 1, &version, tx) != 0) {
3098 cmn_err(CE_PANIC, "failed to add pool version");
3101 /* Newly created pools with the right version are always deflated. */
3102 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3103 spa->spa_deflate = TRUE;
3104 if (zap_add(spa->spa_meta_objset,
3105 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3106 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3107 cmn_err(CE_PANIC, "failed to add deflate");
3112 * Create the deferred-free bpobj. Turn off compression
3113 * because sync-to-convergence takes longer if the blocksize
3116 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3117 dmu_object_set_compress(spa->spa_meta_objset, obj,
3118 ZIO_COMPRESS_OFF, tx);
3119 if (zap_add(spa->spa_meta_objset,
3120 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3121 sizeof (uint64_t), 1, &obj, tx) != 0) {
3122 cmn_err(CE_PANIC, "failed to add bpobj");
3124 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3125 spa->spa_meta_objset, obj));
3128 * Create the pool's history object.
3130 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3131 spa_history_create_obj(spa, tx);
3134 * Set pool properties.
3136 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3137 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3138 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3139 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3141 if (props != NULL) {
3142 spa_configfile_set(spa, props, B_FALSE);
3143 spa_sync_props(spa, props, tx);
3148 spa->spa_sync_on = B_TRUE;
3149 txg_sync_start(spa->spa_dsl_pool);
3152 * We explicitly wait for the first transaction to complete so that our
3153 * bean counters are appropriately updated.
3155 txg_wait_synced(spa->spa_dsl_pool, txg);
3157 spa_config_sync(spa, B_FALSE, B_TRUE);
3159 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3160 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3161 spa_history_log_version(spa, LOG_POOL_CREATE);
3163 spa->spa_minref = refcount_count(&spa->spa_refcount);
3165 mutex_exit(&spa_namespace_lock);
3173 * Get the root pool information from the root disk, then import the root pool
3174 * during the system boot up time.
3176 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3179 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3182 nvlist_t *nvtop, *nvroot;
3185 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3189 * Add this top-level vdev to the child array.
3191 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3193 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3195 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3198 * Put this pool's top-level vdevs into a root vdev.
3200 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3201 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3202 VDEV_TYPE_ROOT) == 0);
3203 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3204 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3205 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3209 * Replace the existing vdev_tree with the new root vdev in
3210 * this pool's configuration (remove the old, add the new).
3212 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3213 nvlist_free(nvroot);
3218 * Walk the vdev tree and see if we can find a device with "better"
3219 * configuration. A configuration is "better" if the label on that
3220 * device has a more recent txg.
3223 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3225 for (int c = 0; c < vd->vdev_children; c++)
3226 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3228 if (vd->vdev_ops->vdev_op_leaf) {
3232 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3236 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3240 * Do we have a better boot device?
3242 if (label_txg > *txg) {
3251 * Import a root pool.
3253 * For x86. devpath_list will consist of devid and/or physpath name of
3254 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3255 * The GRUB "findroot" command will return the vdev we should boot.
3257 * For Sparc, devpath_list consists the physpath name of the booting device
3258 * no matter the rootpool is a single device pool or a mirrored pool.
3260 * "/pci@1f,0/ide@d/disk@0,0:a"
3263 spa_import_rootpool(char *devpath, char *devid)
3266 vdev_t *rvd, *bvd, *avd = NULL;
3267 nvlist_t *config, *nvtop;
3273 * Read the label from the boot device and generate a configuration.
3275 config = spa_generate_rootconf(devpath, devid, &guid);
3276 #if defined(_OBP) && defined(_KERNEL)
3277 if (config == NULL) {
3278 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3280 get_iscsi_bootpath_phy(devpath);
3281 config = spa_generate_rootconf(devpath, devid, &guid);
3285 if (config == NULL) {
3286 cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
3291 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3293 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3295 mutex_enter(&spa_namespace_lock);
3296 if ((spa = spa_lookup(pname)) != NULL) {
3298 * Remove the existing root pool from the namespace so that we
3299 * can replace it with the correct config we just read in.
3304 spa = spa_add(pname, config, NULL);
3305 spa->spa_is_root = B_TRUE;
3306 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3309 * Build up a vdev tree based on the boot device's label config.
3311 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3313 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3314 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3315 VDEV_ALLOC_ROOTPOOL);
3316 spa_config_exit(spa, SCL_ALL, FTAG);
3318 mutex_exit(&spa_namespace_lock);
3319 nvlist_free(config);
3320 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3326 * Get the boot vdev.
3328 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3329 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3330 (u_longlong_t)guid);
3336 * Determine if there is a better boot device.
3339 spa_alt_rootvdev(rvd, &avd, &txg);
3341 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3342 "try booting from '%s'", avd->vdev_path);
3348 * If the boot device is part of a spare vdev then ensure that
3349 * we're booting off the active spare.
3351 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3352 !bvd->vdev_isspare) {
3353 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3354 "try booting from '%s'",
3356 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3362 spa_history_log_version(spa, LOG_POOL_IMPORT);
3364 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3366 spa_config_exit(spa, SCL_ALL, FTAG);
3367 mutex_exit(&spa_namespace_lock);
3369 nvlist_free(config);
3376 vdev_geom_read_pool_label(const char *name, nvlist_t **config);
3379 spa_generate_rootconf(const char *name)
3382 nvlist_t *nvtop, *nvroot;
3386 if (vdev_geom_read_pool_label(name, &config) != 0)
3390 * Multi-vdev root pool configuration discovery is not supported yet.
3393 nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3394 if (nchildren != 1) {
3395 nvlist_free(config);
3400 * Add this top-level vdev to the child array.
3402 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3404 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3408 * Put this pool's top-level vdevs into a root vdev.
3410 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3411 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3412 VDEV_TYPE_ROOT) == 0);
3413 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3414 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3415 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3419 * Replace the existing vdev_tree with the new root vdev in
3420 * this pool's configuration (remove the old, add the new).
3422 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3423 nvlist_free(nvroot);
3428 spa_import_rootpool(const char *name)
3431 vdev_t *rvd, *bvd, *avd = NULL;
3432 nvlist_t *config, *nvtop;
3438 * Read the label from the boot device and generate a configuration.
3440 config = spa_generate_rootconf(name);
3442 mutex_enter(&spa_namespace_lock);
3443 if (config != NULL) {
3444 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3445 &pname) == 0 && strcmp(name, pname) == 0);
3446 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
3449 if ((spa = spa_lookup(pname)) != NULL) {
3451 * Remove the existing root pool from the namespace so
3452 * that we can replace it with the correct config
3457 spa = spa_add(pname, config, NULL);
3458 } else if ((spa = spa_lookup(name)) == NULL) {
3459 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
3463 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
3465 spa->spa_is_root = B_TRUE;
3466 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3469 * Build up a vdev tree based on the boot device's label config.
3471 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3473 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3474 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3475 VDEV_ALLOC_ROOTPOOL);
3476 spa_config_exit(spa, SCL_ALL, FTAG);
3478 mutex_exit(&spa_namespace_lock);
3479 nvlist_free(config);
3480 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3485 spa_history_log_version(spa, LOG_POOL_IMPORT);
3487 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3489 spa_config_exit(spa, SCL_ALL, FTAG);
3490 mutex_exit(&spa_namespace_lock);
3492 nvlist_free(config);
3500 * Import a non-root pool into the system.
3503 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3506 char *altroot = NULL;
3507 spa_load_state_t state = SPA_LOAD_IMPORT;
3508 zpool_rewind_policy_t policy;
3509 uint64_t mode = spa_mode_global;
3510 uint64_t readonly = B_FALSE;
3513 nvlist_t **spares, **l2cache;
3514 uint_t nspares, nl2cache;
3517 * If a pool with this name exists, return failure.
3519 mutex_enter(&spa_namespace_lock);
3520 if (spa_lookup(pool) != NULL) {
3521 mutex_exit(&spa_namespace_lock);
3526 * Create and initialize the spa structure.
3528 (void) nvlist_lookup_string(props,
3529 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3530 (void) nvlist_lookup_uint64(props,
3531 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3534 spa = spa_add(pool, config, altroot);
3535 spa->spa_import_flags = flags;
3538 * Verbatim import - Take a pool and insert it into the namespace
3539 * as if it had been loaded at boot.
3541 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3543 spa_configfile_set(spa, props, B_FALSE);
3545 spa_config_sync(spa, B_FALSE, B_TRUE);
3547 mutex_exit(&spa_namespace_lock);
3548 spa_history_log_version(spa, LOG_POOL_IMPORT);
3553 spa_activate(spa, mode);
3556 * Don't start async tasks until we know everything is healthy.
3558 spa_async_suspend(spa);
3560 zpool_get_rewind_policy(config, &policy);
3561 if (policy.zrp_request & ZPOOL_DO_REWIND)
3562 state = SPA_LOAD_RECOVER;
3565 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3566 * because the user-supplied config is actually the one to trust when
3569 if (state != SPA_LOAD_RECOVER)
3570 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3572 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3573 policy.zrp_request);
3576 * Propagate anything learned while loading the pool and pass it
3577 * back to caller (i.e. rewind info, missing devices, etc).
3579 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3580 spa->spa_load_info) == 0);
3582 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3584 * Toss any existing sparelist, as it doesn't have any validity
3585 * anymore, and conflicts with spa_has_spare().
3587 if (spa->spa_spares.sav_config) {
3588 nvlist_free(spa->spa_spares.sav_config);
3589 spa->spa_spares.sav_config = NULL;
3590 spa_load_spares(spa);
3592 if (spa->spa_l2cache.sav_config) {
3593 nvlist_free(spa->spa_l2cache.sav_config);
3594 spa->spa_l2cache.sav_config = NULL;
3595 spa_load_l2cache(spa);
3598 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3601 error = spa_validate_aux(spa, nvroot, -1ULL,
3604 error = spa_validate_aux(spa, nvroot, -1ULL,
3605 VDEV_ALLOC_L2CACHE);
3606 spa_config_exit(spa, SCL_ALL, FTAG);
3609 spa_configfile_set(spa, props, B_FALSE);
3611 if (error != 0 || (props && spa_writeable(spa) &&
3612 (error = spa_prop_set(spa, props)))) {
3614 spa_deactivate(spa);
3616 mutex_exit(&spa_namespace_lock);
3620 spa_async_resume(spa);
3623 * Override any spares and level 2 cache devices as specified by
3624 * the user, as these may have correct device names/devids, etc.
3626 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3627 &spares, &nspares) == 0) {
3628 if (spa->spa_spares.sav_config)
3629 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3630 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3632 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3633 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3634 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3635 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3636 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3637 spa_load_spares(spa);
3638 spa_config_exit(spa, SCL_ALL, FTAG);
3639 spa->spa_spares.sav_sync = B_TRUE;
3641 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3642 &l2cache, &nl2cache) == 0) {
3643 if (spa->spa_l2cache.sav_config)
3644 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3645 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3647 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3648 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3649 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3650 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3651 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3652 spa_load_l2cache(spa);
3653 spa_config_exit(spa, SCL_ALL, FTAG);
3654 spa->spa_l2cache.sav_sync = B_TRUE;
3658 * Check for any removed devices.
3660 if (spa->spa_autoreplace) {
3661 spa_aux_check_removed(&spa->spa_spares);
3662 spa_aux_check_removed(&spa->spa_l2cache);
3665 if (spa_writeable(spa)) {
3667 * Update the config cache to include the newly-imported pool.
3669 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3673 * It's possible that the pool was expanded while it was exported.
3674 * We kick off an async task to handle this for us.
3676 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3678 mutex_exit(&spa_namespace_lock);
3679 spa_history_log_version(spa, LOG_POOL_IMPORT);
3683 zvol_create_minors(pool);
3690 spa_tryimport(nvlist_t *tryconfig)
3692 nvlist_t *config = NULL;
3698 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3701 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3705 * Create and initialize the spa structure.
3707 mutex_enter(&spa_namespace_lock);
3708 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3709 spa_activate(spa, FREAD);
3712 * Pass off the heavy lifting to spa_load().
3713 * Pass TRUE for mosconfig because the user-supplied config
3714 * is actually the one to trust when doing an import.
3716 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3719 * If 'tryconfig' was at least parsable, return the current config.
3721 if (spa->spa_root_vdev != NULL) {
3722 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3723 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3725 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3727 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3728 spa->spa_uberblock.ub_timestamp) == 0);
3731 * If the bootfs property exists on this pool then we
3732 * copy it out so that external consumers can tell which
3733 * pools are bootable.
3735 if ((!error || error == EEXIST) && spa->spa_bootfs) {
3736 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3739 * We have to play games with the name since the
3740 * pool was opened as TRYIMPORT_NAME.
3742 if (dsl_dsobj_to_dsname(spa_name(spa),
3743 spa->spa_bootfs, tmpname) == 0) {
3745 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3747 cp = strchr(tmpname, '/');
3749 (void) strlcpy(dsname, tmpname,
3752 (void) snprintf(dsname, MAXPATHLEN,
3753 "%s/%s", poolname, ++cp);
3755 VERIFY(nvlist_add_string(config,
3756 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3757 kmem_free(dsname, MAXPATHLEN);
3759 kmem_free(tmpname, MAXPATHLEN);
3763 * Add the list of hot spares and level 2 cache devices.
3765 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3766 spa_add_spares(spa, config);
3767 spa_add_l2cache(spa, config);
3768 spa_config_exit(spa, SCL_CONFIG, FTAG);
3772 spa_deactivate(spa);
3774 mutex_exit(&spa_namespace_lock);
3780 * Pool export/destroy
3782 * The act of destroying or exporting a pool is very simple. We make sure there
3783 * is no more pending I/O and any references to the pool are gone. Then, we
3784 * update the pool state and sync all the labels to disk, removing the
3785 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3786 * we don't sync the labels or remove the configuration cache.
3789 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
3790 boolean_t force, boolean_t hardforce)
3797 if (!(spa_mode_global & FWRITE))
3800 mutex_enter(&spa_namespace_lock);
3801 if ((spa = spa_lookup(pool)) == NULL) {
3802 mutex_exit(&spa_namespace_lock);
3807 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3808 * reacquire the namespace lock, and see if we can export.
3810 spa_open_ref(spa, FTAG);
3811 mutex_exit(&spa_namespace_lock);
3812 spa_async_suspend(spa);
3813 mutex_enter(&spa_namespace_lock);
3814 spa_close(spa, FTAG);
3817 * The pool will be in core if it's openable,
3818 * in which case we can modify its state.
3820 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3822 * Objsets may be open only because they're dirty, so we
3823 * have to force it to sync before checking spa_refcnt.
3825 txg_wait_synced(spa->spa_dsl_pool, 0);
3828 * A pool cannot be exported or destroyed if there are active
3829 * references. If we are resetting a pool, allow references by
3830 * fault injection handlers.
3832 if (!spa_refcount_zero(spa) ||
3833 (spa->spa_inject_ref != 0 &&
3834 new_state != POOL_STATE_UNINITIALIZED)) {
3835 spa_async_resume(spa);
3836 mutex_exit(&spa_namespace_lock);
3841 * A pool cannot be exported if it has an active shared spare.
3842 * This is to prevent other pools stealing the active spare
3843 * from an exported pool. At user's own will, such pool can
3844 * be forcedly exported.
3846 if (!force && new_state == POOL_STATE_EXPORTED &&
3847 spa_has_active_shared_spare(spa)) {
3848 spa_async_resume(spa);
3849 mutex_exit(&spa_namespace_lock);
3854 * We want this to be reflected on every label,
3855 * so mark them all dirty. spa_unload() will do the
3856 * final sync that pushes these changes out.
3858 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
3859 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3860 spa->spa_state = new_state;
3861 spa->spa_final_txg = spa_last_synced_txg(spa) +
3863 vdev_config_dirty(spa->spa_root_vdev);
3864 spa_config_exit(spa, SCL_ALL, FTAG);
3868 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
3870 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3872 spa_deactivate(spa);
3875 if (oldconfig && spa->spa_config)
3876 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
3878 if (new_state != POOL_STATE_UNINITIALIZED) {
3880 spa_config_sync(spa, B_TRUE, B_TRUE);
3883 mutex_exit(&spa_namespace_lock);
3889 * Destroy a storage pool.
3892 spa_destroy(char *pool)
3894 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
3899 * Export a storage pool.
3902 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
3903 boolean_t hardforce)
3905 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
3910 * Similar to spa_export(), this unloads the spa_t without actually removing it
3911 * from the namespace in any way.
3914 spa_reset(char *pool)
3916 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
3921 * ==========================================================================
3922 * Device manipulation
3923 * ==========================================================================
3927 * Add a device to a storage pool.
3930 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3934 vdev_t *rvd = spa->spa_root_vdev;
3936 nvlist_t **spares, **l2cache;
3937 uint_t nspares, nl2cache;
3939 ASSERT(spa_writeable(spa));
3941 txg = spa_vdev_enter(spa);
3943 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
3944 VDEV_ALLOC_ADD)) != 0)
3945 return (spa_vdev_exit(spa, NULL, txg, error));
3947 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
3949 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
3953 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
3957 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
3958 return (spa_vdev_exit(spa, vd, txg, EINVAL));
3960 if (vd->vdev_children != 0 &&
3961 (error = vdev_create(vd, txg, B_FALSE)) != 0)
3962 return (spa_vdev_exit(spa, vd, txg, error));
3965 * We must validate the spares and l2cache devices after checking the
3966 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
3968 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
3969 return (spa_vdev_exit(spa, vd, txg, error));
3972 * Transfer each new top-level vdev from vd to rvd.
3974 for (int c = 0; c < vd->vdev_children; c++) {
3977 * Set the vdev id to the first hole, if one exists.
3979 for (id = 0; id < rvd->vdev_children; id++) {
3980 if (rvd->vdev_child[id]->vdev_ishole) {
3981 vdev_free(rvd->vdev_child[id]);
3985 tvd = vd->vdev_child[c];
3986 vdev_remove_child(vd, tvd);
3988 vdev_add_child(rvd, tvd);
3989 vdev_config_dirty(tvd);
3993 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
3994 ZPOOL_CONFIG_SPARES);
3995 spa_load_spares(spa);
3996 spa->spa_spares.sav_sync = B_TRUE;
3999 if (nl2cache != 0) {
4000 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4001 ZPOOL_CONFIG_L2CACHE);
4002 spa_load_l2cache(spa);
4003 spa->spa_l2cache.sav_sync = B_TRUE;
4007 * We have to be careful when adding new vdevs to an existing pool.
4008 * If other threads start allocating from these vdevs before we
4009 * sync the config cache, and we lose power, then upon reboot we may
4010 * fail to open the pool because there are DVAs that the config cache
4011 * can't translate. Therefore, we first add the vdevs without
4012 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4013 * and then let spa_config_update() initialize the new metaslabs.
4015 * spa_load() checks for added-but-not-initialized vdevs, so that
4016 * if we lose power at any point in this sequence, the remaining
4017 * steps will be completed the next time we load the pool.
4019 (void) spa_vdev_exit(spa, vd, txg, 0);
4021 mutex_enter(&spa_namespace_lock);
4022 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4023 mutex_exit(&spa_namespace_lock);
4029 * Attach a device to a mirror. The arguments are the path to any device
4030 * in the mirror, and the nvroot for the new device. If the path specifies
4031 * a device that is not mirrored, we automatically insert the mirror vdev.
4033 * If 'replacing' is specified, the new device is intended to replace the
4034 * existing device; in this case the two devices are made into their own
4035 * mirror using the 'replacing' vdev, which is functionally identical to
4036 * the mirror vdev (it actually reuses all the same ops) but has a few
4037 * extra rules: you can't attach to it after it's been created, and upon
4038 * completion of resilvering, the first disk (the one being replaced)
4039 * is automatically detached.
4042 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4044 uint64_t txg, dtl_max_txg;
4045 vdev_t *rvd = spa->spa_root_vdev;
4046 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4048 char *oldvdpath, *newvdpath;
4052 ASSERT(spa_writeable(spa));
4054 txg = spa_vdev_enter(spa);
4056 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4059 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4061 if (!oldvd->vdev_ops->vdev_op_leaf)
4062 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4064 pvd = oldvd->vdev_parent;
4066 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4067 VDEV_ALLOC_ATTACH)) != 0)
4068 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4070 if (newrootvd->vdev_children != 1)
4071 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4073 newvd = newrootvd->vdev_child[0];
4075 if (!newvd->vdev_ops->vdev_op_leaf)
4076 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4078 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4079 return (spa_vdev_exit(spa, newrootvd, txg, error));
4082 * Spares can't replace logs
4084 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4085 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4089 * For attach, the only allowable parent is a mirror or the root
4092 if (pvd->vdev_ops != &vdev_mirror_ops &&
4093 pvd->vdev_ops != &vdev_root_ops)
4094 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4096 pvops = &vdev_mirror_ops;
4099 * Active hot spares can only be replaced by inactive hot
4102 if (pvd->vdev_ops == &vdev_spare_ops &&
4103 oldvd->vdev_isspare &&
4104 !spa_has_spare(spa, newvd->vdev_guid))
4105 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4108 * If the source is a hot spare, and the parent isn't already a
4109 * spare, then we want to create a new hot spare. Otherwise, we
4110 * want to create a replacing vdev. The user is not allowed to
4111 * attach to a spared vdev child unless the 'isspare' state is
4112 * the same (spare replaces spare, non-spare replaces
4115 if (pvd->vdev_ops == &vdev_replacing_ops &&
4116 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4117 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4118 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4119 newvd->vdev_isspare != oldvd->vdev_isspare) {
4120 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4123 if (newvd->vdev_isspare)
4124 pvops = &vdev_spare_ops;
4126 pvops = &vdev_replacing_ops;
4130 * Make sure the new device is big enough.
4132 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4133 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4136 * The new device cannot have a higher alignment requirement
4137 * than the top-level vdev.
4139 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4140 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4143 * If this is an in-place replacement, update oldvd's path and devid
4144 * to make it distinguishable from newvd, and unopenable from now on.
4146 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4147 spa_strfree(oldvd->vdev_path);
4148 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4150 (void) sprintf(oldvd->vdev_path, "%s/%s",
4151 newvd->vdev_path, "old");
4152 if (oldvd->vdev_devid != NULL) {
4153 spa_strfree(oldvd->vdev_devid);
4154 oldvd->vdev_devid = NULL;
4158 /* mark the device being resilvered */
4159 newvd->vdev_resilvering = B_TRUE;
4162 * If the parent is not a mirror, or if we're replacing, insert the new
4163 * mirror/replacing/spare vdev above oldvd.
4165 if (pvd->vdev_ops != pvops)
4166 pvd = vdev_add_parent(oldvd, pvops);
4168 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4169 ASSERT(pvd->vdev_ops == pvops);
4170 ASSERT(oldvd->vdev_parent == pvd);
4173 * Extract the new device from its root and add it to pvd.
4175 vdev_remove_child(newrootvd, newvd);
4176 newvd->vdev_id = pvd->vdev_children;
4177 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4178 vdev_add_child(pvd, newvd);
4180 tvd = newvd->vdev_top;
4181 ASSERT(pvd->vdev_top == tvd);
4182 ASSERT(tvd->vdev_parent == rvd);
4184 vdev_config_dirty(tvd);
4187 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4188 * for any dmu_sync-ed blocks. It will propagate upward when
4189 * spa_vdev_exit() calls vdev_dtl_reassess().
4191 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4193 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4194 dtl_max_txg - TXG_INITIAL);
4196 if (newvd->vdev_isspare) {
4197 spa_spare_activate(newvd);
4198 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4201 oldvdpath = spa_strdup(oldvd->vdev_path);
4202 newvdpath = spa_strdup(newvd->vdev_path);
4203 newvd_isspare = newvd->vdev_isspare;
4206 * Mark newvd's DTL dirty in this txg.
4208 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4211 * Restart the resilver
4213 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4218 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4220 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
4221 "%s vdev=%s %s vdev=%s",
4222 replacing && newvd_isspare ? "spare in" :
4223 replacing ? "replace" : "attach", newvdpath,
4224 replacing ? "for" : "to", oldvdpath);
4226 spa_strfree(oldvdpath);
4227 spa_strfree(newvdpath);
4229 if (spa->spa_bootfs)
4230 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4236 * Detach a device from a mirror or replacing vdev.
4237 * If 'replace_done' is specified, only detach if the parent
4238 * is a replacing vdev.
4241 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4245 vdev_t *rvd = spa->spa_root_vdev;
4246 vdev_t *vd, *pvd, *cvd, *tvd;
4247 boolean_t unspare = B_FALSE;
4248 uint64_t unspare_guid;
4251 ASSERT(spa_writeable(spa));
4253 txg = spa_vdev_enter(spa);
4255 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4258 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4260 if (!vd->vdev_ops->vdev_op_leaf)
4261 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4263 pvd = vd->vdev_parent;
4266 * If the parent/child relationship is not as expected, don't do it.
4267 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4268 * vdev that's replacing B with C. The user's intent in replacing
4269 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4270 * the replace by detaching C, the expected behavior is to end up
4271 * M(A,B). But suppose that right after deciding to detach C,
4272 * the replacement of B completes. We would have M(A,C), and then
4273 * ask to detach C, which would leave us with just A -- not what
4274 * the user wanted. To prevent this, we make sure that the
4275 * parent/child relationship hasn't changed -- in this example,
4276 * that C's parent is still the replacing vdev R.
4278 if (pvd->vdev_guid != pguid && pguid != 0)
4279 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4282 * Only 'replacing' or 'spare' vdevs can be replaced.
4284 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4285 pvd->vdev_ops != &vdev_spare_ops)
4286 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4288 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4289 spa_version(spa) >= SPA_VERSION_SPARES);
4292 * Only mirror, replacing, and spare vdevs support detach.
4294 if (pvd->vdev_ops != &vdev_replacing_ops &&
4295 pvd->vdev_ops != &vdev_mirror_ops &&
4296 pvd->vdev_ops != &vdev_spare_ops)
4297 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4300 * If this device has the only valid copy of some data,
4301 * we cannot safely detach it.
4303 if (vdev_dtl_required(vd))
4304 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4306 ASSERT(pvd->vdev_children >= 2);
4309 * If we are detaching the second disk from a replacing vdev, then
4310 * check to see if we changed the original vdev's path to have "/old"
4311 * at the end in spa_vdev_attach(). If so, undo that change now.
4313 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4314 vd->vdev_path != NULL) {
4315 size_t len = strlen(vd->vdev_path);
4317 for (int c = 0; c < pvd->vdev_children; c++) {
4318 cvd = pvd->vdev_child[c];
4320 if (cvd == vd || cvd->vdev_path == NULL)
4323 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4324 strcmp(cvd->vdev_path + len, "/old") == 0) {
4325 spa_strfree(cvd->vdev_path);
4326 cvd->vdev_path = spa_strdup(vd->vdev_path);
4333 * If we are detaching the original disk from a spare, then it implies
4334 * that the spare should become a real disk, and be removed from the
4335 * active spare list for the pool.
4337 if (pvd->vdev_ops == &vdev_spare_ops &&
4339 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4343 * Erase the disk labels so the disk can be used for other things.
4344 * This must be done after all other error cases are handled,
4345 * but before we disembowel vd (so we can still do I/O to it).
4346 * But if we can't do it, don't treat the error as fatal --
4347 * it may be that the unwritability of the disk is the reason
4348 * it's being detached!
4350 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4353 * Remove vd from its parent and compact the parent's children.
4355 vdev_remove_child(pvd, vd);
4356 vdev_compact_children(pvd);
4359 * Remember one of the remaining children so we can get tvd below.
4361 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4364 * If we need to remove the remaining child from the list of hot spares,
4365 * do it now, marking the vdev as no longer a spare in the process.
4366 * We must do this before vdev_remove_parent(), because that can
4367 * change the GUID if it creates a new toplevel GUID. For a similar
4368 * reason, we must remove the spare now, in the same txg as the detach;
4369 * otherwise someone could attach a new sibling, change the GUID, and
4370 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4373 ASSERT(cvd->vdev_isspare);
4374 spa_spare_remove(cvd);
4375 unspare_guid = cvd->vdev_guid;
4376 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4377 cvd->vdev_unspare = B_TRUE;
4381 * If the parent mirror/replacing vdev only has one child,
4382 * the parent is no longer needed. Remove it from the tree.
4384 if (pvd->vdev_children == 1) {
4385 if (pvd->vdev_ops == &vdev_spare_ops)
4386 cvd->vdev_unspare = B_FALSE;
4387 vdev_remove_parent(cvd);
4388 cvd->vdev_resilvering = B_FALSE;
4393 * We don't set tvd until now because the parent we just removed
4394 * may have been the previous top-level vdev.
4396 tvd = cvd->vdev_top;
4397 ASSERT(tvd->vdev_parent == rvd);
4400 * Reevaluate the parent vdev state.
4402 vdev_propagate_state(cvd);
4405 * If the 'autoexpand' property is set on the pool then automatically
4406 * try to expand the size of the pool. For example if the device we
4407 * just detached was smaller than the others, it may be possible to
4408 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4409 * first so that we can obtain the updated sizes of the leaf vdevs.
4411 if (spa->spa_autoexpand) {
4413 vdev_expand(tvd, txg);
4416 vdev_config_dirty(tvd);
4419 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4420 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4421 * But first make sure we're not on any *other* txg's DTL list, to
4422 * prevent vd from being accessed after it's freed.
4424 vdpath = spa_strdup(vd->vdev_path);
4425 for (int t = 0; t < TXG_SIZE; t++)
4426 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4427 vd->vdev_detached = B_TRUE;
4428 vdev_dirty(tvd, VDD_DTL, vd, txg);
4430 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4432 /* hang on to the spa before we release the lock */
4433 spa_open_ref(spa, FTAG);
4435 error = spa_vdev_exit(spa, vd, txg, 0);
4437 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4439 spa_strfree(vdpath);
4442 * If this was the removal of the original device in a hot spare vdev,
4443 * then we want to go through and remove the device from the hot spare
4444 * list of every other pool.
4447 spa_t *altspa = NULL;
4449 mutex_enter(&spa_namespace_lock);
4450 while ((altspa = spa_next(altspa)) != NULL) {
4451 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4455 spa_open_ref(altspa, FTAG);
4456 mutex_exit(&spa_namespace_lock);
4457 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4458 mutex_enter(&spa_namespace_lock);
4459 spa_close(altspa, FTAG);
4461 mutex_exit(&spa_namespace_lock);
4463 /* search the rest of the vdevs for spares to remove */
4464 spa_vdev_resilver_done(spa);
4467 /* all done with the spa; OK to release */
4468 mutex_enter(&spa_namespace_lock);
4469 spa_close(spa, FTAG);
4470 mutex_exit(&spa_namespace_lock);
4476 * Split a set of devices from their mirrors, and create a new pool from them.
4479 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4480 nvlist_t *props, boolean_t exp)
4483 uint64_t txg, *glist;
4485 uint_t c, children, lastlog;
4486 nvlist_t **child, *nvl, *tmp;
4488 char *altroot = NULL;
4489 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4490 boolean_t activate_slog;
4492 ASSERT(spa_writeable(spa));
4494 txg = spa_vdev_enter(spa);
4496 /* clear the log and flush everything up to now */
4497 activate_slog = spa_passivate_log(spa);
4498 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4499 error = spa_offline_log(spa);
4500 txg = spa_vdev_config_enter(spa);
4503 spa_activate_log(spa);
4506 return (spa_vdev_exit(spa, NULL, txg, error));
4508 /* check new spa name before going any further */
4509 if (spa_lookup(newname) != NULL)
4510 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4513 * scan through all the children to ensure they're all mirrors
4515 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4516 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4518 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4520 /* first, check to ensure we've got the right child count */
4521 rvd = spa->spa_root_vdev;
4523 for (c = 0; c < rvd->vdev_children; c++) {
4524 vdev_t *vd = rvd->vdev_child[c];
4526 /* don't count the holes & logs as children */
4527 if (vd->vdev_islog || vd->vdev_ishole) {
4535 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4536 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4538 /* next, ensure no spare or cache devices are part of the split */
4539 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4540 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4541 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4543 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
4544 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
4546 /* then, loop over each vdev and validate it */
4547 for (c = 0; c < children; c++) {
4548 uint64_t is_hole = 0;
4550 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4554 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4555 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4563 /* which disk is going to be split? */
4564 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4570 /* look it up in the spa */
4571 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4572 if (vml[c] == NULL) {
4577 /* make sure there's nothing stopping the split */
4578 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4579 vml[c]->vdev_islog ||
4580 vml[c]->vdev_ishole ||
4581 vml[c]->vdev_isspare ||
4582 vml[c]->vdev_isl2cache ||
4583 !vdev_writeable(vml[c]) ||
4584 vml[c]->vdev_children != 0 ||
4585 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4586 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4591 if (vdev_dtl_required(vml[c])) {
4596 /* we need certain info from the top level */
4597 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4598 vml[c]->vdev_top->vdev_ms_array) == 0);
4599 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4600 vml[c]->vdev_top->vdev_ms_shift) == 0);
4601 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4602 vml[c]->vdev_top->vdev_asize) == 0);
4603 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4604 vml[c]->vdev_top->vdev_ashift) == 0);
4608 kmem_free(vml, children * sizeof (vdev_t *));
4609 kmem_free(glist, children * sizeof (uint64_t));
4610 return (spa_vdev_exit(spa, NULL, txg, error));
4613 /* stop writers from using the disks */
4614 for (c = 0; c < children; c++) {
4616 vml[c]->vdev_offline = B_TRUE;
4618 vdev_reopen(spa->spa_root_vdev);
4621 * Temporarily record the splitting vdevs in the spa config. This
4622 * will disappear once the config is regenerated.
4624 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4625 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4626 glist, children) == 0);
4627 kmem_free(glist, children * sizeof (uint64_t));
4629 mutex_enter(&spa->spa_props_lock);
4630 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4632 mutex_exit(&spa->spa_props_lock);
4633 spa->spa_config_splitting = nvl;
4634 vdev_config_dirty(spa->spa_root_vdev);
4636 /* configure and create the new pool */
4637 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4638 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4639 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4640 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4641 spa_version(spa)) == 0);
4642 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4643 spa->spa_config_txg) == 0);
4644 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4645 spa_generate_guid(NULL)) == 0);
4646 (void) nvlist_lookup_string(props,
4647 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4649 /* add the new pool to the namespace */
4650 newspa = spa_add(newname, config, altroot);
4651 newspa->spa_config_txg = spa->spa_config_txg;
4652 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4654 /* release the spa config lock, retaining the namespace lock */
4655 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4657 if (zio_injection_enabled)
4658 zio_handle_panic_injection(spa, FTAG, 1);
4660 spa_activate(newspa, spa_mode_global);
4661 spa_async_suspend(newspa);
4664 /* mark that we are creating new spa by splitting */
4665 newspa->spa_splitting_newspa = B_TRUE;
4667 /* create the new pool from the disks of the original pool */
4668 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4670 newspa->spa_splitting_newspa = B_FALSE;
4675 /* if that worked, generate a real config for the new pool */
4676 if (newspa->spa_root_vdev != NULL) {
4677 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4678 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4679 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4680 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4681 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4686 if (props != NULL) {
4687 spa_configfile_set(newspa, props, B_FALSE);
4688 error = spa_prop_set(newspa, props);
4693 /* flush everything */
4694 txg = spa_vdev_config_enter(newspa);
4695 vdev_config_dirty(newspa->spa_root_vdev);
4696 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4698 if (zio_injection_enabled)
4699 zio_handle_panic_injection(spa, FTAG, 2);
4701 spa_async_resume(newspa);
4703 /* finally, update the original pool's config */
4704 txg = spa_vdev_config_enter(spa);
4705 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4706 error = dmu_tx_assign(tx, TXG_WAIT);
4709 for (c = 0; c < children; c++) {
4710 if (vml[c] != NULL) {
4713 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4719 vdev_config_dirty(spa->spa_root_vdev);
4720 spa->spa_config_splitting = NULL;
4724 (void) spa_vdev_exit(spa, NULL, txg, 0);
4726 if (zio_injection_enabled)
4727 zio_handle_panic_injection(spa, FTAG, 3);
4729 /* split is complete; log a history record */
4730 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
4731 "split new pool %s from pool %s", newname, spa_name(spa));
4733 kmem_free(vml, children * sizeof (vdev_t *));
4735 /* if we're not going to mount the filesystems in userland, export */
4737 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
4744 spa_deactivate(newspa);
4747 txg = spa_vdev_config_enter(spa);
4749 /* re-online all offlined disks */
4750 for (c = 0; c < children; c++) {
4752 vml[c]->vdev_offline = B_FALSE;
4754 vdev_reopen(spa->spa_root_vdev);
4756 nvlist_free(spa->spa_config_splitting);
4757 spa->spa_config_splitting = NULL;
4758 (void) spa_vdev_exit(spa, NULL, txg, error);
4760 kmem_free(vml, children * sizeof (vdev_t *));
4765 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
4767 for (int i = 0; i < count; i++) {
4770 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
4773 if (guid == target_guid)
4781 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
4782 nvlist_t *dev_to_remove)
4784 nvlist_t **newdev = NULL;
4787 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
4789 for (int i = 0, j = 0; i < count; i++) {
4790 if (dev[i] == dev_to_remove)
4792 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
4795 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
4796 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
4798 for (int i = 0; i < count - 1; i++)
4799 nvlist_free(newdev[i]);
4802 kmem_free(newdev, (count - 1) * sizeof (void *));
4806 * Evacuate the device.
4809 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
4814 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4815 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4816 ASSERT(vd == vd->vdev_top);
4819 * Evacuate the device. We don't hold the config lock as writer
4820 * since we need to do I/O but we do keep the
4821 * spa_namespace_lock held. Once this completes the device
4822 * should no longer have any blocks allocated on it.
4824 if (vd->vdev_islog) {
4825 if (vd->vdev_stat.vs_alloc != 0)
4826 error = spa_offline_log(spa);
4835 * The evacuation succeeded. Remove any remaining MOS metadata
4836 * associated with this vdev, and wait for these changes to sync.
4838 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
4839 txg = spa_vdev_config_enter(spa);
4840 vd->vdev_removing = B_TRUE;
4841 vdev_dirty(vd, 0, NULL, txg);
4842 vdev_config_dirty(vd);
4843 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4849 * Complete the removal by cleaning up the namespace.
4852 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
4854 vdev_t *rvd = spa->spa_root_vdev;
4855 uint64_t id = vd->vdev_id;
4856 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
4858 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4859 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4860 ASSERT(vd == vd->vdev_top);
4863 * Only remove any devices which are empty.
4865 if (vd->vdev_stat.vs_alloc != 0)
4868 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4870 if (list_link_active(&vd->vdev_state_dirty_node))
4871 vdev_state_clean(vd);
4872 if (list_link_active(&vd->vdev_config_dirty_node))
4873 vdev_config_clean(vd);
4878 vdev_compact_children(rvd);
4880 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
4881 vdev_add_child(rvd, vd);
4883 vdev_config_dirty(rvd);
4886 * Reassess the health of our root vdev.
4892 * Remove a device from the pool -
4894 * Removing a device from the vdev namespace requires several steps
4895 * and can take a significant amount of time. As a result we use
4896 * the spa_vdev_config_[enter/exit] functions which allow us to
4897 * grab and release the spa_config_lock while still holding the namespace
4898 * lock. During each step the configuration is synced out.
4902 * Remove a device from the pool. Currently, this supports removing only hot
4903 * spares, slogs, and level 2 ARC devices.
4906 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
4909 metaslab_group_t *mg;
4910 nvlist_t **spares, **l2cache, *nv;
4912 uint_t nspares, nl2cache;
4914 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
4916 ASSERT(spa_writeable(spa));
4919 txg = spa_vdev_enter(spa);
4921 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4923 if (spa->spa_spares.sav_vdevs != NULL &&
4924 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
4925 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
4926 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
4928 * Only remove the hot spare if it's not currently in use
4931 if (vd == NULL || unspare) {
4932 spa_vdev_remove_aux(spa->spa_spares.sav_config,
4933 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
4934 spa_load_spares(spa);
4935 spa->spa_spares.sav_sync = B_TRUE;
4939 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
4940 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
4941 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
4942 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
4944 * Cache devices can always be removed.
4946 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
4947 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
4948 spa_load_l2cache(spa);
4949 spa->spa_l2cache.sav_sync = B_TRUE;
4950 } else if (vd != NULL && vd->vdev_islog) {
4952 ASSERT(vd == vd->vdev_top);
4955 * XXX - Once we have bp-rewrite this should
4956 * become the common case.
4962 * Stop allocating from this vdev.
4964 metaslab_group_passivate(mg);
4967 * Wait for the youngest allocations and frees to sync,
4968 * and then wait for the deferral of those frees to finish.
4970 spa_vdev_config_exit(spa, NULL,
4971 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
4974 * Attempt to evacuate the vdev.
4976 error = spa_vdev_remove_evacuate(spa, vd);
4978 txg = spa_vdev_config_enter(spa);
4981 * If we couldn't evacuate the vdev, unwind.
4984 metaslab_group_activate(mg);
4985 return (spa_vdev_exit(spa, NULL, txg, error));
4989 * Clean up the vdev namespace.
4991 spa_vdev_remove_from_namespace(spa, vd);
4993 } else if (vd != NULL) {
4995 * Normal vdevs cannot be removed (yet).
5000 * There is no vdev of any kind with the specified guid.
5006 return (spa_vdev_exit(spa, NULL, txg, error));
5012 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5013 * current spared, so we can detach it.
5016 spa_vdev_resilver_done_hunt(vdev_t *vd)
5018 vdev_t *newvd, *oldvd;
5020 for (int c = 0; c < vd->vdev_children; c++) {
5021 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5027 * Check for a completed replacement. We always consider the first
5028 * vdev in the list to be the oldest vdev, and the last one to be
5029 * the newest (see spa_vdev_attach() for how that works). In
5030 * the case where the newest vdev is faulted, we will not automatically
5031 * remove it after a resilver completes. This is OK as it will require
5032 * user intervention to determine which disk the admin wishes to keep.
5034 if (vd->vdev_ops == &vdev_replacing_ops) {
5035 ASSERT(vd->vdev_children > 1);
5037 newvd = vd->vdev_child[vd->vdev_children - 1];
5038 oldvd = vd->vdev_child[0];
5040 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5041 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5042 !vdev_dtl_required(oldvd))
5047 * Check for a completed resilver with the 'unspare' flag set.
5049 if (vd->vdev_ops == &vdev_spare_ops) {
5050 vdev_t *first = vd->vdev_child[0];
5051 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5053 if (last->vdev_unspare) {
5056 } else if (first->vdev_unspare) {
5063 if (oldvd != NULL &&
5064 vdev_dtl_empty(newvd, DTL_MISSING) &&
5065 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5066 !vdev_dtl_required(oldvd))
5070 * If there are more than two spares attached to a disk,
5071 * and those spares are not required, then we want to
5072 * attempt to free them up now so that they can be used
5073 * by other pools. Once we're back down to a single
5074 * disk+spare, we stop removing them.
5076 if (vd->vdev_children > 2) {
5077 newvd = vd->vdev_child[1];
5079 if (newvd->vdev_isspare && last->vdev_isspare &&
5080 vdev_dtl_empty(last, DTL_MISSING) &&
5081 vdev_dtl_empty(last, DTL_OUTAGE) &&
5082 !vdev_dtl_required(newvd))
5091 spa_vdev_resilver_done(spa_t *spa)
5093 vdev_t *vd, *pvd, *ppvd;
5094 uint64_t guid, sguid, pguid, ppguid;
5096 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5098 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5099 pvd = vd->vdev_parent;
5100 ppvd = pvd->vdev_parent;
5101 guid = vd->vdev_guid;
5102 pguid = pvd->vdev_guid;
5103 ppguid = ppvd->vdev_guid;
5106 * If we have just finished replacing a hot spared device, then
5107 * we need to detach the parent's first child (the original hot
5110 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5111 ppvd->vdev_children == 2) {
5112 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5113 sguid = ppvd->vdev_child[1]->vdev_guid;
5115 spa_config_exit(spa, SCL_ALL, FTAG);
5116 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5118 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5120 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5123 spa_config_exit(spa, SCL_ALL, FTAG);
5127 * Update the stored path or FRU for this vdev.
5130 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5134 boolean_t sync = B_FALSE;
5136 ASSERT(spa_writeable(spa));
5138 spa_vdev_state_enter(spa, SCL_ALL);
5140 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5141 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5143 if (!vd->vdev_ops->vdev_op_leaf)
5144 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5147 if (strcmp(value, vd->vdev_path) != 0) {
5148 spa_strfree(vd->vdev_path);
5149 vd->vdev_path = spa_strdup(value);
5153 if (vd->vdev_fru == NULL) {
5154 vd->vdev_fru = spa_strdup(value);
5156 } else if (strcmp(value, vd->vdev_fru) != 0) {
5157 spa_strfree(vd->vdev_fru);
5158 vd->vdev_fru = spa_strdup(value);
5163 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5167 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5169 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5173 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5175 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5179 * ==========================================================================
5181 * ==========================================================================
5185 spa_scan_stop(spa_t *spa)
5187 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5188 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5190 return (dsl_scan_cancel(spa->spa_dsl_pool));
5194 spa_scan(spa_t *spa, pool_scan_func_t func)
5196 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5198 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5202 * If a resilver was requested, but there is no DTL on a
5203 * writeable leaf device, we have nothing to do.
5205 if (func == POOL_SCAN_RESILVER &&
5206 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5207 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5211 return (dsl_scan(spa->spa_dsl_pool, func));
5215 * ==========================================================================
5216 * SPA async task processing
5217 * ==========================================================================
5221 spa_async_remove(spa_t *spa, vdev_t *vd)
5223 if (vd->vdev_remove_wanted) {
5224 vd->vdev_remove_wanted = B_FALSE;
5225 vd->vdev_delayed_close = B_FALSE;
5226 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5229 * We want to clear the stats, but we don't want to do a full
5230 * vdev_clear() as that will cause us to throw away
5231 * degraded/faulted state as well as attempt to reopen the
5232 * device, all of which is a waste.
5234 vd->vdev_stat.vs_read_errors = 0;
5235 vd->vdev_stat.vs_write_errors = 0;
5236 vd->vdev_stat.vs_checksum_errors = 0;
5238 vdev_state_dirty(vd->vdev_top);
5241 for (int c = 0; c < vd->vdev_children; c++)
5242 spa_async_remove(spa, vd->vdev_child[c]);
5246 spa_async_probe(spa_t *spa, vdev_t *vd)
5248 if (vd->vdev_probe_wanted) {
5249 vd->vdev_probe_wanted = B_FALSE;
5250 vdev_reopen(vd); /* vdev_open() does the actual probe */
5253 for (int c = 0; c < vd->vdev_children; c++)
5254 spa_async_probe(spa, vd->vdev_child[c]);
5258 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5264 if (!spa->spa_autoexpand)
5267 for (int c = 0; c < vd->vdev_children; c++) {
5268 vdev_t *cvd = vd->vdev_child[c];
5269 spa_async_autoexpand(spa, cvd);
5272 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5275 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5276 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5278 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5279 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5281 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5282 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5285 kmem_free(physpath, MAXPATHLEN);
5289 spa_async_thread(void *arg)
5294 ASSERT(spa->spa_sync_on);
5296 mutex_enter(&spa->spa_async_lock);
5297 tasks = spa->spa_async_tasks;
5298 spa->spa_async_tasks = 0;
5299 mutex_exit(&spa->spa_async_lock);
5302 * See if the config needs to be updated.
5304 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5305 uint64_t old_space, new_space;
5307 mutex_enter(&spa_namespace_lock);
5308 old_space = metaslab_class_get_space(spa_normal_class(spa));
5309 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5310 new_space = metaslab_class_get_space(spa_normal_class(spa));
5311 mutex_exit(&spa_namespace_lock);
5314 * If the pool grew as a result of the config update,
5315 * then log an internal history event.
5317 if (new_space != old_space) {
5318 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5320 "pool '%s' size: %llu(+%llu)",
5321 spa_name(spa), new_space, new_space - old_space);
5326 * See if any devices need to be marked REMOVED.
5328 if (tasks & SPA_ASYNC_REMOVE) {
5329 spa_vdev_state_enter(spa, SCL_NONE);
5330 spa_async_remove(spa, spa->spa_root_vdev);
5331 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5332 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5333 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5334 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5335 (void) spa_vdev_state_exit(spa, NULL, 0);
5338 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5339 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5340 spa_async_autoexpand(spa, spa->spa_root_vdev);
5341 spa_config_exit(spa, SCL_CONFIG, FTAG);
5345 * See if any devices need to be probed.
5347 if (tasks & SPA_ASYNC_PROBE) {
5348 spa_vdev_state_enter(spa, SCL_NONE);
5349 spa_async_probe(spa, spa->spa_root_vdev);
5350 (void) spa_vdev_state_exit(spa, NULL, 0);
5354 * If any devices are done replacing, detach them.
5356 if (tasks & SPA_ASYNC_RESILVER_DONE)
5357 spa_vdev_resilver_done(spa);
5360 * Kick off a resilver.
5362 if (tasks & SPA_ASYNC_RESILVER)
5363 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5366 * Let the world know that we're done.
5368 mutex_enter(&spa->spa_async_lock);
5369 spa->spa_async_thread = NULL;
5370 cv_broadcast(&spa->spa_async_cv);
5371 mutex_exit(&spa->spa_async_lock);
5376 spa_async_suspend(spa_t *spa)
5378 mutex_enter(&spa->spa_async_lock);
5379 spa->spa_async_suspended++;
5380 while (spa->spa_async_thread != NULL)
5381 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5382 mutex_exit(&spa->spa_async_lock);
5386 spa_async_resume(spa_t *spa)
5388 mutex_enter(&spa->spa_async_lock);
5389 ASSERT(spa->spa_async_suspended != 0);
5390 spa->spa_async_suspended--;
5391 mutex_exit(&spa->spa_async_lock);
5395 spa_async_dispatch(spa_t *spa)
5397 mutex_enter(&spa->spa_async_lock);
5398 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5399 spa->spa_async_thread == NULL &&
5400 rootdir != NULL && !vn_is_readonly(rootdir))
5401 spa->spa_async_thread = thread_create(NULL, 0,
5402 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5403 mutex_exit(&spa->spa_async_lock);
5407 spa_async_request(spa_t *spa, int task)
5409 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5410 mutex_enter(&spa->spa_async_lock);
5411 spa->spa_async_tasks |= task;
5412 mutex_exit(&spa->spa_async_lock);
5416 * ==========================================================================
5417 * SPA syncing routines
5418 * ==========================================================================
5422 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5425 bpobj_enqueue(bpo, bp, tx);
5430 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5434 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5440 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5442 char *packed = NULL;
5447 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5450 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5451 * information. This avoids the dbuf_will_dirty() path and
5452 * saves us a pre-read to get data we don't actually care about.
5454 bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
5455 packed = kmem_alloc(bufsize, KM_SLEEP);
5457 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5459 bzero(packed + nvsize, bufsize - nvsize);
5461 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5463 kmem_free(packed, bufsize);
5465 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5466 dmu_buf_will_dirty(db, tx);
5467 *(uint64_t *)db->db_data = nvsize;
5468 dmu_buf_rele(db, FTAG);
5472 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5473 const char *config, const char *entry)
5483 * Update the MOS nvlist describing the list of available devices.
5484 * spa_validate_aux() will have already made sure this nvlist is
5485 * valid and the vdevs are labeled appropriately.
5487 if (sav->sav_object == 0) {
5488 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5489 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5490 sizeof (uint64_t), tx);
5491 VERIFY(zap_update(spa->spa_meta_objset,
5492 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5493 &sav->sav_object, tx) == 0);
5496 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5497 if (sav->sav_count == 0) {
5498 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5500 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5501 for (i = 0; i < sav->sav_count; i++)
5502 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5503 B_FALSE, VDEV_CONFIG_L2CACHE);
5504 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5505 sav->sav_count) == 0);
5506 for (i = 0; i < sav->sav_count; i++)
5507 nvlist_free(list[i]);
5508 kmem_free(list, sav->sav_count * sizeof (void *));
5511 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5512 nvlist_free(nvroot);
5514 sav->sav_sync = B_FALSE;
5518 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5522 if (list_is_empty(&spa->spa_config_dirty_list))
5525 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5527 config = spa_config_generate(spa, spa->spa_root_vdev,
5528 dmu_tx_get_txg(tx), B_FALSE);
5530 spa_config_exit(spa, SCL_STATE, FTAG);
5532 if (spa->spa_config_syncing)
5533 nvlist_free(spa->spa_config_syncing);
5534 spa->spa_config_syncing = config;
5536 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5540 * Set zpool properties.
5543 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5546 objset_t *mos = spa->spa_meta_objset;
5547 nvlist_t *nvp = arg2;
5552 const char *propname;
5553 zprop_type_t proptype;
5555 mutex_enter(&spa->spa_props_lock);
5558 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5559 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
5560 case ZPOOL_PROP_VERSION:
5562 * Only set version for non-zpool-creation cases
5563 * (set/import). spa_create() needs special care
5564 * for version setting.
5566 if (tx->tx_txg != TXG_INITIAL) {
5567 VERIFY(nvpair_value_uint64(elem,
5569 ASSERT(intval <= SPA_VERSION);
5570 ASSERT(intval >= spa_version(spa));
5571 spa->spa_uberblock.ub_version = intval;
5572 vdev_config_dirty(spa->spa_root_vdev);
5576 case ZPOOL_PROP_ALTROOT:
5578 * 'altroot' is a non-persistent property. It should
5579 * have been set temporarily at creation or import time.
5581 ASSERT(spa->spa_root != NULL);
5584 case ZPOOL_PROP_READONLY:
5585 case ZPOOL_PROP_CACHEFILE:
5587 * 'readonly' and 'cachefile' are also non-persisitent
5591 case ZPOOL_PROP_COMMENT:
5592 VERIFY(nvpair_value_string(elem, &strval) == 0);
5593 if (spa->spa_comment != NULL)
5594 spa_strfree(spa->spa_comment);
5595 spa->spa_comment = spa_strdup(strval);
5597 * We need to dirty the configuration on all the vdevs
5598 * so that their labels get updated. It's unnecessary
5599 * to do this for pool creation since the vdev's
5600 * configuratoin has already been dirtied.
5602 if (tx->tx_txg != TXG_INITIAL)
5603 vdev_config_dirty(spa->spa_root_vdev);
5607 * Set pool property values in the poolprops mos object.
5609 if (spa->spa_pool_props_object == 0) {
5610 VERIFY((spa->spa_pool_props_object =
5611 zap_create(mos, DMU_OT_POOL_PROPS,
5612 DMU_OT_NONE, 0, tx)) > 0);
5614 VERIFY(zap_update(mos,
5615 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5616 8, 1, &spa->spa_pool_props_object, tx)
5620 /* normalize the property name */
5621 propname = zpool_prop_to_name(prop);
5622 proptype = zpool_prop_get_type(prop);
5624 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5625 ASSERT(proptype == PROP_TYPE_STRING);
5626 VERIFY(nvpair_value_string(elem, &strval) == 0);
5627 VERIFY(zap_update(mos,
5628 spa->spa_pool_props_object, propname,
5629 1, strlen(strval) + 1, strval, tx) == 0);
5631 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5632 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5634 if (proptype == PROP_TYPE_INDEX) {
5636 VERIFY(zpool_prop_index_to_string(
5637 prop, intval, &unused) == 0);
5639 VERIFY(zap_update(mos,
5640 spa->spa_pool_props_object, propname,
5641 8, 1, &intval, tx) == 0);
5643 ASSERT(0); /* not allowed */
5647 case ZPOOL_PROP_DELEGATION:
5648 spa->spa_delegation = intval;
5650 case ZPOOL_PROP_BOOTFS:
5651 spa->spa_bootfs = intval;
5653 case ZPOOL_PROP_FAILUREMODE:
5654 spa->spa_failmode = intval;
5656 case ZPOOL_PROP_AUTOEXPAND:
5657 spa->spa_autoexpand = intval;
5658 if (tx->tx_txg != TXG_INITIAL)
5659 spa_async_request(spa,
5660 SPA_ASYNC_AUTOEXPAND);
5662 case ZPOOL_PROP_DEDUPDITTO:
5663 spa->spa_dedup_ditto = intval;
5670 /* log internal history if this is not a zpool create */
5671 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5672 tx->tx_txg != TXG_INITIAL) {
5673 spa_history_log_internal(LOG_POOL_PROPSET,
5674 spa, tx, "%s %lld %s",
5675 nvpair_name(elem), intval, spa_name(spa));
5679 mutex_exit(&spa->spa_props_lock);
5683 * Perform one-time upgrade on-disk changes. spa_version() does not
5684 * reflect the new version this txg, so there must be no changes this
5685 * txg to anything that the upgrade code depends on after it executes.
5686 * Therefore this must be called after dsl_pool_sync() does the sync
5690 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5692 dsl_pool_t *dp = spa->spa_dsl_pool;
5694 ASSERT(spa->spa_sync_pass == 1);
5696 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5697 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5698 dsl_pool_create_origin(dp, tx);
5700 /* Keeping the origin open increases spa_minref */
5701 spa->spa_minref += 3;
5704 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5705 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5706 dsl_pool_upgrade_clones(dp, tx);
5709 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5710 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5711 dsl_pool_upgrade_dir_clones(dp, tx);
5713 /* Keeping the freedir open increases spa_minref */
5714 spa->spa_minref += 3;
5719 * Sync the specified transaction group. New blocks may be dirtied as
5720 * part of the process, so we iterate until it converges.
5723 spa_sync(spa_t *spa, uint64_t txg)
5725 dsl_pool_t *dp = spa->spa_dsl_pool;
5726 objset_t *mos = spa->spa_meta_objset;
5727 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
5728 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
5729 vdev_t *rvd = spa->spa_root_vdev;
5734 VERIFY(spa_writeable(spa));
5737 * Lock out configuration changes.
5739 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5741 spa->spa_syncing_txg = txg;
5742 spa->spa_sync_pass = 0;
5745 * If there are any pending vdev state changes, convert them
5746 * into config changes that go out with this transaction group.
5748 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5749 while (list_head(&spa->spa_state_dirty_list) != NULL) {
5751 * We need the write lock here because, for aux vdevs,
5752 * calling vdev_config_dirty() modifies sav_config.
5753 * This is ugly and will become unnecessary when we
5754 * eliminate the aux vdev wart by integrating all vdevs
5755 * into the root vdev tree.
5757 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5758 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
5759 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
5760 vdev_state_clean(vd);
5761 vdev_config_dirty(vd);
5763 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5764 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
5766 spa_config_exit(spa, SCL_STATE, FTAG);
5768 tx = dmu_tx_create_assigned(dp, txg);
5771 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5772 * set spa_deflate if we have no raid-z vdevs.
5774 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
5775 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
5778 for (i = 0; i < rvd->vdev_children; i++) {
5779 vd = rvd->vdev_child[i];
5780 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
5783 if (i == rvd->vdev_children) {
5784 spa->spa_deflate = TRUE;
5785 VERIFY(0 == zap_add(spa->spa_meta_objset,
5786 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
5787 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
5792 * If anything has changed in this txg, or if someone is waiting
5793 * for this txg to sync (eg, spa_vdev_remove()), push the
5794 * deferred frees from the previous txg. If not, leave them
5795 * alone so that we don't generate work on an otherwise idle
5798 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
5799 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
5800 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
5801 ((dsl_scan_active(dp->dp_scan) ||
5802 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
5803 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5804 VERIFY3U(bpobj_iterate(defer_bpo,
5805 spa_free_sync_cb, zio, tx), ==, 0);
5806 VERIFY3U(zio_wait(zio), ==, 0);
5810 * Iterate to convergence.
5813 int pass = ++spa->spa_sync_pass;
5815 spa_sync_config_object(spa, tx);
5816 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
5817 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
5818 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
5819 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
5820 spa_errlog_sync(spa, txg);
5821 dsl_pool_sync(dp, txg);
5823 if (pass <= SYNC_PASS_DEFERRED_FREE) {
5824 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5825 bplist_iterate(free_bpl, spa_free_sync_cb,
5827 VERIFY(zio_wait(zio) == 0);
5829 bplist_iterate(free_bpl, bpobj_enqueue_cb,
5834 dsl_scan_sync(dp, tx);
5836 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
5840 spa_sync_upgrades(spa, tx);
5842 } while (dmu_objset_is_dirty(mos, txg));
5845 * Rewrite the vdev configuration (which includes the uberblock)
5846 * to commit the transaction group.
5848 * If there are no dirty vdevs, we sync the uberblock to a few
5849 * random top-level vdevs that are known to be visible in the
5850 * config cache (see spa_vdev_add() for a complete description).
5851 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5855 * We hold SCL_STATE to prevent vdev open/close/etc.
5856 * while we're attempting to write the vdev labels.
5858 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5860 if (list_is_empty(&spa->spa_config_dirty_list)) {
5861 vdev_t *svd[SPA_DVAS_PER_BP];
5863 int children = rvd->vdev_children;
5864 int c0 = spa_get_random(children);
5866 for (int c = 0; c < children; c++) {
5867 vd = rvd->vdev_child[(c0 + c) % children];
5868 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
5870 svd[svdcount++] = vd;
5871 if (svdcount == SPA_DVAS_PER_BP)
5874 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
5876 error = vdev_config_sync(svd, svdcount, txg,
5879 error = vdev_config_sync(rvd->vdev_child,
5880 rvd->vdev_children, txg, B_FALSE);
5882 error = vdev_config_sync(rvd->vdev_child,
5883 rvd->vdev_children, txg, B_TRUE);
5886 spa_config_exit(spa, SCL_STATE, FTAG);
5890 zio_suspend(spa, NULL);
5891 zio_resume_wait(spa);
5896 * Clear the dirty config list.
5898 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
5899 vdev_config_clean(vd);
5902 * Now that the new config has synced transactionally,
5903 * let it become visible to the config cache.
5905 if (spa->spa_config_syncing != NULL) {
5906 spa_config_set(spa, spa->spa_config_syncing);
5907 spa->spa_config_txg = txg;
5908 spa->spa_config_syncing = NULL;
5911 spa->spa_ubsync = spa->spa_uberblock;
5913 dsl_pool_sync_done(dp, txg);
5916 * Update usable space statistics.
5918 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5919 vdev_sync_done(vd, txg);
5921 spa_update_dspace(spa);
5924 * It had better be the case that we didn't dirty anything
5925 * since vdev_config_sync().
5927 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5928 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5929 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
5931 spa->spa_sync_pass = 0;
5933 spa_config_exit(spa, SCL_CONFIG, FTAG);
5935 spa_handle_ignored_writes(spa);
5938 * If any async tasks have been requested, kick them off.
5940 spa_async_dispatch(spa);
5944 * Sync all pools. We don't want to hold the namespace lock across these
5945 * operations, so we take a reference on the spa_t and drop the lock during the
5949 spa_sync_allpools(void)
5952 mutex_enter(&spa_namespace_lock);
5953 while ((spa = spa_next(spa)) != NULL) {
5954 if (spa_state(spa) != POOL_STATE_ACTIVE ||
5955 !spa_writeable(spa) || spa_suspended(spa))
5957 spa_open_ref(spa, FTAG);
5958 mutex_exit(&spa_namespace_lock);
5959 txg_wait_synced(spa_get_dsl(spa), 0);
5960 mutex_enter(&spa_namespace_lock);
5961 spa_close(spa, FTAG);
5963 mutex_exit(&spa_namespace_lock);
5967 * ==========================================================================
5968 * Miscellaneous routines
5969 * ==========================================================================
5973 * Remove all pools in the system.
5981 * Remove all cached state. All pools should be closed now,
5982 * so every spa in the AVL tree should be unreferenced.
5984 mutex_enter(&spa_namespace_lock);
5985 while ((spa = spa_next(NULL)) != NULL) {
5987 * Stop async tasks. The async thread may need to detach
5988 * a device that's been replaced, which requires grabbing
5989 * spa_namespace_lock, so we must drop it here.
5991 spa_open_ref(spa, FTAG);
5992 mutex_exit(&spa_namespace_lock);
5993 spa_async_suspend(spa);
5994 mutex_enter(&spa_namespace_lock);
5995 spa_close(spa, FTAG);
5997 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5999 spa_deactivate(spa);
6003 mutex_exit(&spa_namespace_lock);
6007 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6012 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6016 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6017 vd = spa->spa_l2cache.sav_vdevs[i];
6018 if (vd->vdev_guid == guid)
6022 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6023 vd = spa->spa_spares.sav_vdevs[i];
6024 if (vd->vdev_guid == guid)
6033 spa_upgrade(spa_t *spa, uint64_t version)
6035 ASSERT(spa_writeable(spa));
6037 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6040 * This should only be called for a non-faulted pool, and since a
6041 * future version would result in an unopenable pool, this shouldn't be
6044 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
6045 ASSERT(version >= spa->spa_uberblock.ub_version);
6047 spa->spa_uberblock.ub_version = version;
6048 vdev_config_dirty(spa->spa_root_vdev);
6050 spa_config_exit(spa, SCL_ALL, FTAG);
6052 txg_wait_synced(spa_get_dsl(spa), 0);
6056 spa_has_spare(spa_t *spa, uint64_t guid)
6060 spa_aux_vdev_t *sav = &spa->spa_spares;
6062 for (i = 0; i < sav->sav_count; i++)
6063 if (sav->sav_vdevs[i]->vdev_guid == guid)
6066 for (i = 0; i < sav->sav_npending; i++) {
6067 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6068 &spareguid) == 0 && spareguid == guid)
6076 * Check if a pool has an active shared spare device.
6077 * Note: reference count of an active spare is 2, as a spare and as a replace
6080 spa_has_active_shared_spare(spa_t *spa)
6084 spa_aux_vdev_t *sav = &spa->spa_spares;
6086 for (i = 0; i < sav->sav_count; i++) {
6087 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6088 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6097 * Post a sysevent corresponding to the given event. The 'name' must be one of
6098 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6099 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6100 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6101 * or zdb as real changes.
6104 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6108 sysevent_attr_list_t *attr = NULL;
6109 sysevent_value_t value;
6112 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6115 value.value_type = SE_DATA_TYPE_STRING;
6116 value.value.sv_string = spa_name(spa);
6117 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6120 value.value_type = SE_DATA_TYPE_UINT64;
6121 value.value.sv_uint64 = spa_guid(spa);
6122 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6126 value.value_type = SE_DATA_TYPE_UINT64;
6127 value.value.sv_uint64 = vd->vdev_guid;
6128 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6132 if (vd->vdev_path) {
6133 value.value_type = SE_DATA_TYPE_STRING;
6134 value.value.sv_string = vd->vdev_path;
6135 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6136 &value, SE_SLEEP) != 0)
6141 if (sysevent_attach_attributes(ev, attr) != 0)
6145 (void) log_sysevent(ev, SE_SLEEP, &eid);
6149 sysevent_free_attr(attr);