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.
27 * This file contains all the routines used when modifying on-disk SPA state.
28 * This includes opening, importing, destroying, exporting a pool, and syncing a
32 #include <sys/zfs_context.h>
33 #include <sys/fm/fs/zfs.h>
34 #include <sys/spa_impl.h>
36 #include <sys/zio_checksum.h>
38 #include <sys/dmu_tx.h>
42 #include <sys/vdev_impl.h>
43 #include <sys/metaslab.h>
44 #include <sys/metaslab_impl.h>
45 #include <sys/uberblock_impl.h>
48 #include <sys/dmu_traverse.h>
49 #include <sys/dmu_objset.h>
50 #include <sys/unique.h>
51 #include <sys/dsl_pool.h>
52 #include <sys/dsl_dataset.h>
53 #include <sys/dsl_dir.h>
54 #include <sys/dsl_prop.h>
55 #include <sys/dsl_synctask.h>
56 #include <sys/fs/zfs.h>
58 #include <sys/callb.h>
59 #include <sys/spa_boot.h>
60 #include <sys/zfs_ioctl.h>
61 #include <sys/dsl_scan.h>
65 #include <sys/callb.h>
66 #include <sys/cpupart.h>
71 #include "zfs_comutil.h"
73 /* Check hostid on import? */
74 static int check_hostid = 1;
76 SYSCTL_DECL(_vfs_zfs);
77 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
78 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
79 "Check hostid on import?");
81 typedef enum zti_modes {
82 zti_mode_fixed, /* value is # of threads (min 1) */
83 zti_mode_online_percent, /* value is % of online CPUs */
84 zti_mode_batch, /* cpu-intensive; value is ignored */
85 zti_mode_null, /* don't create a taskq */
89 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
90 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
91 #define ZTI_BATCH { zti_mode_batch, 0 }
92 #define ZTI_NULL { zti_mode_null, 0 }
94 #define ZTI_ONE ZTI_FIX(1)
96 typedef struct zio_taskq_info {
97 enum zti_modes zti_mode;
101 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
102 "issue", "issue_high", "intr", "intr_high"
106 * Define the taskq threads for the following I/O types:
107 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
109 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
110 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
111 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
112 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
113 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
114 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
115 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
116 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
119 static dsl_syncfunc_t spa_sync_props;
120 static boolean_t spa_has_active_shared_spare(spa_t *spa);
121 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
122 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
124 static void spa_vdev_resilver_done(spa_t *spa);
126 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
128 id_t zio_taskq_psrset_bind = PS_NONE;
131 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
133 uint_t zio_taskq_basedc = 80; /* base duty cycle */
135 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
138 * This (illegal) pool name is used when temporarily importing a spa_t in order
139 * to get the vdev stats associated with the imported devices.
141 #define TRYIMPORT_NAME "$import"
144 * ==========================================================================
145 * SPA properties routines
146 * ==========================================================================
150 * Add a (source=src, propname=propval) list to an nvlist.
153 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
154 uint64_t intval, zprop_source_t src)
156 const char *propname = zpool_prop_to_name(prop);
159 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
160 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
163 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
165 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
167 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
168 nvlist_free(propval);
172 * Get property values from the spa configuration.
175 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
179 uint64_t cap, version;
180 zprop_source_t src = ZPROP_SRC_NONE;
181 spa_config_dirent_t *dp;
183 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
185 if (spa->spa_root_vdev != NULL) {
186 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
187 size = metaslab_class_get_space(spa_normal_class(spa));
188 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
189 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
190 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
191 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
193 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
194 (spa_mode(spa) == FREAD), src);
196 cap = (size == 0) ? 0 : (alloc * 100 / size);
197 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
199 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
200 ddt_get_pool_dedup_ratio(spa), src);
202 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
203 spa->spa_root_vdev->vdev_state, src);
205 version = spa_version(spa);
206 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
207 src = ZPROP_SRC_DEFAULT;
209 src = ZPROP_SRC_LOCAL;
210 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
213 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
215 if (spa->spa_root != NULL)
216 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
219 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
220 if (dp->scd_path == NULL) {
221 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
222 "none", 0, ZPROP_SRC_LOCAL);
223 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
224 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
225 dp->scd_path, 0, ZPROP_SRC_LOCAL);
231 * Get zpool property values.
234 spa_prop_get(spa_t *spa, nvlist_t **nvp)
236 objset_t *mos = spa->spa_meta_objset;
241 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
243 mutex_enter(&spa->spa_props_lock);
246 * Get properties from the spa config.
248 spa_prop_get_config(spa, nvp);
250 /* If no pool property object, no more prop to get. */
251 if (mos == NULL || spa->spa_pool_props_object == 0) {
252 mutex_exit(&spa->spa_props_lock);
257 * Get properties from the MOS pool property object.
259 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
260 (err = zap_cursor_retrieve(&zc, &za)) == 0;
261 zap_cursor_advance(&zc)) {
264 zprop_source_t src = ZPROP_SRC_DEFAULT;
267 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
270 switch (za.za_integer_length) {
272 /* integer property */
273 if (za.za_first_integer !=
274 zpool_prop_default_numeric(prop))
275 src = ZPROP_SRC_LOCAL;
277 if (prop == ZPOOL_PROP_BOOTFS) {
279 dsl_dataset_t *ds = NULL;
281 dp = spa_get_dsl(spa);
282 rw_enter(&dp->dp_config_rwlock, RW_READER);
283 if (err = dsl_dataset_hold_obj(dp,
284 za.za_first_integer, FTAG, &ds)) {
285 rw_exit(&dp->dp_config_rwlock);
290 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
292 dsl_dataset_name(ds, strval);
293 dsl_dataset_rele(ds, FTAG);
294 rw_exit(&dp->dp_config_rwlock);
297 intval = za.za_first_integer;
300 spa_prop_add_list(*nvp, prop, strval, intval, src);
304 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
309 /* string property */
310 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
311 err = zap_lookup(mos, spa->spa_pool_props_object,
312 za.za_name, 1, za.za_num_integers, strval);
314 kmem_free(strval, za.za_num_integers);
317 spa_prop_add_list(*nvp, prop, strval, 0, src);
318 kmem_free(strval, za.za_num_integers);
325 zap_cursor_fini(&zc);
326 mutex_exit(&spa->spa_props_lock);
328 if (err && err != ENOENT) {
338 * Validate the given pool properties nvlist and modify the list
339 * for the property values to be set.
342 spa_prop_validate(spa_t *spa, nvlist_t *props)
345 int error = 0, reset_bootfs = 0;
349 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
351 char *propname, *strval;
356 propname = nvpair_name(elem);
358 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
362 case ZPOOL_PROP_VERSION:
363 error = nvpair_value_uint64(elem, &intval);
365 (intval < spa_version(spa) || intval > SPA_VERSION))
369 case ZPOOL_PROP_DELEGATION:
370 case ZPOOL_PROP_AUTOREPLACE:
371 case ZPOOL_PROP_LISTSNAPS:
372 case ZPOOL_PROP_AUTOEXPAND:
373 error = nvpair_value_uint64(elem, &intval);
374 if (!error && intval > 1)
378 case ZPOOL_PROP_BOOTFS:
380 * If the pool version is less than SPA_VERSION_BOOTFS,
381 * or the pool is still being created (version == 0),
382 * the bootfs property cannot be set.
384 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
390 * Make sure the vdev config is bootable
392 if (!vdev_is_bootable(spa->spa_root_vdev)) {
399 error = nvpair_value_string(elem, &strval);
404 if (strval == NULL || strval[0] == '\0') {
405 objnum = zpool_prop_default_numeric(
410 if (error = dmu_objset_hold(strval, FTAG, &os))
413 /* Must be ZPL and not gzip compressed. */
415 if (dmu_objset_type(os) != DMU_OST_ZFS) {
417 } else if ((error = dsl_prop_get_integer(strval,
418 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
419 &compress, NULL)) == 0 &&
420 !BOOTFS_COMPRESS_VALID(compress)) {
423 objnum = dmu_objset_id(os);
425 dmu_objset_rele(os, FTAG);
429 case ZPOOL_PROP_FAILUREMODE:
430 error = nvpair_value_uint64(elem, &intval);
431 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
432 intval > ZIO_FAILURE_MODE_PANIC))
436 * This is a special case which only occurs when
437 * the pool has completely failed. This allows
438 * the user to change the in-core failmode property
439 * without syncing it out to disk (I/Os might
440 * currently be blocked). We do this by returning
441 * EIO to the caller (spa_prop_set) to trick it
442 * into thinking we encountered a property validation
445 if (!error && spa_suspended(spa)) {
446 spa->spa_failmode = intval;
451 case ZPOOL_PROP_CACHEFILE:
452 if ((error = nvpair_value_string(elem, &strval)) != 0)
455 if (strval[0] == '\0')
458 if (strcmp(strval, "none") == 0)
461 if (strval[0] != '/') {
466 slash = strrchr(strval, '/');
467 ASSERT(slash != NULL);
469 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
470 strcmp(slash, "/..") == 0)
474 case ZPOOL_PROP_DEDUPDITTO:
475 if (spa_version(spa) < SPA_VERSION_DEDUP)
478 error = nvpair_value_uint64(elem, &intval);
480 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
489 if (!error && reset_bootfs) {
490 error = nvlist_remove(props,
491 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
494 error = nvlist_add_uint64(props,
495 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
503 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
506 spa_config_dirent_t *dp;
508 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
512 dp = kmem_alloc(sizeof (spa_config_dirent_t),
515 if (cachefile[0] == '\0')
516 dp->scd_path = spa_strdup(spa_config_path);
517 else if (strcmp(cachefile, "none") == 0)
520 dp->scd_path = spa_strdup(cachefile);
522 list_insert_head(&spa->spa_config_list, dp);
524 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
528 spa_prop_set(spa_t *spa, nvlist_t *nvp)
532 boolean_t need_sync = B_FALSE;
535 if ((error = spa_prop_validate(spa, nvp)) != 0)
539 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
540 if ((prop = zpool_name_to_prop(
541 nvpair_name(elem))) == ZPROP_INVAL)
544 if (prop == ZPOOL_PROP_CACHEFILE ||
545 prop == ZPOOL_PROP_ALTROOT ||
546 prop == ZPOOL_PROP_READONLY)
554 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
561 * If the bootfs property value is dsobj, clear it.
564 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
566 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
567 VERIFY(zap_remove(spa->spa_meta_objset,
568 spa->spa_pool_props_object,
569 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
575 * ==========================================================================
576 * SPA state manipulation (open/create/destroy/import/export)
577 * ==========================================================================
581 spa_error_entry_compare(const void *a, const void *b)
583 spa_error_entry_t *sa = (spa_error_entry_t *)a;
584 spa_error_entry_t *sb = (spa_error_entry_t *)b;
587 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
588 sizeof (zbookmark_t));
599 * Utility function which retrieves copies of the current logs and
600 * re-initializes them in the process.
603 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
605 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
607 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
608 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
610 avl_create(&spa->spa_errlist_scrub,
611 spa_error_entry_compare, sizeof (spa_error_entry_t),
612 offsetof(spa_error_entry_t, se_avl));
613 avl_create(&spa->spa_errlist_last,
614 spa_error_entry_compare, sizeof (spa_error_entry_t),
615 offsetof(spa_error_entry_t, se_avl));
619 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
622 uint_t flags = TASKQ_PREPOPULATE;
623 boolean_t batch = B_FALSE;
627 return (NULL); /* no taskq needed */
630 ASSERT3U(value, >=, 1);
631 value = MAX(value, 1);
636 flags |= TASKQ_THREADS_CPU_PCT;
637 value = zio_taskq_batch_pct;
640 case zti_mode_online_percent:
641 flags |= TASKQ_THREADS_CPU_PCT;
645 panic("unrecognized mode for %s taskq (%u:%u) in "
652 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
654 flags |= TASKQ_DC_BATCH;
656 return (taskq_create_sysdc(name, value, 50, INT_MAX,
657 spa->spa_proc, zio_taskq_basedc, flags));
660 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
661 spa->spa_proc, flags));
665 spa_create_zio_taskqs(spa_t *spa)
667 for (int t = 0; t < ZIO_TYPES; t++) {
668 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
669 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
670 enum zti_modes mode = ztip->zti_mode;
671 uint_t value = ztip->zti_value;
674 (void) snprintf(name, sizeof (name),
675 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
677 spa->spa_zio_taskq[t][q] =
678 spa_taskq_create(spa, name, mode, value);
686 spa_thread(void *arg)
691 user_t *pu = PTOU(curproc);
693 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
696 ASSERT(curproc != &p0);
697 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
698 "zpool-%s", spa->spa_name);
699 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
702 /* bind this thread to the requested psrset */
703 if (zio_taskq_psrset_bind != PS_NONE) {
705 mutex_enter(&cpu_lock);
706 mutex_enter(&pidlock);
707 mutex_enter(&curproc->p_lock);
709 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
710 0, NULL, NULL) == 0) {
711 curthread->t_bind_pset = zio_taskq_psrset_bind;
714 "Couldn't bind process for zfs pool \"%s\" to "
715 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
718 mutex_exit(&curproc->p_lock);
719 mutex_exit(&pidlock);
720 mutex_exit(&cpu_lock);
726 if (zio_taskq_sysdc) {
727 sysdc_thread_enter(curthread, 100, 0);
731 spa->spa_proc = curproc;
732 spa->spa_did = curthread->t_did;
734 spa_create_zio_taskqs(spa);
736 mutex_enter(&spa->spa_proc_lock);
737 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
739 spa->spa_proc_state = SPA_PROC_ACTIVE;
740 cv_broadcast(&spa->spa_proc_cv);
742 CALLB_CPR_SAFE_BEGIN(&cprinfo);
743 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
744 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
745 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
747 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
748 spa->spa_proc_state = SPA_PROC_GONE;
750 cv_broadcast(&spa->spa_proc_cv);
751 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
753 mutex_enter(&curproc->p_lock);
756 #endif /* SPA_PROCESS */
760 * Activate an uninitialized pool.
763 spa_activate(spa_t *spa, int mode)
765 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
767 spa->spa_state = POOL_STATE_ACTIVE;
768 spa->spa_mode = mode;
770 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
771 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
773 /* Try to create a covering process */
774 mutex_enter(&spa->spa_proc_lock);
775 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
776 ASSERT(spa->spa_proc == &p0);
780 /* Only create a process if we're going to be around a while. */
781 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
782 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
784 spa->spa_proc_state = SPA_PROC_CREATED;
785 while (spa->spa_proc_state == SPA_PROC_CREATED) {
786 cv_wait(&spa->spa_proc_cv,
787 &spa->spa_proc_lock);
789 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
790 ASSERT(spa->spa_proc != &p0);
791 ASSERT(spa->spa_did != 0);
795 "Couldn't create process for zfs pool \"%s\"\n",
800 #endif /* SPA_PROCESS */
801 mutex_exit(&spa->spa_proc_lock);
803 /* If we didn't create a process, we need to create our taskqs. */
804 ASSERT(spa->spa_proc == &p0);
805 if (spa->spa_proc == &p0) {
806 spa_create_zio_taskqs(spa);
809 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
810 offsetof(vdev_t, vdev_config_dirty_node));
811 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
812 offsetof(vdev_t, vdev_state_dirty_node));
814 txg_list_create(&spa->spa_vdev_txg_list,
815 offsetof(struct vdev, vdev_txg_node));
817 avl_create(&spa->spa_errlist_scrub,
818 spa_error_entry_compare, sizeof (spa_error_entry_t),
819 offsetof(spa_error_entry_t, se_avl));
820 avl_create(&spa->spa_errlist_last,
821 spa_error_entry_compare, sizeof (spa_error_entry_t),
822 offsetof(spa_error_entry_t, se_avl));
826 * Opposite of spa_activate().
829 spa_deactivate(spa_t *spa)
831 ASSERT(spa->spa_sync_on == B_FALSE);
832 ASSERT(spa->spa_dsl_pool == NULL);
833 ASSERT(spa->spa_root_vdev == NULL);
834 ASSERT(spa->spa_async_zio_root == NULL);
835 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
837 txg_list_destroy(&spa->spa_vdev_txg_list);
839 list_destroy(&spa->spa_config_dirty_list);
840 list_destroy(&spa->spa_state_dirty_list);
842 for (int t = 0; t < ZIO_TYPES; t++) {
843 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
844 if (spa->spa_zio_taskq[t][q] != NULL)
845 taskq_destroy(spa->spa_zio_taskq[t][q]);
846 spa->spa_zio_taskq[t][q] = NULL;
850 metaslab_class_destroy(spa->spa_normal_class);
851 spa->spa_normal_class = NULL;
853 metaslab_class_destroy(spa->spa_log_class);
854 spa->spa_log_class = NULL;
857 * If this was part of an import or the open otherwise failed, we may
858 * still have errors left in the queues. Empty them just in case.
860 spa_errlog_drain(spa);
862 avl_destroy(&spa->spa_errlist_scrub);
863 avl_destroy(&spa->spa_errlist_last);
865 spa->spa_state = POOL_STATE_UNINITIALIZED;
867 mutex_enter(&spa->spa_proc_lock);
868 if (spa->spa_proc_state != SPA_PROC_NONE) {
869 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
870 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
871 cv_broadcast(&spa->spa_proc_cv);
872 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
873 ASSERT(spa->spa_proc != &p0);
874 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
876 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
877 spa->spa_proc_state = SPA_PROC_NONE;
879 ASSERT(spa->spa_proc == &p0);
880 mutex_exit(&spa->spa_proc_lock);
884 * We want to make sure spa_thread() has actually exited the ZFS
885 * module, so that the module can't be unloaded out from underneath
888 if (spa->spa_did != 0) {
889 thread_join(spa->spa_did);
892 #endif /* SPA_PROCESS */
896 * Verify a pool configuration, and construct the vdev tree appropriately. This
897 * will create all the necessary vdevs in the appropriate layout, with each vdev
898 * in the CLOSED state. This will prep the pool before open/creation/import.
899 * All vdev validation is done by the vdev_alloc() routine.
902 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
903 uint_t id, int atype)
909 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
912 if ((*vdp)->vdev_ops->vdev_op_leaf)
915 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
927 for (int c = 0; c < children; c++) {
929 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
937 ASSERT(*vdp != NULL);
943 * Opposite of spa_load().
946 spa_unload(spa_t *spa)
950 ASSERT(MUTEX_HELD(&spa_namespace_lock));
955 spa_async_suspend(spa);
960 if (spa->spa_sync_on) {
961 txg_sync_stop(spa->spa_dsl_pool);
962 spa->spa_sync_on = B_FALSE;
966 * Wait for any outstanding async I/O to complete.
968 if (spa->spa_async_zio_root != NULL) {
969 (void) zio_wait(spa->spa_async_zio_root);
970 spa->spa_async_zio_root = NULL;
973 bpobj_close(&spa->spa_deferred_bpobj);
976 * Close the dsl pool.
978 if (spa->spa_dsl_pool) {
979 dsl_pool_close(spa->spa_dsl_pool);
980 spa->spa_dsl_pool = NULL;
981 spa->spa_meta_objset = NULL;
986 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
989 * Drop and purge level 2 cache
991 spa_l2cache_drop(spa);
996 if (spa->spa_root_vdev)
997 vdev_free(spa->spa_root_vdev);
998 ASSERT(spa->spa_root_vdev == NULL);
1000 for (i = 0; i < spa->spa_spares.sav_count; i++)
1001 vdev_free(spa->spa_spares.sav_vdevs[i]);
1002 if (spa->spa_spares.sav_vdevs) {
1003 kmem_free(spa->spa_spares.sav_vdevs,
1004 spa->spa_spares.sav_count * sizeof (void *));
1005 spa->spa_spares.sav_vdevs = NULL;
1007 if (spa->spa_spares.sav_config) {
1008 nvlist_free(spa->spa_spares.sav_config);
1009 spa->spa_spares.sav_config = NULL;
1011 spa->spa_spares.sav_count = 0;
1013 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
1014 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1015 if (spa->spa_l2cache.sav_vdevs) {
1016 kmem_free(spa->spa_l2cache.sav_vdevs,
1017 spa->spa_l2cache.sav_count * sizeof (void *));
1018 spa->spa_l2cache.sav_vdevs = NULL;
1020 if (spa->spa_l2cache.sav_config) {
1021 nvlist_free(spa->spa_l2cache.sav_config);
1022 spa->spa_l2cache.sav_config = NULL;
1024 spa->spa_l2cache.sav_count = 0;
1026 spa->spa_async_suspended = 0;
1028 spa_config_exit(spa, SCL_ALL, FTAG);
1032 * Load (or re-load) the current list of vdevs describing the active spares for
1033 * this pool. When this is called, we have some form of basic information in
1034 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1035 * then re-generate a more complete list including status information.
1038 spa_load_spares(spa_t *spa)
1045 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1048 * First, close and free any existing spare vdevs.
1050 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1051 vd = spa->spa_spares.sav_vdevs[i];
1053 /* Undo the call to spa_activate() below */
1054 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1055 B_FALSE)) != NULL && tvd->vdev_isspare)
1056 spa_spare_remove(tvd);
1061 if (spa->spa_spares.sav_vdevs)
1062 kmem_free(spa->spa_spares.sav_vdevs,
1063 spa->spa_spares.sav_count * sizeof (void *));
1065 if (spa->spa_spares.sav_config == NULL)
1068 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1069 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1071 spa->spa_spares.sav_count = (int)nspares;
1072 spa->spa_spares.sav_vdevs = NULL;
1078 * Construct the array of vdevs, opening them to get status in the
1079 * process. For each spare, there is potentially two different vdev_t
1080 * structures associated with it: one in the list of spares (used only
1081 * for basic validation purposes) and one in the active vdev
1082 * configuration (if it's spared in). During this phase we open and
1083 * validate each vdev on the spare list. If the vdev also exists in the
1084 * active configuration, then we also mark this vdev as an active spare.
1086 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1088 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1089 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1090 VDEV_ALLOC_SPARE) == 0);
1093 spa->spa_spares.sav_vdevs[i] = vd;
1095 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1096 B_FALSE)) != NULL) {
1097 if (!tvd->vdev_isspare)
1101 * We only mark the spare active if we were successfully
1102 * able to load the vdev. Otherwise, importing a pool
1103 * with a bad active spare would result in strange
1104 * behavior, because multiple pool would think the spare
1105 * is actively in use.
1107 * There is a vulnerability here to an equally bizarre
1108 * circumstance, where a dead active spare is later
1109 * brought back to life (onlined or otherwise). Given
1110 * the rarity of this scenario, and the extra complexity
1111 * it adds, we ignore the possibility.
1113 if (!vdev_is_dead(tvd))
1114 spa_spare_activate(tvd);
1118 vd->vdev_aux = &spa->spa_spares;
1120 if (vdev_open(vd) != 0)
1123 if (vdev_validate_aux(vd) == 0)
1128 * Recompute the stashed list of spares, with status information
1131 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1132 DATA_TYPE_NVLIST_ARRAY) == 0);
1134 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1136 for (i = 0; i < spa->spa_spares.sav_count; i++)
1137 spares[i] = vdev_config_generate(spa,
1138 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1139 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1140 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1141 for (i = 0; i < spa->spa_spares.sav_count; i++)
1142 nvlist_free(spares[i]);
1143 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1147 * Load (or re-load) the current list of vdevs describing the active l2cache for
1148 * this pool. When this is called, we have some form of basic information in
1149 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1150 * then re-generate a more complete list including status information.
1151 * Devices which are already active have their details maintained, and are
1155 spa_load_l2cache(spa_t *spa)
1159 int i, j, oldnvdevs;
1161 vdev_t *vd, **oldvdevs, **newvdevs;
1162 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1164 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1166 if (sav->sav_config != NULL) {
1167 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1168 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1169 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1174 oldvdevs = sav->sav_vdevs;
1175 oldnvdevs = sav->sav_count;
1176 sav->sav_vdevs = NULL;
1180 * Process new nvlist of vdevs.
1182 for (i = 0; i < nl2cache; i++) {
1183 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1187 for (j = 0; j < oldnvdevs; j++) {
1189 if (vd != NULL && guid == vd->vdev_guid) {
1191 * Retain previous vdev for add/remove ops.
1199 if (newvdevs[i] == NULL) {
1203 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1204 VDEV_ALLOC_L2CACHE) == 0);
1209 * Commit this vdev as an l2cache device,
1210 * even if it fails to open.
1212 spa_l2cache_add(vd);
1217 spa_l2cache_activate(vd);
1219 if (vdev_open(vd) != 0)
1222 (void) vdev_validate_aux(vd);
1224 if (!vdev_is_dead(vd))
1225 l2arc_add_vdev(spa, vd);
1230 * Purge vdevs that were dropped
1232 for (i = 0; i < oldnvdevs; i++) {
1237 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1238 pool != 0ULL && l2arc_vdev_present(vd))
1239 l2arc_remove_vdev(vd);
1240 (void) vdev_close(vd);
1241 spa_l2cache_remove(vd);
1246 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1248 if (sav->sav_config == NULL)
1251 sav->sav_vdevs = newvdevs;
1252 sav->sav_count = (int)nl2cache;
1255 * Recompute the stashed list of l2cache devices, with status
1256 * information this time.
1258 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1259 DATA_TYPE_NVLIST_ARRAY) == 0);
1261 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1262 for (i = 0; i < sav->sav_count; i++)
1263 l2cache[i] = vdev_config_generate(spa,
1264 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1265 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1266 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1268 for (i = 0; i < sav->sav_count; i++)
1269 nvlist_free(l2cache[i]);
1271 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1275 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1278 char *packed = NULL;
1283 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1284 nvsize = *(uint64_t *)db->db_data;
1285 dmu_buf_rele(db, FTAG);
1287 packed = kmem_alloc(nvsize, KM_SLEEP);
1288 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1291 error = nvlist_unpack(packed, nvsize, value, 0);
1292 kmem_free(packed, nvsize);
1298 * Checks to see if the given vdev could not be opened, in which case we post a
1299 * sysevent to notify the autoreplace code that the device has been removed.
1302 spa_check_removed(vdev_t *vd)
1304 for (int c = 0; c < vd->vdev_children; c++)
1305 spa_check_removed(vd->vdev_child[c]);
1307 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1308 zfs_post_autoreplace(vd->vdev_spa, vd);
1309 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1314 * Validate the current config against the MOS config
1317 spa_config_valid(spa_t *spa, nvlist_t *config)
1319 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1322 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1324 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1325 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1327 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1330 * If we're doing a normal import, then build up any additional
1331 * diagnostic information about missing devices in this config.
1332 * We'll pass this up to the user for further processing.
1334 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1335 nvlist_t **child, *nv;
1338 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1340 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1342 for (int c = 0; c < rvd->vdev_children; c++) {
1343 vdev_t *tvd = rvd->vdev_child[c];
1344 vdev_t *mtvd = mrvd->vdev_child[c];
1346 if (tvd->vdev_ops == &vdev_missing_ops &&
1347 mtvd->vdev_ops != &vdev_missing_ops &&
1349 child[idx++] = vdev_config_generate(spa, mtvd,
1354 VERIFY(nvlist_add_nvlist_array(nv,
1355 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1356 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1357 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1359 for (int i = 0; i < idx; i++)
1360 nvlist_free(child[i]);
1363 kmem_free(child, rvd->vdev_children * sizeof (char **));
1367 * Compare the root vdev tree with the information we have
1368 * from the MOS config (mrvd). Check each top-level vdev
1369 * with the corresponding MOS config top-level (mtvd).
1371 for (int c = 0; c < rvd->vdev_children; c++) {
1372 vdev_t *tvd = rvd->vdev_child[c];
1373 vdev_t *mtvd = mrvd->vdev_child[c];
1376 * Resolve any "missing" vdevs in the current configuration.
1377 * If we find that the MOS config has more accurate information
1378 * about the top-level vdev then use that vdev instead.
1380 if (tvd->vdev_ops == &vdev_missing_ops &&
1381 mtvd->vdev_ops != &vdev_missing_ops) {
1383 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1387 * Device specific actions.
1389 if (mtvd->vdev_islog) {
1390 spa_set_log_state(spa, SPA_LOG_CLEAR);
1393 * XXX - once we have 'readonly' pool
1394 * support we should be able to handle
1395 * missing data devices by transitioning
1396 * the pool to readonly.
1402 * Swap the missing vdev with the data we were
1403 * able to obtain from the MOS config.
1405 vdev_remove_child(rvd, tvd);
1406 vdev_remove_child(mrvd, mtvd);
1408 vdev_add_child(rvd, mtvd);
1409 vdev_add_child(mrvd, tvd);
1411 spa_config_exit(spa, SCL_ALL, FTAG);
1413 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1416 } else if (mtvd->vdev_islog) {
1418 * Load the slog device's state from the MOS config
1419 * since it's possible that the label does not
1420 * contain the most up-to-date information.
1422 vdev_load_log_state(tvd, mtvd);
1427 spa_config_exit(spa, SCL_ALL, FTAG);
1430 * Ensure we were able to validate the config.
1432 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1436 * Check for missing log devices
1439 spa_check_logs(spa_t *spa)
1441 switch (spa->spa_log_state) {
1442 case SPA_LOG_MISSING:
1443 /* need to recheck in case slog has been restored */
1444 case SPA_LOG_UNKNOWN:
1445 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1446 DS_FIND_CHILDREN)) {
1447 spa_set_log_state(spa, SPA_LOG_MISSING);
1456 spa_passivate_log(spa_t *spa)
1458 vdev_t *rvd = spa->spa_root_vdev;
1459 boolean_t slog_found = B_FALSE;
1461 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1463 if (!spa_has_slogs(spa))
1466 for (int c = 0; c < rvd->vdev_children; c++) {
1467 vdev_t *tvd = rvd->vdev_child[c];
1468 metaslab_group_t *mg = tvd->vdev_mg;
1470 if (tvd->vdev_islog) {
1471 metaslab_group_passivate(mg);
1472 slog_found = B_TRUE;
1476 return (slog_found);
1480 spa_activate_log(spa_t *spa)
1482 vdev_t *rvd = spa->spa_root_vdev;
1484 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1486 for (int c = 0; c < rvd->vdev_children; c++) {
1487 vdev_t *tvd = rvd->vdev_child[c];
1488 metaslab_group_t *mg = tvd->vdev_mg;
1490 if (tvd->vdev_islog)
1491 metaslab_group_activate(mg);
1496 spa_offline_log(spa_t *spa)
1500 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1501 NULL, DS_FIND_CHILDREN)) == 0) {
1504 * We successfully offlined the log device, sync out the
1505 * current txg so that the "stubby" block can be removed
1508 txg_wait_synced(spa->spa_dsl_pool, 0);
1514 spa_aux_check_removed(spa_aux_vdev_t *sav)
1518 for (i = 0; i < sav->sav_count; i++)
1519 spa_check_removed(sav->sav_vdevs[i]);
1523 spa_claim_notify(zio_t *zio)
1525 spa_t *spa = zio->io_spa;
1530 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1531 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1532 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1533 mutex_exit(&spa->spa_props_lock);
1536 typedef struct spa_load_error {
1537 uint64_t sle_meta_count;
1538 uint64_t sle_data_count;
1542 spa_load_verify_done(zio_t *zio)
1544 blkptr_t *bp = zio->io_bp;
1545 spa_load_error_t *sle = zio->io_private;
1546 dmu_object_type_t type = BP_GET_TYPE(bp);
1547 int error = zio->io_error;
1550 if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
1551 type != DMU_OT_INTENT_LOG)
1552 atomic_add_64(&sle->sle_meta_count, 1);
1554 atomic_add_64(&sle->sle_data_count, 1);
1556 zio_data_buf_free(zio->io_data, zio->io_size);
1561 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1562 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1566 size_t size = BP_GET_PSIZE(bp);
1567 void *data = zio_data_buf_alloc(size);
1569 zio_nowait(zio_read(rio, spa, bp, data, size,
1570 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1571 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1572 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1578 spa_load_verify(spa_t *spa)
1581 spa_load_error_t sle = { 0 };
1582 zpool_rewind_policy_t policy;
1583 boolean_t verify_ok = B_FALSE;
1586 zpool_get_rewind_policy(spa->spa_config, &policy);
1588 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1591 rio = zio_root(spa, NULL, &sle,
1592 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1594 error = traverse_pool(spa, spa->spa_verify_min_txg,
1595 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1597 (void) zio_wait(rio);
1599 spa->spa_load_meta_errors = sle.sle_meta_count;
1600 spa->spa_load_data_errors = sle.sle_data_count;
1602 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1603 sle.sle_data_count <= policy.zrp_maxdata) {
1607 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1608 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1610 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1611 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1612 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1613 VERIFY(nvlist_add_int64(spa->spa_load_info,
1614 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1615 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1616 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1618 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1622 if (error != ENXIO && error != EIO)
1627 return (verify_ok ? 0 : EIO);
1631 * Find a value in the pool props object.
1634 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1636 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1637 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1641 * Find a value in the pool directory object.
1644 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1646 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1647 name, sizeof (uint64_t), 1, val));
1651 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1653 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1658 * Fix up config after a partly-completed split. This is done with the
1659 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1660 * pool have that entry in their config, but only the splitting one contains
1661 * a list of all the guids of the vdevs that are being split off.
1663 * This function determines what to do with that list: either rejoin
1664 * all the disks to the pool, or complete the splitting process. To attempt
1665 * the rejoin, each disk that is offlined is marked online again, and
1666 * we do a reopen() call. If the vdev label for every disk that was
1667 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1668 * then we call vdev_split() on each disk, and complete the split.
1670 * Otherwise we leave the config alone, with all the vdevs in place in
1671 * the original pool.
1674 spa_try_repair(spa_t *spa, nvlist_t *config)
1681 boolean_t attempt_reopen;
1683 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1686 /* check that the config is complete */
1687 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1688 &glist, &gcount) != 0)
1691 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1693 /* attempt to online all the vdevs & validate */
1694 attempt_reopen = B_TRUE;
1695 for (i = 0; i < gcount; i++) {
1696 if (glist[i] == 0) /* vdev is hole */
1699 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1700 if (vd[i] == NULL) {
1702 * Don't bother attempting to reopen the disks;
1703 * just do the split.
1705 attempt_reopen = B_FALSE;
1707 /* attempt to re-online it */
1708 vd[i]->vdev_offline = B_FALSE;
1712 if (attempt_reopen) {
1713 vdev_reopen(spa->spa_root_vdev);
1715 /* check each device to see what state it's in */
1716 for (extracted = 0, i = 0; i < gcount; i++) {
1717 if (vd[i] != NULL &&
1718 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1725 * If every disk has been moved to the new pool, or if we never
1726 * even attempted to look at them, then we split them off for
1729 if (!attempt_reopen || gcount == extracted) {
1730 for (i = 0; i < gcount; i++)
1733 vdev_reopen(spa->spa_root_vdev);
1736 kmem_free(vd, gcount * sizeof (vdev_t *));
1740 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1741 boolean_t mosconfig)
1743 nvlist_t *config = spa->spa_config;
1744 char *ereport = FM_EREPORT_ZFS_POOL;
1749 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1753 * Versioning wasn't explicitly added to the label until later, so if
1754 * it's not present treat it as the initial version.
1756 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1757 &spa->spa_ubsync.ub_version) != 0)
1758 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1760 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1761 &spa->spa_config_txg);
1763 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1764 spa_guid_exists(pool_guid, 0)) {
1767 spa->spa_load_guid = pool_guid;
1769 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1771 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1775 gethrestime(&spa->spa_loaded_ts);
1776 error = spa_load_impl(spa, pool_guid, config, state, type,
1777 mosconfig, &ereport);
1780 spa->spa_minref = refcount_count(&spa->spa_refcount);
1782 if (error != EEXIST) {
1783 spa->spa_loaded_ts.tv_sec = 0;
1784 spa->spa_loaded_ts.tv_nsec = 0;
1786 if (error != EBADF) {
1787 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1790 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
1797 * Load an existing storage pool, using the pool's builtin spa_config as a
1798 * source of configuration information.
1801 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
1802 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1806 nvlist_t *nvroot = NULL;
1808 uberblock_t *ub = &spa->spa_uberblock;
1809 uint64_t children, config_cache_txg = spa->spa_config_txg;
1810 int orig_mode = spa->spa_mode;
1815 * If this is an untrusted config, access the pool in read-only mode.
1816 * This prevents things like resilvering recently removed devices.
1819 spa->spa_mode = FREAD;
1821 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1823 spa->spa_load_state = state;
1825 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
1828 parse = (type == SPA_IMPORT_EXISTING ?
1829 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
1832 * Create "The Godfather" zio to hold all async IOs
1834 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1835 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
1838 * Parse the configuration into a vdev tree. We explicitly set the
1839 * value that will be returned by spa_version() since parsing the
1840 * configuration requires knowing the version number.
1842 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1843 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
1844 spa_config_exit(spa, SCL_ALL, FTAG);
1849 ASSERT(spa->spa_root_vdev == rvd);
1851 if (type != SPA_IMPORT_ASSEMBLE) {
1852 ASSERT(spa_guid(spa) == pool_guid);
1856 * Try to open all vdevs, loading each label in the process.
1858 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1859 error = vdev_open(rvd);
1860 spa_config_exit(spa, SCL_ALL, FTAG);
1865 * We need to validate the vdev labels against the configuration that
1866 * we have in hand, which is dependent on the setting of mosconfig. If
1867 * mosconfig is true then we're validating the vdev labels based on
1868 * that config. Otherwise, we're validating against the cached config
1869 * (zpool.cache) that was read when we loaded the zfs module, and then
1870 * later we will recursively call spa_load() and validate against
1873 * If we're assembling a new pool that's been split off from an
1874 * existing pool, the labels haven't yet been updated so we skip
1875 * validation for now.
1877 if (type != SPA_IMPORT_ASSEMBLE) {
1878 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1879 error = vdev_validate(rvd);
1880 spa_config_exit(spa, SCL_ALL, FTAG);
1885 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
1890 * Find the best uberblock.
1892 vdev_uberblock_load(NULL, rvd, ub);
1895 * If we weren't able to find a single valid uberblock, return failure.
1897 if (ub->ub_txg == 0)
1898 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
1901 * If the pool is newer than the code, we can't open it.
1903 if (ub->ub_version > SPA_VERSION)
1904 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1907 * If the vdev guid sum doesn't match the uberblock, we have an
1908 * incomplete configuration. We first check to see if the pool
1909 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
1910 * If it is, defer the vdev_guid_sum check till later so we
1911 * can handle missing vdevs.
1913 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
1914 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
1915 rvd->vdev_guid_sum != ub->ub_guid_sum)
1916 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
1918 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
1919 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1920 spa_try_repair(spa, config);
1921 spa_config_exit(spa, SCL_ALL, FTAG);
1922 nvlist_free(spa->spa_config_splitting);
1923 spa->spa_config_splitting = NULL;
1927 * Initialize internal SPA structures.
1929 spa->spa_state = POOL_STATE_ACTIVE;
1930 spa->spa_ubsync = spa->spa_uberblock;
1931 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
1932 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
1933 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
1934 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
1935 spa->spa_claim_max_txg = spa->spa_first_txg;
1936 spa->spa_prev_software_version = ub->ub_software_version;
1938 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
1940 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1941 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1943 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
1944 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1948 nvlist_t *policy = NULL, *nvconfig;
1950 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
1951 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1953 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
1954 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
1956 unsigned long myhostid = 0;
1958 VERIFY(nvlist_lookup_string(nvconfig,
1959 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1962 myhostid = zone_get_hostid(NULL);
1965 * We're emulating the system's hostid in userland, so
1966 * we can't use zone_get_hostid().
1968 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
1969 #endif /* _KERNEL */
1970 if (check_hostid && hostid != 0 && myhostid != 0 &&
1971 hostid != myhostid) {
1972 nvlist_free(nvconfig);
1973 cmn_err(CE_WARN, "pool '%s' could not be "
1974 "loaded as it was last accessed by "
1975 "another system (host: %s hostid: 0x%lx). "
1976 "See: http://www.sun.com/msg/ZFS-8000-EY",
1977 spa_name(spa), hostname,
1978 (unsigned long)hostid);
1982 if (nvlist_lookup_nvlist(spa->spa_config,
1983 ZPOOL_REWIND_POLICY, &policy) == 0)
1984 VERIFY(nvlist_add_nvlist(nvconfig,
1985 ZPOOL_REWIND_POLICY, policy) == 0);
1987 spa_config_set(spa, nvconfig);
1989 spa_deactivate(spa);
1990 spa_activate(spa, orig_mode);
1992 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
1995 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
1996 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1997 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
1999 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2002 * Load the bit that tells us to use the new accounting function
2003 * (raid-z deflation). If we have an older pool, this will not
2006 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2007 if (error != 0 && error != ENOENT)
2008 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2010 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2011 &spa->spa_creation_version);
2012 if (error != 0 && error != ENOENT)
2013 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2016 * Load the persistent error log. If we have an older pool, this will
2019 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2020 if (error != 0 && error != ENOENT)
2021 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2023 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2024 &spa->spa_errlog_scrub);
2025 if (error != 0 && error != ENOENT)
2026 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2029 * Load the history object. If we have an older pool, this
2030 * will not be present.
2032 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2033 if (error != 0 && error != ENOENT)
2034 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2037 * If we're assembling the pool from the split-off vdevs of
2038 * an existing pool, we don't want to attach the spares & cache
2043 * Load any hot spares for this pool.
2045 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2046 if (error != 0 && error != ENOENT)
2047 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2048 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2049 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2050 if (load_nvlist(spa, spa->spa_spares.sav_object,
2051 &spa->spa_spares.sav_config) != 0)
2052 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2054 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2055 spa_load_spares(spa);
2056 spa_config_exit(spa, SCL_ALL, FTAG);
2057 } else if (error == 0) {
2058 spa->spa_spares.sav_sync = B_TRUE;
2062 * Load any level 2 ARC devices for this pool.
2064 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2065 &spa->spa_l2cache.sav_object);
2066 if (error != 0 && error != ENOENT)
2067 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2068 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2069 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2070 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2071 &spa->spa_l2cache.sav_config) != 0)
2072 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2074 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2075 spa_load_l2cache(spa);
2076 spa_config_exit(spa, SCL_ALL, FTAG);
2077 } else if (error == 0) {
2078 spa->spa_l2cache.sav_sync = B_TRUE;
2081 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2083 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2084 if (error && error != ENOENT)
2085 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2088 uint64_t autoreplace;
2090 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2091 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2092 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2093 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2094 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2095 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2096 &spa->spa_dedup_ditto);
2098 spa->spa_autoreplace = (autoreplace != 0);
2102 * If the 'autoreplace' property is set, then post a resource notifying
2103 * the ZFS DE that it should not issue any faults for unopenable
2104 * devices. We also iterate over the vdevs, and post a sysevent for any
2105 * unopenable vdevs so that the normal autoreplace handler can take
2108 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2109 spa_check_removed(spa->spa_root_vdev);
2111 * For the import case, this is done in spa_import(), because
2112 * at this point we're using the spare definitions from
2113 * the MOS config, not necessarily from the userland config.
2115 if (state != SPA_LOAD_IMPORT) {
2116 spa_aux_check_removed(&spa->spa_spares);
2117 spa_aux_check_removed(&spa->spa_l2cache);
2122 * Load the vdev state for all toplevel vdevs.
2127 * Propagate the leaf DTLs we just loaded all the way up the tree.
2129 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2130 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2131 spa_config_exit(spa, SCL_ALL, FTAG);
2134 * Load the DDTs (dedup tables).
2136 error = ddt_load(spa);
2138 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2140 spa_update_dspace(spa);
2143 * Validate the config, using the MOS config to fill in any
2144 * information which might be missing. If we fail to validate
2145 * the config then declare the pool unfit for use. If we're
2146 * assembling a pool from a split, the log is not transferred
2149 if (type != SPA_IMPORT_ASSEMBLE) {
2152 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2153 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2155 if (!spa_config_valid(spa, nvconfig)) {
2156 nvlist_free(nvconfig);
2157 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2160 nvlist_free(nvconfig);
2163 * Now that we've validate the config, check the state of the
2164 * root vdev. If it can't be opened, it indicates one or
2165 * more toplevel vdevs are faulted.
2167 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2170 if (spa_check_logs(spa)) {
2171 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2172 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2177 * We've successfully opened the pool, verify that we're ready
2178 * to start pushing transactions.
2180 if (state != SPA_LOAD_TRYIMPORT) {
2181 if (error = spa_load_verify(spa))
2182 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2186 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2187 spa->spa_load_max_txg == UINT64_MAX)) {
2189 int need_update = B_FALSE;
2191 ASSERT(state != SPA_LOAD_TRYIMPORT);
2194 * Claim log blocks that haven't been committed yet.
2195 * This must all happen in a single txg.
2196 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2197 * invoked from zil_claim_log_block()'s i/o done callback.
2198 * Price of rollback is that we abandon the log.
2200 spa->spa_claiming = B_TRUE;
2202 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2203 spa_first_txg(spa));
2204 (void) dmu_objset_find(spa_name(spa),
2205 zil_claim, tx, DS_FIND_CHILDREN);
2208 spa->spa_claiming = B_FALSE;
2210 spa_set_log_state(spa, SPA_LOG_GOOD);
2211 spa->spa_sync_on = B_TRUE;
2212 txg_sync_start(spa->spa_dsl_pool);
2215 * Wait for all claims to sync. We sync up to the highest
2216 * claimed log block birth time so that claimed log blocks
2217 * don't appear to be from the future. spa_claim_max_txg
2218 * will have been set for us by either zil_check_log_chain()
2219 * (invoked from spa_check_logs()) or zil_claim() above.
2221 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2224 * If the config cache is stale, or we have uninitialized
2225 * metaslabs (see spa_vdev_add()), then update the config.
2227 * If this is a verbatim import, trust the current
2228 * in-core spa_config and update the disk labels.
2230 if (config_cache_txg != spa->spa_config_txg ||
2231 state == SPA_LOAD_IMPORT ||
2232 state == SPA_LOAD_RECOVER ||
2233 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2234 need_update = B_TRUE;
2236 for (int c = 0; c < rvd->vdev_children; c++)
2237 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2238 need_update = B_TRUE;
2241 * Update the config cache asychronously in case we're the
2242 * root pool, in which case the config cache isn't writable yet.
2245 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2248 * Check all DTLs to see if anything needs resilvering.
2250 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2251 vdev_resilver_needed(rvd, NULL, NULL))
2252 spa_async_request(spa, SPA_ASYNC_RESILVER);
2255 * Delete any inconsistent datasets.
2257 (void) dmu_objset_find(spa_name(spa),
2258 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2261 * Clean up any stale temporary dataset userrefs.
2263 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2270 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2272 int mode = spa->spa_mode;
2275 spa_deactivate(spa);
2277 spa->spa_load_max_txg--;
2279 spa_activate(spa, mode);
2280 spa_async_suspend(spa);
2282 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2286 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2287 uint64_t max_request, int rewind_flags)
2289 nvlist_t *config = NULL;
2290 int load_error, rewind_error;
2291 uint64_t safe_rewind_txg;
2294 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2295 spa->spa_load_max_txg = spa->spa_load_txg;
2296 spa_set_log_state(spa, SPA_LOG_CLEAR);
2298 spa->spa_load_max_txg = max_request;
2301 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2303 if (load_error == 0)
2306 if (spa->spa_root_vdev != NULL)
2307 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2309 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2310 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2312 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2313 nvlist_free(config);
2314 return (load_error);
2317 /* Price of rolling back is discarding txgs, including log */
2318 if (state == SPA_LOAD_RECOVER)
2319 spa_set_log_state(spa, SPA_LOG_CLEAR);
2321 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2322 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2323 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2324 TXG_INITIAL : safe_rewind_txg;
2327 * Continue as long as we're finding errors, we're still within
2328 * the acceptable rewind range, and we're still finding uberblocks
2330 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2331 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2332 if (spa->spa_load_max_txg < safe_rewind_txg)
2333 spa->spa_extreme_rewind = B_TRUE;
2334 rewind_error = spa_load_retry(spa, state, mosconfig);
2337 spa->spa_extreme_rewind = B_FALSE;
2338 spa->spa_load_max_txg = UINT64_MAX;
2340 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2341 spa_config_set(spa, config);
2343 return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
2349 * The import case is identical to an open except that the configuration is sent
2350 * down from userland, instead of grabbed from the configuration cache. For the
2351 * case of an open, the pool configuration will exist in the
2352 * POOL_STATE_UNINITIALIZED state.
2354 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2355 * the same time open the pool, without having to keep around the spa_t in some
2359 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2363 spa_load_state_t state = SPA_LOAD_OPEN;
2365 int locked = B_FALSE;
2366 int firstopen = B_FALSE;
2371 * As disgusting as this is, we need to support recursive calls to this
2372 * function because dsl_dir_open() is called during spa_load(), and ends
2373 * up calling spa_open() again. The real fix is to figure out how to
2374 * avoid dsl_dir_open() calling this in the first place.
2376 if (mutex_owner(&spa_namespace_lock) != curthread) {
2377 mutex_enter(&spa_namespace_lock);
2381 if ((spa = spa_lookup(pool)) == NULL) {
2383 mutex_exit(&spa_namespace_lock);
2387 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2388 zpool_rewind_policy_t policy;
2392 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2394 if (policy.zrp_request & ZPOOL_DO_REWIND)
2395 state = SPA_LOAD_RECOVER;
2397 spa_activate(spa, spa_mode_global);
2399 if (state != SPA_LOAD_RECOVER)
2400 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2402 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2403 policy.zrp_request);
2405 if (error == EBADF) {
2407 * If vdev_validate() returns failure (indicated by
2408 * EBADF), it indicates that one of the vdevs indicates
2409 * that the pool has been exported or destroyed. If
2410 * this is the case, the config cache is out of sync and
2411 * we should remove the pool from the namespace.
2414 spa_deactivate(spa);
2415 spa_config_sync(spa, B_TRUE, B_TRUE);
2418 mutex_exit(&spa_namespace_lock);
2424 * We can't open the pool, but we still have useful
2425 * information: the state of each vdev after the
2426 * attempted vdev_open(). Return this to the user.
2428 if (config != NULL && spa->spa_config) {
2429 VERIFY(nvlist_dup(spa->spa_config, config,
2431 VERIFY(nvlist_add_nvlist(*config,
2432 ZPOOL_CONFIG_LOAD_INFO,
2433 spa->spa_load_info) == 0);
2436 spa_deactivate(spa);
2437 spa->spa_last_open_failed = error;
2439 mutex_exit(&spa_namespace_lock);
2445 spa_open_ref(spa, tag);
2448 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2451 * If we've recovered the pool, pass back any information we
2452 * gathered while doing the load.
2454 if (state == SPA_LOAD_RECOVER) {
2455 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2456 spa->spa_load_info) == 0);
2460 spa->spa_last_open_failed = 0;
2461 spa->spa_last_ubsync_txg = 0;
2462 spa->spa_load_txg = 0;
2463 mutex_exit(&spa_namespace_lock);
2467 zvol_create_minors(pool);
2478 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2481 return (spa_open_common(name, spapp, tag, policy, config));
2485 spa_open(const char *name, spa_t **spapp, void *tag)
2487 return (spa_open_common(name, spapp, tag, NULL, NULL));
2491 * Lookup the given spa_t, incrementing the inject count in the process,
2492 * preventing it from being exported or destroyed.
2495 spa_inject_addref(char *name)
2499 mutex_enter(&spa_namespace_lock);
2500 if ((spa = spa_lookup(name)) == NULL) {
2501 mutex_exit(&spa_namespace_lock);
2504 spa->spa_inject_ref++;
2505 mutex_exit(&spa_namespace_lock);
2511 spa_inject_delref(spa_t *spa)
2513 mutex_enter(&spa_namespace_lock);
2514 spa->spa_inject_ref--;
2515 mutex_exit(&spa_namespace_lock);
2519 * Add spares device information to the nvlist.
2522 spa_add_spares(spa_t *spa, nvlist_t *config)
2532 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2534 if (spa->spa_spares.sav_count == 0)
2537 VERIFY(nvlist_lookup_nvlist(config,
2538 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2539 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2540 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2542 VERIFY(nvlist_add_nvlist_array(nvroot,
2543 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2544 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2545 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2548 * Go through and find any spares which have since been
2549 * repurposed as an active spare. If this is the case, update
2550 * their status appropriately.
2552 for (i = 0; i < nspares; i++) {
2553 VERIFY(nvlist_lookup_uint64(spares[i],
2554 ZPOOL_CONFIG_GUID, &guid) == 0);
2555 if (spa_spare_exists(guid, &pool, NULL) &&
2557 VERIFY(nvlist_lookup_uint64_array(
2558 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2559 (uint64_t **)&vs, &vsc) == 0);
2560 vs->vs_state = VDEV_STATE_CANT_OPEN;
2561 vs->vs_aux = VDEV_AUX_SPARED;
2568 * Add l2cache device information to the nvlist, including vdev stats.
2571 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2574 uint_t i, j, nl2cache;
2581 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2583 if (spa->spa_l2cache.sav_count == 0)
2586 VERIFY(nvlist_lookup_nvlist(config,
2587 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2588 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2589 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2590 if (nl2cache != 0) {
2591 VERIFY(nvlist_add_nvlist_array(nvroot,
2592 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2593 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2594 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2597 * Update level 2 cache device stats.
2600 for (i = 0; i < nl2cache; i++) {
2601 VERIFY(nvlist_lookup_uint64(l2cache[i],
2602 ZPOOL_CONFIG_GUID, &guid) == 0);
2605 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2607 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2608 vd = spa->spa_l2cache.sav_vdevs[j];
2614 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2615 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2617 vdev_get_stats(vd, vs);
2623 spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2629 error = spa_open_common(name, &spa, FTAG, NULL, config);
2633 * This still leaves a window of inconsistency where the spares
2634 * or l2cache devices could change and the config would be
2635 * self-inconsistent.
2637 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2639 if (*config != NULL) {
2640 uint64_t loadtimes[2];
2642 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
2643 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
2644 VERIFY(nvlist_add_uint64_array(*config,
2645 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
2647 VERIFY(nvlist_add_uint64(*config,
2648 ZPOOL_CONFIG_ERRCOUNT,
2649 spa_get_errlog_size(spa)) == 0);
2651 if (spa_suspended(spa))
2652 VERIFY(nvlist_add_uint64(*config,
2653 ZPOOL_CONFIG_SUSPENDED,
2654 spa->spa_failmode) == 0);
2656 spa_add_spares(spa, *config);
2657 spa_add_l2cache(spa, *config);
2662 * We want to get the alternate root even for faulted pools, so we cheat
2663 * and call spa_lookup() directly.
2667 mutex_enter(&spa_namespace_lock);
2668 spa = spa_lookup(name);
2670 spa_altroot(spa, altroot, buflen);
2674 mutex_exit(&spa_namespace_lock);
2676 spa_altroot(spa, altroot, buflen);
2681 spa_config_exit(spa, SCL_CONFIG, FTAG);
2682 spa_close(spa, FTAG);
2689 * Validate that the auxiliary device array is well formed. We must have an
2690 * array of nvlists, each which describes a valid leaf vdev. If this is an
2691 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2692 * specified, as long as they are well-formed.
2695 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
2696 spa_aux_vdev_t *sav, const char *config, uint64_t version,
2697 vdev_labeltype_t label)
2704 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2707 * It's acceptable to have no devs specified.
2709 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
2716 * Make sure the pool is formatted with a version that supports this
2719 if (spa_version(spa) < version)
2723 * Set the pending device list so we correctly handle device in-use
2726 sav->sav_pending = dev;
2727 sav->sav_npending = ndev;
2729 for (i = 0; i < ndev; i++) {
2730 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
2734 if (!vd->vdev_ops->vdev_op_leaf) {
2741 * The L2ARC currently only supports disk devices in
2742 * kernel context. For user-level testing, we allow it.
2745 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
2746 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
2753 if ((error = vdev_open(vd)) == 0 &&
2754 (error = vdev_label_init(vd, crtxg, label)) == 0) {
2755 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
2756 vd->vdev_guid) == 0);
2762 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
2769 sav->sav_pending = NULL;
2770 sav->sav_npending = 0;
2775 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
2779 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2781 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2782 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
2783 VDEV_LABEL_SPARE)) != 0) {
2787 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2788 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
2789 VDEV_LABEL_L2CACHE));
2793 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
2798 if (sav->sav_config != NULL) {
2804 * Generate new dev list by concatentating with the
2807 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
2808 &olddevs, &oldndevs) == 0);
2810 newdevs = kmem_alloc(sizeof (void *) *
2811 (ndevs + oldndevs), KM_SLEEP);
2812 for (i = 0; i < oldndevs; i++)
2813 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
2815 for (i = 0; i < ndevs; i++)
2816 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
2819 VERIFY(nvlist_remove(sav->sav_config, config,
2820 DATA_TYPE_NVLIST_ARRAY) == 0);
2822 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2823 config, newdevs, ndevs + oldndevs) == 0);
2824 for (i = 0; i < oldndevs + ndevs; i++)
2825 nvlist_free(newdevs[i]);
2826 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
2829 * Generate a new dev list.
2831 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
2833 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
2839 * Stop and drop level 2 ARC devices
2842 spa_l2cache_drop(spa_t *spa)
2846 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2848 for (i = 0; i < sav->sav_count; i++) {
2851 vd = sav->sav_vdevs[i];
2854 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2855 pool != 0ULL && l2arc_vdev_present(vd))
2856 l2arc_remove_vdev(vd);
2857 if (vd->vdev_isl2cache)
2858 spa_l2cache_remove(vd);
2859 vdev_clear_stats(vd);
2860 (void) vdev_close(vd);
2868 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
2869 const char *history_str, nvlist_t *zplprops)
2872 char *altroot = NULL;
2877 uint64_t txg = TXG_INITIAL;
2878 nvlist_t **spares, **l2cache;
2879 uint_t nspares, nl2cache;
2880 uint64_t version, obj;
2883 * If this pool already exists, return failure.
2885 mutex_enter(&spa_namespace_lock);
2886 if (spa_lookup(pool) != NULL) {
2887 mutex_exit(&spa_namespace_lock);
2892 * Allocate a new spa_t structure.
2894 (void) nvlist_lookup_string(props,
2895 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2896 spa = spa_add(pool, NULL, altroot);
2897 spa_activate(spa, spa_mode_global);
2899 if (props && (error = spa_prop_validate(spa, props))) {
2900 spa_deactivate(spa);
2902 mutex_exit(&spa_namespace_lock);
2906 if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
2908 version = SPA_VERSION;
2909 ASSERT(version <= SPA_VERSION);
2911 spa->spa_first_txg = txg;
2912 spa->spa_uberblock.ub_txg = txg - 1;
2913 spa->spa_uberblock.ub_version = version;
2914 spa->spa_ubsync = spa->spa_uberblock;
2917 * Create "The Godfather" zio to hold all async IOs
2919 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2920 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2923 * Create the root vdev.
2925 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2927 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
2929 ASSERT(error != 0 || rvd != NULL);
2930 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
2932 if (error == 0 && !zfs_allocatable_devs(nvroot))
2936 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
2937 (error = spa_validate_aux(spa, nvroot, txg,
2938 VDEV_ALLOC_ADD)) == 0) {
2939 for (int c = 0; c < rvd->vdev_children; c++) {
2940 vdev_metaslab_set_size(rvd->vdev_child[c]);
2941 vdev_expand(rvd->vdev_child[c], txg);
2945 spa_config_exit(spa, SCL_ALL, FTAG);
2949 spa_deactivate(spa);
2951 mutex_exit(&spa_namespace_lock);
2956 * Get the list of spares, if specified.
2958 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2959 &spares, &nspares) == 0) {
2960 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
2962 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
2963 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2964 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2965 spa_load_spares(spa);
2966 spa_config_exit(spa, SCL_ALL, FTAG);
2967 spa->spa_spares.sav_sync = B_TRUE;
2971 * Get the list of level 2 cache devices, if specified.
2973 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2974 &l2cache, &nl2cache) == 0) {
2975 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2976 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2977 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2978 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2979 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2980 spa_load_l2cache(spa);
2981 spa_config_exit(spa, SCL_ALL, FTAG);
2982 spa->spa_l2cache.sav_sync = B_TRUE;
2985 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2986 spa->spa_meta_objset = dp->dp_meta_objset;
2989 * Create DDTs (dedup tables).
2993 spa_update_dspace(spa);
2995 tx = dmu_tx_create_assigned(dp, txg);
2998 * Create the pool config object.
3000 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3001 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3002 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3004 if (zap_add(spa->spa_meta_objset,
3005 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3006 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3007 cmn_err(CE_PANIC, "failed to add pool config");
3010 if (zap_add(spa->spa_meta_objset,
3011 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3012 sizeof (uint64_t), 1, &version, tx) != 0) {
3013 cmn_err(CE_PANIC, "failed to add pool version");
3016 /* Newly created pools with the right version are always deflated. */
3017 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3018 spa->spa_deflate = TRUE;
3019 if (zap_add(spa->spa_meta_objset,
3020 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3021 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3022 cmn_err(CE_PANIC, "failed to add deflate");
3027 * Create the deferred-free bpobj. Turn off compression
3028 * because sync-to-convergence takes longer if the blocksize
3031 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3032 dmu_object_set_compress(spa->spa_meta_objset, obj,
3033 ZIO_COMPRESS_OFF, tx);
3034 if (zap_add(spa->spa_meta_objset,
3035 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3036 sizeof (uint64_t), 1, &obj, tx) != 0) {
3037 cmn_err(CE_PANIC, "failed to add bpobj");
3039 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3040 spa->spa_meta_objset, obj));
3043 * Create the pool's history object.
3045 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3046 spa_history_create_obj(spa, tx);
3049 * Set pool properties.
3051 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3052 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3053 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3054 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3056 if (props != NULL) {
3057 spa_configfile_set(spa, props, B_FALSE);
3058 spa_sync_props(spa, props, tx);
3063 spa->spa_sync_on = B_TRUE;
3064 txg_sync_start(spa->spa_dsl_pool);
3067 * We explicitly wait for the first transaction to complete so that our
3068 * bean counters are appropriately updated.
3070 txg_wait_synced(spa->spa_dsl_pool, txg);
3072 spa_config_sync(spa, B_FALSE, B_TRUE);
3074 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3075 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3076 spa_history_log_version(spa, LOG_POOL_CREATE);
3078 spa->spa_minref = refcount_count(&spa->spa_refcount);
3080 mutex_exit(&spa_namespace_lock);
3088 * Get the root pool information from the root disk, then import the root pool
3089 * during the system boot up time.
3091 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3094 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3097 nvlist_t *nvtop, *nvroot;
3100 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3104 * Add this top-level vdev to the child array.
3106 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3108 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3110 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3113 * Put this pool's top-level vdevs into a root vdev.
3115 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3116 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3117 VDEV_TYPE_ROOT) == 0);
3118 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3119 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3120 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3124 * Replace the existing vdev_tree with the new root vdev in
3125 * this pool's configuration (remove the old, add the new).
3127 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3128 nvlist_free(nvroot);
3133 * Walk the vdev tree and see if we can find a device with "better"
3134 * configuration. A configuration is "better" if the label on that
3135 * device has a more recent txg.
3138 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3140 for (int c = 0; c < vd->vdev_children; c++)
3141 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3143 if (vd->vdev_ops->vdev_op_leaf) {
3147 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3151 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3155 * Do we have a better boot device?
3157 if (label_txg > *txg) {
3166 * Import a root pool.
3168 * For x86. devpath_list will consist of devid and/or physpath name of
3169 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3170 * The GRUB "findroot" command will return the vdev we should boot.
3172 * For Sparc, devpath_list consists the physpath name of the booting device
3173 * no matter the rootpool is a single device pool or a mirrored pool.
3175 * "/pci@1f,0/ide@d/disk@0,0:a"
3178 spa_import_rootpool(char *devpath, char *devid)
3181 vdev_t *rvd, *bvd, *avd = NULL;
3182 nvlist_t *config, *nvtop;
3188 * Read the label from the boot device and generate a configuration.
3190 config = spa_generate_rootconf(devpath, devid, &guid);
3191 #if defined(_OBP) && defined(_KERNEL)
3192 if (config == NULL) {
3193 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3195 get_iscsi_bootpath_phy(devpath);
3196 config = spa_generate_rootconf(devpath, devid, &guid);
3200 if (config == NULL) {
3201 cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
3206 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3208 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3210 mutex_enter(&spa_namespace_lock);
3211 if ((spa = spa_lookup(pname)) != NULL) {
3213 * Remove the existing root pool from the namespace so that we
3214 * can replace it with the correct config we just read in.
3219 spa = spa_add(pname, config, NULL);
3220 spa->spa_is_root = B_TRUE;
3221 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3224 * Build up a vdev tree based on the boot device's label config.
3226 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3228 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3229 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3230 VDEV_ALLOC_ROOTPOOL);
3231 spa_config_exit(spa, SCL_ALL, FTAG);
3233 mutex_exit(&spa_namespace_lock);
3234 nvlist_free(config);
3235 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3241 * Get the boot vdev.
3243 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3244 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3245 (u_longlong_t)guid);
3251 * Determine if there is a better boot device.
3254 spa_alt_rootvdev(rvd, &avd, &txg);
3256 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3257 "try booting from '%s'", avd->vdev_path);
3263 * If the boot device is part of a spare vdev then ensure that
3264 * we're booting off the active spare.
3266 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3267 !bvd->vdev_isspare) {
3268 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3269 "try booting from '%s'",
3271 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3277 spa_history_log_version(spa, LOG_POOL_IMPORT);
3279 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3281 spa_config_exit(spa, SCL_ALL, FTAG);
3282 mutex_exit(&spa_namespace_lock);
3284 nvlist_free(config);
3292 * Import a non-root pool into the system.
3295 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3298 char *altroot = NULL;
3299 spa_load_state_t state = SPA_LOAD_IMPORT;
3300 zpool_rewind_policy_t policy;
3301 uint64_t mode = spa_mode_global;
3302 uint64_t readonly = B_FALSE;
3305 nvlist_t **spares, **l2cache;
3306 uint_t nspares, nl2cache;
3309 * If a pool with this name exists, return failure.
3311 mutex_enter(&spa_namespace_lock);
3312 if (spa_lookup(pool) != NULL) {
3313 mutex_exit(&spa_namespace_lock);
3318 * Create and initialize the spa structure.
3320 (void) nvlist_lookup_string(props,
3321 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3322 (void) nvlist_lookup_uint64(props,
3323 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3326 spa = spa_add(pool, config, altroot);
3327 spa->spa_import_flags = flags;
3330 * Verbatim import - Take a pool and insert it into the namespace
3331 * as if it had been loaded at boot.
3333 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3335 spa_configfile_set(spa, props, B_FALSE);
3337 spa_config_sync(spa, B_FALSE, B_TRUE);
3339 mutex_exit(&spa_namespace_lock);
3340 spa_history_log_version(spa, LOG_POOL_IMPORT);
3345 spa_activate(spa, mode);
3348 * Don't start async tasks until we know everything is healthy.
3350 spa_async_suspend(spa);
3352 zpool_get_rewind_policy(config, &policy);
3353 if (policy.zrp_request & ZPOOL_DO_REWIND)
3354 state = SPA_LOAD_RECOVER;
3357 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3358 * because the user-supplied config is actually the one to trust when
3361 if (state != SPA_LOAD_RECOVER)
3362 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3364 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3365 policy.zrp_request);
3368 * Propagate anything learned while loading the pool and pass it
3369 * back to caller (i.e. rewind info, missing devices, etc).
3371 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3372 spa->spa_load_info) == 0);
3374 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3376 * Toss any existing sparelist, as it doesn't have any validity
3377 * anymore, and conflicts with spa_has_spare().
3379 if (spa->spa_spares.sav_config) {
3380 nvlist_free(spa->spa_spares.sav_config);
3381 spa->spa_spares.sav_config = NULL;
3382 spa_load_spares(spa);
3384 if (spa->spa_l2cache.sav_config) {
3385 nvlist_free(spa->spa_l2cache.sav_config);
3386 spa->spa_l2cache.sav_config = NULL;
3387 spa_load_l2cache(spa);
3390 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3393 error = spa_validate_aux(spa, nvroot, -1ULL,
3396 error = spa_validate_aux(spa, nvroot, -1ULL,
3397 VDEV_ALLOC_L2CACHE);
3398 spa_config_exit(spa, SCL_ALL, FTAG);
3401 spa_configfile_set(spa, props, B_FALSE);
3403 if (error != 0 || (props && spa_writeable(spa) &&
3404 (error = spa_prop_set(spa, props)))) {
3406 spa_deactivate(spa);
3408 mutex_exit(&spa_namespace_lock);
3412 spa_async_resume(spa);
3415 * Override any spares and level 2 cache devices as specified by
3416 * the user, as these may have correct device names/devids, etc.
3418 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3419 &spares, &nspares) == 0) {
3420 if (spa->spa_spares.sav_config)
3421 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3422 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3424 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3425 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3426 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3427 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3428 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3429 spa_load_spares(spa);
3430 spa_config_exit(spa, SCL_ALL, FTAG);
3431 spa->spa_spares.sav_sync = B_TRUE;
3433 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3434 &l2cache, &nl2cache) == 0) {
3435 if (spa->spa_l2cache.sav_config)
3436 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3437 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3439 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3440 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3441 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3442 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3443 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3444 spa_load_l2cache(spa);
3445 spa_config_exit(spa, SCL_ALL, FTAG);
3446 spa->spa_l2cache.sav_sync = B_TRUE;
3450 * Check for any removed devices.
3452 if (spa->spa_autoreplace) {
3453 spa_aux_check_removed(&spa->spa_spares);
3454 spa_aux_check_removed(&spa->spa_l2cache);
3457 if (spa_writeable(spa)) {
3459 * Update the config cache to include the newly-imported pool.
3461 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3465 * It's possible that the pool was expanded while it was exported.
3466 * We kick off an async task to handle this for us.
3468 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3470 mutex_exit(&spa_namespace_lock);
3471 spa_history_log_version(spa, LOG_POOL_IMPORT);
3475 zvol_create_minors(pool);
3482 spa_tryimport(nvlist_t *tryconfig)
3484 nvlist_t *config = NULL;
3490 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3493 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3497 * Create and initialize the spa structure.
3499 mutex_enter(&spa_namespace_lock);
3500 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3501 spa_activate(spa, FREAD);
3504 * Pass off the heavy lifting to spa_load().
3505 * Pass TRUE for mosconfig because the user-supplied config
3506 * is actually the one to trust when doing an import.
3508 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3511 * If 'tryconfig' was at least parsable, return the current config.
3513 if (spa->spa_root_vdev != NULL) {
3514 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3515 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3517 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3519 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3520 spa->spa_uberblock.ub_timestamp) == 0);
3523 * If the bootfs property exists on this pool then we
3524 * copy it out so that external consumers can tell which
3525 * pools are bootable.
3527 if ((!error || error == EEXIST) && spa->spa_bootfs) {
3528 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3531 * We have to play games with the name since the
3532 * pool was opened as TRYIMPORT_NAME.
3534 if (dsl_dsobj_to_dsname(spa_name(spa),
3535 spa->spa_bootfs, tmpname) == 0) {
3537 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3539 cp = strchr(tmpname, '/');
3541 (void) strlcpy(dsname, tmpname,
3544 (void) snprintf(dsname, MAXPATHLEN,
3545 "%s/%s", poolname, ++cp);
3547 VERIFY(nvlist_add_string(config,
3548 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3549 kmem_free(dsname, MAXPATHLEN);
3551 kmem_free(tmpname, MAXPATHLEN);
3555 * Add the list of hot spares and level 2 cache devices.
3557 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3558 spa_add_spares(spa, config);
3559 spa_add_l2cache(spa, config);
3560 spa_config_exit(spa, SCL_CONFIG, FTAG);
3564 spa_deactivate(spa);
3566 mutex_exit(&spa_namespace_lock);
3572 * Pool export/destroy
3574 * The act of destroying or exporting a pool is very simple. We make sure there
3575 * is no more pending I/O and any references to the pool are gone. Then, we
3576 * update the pool state and sync all the labels to disk, removing the
3577 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3578 * we don't sync the labels or remove the configuration cache.
3581 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
3582 boolean_t force, boolean_t hardforce)
3589 if (!(spa_mode_global & FWRITE))
3592 mutex_enter(&spa_namespace_lock);
3593 if ((spa = spa_lookup(pool)) == NULL) {
3594 mutex_exit(&spa_namespace_lock);
3599 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3600 * reacquire the namespace lock, and see if we can export.
3602 spa_open_ref(spa, FTAG);
3603 mutex_exit(&spa_namespace_lock);
3604 spa_async_suspend(spa);
3605 mutex_enter(&spa_namespace_lock);
3606 spa_close(spa, FTAG);
3609 * The pool will be in core if it's openable,
3610 * in which case we can modify its state.
3612 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3614 * Objsets may be open only because they're dirty, so we
3615 * have to force it to sync before checking spa_refcnt.
3617 txg_wait_synced(spa->spa_dsl_pool, 0);
3620 * A pool cannot be exported or destroyed if there are active
3621 * references. If we are resetting a pool, allow references by
3622 * fault injection handlers.
3624 if (!spa_refcount_zero(spa) ||
3625 (spa->spa_inject_ref != 0 &&
3626 new_state != POOL_STATE_UNINITIALIZED)) {
3627 spa_async_resume(spa);
3628 mutex_exit(&spa_namespace_lock);
3633 * A pool cannot be exported if it has an active shared spare.
3634 * This is to prevent other pools stealing the active spare
3635 * from an exported pool. At user's own will, such pool can
3636 * be forcedly exported.
3638 if (!force && new_state == POOL_STATE_EXPORTED &&
3639 spa_has_active_shared_spare(spa)) {
3640 spa_async_resume(spa);
3641 mutex_exit(&spa_namespace_lock);
3646 * We want this to be reflected on every label,
3647 * so mark them all dirty. spa_unload() will do the
3648 * final sync that pushes these changes out.
3650 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
3651 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3652 spa->spa_state = new_state;
3653 spa->spa_final_txg = spa_last_synced_txg(spa) +
3655 vdev_config_dirty(spa->spa_root_vdev);
3656 spa_config_exit(spa, SCL_ALL, FTAG);
3660 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
3662 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3664 spa_deactivate(spa);
3667 if (oldconfig && spa->spa_config)
3668 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
3670 if (new_state != POOL_STATE_UNINITIALIZED) {
3672 spa_config_sync(spa, B_TRUE, B_TRUE);
3675 mutex_exit(&spa_namespace_lock);
3681 * Destroy a storage pool.
3684 spa_destroy(char *pool)
3686 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
3691 * Export a storage pool.
3694 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
3695 boolean_t hardforce)
3697 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
3702 * Similar to spa_export(), this unloads the spa_t without actually removing it
3703 * from the namespace in any way.
3706 spa_reset(char *pool)
3708 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
3713 * ==========================================================================
3714 * Device manipulation
3715 * ==========================================================================
3719 * Add a device to a storage pool.
3722 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3726 vdev_t *rvd = spa->spa_root_vdev;
3728 nvlist_t **spares, **l2cache;
3729 uint_t nspares, nl2cache;
3731 ASSERT(spa_writeable(spa));
3733 txg = spa_vdev_enter(spa);
3735 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
3736 VDEV_ALLOC_ADD)) != 0)
3737 return (spa_vdev_exit(spa, NULL, txg, error));
3739 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
3741 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
3745 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
3749 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
3750 return (spa_vdev_exit(spa, vd, txg, EINVAL));
3752 if (vd->vdev_children != 0 &&
3753 (error = vdev_create(vd, txg, B_FALSE)) != 0)
3754 return (spa_vdev_exit(spa, vd, txg, error));
3757 * We must validate the spares and l2cache devices after checking the
3758 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
3760 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
3761 return (spa_vdev_exit(spa, vd, txg, error));
3764 * Transfer each new top-level vdev from vd to rvd.
3766 for (int c = 0; c < vd->vdev_children; c++) {
3769 * Set the vdev id to the first hole, if one exists.
3771 for (id = 0; id < rvd->vdev_children; id++) {
3772 if (rvd->vdev_child[id]->vdev_ishole) {
3773 vdev_free(rvd->vdev_child[id]);
3777 tvd = vd->vdev_child[c];
3778 vdev_remove_child(vd, tvd);
3780 vdev_add_child(rvd, tvd);
3781 vdev_config_dirty(tvd);
3785 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
3786 ZPOOL_CONFIG_SPARES);
3787 spa_load_spares(spa);
3788 spa->spa_spares.sav_sync = B_TRUE;
3791 if (nl2cache != 0) {
3792 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
3793 ZPOOL_CONFIG_L2CACHE);
3794 spa_load_l2cache(spa);
3795 spa->spa_l2cache.sav_sync = B_TRUE;
3799 * We have to be careful when adding new vdevs to an existing pool.
3800 * If other threads start allocating from these vdevs before we
3801 * sync the config cache, and we lose power, then upon reboot we may
3802 * fail to open the pool because there are DVAs that the config cache
3803 * can't translate. Therefore, we first add the vdevs without
3804 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
3805 * and then let spa_config_update() initialize the new metaslabs.
3807 * spa_load() checks for added-but-not-initialized vdevs, so that
3808 * if we lose power at any point in this sequence, the remaining
3809 * steps will be completed the next time we load the pool.
3811 (void) spa_vdev_exit(spa, vd, txg, 0);
3813 mutex_enter(&spa_namespace_lock);
3814 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3815 mutex_exit(&spa_namespace_lock);
3821 * Attach a device to a mirror. The arguments are the path to any device
3822 * in the mirror, and the nvroot for the new device. If the path specifies
3823 * a device that is not mirrored, we automatically insert the mirror vdev.
3825 * If 'replacing' is specified, the new device is intended to replace the
3826 * existing device; in this case the two devices are made into their own
3827 * mirror using the 'replacing' vdev, which is functionally identical to
3828 * the mirror vdev (it actually reuses all the same ops) but has a few
3829 * extra rules: you can't attach to it after it's been created, and upon
3830 * completion of resilvering, the first disk (the one being replaced)
3831 * is automatically detached.
3834 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3836 uint64_t txg, dtl_max_txg;
3837 vdev_t *rvd = spa->spa_root_vdev;
3838 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
3840 char *oldvdpath, *newvdpath;
3844 ASSERT(spa_writeable(spa));
3846 txg = spa_vdev_enter(spa);
3848 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3851 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3853 if (!oldvd->vdev_ops->vdev_op_leaf)
3854 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3856 pvd = oldvd->vdev_parent;
3858 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
3859 VDEV_ALLOC_ADD)) != 0)
3860 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
3862 if (newrootvd->vdev_children != 1)
3863 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3865 newvd = newrootvd->vdev_child[0];
3867 if (!newvd->vdev_ops->vdev_op_leaf)
3868 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3870 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3871 return (spa_vdev_exit(spa, newrootvd, txg, error));
3874 * Spares can't replace logs
3876 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
3877 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3881 * For attach, the only allowable parent is a mirror or the root
3884 if (pvd->vdev_ops != &vdev_mirror_ops &&
3885 pvd->vdev_ops != &vdev_root_ops)
3886 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3888 pvops = &vdev_mirror_ops;
3891 * Active hot spares can only be replaced by inactive hot
3894 if (pvd->vdev_ops == &vdev_spare_ops &&
3895 oldvd->vdev_isspare &&
3896 !spa_has_spare(spa, newvd->vdev_guid))
3897 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3900 * If the source is a hot spare, and the parent isn't already a
3901 * spare, then we want to create a new hot spare. Otherwise, we
3902 * want to create a replacing vdev. The user is not allowed to
3903 * attach to a spared vdev child unless the 'isspare' state is
3904 * the same (spare replaces spare, non-spare replaces
3907 if (pvd->vdev_ops == &vdev_replacing_ops &&
3908 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
3909 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3910 } else if (pvd->vdev_ops == &vdev_spare_ops &&
3911 newvd->vdev_isspare != oldvd->vdev_isspare) {
3912 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3915 if (newvd->vdev_isspare)
3916 pvops = &vdev_spare_ops;
3918 pvops = &vdev_replacing_ops;
3922 * Make sure the new device is big enough.
3924 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3925 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3928 * The new device cannot have a higher alignment requirement
3929 * than the top-level vdev.
3931 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3932 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3935 * If this is an in-place replacement, update oldvd's path and devid
3936 * to make it distinguishable from newvd, and unopenable from now on.
3938 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3939 spa_strfree(oldvd->vdev_path);
3940 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3942 (void) sprintf(oldvd->vdev_path, "%s/%s",
3943 newvd->vdev_path, "old");
3944 if (oldvd->vdev_devid != NULL) {
3945 spa_strfree(oldvd->vdev_devid);
3946 oldvd->vdev_devid = NULL;
3950 /* mark the device being resilvered */
3951 newvd->vdev_resilvering = B_TRUE;
3954 * If the parent is not a mirror, or if we're replacing, insert the new
3955 * mirror/replacing/spare vdev above oldvd.
3957 if (pvd->vdev_ops != pvops)
3958 pvd = vdev_add_parent(oldvd, pvops);
3960 ASSERT(pvd->vdev_top->vdev_parent == rvd);
3961 ASSERT(pvd->vdev_ops == pvops);
3962 ASSERT(oldvd->vdev_parent == pvd);
3965 * Extract the new device from its root and add it to pvd.
3967 vdev_remove_child(newrootvd, newvd);
3968 newvd->vdev_id = pvd->vdev_children;
3969 newvd->vdev_crtxg = oldvd->vdev_crtxg;
3970 vdev_add_child(pvd, newvd);
3972 tvd = newvd->vdev_top;
3973 ASSERT(pvd->vdev_top == tvd);
3974 ASSERT(tvd->vdev_parent == rvd);
3976 vdev_config_dirty(tvd);
3979 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
3980 * for any dmu_sync-ed blocks. It will propagate upward when
3981 * spa_vdev_exit() calls vdev_dtl_reassess().
3983 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
3985 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
3986 dtl_max_txg - TXG_INITIAL);
3988 if (newvd->vdev_isspare) {
3989 spa_spare_activate(newvd);
3990 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
3993 oldvdpath = spa_strdup(oldvd->vdev_path);
3994 newvdpath = spa_strdup(newvd->vdev_path);
3995 newvd_isspare = newvd->vdev_isspare;
3998 * Mark newvd's DTL dirty in this txg.
4000 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4003 * Restart the resilver
4005 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4010 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4012 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
4013 "%s vdev=%s %s vdev=%s",
4014 replacing && newvd_isspare ? "spare in" :
4015 replacing ? "replace" : "attach", newvdpath,
4016 replacing ? "for" : "to", oldvdpath);
4018 spa_strfree(oldvdpath);
4019 spa_strfree(newvdpath);
4021 if (spa->spa_bootfs)
4022 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4028 * Detach a device from a mirror or replacing vdev.
4029 * If 'replace_done' is specified, only detach if the parent
4030 * is a replacing vdev.
4033 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4037 vdev_t *rvd = spa->spa_root_vdev;
4038 vdev_t *vd, *pvd, *cvd, *tvd;
4039 boolean_t unspare = B_FALSE;
4040 uint64_t unspare_guid;
4043 ASSERT(spa_writeable(spa));
4045 txg = spa_vdev_enter(spa);
4047 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4050 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4052 if (!vd->vdev_ops->vdev_op_leaf)
4053 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4055 pvd = vd->vdev_parent;
4058 * If the parent/child relationship is not as expected, don't do it.
4059 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4060 * vdev that's replacing B with C. The user's intent in replacing
4061 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4062 * the replace by detaching C, the expected behavior is to end up
4063 * M(A,B). But suppose that right after deciding to detach C,
4064 * the replacement of B completes. We would have M(A,C), and then
4065 * ask to detach C, which would leave us with just A -- not what
4066 * the user wanted. To prevent this, we make sure that the
4067 * parent/child relationship hasn't changed -- in this example,
4068 * that C's parent is still the replacing vdev R.
4070 if (pvd->vdev_guid != pguid && pguid != 0)
4071 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4074 * Only 'replacing' or 'spare' vdevs can be replaced.
4076 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4077 pvd->vdev_ops != &vdev_spare_ops)
4078 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4080 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4081 spa_version(spa) >= SPA_VERSION_SPARES);
4084 * Only mirror, replacing, and spare vdevs support detach.
4086 if (pvd->vdev_ops != &vdev_replacing_ops &&
4087 pvd->vdev_ops != &vdev_mirror_ops &&
4088 pvd->vdev_ops != &vdev_spare_ops)
4089 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4092 * If this device has the only valid copy of some data,
4093 * we cannot safely detach it.
4095 if (vdev_dtl_required(vd))
4096 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4098 ASSERT(pvd->vdev_children >= 2);
4101 * If we are detaching the second disk from a replacing vdev, then
4102 * check to see if we changed the original vdev's path to have "/old"
4103 * at the end in spa_vdev_attach(). If so, undo that change now.
4105 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4106 vd->vdev_path != NULL) {
4107 size_t len = strlen(vd->vdev_path);
4109 for (int c = 0; c < pvd->vdev_children; c++) {
4110 cvd = pvd->vdev_child[c];
4112 if (cvd == vd || cvd->vdev_path == NULL)
4115 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4116 strcmp(cvd->vdev_path + len, "/old") == 0) {
4117 spa_strfree(cvd->vdev_path);
4118 cvd->vdev_path = spa_strdup(vd->vdev_path);
4125 * If we are detaching the original disk from a spare, then it implies
4126 * that the spare should become a real disk, and be removed from the
4127 * active spare list for the pool.
4129 if (pvd->vdev_ops == &vdev_spare_ops &&
4131 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4135 * Erase the disk labels so the disk can be used for other things.
4136 * This must be done after all other error cases are handled,
4137 * but before we disembowel vd (so we can still do I/O to it).
4138 * But if we can't do it, don't treat the error as fatal --
4139 * it may be that the unwritability of the disk is the reason
4140 * it's being detached!
4142 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4145 * Remove vd from its parent and compact the parent's children.
4147 vdev_remove_child(pvd, vd);
4148 vdev_compact_children(pvd);
4151 * Remember one of the remaining children so we can get tvd below.
4153 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4156 * If we need to remove the remaining child from the list of hot spares,
4157 * do it now, marking the vdev as no longer a spare in the process.
4158 * We must do this before vdev_remove_parent(), because that can
4159 * change the GUID if it creates a new toplevel GUID. For a similar
4160 * reason, we must remove the spare now, in the same txg as the detach;
4161 * otherwise someone could attach a new sibling, change the GUID, and
4162 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4165 ASSERT(cvd->vdev_isspare);
4166 spa_spare_remove(cvd);
4167 unspare_guid = cvd->vdev_guid;
4168 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4169 cvd->vdev_unspare = B_TRUE;
4173 * If the parent mirror/replacing vdev only has one child,
4174 * the parent is no longer needed. Remove it from the tree.
4176 if (pvd->vdev_children == 1) {
4177 if (pvd->vdev_ops == &vdev_spare_ops)
4178 cvd->vdev_unspare = B_FALSE;
4179 vdev_remove_parent(cvd);
4180 cvd->vdev_resilvering = B_FALSE;
4185 * We don't set tvd until now because the parent we just removed
4186 * may have been the previous top-level vdev.
4188 tvd = cvd->vdev_top;
4189 ASSERT(tvd->vdev_parent == rvd);
4192 * Reevaluate the parent vdev state.
4194 vdev_propagate_state(cvd);
4197 * If the 'autoexpand' property is set on the pool then automatically
4198 * try to expand the size of the pool. For example if the device we
4199 * just detached was smaller than the others, it may be possible to
4200 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4201 * first so that we can obtain the updated sizes of the leaf vdevs.
4203 if (spa->spa_autoexpand) {
4205 vdev_expand(tvd, txg);
4208 vdev_config_dirty(tvd);
4211 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4212 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4213 * But first make sure we're not on any *other* txg's DTL list, to
4214 * prevent vd from being accessed after it's freed.
4216 vdpath = spa_strdup(vd->vdev_path);
4217 for (int t = 0; t < TXG_SIZE; t++)
4218 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4219 vd->vdev_detached = B_TRUE;
4220 vdev_dirty(tvd, VDD_DTL, vd, txg);
4222 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4224 /* hang on to the spa before we release the lock */
4225 spa_open_ref(spa, FTAG);
4227 error = spa_vdev_exit(spa, vd, txg, 0);
4229 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4231 spa_strfree(vdpath);
4234 * If this was the removal of the original device in a hot spare vdev,
4235 * then we want to go through and remove the device from the hot spare
4236 * list of every other pool.
4239 spa_t *altspa = NULL;
4241 mutex_enter(&spa_namespace_lock);
4242 while ((altspa = spa_next(altspa)) != NULL) {
4243 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4247 spa_open_ref(altspa, FTAG);
4248 mutex_exit(&spa_namespace_lock);
4249 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4250 mutex_enter(&spa_namespace_lock);
4251 spa_close(altspa, FTAG);
4253 mutex_exit(&spa_namespace_lock);
4255 /* search the rest of the vdevs for spares to remove */
4256 spa_vdev_resilver_done(spa);
4259 /* all done with the spa; OK to release */
4260 mutex_enter(&spa_namespace_lock);
4261 spa_close(spa, FTAG);
4262 mutex_exit(&spa_namespace_lock);
4268 * Split a set of devices from their mirrors, and create a new pool from them.
4271 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4272 nvlist_t *props, boolean_t exp)
4275 uint64_t txg, *glist;
4277 uint_t c, children, lastlog;
4278 nvlist_t **child, *nvl, *tmp;
4280 char *altroot = NULL;
4281 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4282 boolean_t activate_slog;
4284 ASSERT(spa_writeable(spa));
4286 txg = spa_vdev_enter(spa);
4288 /* clear the log and flush everything up to now */
4289 activate_slog = spa_passivate_log(spa);
4290 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4291 error = spa_offline_log(spa);
4292 txg = spa_vdev_config_enter(spa);
4295 spa_activate_log(spa);
4298 return (spa_vdev_exit(spa, NULL, txg, error));
4300 /* check new spa name before going any further */
4301 if (spa_lookup(newname) != NULL)
4302 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4305 * scan through all the children to ensure they're all mirrors
4307 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4308 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4310 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4312 /* first, check to ensure we've got the right child count */
4313 rvd = spa->spa_root_vdev;
4315 for (c = 0; c < rvd->vdev_children; c++) {
4316 vdev_t *vd = rvd->vdev_child[c];
4318 /* don't count the holes & logs as children */
4319 if (vd->vdev_islog || vd->vdev_ishole) {
4327 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4328 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4330 /* next, ensure no spare or cache devices are part of the split */
4331 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4332 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4333 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4335 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
4336 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
4338 /* then, loop over each vdev and validate it */
4339 for (c = 0; c < children; c++) {
4340 uint64_t is_hole = 0;
4342 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4346 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4347 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4355 /* which disk is going to be split? */
4356 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4362 /* look it up in the spa */
4363 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4364 if (vml[c] == NULL) {
4369 /* make sure there's nothing stopping the split */
4370 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4371 vml[c]->vdev_islog ||
4372 vml[c]->vdev_ishole ||
4373 vml[c]->vdev_isspare ||
4374 vml[c]->vdev_isl2cache ||
4375 !vdev_writeable(vml[c]) ||
4376 vml[c]->vdev_children != 0 ||
4377 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4378 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4383 if (vdev_dtl_required(vml[c])) {
4388 /* we need certain info from the top level */
4389 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4390 vml[c]->vdev_top->vdev_ms_array) == 0);
4391 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4392 vml[c]->vdev_top->vdev_ms_shift) == 0);
4393 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4394 vml[c]->vdev_top->vdev_asize) == 0);
4395 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4396 vml[c]->vdev_top->vdev_ashift) == 0);
4400 kmem_free(vml, children * sizeof (vdev_t *));
4401 kmem_free(glist, children * sizeof (uint64_t));
4402 return (spa_vdev_exit(spa, NULL, txg, error));
4405 /* stop writers from using the disks */
4406 for (c = 0; c < children; c++) {
4408 vml[c]->vdev_offline = B_TRUE;
4410 vdev_reopen(spa->spa_root_vdev);
4413 * Temporarily record the splitting vdevs in the spa config. This
4414 * will disappear once the config is regenerated.
4416 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4417 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4418 glist, children) == 0);
4419 kmem_free(glist, children * sizeof (uint64_t));
4421 mutex_enter(&spa->spa_props_lock);
4422 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4424 mutex_exit(&spa->spa_props_lock);
4425 spa->spa_config_splitting = nvl;
4426 vdev_config_dirty(spa->spa_root_vdev);
4428 /* configure and create the new pool */
4429 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4430 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4431 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4432 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4433 spa_version(spa)) == 0);
4434 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4435 spa->spa_config_txg) == 0);
4436 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4437 spa_generate_guid(NULL)) == 0);
4438 (void) nvlist_lookup_string(props,
4439 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4441 /* add the new pool to the namespace */
4442 newspa = spa_add(newname, config, altroot);
4443 newspa->spa_config_txg = spa->spa_config_txg;
4444 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4446 /* release the spa config lock, retaining the namespace lock */
4447 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4449 if (zio_injection_enabled)
4450 zio_handle_panic_injection(spa, FTAG, 1);
4452 spa_activate(newspa, spa_mode_global);
4453 spa_async_suspend(newspa);
4456 /* mark that we are creating new spa by splitting */
4457 newspa->spa_splitting_newspa = B_TRUE;
4459 /* create the new pool from the disks of the original pool */
4460 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4462 newspa->spa_splitting_newspa = B_FALSE;
4467 /* if that worked, generate a real config for the new pool */
4468 if (newspa->spa_root_vdev != NULL) {
4469 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4470 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4471 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4472 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4473 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4478 if (props != NULL) {
4479 spa_configfile_set(newspa, props, B_FALSE);
4480 error = spa_prop_set(newspa, props);
4485 /* flush everything */
4486 txg = spa_vdev_config_enter(newspa);
4487 vdev_config_dirty(newspa->spa_root_vdev);
4488 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4490 if (zio_injection_enabled)
4491 zio_handle_panic_injection(spa, FTAG, 2);
4493 spa_async_resume(newspa);
4495 /* finally, update the original pool's config */
4496 txg = spa_vdev_config_enter(spa);
4497 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4498 error = dmu_tx_assign(tx, TXG_WAIT);
4501 for (c = 0; c < children; c++) {
4502 if (vml[c] != NULL) {
4505 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4511 vdev_config_dirty(spa->spa_root_vdev);
4512 spa->spa_config_splitting = NULL;
4516 (void) spa_vdev_exit(spa, NULL, txg, 0);
4518 if (zio_injection_enabled)
4519 zio_handle_panic_injection(spa, FTAG, 3);
4521 /* split is complete; log a history record */
4522 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
4523 "split new pool %s from pool %s", newname, spa_name(spa));
4525 kmem_free(vml, children * sizeof (vdev_t *));
4527 /* if we're not going to mount the filesystems in userland, export */
4529 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
4536 spa_deactivate(newspa);
4539 txg = spa_vdev_config_enter(spa);
4541 /* re-online all offlined disks */
4542 for (c = 0; c < children; c++) {
4544 vml[c]->vdev_offline = B_FALSE;
4546 vdev_reopen(spa->spa_root_vdev);
4548 nvlist_free(spa->spa_config_splitting);
4549 spa->spa_config_splitting = NULL;
4550 (void) spa_vdev_exit(spa, NULL, txg, error);
4552 kmem_free(vml, children * sizeof (vdev_t *));
4557 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
4559 for (int i = 0; i < count; i++) {
4562 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
4565 if (guid == target_guid)
4573 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
4574 nvlist_t *dev_to_remove)
4576 nvlist_t **newdev = NULL;
4579 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
4581 for (int i = 0, j = 0; i < count; i++) {
4582 if (dev[i] == dev_to_remove)
4584 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
4587 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
4588 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
4590 for (int i = 0; i < count - 1; i++)
4591 nvlist_free(newdev[i]);
4594 kmem_free(newdev, (count - 1) * sizeof (void *));
4598 * Evacuate the device.
4601 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
4606 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4607 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4608 ASSERT(vd == vd->vdev_top);
4611 * Evacuate the device. We don't hold the config lock as writer
4612 * since we need to do I/O but we do keep the
4613 * spa_namespace_lock held. Once this completes the device
4614 * should no longer have any blocks allocated on it.
4616 if (vd->vdev_islog) {
4617 if (vd->vdev_stat.vs_alloc != 0)
4618 error = spa_offline_log(spa);
4627 * The evacuation succeeded. Remove any remaining MOS metadata
4628 * associated with this vdev, and wait for these changes to sync.
4630 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
4631 txg = spa_vdev_config_enter(spa);
4632 vd->vdev_removing = B_TRUE;
4633 vdev_dirty(vd, 0, NULL, txg);
4634 vdev_config_dirty(vd);
4635 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4641 * Complete the removal by cleaning up the namespace.
4644 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
4646 vdev_t *rvd = spa->spa_root_vdev;
4647 uint64_t id = vd->vdev_id;
4648 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
4650 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4651 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4652 ASSERT(vd == vd->vdev_top);
4655 * Only remove any devices which are empty.
4657 if (vd->vdev_stat.vs_alloc != 0)
4660 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4662 if (list_link_active(&vd->vdev_state_dirty_node))
4663 vdev_state_clean(vd);
4664 if (list_link_active(&vd->vdev_config_dirty_node))
4665 vdev_config_clean(vd);
4670 vdev_compact_children(rvd);
4672 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
4673 vdev_add_child(rvd, vd);
4675 vdev_config_dirty(rvd);
4678 * Reassess the health of our root vdev.
4684 * Remove a device from the pool -
4686 * Removing a device from the vdev namespace requires several steps
4687 * and can take a significant amount of time. As a result we use
4688 * the spa_vdev_config_[enter/exit] functions which allow us to
4689 * grab and release the spa_config_lock while still holding the namespace
4690 * lock. During each step the configuration is synced out.
4694 * Remove a device from the pool. Currently, this supports removing only hot
4695 * spares, slogs, and level 2 ARC devices.
4698 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
4701 metaslab_group_t *mg;
4702 nvlist_t **spares, **l2cache, *nv;
4704 uint_t nspares, nl2cache;
4706 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
4708 ASSERT(spa_writeable(spa));
4711 txg = spa_vdev_enter(spa);
4713 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4715 if (spa->spa_spares.sav_vdevs != NULL &&
4716 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
4717 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
4718 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
4720 * Only remove the hot spare if it's not currently in use
4723 if (vd == NULL || unspare) {
4724 spa_vdev_remove_aux(spa->spa_spares.sav_config,
4725 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
4726 spa_load_spares(spa);
4727 spa->spa_spares.sav_sync = B_TRUE;
4731 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
4732 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
4733 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
4734 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
4736 * Cache devices can always be removed.
4738 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
4739 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
4740 spa_load_l2cache(spa);
4741 spa->spa_l2cache.sav_sync = B_TRUE;
4742 } else if (vd != NULL && vd->vdev_islog) {
4744 ASSERT(vd == vd->vdev_top);
4747 * XXX - Once we have bp-rewrite this should
4748 * become the common case.
4754 * Stop allocating from this vdev.
4756 metaslab_group_passivate(mg);
4759 * Wait for the youngest allocations and frees to sync,
4760 * and then wait for the deferral of those frees to finish.
4762 spa_vdev_config_exit(spa, NULL,
4763 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
4766 * Attempt to evacuate the vdev.
4768 error = spa_vdev_remove_evacuate(spa, vd);
4770 txg = spa_vdev_config_enter(spa);
4773 * If we couldn't evacuate the vdev, unwind.
4776 metaslab_group_activate(mg);
4777 return (spa_vdev_exit(spa, NULL, txg, error));
4781 * Clean up the vdev namespace.
4783 spa_vdev_remove_from_namespace(spa, vd);
4785 } else if (vd != NULL) {
4787 * Normal vdevs cannot be removed (yet).
4792 * There is no vdev of any kind with the specified guid.
4798 return (spa_vdev_exit(spa, NULL, txg, error));
4804 * Find any device that's done replacing, or a vdev marked 'unspare' that's
4805 * current spared, so we can detach it.
4808 spa_vdev_resilver_done_hunt(vdev_t *vd)
4810 vdev_t *newvd, *oldvd;
4812 for (int c = 0; c < vd->vdev_children; c++) {
4813 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
4819 * Check for a completed replacement. We always consider the first
4820 * vdev in the list to be the oldest vdev, and the last one to be
4821 * the newest (see spa_vdev_attach() for how that works). In
4822 * the case where the newest vdev is faulted, we will not automatically
4823 * remove it after a resilver completes. This is OK as it will require
4824 * user intervention to determine which disk the admin wishes to keep.
4826 if (vd->vdev_ops == &vdev_replacing_ops) {
4827 ASSERT(vd->vdev_children > 1);
4829 newvd = vd->vdev_child[vd->vdev_children - 1];
4830 oldvd = vd->vdev_child[0];
4832 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
4833 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4834 !vdev_dtl_required(oldvd))
4839 * Check for a completed resilver with the 'unspare' flag set.
4841 if (vd->vdev_ops == &vdev_spare_ops) {
4842 vdev_t *first = vd->vdev_child[0];
4843 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
4845 if (last->vdev_unspare) {
4848 } else if (first->vdev_unspare) {
4855 if (oldvd != NULL &&
4856 vdev_dtl_empty(newvd, DTL_MISSING) &&
4857 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4858 !vdev_dtl_required(oldvd))
4862 * If there are more than two spares attached to a disk,
4863 * and those spares are not required, then we want to
4864 * attempt to free them up now so that they can be used
4865 * by other pools. Once we're back down to a single
4866 * disk+spare, we stop removing them.
4868 if (vd->vdev_children > 2) {
4869 newvd = vd->vdev_child[1];
4871 if (newvd->vdev_isspare && last->vdev_isspare &&
4872 vdev_dtl_empty(last, DTL_MISSING) &&
4873 vdev_dtl_empty(last, DTL_OUTAGE) &&
4874 !vdev_dtl_required(newvd))
4883 spa_vdev_resilver_done(spa_t *spa)
4885 vdev_t *vd, *pvd, *ppvd;
4886 uint64_t guid, sguid, pguid, ppguid;
4888 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4890 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
4891 pvd = vd->vdev_parent;
4892 ppvd = pvd->vdev_parent;
4893 guid = vd->vdev_guid;
4894 pguid = pvd->vdev_guid;
4895 ppguid = ppvd->vdev_guid;
4898 * If we have just finished replacing a hot spared device, then
4899 * we need to detach the parent's first child (the original hot
4902 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
4903 ppvd->vdev_children == 2) {
4904 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
4905 sguid = ppvd->vdev_child[1]->vdev_guid;
4907 spa_config_exit(spa, SCL_ALL, FTAG);
4908 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
4910 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
4912 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4915 spa_config_exit(spa, SCL_ALL, FTAG);
4919 * Update the stored path or FRU for this vdev.
4922 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
4926 boolean_t sync = B_FALSE;
4928 ASSERT(spa_writeable(spa));
4930 spa_vdev_state_enter(spa, SCL_ALL);
4932 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4933 return (spa_vdev_state_exit(spa, NULL, ENOENT));
4935 if (!vd->vdev_ops->vdev_op_leaf)
4936 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
4939 if (strcmp(value, vd->vdev_path) != 0) {
4940 spa_strfree(vd->vdev_path);
4941 vd->vdev_path = spa_strdup(value);
4945 if (vd->vdev_fru == NULL) {
4946 vd->vdev_fru = spa_strdup(value);
4948 } else if (strcmp(value, vd->vdev_fru) != 0) {
4949 spa_strfree(vd->vdev_fru);
4950 vd->vdev_fru = spa_strdup(value);
4955 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
4959 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
4961 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
4965 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
4967 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
4971 * ==========================================================================
4973 * ==========================================================================
4977 spa_scan_stop(spa_t *spa)
4979 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4980 if (dsl_scan_resilvering(spa->spa_dsl_pool))
4982 return (dsl_scan_cancel(spa->spa_dsl_pool));
4986 spa_scan(spa_t *spa, pool_scan_func_t func)
4988 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4990 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
4994 * If a resilver was requested, but there is no DTL on a
4995 * writeable leaf device, we have nothing to do.
4997 if (func == POOL_SCAN_RESILVER &&
4998 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
4999 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5003 return (dsl_scan(spa->spa_dsl_pool, func));
5007 * ==========================================================================
5008 * SPA async task processing
5009 * ==========================================================================
5013 spa_async_remove(spa_t *spa, vdev_t *vd)
5015 if (vd->vdev_remove_wanted) {
5016 vd->vdev_remove_wanted = B_FALSE;
5017 vd->vdev_delayed_close = B_FALSE;
5018 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5021 * We want to clear the stats, but we don't want to do a full
5022 * vdev_clear() as that will cause us to throw away
5023 * degraded/faulted state as well as attempt to reopen the
5024 * device, all of which is a waste.
5026 vd->vdev_stat.vs_read_errors = 0;
5027 vd->vdev_stat.vs_write_errors = 0;
5028 vd->vdev_stat.vs_checksum_errors = 0;
5030 vdev_state_dirty(vd->vdev_top);
5033 for (int c = 0; c < vd->vdev_children; c++)
5034 spa_async_remove(spa, vd->vdev_child[c]);
5038 spa_async_probe(spa_t *spa, vdev_t *vd)
5040 if (vd->vdev_probe_wanted) {
5041 vd->vdev_probe_wanted = B_FALSE;
5042 vdev_reopen(vd); /* vdev_open() does the actual probe */
5045 for (int c = 0; c < vd->vdev_children; c++)
5046 spa_async_probe(spa, vd->vdev_child[c]);
5050 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5056 if (!spa->spa_autoexpand)
5059 for (int c = 0; c < vd->vdev_children; c++) {
5060 vdev_t *cvd = vd->vdev_child[c];
5061 spa_async_autoexpand(spa, cvd);
5064 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5067 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5068 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5070 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5071 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5073 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5074 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5077 kmem_free(physpath, MAXPATHLEN);
5081 spa_async_thread(void *arg)
5086 ASSERT(spa->spa_sync_on);
5088 mutex_enter(&spa->spa_async_lock);
5089 tasks = spa->spa_async_tasks;
5090 spa->spa_async_tasks = 0;
5091 mutex_exit(&spa->spa_async_lock);
5094 * See if the config needs to be updated.
5096 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5097 uint64_t old_space, new_space;
5099 mutex_enter(&spa_namespace_lock);
5100 old_space = metaslab_class_get_space(spa_normal_class(spa));
5101 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5102 new_space = metaslab_class_get_space(spa_normal_class(spa));
5103 mutex_exit(&spa_namespace_lock);
5106 * If the pool grew as a result of the config update,
5107 * then log an internal history event.
5109 if (new_space != old_space) {
5110 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5112 "pool '%s' size: %llu(+%llu)",
5113 spa_name(spa), new_space, new_space - old_space);
5118 * See if any devices need to be marked REMOVED.
5120 if (tasks & SPA_ASYNC_REMOVE) {
5121 spa_vdev_state_enter(spa, SCL_NONE);
5122 spa_async_remove(spa, spa->spa_root_vdev);
5123 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5124 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5125 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5126 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5127 (void) spa_vdev_state_exit(spa, NULL, 0);
5130 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5131 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5132 spa_async_autoexpand(spa, spa->spa_root_vdev);
5133 spa_config_exit(spa, SCL_CONFIG, FTAG);
5137 * See if any devices need to be probed.
5139 if (tasks & SPA_ASYNC_PROBE) {
5140 spa_vdev_state_enter(spa, SCL_NONE);
5141 spa_async_probe(spa, spa->spa_root_vdev);
5142 (void) spa_vdev_state_exit(spa, NULL, 0);
5146 * If any devices are done replacing, detach them.
5148 if (tasks & SPA_ASYNC_RESILVER_DONE)
5149 spa_vdev_resilver_done(spa);
5152 * Kick off a resilver.
5154 if (tasks & SPA_ASYNC_RESILVER)
5155 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5158 * Let the world know that we're done.
5160 mutex_enter(&spa->spa_async_lock);
5161 spa->spa_async_thread = NULL;
5162 cv_broadcast(&spa->spa_async_cv);
5163 mutex_exit(&spa->spa_async_lock);
5168 spa_async_suspend(spa_t *spa)
5170 mutex_enter(&spa->spa_async_lock);
5171 spa->spa_async_suspended++;
5172 while (spa->spa_async_thread != NULL)
5173 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5174 mutex_exit(&spa->spa_async_lock);
5178 spa_async_resume(spa_t *spa)
5180 mutex_enter(&spa->spa_async_lock);
5181 ASSERT(spa->spa_async_suspended != 0);
5182 spa->spa_async_suspended--;
5183 mutex_exit(&spa->spa_async_lock);
5187 spa_async_dispatch(spa_t *spa)
5189 mutex_enter(&spa->spa_async_lock);
5190 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5191 spa->spa_async_thread == NULL &&
5192 rootdir != NULL && !vn_is_readonly(rootdir))
5193 spa->spa_async_thread = thread_create(NULL, 0,
5194 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5195 mutex_exit(&spa->spa_async_lock);
5199 spa_async_request(spa_t *spa, int task)
5201 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5202 mutex_enter(&spa->spa_async_lock);
5203 spa->spa_async_tasks |= task;
5204 mutex_exit(&spa->spa_async_lock);
5208 * ==========================================================================
5209 * SPA syncing routines
5210 * ==========================================================================
5214 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5217 bpobj_enqueue(bpo, bp, tx);
5222 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5226 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5232 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5234 char *packed = NULL;
5239 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5242 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5243 * information. This avoids the dbuf_will_dirty() path and
5244 * saves us a pre-read to get data we don't actually care about.
5246 bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
5247 packed = kmem_alloc(bufsize, KM_SLEEP);
5249 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5251 bzero(packed + nvsize, bufsize - nvsize);
5253 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5255 kmem_free(packed, bufsize);
5257 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5258 dmu_buf_will_dirty(db, tx);
5259 *(uint64_t *)db->db_data = nvsize;
5260 dmu_buf_rele(db, FTAG);
5264 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5265 const char *config, const char *entry)
5275 * Update the MOS nvlist describing the list of available devices.
5276 * spa_validate_aux() will have already made sure this nvlist is
5277 * valid and the vdevs are labeled appropriately.
5279 if (sav->sav_object == 0) {
5280 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5281 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5282 sizeof (uint64_t), tx);
5283 VERIFY(zap_update(spa->spa_meta_objset,
5284 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5285 &sav->sav_object, tx) == 0);
5288 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5289 if (sav->sav_count == 0) {
5290 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5292 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5293 for (i = 0; i < sav->sav_count; i++)
5294 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5295 B_FALSE, VDEV_CONFIG_L2CACHE);
5296 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5297 sav->sav_count) == 0);
5298 for (i = 0; i < sav->sav_count; i++)
5299 nvlist_free(list[i]);
5300 kmem_free(list, sav->sav_count * sizeof (void *));
5303 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5304 nvlist_free(nvroot);
5306 sav->sav_sync = B_FALSE;
5310 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5314 if (list_is_empty(&spa->spa_config_dirty_list))
5317 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5319 config = spa_config_generate(spa, spa->spa_root_vdev,
5320 dmu_tx_get_txg(tx), B_FALSE);
5322 spa_config_exit(spa, SCL_STATE, FTAG);
5324 if (spa->spa_config_syncing)
5325 nvlist_free(spa->spa_config_syncing);
5326 spa->spa_config_syncing = config;
5328 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5332 * Set zpool properties.
5335 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5338 objset_t *mos = spa->spa_meta_objset;
5339 nvlist_t *nvp = arg2;
5344 const char *propname;
5345 zprop_type_t proptype;
5347 mutex_enter(&spa->spa_props_lock);
5350 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5351 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
5352 case ZPOOL_PROP_VERSION:
5354 * Only set version for non-zpool-creation cases
5355 * (set/import). spa_create() needs special care
5356 * for version setting.
5358 if (tx->tx_txg != TXG_INITIAL) {
5359 VERIFY(nvpair_value_uint64(elem,
5361 ASSERT(intval <= SPA_VERSION);
5362 ASSERT(intval >= spa_version(spa));
5363 spa->spa_uberblock.ub_version = intval;
5364 vdev_config_dirty(spa->spa_root_vdev);
5368 case ZPOOL_PROP_ALTROOT:
5370 * 'altroot' is a non-persistent property. It should
5371 * have been set temporarily at creation or import time.
5373 ASSERT(spa->spa_root != NULL);
5376 case ZPOOL_PROP_READONLY:
5377 case ZPOOL_PROP_CACHEFILE:
5379 * 'readonly' and 'cachefile' are also non-persisitent
5385 * Set pool property values in the poolprops mos object.
5387 if (spa->spa_pool_props_object == 0) {
5388 VERIFY((spa->spa_pool_props_object =
5389 zap_create(mos, DMU_OT_POOL_PROPS,
5390 DMU_OT_NONE, 0, tx)) > 0);
5392 VERIFY(zap_update(mos,
5393 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5394 8, 1, &spa->spa_pool_props_object, tx)
5398 /* normalize the property name */
5399 propname = zpool_prop_to_name(prop);
5400 proptype = zpool_prop_get_type(prop);
5402 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5403 ASSERT(proptype == PROP_TYPE_STRING);
5404 VERIFY(nvpair_value_string(elem, &strval) == 0);
5405 VERIFY(zap_update(mos,
5406 spa->spa_pool_props_object, propname,
5407 1, strlen(strval) + 1, strval, tx) == 0);
5409 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5410 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5412 if (proptype == PROP_TYPE_INDEX) {
5414 VERIFY(zpool_prop_index_to_string(
5415 prop, intval, &unused) == 0);
5417 VERIFY(zap_update(mos,
5418 spa->spa_pool_props_object, propname,
5419 8, 1, &intval, tx) == 0);
5421 ASSERT(0); /* not allowed */
5425 case ZPOOL_PROP_DELEGATION:
5426 spa->spa_delegation = intval;
5428 case ZPOOL_PROP_BOOTFS:
5429 spa->spa_bootfs = intval;
5431 case ZPOOL_PROP_FAILUREMODE:
5432 spa->spa_failmode = intval;
5434 case ZPOOL_PROP_AUTOEXPAND:
5435 spa->spa_autoexpand = intval;
5436 if (tx->tx_txg != TXG_INITIAL)
5437 spa_async_request(spa,
5438 SPA_ASYNC_AUTOEXPAND);
5440 case ZPOOL_PROP_DEDUPDITTO:
5441 spa->spa_dedup_ditto = intval;
5448 /* log internal history if this is not a zpool create */
5449 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5450 tx->tx_txg != TXG_INITIAL) {
5451 spa_history_log_internal(LOG_POOL_PROPSET,
5452 spa, tx, "%s %lld %s",
5453 nvpair_name(elem), intval, spa_name(spa));
5457 mutex_exit(&spa->spa_props_lock);
5461 * Perform one-time upgrade on-disk changes. spa_version() does not
5462 * reflect the new version this txg, so there must be no changes this
5463 * txg to anything that the upgrade code depends on after it executes.
5464 * Therefore this must be called after dsl_pool_sync() does the sync
5468 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5470 dsl_pool_t *dp = spa->spa_dsl_pool;
5472 ASSERT(spa->spa_sync_pass == 1);
5474 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5475 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5476 dsl_pool_create_origin(dp, tx);
5478 /* Keeping the origin open increases spa_minref */
5479 spa->spa_minref += 3;
5482 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5483 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5484 dsl_pool_upgrade_clones(dp, tx);
5487 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5488 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5489 dsl_pool_upgrade_dir_clones(dp, tx);
5491 /* Keeping the freedir open increases spa_minref */
5492 spa->spa_minref += 3;
5497 * Sync the specified transaction group. New blocks may be dirtied as
5498 * part of the process, so we iterate until it converges.
5501 spa_sync(spa_t *spa, uint64_t txg)
5503 dsl_pool_t *dp = spa->spa_dsl_pool;
5504 objset_t *mos = spa->spa_meta_objset;
5505 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
5506 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
5507 vdev_t *rvd = spa->spa_root_vdev;
5512 VERIFY(spa_writeable(spa));
5515 * Lock out configuration changes.
5517 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5519 spa->spa_syncing_txg = txg;
5520 spa->spa_sync_pass = 0;
5523 * If there are any pending vdev state changes, convert them
5524 * into config changes that go out with this transaction group.
5526 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5527 while (list_head(&spa->spa_state_dirty_list) != NULL) {
5529 * We need the write lock here because, for aux vdevs,
5530 * calling vdev_config_dirty() modifies sav_config.
5531 * This is ugly and will become unnecessary when we
5532 * eliminate the aux vdev wart by integrating all vdevs
5533 * into the root vdev tree.
5535 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5536 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
5537 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
5538 vdev_state_clean(vd);
5539 vdev_config_dirty(vd);
5541 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5542 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
5544 spa_config_exit(spa, SCL_STATE, FTAG);
5546 tx = dmu_tx_create_assigned(dp, txg);
5549 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5550 * set spa_deflate if we have no raid-z vdevs.
5552 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
5553 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
5556 for (i = 0; i < rvd->vdev_children; i++) {
5557 vd = rvd->vdev_child[i];
5558 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
5561 if (i == rvd->vdev_children) {
5562 spa->spa_deflate = TRUE;
5563 VERIFY(0 == zap_add(spa->spa_meta_objset,
5564 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
5565 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
5570 * If anything has changed in this txg, or if someone is waiting
5571 * for this txg to sync (eg, spa_vdev_remove()), push the
5572 * deferred frees from the previous txg. If not, leave them
5573 * alone so that we don't generate work on an otherwise idle
5576 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
5577 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
5578 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
5579 ((dsl_scan_active(dp->dp_scan) ||
5580 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
5581 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5582 VERIFY3U(bpobj_iterate(defer_bpo,
5583 spa_free_sync_cb, zio, tx), ==, 0);
5584 VERIFY3U(zio_wait(zio), ==, 0);
5588 * Iterate to convergence.
5591 int pass = ++spa->spa_sync_pass;
5593 spa_sync_config_object(spa, tx);
5594 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
5595 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
5596 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
5597 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
5598 spa_errlog_sync(spa, txg);
5599 dsl_pool_sync(dp, txg);
5601 if (pass <= SYNC_PASS_DEFERRED_FREE) {
5602 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5603 bplist_iterate(free_bpl, spa_free_sync_cb,
5605 VERIFY(zio_wait(zio) == 0);
5607 bplist_iterate(free_bpl, bpobj_enqueue_cb,
5612 dsl_scan_sync(dp, tx);
5614 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
5618 spa_sync_upgrades(spa, tx);
5620 } while (dmu_objset_is_dirty(mos, txg));
5623 * Rewrite the vdev configuration (which includes the uberblock)
5624 * to commit the transaction group.
5626 * If there are no dirty vdevs, we sync the uberblock to a few
5627 * random top-level vdevs that are known to be visible in the
5628 * config cache (see spa_vdev_add() for a complete description).
5629 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5633 * We hold SCL_STATE to prevent vdev open/close/etc.
5634 * while we're attempting to write the vdev labels.
5636 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5638 if (list_is_empty(&spa->spa_config_dirty_list)) {
5639 vdev_t *svd[SPA_DVAS_PER_BP];
5641 int children = rvd->vdev_children;
5642 int c0 = spa_get_random(children);
5644 for (int c = 0; c < children; c++) {
5645 vd = rvd->vdev_child[(c0 + c) % children];
5646 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
5648 svd[svdcount++] = vd;
5649 if (svdcount == SPA_DVAS_PER_BP)
5652 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
5654 error = vdev_config_sync(svd, svdcount, txg,
5657 error = vdev_config_sync(rvd->vdev_child,
5658 rvd->vdev_children, txg, B_FALSE);
5660 error = vdev_config_sync(rvd->vdev_child,
5661 rvd->vdev_children, txg, B_TRUE);
5664 spa_config_exit(spa, SCL_STATE, FTAG);
5668 zio_suspend(spa, NULL);
5669 zio_resume_wait(spa);
5674 * Clear the dirty config list.
5676 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
5677 vdev_config_clean(vd);
5680 * Now that the new config has synced transactionally,
5681 * let it become visible to the config cache.
5683 if (spa->spa_config_syncing != NULL) {
5684 spa_config_set(spa, spa->spa_config_syncing);
5685 spa->spa_config_txg = txg;
5686 spa->spa_config_syncing = NULL;
5689 spa->spa_ubsync = spa->spa_uberblock;
5691 dsl_pool_sync_done(dp, txg);
5694 * Update usable space statistics.
5696 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5697 vdev_sync_done(vd, txg);
5699 spa_update_dspace(spa);
5702 * It had better be the case that we didn't dirty anything
5703 * since vdev_config_sync().
5705 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5706 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5707 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
5709 spa->spa_sync_pass = 0;
5711 spa_config_exit(spa, SCL_CONFIG, FTAG);
5713 spa_handle_ignored_writes(spa);
5716 * If any async tasks have been requested, kick them off.
5718 spa_async_dispatch(spa);
5722 * Sync all pools. We don't want to hold the namespace lock across these
5723 * operations, so we take a reference on the spa_t and drop the lock during the
5727 spa_sync_allpools(void)
5730 mutex_enter(&spa_namespace_lock);
5731 while ((spa = spa_next(spa)) != NULL) {
5732 if (spa_state(spa) != POOL_STATE_ACTIVE ||
5733 !spa_writeable(spa) || spa_suspended(spa))
5735 spa_open_ref(spa, FTAG);
5736 mutex_exit(&spa_namespace_lock);
5737 txg_wait_synced(spa_get_dsl(spa), 0);
5738 mutex_enter(&spa_namespace_lock);
5739 spa_close(spa, FTAG);
5741 mutex_exit(&spa_namespace_lock);
5745 * ==========================================================================
5746 * Miscellaneous routines
5747 * ==========================================================================
5751 * Remove all pools in the system.
5759 * Remove all cached state. All pools should be closed now,
5760 * so every spa in the AVL tree should be unreferenced.
5762 mutex_enter(&spa_namespace_lock);
5763 while ((spa = spa_next(NULL)) != NULL) {
5765 * Stop async tasks. The async thread may need to detach
5766 * a device that's been replaced, which requires grabbing
5767 * spa_namespace_lock, so we must drop it here.
5769 spa_open_ref(spa, FTAG);
5770 mutex_exit(&spa_namespace_lock);
5771 spa_async_suspend(spa);
5772 mutex_enter(&spa_namespace_lock);
5773 spa_close(spa, FTAG);
5775 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5777 spa_deactivate(spa);
5781 mutex_exit(&spa_namespace_lock);
5785 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
5790 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
5794 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
5795 vd = spa->spa_l2cache.sav_vdevs[i];
5796 if (vd->vdev_guid == guid)
5800 for (i = 0; i < spa->spa_spares.sav_count; i++) {
5801 vd = spa->spa_spares.sav_vdevs[i];
5802 if (vd->vdev_guid == guid)
5811 spa_upgrade(spa_t *spa, uint64_t version)
5813 ASSERT(spa_writeable(spa));
5815 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5818 * This should only be called for a non-faulted pool, and since a
5819 * future version would result in an unopenable pool, this shouldn't be
5822 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
5823 ASSERT(version >= spa->spa_uberblock.ub_version);
5825 spa->spa_uberblock.ub_version = version;
5826 vdev_config_dirty(spa->spa_root_vdev);
5828 spa_config_exit(spa, SCL_ALL, FTAG);
5830 txg_wait_synced(spa_get_dsl(spa), 0);
5834 spa_has_spare(spa_t *spa, uint64_t guid)
5838 spa_aux_vdev_t *sav = &spa->spa_spares;
5840 for (i = 0; i < sav->sav_count; i++)
5841 if (sav->sav_vdevs[i]->vdev_guid == guid)
5844 for (i = 0; i < sav->sav_npending; i++) {
5845 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
5846 &spareguid) == 0 && spareguid == guid)
5854 * Check if a pool has an active shared spare device.
5855 * Note: reference count of an active spare is 2, as a spare and as a replace
5858 spa_has_active_shared_spare(spa_t *spa)
5862 spa_aux_vdev_t *sav = &spa->spa_spares;
5864 for (i = 0; i < sav->sav_count; i++) {
5865 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
5866 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
5875 * Post a sysevent corresponding to the given event. The 'name' must be one of
5876 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
5877 * filled in from the spa and (optionally) the vdev. This doesn't do anything
5878 * in the userland libzpool, as we don't want consumers to misinterpret ztest
5879 * or zdb as real changes.
5882 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
5886 sysevent_attr_list_t *attr = NULL;
5887 sysevent_value_t value;
5890 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
5893 value.value_type = SE_DATA_TYPE_STRING;
5894 value.value.sv_string = spa_name(spa);
5895 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
5898 value.value_type = SE_DATA_TYPE_UINT64;
5899 value.value.sv_uint64 = spa_guid(spa);
5900 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
5904 value.value_type = SE_DATA_TYPE_UINT64;
5905 value.value.sv_uint64 = vd->vdev_guid;
5906 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
5910 if (vd->vdev_path) {
5911 value.value_type = SE_DATA_TYPE_STRING;
5912 value.value.sv_string = vd->vdev_path;
5913 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
5914 &value, SE_SLEEP) != 0)
5919 if (sysevent_attach_attributes(ev, attr) != 0)
5923 (void) log_sysevent(ev, SE_SLEEP, &eid);
5927 sysevent_free_attr(attr);