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/systeminfo.h>
60 #include <sys/spa_boot.h>
61 #include <sys/zfs_ioctl.h>
62 #include <sys/dsl_scan.h>
65 #include <sys/bootprops.h>
66 #include <sys/callb.h>
67 #include <sys/cpupart.h>
69 #include <sys/sysdc.h>
74 #include "zfs_comutil.h"
76 typedef enum zti_modes {
77 zti_mode_fixed, /* value is # of threads (min 1) */
78 zti_mode_online_percent, /* value is % of online CPUs */
79 zti_mode_batch, /* cpu-intensive; value is ignored */
80 zti_mode_null, /* don't create a taskq */
84 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
85 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
86 #define ZTI_BATCH { zti_mode_batch, 0 }
87 #define ZTI_NULL { zti_mode_null, 0 }
89 #define ZTI_ONE ZTI_FIX(1)
91 typedef struct zio_taskq_info {
92 enum zti_modes zti_mode;
96 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
97 "issue", "issue_high", "intr", "intr_high"
101 * Define the taskq threads for the following I/O types:
102 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
104 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
105 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
106 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
107 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
108 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
109 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
110 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
111 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
114 static dsl_syncfunc_t spa_sync_props;
115 static boolean_t spa_has_active_shared_spare(spa_t *spa);
116 static inline int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
117 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
119 static void spa_vdev_resilver_done(spa_t *spa);
121 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
122 id_t zio_taskq_psrset_bind = PS_NONE;
123 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
124 uint_t zio_taskq_basedc = 80; /* base duty cycle */
126 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
129 * This (illegal) pool name is used when temporarily importing a spa_t in order
130 * to get the vdev stats associated with the imported devices.
132 #define TRYIMPORT_NAME "$import"
135 * ==========================================================================
136 * SPA properties routines
137 * ==========================================================================
141 * Add a (source=src, propname=propval) list to an nvlist.
144 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
145 uint64_t intval, zprop_source_t src)
147 const char *propname = zpool_prop_to_name(prop);
150 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
151 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
154 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
156 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
158 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
159 nvlist_free(propval);
163 * Get property values from the spa configuration.
166 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
170 uint64_t cap, version;
171 zprop_source_t src = ZPROP_SRC_NONE;
172 spa_config_dirent_t *dp;
174 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
176 if (spa->spa_root_vdev != NULL) {
177 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
178 size = metaslab_class_get_space(spa_normal_class(spa));
179 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
180 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
181 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
182 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
184 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
185 (spa_mode(spa) == FREAD), src);
187 cap = (size == 0) ? 0 : (alloc * 100 / size);
188 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
190 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
191 ddt_get_pool_dedup_ratio(spa), src);
193 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
194 spa->spa_root_vdev->vdev_state, src);
196 version = spa_version(spa);
197 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
198 src = ZPROP_SRC_DEFAULT;
200 src = ZPROP_SRC_LOCAL;
201 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
204 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
206 if (spa->spa_root != NULL)
207 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
210 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
211 if (dp->scd_path == NULL) {
212 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
213 "none", 0, ZPROP_SRC_LOCAL);
214 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
215 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
216 dp->scd_path, 0, ZPROP_SRC_LOCAL);
222 * Get zpool property values.
225 spa_prop_get(spa_t *spa, nvlist_t **nvp)
227 objset_t *mos = spa->spa_meta_objset;
232 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
234 mutex_enter(&spa->spa_props_lock);
237 * Get properties from the spa config.
239 spa_prop_get_config(spa, nvp);
241 /* If no pool property object, no more prop to get. */
242 if (mos == NULL || spa->spa_pool_props_object == 0) {
243 mutex_exit(&spa->spa_props_lock);
248 * Get properties from the MOS pool property object.
250 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
251 (err = zap_cursor_retrieve(&zc, &za)) == 0;
252 zap_cursor_advance(&zc)) {
255 zprop_source_t src = ZPROP_SRC_DEFAULT;
258 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
261 switch (za.za_integer_length) {
263 /* integer property */
264 if (za.za_first_integer !=
265 zpool_prop_default_numeric(prop))
266 src = ZPROP_SRC_LOCAL;
268 if (prop == ZPOOL_PROP_BOOTFS) {
270 dsl_dataset_t *ds = NULL;
272 dp = spa_get_dsl(spa);
273 rw_enter(&dp->dp_config_rwlock, RW_READER);
274 if ((err = dsl_dataset_hold_obj(dp,
275 za.za_first_integer, FTAG, &ds))) {
276 rw_exit(&dp->dp_config_rwlock);
281 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
283 dsl_dataset_name(ds, strval);
284 dsl_dataset_rele(ds, FTAG);
285 rw_exit(&dp->dp_config_rwlock);
288 intval = za.za_first_integer;
291 spa_prop_add_list(*nvp, prop, strval, intval, src);
295 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
300 /* string property */
301 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
302 err = zap_lookup(mos, spa->spa_pool_props_object,
303 za.za_name, 1, za.za_num_integers, strval);
305 kmem_free(strval, za.za_num_integers);
308 spa_prop_add_list(*nvp, prop, strval, 0, src);
309 kmem_free(strval, za.za_num_integers);
316 zap_cursor_fini(&zc);
317 mutex_exit(&spa->spa_props_lock);
319 if (err && err != ENOENT) {
329 * Validate the given pool properties nvlist and modify the list
330 * for the property values to be set.
333 spa_prop_validate(spa_t *spa, nvlist_t *props)
336 int error = 0, reset_bootfs = 0;
340 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
342 char *propname, *strval;
347 propname = nvpair_name(elem);
349 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
353 case ZPOOL_PROP_VERSION:
354 error = nvpair_value_uint64(elem, &intval);
356 (intval < spa_version(spa) || intval > SPA_VERSION))
360 case ZPOOL_PROP_DELEGATION:
361 case ZPOOL_PROP_AUTOREPLACE:
362 case ZPOOL_PROP_LISTSNAPS:
363 case ZPOOL_PROP_AUTOEXPAND:
364 error = nvpair_value_uint64(elem, &intval);
365 if (!error && intval > 1)
369 case ZPOOL_PROP_BOOTFS:
371 * If the pool version is less than SPA_VERSION_BOOTFS,
372 * or the pool is still being created (version == 0),
373 * the bootfs property cannot be set.
375 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
381 * Make sure the vdev config is bootable
383 if (!vdev_is_bootable(spa->spa_root_vdev)) {
390 error = nvpair_value_string(elem, &strval);
395 if (strval == NULL || strval[0] == '\0') {
396 objnum = zpool_prop_default_numeric(
401 if ((error = dmu_objset_hold(strval,FTAG,&os)))
404 /* Must be ZPL and not gzip compressed. */
406 if (dmu_objset_type(os) != DMU_OST_ZFS) {
408 } else if ((error = dsl_prop_get_integer(strval,
409 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
410 &compress, NULL)) == 0 &&
411 !BOOTFS_COMPRESS_VALID(compress)) {
414 objnum = dmu_objset_id(os);
416 dmu_objset_rele(os, FTAG);
420 case ZPOOL_PROP_FAILUREMODE:
421 error = nvpair_value_uint64(elem, &intval);
422 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
423 intval > ZIO_FAILURE_MODE_PANIC))
427 * This is a special case which only occurs when
428 * the pool has completely failed. This allows
429 * the user to change the in-core failmode property
430 * without syncing it out to disk (I/Os might
431 * currently be blocked). We do this by returning
432 * EIO to the caller (spa_prop_set) to trick it
433 * into thinking we encountered a property validation
436 if (!error && spa_suspended(spa)) {
437 spa->spa_failmode = intval;
442 case ZPOOL_PROP_CACHEFILE:
443 if ((error = nvpair_value_string(elem, &strval)) != 0)
446 if (strval[0] == '\0')
449 if (strcmp(strval, "none") == 0)
452 if (strval[0] != '/') {
457 slash = strrchr(strval, '/');
458 ASSERT(slash != NULL);
460 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
461 strcmp(slash, "/..") == 0)
465 case ZPOOL_PROP_DEDUPDITTO:
466 if (spa_version(spa) < SPA_VERSION_DEDUP)
469 error = nvpair_value_uint64(elem, &intval);
471 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
483 if (!error && reset_bootfs) {
484 error = nvlist_remove(props,
485 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
488 error = nvlist_add_uint64(props,
489 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
497 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
500 spa_config_dirent_t *dp;
502 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
506 dp = kmem_alloc(sizeof (spa_config_dirent_t),
509 if (cachefile[0] == '\0')
510 dp->scd_path = spa_strdup(spa_config_path);
511 else if (strcmp(cachefile, "none") == 0)
514 dp->scd_path = spa_strdup(cachefile);
516 list_insert_head(&spa->spa_config_list, dp);
518 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
522 spa_prop_set(spa_t *spa, nvlist_t *nvp)
526 boolean_t need_sync = B_FALSE;
529 if ((error = spa_prop_validate(spa, nvp)) != 0)
533 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
534 if ((prop = zpool_name_to_prop(
535 nvpair_name(elem))) == ZPROP_INVAL)
538 if (prop == ZPOOL_PROP_CACHEFILE ||
539 prop == ZPOOL_PROP_ALTROOT ||
540 prop == ZPOOL_PROP_READONLY)
548 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
555 * If the bootfs property value is dsobj, clear it.
558 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
560 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
561 VERIFY(zap_remove(spa->spa_meta_objset,
562 spa->spa_pool_props_object,
563 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
569 * ==========================================================================
570 * SPA state manipulation (open/create/destroy/import/export)
571 * ==========================================================================
575 spa_error_entry_compare(const void *a, const void *b)
577 spa_error_entry_t *sa = (spa_error_entry_t *)a;
578 spa_error_entry_t *sb = (spa_error_entry_t *)b;
581 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
582 sizeof (zbookmark_t));
593 * Utility function which retrieves copies of the current logs and
594 * re-initializes them in the process.
597 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
599 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
601 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
602 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
604 avl_create(&spa->spa_errlist_scrub,
605 spa_error_entry_compare, sizeof (spa_error_entry_t),
606 offsetof(spa_error_entry_t, se_avl));
607 avl_create(&spa->spa_errlist_last,
608 spa_error_entry_compare, sizeof (spa_error_entry_t),
609 offsetof(spa_error_entry_t, se_avl));
613 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
616 uint_t flags = TASKQ_PREPOPULATE;
617 boolean_t batch = B_FALSE;
621 return (NULL); /* no taskq needed */
624 ASSERT3U(value, >=, 1);
625 value = MAX(value, 1);
630 flags |= TASKQ_THREADS_CPU_PCT;
631 value = zio_taskq_batch_pct;
634 case zti_mode_online_percent:
635 flags |= TASKQ_THREADS_CPU_PCT;
639 panic("unrecognized mode for %s taskq (%u:%u) in "
645 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
647 flags |= TASKQ_DC_BATCH;
649 return (taskq_create_sysdc(name, value, 50, INT_MAX,
650 spa->spa_proc, zio_taskq_basedc, flags));
652 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
653 spa->spa_proc, flags));
657 spa_create_zio_taskqs(spa_t *spa)
661 for (t = 0; t < ZIO_TYPES; t++) {
662 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
663 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
664 enum zti_modes mode = ztip->zti_mode;
665 uint_t value = ztip->zti_value;
668 (void) snprintf(name, sizeof (name),
669 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
671 spa->spa_zio_taskq[t][q] =
672 spa_taskq_create(spa, name, mode, value);
679 spa_thread(void *arg)
684 user_t *pu = PTOU(curproc);
686 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
689 ASSERT(curproc != &p0);
690 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
691 "zpool-%s", spa->spa_name);
692 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
694 /* bind this thread to the requested psrset */
695 if (zio_taskq_psrset_bind != PS_NONE) {
697 mutex_enter(&cpu_lock);
698 mutex_enter(&pidlock);
699 mutex_enter(&curproc->p_lock);
701 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
702 0, NULL, NULL) == 0) {
703 curthread->t_bind_pset = zio_taskq_psrset_bind;
706 "Couldn't bind process for zfs pool \"%s\" to "
707 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
710 mutex_exit(&curproc->p_lock);
711 mutex_exit(&pidlock);
712 mutex_exit(&cpu_lock);
716 if (zio_taskq_sysdc) {
717 sysdc_thread_enter(curthread, 100, 0);
720 spa->spa_proc = curproc;
721 spa->spa_did = curthread->t_did;
723 spa_create_zio_taskqs(spa);
725 mutex_enter(&spa->spa_proc_lock);
726 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
728 spa->spa_proc_state = SPA_PROC_ACTIVE;
729 cv_broadcast(&spa->spa_proc_cv);
731 CALLB_CPR_SAFE_BEGIN(&cprinfo);
732 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
733 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
734 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
736 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
737 spa->spa_proc_state = SPA_PROC_GONE;
739 cv_broadcast(&spa->spa_proc_cv);
740 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
742 mutex_enter(&curproc->p_lock);
748 * Activate an uninitialized pool.
751 spa_activate(spa_t *spa, int mode)
753 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
755 spa->spa_state = POOL_STATE_ACTIVE;
756 spa->spa_mode = mode;
758 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
759 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
761 /* Try to create a covering process */
762 mutex_enter(&spa->spa_proc_lock);
763 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
764 ASSERT(spa->spa_proc == &p0);
767 /* Only create a process if we're going to be around a while. */
768 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
769 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
771 spa->spa_proc_state = SPA_PROC_CREATED;
772 while (spa->spa_proc_state == SPA_PROC_CREATED) {
773 cv_wait(&spa->spa_proc_cv,
774 &spa->spa_proc_lock);
776 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
777 ASSERT(spa->spa_proc != &p0);
778 ASSERT(spa->spa_did != 0);
782 "Couldn't create process for zfs pool \"%s\"\n",
787 mutex_exit(&spa->spa_proc_lock);
789 /* If we didn't create a process, we need to create our taskqs. */
790 if (spa->spa_proc == &p0) {
791 spa_create_zio_taskqs(spa);
794 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
795 offsetof(vdev_t, vdev_config_dirty_node));
796 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
797 offsetof(vdev_t, vdev_state_dirty_node));
799 txg_list_create(&spa->spa_vdev_txg_list,
800 offsetof(struct vdev, vdev_txg_node));
802 avl_create(&spa->spa_errlist_scrub,
803 spa_error_entry_compare, sizeof (spa_error_entry_t),
804 offsetof(spa_error_entry_t, se_avl));
805 avl_create(&spa->spa_errlist_last,
806 spa_error_entry_compare, sizeof (spa_error_entry_t),
807 offsetof(spa_error_entry_t, se_avl));
811 * Opposite of spa_activate().
814 spa_deactivate(spa_t *spa)
818 ASSERT(spa->spa_sync_on == B_FALSE);
819 ASSERT(spa->spa_dsl_pool == NULL);
820 ASSERT(spa->spa_root_vdev == NULL);
821 ASSERT(spa->spa_async_zio_root == NULL);
822 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
824 txg_list_destroy(&spa->spa_vdev_txg_list);
826 list_destroy(&spa->spa_config_dirty_list);
827 list_destroy(&spa->spa_state_dirty_list);
829 for (t = 0; t < ZIO_TYPES; t++) {
830 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
831 if (spa->spa_zio_taskq[t][q] != NULL)
832 taskq_destroy(spa->spa_zio_taskq[t][q]);
833 spa->spa_zio_taskq[t][q] = NULL;
837 metaslab_class_destroy(spa->spa_normal_class);
838 spa->spa_normal_class = NULL;
840 metaslab_class_destroy(spa->spa_log_class);
841 spa->spa_log_class = NULL;
844 * If this was part of an import or the open otherwise failed, we may
845 * still have errors left in the queues. Empty them just in case.
847 spa_errlog_drain(spa);
849 avl_destroy(&spa->spa_errlist_scrub);
850 avl_destroy(&spa->spa_errlist_last);
852 spa->spa_state = POOL_STATE_UNINITIALIZED;
854 mutex_enter(&spa->spa_proc_lock);
855 if (spa->spa_proc_state != SPA_PROC_NONE) {
856 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
857 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
858 cv_broadcast(&spa->spa_proc_cv);
859 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
860 ASSERT(spa->spa_proc != &p0);
861 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
863 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
864 spa->spa_proc_state = SPA_PROC_NONE;
866 ASSERT(spa->spa_proc == &p0);
867 mutex_exit(&spa->spa_proc_lock);
870 * We want to make sure spa_thread() has actually exited the ZFS
871 * module, so that the module can't be unloaded out from underneath
874 if (spa->spa_did != 0) {
875 thread_join(spa->spa_did);
881 * Verify a pool configuration, and construct the vdev tree appropriately. This
882 * will create all the necessary vdevs in the appropriate layout, with each vdev
883 * in the CLOSED state. This will prep the pool before open/creation/import.
884 * All vdev validation is done by the vdev_alloc() routine.
887 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
888 uint_t id, int atype)
895 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
898 if ((*vdp)->vdev_ops->vdev_op_leaf)
901 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
913 for (c = 0; c < children; c++) {
915 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
923 ASSERT(*vdp != NULL);
929 * Opposite of spa_load().
932 spa_unload(spa_t *spa)
936 ASSERT(MUTEX_HELD(&spa_namespace_lock));
941 spa_async_suspend(spa);
946 if (spa->spa_sync_on) {
947 txg_sync_stop(spa->spa_dsl_pool);
948 spa->spa_sync_on = B_FALSE;
952 * Wait for any outstanding async I/O to complete.
954 if (spa->spa_async_zio_root != NULL) {
955 (void) zio_wait(spa->spa_async_zio_root);
956 spa->spa_async_zio_root = NULL;
959 bpobj_close(&spa->spa_deferred_bpobj);
962 * Close the dsl pool.
964 if (spa->spa_dsl_pool) {
965 dsl_pool_close(spa->spa_dsl_pool);
966 spa->spa_dsl_pool = NULL;
967 spa->spa_meta_objset = NULL;
972 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
975 * Drop and purge level 2 cache
977 spa_l2cache_drop(spa);
982 if (spa->spa_root_vdev)
983 vdev_free(spa->spa_root_vdev);
984 ASSERT(spa->spa_root_vdev == NULL);
986 for (i = 0; i < spa->spa_spares.sav_count; i++)
987 vdev_free(spa->spa_spares.sav_vdevs[i]);
988 if (spa->spa_spares.sav_vdevs) {
989 kmem_free(spa->spa_spares.sav_vdevs,
990 spa->spa_spares.sav_count * sizeof (void *));
991 spa->spa_spares.sav_vdevs = NULL;
993 if (spa->spa_spares.sav_config) {
994 nvlist_free(spa->spa_spares.sav_config);
995 spa->spa_spares.sav_config = NULL;
997 spa->spa_spares.sav_count = 0;
999 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
1000 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1001 if (spa->spa_l2cache.sav_vdevs) {
1002 kmem_free(spa->spa_l2cache.sav_vdevs,
1003 spa->spa_l2cache.sav_count * sizeof (void *));
1004 spa->spa_l2cache.sav_vdevs = NULL;
1006 if (spa->spa_l2cache.sav_config) {
1007 nvlist_free(spa->spa_l2cache.sav_config);
1008 spa->spa_l2cache.sav_config = NULL;
1010 spa->spa_l2cache.sav_count = 0;
1012 spa->spa_async_suspended = 0;
1014 spa_config_exit(spa, SCL_ALL, FTAG);
1018 * Load (or re-load) the current list of vdevs describing the active spares for
1019 * this pool. When this is called, we have some form of basic information in
1020 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1021 * then re-generate a more complete list including status information.
1024 spa_load_spares(spa_t *spa)
1031 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1034 * First, close and free any existing spare vdevs.
1036 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1037 vd = spa->spa_spares.sav_vdevs[i];
1039 /* Undo the call to spa_activate() below */
1040 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1041 B_FALSE)) != NULL && tvd->vdev_isspare)
1042 spa_spare_remove(tvd);
1047 if (spa->spa_spares.sav_vdevs)
1048 kmem_free(spa->spa_spares.sav_vdevs,
1049 spa->spa_spares.sav_count * sizeof (void *));
1051 if (spa->spa_spares.sav_config == NULL)
1054 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1055 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1057 spa->spa_spares.sav_count = (int)nspares;
1058 spa->spa_spares.sav_vdevs = NULL;
1064 * Construct the array of vdevs, opening them to get status in the
1065 * process. For each spare, there is potentially two different vdev_t
1066 * structures associated with it: one in the list of spares (used only
1067 * for basic validation purposes) and one in the active vdev
1068 * configuration (if it's spared in). During this phase we open and
1069 * validate each vdev on the spare list. If the vdev also exists in the
1070 * active configuration, then we also mark this vdev as an active spare.
1072 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1074 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1075 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1076 VDEV_ALLOC_SPARE) == 0);
1079 spa->spa_spares.sav_vdevs[i] = vd;
1081 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1082 B_FALSE)) != NULL) {
1083 if (!tvd->vdev_isspare)
1087 * We only mark the spare active if we were successfully
1088 * able to load the vdev. Otherwise, importing a pool
1089 * with a bad active spare would result in strange
1090 * behavior, because multiple pool would think the spare
1091 * is actively in use.
1093 * There is a vulnerability here to an equally bizarre
1094 * circumstance, where a dead active spare is later
1095 * brought back to life (onlined or otherwise). Given
1096 * the rarity of this scenario, and the extra complexity
1097 * it adds, we ignore the possibility.
1099 if (!vdev_is_dead(tvd))
1100 spa_spare_activate(tvd);
1104 vd->vdev_aux = &spa->spa_spares;
1106 if (vdev_open(vd) != 0)
1109 if (vdev_validate_aux(vd) == 0)
1114 * Recompute the stashed list of spares, with status information
1117 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1118 DATA_TYPE_NVLIST_ARRAY) == 0);
1120 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1122 for (i = 0; i < spa->spa_spares.sav_count; i++)
1123 spares[i] = vdev_config_generate(spa,
1124 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1125 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1126 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1127 for (i = 0; i < spa->spa_spares.sav_count; i++)
1128 nvlist_free(spares[i]);
1129 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1133 * Load (or re-load) the current list of vdevs describing the active l2cache for
1134 * this pool. When this is called, we have some form of basic information in
1135 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1136 * then re-generate a more complete list including status information.
1137 * Devices which are already active have their details maintained, and are
1141 spa_load_l2cache(spa_t *spa)
1145 int i, j, oldnvdevs;
1147 vdev_t *vd, **oldvdevs, **newvdevs = NULL;
1148 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1150 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1152 if (sav->sav_config != NULL) {
1153 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1154 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1155 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1160 oldvdevs = sav->sav_vdevs;
1161 oldnvdevs = sav->sav_count;
1162 sav->sav_vdevs = NULL;
1166 * Process new nvlist of vdevs.
1168 for (i = 0; i < nl2cache; i++) {
1169 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1173 for (j = 0; j < oldnvdevs; j++) {
1175 if (vd != NULL && guid == vd->vdev_guid) {
1177 * Retain previous vdev for add/remove ops.
1185 if (newvdevs[i] == NULL) {
1189 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1190 VDEV_ALLOC_L2CACHE) == 0);
1195 * Commit this vdev as an l2cache device,
1196 * even if it fails to open.
1198 spa_l2cache_add(vd);
1203 spa_l2cache_activate(vd);
1205 if (vdev_open(vd) != 0)
1208 (void) vdev_validate_aux(vd);
1210 if (!vdev_is_dead(vd))
1211 l2arc_add_vdev(spa, vd);
1216 * Purge vdevs that were dropped
1218 for (i = 0; i < oldnvdevs; i++) {
1223 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1224 pool != 0ULL && l2arc_vdev_present(vd))
1225 l2arc_remove_vdev(vd);
1226 (void) vdev_close(vd);
1227 spa_l2cache_remove(vd);
1232 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1234 if (sav->sav_config == NULL)
1237 sav->sav_vdevs = newvdevs;
1238 sav->sav_count = (int)nl2cache;
1241 * Recompute the stashed list of l2cache devices, with status
1242 * information this time.
1244 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1245 DATA_TYPE_NVLIST_ARRAY) == 0);
1247 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1248 for (i = 0; i < sav->sav_count; i++)
1249 l2cache[i] = vdev_config_generate(spa,
1250 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1251 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1252 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1254 for (i = 0; i < sav->sav_count; i++)
1255 nvlist_free(l2cache[i]);
1257 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1261 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1264 char *packed = NULL;
1269 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1270 nvsize = *(uint64_t *)db->db_data;
1271 dmu_buf_rele(db, FTAG);
1273 packed = kmem_alloc(nvsize, KM_SLEEP);
1274 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1277 error = nvlist_unpack(packed, nvsize, value, 0);
1278 kmem_free(packed, nvsize);
1284 * Checks to see if the given vdev could not be opened, in which case we post a
1285 * sysevent to notify the autoreplace code that the device has been removed.
1288 spa_check_removed(vdev_t *vd)
1292 for (c = 0; c < vd->vdev_children; c++)
1293 spa_check_removed(vd->vdev_child[c]);
1295 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1296 zfs_post_autoreplace(vd->vdev_spa, vd);
1297 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1302 * Validate the current config against the MOS config
1305 spa_config_valid(spa_t *spa, nvlist_t *config)
1307 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1311 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1313 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1314 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1316 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1319 * If we're doing a normal import, then build up any additional
1320 * diagnostic information about missing devices in this config.
1321 * We'll pass this up to the user for further processing.
1323 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1324 nvlist_t **child, *nv;
1327 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1329 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1331 for (c = 0; c < rvd->vdev_children; c++) {
1332 vdev_t *tvd = rvd->vdev_child[c];
1333 vdev_t *mtvd = mrvd->vdev_child[c];
1335 if (tvd->vdev_ops == &vdev_missing_ops &&
1336 mtvd->vdev_ops != &vdev_missing_ops &&
1338 child[idx++] = vdev_config_generate(spa, mtvd,
1343 VERIFY(nvlist_add_nvlist_array(nv,
1344 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1345 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1346 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1348 for (i = 0; i < idx; i++)
1349 nvlist_free(child[i]);
1352 kmem_free(child, rvd->vdev_children * sizeof (char **));
1356 * Compare the root vdev tree with the information we have
1357 * from the MOS config (mrvd). Check each top-level vdev
1358 * with the corresponding MOS config top-level (mtvd).
1360 for (c = 0; c < rvd->vdev_children; c++) {
1361 vdev_t *tvd = rvd->vdev_child[c];
1362 vdev_t *mtvd = mrvd->vdev_child[c];
1365 * Resolve any "missing" vdevs in the current configuration.
1366 * If we find that the MOS config has more accurate information
1367 * about the top-level vdev then use that vdev instead.
1369 if (tvd->vdev_ops == &vdev_missing_ops &&
1370 mtvd->vdev_ops != &vdev_missing_ops) {
1372 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1376 * Device specific actions.
1378 if (mtvd->vdev_islog) {
1379 spa_set_log_state(spa, SPA_LOG_CLEAR);
1382 * XXX - once we have 'readonly' pool
1383 * support we should be able to handle
1384 * missing data devices by transitioning
1385 * the pool to readonly.
1391 * Swap the missing vdev with the data we were
1392 * able to obtain from the MOS config.
1394 vdev_remove_child(rvd, tvd);
1395 vdev_remove_child(mrvd, mtvd);
1397 vdev_add_child(rvd, mtvd);
1398 vdev_add_child(mrvd, tvd);
1400 spa_config_exit(spa, SCL_ALL, FTAG);
1402 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1405 } else if (mtvd->vdev_islog) {
1407 * Load the slog device's state from the MOS config
1408 * since it's possible that the label does not
1409 * contain the most up-to-date information.
1411 vdev_load_log_state(tvd, mtvd);
1416 spa_config_exit(spa, SCL_ALL, FTAG);
1419 * Ensure we were able to validate the config.
1421 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1425 * Check for missing log devices
1428 spa_check_logs(spa_t *spa)
1430 switch (spa->spa_log_state) {
1433 case SPA_LOG_MISSING:
1434 /* need to recheck in case slog has been restored */
1435 case SPA_LOG_UNKNOWN:
1436 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1437 DS_FIND_CHILDREN)) {
1438 spa_set_log_state(spa, SPA_LOG_MISSING);
1447 spa_passivate_log(spa_t *spa)
1449 vdev_t *rvd = spa->spa_root_vdev;
1450 boolean_t slog_found = B_FALSE;
1453 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1455 if (!spa_has_slogs(spa))
1458 for (c = 0; c < rvd->vdev_children; c++) {
1459 vdev_t *tvd = rvd->vdev_child[c];
1460 metaslab_group_t *mg = tvd->vdev_mg;
1462 if (tvd->vdev_islog) {
1463 metaslab_group_passivate(mg);
1464 slog_found = B_TRUE;
1468 return (slog_found);
1472 spa_activate_log(spa_t *spa)
1474 vdev_t *rvd = spa->spa_root_vdev;
1477 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1479 for (c = 0; c < rvd->vdev_children; c++) {
1480 vdev_t *tvd = rvd->vdev_child[c];
1481 metaslab_group_t *mg = tvd->vdev_mg;
1483 if (tvd->vdev_islog)
1484 metaslab_group_activate(mg);
1489 spa_offline_log(spa_t *spa)
1493 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1494 NULL, DS_FIND_CHILDREN)) == 0) {
1497 * We successfully offlined the log device, sync out the
1498 * current txg so that the "stubby" block can be removed
1501 txg_wait_synced(spa->spa_dsl_pool, 0);
1507 spa_aux_check_removed(spa_aux_vdev_t *sav)
1511 for (i = 0; i < sav->sav_count; i++)
1512 spa_check_removed(sav->sav_vdevs[i]);
1516 spa_claim_notify(zio_t *zio)
1518 spa_t *spa = zio->io_spa;
1523 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1524 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1525 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1526 mutex_exit(&spa->spa_props_lock);
1529 typedef struct spa_load_error {
1530 uint64_t sle_meta_count;
1531 uint64_t sle_data_count;
1535 spa_load_verify_done(zio_t *zio)
1537 blkptr_t *bp = zio->io_bp;
1538 spa_load_error_t *sle = zio->io_private;
1539 dmu_object_type_t type = BP_GET_TYPE(bp);
1540 int error = zio->io_error;
1543 if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
1544 type != DMU_OT_INTENT_LOG)
1545 atomic_add_64(&sle->sle_meta_count, 1);
1547 atomic_add_64(&sle->sle_data_count, 1);
1549 zio_data_buf_free(zio->io_data, zio->io_size);
1554 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1555 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1559 size_t size = BP_GET_PSIZE(bp);
1560 void *data = zio_data_buf_alloc(size);
1562 zio_nowait(zio_read(rio, spa, bp, data, size,
1563 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1564 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1565 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1571 spa_load_verify(spa_t *spa)
1574 spa_load_error_t sle = { 0 };
1575 zpool_rewind_policy_t policy;
1576 boolean_t verify_ok = B_FALSE;
1579 zpool_get_rewind_policy(spa->spa_config, &policy);
1581 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1584 rio = zio_root(spa, NULL, &sle,
1585 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1587 error = traverse_pool(spa, spa->spa_verify_min_txg,
1588 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1590 (void) zio_wait(rio);
1592 spa->spa_load_meta_errors = sle.sle_meta_count;
1593 spa->spa_load_data_errors = sle.sle_data_count;
1595 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1596 sle.sle_data_count <= policy.zrp_maxdata) {
1600 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1601 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1603 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1604 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1605 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1606 VERIFY(nvlist_add_int64(spa->spa_load_info,
1607 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1608 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1609 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1611 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1615 if (error != ENXIO && error != EIO)
1620 return (verify_ok ? 0 : EIO);
1624 * Find a value in the pool props object.
1627 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1629 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1630 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1634 * Find a value in the pool directory object.
1637 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1639 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1640 name, sizeof (uint64_t), 1, val));
1644 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1646 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1651 * Fix up config after a partly-completed split. This is done with the
1652 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1653 * pool have that entry in their config, but only the splitting one contains
1654 * a list of all the guids of the vdevs that are being split off.
1656 * This function determines what to do with that list: either rejoin
1657 * all the disks to the pool, or complete the splitting process. To attempt
1658 * the rejoin, each disk that is offlined is marked online again, and
1659 * we do a reopen() call. If the vdev label for every disk that was
1660 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1661 * then we call vdev_split() on each disk, and complete the split.
1663 * Otherwise we leave the config alone, with all the vdevs in place in
1664 * the original pool.
1667 spa_try_repair(spa_t *spa, nvlist_t *config)
1674 boolean_t attempt_reopen;
1676 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1679 /* check that the config is complete */
1680 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1681 &glist, &gcount) != 0)
1684 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1686 /* attempt to online all the vdevs & validate */
1687 attempt_reopen = B_TRUE;
1688 for (i = 0; i < gcount; i++) {
1689 if (glist[i] == 0) /* vdev is hole */
1692 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1693 if (vd[i] == NULL) {
1695 * Don't bother attempting to reopen the disks;
1696 * just do the split.
1698 attempt_reopen = B_FALSE;
1700 /* attempt to re-online it */
1701 vd[i]->vdev_offline = B_FALSE;
1705 if (attempt_reopen) {
1706 vdev_reopen(spa->spa_root_vdev);
1708 /* check each device to see what state it's in */
1709 for (extracted = 0, i = 0; i < gcount; i++) {
1710 if (vd[i] != NULL &&
1711 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1718 * If every disk has been moved to the new pool, or if we never
1719 * even attempted to look at them, then we split them off for
1722 if (!attempt_reopen || gcount == extracted) {
1723 for (i = 0; i < gcount; i++)
1726 vdev_reopen(spa->spa_root_vdev);
1729 kmem_free(vd, gcount * sizeof (vdev_t *));
1733 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1734 boolean_t mosconfig)
1736 nvlist_t *config = spa->spa_config;
1737 char *ereport = FM_EREPORT_ZFS_POOL;
1742 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1746 * Versioning wasn't explicitly added to the label until later, so if
1747 * it's not present treat it as the initial version.
1749 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1750 &spa->spa_ubsync.ub_version) != 0)
1751 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1753 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1754 &spa->spa_config_txg);
1756 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1757 spa_guid_exists(pool_guid, 0)) {
1760 spa->spa_load_guid = pool_guid;
1762 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1764 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1768 gethrestime(&spa->spa_loaded_ts);
1769 error = spa_load_impl(spa, pool_guid, config, state, type,
1770 mosconfig, &ereport);
1773 spa->spa_minref = refcount_count(&spa->spa_refcount);
1775 if (error != EEXIST) {
1776 spa->spa_loaded_ts.tv_sec = 0;
1777 spa->spa_loaded_ts.tv_nsec = 0;
1779 if (error != EBADF) {
1780 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1783 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
1790 * Load an existing storage pool, using the pool's builtin spa_config as a
1791 * source of configuration information.
1793 __attribute__((always_inline))
1795 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
1796 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1800 nvlist_t *nvroot = NULL;
1802 uberblock_t *ub = &spa->spa_uberblock;
1803 uint64_t children, config_cache_txg = spa->spa_config_txg;
1804 int orig_mode = spa->spa_mode;
1809 * If this is an untrusted config, access the pool in read-only mode.
1810 * This prevents things like resilvering recently removed devices.
1813 spa->spa_mode = FREAD;
1815 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1817 spa->spa_load_state = state;
1819 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
1822 parse = (type == SPA_IMPORT_EXISTING ?
1823 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
1826 * Create "The Godfather" zio to hold all async IOs
1828 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1829 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
1832 * Parse the configuration into a vdev tree. We explicitly set the
1833 * value that will be returned by spa_version() since parsing the
1834 * configuration requires knowing the version number.
1836 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1837 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
1838 spa_config_exit(spa, SCL_ALL, FTAG);
1843 ASSERT(spa->spa_root_vdev == rvd);
1845 if (type != SPA_IMPORT_ASSEMBLE) {
1846 ASSERT(spa_guid(spa) == pool_guid);
1850 * Try to open all vdevs, loading each label in the process.
1852 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1853 error = vdev_open(rvd);
1854 spa_config_exit(spa, SCL_ALL, FTAG);
1859 * We need to validate the vdev labels against the configuration that
1860 * we have in hand, which is dependent on the setting of mosconfig. If
1861 * mosconfig is true then we're validating the vdev labels based on
1862 * that config. Otherwise, we're validating against the cached config
1863 * (zpool.cache) that was read when we loaded the zfs module, and then
1864 * later we will recursively call spa_load() and validate against
1867 * If we're assembling a new pool that's been split off from an
1868 * existing pool, the labels haven't yet been updated so we skip
1869 * validation for now.
1871 if (type != SPA_IMPORT_ASSEMBLE) {
1872 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1873 error = vdev_validate(rvd);
1874 spa_config_exit(spa, SCL_ALL, FTAG);
1879 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
1884 * Find the best uberblock.
1886 vdev_uberblock_load(NULL, rvd, ub);
1889 * If we weren't able to find a single valid uberblock, return failure.
1891 if (ub->ub_txg == 0)
1892 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
1895 * If the pool is newer than the code, we can't open it.
1897 if (ub->ub_version > SPA_VERSION)
1898 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1901 * If the vdev guid sum doesn't match the uberblock, we have an
1902 * incomplete configuration. We first check to see if the pool
1903 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
1904 * If it is, defer the vdev_guid_sum check till later so we
1905 * can handle missing vdevs.
1907 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
1908 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
1909 rvd->vdev_guid_sum != ub->ub_guid_sum)
1910 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
1912 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
1913 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1914 spa_try_repair(spa, config);
1915 spa_config_exit(spa, SCL_ALL, FTAG);
1916 nvlist_free(spa->spa_config_splitting);
1917 spa->spa_config_splitting = NULL;
1921 * Initialize internal SPA structures.
1923 spa->spa_state = POOL_STATE_ACTIVE;
1924 spa->spa_ubsync = spa->spa_uberblock;
1925 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
1926 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
1927 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
1928 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
1929 spa->spa_claim_max_txg = spa->spa_first_txg;
1930 spa->spa_prev_software_version = ub->ub_software_version;
1932 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
1934 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1935 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1937 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
1938 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1942 nvlist_t *policy = NULL, *nvconfig;
1944 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
1945 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1947 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
1948 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
1950 unsigned long myhostid = 0;
1952 VERIFY(nvlist_lookup_string(nvconfig,
1953 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1956 myhostid = zone_get_hostid(NULL);
1959 * We're emulating the system's hostid in userland, so
1960 * we can't use zone_get_hostid().
1962 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
1963 #endif /* _KERNEL */
1964 if (hostid != 0 && myhostid != 0 &&
1965 hostid != myhostid) {
1966 nvlist_free(nvconfig);
1967 cmn_err(CE_WARN, "pool '%s' could not be "
1968 "loaded as it was last accessed by "
1969 "another system (host: %s hostid: 0x%lx). "
1970 "See: http://www.sun.com/msg/ZFS-8000-EY",
1971 spa_name(spa), hostname,
1972 (unsigned long)hostid);
1976 if (nvlist_lookup_nvlist(spa->spa_config,
1977 ZPOOL_REWIND_POLICY, &policy) == 0)
1978 VERIFY(nvlist_add_nvlist(nvconfig,
1979 ZPOOL_REWIND_POLICY, policy) == 0);
1981 spa_config_set(spa, nvconfig);
1983 spa_deactivate(spa);
1984 spa_activate(spa, orig_mode);
1986 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
1989 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
1990 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1991 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
1993 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
1996 * Load the bit that tells us to use the new accounting function
1997 * (raid-z deflation). If we have an older pool, this will not
2000 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2001 if (error != 0 && error != ENOENT)
2002 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2004 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2005 &spa->spa_creation_version);
2006 if (error != 0 && error != ENOENT)
2007 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2010 * Load the persistent error log. If we have an older pool, this will
2013 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2014 if (error != 0 && error != ENOENT)
2015 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2017 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2018 &spa->spa_errlog_scrub);
2019 if (error != 0 && error != ENOENT)
2020 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2023 * Load the history object. If we have an older pool, this
2024 * will not be present.
2026 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2027 if (error != 0 && error != ENOENT)
2028 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2031 * If we're assembling the pool from the split-off vdevs of
2032 * an existing pool, we don't want to attach the spares & cache
2037 * Load any hot spares for this pool.
2039 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2040 if (error != 0 && error != ENOENT)
2041 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2042 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2043 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2044 if (load_nvlist(spa, spa->spa_spares.sav_object,
2045 &spa->spa_spares.sav_config) != 0)
2046 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2048 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2049 spa_load_spares(spa);
2050 spa_config_exit(spa, SCL_ALL, FTAG);
2051 } else if (error == 0) {
2052 spa->spa_spares.sav_sync = B_TRUE;
2056 * Load any level 2 ARC devices for this pool.
2058 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2059 &spa->spa_l2cache.sav_object);
2060 if (error != 0 && error != ENOENT)
2061 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2062 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2063 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2064 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2065 &spa->spa_l2cache.sav_config) != 0)
2066 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2068 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2069 spa_load_l2cache(spa);
2070 spa_config_exit(spa, SCL_ALL, FTAG);
2071 } else if (error == 0) {
2072 spa->spa_l2cache.sav_sync = B_TRUE;
2075 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2077 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2078 if (error && error != ENOENT)
2079 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2082 uint64_t autoreplace;
2084 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2085 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2086 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2087 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2088 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2089 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2090 &spa->spa_dedup_ditto);
2092 spa->spa_autoreplace = (autoreplace != 0);
2096 * If the 'autoreplace' property is set, then post a resource notifying
2097 * the ZFS DE that it should not issue any faults for unopenable
2098 * devices. We also iterate over the vdevs, and post a sysevent for any
2099 * unopenable vdevs so that the normal autoreplace handler can take
2102 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2103 spa_check_removed(spa->spa_root_vdev);
2105 * For the import case, this is done in spa_import(), because
2106 * at this point we're using the spare definitions from
2107 * the MOS config, not necessarily from the userland config.
2109 if (state != SPA_LOAD_IMPORT) {
2110 spa_aux_check_removed(&spa->spa_spares);
2111 spa_aux_check_removed(&spa->spa_l2cache);
2116 * Load the vdev state for all toplevel vdevs.
2121 * Propagate the leaf DTLs we just loaded all the way up the tree.
2123 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2124 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2125 spa_config_exit(spa, SCL_ALL, FTAG);
2128 * Load the DDTs (dedup tables).
2130 error = ddt_load(spa);
2132 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2134 spa_update_dspace(spa);
2137 * Validate the config, using the MOS config to fill in any
2138 * information which might be missing. If we fail to validate
2139 * the config then declare the pool unfit for use. If we're
2140 * assembling a pool from a split, the log is not transferred
2143 if (type != SPA_IMPORT_ASSEMBLE) {
2146 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2147 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2149 if (!spa_config_valid(spa, nvconfig)) {
2150 nvlist_free(nvconfig);
2151 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2154 nvlist_free(nvconfig);
2157 * Now that we've validate the config, check the state of the
2158 * root vdev. If it can't be opened, it indicates one or
2159 * more toplevel vdevs are faulted.
2161 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2164 if (spa_check_logs(spa)) {
2165 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2166 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2171 * We've successfully opened the pool, verify that we're ready
2172 * to start pushing transactions.
2174 if (state != SPA_LOAD_TRYIMPORT) {
2175 if ((error = spa_load_verify(spa)))
2176 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2180 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2181 spa->spa_load_max_txg == UINT64_MAX)) {
2183 int need_update = B_FALSE;
2186 ASSERT(state != SPA_LOAD_TRYIMPORT);
2189 * Claim log blocks that haven't been committed yet.
2190 * This must all happen in a single txg.
2191 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2192 * invoked from zil_claim_log_block()'s i/o done callback.
2193 * Price of rollback is that we abandon the log.
2195 spa->spa_claiming = B_TRUE;
2197 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2198 spa_first_txg(spa));
2199 (void) dmu_objset_find(spa_name(spa),
2200 zil_claim, tx, DS_FIND_CHILDREN);
2203 spa->spa_claiming = B_FALSE;
2205 spa_set_log_state(spa, SPA_LOG_GOOD);
2206 spa->spa_sync_on = B_TRUE;
2207 txg_sync_start(spa->spa_dsl_pool);
2210 * Wait for all claims to sync. We sync up to the highest
2211 * claimed log block birth time so that claimed log blocks
2212 * don't appear to be from the future. spa_claim_max_txg
2213 * will have been set for us by either zil_check_log_chain()
2214 * (invoked from spa_check_logs()) or zil_claim() above.
2216 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2219 * If the config cache is stale, or we have uninitialized
2220 * metaslabs (see spa_vdev_add()), then update the config.
2222 * If this is a verbatim import, trust the current
2223 * in-core spa_config and update the disk labels.
2225 if (config_cache_txg != spa->spa_config_txg ||
2226 state == SPA_LOAD_IMPORT ||
2227 state == SPA_LOAD_RECOVER ||
2228 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2229 need_update = B_TRUE;
2231 for (c = 0; c < rvd->vdev_children; c++)
2232 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2233 need_update = B_TRUE;
2236 * Update the config cache asychronously in case we're the
2237 * root pool, in which case the config cache isn't writable yet.
2240 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2243 * Check all DTLs to see if anything needs resilvering.
2245 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2246 vdev_resilver_needed(rvd, NULL, NULL))
2247 spa_async_request(spa, SPA_ASYNC_RESILVER);
2250 * Delete any inconsistent datasets.
2252 (void) dmu_objset_find(spa_name(spa),
2253 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2256 * Clean up any stale temporary dataset userrefs.
2258 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2265 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2267 int mode = spa->spa_mode;
2270 spa_deactivate(spa);
2272 spa->spa_load_max_txg--;
2274 spa_activate(spa, mode);
2275 spa_async_suspend(spa);
2277 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2281 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2282 uint64_t max_request, int rewind_flags)
2284 nvlist_t *config = NULL;
2285 int load_error, rewind_error;
2286 uint64_t safe_rewind_txg;
2289 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2290 spa->spa_load_max_txg = spa->spa_load_txg;
2291 spa_set_log_state(spa, SPA_LOG_CLEAR);
2293 spa->spa_load_max_txg = max_request;
2296 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2298 if (load_error == 0)
2301 if (spa->spa_root_vdev != NULL)
2302 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2304 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2305 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2307 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2308 nvlist_free(config);
2309 return (load_error);
2312 /* Price of rolling back is discarding txgs, including log */
2313 if (state == SPA_LOAD_RECOVER)
2314 spa_set_log_state(spa, SPA_LOG_CLEAR);
2316 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2317 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2318 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2319 TXG_INITIAL : safe_rewind_txg;
2322 * Continue as long as we're finding errors, we're still within
2323 * the acceptable rewind range, and we're still finding uberblocks
2325 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2326 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2327 if (spa->spa_load_max_txg < safe_rewind_txg)
2328 spa->spa_extreme_rewind = B_TRUE;
2329 rewind_error = spa_load_retry(spa, state, mosconfig);
2332 spa->spa_extreme_rewind = B_FALSE;
2333 spa->spa_load_max_txg = UINT64_MAX;
2335 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2336 spa_config_set(spa, config);
2338 return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
2344 * The import case is identical to an open except that the configuration is sent
2345 * down from userland, instead of grabbed from the configuration cache. For the
2346 * case of an open, the pool configuration will exist in the
2347 * POOL_STATE_UNINITIALIZED state.
2349 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2350 * the same time open the pool, without having to keep around the spa_t in some
2354 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2358 spa_load_state_t state = SPA_LOAD_OPEN;
2360 int locked = B_FALSE;
2365 * As disgusting as this is, we need to support recursive calls to this
2366 * function because dsl_dir_open() is called during spa_load(), and ends
2367 * up calling spa_open() again. The real fix is to figure out how to
2368 * avoid dsl_dir_open() calling this in the first place.
2370 if (mutex_owner(&spa_namespace_lock) != curthread) {
2371 mutex_enter(&spa_namespace_lock);
2375 if ((spa = spa_lookup(pool)) == NULL) {
2377 mutex_exit(&spa_namespace_lock);
2381 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2382 zpool_rewind_policy_t policy;
2384 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2386 if (policy.zrp_request & ZPOOL_DO_REWIND)
2387 state = SPA_LOAD_RECOVER;
2389 spa_activate(spa, spa_mode_global);
2391 if (state != SPA_LOAD_RECOVER)
2392 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2394 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2395 policy.zrp_request);
2397 if (error == EBADF) {
2399 * If vdev_validate() returns failure (indicated by
2400 * EBADF), it indicates that one of the vdevs indicates
2401 * that the pool has been exported or destroyed. If
2402 * this is the case, the config cache is out of sync and
2403 * we should remove the pool from the namespace.
2406 spa_deactivate(spa);
2407 spa_config_sync(spa, B_TRUE, B_TRUE);
2410 mutex_exit(&spa_namespace_lock);
2416 * We can't open the pool, but we still have useful
2417 * information: the state of each vdev after the
2418 * attempted vdev_open(). Return this to the user.
2420 if (config != NULL && spa->spa_config) {
2421 VERIFY(nvlist_dup(spa->spa_config, config,
2423 VERIFY(nvlist_add_nvlist(*config,
2424 ZPOOL_CONFIG_LOAD_INFO,
2425 spa->spa_load_info) == 0);
2428 spa_deactivate(spa);
2429 spa->spa_last_open_failed = error;
2431 mutex_exit(&spa_namespace_lock);
2437 spa_open_ref(spa, tag);
2440 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2443 * If we've recovered the pool, pass back any information we
2444 * gathered while doing the load.
2446 if (state == SPA_LOAD_RECOVER) {
2447 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2448 spa->spa_load_info) == 0);
2452 spa->spa_last_open_failed = 0;
2453 spa->spa_last_ubsync_txg = 0;
2454 spa->spa_load_txg = 0;
2455 mutex_exit(&spa_namespace_lock);
2464 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2467 return (spa_open_common(name, spapp, tag, policy, config));
2471 spa_open(const char *name, spa_t **spapp, void *tag)
2473 return (spa_open_common(name, spapp, tag, NULL, NULL));
2477 * Lookup the given spa_t, incrementing the inject count in the process,
2478 * preventing it from being exported or destroyed.
2481 spa_inject_addref(char *name)
2485 mutex_enter(&spa_namespace_lock);
2486 if ((spa = spa_lookup(name)) == NULL) {
2487 mutex_exit(&spa_namespace_lock);
2490 spa->spa_inject_ref++;
2491 mutex_exit(&spa_namespace_lock);
2497 spa_inject_delref(spa_t *spa)
2499 mutex_enter(&spa_namespace_lock);
2500 spa->spa_inject_ref--;
2501 mutex_exit(&spa_namespace_lock);
2505 * Add spares device information to the nvlist.
2508 spa_add_spares(spa_t *spa, nvlist_t *config)
2518 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2520 if (spa->spa_spares.sav_count == 0)
2523 VERIFY(nvlist_lookup_nvlist(config,
2524 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2525 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2526 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2528 VERIFY(nvlist_add_nvlist_array(nvroot,
2529 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2530 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2531 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2534 * Go through and find any spares which have since been
2535 * repurposed as an active spare. If this is the case, update
2536 * their status appropriately.
2538 for (i = 0; i < nspares; i++) {
2539 VERIFY(nvlist_lookup_uint64(spares[i],
2540 ZPOOL_CONFIG_GUID, &guid) == 0);
2541 if (spa_spare_exists(guid, &pool, NULL) &&
2543 VERIFY(nvlist_lookup_uint64_array(
2544 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2545 (uint64_t **)&vs, &vsc) == 0);
2546 vs->vs_state = VDEV_STATE_CANT_OPEN;
2547 vs->vs_aux = VDEV_AUX_SPARED;
2554 * Add l2cache device information to the nvlist, including vdev stats.
2557 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2560 uint_t i, j, nl2cache;
2567 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2569 if (spa->spa_l2cache.sav_count == 0)
2572 VERIFY(nvlist_lookup_nvlist(config,
2573 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2574 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2575 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2576 if (nl2cache != 0) {
2577 VERIFY(nvlist_add_nvlist_array(nvroot,
2578 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2579 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2580 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2583 * Update level 2 cache device stats.
2586 for (i = 0; i < nl2cache; i++) {
2587 VERIFY(nvlist_lookup_uint64(l2cache[i],
2588 ZPOOL_CONFIG_GUID, &guid) == 0);
2591 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2593 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2594 vd = spa->spa_l2cache.sav_vdevs[j];
2600 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2601 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2603 vdev_get_stats(vd, vs);
2609 spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2615 error = spa_open_common(name, &spa, FTAG, NULL, config);
2619 * This still leaves a window of inconsistency where the spares
2620 * or l2cache devices could change and the config would be
2621 * self-inconsistent.
2623 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2625 if (*config != NULL) {
2626 uint64_t loadtimes[2];
2628 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
2629 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
2630 VERIFY(nvlist_add_uint64_array(*config,
2631 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
2633 VERIFY(nvlist_add_uint64(*config,
2634 ZPOOL_CONFIG_ERRCOUNT,
2635 spa_get_errlog_size(spa)) == 0);
2637 if (spa_suspended(spa))
2638 VERIFY(nvlist_add_uint64(*config,
2639 ZPOOL_CONFIG_SUSPENDED,
2640 spa->spa_failmode) == 0);
2642 spa_add_spares(spa, *config);
2643 spa_add_l2cache(spa, *config);
2648 * We want to get the alternate root even for faulted pools, so we cheat
2649 * and call spa_lookup() directly.
2653 mutex_enter(&spa_namespace_lock);
2654 spa = spa_lookup(name);
2656 spa_altroot(spa, altroot, buflen);
2660 mutex_exit(&spa_namespace_lock);
2662 spa_altroot(spa, altroot, buflen);
2667 spa_config_exit(spa, SCL_CONFIG, FTAG);
2668 spa_close(spa, FTAG);
2675 * Validate that the auxiliary device array is well formed. We must have an
2676 * array of nvlists, each which describes a valid leaf vdev. If this is an
2677 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2678 * specified, as long as they are well-formed.
2681 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
2682 spa_aux_vdev_t *sav, const char *config, uint64_t version,
2683 vdev_labeltype_t label)
2690 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2693 * It's acceptable to have no devs specified.
2695 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
2702 * Make sure the pool is formatted with a version that supports this
2705 if (spa_version(spa) < version)
2709 * Set the pending device list so we correctly handle device in-use
2712 sav->sav_pending = dev;
2713 sav->sav_npending = ndev;
2715 for (i = 0; i < ndev; i++) {
2716 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
2720 if (!vd->vdev_ops->vdev_op_leaf) {
2727 * The L2ARC currently only supports disk devices in
2728 * kernel context. For user-level testing, we allow it.
2731 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
2732 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
2739 if ((error = vdev_open(vd)) == 0 &&
2740 (error = vdev_label_init(vd, crtxg, label)) == 0) {
2741 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
2742 vd->vdev_guid) == 0);
2748 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
2755 sav->sav_pending = NULL;
2756 sav->sav_npending = 0;
2761 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
2765 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2767 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2768 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
2769 VDEV_LABEL_SPARE)) != 0) {
2773 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2774 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
2775 VDEV_LABEL_L2CACHE));
2779 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
2784 if (sav->sav_config != NULL) {
2790 * Generate new dev list by concatentating with the
2793 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
2794 &olddevs, &oldndevs) == 0);
2796 newdevs = kmem_alloc(sizeof (void *) *
2797 (ndevs + oldndevs), KM_SLEEP);
2798 for (i = 0; i < oldndevs; i++)
2799 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
2801 for (i = 0; i < ndevs; i++)
2802 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
2805 VERIFY(nvlist_remove(sav->sav_config, config,
2806 DATA_TYPE_NVLIST_ARRAY) == 0);
2808 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2809 config, newdevs, ndevs + oldndevs) == 0);
2810 for (i = 0; i < oldndevs + ndevs; i++)
2811 nvlist_free(newdevs[i]);
2812 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
2815 * Generate a new dev list.
2817 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
2819 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
2825 * Stop and drop level 2 ARC devices
2828 spa_l2cache_drop(spa_t *spa)
2832 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2834 for (i = 0; i < sav->sav_count; i++) {
2837 vd = sav->sav_vdevs[i];
2840 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2841 pool != 0ULL && l2arc_vdev_present(vd))
2842 l2arc_remove_vdev(vd);
2843 if (vd->vdev_isl2cache)
2844 spa_l2cache_remove(vd);
2845 vdev_clear_stats(vd);
2846 (void) vdev_close(vd);
2854 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
2855 const char *history_str, nvlist_t *zplprops)
2858 char *altroot = NULL;
2863 uint64_t txg = TXG_INITIAL;
2864 nvlist_t **spares, **l2cache;
2865 uint_t nspares, nl2cache;
2866 uint64_t version, obj;
2870 * If this pool already exists, return failure.
2872 mutex_enter(&spa_namespace_lock);
2873 if (spa_lookup(pool) != NULL) {
2874 mutex_exit(&spa_namespace_lock);
2879 * Allocate a new spa_t structure.
2881 (void) nvlist_lookup_string(props,
2882 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2883 spa = spa_add(pool, NULL, altroot);
2884 spa_activate(spa, spa_mode_global);
2886 if (props && (error = spa_prop_validate(spa, props))) {
2887 spa_deactivate(spa);
2889 mutex_exit(&spa_namespace_lock);
2893 if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
2895 version = SPA_VERSION;
2896 ASSERT(version <= SPA_VERSION);
2898 spa->spa_first_txg = txg;
2899 spa->spa_uberblock.ub_txg = txg - 1;
2900 spa->spa_uberblock.ub_version = version;
2901 spa->spa_ubsync = spa->spa_uberblock;
2904 * Create "The Godfather" zio to hold all async IOs
2906 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2907 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2910 * Create the root vdev.
2912 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2914 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
2916 ASSERT(error != 0 || rvd != NULL);
2917 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
2919 if (error == 0 && !zfs_allocatable_devs(nvroot))
2923 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
2924 (error = spa_validate_aux(spa, nvroot, txg,
2925 VDEV_ALLOC_ADD)) == 0) {
2926 for (c = 0; c < rvd->vdev_children; c++) {
2927 vdev_metaslab_set_size(rvd->vdev_child[c]);
2928 vdev_expand(rvd->vdev_child[c], txg);
2932 spa_config_exit(spa, SCL_ALL, FTAG);
2936 spa_deactivate(spa);
2938 mutex_exit(&spa_namespace_lock);
2943 * Get the list of spares, if specified.
2945 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2946 &spares, &nspares) == 0) {
2947 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
2949 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
2950 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2951 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2952 spa_load_spares(spa);
2953 spa_config_exit(spa, SCL_ALL, FTAG);
2954 spa->spa_spares.sav_sync = B_TRUE;
2958 * Get the list of level 2 cache devices, if specified.
2960 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2961 &l2cache, &nl2cache) == 0) {
2962 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2963 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2964 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2965 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2966 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2967 spa_load_l2cache(spa);
2968 spa_config_exit(spa, SCL_ALL, FTAG);
2969 spa->spa_l2cache.sav_sync = B_TRUE;
2972 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2973 spa->spa_meta_objset = dp->dp_meta_objset;
2976 * Create DDTs (dedup tables).
2980 spa_update_dspace(spa);
2982 tx = dmu_tx_create_assigned(dp, txg);
2985 * Create the pool config object.
2987 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
2988 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2989 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2991 if (zap_add(spa->spa_meta_objset,
2992 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
2993 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
2994 cmn_err(CE_PANIC, "failed to add pool config");
2997 if (zap_add(spa->spa_meta_objset,
2998 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
2999 sizeof (uint64_t), 1, &version, tx) != 0) {
3000 cmn_err(CE_PANIC, "failed to add pool version");
3003 /* Newly created pools with the right version are always deflated. */
3004 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3005 spa->spa_deflate = TRUE;
3006 if (zap_add(spa->spa_meta_objset,
3007 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3008 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3009 cmn_err(CE_PANIC, "failed to add deflate");
3014 * Create the deferred-free bpobj. Turn off compression
3015 * because sync-to-convergence takes longer if the blocksize
3018 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3019 dmu_object_set_compress(spa->spa_meta_objset, obj,
3020 ZIO_COMPRESS_OFF, tx);
3021 if (zap_add(spa->spa_meta_objset,
3022 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3023 sizeof (uint64_t), 1, &obj, tx) != 0) {
3024 cmn_err(CE_PANIC, "failed to add bpobj");
3026 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3027 spa->spa_meta_objset, obj));
3030 * Create the pool's history object.
3032 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3033 spa_history_create_obj(spa, tx);
3036 * Set pool properties.
3038 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3039 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3040 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3041 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3043 if (props != NULL) {
3044 spa_configfile_set(spa, props, B_FALSE);
3045 spa_sync_props(spa, props, tx);
3050 spa->spa_sync_on = B_TRUE;
3051 txg_sync_start(spa->spa_dsl_pool);
3054 * We explicitly wait for the first transaction to complete so that our
3055 * bean counters are appropriately updated.
3057 txg_wait_synced(spa->spa_dsl_pool, txg);
3059 spa_config_sync(spa, B_FALSE, B_TRUE);
3061 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3062 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3063 spa_history_log_version(spa, LOG_POOL_CREATE);
3065 spa->spa_minref = refcount_count(&spa->spa_refcount);
3067 mutex_exit(&spa_namespace_lock);
3074 * Get the root pool information from the root disk, then import the root pool
3075 * during the system boot up time.
3077 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3080 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3083 nvlist_t *nvtop, *nvroot;
3086 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3090 * Add this top-level vdev to the child array.
3092 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3094 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3096 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3099 * Put this pool's top-level vdevs into a root vdev.
3101 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3102 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3103 VDEV_TYPE_ROOT) == 0);
3104 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3105 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3106 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3110 * Replace the existing vdev_tree with the new root vdev in
3111 * this pool's configuration (remove the old, add the new).
3113 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3114 nvlist_free(nvroot);
3119 * Walk the vdev tree and see if we can find a device with "better"
3120 * configuration. A configuration is "better" if the label on that
3121 * device has a more recent txg.
3124 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3128 for (c = 0; c < vd->vdev_children; c++)
3129 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3131 if (vd->vdev_ops->vdev_op_leaf) {
3135 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3139 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3143 * Do we have a better boot device?
3145 if (label_txg > *txg) {
3154 * Import a root pool.
3156 * For x86. devpath_list will consist of devid and/or physpath name of
3157 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3158 * The GRUB "findroot" command will return the vdev we should boot.
3160 * For Sparc, devpath_list consists the physpath name of the booting device
3161 * no matter the rootpool is a single device pool or a mirrored pool.
3163 * "/pci@1f,0/ide@d/disk@0,0:a"
3166 spa_import_rootpool(char *devpath, char *devid)
3169 vdev_t *rvd, *bvd, *avd = NULL;
3170 nvlist_t *config, *nvtop;
3176 * Read the label from the boot device and generate a configuration.
3178 config = spa_generate_rootconf(devpath, devid, &guid);
3179 #if defined(_OBP) && defined(_KERNEL)
3180 if (config == NULL) {
3181 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3183 get_iscsi_bootpath_phy(devpath);
3184 config = spa_generate_rootconf(devpath, devid, &guid);
3188 if (config == NULL) {
3189 cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
3194 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3196 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3198 mutex_enter(&spa_namespace_lock);
3199 if ((spa = spa_lookup(pname)) != NULL) {
3201 * Remove the existing root pool from the namespace so that we
3202 * can replace it with the correct config we just read in.
3207 spa = spa_add(pname, config, NULL);
3208 spa->spa_is_root = B_TRUE;
3209 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3212 * Build up a vdev tree based on the boot device's label config.
3214 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3216 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3217 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3218 VDEV_ALLOC_ROOTPOOL);
3219 spa_config_exit(spa, SCL_ALL, FTAG);
3221 mutex_exit(&spa_namespace_lock);
3222 nvlist_free(config);
3223 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3229 * Get the boot vdev.
3231 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3232 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3233 (u_longlong_t)guid);
3239 * Determine if there is a better boot device.
3242 spa_alt_rootvdev(rvd, &avd, &txg);
3244 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3245 "try booting from '%s'", avd->vdev_path);
3251 * If the boot device is part of a spare vdev then ensure that
3252 * we're booting off the active spare.
3254 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3255 !bvd->vdev_isspare) {
3256 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3257 "try booting from '%s'",
3259 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3265 spa_history_log_version(spa, LOG_POOL_IMPORT);
3267 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3269 spa_config_exit(spa, SCL_ALL, FTAG);
3270 mutex_exit(&spa_namespace_lock);
3272 nvlist_free(config);
3279 * Import a non-root pool into the system.
3282 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3285 char *altroot = NULL;
3286 spa_load_state_t state = SPA_LOAD_IMPORT;
3287 zpool_rewind_policy_t policy;
3288 uint64_t mode = spa_mode_global;
3289 uint64_t readonly = B_FALSE;
3292 nvlist_t **spares, **l2cache;
3293 uint_t nspares, nl2cache;
3296 * If a pool with this name exists, return failure.
3298 mutex_enter(&spa_namespace_lock);
3299 if (spa_lookup(pool) != NULL) {
3300 mutex_exit(&spa_namespace_lock);
3305 * Create and initialize the spa structure.
3307 (void) nvlist_lookup_string(props,
3308 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3309 (void) nvlist_lookup_uint64(props,
3310 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3313 spa = spa_add(pool, config, altroot);
3314 spa->spa_import_flags = flags;
3317 * Verbatim import - Take a pool and insert it into the namespace
3318 * as if it had been loaded at boot.
3320 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3322 spa_configfile_set(spa, props, B_FALSE);
3324 spa_config_sync(spa, B_FALSE, B_TRUE);
3326 mutex_exit(&spa_namespace_lock);
3327 spa_history_log_version(spa, LOG_POOL_IMPORT);
3332 spa_activate(spa, mode);
3335 * Don't start async tasks until we know everything is healthy.
3337 spa_async_suspend(spa);
3339 zpool_get_rewind_policy(config, &policy);
3340 if (policy.zrp_request & ZPOOL_DO_REWIND)
3341 state = SPA_LOAD_RECOVER;
3344 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3345 * because the user-supplied config is actually the one to trust when
3348 if (state != SPA_LOAD_RECOVER)
3349 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3351 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3352 policy.zrp_request);
3355 * Propagate anything learned while loading the pool and pass it
3356 * back to caller (i.e. rewind info, missing devices, etc).
3358 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3359 spa->spa_load_info) == 0);
3361 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3363 * Toss any existing sparelist, as it doesn't have any validity
3364 * anymore, and conflicts with spa_has_spare().
3366 if (spa->spa_spares.sav_config) {
3367 nvlist_free(spa->spa_spares.sav_config);
3368 spa->spa_spares.sav_config = NULL;
3369 spa_load_spares(spa);
3371 if (spa->spa_l2cache.sav_config) {
3372 nvlist_free(spa->spa_l2cache.sav_config);
3373 spa->spa_l2cache.sav_config = NULL;
3374 spa_load_l2cache(spa);
3377 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3380 error = spa_validate_aux(spa, nvroot, -1ULL,
3383 error = spa_validate_aux(spa, nvroot, -1ULL,
3384 VDEV_ALLOC_L2CACHE);
3385 spa_config_exit(spa, SCL_ALL, FTAG);
3388 spa_configfile_set(spa, props, B_FALSE);
3390 if (error != 0 || (props && spa_writeable(spa) &&
3391 (error = spa_prop_set(spa, props)))) {
3393 spa_deactivate(spa);
3395 mutex_exit(&spa_namespace_lock);
3399 spa_async_resume(spa);
3402 * Override any spares and level 2 cache devices as specified by
3403 * the user, as these may have correct device names/devids, etc.
3405 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3406 &spares, &nspares) == 0) {
3407 if (spa->spa_spares.sav_config)
3408 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3409 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3411 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3412 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3413 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3414 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3415 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3416 spa_load_spares(spa);
3417 spa_config_exit(spa, SCL_ALL, FTAG);
3418 spa->spa_spares.sav_sync = B_TRUE;
3420 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3421 &l2cache, &nl2cache) == 0) {
3422 if (spa->spa_l2cache.sav_config)
3423 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3424 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3426 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3427 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3428 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3429 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3430 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3431 spa_load_l2cache(spa);
3432 spa_config_exit(spa, SCL_ALL, FTAG);
3433 spa->spa_l2cache.sav_sync = B_TRUE;
3437 * Check for any removed devices.
3439 if (spa->spa_autoreplace) {
3440 spa_aux_check_removed(&spa->spa_spares);
3441 spa_aux_check_removed(&spa->spa_l2cache);
3444 if (spa_writeable(spa)) {
3446 * Update the config cache to include the newly-imported pool.
3448 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3452 * It's possible that the pool was expanded while it was exported.
3453 * We kick off an async task to handle this for us.
3455 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3457 mutex_exit(&spa_namespace_lock);
3458 spa_history_log_version(spa, LOG_POOL_IMPORT);
3464 spa_tryimport(nvlist_t *tryconfig)
3466 nvlist_t *config = NULL;
3472 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3475 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3479 * Create and initialize the spa structure.
3481 mutex_enter(&spa_namespace_lock);
3482 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3483 spa_activate(spa, FREAD);
3486 * Pass off the heavy lifting to spa_load().
3487 * Pass TRUE for mosconfig because the user-supplied config
3488 * is actually the one to trust when doing an import.
3490 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3493 * If 'tryconfig' was at least parsable, return the current config.
3495 if (spa->spa_root_vdev != NULL) {
3496 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3497 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3499 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3501 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3502 spa->spa_uberblock.ub_timestamp) == 0);
3505 * If the bootfs property exists on this pool then we
3506 * copy it out so that external consumers can tell which
3507 * pools are bootable.
3509 if ((!error || error == EEXIST) && spa->spa_bootfs) {
3510 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3513 * We have to play games with the name since the
3514 * pool was opened as TRYIMPORT_NAME.
3516 if (dsl_dsobj_to_dsname(spa_name(spa),
3517 spa->spa_bootfs, tmpname) == 0) {
3519 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3521 cp = strchr(tmpname, '/');
3523 (void) strlcpy(dsname, tmpname,
3526 (void) snprintf(dsname, MAXPATHLEN,
3527 "%s/%s", poolname, ++cp);
3529 VERIFY(nvlist_add_string(config,
3530 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3531 kmem_free(dsname, MAXPATHLEN);
3533 kmem_free(tmpname, MAXPATHLEN);
3537 * Add the list of hot spares and level 2 cache devices.
3539 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3540 spa_add_spares(spa, config);
3541 spa_add_l2cache(spa, config);
3542 spa_config_exit(spa, SCL_CONFIG, FTAG);
3546 spa_deactivate(spa);
3548 mutex_exit(&spa_namespace_lock);
3554 * Pool export/destroy
3556 * The act of destroying or exporting a pool is very simple. We make sure there
3557 * is no more pending I/O and any references to the pool are gone. Then, we
3558 * update the pool state and sync all the labels to disk, removing the
3559 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3560 * we don't sync the labels or remove the configuration cache.
3563 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
3564 boolean_t force, boolean_t hardforce)
3571 if (!(spa_mode_global & FWRITE))
3574 mutex_enter(&spa_namespace_lock);
3575 if ((spa = spa_lookup(pool)) == NULL) {
3576 mutex_exit(&spa_namespace_lock);
3581 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3582 * reacquire the namespace lock, and see if we can export.
3584 spa_open_ref(spa, FTAG);
3585 mutex_exit(&spa_namespace_lock);
3586 spa_async_suspend(spa);
3587 mutex_enter(&spa_namespace_lock);
3588 spa_close(spa, FTAG);
3591 * The pool will be in core if it's openable,
3592 * in which case we can modify its state.
3594 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3596 * Objsets may be open only because they're dirty, so we
3597 * have to force it to sync before checking spa_refcnt.
3599 txg_wait_synced(spa->spa_dsl_pool, 0);
3602 * A pool cannot be exported or destroyed if there are active
3603 * references. If we are resetting a pool, allow references by
3604 * fault injection handlers.
3606 if (!spa_refcount_zero(spa) ||
3607 (spa->spa_inject_ref != 0 &&
3608 new_state != POOL_STATE_UNINITIALIZED)) {
3609 spa_async_resume(spa);
3610 mutex_exit(&spa_namespace_lock);
3615 * A pool cannot be exported if it has an active shared spare.
3616 * This is to prevent other pools stealing the active spare
3617 * from an exported pool. At user's own will, such pool can
3618 * be forcedly exported.
3620 if (!force && new_state == POOL_STATE_EXPORTED &&
3621 spa_has_active_shared_spare(spa)) {
3622 spa_async_resume(spa);
3623 mutex_exit(&spa_namespace_lock);
3628 * We want this to be reflected on every label,
3629 * so mark them all dirty. spa_unload() will do the
3630 * final sync that pushes these changes out.
3632 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
3633 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3634 spa->spa_state = new_state;
3635 spa->spa_final_txg = spa_last_synced_txg(spa) +
3637 vdev_config_dirty(spa->spa_root_vdev);
3638 spa_config_exit(spa, SCL_ALL, FTAG);
3642 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
3644 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3646 spa_deactivate(spa);
3649 if (oldconfig && spa->spa_config)
3650 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
3652 if (new_state != POOL_STATE_UNINITIALIZED) {
3654 spa_config_sync(spa, B_TRUE, B_TRUE);
3657 mutex_exit(&spa_namespace_lock);
3663 * Destroy a storage pool.
3666 spa_destroy(char *pool)
3668 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
3673 * Export a storage pool.
3676 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
3677 boolean_t hardforce)
3679 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
3684 * Similar to spa_export(), this unloads the spa_t without actually removing it
3685 * from the namespace in any way.
3688 spa_reset(char *pool)
3690 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
3695 * ==========================================================================
3696 * Device manipulation
3697 * ==========================================================================
3701 * Add a device to a storage pool.
3704 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3708 vdev_t *rvd = spa->spa_root_vdev;
3710 nvlist_t **spares, **l2cache;
3711 uint_t nspares, nl2cache;
3714 ASSERT(spa_writeable(spa));
3716 txg = spa_vdev_enter(spa);
3718 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
3719 VDEV_ALLOC_ADD)) != 0)
3720 return (spa_vdev_exit(spa, NULL, txg, error));
3722 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
3724 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
3728 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
3732 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
3733 return (spa_vdev_exit(spa, vd, txg, EINVAL));
3735 if (vd->vdev_children != 0 &&
3736 (error = vdev_create(vd, txg, B_FALSE)) != 0)
3737 return (spa_vdev_exit(spa, vd, txg, error));
3740 * We must validate the spares and l2cache devices after checking the
3741 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
3743 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
3744 return (spa_vdev_exit(spa, vd, txg, error));
3747 * Transfer each new top-level vdev from vd to rvd.
3749 for (c = 0; c < vd->vdev_children; c++) {
3752 * Set the vdev id to the first hole, if one exists.
3754 for (id = 0; id < rvd->vdev_children; id++) {
3755 if (rvd->vdev_child[id]->vdev_ishole) {
3756 vdev_free(rvd->vdev_child[id]);
3760 tvd = vd->vdev_child[c];
3761 vdev_remove_child(vd, tvd);
3763 vdev_add_child(rvd, tvd);
3764 vdev_config_dirty(tvd);
3768 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
3769 ZPOOL_CONFIG_SPARES);
3770 spa_load_spares(spa);
3771 spa->spa_spares.sav_sync = B_TRUE;
3774 if (nl2cache != 0) {
3775 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
3776 ZPOOL_CONFIG_L2CACHE);
3777 spa_load_l2cache(spa);
3778 spa->spa_l2cache.sav_sync = B_TRUE;
3782 * We have to be careful when adding new vdevs to an existing pool.
3783 * If other threads start allocating from these vdevs before we
3784 * sync the config cache, and we lose power, then upon reboot we may
3785 * fail to open the pool because there are DVAs that the config cache
3786 * can't translate. Therefore, we first add the vdevs without
3787 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
3788 * and then let spa_config_update() initialize the new metaslabs.
3790 * spa_load() checks for added-but-not-initialized vdevs, so that
3791 * if we lose power at any point in this sequence, the remaining
3792 * steps will be completed the next time we load the pool.
3794 (void) spa_vdev_exit(spa, vd, txg, 0);
3796 mutex_enter(&spa_namespace_lock);
3797 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3798 mutex_exit(&spa_namespace_lock);
3804 * Attach a device to a mirror. The arguments are the path to any device
3805 * in the mirror, and the nvroot for the new device. If the path specifies
3806 * a device that is not mirrored, we automatically insert the mirror vdev.
3808 * If 'replacing' is specified, the new device is intended to replace the
3809 * existing device; in this case the two devices are made into their own
3810 * mirror using the 'replacing' vdev, which is functionally identical to
3811 * the mirror vdev (it actually reuses all the same ops) but has a few
3812 * extra rules: you can't attach to it after it's been created, and upon
3813 * completion of resilvering, the first disk (the one being replaced)
3814 * is automatically detached.
3817 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3819 uint64_t txg, dtl_max_txg;
3820 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
3821 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
3823 char *oldvdpath, *newvdpath;
3827 ASSERT(spa_writeable(spa));
3829 txg = spa_vdev_enter(spa);
3831 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3834 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3836 if (!oldvd->vdev_ops->vdev_op_leaf)
3837 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3839 pvd = oldvd->vdev_parent;
3841 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
3842 VDEV_ALLOC_ADD)) != 0)
3843 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
3845 if (newrootvd->vdev_children != 1)
3846 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3848 newvd = newrootvd->vdev_child[0];
3850 if (!newvd->vdev_ops->vdev_op_leaf)
3851 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3853 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3854 return (spa_vdev_exit(spa, newrootvd, txg, error));
3857 * Spares can't replace logs
3859 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
3860 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3864 * For attach, the only allowable parent is a mirror or the root
3867 if (pvd->vdev_ops != &vdev_mirror_ops &&
3868 pvd->vdev_ops != &vdev_root_ops)
3869 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3871 pvops = &vdev_mirror_ops;
3874 * Active hot spares can only be replaced by inactive hot
3877 if (pvd->vdev_ops == &vdev_spare_ops &&
3878 oldvd->vdev_isspare &&
3879 !spa_has_spare(spa, newvd->vdev_guid))
3880 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3883 * If the source is a hot spare, and the parent isn't already a
3884 * spare, then we want to create a new hot spare. Otherwise, we
3885 * want to create a replacing vdev. The user is not allowed to
3886 * attach to a spared vdev child unless the 'isspare' state is
3887 * the same (spare replaces spare, non-spare replaces
3890 if (pvd->vdev_ops == &vdev_replacing_ops &&
3891 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
3892 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3893 } else if (pvd->vdev_ops == &vdev_spare_ops &&
3894 newvd->vdev_isspare != oldvd->vdev_isspare) {
3895 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3898 if (newvd->vdev_isspare)
3899 pvops = &vdev_spare_ops;
3901 pvops = &vdev_replacing_ops;
3905 * Make sure the new device is big enough.
3907 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3908 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
3911 * The new device cannot have a higher alignment requirement
3912 * than the top-level vdev.
3914 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
3915 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
3918 * If this is an in-place replacement, update oldvd's path and devid
3919 * to make it distinguishable from newvd, and unopenable from now on.
3921 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
3922 spa_strfree(oldvd->vdev_path);
3923 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
3925 (void) sprintf(oldvd->vdev_path, "%s/%s",
3926 newvd->vdev_path, "old");
3927 if (oldvd->vdev_devid != NULL) {
3928 spa_strfree(oldvd->vdev_devid);
3929 oldvd->vdev_devid = NULL;
3933 /* mark the device being resilvered */
3934 newvd->vdev_resilvering = B_TRUE;
3937 * If the parent is not a mirror, or if we're replacing, insert the new
3938 * mirror/replacing/spare vdev above oldvd.
3940 if (pvd->vdev_ops != pvops)
3941 pvd = vdev_add_parent(oldvd, pvops);
3943 ASSERT(pvd->vdev_top->vdev_parent == rvd);
3944 ASSERT(pvd->vdev_ops == pvops);
3945 ASSERT(oldvd->vdev_parent == pvd);
3948 * Extract the new device from its root and add it to pvd.
3950 vdev_remove_child(newrootvd, newvd);
3951 newvd->vdev_id = pvd->vdev_children;
3952 newvd->vdev_crtxg = oldvd->vdev_crtxg;
3953 vdev_add_child(pvd, newvd);
3955 tvd = newvd->vdev_top;
3956 ASSERT(pvd->vdev_top == tvd);
3957 ASSERT(tvd->vdev_parent == rvd);
3959 vdev_config_dirty(tvd);
3962 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
3963 * for any dmu_sync-ed blocks. It will propagate upward when
3964 * spa_vdev_exit() calls vdev_dtl_reassess().
3966 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
3968 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
3969 dtl_max_txg - TXG_INITIAL);
3971 if (newvd->vdev_isspare) {
3972 spa_spare_activate(newvd);
3973 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
3976 oldvdpath = spa_strdup(oldvd->vdev_path);
3977 newvdpath = spa_strdup(newvd->vdev_path);
3978 newvd_isspare = newvd->vdev_isspare;
3981 * Mark newvd's DTL dirty in this txg.
3983 vdev_dirty(tvd, VDD_DTL, newvd, txg);
3986 * Restart the resilver
3988 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
3993 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
3995 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
3996 "%s vdev=%s %s vdev=%s",
3997 replacing && newvd_isspare ? "spare in" :
3998 replacing ? "replace" : "attach", newvdpath,
3999 replacing ? "for" : "to", oldvdpath);
4001 spa_strfree(oldvdpath);
4002 spa_strfree(newvdpath);
4004 if (spa->spa_bootfs)
4005 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4011 * Detach a device from a mirror or replacing vdev.
4012 * If 'replace_done' is specified, only detach if the parent
4013 * is a replacing vdev.
4016 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4020 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
4021 vdev_t *vd, *pvd, *cvd, *tvd;
4022 boolean_t unspare = B_FALSE;
4023 uint64_t unspare_guid = 0;
4027 ASSERT(spa_writeable(spa));
4029 txg = spa_vdev_enter(spa);
4031 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4034 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4036 if (!vd->vdev_ops->vdev_op_leaf)
4037 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4039 pvd = vd->vdev_parent;
4042 * If the parent/child relationship is not as expected, don't do it.
4043 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4044 * vdev that's replacing B with C. The user's intent in replacing
4045 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4046 * the replace by detaching C, the expected behavior is to end up
4047 * M(A,B). But suppose that right after deciding to detach C,
4048 * the replacement of B completes. We would have M(A,C), and then
4049 * ask to detach C, which would leave us with just A -- not what
4050 * the user wanted. To prevent this, we make sure that the
4051 * parent/child relationship hasn't changed -- in this example,
4052 * that C's parent is still the replacing vdev R.
4054 if (pvd->vdev_guid != pguid && pguid != 0)
4055 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4058 * Only 'replacing' or 'spare' vdevs can be replaced.
4060 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4061 pvd->vdev_ops != &vdev_spare_ops)
4062 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4064 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4065 spa_version(spa) >= SPA_VERSION_SPARES);
4068 * Only mirror, replacing, and spare vdevs support detach.
4070 if (pvd->vdev_ops != &vdev_replacing_ops &&
4071 pvd->vdev_ops != &vdev_mirror_ops &&
4072 pvd->vdev_ops != &vdev_spare_ops)
4073 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4076 * If this device has the only valid copy of some data,
4077 * we cannot safely detach it.
4079 if (vdev_dtl_required(vd))
4080 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4082 ASSERT(pvd->vdev_children >= 2);
4085 * If we are detaching the second disk from a replacing vdev, then
4086 * check to see if we changed the original vdev's path to have "/old"
4087 * at the end in spa_vdev_attach(). If so, undo that change now.
4089 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4090 vd->vdev_path != NULL) {
4091 size_t len = strlen(vd->vdev_path);
4093 for (c = 0; c < pvd->vdev_children; c++) {
4094 cvd = pvd->vdev_child[c];
4096 if (cvd == vd || cvd->vdev_path == NULL)
4099 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4100 strcmp(cvd->vdev_path + len, "/old") == 0) {
4101 spa_strfree(cvd->vdev_path);
4102 cvd->vdev_path = spa_strdup(vd->vdev_path);
4109 * If we are detaching the original disk from a spare, then it implies
4110 * that the spare should become a real disk, and be removed from the
4111 * active spare list for the pool.
4113 if (pvd->vdev_ops == &vdev_spare_ops &&
4115 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4119 * Erase the disk labels so the disk can be used for other things.
4120 * This must be done after all other error cases are handled,
4121 * but before we disembowel vd (so we can still do I/O to it).
4122 * But if we can't do it, don't treat the error as fatal --
4123 * it may be that the unwritability of the disk is the reason
4124 * it's being detached!
4126 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4129 * Remove vd from its parent and compact the parent's children.
4131 vdev_remove_child(pvd, vd);
4132 vdev_compact_children(pvd);
4135 * Remember one of the remaining children so we can get tvd below.
4137 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4140 * If we need to remove the remaining child from the list of hot spares,
4141 * do it now, marking the vdev as no longer a spare in the process.
4142 * We must do this before vdev_remove_parent(), because that can
4143 * change the GUID if it creates a new toplevel GUID. For a similar
4144 * reason, we must remove the spare now, in the same txg as the detach;
4145 * otherwise someone could attach a new sibling, change the GUID, and
4146 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4149 ASSERT(cvd->vdev_isspare);
4150 spa_spare_remove(cvd);
4151 unspare_guid = cvd->vdev_guid;
4152 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4153 cvd->vdev_unspare = B_TRUE;
4157 * If the parent mirror/replacing vdev only has one child,
4158 * the parent is no longer needed. Remove it from the tree.
4160 if (pvd->vdev_children == 1) {
4161 if (pvd->vdev_ops == &vdev_spare_ops)
4162 cvd->vdev_unspare = B_FALSE;
4163 vdev_remove_parent(cvd);
4164 cvd->vdev_resilvering = B_FALSE;
4169 * We don't set tvd until now because the parent we just removed
4170 * may have been the previous top-level vdev.
4172 tvd = cvd->vdev_top;
4173 ASSERT(tvd->vdev_parent == rvd);
4176 * Reevaluate the parent vdev state.
4178 vdev_propagate_state(cvd);
4181 * If the 'autoexpand' property is set on the pool then automatically
4182 * try to expand the size of the pool. For example if the device we
4183 * just detached was smaller than the others, it may be possible to
4184 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4185 * first so that we can obtain the updated sizes of the leaf vdevs.
4187 if (spa->spa_autoexpand) {
4189 vdev_expand(tvd, txg);
4192 vdev_config_dirty(tvd);
4195 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4196 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4197 * But first make sure we're not on any *other* txg's DTL list, to
4198 * prevent vd from being accessed after it's freed.
4200 vdpath = spa_strdup(vd->vdev_path);
4201 for (t = 0; t < TXG_SIZE; t++)
4202 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4203 vd->vdev_detached = B_TRUE;
4204 vdev_dirty(tvd, VDD_DTL, vd, txg);
4206 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4208 /* hang on to the spa before we release the lock */
4209 spa_open_ref(spa, FTAG);
4211 error = spa_vdev_exit(spa, vd, txg, 0);
4213 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4215 spa_strfree(vdpath);
4218 * If this was the removal of the original device in a hot spare vdev,
4219 * then we want to go through and remove the device from the hot spare
4220 * list of every other pool.
4223 spa_t *altspa = NULL;
4225 mutex_enter(&spa_namespace_lock);
4226 while ((altspa = spa_next(altspa)) != NULL) {
4227 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4231 spa_open_ref(altspa, FTAG);
4232 mutex_exit(&spa_namespace_lock);
4233 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4234 mutex_enter(&spa_namespace_lock);
4235 spa_close(altspa, FTAG);
4237 mutex_exit(&spa_namespace_lock);
4239 /* search the rest of the vdevs for spares to remove */
4240 spa_vdev_resilver_done(spa);
4243 /* all done with the spa; OK to release */
4244 mutex_enter(&spa_namespace_lock);
4245 spa_close(spa, FTAG);
4246 mutex_exit(&spa_namespace_lock);
4252 * Split a set of devices from their mirrors, and create a new pool from them.
4255 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4256 nvlist_t *props, boolean_t exp)
4259 uint64_t txg, *glist;
4261 uint_t c, children, lastlog;
4262 nvlist_t **child, *nvl, *tmp;
4264 char *altroot = NULL;
4265 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4266 boolean_t activate_slog;
4268 ASSERT(spa_writeable(spa));
4270 txg = spa_vdev_enter(spa);
4272 /* clear the log and flush everything up to now */
4273 activate_slog = spa_passivate_log(spa);
4274 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4275 error = spa_offline_log(spa);
4276 txg = spa_vdev_config_enter(spa);
4279 spa_activate_log(spa);
4282 return (spa_vdev_exit(spa, NULL, txg, error));
4284 /* check new spa name before going any further */
4285 if (spa_lookup(newname) != NULL)
4286 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4289 * scan through all the children to ensure they're all mirrors
4291 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4292 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4294 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4296 /* first, check to ensure we've got the right child count */
4297 rvd = spa->spa_root_vdev;
4299 for (c = 0; c < rvd->vdev_children; c++) {
4300 vdev_t *vd = rvd->vdev_child[c];
4302 /* don't count the holes & logs as children */
4303 if (vd->vdev_islog || vd->vdev_ishole) {
4311 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4312 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4314 /* next, ensure no spare or cache devices are part of the split */
4315 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4316 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4317 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4319 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
4320 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
4322 /* then, loop over each vdev and validate it */
4323 for (c = 0; c < children; c++) {
4324 uint64_t is_hole = 0;
4326 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4330 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4331 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4339 /* which disk is going to be split? */
4340 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4346 /* look it up in the spa */
4347 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4348 if (vml[c] == NULL) {
4353 /* make sure there's nothing stopping the split */
4354 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4355 vml[c]->vdev_islog ||
4356 vml[c]->vdev_ishole ||
4357 vml[c]->vdev_isspare ||
4358 vml[c]->vdev_isl2cache ||
4359 !vdev_writeable(vml[c]) ||
4360 vml[c]->vdev_children != 0 ||
4361 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4362 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4367 if (vdev_dtl_required(vml[c])) {
4372 /* we need certain info from the top level */
4373 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4374 vml[c]->vdev_top->vdev_ms_array) == 0);
4375 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4376 vml[c]->vdev_top->vdev_ms_shift) == 0);
4377 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4378 vml[c]->vdev_top->vdev_asize) == 0);
4379 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4380 vml[c]->vdev_top->vdev_ashift) == 0);
4384 kmem_free(vml, children * sizeof (vdev_t *));
4385 kmem_free(glist, children * sizeof (uint64_t));
4386 return (spa_vdev_exit(spa, NULL, txg, error));
4389 /* stop writers from using the disks */
4390 for (c = 0; c < children; c++) {
4392 vml[c]->vdev_offline = B_TRUE;
4394 vdev_reopen(spa->spa_root_vdev);
4397 * Temporarily record the splitting vdevs in the spa config. This
4398 * will disappear once the config is regenerated.
4400 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4401 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4402 glist, children) == 0);
4403 kmem_free(glist, children * sizeof (uint64_t));
4405 mutex_enter(&spa->spa_props_lock);
4406 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4408 mutex_exit(&spa->spa_props_lock);
4409 spa->spa_config_splitting = nvl;
4410 vdev_config_dirty(spa->spa_root_vdev);
4412 /* configure and create the new pool */
4413 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4414 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4415 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4416 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4417 spa_version(spa)) == 0);
4418 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4419 spa->spa_config_txg) == 0);
4420 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4421 spa_generate_guid(NULL)) == 0);
4422 (void) nvlist_lookup_string(props,
4423 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4425 /* add the new pool to the namespace */
4426 newspa = spa_add(newname, config, altroot);
4427 newspa->spa_config_txg = spa->spa_config_txg;
4428 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4430 /* release the spa config lock, retaining the namespace lock */
4431 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4433 if (zio_injection_enabled)
4434 zio_handle_panic_injection(spa, FTAG, 1);
4436 spa_activate(newspa, spa_mode_global);
4437 spa_async_suspend(newspa);
4439 /* create the new pool from the disks of the original pool */
4440 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4444 /* if that worked, generate a real config for the new pool */
4445 if (newspa->spa_root_vdev != NULL) {
4446 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4447 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4448 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4449 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4450 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4455 if (props != NULL) {
4456 spa_configfile_set(newspa, props, B_FALSE);
4457 error = spa_prop_set(newspa, props);
4462 /* flush everything */
4463 txg = spa_vdev_config_enter(newspa);
4464 vdev_config_dirty(newspa->spa_root_vdev);
4465 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4467 if (zio_injection_enabled)
4468 zio_handle_panic_injection(spa, FTAG, 2);
4470 spa_async_resume(newspa);
4472 /* finally, update the original pool's config */
4473 txg = spa_vdev_config_enter(spa);
4474 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4475 error = dmu_tx_assign(tx, TXG_WAIT);
4478 for (c = 0; c < children; c++) {
4479 if (vml[c] != NULL) {
4482 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4488 vdev_config_dirty(spa->spa_root_vdev);
4489 spa->spa_config_splitting = NULL;
4493 (void) spa_vdev_exit(spa, NULL, txg, 0);
4495 if (zio_injection_enabled)
4496 zio_handle_panic_injection(spa, FTAG, 3);
4498 /* split is complete; log a history record */
4499 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
4500 "split new pool %s from pool %s", newname, spa_name(spa));
4502 kmem_free(vml, children * sizeof (vdev_t *));
4504 /* if we're not going to mount the filesystems in userland, export */
4506 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
4513 spa_deactivate(newspa);
4516 txg = spa_vdev_config_enter(spa);
4518 /* re-online all offlined disks */
4519 for (c = 0; c < children; c++) {
4521 vml[c]->vdev_offline = B_FALSE;
4523 vdev_reopen(spa->spa_root_vdev);
4525 nvlist_free(spa->spa_config_splitting);
4526 spa->spa_config_splitting = NULL;
4527 (void) spa_vdev_exit(spa, NULL, txg, error);
4529 kmem_free(vml, children * sizeof (vdev_t *));
4534 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
4538 for (i = 0; i < count; i++) {
4541 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
4544 if (guid == target_guid)
4552 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
4553 nvlist_t *dev_to_remove)
4555 nvlist_t **newdev = NULL;
4559 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
4561 for (i = 0, j = 0; i < count; i++) {
4562 if (dev[i] == dev_to_remove)
4564 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
4567 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
4568 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
4570 for (i = 0; i < count - 1; i++)
4571 nvlist_free(newdev[i]);
4574 kmem_free(newdev, (count - 1) * sizeof (void *));
4578 * Evacuate the device.
4581 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
4586 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4587 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4588 ASSERT(vd == vd->vdev_top);
4591 * Evacuate the device. We don't hold the config lock as writer
4592 * since we need to do I/O but we do keep the
4593 * spa_namespace_lock held. Once this completes the device
4594 * should no longer have any blocks allocated on it.
4596 if (vd->vdev_islog) {
4597 if (vd->vdev_stat.vs_alloc != 0)
4598 error = spa_offline_log(spa);
4607 * The evacuation succeeded. Remove any remaining MOS metadata
4608 * associated with this vdev, and wait for these changes to sync.
4610 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
4611 txg = spa_vdev_config_enter(spa);
4612 vd->vdev_removing = B_TRUE;
4613 vdev_dirty(vd, 0, NULL, txg);
4614 vdev_config_dirty(vd);
4615 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4621 * Complete the removal by cleaning up the namespace.
4624 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
4626 vdev_t *rvd = spa->spa_root_vdev;
4627 uint64_t id = vd->vdev_id;
4628 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
4630 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4631 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4632 ASSERT(vd == vd->vdev_top);
4635 * Only remove any devices which are empty.
4637 if (vd->vdev_stat.vs_alloc != 0)
4640 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4642 if (list_link_active(&vd->vdev_state_dirty_node))
4643 vdev_state_clean(vd);
4644 if (list_link_active(&vd->vdev_config_dirty_node))
4645 vdev_config_clean(vd);
4650 vdev_compact_children(rvd);
4652 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
4653 vdev_add_child(rvd, vd);
4655 vdev_config_dirty(rvd);
4658 * Reassess the health of our root vdev.
4664 * Remove a device from the pool -
4666 * Removing a device from the vdev namespace requires several steps
4667 * and can take a significant amount of time. As a result we use
4668 * the spa_vdev_config_[enter/exit] functions which allow us to
4669 * grab and release the spa_config_lock while still holding the namespace
4670 * lock. During each step the configuration is synced out.
4674 * Remove a device from the pool. Currently, this supports removing only hot
4675 * spares, slogs, and level 2 ARC devices.
4678 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
4681 metaslab_group_t *mg;
4682 nvlist_t **spares, **l2cache, *nv;
4684 uint_t nspares, nl2cache;
4686 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
4688 ASSERT(spa_writeable(spa));
4691 txg = spa_vdev_enter(spa);
4693 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4695 if (spa->spa_spares.sav_vdevs != NULL &&
4696 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
4697 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
4698 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
4700 * Only remove the hot spare if it's not currently in use
4703 if (vd == NULL || unspare) {
4704 spa_vdev_remove_aux(spa->spa_spares.sav_config,
4705 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
4706 spa_load_spares(spa);
4707 spa->spa_spares.sav_sync = B_TRUE;
4711 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
4712 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
4713 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
4714 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
4716 * Cache devices can always be removed.
4718 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
4719 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
4720 spa_load_l2cache(spa);
4721 spa->spa_l2cache.sav_sync = B_TRUE;
4722 } else if (vd != NULL && vd->vdev_islog) {
4724 ASSERT(vd == vd->vdev_top);
4727 * XXX - Once we have bp-rewrite this should
4728 * become the common case.
4734 * Stop allocating from this vdev.
4736 metaslab_group_passivate(mg);
4739 * Wait for the youngest allocations and frees to sync,
4740 * and then wait for the deferral of those frees to finish.
4742 spa_vdev_config_exit(spa, NULL,
4743 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
4746 * Attempt to evacuate the vdev.
4748 error = spa_vdev_remove_evacuate(spa, vd);
4750 txg = spa_vdev_config_enter(spa);
4753 * If we couldn't evacuate the vdev, unwind.
4756 metaslab_group_activate(mg);
4757 return (spa_vdev_exit(spa, NULL, txg, error));
4761 * Clean up the vdev namespace.
4763 spa_vdev_remove_from_namespace(spa, vd);
4765 } else if (vd != NULL) {
4767 * Normal vdevs cannot be removed (yet).
4772 * There is no vdev of any kind with the specified guid.
4778 return (spa_vdev_exit(spa, NULL, txg, error));
4784 * Find any device that's done replacing, or a vdev marked 'unspare' that's
4785 * current spared, so we can detach it.
4788 spa_vdev_resilver_done_hunt(vdev_t *vd)
4790 vdev_t *newvd, *oldvd;
4793 for (c = 0; c < vd->vdev_children; c++) {
4794 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
4800 * Check for a completed replacement. We always consider the first
4801 * vdev in the list to be the oldest vdev, and the last one to be
4802 * the newest (see spa_vdev_attach() for how that works). In
4803 * the case where the newest vdev is faulted, we will not automatically
4804 * remove it after a resilver completes. This is OK as it will require
4805 * user intervention to determine which disk the admin wishes to keep.
4807 if (vd->vdev_ops == &vdev_replacing_ops) {
4808 ASSERT(vd->vdev_children > 1);
4810 newvd = vd->vdev_child[vd->vdev_children - 1];
4811 oldvd = vd->vdev_child[0];
4813 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
4814 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4815 !vdev_dtl_required(oldvd))
4820 * Check for a completed resilver with the 'unspare' flag set.
4822 if (vd->vdev_ops == &vdev_spare_ops) {
4823 vdev_t *first = vd->vdev_child[0];
4824 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
4826 if (last->vdev_unspare) {
4829 } else if (first->vdev_unspare) {
4836 if (oldvd != NULL &&
4837 vdev_dtl_empty(newvd, DTL_MISSING) &&
4838 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4839 !vdev_dtl_required(oldvd))
4843 * If there are more than two spares attached to a disk,
4844 * and those spares are not required, then we want to
4845 * attempt to free them up now so that they can be used
4846 * by other pools. Once we're back down to a single
4847 * disk+spare, we stop removing them.
4849 if (vd->vdev_children > 2) {
4850 newvd = vd->vdev_child[1];
4852 if (newvd->vdev_isspare && last->vdev_isspare &&
4853 vdev_dtl_empty(last, DTL_MISSING) &&
4854 vdev_dtl_empty(last, DTL_OUTAGE) &&
4855 !vdev_dtl_required(newvd))
4864 spa_vdev_resilver_done(spa_t *spa)
4866 vdev_t *vd, *pvd, *ppvd;
4867 uint64_t guid, sguid, pguid, ppguid;
4869 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4871 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
4872 pvd = vd->vdev_parent;
4873 ppvd = pvd->vdev_parent;
4874 guid = vd->vdev_guid;
4875 pguid = pvd->vdev_guid;
4876 ppguid = ppvd->vdev_guid;
4879 * If we have just finished replacing a hot spared device, then
4880 * we need to detach the parent's first child (the original hot
4883 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
4884 ppvd->vdev_children == 2) {
4885 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
4886 sguid = ppvd->vdev_child[1]->vdev_guid;
4888 spa_config_exit(spa, SCL_ALL, FTAG);
4889 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
4891 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
4893 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4896 spa_config_exit(spa, SCL_ALL, FTAG);
4900 * Update the stored path or FRU for this vdev.
4903 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
4907 boolean_t sync = B_FALSE;
4909 ASSERT(spa_writeable(spa));
4911 spa_vdev_state_enter(spa, SCL_ALL);
4913 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
4914 return (spa_vdev_state_exit(spa, NULL, ENOENT));
4916 if (!vd->vdev_ops->vdev_op_leaf)
4917 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
4920 if (strcmp(value, vd->vdev_path) != 0) {
4921 spa_strfree(vd->vdev_path);
4922 vd->vdev_path = spa_strdup(value);
4926 if (vd->vdev_fru == NULL) {
4927 vd->vdev_fru = spa_strdup(value);
4929 } else if (strcmp(value, vd->vdev_fru) != 0) {
4930 spa_strfree(vd->vdev_fru);
4931 vd->vdev_fru = spa_strdup(value);
4936 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
4940 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
4942 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
4946 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
4948 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
4952 * ==========================================================================
4954 * ==========================================================================
4958 spa_scan_stop(spa_t *spa)
4960 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4961 if (dsl_scan_resilvering(spa->spa_dsl_pool))
4963 return (dsl_scan_cancel(spa->spa_dsl_pool));
4967 spa_scan(spa_t *spa, pool_scan_func_t func)
4969 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4971 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
4975 * If a resilver was requested, but there is no DTL on a
4976 * writeable leaf device, we have nothing to do.
4978 if (func == POOL_SCAN_RESILVER &&
4979 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
4980 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
4984 return (dsl_scan(spa->spa_dsl_pool, func));
4988 * ==========================================================================
4989 * SPA async task processing
4990 * ==========================================================================
4994 spa_async_remove(spa_t *spa, vdev_t *vd)
4998 if (vd->vdev_remove_wanted) {
4999 vd->vdev_remove_wanted = B_FALSE;
5000 vd->vdev_delayed_close = B_FALSE;
5001 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5004 * We want to clear the stats, but we don't want to do a full
5005 * vdev_clear() as that will cause us to throw away
5006 * degraded/faulted state as well as attempt to reopen the
5007 * device, all of which is a waste.
5009 vd->vdev_stat.vs_read_errors = 0;
5010 vd->vdev_stat.vs_write_errors = 0;
5011 vd->vdev_stat.vs_checksum_errors = 0;
5013 vdev_state_dirty(vd->vdev_top);
5016 for (c = 0; c < vd->vdev_children; c++)
5017 spa_async_remove(spa, vd->vdev_child[c]);
5021 spa_async_probe(spa_t *spa, vdev_t *vd)
5025 if (vd->vdev_probe_wanted) {
5026 vd->vdev_probe_wanted = B_FALSE;
5027 vdev_reopen(vd); /* vdev_open() does the actual probe */
5030 for (c = 0; c < vd->vdev_children; c++)
5031 spa_async_probe(spa, vd->vdev_child[c]);
5035 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5042 if (!spa->spa_autoexpand)
5045 for (c = 0; c < vd->vdev_children; c++) {
5046 vdev_t *cvd = vd->vdev_child[c];
5047 spa_async_autoexpand(spa, cvd);
5050 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5053 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5054 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5056 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5057 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5059 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5060 ESC_DEV_DLE, attr, &eid, DDI_SLEEP);
5063 kmem_free(physpath, MAXPATHLEN);
5067 spa_async_thread(spa_t *spa)
5071 ASSERT(spa->spa_sync_on);
5073 mutex_enter(&spa->spa_async_lock);
5074 tasks = spa->spa_async_tasks;
5075 spa->spa_async_tasks = 0;
5076 mutex_exit(&spa->spa_async_lock);
5079 * See if the config needs to be updated.
5081 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5082 uint64_t old_space, new_space;
5084 mutex_enter(&spa_namespace_lock);
5085 old_space = metaslab_class_get_space(spa_normal_class(spa));
5086 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5087 new_space = metaslab_class_get_space(spa_normal_class(spa));
5088 mutex_exit(&spa_namespace_lock);
5091 * If the pool grew as a result of the config update,
5092 * then log an internal history event.
5094 if (new_space != old_space) {
5095 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5097 "pool '%s' size: %llu(+%llu)",
5098 spa_name(spa), new_space, new_space - old_space);
5103 * See if any devices need to be marked REMOVED.
5105 if (tasks & SPA_ASYNC_REMOVE) {
5106 spa_vdev_state_enter(spa, SCL_NONE);
5107 spa_async_remove(spa, spa->spa_root_vdev);
5108 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
5109 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5110 for (i = 0; i < spa->spa_spares.sav_count; i++)
5111 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5112 (void) spa_vdev_state_exit(spa, NULL, 0);
5115 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5116 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5117 spa_async_autoexpand(spa, spa->spa_root_vdev);
5118 spa_config_exit(spa, SCL_CONFIG, FTAG);
5122 * See if any devices need to be probed.
5124 if (tasks & SPA_ASYNC_PROBE) {
5125 spa_vdev_state_enter(spa, SCL_NONE);
5126 spa_async_probe(spa, spa->spa_root_vdev);
5127 (void) spa_vdev_state_exit(spa, NULL, 0);
5131 * If any devices are done replacing, detach them.
5133 if (tasks & SPA_ASYNC_RESILVER_DONE)
5134 spa_vdev_resilver_done(spa);
5137 * Kick off a resilver.
5139 if (tasks & SPA_ASYNC_RESILVER)
5140 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5143 * Let the world know that we're done.
5145 mutex_enter(&spa->spa_async_lock);
5146 spa->spa_async_thread = NULL;
5147 cv_broadcast(&spa->spa_async_cv);
5148 mutex_exit(&spa->spa_async_lock);
5153 spa_async_suspend(spa_t *spa)
5155 mutex_enter(&spa->spa_async_lock);
5156 spa->spa_async_suspended++;
5157 while (spa->spa_async_thread != NULL)
5158 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5159 mutex_exit(&spa->spa_async_lock);
5163 spa_async_resume(spa_t *spa)
5165 mutex_enter(&spa->spa_async_lock);
5166 ASSERT(spa->spa_async_suspended != 0);
5167 spa->spa_async_suspended--;
5168 mutex_exit(&spa->spa_async_lock);
5172 spa_async_dispatch(spa_t *spa)
5174 mutex_enter(&spa->spa_async_lock);
5175 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5176 spa->spa_async_thread == NULL &&
5177 rootdir != NULL && !vn_is_readonly(rootdir))
5178 spa->spa_async_thread = thread_create(NULL, 0,
5179 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5180 mutex_exit(&spa->spa_async_lock);
5184 spa_async_request(spa_t *spa, int task)
5186 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5187 mutex_enter(&spa->spa_async_lock);
5188 spa->spa_async_tasks |= task;
5189 mutex_exit(&spa->spa_async_lock);
5193 * ==========================================================================
5194 * SPA syncing routines
5195 * ==========================================================================
5199 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5202 bpobj_enqueue(bpo, bp, tx);
5207 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5211 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5217 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5219 char *packed = NULL;
5224 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5227 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5228 * information. This avoids the dbuf_will_dirty() path and
5229 * saves us a pre-read to get data we don't actually care about.
5231 bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
5232 packed = kmem_alloc(bufsize, KM_SLEEP);
5234 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5236 bzero(packed + nvsize, bufsize - nvsize);
5238 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5240 kmem_free(packed, bufsize);
5242 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5243 dmu_buf_will_dirty(db, tx);
5244 *(uint64_t *)db->db_data = nvsize;
5245 dmu_buf_rele(db, FTAG);
5249 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5250 const char *config, const char *entry)
5260 * Update the MOS nvlist describing the list of available devices.
5261 * spa_validate_aux() will have already made sure this nvlist is
5262 * valid and the vdevs are labeled appropriately.
5264 if (sav->sav_object == 0) {
5265 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5266 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5267 sizeof (uint64_t), tx);
5268 VERIFY(zap_update(spa->spa_meta_objset,
5269 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5270 &sav->sav_object, tx) == 0);
5273 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5274 if (sav->sav_count == 0) {
5275 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5277 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5278 for (i = 0; i < sav->sav_count; i++)
5279 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5280 B_FALSE, VDEV_CONFIG_L2CACHE);
5281 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5282 sav->sav_count) == 0);
5283 for (i = 0; i < sav->sav_count; i++)
5284 nvlist_free(list[i]);
5285 kmem_free(list, sav->sav_count * sizeof (void *));
5288 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5289 nvlist_free(nvroot);
5291 sav->sav_sync = B_FALSE;
5295 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5299 if (list_is_empty(&spa->spa_config_dirty_list))
5302 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5304 config = spa_config_generate(spa, spa->spa_root_vdev,
5305 dmu_tx_get_txg(tx), B_FALSE);
5307 spa_config_exit(spa, SCL_STATE, FTAG);
5309 if (spa->spa_config_syncing)
5310 nvlist_free(spa->spa_config_syncing);
5311 spa->spa_config_syncing = config;
5313 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5317 * Set zpool properties.
5320 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5323 objset_t *mos = spa->spa_meta_objset;
5324 nvlist_t *nvp = arg2;
5329 const char *propname;
5330 zprop_type_t proptype;
5332 mutex_enter(&spa->spa_props_lock);
5335 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5336 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
5337 case ZPOOL_PROP_VERSION:
5339 * Only set version for non-zpool-creation cases
5340 * (set/import). spa_create() needs special care
5341 * for version setting.
5343 if (tx->tx_txg != TXG_INITIAL) {
5344 VERIFY(nvpair_value_uint64(elem,
5346 ASSERT(intval <= SPA_VERSION);
5347 ASSERT(intval >= spa_version(spa));
5348 spa->spa_uberblock.ub_version = intval;
5349 vdev_config_dirty(spa->spa_root_vdev);
5353 case ZPOOL_PROP_ALTROOT:
5355 * 'altroot' is a non-persistent property. It should
5356 * have been set temporarily at creation or import time.
5358 ASSERT(spa->spa_root != NULL);
5361 case ZPOOL_PROP_READONLY:
5362 case ZPOOL_PROP_CACHEFILE:
5364 * 'readonly' and 'cachefile' are also non-persisitent
5370 * Set pool property values in the poolprops mos object.
5372 if (spa->spa_pool_props_object == 0) {
5373 VERIFY((spa->spa_pool_props_object =
5374 zap_create(mos, DMU_OT_POOL_PROPS,
5375 DMU_OT_NONE, 0, tx)) > 0);
5377 VERIFY(zap_update(mos,
5378 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5379 8, 1, &spa->spa_pool_props_object, tx)
5383 /* normalize the property name */
5384 propname = zpool_prop_to_name(prop);
5385 proptype = zpool_prop_get_type(prop);
5387 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5388 ASSERT(proptype == PROP_TYPE_STRING);
5389 VERIFY(nvpair_value_string(elem, &strval) == 0);
5390 VERIFY(zap_update(mos,
5391 spa->spa_pool_props_object, propname,
5392 1, strlen(strval) + 1, strval, tx) == 0);
5394 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5395 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5397 if (proptype == PROP_TYPE_INDEX) {
5399 VERIFY(zpool_prop_index_to_string(
5400 prop, intval, &unused) == 0);
5402 VERIFY(zap_update(mos,
5403 spa->spa_pool_props_object, propname,
5404 8, 1, &intval, tx) == 0);
5406 ASSERT(0); /* not allowed */
5410 case ZPOOL_PROP_DELEGATION:
5411 spa->spa_delegation = intval;
5413 case ZPOOL_PROP_BOOTFS:
5414 spa->spa_bootfs = intval;
5416 case ZPOOL_PROP_FAILUREMODE:
5417 spa->spa_failmode = intval;
5419 case ZPOOL_PROP_AUTOEXPAND:
5420 spa->spa_autoexpand = intval;
5421 if (tx->tx_txg != TXG_INITIAL)
5422 spa_async_request(spa,
5423 SPA_ASYNC_AUTOEXPAND);
5425 case ZPOOL_PROP_DEDUPDITTO:
5426 spa->spa_dedup_ditto = intval;
5433 /* log internal history if this is not a zpool create */
5434 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5435 tx->tx_txg != TXG_INITIAL) {
5436 spa_history_log_internal(LOG_POOL_PROPSET,
5437 spa, tx, "%s %lld %s",
5438 nvpair_name(elem), intval, spa_name(spa));
5442 mutex_exit(&spa->spa_props_lock);
5446 * Perform one-time upgrade on-disk changes. spa_version() does not
5447 * reflect the new version this txg, so there must be no changes this
5448 * txg to anything that the upgrade code depends on after it executes.
5449 * Therefore this must be called after dsl_pool_sync() does the sync
5453 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5455 dsl_pool_t *dp = spa->spa_dsl_pool;
5457 ASSERT(spa->spa_sync_pass == 1);
5459 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5460 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5461 dsl_pool_create_origin(dp, tx);
5463 /* Keeping the origin open increases spa_minref */
5464 spa->spa_minref += 3;
5467 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5468 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5469 dsl_pool_upgrade_clones(dp, tx);
5472 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5473 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5474 dsl_pool_upgrade_dir_clones(dp, tx);
5476 /* Keeping the freedir open increases spa_minref */
5477 spa->spa_minref += 3;
5482 * Sync the specified transaction group. New blocks may be dirtied as
5483 * part of the process, so we iterate until it converges.
5486 spa_sync(spa_t *spa, uint64_t txg)
5488 dsl_pool_t *dp = spa->spa_dsl_pool;
5489 objset_t *mos = spa->spa_meta_objset;
5490 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
5491 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
5492 vdev_t *rvd = spa->spa_root_vdev;
5498 VERIFY(spa_writeable(spa));
5501 * Lock out configuration changes.
5503 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5505 spa->spa_syncing_txg = txg;
5506 spa->spa_sync_pass = 0;
5509 * If there are any pending vdev state changes, convert them
5510 * into config changes that go out with this transaction group.
5512 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5513 while (list_head(&spa->spa_state_dirty_list) != NULL) {
5515 * We need the write lock here because, for aux vdevs,
5516 * calling vdev_config_dirty() modifies sav_config.
5517 * This is ugly and will become unnecessary when we
5518 * eliminate the aux vdev wart by integrating all vdevs
5519 * into the root vdev tree.
5521 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5522 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
5523 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
5524 vdev_state_clean(vd);
5525 vdev_config_dirty(vd);
5527 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5528 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
5530 spa_config_exit(spa, SCL_STATE, FTAG);
5532 tx = dmu_tx_create_assigned(dp, txg);
5535 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5536 * set spa_deflate if we have no raid-z vdevs.
5538 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
5539 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
5542 for (i = 0; i < rvd->vdev_children; i++) {
5543 vd = rvd->vdev_child[i];
5544 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
5547 if (i == rvd->vdev_children) {
5548 spa->spa_deflate = TRUE;
5549 VERIFY(0 == zap_add(spa->spa_meta_objset,
5550 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
5551 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
5556 * If anything has changed in this txg, or if someone is waiting
5557 * for this txg to sync (eg, spa_vdev_remove()), push the
5558 * deferred frees from the previous txg. If not, leave them
5559 * alone so that we don't generate work on an otherwise idle
5562 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
5563 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
5564 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
5565 ((dsl_scan_active(dp->dp_scan) ||
5566 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
5567 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5568 VERIFY3U(bpobj_iterate(defer_bpo,
5569 spa_free_sync_cb, zio, tx), ==, 0);
5570 VERIFY3U(zio_wait(zio), ==, 0);
5574 * Iterate to convergence.
5577 int pass = ++spa->spa_sync_pass;
5579 spa_sync_config_object(spa, tx);
5580 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
5581 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
5582 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
5583 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
5584 spa_errlog_sync(spa, txg);
5585 dsl_pool_sync(dp, txg);
5587 if (pass <= SYNC_PASS_DEFERRED_FREE) {
5588 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5589 bplist_iterate(free_bpl, spa_free_sync_cb,
5591 VERIFY(zio_wait(zio) == 0);
5593 bplist_iterate(free_bpl, bpobj_enqueue_cb,
5598 dsl_scan_sync(dp, tx);
5600 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)))
5604 spa_sync_upgrades(spa, tx);
5606 } while (dmu_objset_is_dirty(mos, txg));
5609 * Rewrite the vdev configuration (which includes the uberblock)
5610 * to commit the transaction group.
5612 * If there are no dirty vdevs, we sync the uberblock to a few
5613 * random top-level vdevs that are known to be visible in the
5614 * config cache (see spa_vdev_add() for a complete description).
5615 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5619 * We hold SCL_STATE to prevent vdev open/close/etc.
5620 * while we're attempting to write the vdev labels.
5622 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5624 if (list_is_empty(&spa->spa_config_dirty_list)) {
5625 vdev_t *svd[SPA_DVAS_PER_BP];
5627 int children = rvd->vdev_children;
5628 int c0 = spa_get_random(children);
5630 for (c = 0; c < children; c++) {
5631 vd = rvd->vdev_child[(c0 + c) % children];
5632 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
5634 svd[svdcount++] = vd;
5635 if (svdcount == SPA_DVAS_PER_BP)
5638 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
5640 error = vdev_config_sync(svd, svdcount, txg,
5643 error = vdev_config_sync(rvd->vdev_child,
5644 rvd->vdev_children, txg, B_FALSE);
5646 error = vdev_config_sync(rvd->vdev_child,
5647 rvd->vdev_children, txg, B_TRUE);
5650 spa_config_exit(spa, SCL_STATE, FTAG);
5654 zio_suspend(spa, NULL);
5655 zio_resume_wait(spa);
5660 * Clear the dirty config list.
5662 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
5663 vdev_config_clean(vd);
5666 * Now that the new config has synced transactionally,
5667 * let it become visible to the config cache.
5669 if (spa->spa_config_syncing != NULL) {
5670 spa_config_set(spa, spa->spa_config_syncing);
5671 spa->spa_config_txg = txg;
5672 spa->spa_config_syncing = NULL;
5675 spa->spa_ubsync = spa->spa_uberblock;
5677 dsl_pool_sync_done(dp, txg);
5680 * Update usable space statistics.
5682 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))))
5683 vdev_sync_done(vd, txg);
5685 spa_update_dspace(spa);
5688 * It had better be the case that we didn't dirty anything
5689 * since vdev_config_sync().
5691 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5692 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5693 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
5695 spa->spa_sync_pass = 0;
5697 spa_config_exit(spa, SCL_CONFIG, FTAG);
5699 spa_handle_ignored_writes(spa);
5702 * If any async tasks have been requested, kick them off.
5704 spa_async_dispatch(spa);
5708 * Sync all pools. We don't want to hold the namespace lock across these
5709 * operations, so we take a reference on the spa_t and drop the lock during the
5713 spa_sync_allpools(void)
5716 mutex_enter(&spa_namespace_lock);
5717 while ((spa = spa_next(spa)) != NULL) {
5718 if (spa_state(spa) != POOL_STATE_ACTIVE ||
5719 !spa_writeable(spa) || spa_suspended(spa))
5721 spa_open_ref(spa, FTAG);
5722 mutex_exit(&spa_namespace_lock);
5723 txg_wait_synced(spa_get_dsl(spa), 0);
5724 mutex_enter(&spa_namespace_lock);
5725 spa_close(spa, FTAG);
5727 mutex_exit(&spa_namespace_lock);
5731 * ==========================================================================
5732 * Miscellaneous routines
5733 * ==========================================================================
5737 * Remove all pools in the system.
5745 * Remove all cached state. All pools should be closed now,
5746 * so every spa in the AVL tree should be unreferenced.
5748 mutex_enter(&spa_namespace_lock);
5749 while ((spa = spa_next(NULL)) != NULL) {
5751 * Stop async tasks. The async thread may need to detach
5752 * a device that's been replaced, which requires grabbing
5753 * spa_namespace_lock, so we must drop it here.
5755 spa_open_ref(spa, FTAG);
5756 mutex_exit(&spa_namespace_lock);
5757 spa_async_suspend(spa);
5758 mutex_enter(&spa_namespace_lock);
5759 spa_close(spa, FTAG);
5761 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5763 spa_deactivate(spa);
5767 mutex_exit(&spa_namespace_lock);
5771 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
5776 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
5780 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
5781 vd = spa->spa_l2cache.sav_vdevs[i];
5782 if (vd->vdev_guid == guid)
5786 for (i = 0; i < spa->spa_spares.sav_count; i++) {
5787 vd = spa->spa_spares.sav_vdevs[i];
5788 if (vd->vdev_guid == guid)
5797 spa_upgrade(spa_t *spa, uint64_t version)
5799 ASSERT(spa_writeable(spa));
5801 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5804 * This should only be called for a non-faulted pool, and since a
5805 * future version would result in an unopenable pool, this shouldn't be
5808 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
5809 ASSERT(version >= spa->spa_uberblock.ub_version);
5811 spa->spa_uberblock.ub_version = version;
5812 vdev_config_dirty(spa->spa_root_vdev);
5814 spa_config_exit(spa, SCL_ALL, FTAG);
5816 txg_wait_synced(spa_get_dsl(spa), 0);
5820 spa_has_spare(spa_t *spa, uint64_t guid)
5824 spa_aux_vdev_t *sav = &spa->spa_spares;
5826 for (i = 0; i < sav->sav_count; i++)
5827 if (sav->sav_vdevs[i]->vdev_guid == guid)
5830 for (i = 0; i < sav->sav_npending; i++) {
5831 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
5832 &spareguid) == 0 && spareguid == guid)
5840 * Check if a pool has an active shared spare device.
5841 * Note: reference count of an active spare is 2, as a spare and as a replace
5844 spa_has_active_shared_spare(spa_t *spa)
5848 spa_aux_vdev_t *sav = &spa->spa_spares;
5850 for (i = 0; i < sav->sav_count; i++) {
5851 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
5852 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
5861 * Post a sysevent corresponding to the given event. The 'name' must be one of
5862 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
5863 * filled in from the spa and (optionally) the vdev. This doesn't do anything
5864 * in the userland libzpool, as we don't want consumers to misinterpret ztest
5865 * or zdb as real changes.
5868 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
5872 sysevent_attr_list_t *attr = NULL;
5873 sysevent_value_t value;
5876 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
5879 value.value_type = SE_DATA_TYPE_STRING;
5880 value.value.sv_string = spa_name(spa);
5881 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
5884 value.value_type = SE_DATA_TYPE_UINT64;
5885 value.value.sv_uint64 = spa_guid(spa);
5886 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
5890 value.value_type = SE_DATA_TYPE_UINT64;
5891 value.value.sv_uint64 = vd->vdev_guid;
5892 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
5896 if (vd->vdev_path) {
5897 value.value_type = SE_DATA_TYPE_STRING;
5898 value.value.sv_string = vd->vdev_path;
5899 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
5900 &value, SE_SLEEP) != 0)
5905 if (sysevent_attach_attributes(ev, attr) != 0)
5909 (void) log_sysevent(ev, SE_SLEEP, &eid);
5913 sysevent_free_attr(attr);