4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
29 * This file contains all the routines used when modifying on-disk SPA state.
30 * This includes opening, importing, destroying, exporting a pool, and syncing a
34 #include <sys/zfs_context.h>
35 #include <sys/fm/fs/zfs.h>
36 #include <sys/spa_impl.h>
38 #include <sys/zio_checksum.h>
40 #include <sys/dmu_tx.h>
44 #include <sys/vdev_impl.h>
45 #include <sys/vdev_disk.h>
46 #include <sys/metaslab.h>
47 #include <sys/metaslab_impl.h>
48 #include <sys/uberblock_impl.h>
51 #include <sys/dmu_traverse.h>
52 #include <sys/dmu_objset.h>
53 #include <sys/unique.h>
54 #include <sys/dsl_pool.h>
55 #include <sys/dsl_dataset.h>
56 #include <sys/dsl_dir.h>
57 #include <sys/dsl_prop.h>
58 #include <sys/dsl_synctask.h>
59 #include <sys/fs/zfs.h>
61 #include <sys/callb.h>
62 #include <sys/systeminfo.h>
63 #include <sys/spa_boot.h>
64 #include <sys/zfs_ioctl.h>
65 #include <sys/dsl_scan.h>
68 #include <sys/bootprops.h>
69 #include <sys/callb.h>
70 #include <sys/cpupart.h>
72 #include <sys/sysdc.h>
77 #include "zfs_comutil.h"
79 typedef enum zti_modes {
80 zti_mode_fixed, /* value is # of threads (min 1) */
81 zti_mode_online_percent, /* value is % of online CPUs */
82 zti_mode_batch, /* cpu-intensive; value is ignored */
83 zti_mode_null, /* don't create a taskq */
87 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
88 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
89 #define ZTI_BATCH { zti_mode_batch, 0 }
90 #define ZTI_NULL { zti_mode_null, 0 }
92 #define ZTI_ONE ZTI_FIX(1)
94 typedef struct zio_taskq_info {
95 enum zti_modes zti_mode;
99 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
100 "iss", "iss_h", "int", "int_h"
104 * Define the taskq threads for the following I/O types:
105 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
107 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
108 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
109 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
110 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
111 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(16), ZTI_FIX(5) },
112 { ZTI_PCT(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
113 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
114 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
117 static dsl_syncfunc_t spa_sync_props;
118 static boolean_t spa_has_active_shared_spare(spa_t *spa);
119 static inline int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
120 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
122 static void spa_vdev_resilver_done(spa_t *spa);
124 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
125 id_t zio_taskq_psrset_bind = PS_NONE;
126 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
127 uint_t zio_taskq_basedc = 80; /* base duty cycle */
129 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
132 * This (illegal) pool name is used when temporarily importing a spa_t in order
133 * to get the vdev stats associated with the imported devices.
135 #define TRYIMPORT_NAME "$import"
138 * ==========================================================================
139 * SPA properties routines
140 * ==========================================================================
144 * Add a (source=src, propname=propval) list to an nvlist.
147 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
148 uint64_t intval, zprop_source_t src)
150 const char *propname = zpool_prop_to_name(prop);
153 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
154 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
157 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
159 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
161 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
162 nvlist_free(propval);
166 * Get property values from the spa configuration.
169 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
171 vdev_t *rvd = spa->spa_root_vdev;
175 uint64_t cap, version;
176 zprop_source_t src = ZPROP_SRC_NONE;
177 spa_config_dirent_t *dp;
180 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
183 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
184 size = metaslab_class_get_space(spa_normal_class(spa));
185 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
186 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
187 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
188 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
192 for (c = 0; c < rvd->vdev_children; c++) {
193 vdev_t *tvd = rvd->vdev_child[c];
194 space += tvd->vdev_max_asize - tvd->vdev_asize;
196 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
199 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
200 (spa_mode(spa) == FREAD), src);
202 cap = (size == 0) ? 0 : (alloc * 100 / size);
203 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
205 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
206 ddt_get_pool_dedup_ratio(spa), src);
208 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
209 rvd->vdev_state, src);
211 version = spa_version(spa);
212 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
213 src = ZPROP_SRC_DEFAULT;
215 src = ZPROP_SRC_LOCAL;
216 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
219 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
221 if (spa->spa_comment != NULL) {
222 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
226 if (spa->spa_root != NULL)
227 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
230 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
231 if (dp->scd_path == NULL) {
232 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
233 "none", 0, ZPROP_SRC_LOCAL);
234 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
235 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
236 dp->scd_path, 0, ZPROP_SRC_LOCAL);
242 * Get zpool property values.
245 spa_prop_get(spa_t *spa, nvlist_t **nvp)
247 objset_t *mos = spa->spa_meta_objset;
252 err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_PUSHPAGE);
256 mutex_enter(&spa->spa_props_lock);
259 * Get properties from the spa config.
261 spa_prop_get_config(spa, nvp);
263 /* If no pool property object, no more prop to get. */
264 if (mos == NULL || spa->spa_pool_props_object == 0) {
265 mutex_exit(&spa->spa_props_lock);
270 * Get properties from the MOS pool property object.
272 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
273 (err = zap_cursor_retrieve(&zc, &za)) == 0;
274 zap_cursor_advance(&zc)) {
277 zprop_source_t src = ZPROP_SRC_DEFAULT;
280 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
283 switch (za.za_integer_length) {
285 /* integer property */
286 if (za.za_first_integer !=
287 zpool_prop_default_numeric(prop))
288 src = ZPROP_SRC_LOCAL;
290 if (prop == ZPOOL_PROP_BOOTFS) {
292 dsl_dataset_t *ds = NULL;
294 dp = spa_get_dsl(spa);
295 rw_enter(&dp->dp_config_rwlock, RW_READER);
296 if ((err = dsl_dataset_hold_obj(dp,
297 za.za_first_integer, FTAG, &ds))) {
298 rw_exit(&dp->dp_config_rwlock);
303 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
305 dsl_dataset_name(ds, strval);
306 dsl_dataset_rele(ds, FTAG);
307 rw_exit(&dp->dp_config_rwlock);
310 intval = za.za_first_integer;
313 spa_prop_add_list(*nvp, prop, strval, intval, src);
317 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
322 /* string property */
323 strval = kmem_alloc(za.za_num_integers, KM_PUSHPAGE);
324 err = zap_lookup(mos, spa->spa_pool_props_object,
325 za.za_name, 1, za.za_num_integers, strval);
327 kmem_free(strval, za.za_num_integers);
330 spa_prop_add_list(*nvp, prop, strval, 0, src);
331 kmem_free(strval, za.za_num_integers);
338 zap_cursor_fini(&zc);
339 mutex_exit(&spa->spa_props_lock);
341 if (err && err != ENOENT) {
351 * Validate the given pool properties nvlist and modify the list
352 * for the property values to be set.
355 spa_prop_validate(spa_t *spa, nvlist_t *props)
358 int error = 0, reset_bootfs = 0;
362 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
364 char *propname, *strval;
369 propname = nvpair_name(elem);
371 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
375 case ZPOOL_PROP_VERSION:
376 error = nvpair_value_uint64(elem, &intval);
378 (intval < spa_version(spa) || intval > SPA_VERSION))
382 case ZPOOL_PROP_DELEGATION:
383 case ZPOOL_PROP_AUTOREPLACE:
384 case ZPOOL_PROP_LISTSNAPS:
385 case ZPOOL_PROP_AUTOEXPAND:
386 error = nvpair_value_uint64(elem, &intval);
387 if (!error && intval > 1)
391 case ZPOOL_PROP_BOOTFS:
393 * If the pool version is less than SPA_VERSION_BOOTFS,
394 * or the pool is still being created (version == 0),
395 * the bootfs property cannot be set.
397 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
403 * Make sure the vdev config is bootable
405 if (!vdev_is_bootable(spa->spa_root_vdev)) {
412 error = nvpair_value_string(elem, &strval);
417 if (strval == NULL || strval[0] == '\0') {
418 objnum = zpool_prop_default_numeric(
423 if ((error = dmu_objset_hold(strval,FTAG,&os)))
426 /* Must be ZPL and not gzip compressed. */
428 if (dmu_objset_type(os) != DMU_OST_ZFS) {
430 } else if ((error = dsl_prop_get_integer(strval,
431 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
432 &compress, NULL)) == 0 &&
433 !BOOTFS_COMPRESS_VALID(compress)) {
436 objnum = dmu_objset_id(os);
438 dmu_objset_rele(os, FTAG);
442 case ZPOOL_PROP_FAILUREMODE:
443 error = nvpair_value_uint64(elem, &intval);
444 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
445 intval > ZIO_FAILURE_MODE_PANIC))
449 * This is a special case which only occurs when
450 * the pool has completely failed. This allows
451 * the user to change the in-core failmode property
452 * without syncing it out to disk (I/Os might
453 * currently be blocked). We do this by returning
454 * EIO to the caller (spa_prop_set) to trick it
455 * into thinking we encountered a property validation
458 if (!error && spa_suspended(spa)) {
459 spa->spa_failmode = intval;
464 case ZPOOL_PROP_CACHEFILE:
465 if ((error = nvpair_value_string(elem, &strval)) != 0)
468 if (strval[0] == '\0')
471 if (strcmp(strval, "none") == 0)
474 if (strval[0] != '/') {
479 slash = strrchr(strval, '/');
480 ASSERT(slash != NULL);
482 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
483 strcmp(slash, "/..") == 0)
487 case ZPOOL_PROP_COMMENT:
488 if ((error = nvpair_value_string(elem, &strval)) != 0)
490 for (check = strval; *check != '\0'; check++) {
491 if (!isprint(*check)) {
497 if (strlen(strval) > ZPROP_MAX_COMMENT)
501 case ZPOOL_PROP_DEDUPDITTO:
502 if (spa_version(spa) < SPA_VERSION_DEDUP)
505 error = nvpair_value_uint64(elem, &intval);
507 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
519 if (!error && reset_bootfs) {
520 error = nvlist_remove(props,
521 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
524 error = nvlist_add_uint64(props,
525 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
533 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
536 spa_config_dirent_t *dp;
538 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
542 dp = kmem_alloc(sizeof (spa_config_dirent_t),
545 if (cachefile[0] == '\0')
546 dp->scd_path = spa_strdup(spa_config_path);
547 else if (strcmp(cachefile, "none") == 0)
550 dp->scd_path = spa_strdup(cachefile);
552 list_insert_head(&spa->spa_config_list, dp);
554 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
558 spa_prop_set(spa_t *spa, nvlist_t *nvp)
562 boolean_t need_sync = B_FALSE;
565 if ((error = spa_prop_validate(spa, nvp)) != 0)
569 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
570 if ((prop = zpool_name_to_prop(
571 nvpair_name(elem))) == ZPROP_INVAL)
574 if (prop == ZPOOL_PROP_CACHEFILE ||
575 prop == ZPOOL_PROP_ALTROOT ||
576 prop == ZPOOL_PROP_READONLY)
584 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
591 * If the bootfs property value is dsobj, clear it.
594 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
596 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
597 VERIFY(zap_remove(spa->spa_meta_objset,
598 spa->spa_pool_props_object,
599 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
605 * Change the GUID for the pool. This is done so that we can later
606 * re-import a pool built from a clone of our own vdevs. We will modify
607 * the root vdev's guid, our own pool guid, and then mark all of our
608 * vdevs dirty. Note that we must make sure that all our vdevs are
609 * online when we do this, or else any vdevs that weren't present
610 * would be orphaned from our pool. We are also going to issue a
611 * sysevent to update any watchers.
614 spa_change_guid(spa_t *spa)
616 uint64_t oldguid, newguid;
619 if (!(spa_mode_global & FWRITE))
622 txg = spa_vdev_enter(spa);
624 if (spa->spa_root_vdev->vdev_state != VDEV_STATE_HEALTHY)
625 return (spa_vdev_exit(spa, NULL, txg, ENXIO));
627 oldguid = spa_guid(spa);
628 newguid = spa_generate_guid(NULL);
629 ASSERT3U(oldguid, !=, newguid);
631 spa->spa_root_vdev->vdev_guid = newguid;
632 spa->spa_root_vdev->vdev_guid_sum += (newguid - oldguid);
634 vdev_config_dirty(spa->spa_root_vdev);
636 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_REGUID);
638 return (spa_vdev_exit(spa, NULL, txg, 0));
642 * ==========================================================================
643 * SPA state manipulation (open/create/destroy/import/export)
644 * ==========================================================================
648 spa_error_entry_compare(const void *a, const void *b)
650 spa_error_entry_t *sa = (spa_error_entry_t *)a;
651 spa_error_entry_t *sb = (spa_error_entry_t *)b;
654 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
655 sizeof (zbookmark_t));
666 * Utility function which retrieves copies of the current logs and
667 * re-initializes them in the process.
670 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
672 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
674 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
675 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
677 avl_create(&spa->spa_errlist_scrub,
678 spa_error_entry_compare, sizeof (spa_error_entry_t),
679 offsetof(spa_error_entry_t, se_avl));
680 avl_create(&spa->spa_errlist_last,
681 spa_error_entry_compare, sizeof (spa_error_entry_t),
682 offsetof(spa_error_entry_t, se_avl));
686 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
689 uint_t flags = TASKQ_PREPOPULATE;
690 boolean_t batch = B_FALSE;
694 return (NULL); /* no taskq needed */
697 ASSERT3U(value, >=, 1);
698 value = MAX(value, 1);
703 flags |= TASKQ_THREADS_CPU_PCT;
704 value = zio_taskq_batch_pct;
707 case zti_mode_online_percent:
708 flags |= TASKQ_THREADS_CPU_PCT;
712 panic("unrecognized mode for %s taskq (%u:%u) in "
718 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
720 flags |= TASKQ_DC_BATCH;
722 return (taskq_create_sysdc(name, value, 50, INT_MAX,
723 spa->spa_proc, zio_taskq_basedc, flags));
725 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
726 spa->spa_proc, flags));
730 spa_create_zio_taskqs(spa_t *spa)
734 for (t = 0; t < ZIO_TYPES; t++) {
735 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
736 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
737 enum zti_modes mode = ztip->zti_mode;
738 uint_t value = ztip->zti_value;
741 (void) snprintf(name, sizeof (name),
742 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
744 spa->spa_zio_taskq[t][q] =
745 spa_taskq_create(spa, name, mode, value);
750 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
752 spa_thread(void *arg)
757 user_t *pu = PTOU(curproc);
759 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
762 ASSERT(curproc != &p0);
763 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
764 "zpool-%s", spa->spa_name);
765 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
767 /* bind this thread to the requested psrset */
768 if (zio_taskq_psrset_bind != PS_NONE) {
770 mutex_enter(&cpu_lock);
771 mutex_enter(&pidlock);
772 mutex_enter(&curproc->p_lock);
774 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
775 0, NULL, NULL) == 0) {
776 curthread->t_bind_pset = zio_taskq_psrset_bind;
779 "Couldn't bind process for zfs pool \"%s\" to "
780 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
783 mutex_exit(&curproc->p_lock);
784 mutex_exit(&pidlock);
785 mutex_exit(&cpu_lock);
789 if (zio_taskq_sysdc) {
790 sysdc_thread_enter(curthread, 100, 0);
793 spa->spa_proc = curproc;
794 spa->spa_did = curthread->t_did;
796 spa_create_zio_taskqs(spa);
798 mutex_enter(&spa->spa_proc_lock);
799 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
801 spa->spa_proc_state = SPA_PROC_ACTIVE;
802 cv_broadcast(&spa->spa_proc_cv);
804 CALLB_CPR_SAFE_BEGIN(&cprinfo);
805 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
806 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
807 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
809 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
810 spa->spa_proc_state = SPA_PROC_GONE;
812 cv_broadcast(&spa->spa_proc_cv);
813 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
815 mutex_enter(&curproc->p_lock);
821 * Activate an uninitialized pool.
824 spa_activate(spa_t *spa, int mode)
826 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
828 spa->spa_state = POOL_STATE_ACTIVE;
829 spa->spa_mode = mode;
831 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
832 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
834 /* Try to create a covering process */
835 mutex_enter(&spa->spa_proc_lock);
836 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
837 ASSERT(spa->spa_proc == &p0);
840 #ifdef HAVE_SPA_THREAD
841 /* Only create a process if we're going to be around a while. */
842 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
843 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
845 spa->spa_proc_state = SPA_PROC_CREATED;
846 while (spa->spa_proc_state == SPA_PROC_CREATED) {
847 cv_wait(&spa->spa_proc_cv,
848 &spa->spa_proc_lock);
850 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
851 ASSERT(spa->spa_proc != &p0);
852 ASSERT(spa->spa_did != 0);
856 "Couldn't create process for zfs pool \"%s\"\n",
861 #endif /* HAVE_SPA_THREAD */
862 mutex_exit(&spa->spa_proc_lock);
864 /* If we didn't create a process, we need to create our taskqs. */
865 if (spa->spa_proc == &p0) {
866 spa_create_zio_taskqs(spa);
869 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
870 offsetof(vdev_t, vdev_config_dirty_node));
871 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
872 offsetof(vdev_t, vdev_state_dirty_node));
874 txg_list_create(&spa->spa_vdev_txg_list,
875 offsetof(struct vdev, vdev_txg_node));
877 avl_create(&spa->spa_errlist_scrub,
878 spa_error_entry_compare, sizeof (spa_error_entry_t),
879 offsetof(spa_error_entry_t, se_avl));
880 avl_create(&spa->spa_errlist_last,
881 spa_error_entry_compare, sizeof (spa_error_entry_t),
882 offsetof(spa_error_entry_t, se_avl));
886 * Opposite of spa_activate().
889 spa_deactivate(spa_t *spa)
893 ASSERT(spa->spa_sync_on == B_FALSE);
894 ASSERT(spa->spa_dsl_pool == NULL);
895 ASSERT(spa->spa_root_vdev == NULL);
896 ASSERT(spa->spa_async_zio_root == NULL);
897 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
899 txg_list_destroy(&spa->spa_vdev_txg_list);
901 list_destroy(&spa->spa_config_dirty_list);
902 list_destroy(&spa->spa_state_dirty_list);
904 for (t = 0; t < ZIO_TYPES; t++) {
905 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
906 if (spa->spa_zio_taskq[t][q] != NULL)
907 taskq_destroy(spa->spa_zio_taskq[t][q]);
908 spa->spa_zio_taskq[t][q] = NULL;
912 metaslab_class_destroy(spa->spa_normal_class);
913 spa->spa_normal_class = NULL;
915 metaslab_class_destroy(spa->spa_log_class);
916 spa->spa_log_class = NULL;
919 * If this was part of an import or the open otherwise failed, we may
920 * still have errors left in the queues. Empty them just in case.
922 spa_errlog_drain(spa);
924 avl_destroy(&spa->spa_errlist_scrub);
925 avl_destroy(&spa->spa_errlist_last);
927 spa->spa_state = POOL_STATE_UNINITIALIZED;
929 mutex_enter(&spa->spa_proc_lock);
930 if (spa->spa_proc_state != SPA_PROC_NONE) {
931 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
932 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
933 cv_broadcast(&spa->spa_proc_cv);
934 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
935 ASSERT(spa->spa_proc != &p0);
936 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
938 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
939 spa->spa_proc_state = SPA_PROC_NONE;
941 ASSERT(spa->spa_proc == &p0);
942 mutex_exit(&spa->spa_proc_lock);
945 * We want to make sure spa_thread() has actually exited the ZFS
946 * module, so that the module can't be unloaded out from underneath
949 if (spa->spa_did != 0) {
950 thread_join(spa->spa_did);
956 * Verify a pool configuration, and construct the vdev tree appropriately. This
957 * will create all the necessary vdevs in the appropriate layout, with each vdev
958 * in the CLOSED state. This will prep the pool before open/creation/import.
959 * All vdev validation is done by the vdev_alloc() routine.
962 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
963 uint_t id, int atype)
970 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
973 if ((*vdp)->vdev_ops->vdev_op_leaf)
976 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
988 for (c = 0; c < children; c++) {
990 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
998 ASSERT(*vdp != NULL);
1004 * Opposite of spa_load().
1007 spa_unload(spa_t *spa)
1011 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1016 spa_async_suspend(spa);
1021 if (spa->spa_sync_on) {
1022 txg_sync_stop(spa->spa_dsl_pool);
1023 spa->spa_sync_on = B_FALSE;
1027 * Wait for any outstanding async I/O to complete.
1029 if (spa->spa_async_zio_root != NULL) {
1030 (void) zio_wait(spa->spa_async_zio_root);
1031 spa->spa_async_zio_root = NULL;
1034 bpobj_close(&spa->spa_deferred_bpobj);
1037 * Close the dsl pool.
1039 if (spa->spa_dsl_pool) {
1040 dsl_pool_close(spa->spa_dsl_pool);
1041 spa->spa_dsl_pool = NULL;
1042 spa->spa_meta_objset = NULL;
1047 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1050 * Drop and purge level 2 cache
1052 spa_l2cache_drop(spa);
1057 if (spa->spa_root_vdev)
1058 vdev_free(spa->spa_root_vdev);
1059 ASSERT(spa->spa_root_vdev == NULL);
1061 for (i = 0; i < spa->spa_spares.sav_count; i++)
1062 vdev_free(spa->spa_spares.sav_vdevs[i]);
1063 if (spa->spa_spares.sav_vdevs) {
1064 kmem_free(spa->spa_spares.sav_vdevs,
1065 spa->spa_spares.sav_count * sizeof (void *));
1066 spa->spa_spares.sav_vdevs = NULL;
1068 if (spa->spa_spares.sav_config) {
1069 nvlist_free(spa->spa_spares.sav_config);
1070 spa->spa_spares.sav_config = NULL;
1072 spa->spa_spares.sav_count = 0;
1074 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1075 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1076 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1078 if (spa->spa_l2cache.sav_vdevs) {
1079 kmem_free(spa->spa_l2cache.sav_vdevs,
1080 spa->spa_l2cache.sav_count * sizeof (void *));
1081 spa->spa_l2cache.sav_vdevs = NULL;
1083 if (spa->spa_l2cache.sav_config) {
1084 nvlist_free(spa->spa_l2cache.sav_config);
1085 spa->spa_l2cache.sav_config = NULL;
1087 spa->spa_l2cache.sav_count = 0;
1089 spa->spa_async_suspended = 0;
1091 if (spa->spa_comment != NULL) {
1092 spa_strfree(spa->spa_comment);
1093 spa->spa_comment = NULL;
1096 spa_config_exit(spa, SCL_ALL, FTAG);
1100 * Load (or re-load) the current list of vdevs describing the active spares for
1101 * this pool. When this is called, we have some form of basic information in
1102 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1103 * then re-generate a more complete list including status information.
1106 spa_load_spares(spa_t *spa)
1113 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1116 * First, close and free any existing spare vdevs.
1118 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1119 vd = spa->spa_spares.sav_vdevs[i];
1121 /* Undo the call to spa_activate() below */
1122 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1123 B_FALSE)) != NULL && tvd->vdev_isspare)
1124 spa_spare_remove(tvd);
1129 if (spa->spa_spares.sav_vdevs)
1130 kmem_free(spa->spa_spares.sav_vdevs,
1131 spa->spa_spares.sav_count * sizeof (void *));
1133 if (spa->spa_spares.sav_config == NULL)
1136 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1137 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1139 spa->spa_spares.sav_count = (int)nspares;
1140 spa->spa_spares.sav_vdevs = NULL;
1146 * Construct the array of vdevs, opening them to get status in the
1147 * process. For each spare, there is potentially two different vdev_t
1148 * structures associated with it: one in the list of spares (used only
1149 * for basic validation purposes) and one in the active vdev
1150 * configuration (if it's spared in). During this phase we open and
1151 * validate each vdev on the spare list. If the vdev also exists in the
1152 * active configuration, then we also mark this vdev as an active spare.
1154 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1156 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1157 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1158 VDEV_ALLOC_SPARE) == 0);
1161 spa->spa_spares.sav_vdevs[i] = vd;
1163 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1164 B_FALSE)) != NULL) {
1165 if (!tvd->vdev_isspare)
1169 * We only mark the spare active if we were successfully
1170 * able to load the vdev. Otherwise, importing a pool
1171 * with a bad active spare would result in strange
1172 * behavior, because multiple pool would think the spare
1173 * is actively in use.
1175 * There is a vulnerability here to an equally bizarre
1176 * circumstance, where a dead active spare is later
1177 * brought back to life (onlined or otherwise). Given
1178 * the rarity of this scenario, and the extra complexity
1179 * it adds, we ignore the possibility.
1181 if (!vdev_is_dead(tvd))
1182 spa_spare_activate(tvd);
1186 vd->vdev_aux = &spa->spa_spares;
1188 if (vdev_open(vd) != 0)
1191 if (vdev_validate_aux(vd) == 0)
1196 * Recompute the stashed list of spares, with status information
1199 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1200 DATA_TYPE_NVLIST_ARRAY) == 0);
1202 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1204 for (i = 0; i < spa->spa_spares.sav_count; i++)
1205 spares[i] = vdev_config_generate(spa,
1206 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1207 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1208 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1209 for (i = 0; i < spa->spa_spares.sav_count; i++)
1210 nvlist_free(spares[i]);
1211 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1215 * Load (or re-load) the current list of vdevs describing the active l2cache for
1216 * this pool. When this is called, we have some form of basic information in
1217 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1218 * then re-generate a more complete list including status information.
1219 * Devices which are already active have their details maintained, and are
1223 spa_load_l2cache(spa_t *spa)
1227 int i, j, oldnvdevs;
1229 vdev_t *vd, **oldvdevs, **newvdevs = NULL;
1230 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1232 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1234 if (sav->sav_config != NULL) {
1235 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1236 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1237 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_PUSHPAGE);
1242 oldvdevs = sav->sav_vdevs;
1243 oldnvdevs = sav->sav_count;
1244 sav->sav_vdevs = NULL;
1248 * Process new nvlist of vdevs.
1250 for (i = 0; i < nl2cache; i++) {
1251 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1255 for (j = 0; j < oldnvdevs; j++) {
1257 if (vd != NULL && guid == vd->vdev_guid) {
1259 * Retain previous vdev for add/remove ops.
1267 if (newvdevs[i] == NULL) {
1271 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1272 VDEV_ALLOC_L2CACHE) == 0);
1277 * Commit this vdev as an l2cache device,
1278 * even if it fails to open.
1280 spa_l2cache_add(vd);
1285 spa_l2cache_activate(vd);
1287 if (vdev_open(vd) != 0)
1290 (void) vdev_validate_aux(vd);
1292 if (!vdev_is_dead(vd))
1293 l2arc_add_vdev(spa, vd);
1298 * Purge vdevs that were dropped
1300 for (i = 0; i < oldnvdevs; i++) {
1305 ASSERT(vd->vdev_isl2cache);
1307 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1308 pool != 0ULL && l2arc_vdev_present(vd))
1309 l2arc_remove_vdev(vd);
1310 vdev_clear_stats(vd);
1316 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1318 if (sav->sav_config == NULL)
1321 sav->sav_vdevs = newvdevs;
1322 sav->sav_count = (int)nl2cache;
1325 * Recompute the stashed list of l2cache devices, with status
1326 * information this time.
1328 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1329 DATA_TYPE_NVLIST_ARRAY) == 0);
1331 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE);
1332 for (i = 0; i < sav->sav_count; i++)
1333 l2cache[i] = vdev_config_generate(spa,
1334 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1335 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1336 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1338 for (i = 0; i < sav->sav_count; i++)
1339 nvlist_free(l2cache[i]);
1341 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1345 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1348 char *packed = NULL;
1353 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1357 nvsize = *(uint64_t *)db->db_data;
1358 dmu_buf_rele(db, FTAG);
1360 packed = kmem_alloc(nvsize, KM_PUSHPAGE | KM_NODEBUG);
1361 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1364 error = nvlist_unpack(packed, nvsize, value, 0);
1365 kmem_free(packed, nvsize);
1371 * Checks to see if the given vdev could not be opened, in which case we post a
1372 * sysevent to notify the autoreplace code that the device has been removed.
1375 spa_check_removed(vdev_t *vd)
1379 for (c = 0; c < vd->vdev_children; c++)
1380 spa_check_removed(vd->vdev_child[c]);
1382 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1383 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE,
1384 vd->vdev_spa, vd, NULL, 0, 0);
1385 spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_CHECK);
1390 * Validate the current config against the MOS config
1393 spa_config_valid(spa_t *spa, nvlist_t *config)
1395 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1399 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1401 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1402 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1404 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1407 * If we're doing a normal import, then build up any additional
1408 * diagnostic information about missing devices in this config.
1409 * We'll pass this up to the user for further processing.
1411 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1412 nvlist_t **child, *nv;
1415 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1417 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
1419 for (c = 0; c < rvd->vdev_children; c++) {
1420 vdev_t *tvd = rvd->vdev_child[c];
1421 vdev_t *mtvd = mrvd->vdev_child[c];
1423 if (tvd->vdev_ops == &vdev_missing_ops &&
1424 mtvd->vdev_ops != &vdev_missing_ops &&
1426 child[idx++] = vdev_config_generate(spa, mtvd,
1431 VERIFY(nvlist_add_nvlist_array(nv,
1432 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1433 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1434 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1436 for (i = 0; i < idx; i++)
1437 nvlist_free(child[i]);
1440 kmem_free(child, rvd->vdev_children * sizeof (char **));
1444 * Compare the root vdev tree with the information we have
1445 * from the MOS config (mrvd). Check each top-level vdev
1446 * with the corresponding MOS config top-level (mtvd).
1448 for (c = 0; c < rvd->vdev_children; c++) {
1449 vdev_t *tvd = rvd->vdev_child[c];
1450 vdev_t *mtvd = mrvd->vdev_child[c];
1453 * Resolve any "missing" vdevs in the current configuration.
1454 * If we find that the MOS config has more accurate information
1455 * about the top-level vdev then use that vdev instead.
1457 if (tvd->vdev_ops == &vdev_missing_ops &&
1458 mtvd->vdev_ops != &vdev_missing_ops) {
1460 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1464 * Device specific actions.
1466 if (mtvd->vdev_islog) {
1467 spa_set_log_state(spa, SPA_LOG_CLEAR);
1470 * XXX - once we have 'readonly' pool
1471 * support we should be able to handle
1472 * missing data devices by transitioning
1473 * the pool to readonly.
1479 * Swap the missing vdev with the data we were
1480 * able to obtain from the MOS config.
1482 vdev_remove_child(rvd, tvd);
1483 vdev_remove_child(mrvd, mtvd);
1485 vdev_add_child(rvd, mtvd);
1486 vdev_add_child(mrvd, tvd);
1488 spa_config_exit(spa, SCL_ALL, FTAG);
1490 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1493 } else if (mtvd->vdev_islog) {
1495 * Load the slog device's state from the MOS config
1496 * since it's possible that the label does not
1497 * contain the most up-to-date information.
1499 vdev_load_log_state(tvd, mtvd);
1504 spa_config_exit(spa, SCL_ALL, FTAG);
1507 * Ensure we were able to validate the config.
1509 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1513 * Check for missing log devices
1516 spa_check_logs(spa_t *spa)
1518 switch (spa->spa_log_state) {
1521 case SPA_LOG_MISSING:
1522 /* need to recheck in case slog has been restored */
1523 case SPA_LOG_UNKNOWN:
1524 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1525 DS_FIND_CHILDREN)) {
1526 spa_set_log_state(spa, SPA_LOG_MISSING);
1535 spa_passivate_log(spa_t *spa)
1537 vdev_t *rvd = spa->spa_root_vdev;
1538 boolean_t slog_found = B_FALSE;
1541 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1543 if (!spa_has_slogs(spa))
1546 for (c = 0; c < rvd->vdev_children; c++) {
1547 vdev_t *tvd = rvd->vdev_child[c];
1548 metaslab_group_t *mg = tvd->vdev_mg;
1550 if (tvd->vdev_islog) {
1551 metaslab_group_passivate(mg);
1552 slog_found = B_TRUE;
1556 return (slog_found);
1560 spa_activate_log(spa_t *spa)
1562 vdev_t *rvd = spa->spa_root_vdev;
1565 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1567 for (c = 0; c < rvd->vdev_children; c++) {
1568 vdev_t *tvd = rvd->vdev_child[c];
1569 metaslab_group_t *mg = tvd->vdev_mg;
1571 if (tvd->vdev_islog)
1572 metaslab_group_activate(mg);
1577 spa_offline_log(spa_t *spa)
1581 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1582 NULL, DS_FIND_CHILDREN)) == 0) {
1585 * We successfully offlined the log device, sync out the
1586 * current txg so that the "stubby" block can be removed
1589 txg_wait_synced(spa->spa_dsl_pool, 0);
1595 spa_aux_check_removed(spa_aux_vdev_t *sav)
1599 for (i = 0; i < sav->sav_count; i++)
1600 spa_check_removed(sav->sav_vdevs[i]);
1604 spa_claim_notify(zio_t *zio)
1606 spa_t *spa = zio->io_spa;
1611 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1612 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1613 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1614 mutex_exit(&spa->spa_props_lock);
1617 typedef struct spa_load_error {
1618 uint64_t sle_meta_count;
1619 uint64_t sle_data_count;
1623 spa_load_verify_done(zio_t *zio)
1625 blkptr_t *bp = zio->io_bp;
1626 spa_load_error_t *sle = zio->io_private;
1627 dmu_object_type_t type = BP_GET_TYPE(bp);
1628 int error = zio->io_error;
1631 if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
1632 type != DMU_OT_INTENT_LOG)
1633 atomic_add_64(&sle->sle_meta_count, 1);
1635 atomic_add_64(&sle->sle_data_count, 1);
1637 zio_data_buf_free(zio->io_data, zio->io_size);
1642 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1643 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1647 size_t size = BP_GET_PSIZE(bp);
1648 void *data = zio_data_buf_alloc(size);
1650 zio_nowait(zio_read(rio, spa, bp, data, size,
1651 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1652 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1653 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1659 spa_load_verify(spa_t *spa)
1662 spa_load_error_t sle = { 0 };
1663 zpool_rewind_policy_t policy;
1664 boolean_t verify_ok = B_FALSE;
1667 zpool_get_rewind_policy(spa->spa_config, &policy);
1669 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1672 rio = zio_root(spa, NULL, &sle,
1673 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1675 error = traverse_pool(spa, spa->spa_verify_min_txg,
1676 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1678 (void) zio_wait(rio);
1680 spa->spa_load_meta_errors = sle.sle_meta_count;
1681 spa->spa_load_data_errors = sle.sle_data_count;
1683 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1684 sle.sle_data_count <= policy.zrp_maxdata) {
1688 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1689 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1691 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1692 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1693 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1694 VERIFY(nvlist_add_int64(spa->spa_load_info,
1695 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1696 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1697 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1699 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1703 if (error != ENXIO && error != EIO)
1708 return (verify_ok ? 0 : EIO);
1712 * Find a value in the pool props object.
1715 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1717 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1718 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1722 * Find a value in the pool directory object.
1725 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1727 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1728 name, sizeof (uint64_t), 1, val));
1732 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1734 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1739 * Fix up config after a partly-completed split. This is done with the
1740 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1741 * pool have that entry in their config, but only the splitting one contains
1742 * a list of all the guids of the vdevs that are being split off.
1744 * This function determines what to do with that list: either rejoin
1745 * all the disks to the pool, or complete the splitting process. To attempt
1746 * the rejoin, each disk that is offlined is marked online again, and
1747 * we do a reopen() call. If the vdev label for every disk that was
1748 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1749 * then we call vdev_split() on each disk, and complete the split.
1751 * Otherwise we leave the config alone, with all the vdevs in place in
1752 * the original pool.
1755 spa_try_repair(spa_t *spa, nvlist_t *config)
1762 boolean_t attempt_reopen;
1764 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1767 /* check that the config is complete */
1768 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1769 &glist, &gcount) != 0)
1772 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_PUSHPAGE);
1774 /* attempt to online all the vdevs & validate */
1775 attempt_reopen = B_TRUE;
1776 for (i = 0; i < gcount; i++) {
1777 if (glist[i] == 0) /* vdev is hole */
1780 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1781 if (vd[i] == NULL) {
1783 * Don't bother attempting to reopen the disks;
1784 * just do the split.
1786 attempt_reopen = B_FALSE;
1788 /* attempt to re-online it */
1789 vd[i]->vdev_offline = B_FALSE;
1793 if (attempt_reopen) {
1794 vdev_reopen(spa->spa_root_vdev);
1796 /* check each device to see what state it's in */
1797 for (extracted = 0, i = 0; i < gcount; i++) {
1798 if (vd[i] != NULL &&
1799 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1806 * If every disk has been moved to the new pool, or if we never
1807 * even attempted to look at them, then we split them off for
1810 if (!attempt_reopen || gcount == extracted) {
1811 for (i = 0; i < gcount; i++)
1814 vdev_reopen(spa->spa_root_vdev);
1817 kmem_free(vd, gcount * sizeof (vdev_t *));
1821 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1822 boolean_t mosconfig)
1824 nvlist_t *config = spa->spa_config;
1825 char *ereport = FM_EREPORT_ZFS_POOL;
1831 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1834 ASSERT(spa->spa_comment == NULL);
1835 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1836 spa->spa_comment = spa_strdup(comment);
1839 * Versioning wasn't explicitly added to the label until later, so if
1840 * it's not present treat it as the initial version.
1842 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1843 &spa->spa_ubsync.ub_version) != 0)
1844 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1846 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1847 &spa->spa_config_txg);
1849 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1850 spa_guid_exists(pool_guid, 0)) {
1853 spa->spa_config_guid = pool_guid;
1855 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1857 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1861 gethrestime(&spa->spa_loaded_ts);
1862 error = spa_load_impl(spa, pool_guid, config, state, type,
1863 mosconfig, &ereport);
1866 spa->spa_minref = refcount_count(&spa->spa_refcount);
1868 if (error != EEXIST) {
1869 spa->spa_loaded_ts.tv_sec = 0;
1870 spa->spa_loaded_ts.tv_nsec = 0;
1872 if (error != EBADF) {
1873 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1876 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
1883 * Load an existing storage pool, using the pool's builtin spa_config as a
1884 * source of configuration information.
1886 __attribute__((always_inline))
1888 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
1889 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1893 nvlist_t *nvroot = NULL;
1895 uberblock_t *ub = &spa->spa_uberblock;
1896 uint64_t children, config_cache_txg = spa->spa_config_txg;
1897 int orig_mode = spa->spa_mode;
1902 * If this is an untrusted config, access the pool in read-only mode.
1903 * This prevents things like resilvering recently removed devices.
1906 spa->spa_mode = FREAD;
1908 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1910 spa->spa_load_state = state;
1912 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
1915 parse = (type == SPA_IMPORT_EXISTING ?
1916 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
1919 * Create "The Godfather" zio to hold all async IOs
1921 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1922 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
1925 * Parse the configuration into a vdev tree. We explicitly set the
1926 * value that will be returned by spa_version() since parsing the
1927 * configuration requires knowing the version number.
1929 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1930 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
1931 spa_config_exit(spa, SCL_ALL, FTAG);
1936 ASSERT(spa->spa_root_vdev == rvd);
1938 if (type != SPA_IMPORT_ASSEMBLE) {
1939 ASSERT(spa_guid(spa) == pool_guid);
1943 * Try to open all vdevs, loading each label in the process.
1945 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1946 error = vdev_open(rvd);
1947 spa_config_exit(spa, SCL_ALL, FTAG);
1952 * We need to validate the vdev labels against the configuration that
1953 * we have in hand, which is dependent on the setting of mosconfig. If
1954 * mosconfig is true then we're validating the vdev labels based on
1955 * that config. Otherwise, we're validating against the cached config
1956 * (zpool.cache) that was read when we loaded the zfs module, and then
1957 * later we will recursively call spa_load() and validate against
1960 * If we're assembling a new pool that's been split off from an
1961 * existing pool, the labels haven't yet been updated so we skip
1962 * validation for now.
1964 if (type != SPA_IMPORT_ASSEMBLE) {
1965 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1966 error = vdev_validate(rvd, mosconfig);
1967 spa_config_exit(spa, SCL_ALL, FTAG);
1972 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
1977 * Find the best uberblock.
1979 vdev_uberblock_load(NULL, rvd, ub);
1982 * If we weren't able to find a single valid uberblock, return failure.
1984 if (ub->ub_txg == 0)
1985 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
1988 * If the pool is newer than the code, we can't open it.
1990 if (ub->ub_version > SPA_VERSION)
1991 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1994 * If the vdev guid sum doesn't match the uberblock, we have an
1995 * incomplete configuration. We first check to see if the pool
1996 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
1997 * If it is, defer the vdev_guid_sum check till later so we
1998 * can handle missing vdevs.
2000 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2001 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2002 rvd->vdev_guid_sum != ub->ub_guid_sum)
2003 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2005 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2006 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2007 spa_try_repair(spa, config);
2008 spa_config_exit(spa, SCL_ALL, FTAG);
2009 nvlist_free(spa->spa_config_splitting);
2010 spa->spa_config_splitting = NULL;
2014 * Initialize internal SPA structures.
2016 spa->spa_state = POOL_STATE_ACTIVE;
2017 spa->spa_ubsync = spa->spa_uberblock;
2018 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2019 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2020 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2021 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2022 spa->spa_claim_max_txg = spa->spa_first_txg;
2023 spa->spa_prev_software_version = ub->ub_software_version;
2025 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2027 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2028 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2030 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2031 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2035 nvlist_t *policy = NULL, *nvconfig;
2037 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2038 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2040 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2041 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2043 unsigned long myhostid = 0;
2045 VERIFY(nvlist_lookup_string(nvconfig,
2046 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2049 myhostid = zone_get_hostid(NULL);
2052 * We're emulating the system's hostid in userland, so
2053 * we can't use zone_get_hostid().
2055 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2056 #endif /* _KERNEL */
2057 if (hostid != 0 && myhostid != 0 &&
2058 hostid != myhostid) {
2059 nvlist_free(nvconfig);
2060 cmn_err(CE_WARN, "pool '%s' could not be "
2061 "loaded as it was last accessed by "
2062 "another system (host: %s hostid: 0x%lx). "
2063 "See: http://zfsonlinux.org/msg/ZFS-8000-EY",
2064 spa_name(spa), hostname,
2065 (unsigned long)hostid);
2069 if (nvlist_lookup_nvlist(spa->spa_config,
2070 ZPOOL_REWIND_POLICY, &policy) == 0)
2071 VERIFY(nvlist_add_nvlist(nvconfig,
2072 ZPOOL_REWIND_POLICY, policy) == 0);
2074 spa_config_set(spa, nvconfig);
2076 spa_deactivate(spa);
2077 spa_activate(spa, orig_mode);
2079 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2082 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2083 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2084 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2086 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2089 * Load the bit that tells us to use the new accounting function
2090 * (raid-z deflation). If we have an older pool, this will not
2093 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2094 if (error != 0 && error != ENOENT)
2095 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2097 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2098 &spa->spa_creation_version);
2099 if (error != 0 && error != ENOENT)
2100 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2103 * Load the persistent error log. If we have an older pool, this will
2106 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2107 if (error != 0 && error != ENOENT)
2108 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2110 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2111 &spa->spa_errlog_scrub);
2112 if (error != 0 && error != ENOENT)
2113 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2116 * Load the history object. If we have an older pool, this
2117 * will not be present.
2119 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2120 if (error != 0 && error != ENOENT)
2121 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2124 * If we're assembling the pool from the split-off vdevs of
2125 * an existing pool, we don't want to attach the spares & cache
2130 * Load any hot spares for this pool.
2132 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2133 if (error != 0 && error != ENOENT)
2134 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2135 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2136 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2137 if (load_nvlist(spa, spa->spa_spares.sav_object,
2138 &spa->spa_spares.sav_config) != 0)
2139 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2141 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2142 spa_load_spares(spa);
2143 spa_config_exit(spa, SCL_ALL, FTAG);
2144 } else if (error == 0) {
2145 spa->spa_spares.sav_sync = B_TRUE;
2149 * Load any level 2 ARC devices for this pool.
2151 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2152 &spa->spa_l2cache.sav_object);
2153 if (error != 0 && error != ENOENT)
2154 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2155 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2156 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2157 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2158 &spa->spa_l2cache.sav_config) != 0)
2159 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2161 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2162 spa_load_l2cache(spa);
2163 spa_config_exit(spa, SCL_ALL, FTAG);
2164 } else if (error == 0) {
2165 spa->spa_l2cache.sav_sync = B_TRUE;
2168 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2170 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2171 if (error && error != ENOENT)
2172 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2175 uint64_t autoreplace;
2177 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2178 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2179 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2180 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2181 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2182 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2183 &spa->spa_dedup_ditto);
2185 spa->spa_autoreplace = (autoreplace != 0);
2189 * If the 'autoreplace' property is set, then post a resource notifying
2190 * the ZFS DE that it should not issue any faults for unopenable
2191 * devices. We also iterate over the vdevs, and post a sysevent for any
2192 * unopenable vdevs so that the normal autoreplace handler can take
2195 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2196 spa_check_removed(spa->spa_root_vdev);
2198 * For the import case, this is done in spa_import(), because
2199 * at this point we're using the spare definitions from
2200 * the MOS config, not necessarily from the userland config.
2202 if (state != SPA_LOAD_IMPORT) {
2203 spa_aux_check_removed(&spa->spa_spares);
2204 spa_aux_check_removed(&spa->spa_l2cache);
2209 * Load the vdev state for all toplevel vdevs.
2214 * Propagate the leaf DTLs we just loaded all the way up the tree.
2216 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2217 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2218 spa_config_exit(spa, SCL_ALL, FTAG);
2221 * Load the DDTs (dedup tables).
2223 error = ddt_load(spa);
2225 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2227 spa_update_dspace(spa);
2230 * Validate the config, using the MOS config to fill in any
2231 * information which might be missing. If we fail to validate
2232 * the config then declare the pool unfit for use. If we're
2233 * assembling a pool from a split, the log is not transferred
2236 if (type != SPA_IMPORT_ASSEMBLE) {
2239 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2240 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2242 if (!spa_config_valid(spa, nvconfig)) {
2243 nvlist_free(nvconfig);
2244 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2247 nvlist_free(nvconfig);
2250 * Now that we've validate the config, check the state of the
2251 * root vdev. If it can't be opened, it indicates one or
2252 * more toplevel vdevs are faulted.
2254 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2257 if (spa_check_logs(spa)) {
2258 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2259 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2264 * We've successfully opened the pool, verify that we're ready
2265 * to start pushing transactions.
2267 if (state != SPA_LOAD_TRYIMPORT) {
2268 if ((error = spa_load_verify(spa)))
2269 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2273 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2274 spa->spa_load_max_txg == UINT64_MAX)) {
2276 int need_update = B_FALSE;
2279 ASSERT(state != SPA_LOAD_TRYIMPORT);
2282 * Claim log blocks that haven't been committed yet.
2283 * This must all happen in a single txg.
2284 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2285 * invoked from zil_claim_log_block()'s i/o done callback.
2286 * Price of rollback is that we abandon the log.
2288 spa->spa_claiming = B_TRUE;
2290 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2291 spa_first_txg(spa));
2292 (void) dmu_objset_find(spa_name(spa),
2293 zil_claim, tx, DS_FIND_CHILDREN);
2296 spa->spa_claiming = B_FALSE;
2298 spa_set_log_state(spa, SPA_LOG_GOOD);
2299 spa->spa_sync_on = B_TRUE;
2300 txg_sync_start(spa->spa_dsl_pool);
2303 * Wait for all claims to sync. We sync up to the highest
2304 * claimed log block birth time so that claimed log blocks
2305 * don't appear to be from the future. spa_claim_max_txg
2306 * will have been set for us by either zil_check_log_chain()
2307 * (invoked from spa_check_logs()) or zil_claim() above.
2309 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2312 * If the config cache is stale, or we have uninitialized
2313 * metaslabs (see spa_vdev_add()), then update the config.
2315 * If this is a verbatim import, trust the current
2316 * in-core spa_config and update the disk labels.
2318 if (config_cache_txg != spa->spa_config_txg ||
2319 state == SPA_LOAD_IMPORT ||
2320 state == SPA_LOAD_RECOVER ||
2321 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2322 need_update = B_TRUE;
2324 for (c = 0; c < rvd->vdev_children; c++)
2325 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2326 need_update = B_TRUE;
2329 * Update the config cache asychronously in case we're the
2330 * root pool, in which case the config cache isn't writable yet.
2333 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2336 * Check all DTLs to see if anything needs resilvering.
2338 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2339 vdev_resilver_needed(rvd, NULL, NULL))
2340 spa_async_request(spa, SPA_ASYNC_RESILVER);
2343 * Delete any inconsistent datasets.
2345 (void) dmu_objset_find(spa_name(spa),
2346 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2349 * Clean up any stale temporary dataset userrefs.
2351 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2358 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2360 int mode = spa->spa_mode;
2363 spa_deactivate(spa);
2365 spa->spa_load_max_txg--;
2367 spa_activate(spa, mode);
2368 spa_async_suspend(spa);
2370 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2374 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2375 uint64_t max_request, int rewind_flags)
2377 nvlist_t *config = NULL;
2378 int load_error, rewind_error;
2379 uint64_t safe_rewind_txg;
2382 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2383 spa->spa_load_max_txg = spa->spa_load_txg;
2384 spa_set_log_state(spa, SPA_LOG_CLEAR);
2386 spa->spa_load_max_txg = max_request;
2389 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2391 if (load_error == 0)
2394 if (spa->spa_root_vdev != NULL)
2395 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2397 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2398 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2400 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2401 nvlist_free(config);
2402 return (load_error);
2405 /* Price of rolling back is discarding txgs, including log */
2406 if (state == SPA_LOAD_RECOVER)
2407 spa_set_log_state(spa, SPA_LOG_CLEAR);
2409 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2410 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2411 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2412 TXG_INITIAL : safe_rewind_txg;
2415 * Continue as long as we're finding errors, we're still within
2416 * the acceptable rewind range, and we're still finding uberblocks
2418 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2419 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2420 if (spa->spa_load_max_txg < safe_rewind_txg)
2421 spa->spa_extreme_rewind = B_TRUE;
2422 rewind_error = spa_load_retry(spa, state, mosconfig);
2425 spa->spa_extreme_rewind = B_FALSE;
2426 spa->spa_load_max_txg = UINT64_MAX;
2428 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2429 spa_config_set(spa, config);
2431 return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
2437 * The import case is identical to an open except that the configuration is sent
2438 * down from userland, instead of grabbed from the configuration cache. For the
2439 * case of an open, the pool configuration will exist in the
2440 * POOL_STATE_UNINITIALIZED state.
2442 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2443 * the same time open the pool, without having to keep around the spa_t in some
2447 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2451 spa_load_state_t state = SPA_LOAD_OPEN;
2453 int locked = B_FALSE;
2458 * As disgusting as this is, we need to support recursive calls to this
2459 * function because dsl_dir_open() is called during spa_load(), and ends
2460 * up calling spa_open() again. The real fix is to figure out how to
2461 * avoid dsl_dir_open() calling this in the first place.
2463 if (mutex_owner(&spa_namespace_lock) != curthread) {
2464 mutex_enter(&spa_namespace_lock);
2468 if ((spa = spa_lookup(pool)) == NULL) {
2470 mutex_exit(&spa_namespace_lock);
2474 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2475 zpool_rewind_policy_t policy;
2477 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2479 if (policy.zrp_request & ZPOOL_DO_REWIND)
2480 state = SPA_LOAD_RECOVER;
2482 spa_activate(spa, spa_mode_global);
2484 if (state != SPA_LOAD_RECOVER)
2485 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2487 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2488 policy.zrp_request);
2490 if (error == EBADF) {
2492 * If vdev_validate() returns failure (indicated by
2493 * EBADF), it indicates that one of the vdevs indicates
2494 * that the pool has been exported or destroyed. If
2495 * this is the case, the config cache is out of sync and
2496 * we should remove the pool from the namespace.
2499 spa_deactivate(spa);
2500 spa_config_sync(spa, B_TRUE, B_TRUE);
2503 mutex_exit(&spa_namespace_lock);
2509 * We can't open the pool, but we still have useful
2510 * information: the state of each vdev after the
2511 * attempted vdev_open(). Return this to the user.
2513 if (config != NULL && spa->spa_config) {
2514 VERIFY(nvlist_dup(spa->spa_config, config,
2516 VERIFY(nvlist_add_nvlist(*config,
2517 ZPOOL_CONFIG_LOAD_INFO,
2518 spa->spa_load_info) == 0);
2521 spa_deactivate(spa);
2522 spa->spa_last_open_failed = error;
2524 mutex_exit(&spa_namespace_lock);
2530 spa_open_ref(spa, tag);
2533 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2536 * If we've recovered the pool, pass back any information we
2537 * gathered while doing the load.
2539 if (state == SPA_LOAD_RECOVER) {
2540 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2541 spa->spa_load_info) == 0);
2545 spa->spa_last_open_failed = 0;
2546 spa->spa_last_ubsync_txg = 0;
2547 spa->spa_load_txg = 0;
2548 mutex_exit(&spa_namespace_lock);
2557 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2560 return (spa_open_common(name, spapp, tag, policy, config));
2564 spa_open(const char *name, spa_t **spapp, void *tag)
2566 return (spa_open_common(name, spapp, tag, NULL, NULL));
2570 * Lookup the given spa_t, incrementing the inject count in the process,
2571 * preventing it from being exported or destroyed.
2574 spa_inject_addref(char *name)
2578 mutex_enter(&spa_namespace_lock);
2579 if ((spa = spa_lookup(name)) == NULL) {
2580 mutex_exit(&spa_namespace_lock);
2583 spa->spa_inject_ref++;
2584 mutex_exit(&spa_namespace_lock);
2590 spa_inject_delref(spa_t *spa)
2592 mutex_enter(&spa_namespace_lock);
2593 spa->spa_inject_ref--;
2594 mutex_exit(&spa_namespace_lock);
2598 * Add spares device information to the nvlist.
2601 spa_add_spares(spa_t *spa, nvlist_t *config)
2611 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2613 if (spa->spa_spares.sav_count == 0)
2616 VERIFY(nvlist_lookup_nvlist(config,
2617 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2618 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2619 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2621 VERIFY(nvlist_add_nvlist_array(nvroot,
2622 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2623 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2624 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2627 * Go through and find any spares which have since been
2628 * repurposed as an active spare. If this is the case, update
2629 * their status appropriately.
2631 for (i = 0; i < nspares; i++) {
2632 VERIFY(nvlist_lookup_uint64(spares[i],
2633 ZPOOL_CONFIG_GUID, &guid) == 0);
2634 if (spa_spare_exists(guid, &pool, NULL) &&
2636 VERIFY(nvlist_lookup_uint64_array(
2637 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2638 (uint64_t **)&vs, &vsc) == 0);
2639 vs->vs_state = VDEV_STATE_CANT_OPEN;
2640 vs->vs_aux = VDEV_AUX_SPARED;
2647 * Add l2cache device information to the nvlist, including vdev stats.
2650 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2653 uint_t i, j, nl2cache;
2660 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2662 if (spa->spa_l2cache.sav_count == 0)
2665 VERIFY(nvlist_lookup_nvlist(config,
2666 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2667 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2668 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2669 if (nl2cache != 0) {
2670 VERIFY(nvlist_add_nvlist_array(nvroot,
2671 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2672 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2673 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2676 * Update level 2 cache device stats.
2679 for (i = 0; i < nl2cache; i++) {
2680 VERIFY(nvlist_lookup_uint64(l2cache[i],
2681 ZPOOL_CONFIG_GUID, &guid) == 0);
2684 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2686 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2687 vd = spa->spa_l2cache.sav_vdevs[j];
2693 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2694 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2696 vdev_get_stats(vd, vs);
2702 spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2708 error = spa_open_common(name, &spa, FTAG, NULL, config);
2712 * This still leaves a window of inconsistency where the spares
2713 * or l2cache devices could change and the config would be
2714 * self-inconsistent.
2716 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2718 if (*config != NULL) {
2719 uint64_t loadtimes[2];
2721 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
2722 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
2723 VERIFY(nvlist_add_uint64_array(*config,
2724 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
2726 VERIFY(nvlist_add_uint64(*config,
2727 ZPOOL_CONFIG_ERRCOUNT,
2728 spa_get_errlog_size(spa)) == 0);
2730 if (spa_suspended(spa))
2731 VERIFY(nvlist_add_uint64(*config,
2732 ZPOOL_CONFIG_SUSPENDED,
2733 spa->spa_failmode) == 0);
2735 spa_add_spares(spa, *config);
2736 spa_add_l2cache(spa, *config);
2741 * We want to get the alternate root even for faulted pools, so we cheat
2742 * and call spa_lookup() directly.
2746 mutex_enter(&spa_namespace_lock);
2747 spa = spa_lookup(name);
2749 spa_altroot(spa, altroot, buflen);
2753 mutex_exit(&spa_namespace_lock);
2755 spa_altroot(spa, altroot, buflen);
2760 spa_config_exit(spa, SCL_CONFIG, FTAG);
2761 spa_close(spa, FTAG);
2768 * Validate that the auxiliary device array is well formed. We must have an
2769 * array of nvlists, each which describes a valid leaf vdev. If this is an
2770 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2771 * specified, as long as they are well-formed.
2774 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
2775 spa_aux_vdev_t *sav, const char *config, uint64_t version,
2776 vdev_labeltype_t label)
2783 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2786 * It's acceptable to have no devs specified.
2788 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
2795 * Make sure the pool is formatted with a version that supports this
2798 if (spa_version(spa) < version)
2802 * Set the pending device list so we correctly handle device in-use
2805 sav->sav_pending = dev;
2806 sav->sav_npending = ndev;
2808 for (i = 0; i < ndev; i++) {
2809 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
2813 if (!vd->vdev_ops->vdev_op_leaf) {
2820 * The L2ARC currently only supports disk devices in
2821 * kernel context. For user-level testing, we allow it.
2824 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
2825 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
2833 if ((error = vdev_open(vd)) == 0 &&
2834 (error = vdev_label_init(vd, crtxg, label)) == 0) {
2835 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
2836 vd->vdev_guid) == 0);
2842 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
2849 sav->sav_pending = NULL;
2850 sav->sav_npending = 0;
2855 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
2859 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2861 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2862 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
2863 VDEV_LABEL_SPARE)) != 0) {
2867 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2868 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
2869 VDEV_LABEL_L2CACHE));
2873 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
2878 if (sav->sav_config != NULL) {
2884 * Generate new dev list by concatentating with the
2887 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
2888 &olddevs, &oldndevs) == 0);
2890 newdevs = kmem_alloc(sizeof (void *) *
2891 (ndevs + oldndevs), KM_PUSHPAGE);
2892 for (i = 0; i < oldndevs; i++)
2893 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
2895 for (i = 0; i < ndevs; i++)
2896 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
2899 VERIFY(nvlist_remove(sav->sav_config, config,
2900 DATA_TYPE_NVLIST_ARRAY) == 0);
2902 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2903 config, newdevs, ndevs + oldndevs) == 0);
2904 for (i = 0; i < oldndevs + ndevs; i++)
2905 nvlist_free(newdevs[i]);
2906 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
2909 * Generate a new dev list.
2911 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
2913 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
2919 * Stop and drop level 2 ARC devices
2922 spa_l2cache_drop(spa_t *spa)
2926 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2928 for (i = 0; i < sav->sav_count; i++) {
2931 vd = sav->sav_vdevs[i];
2934 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2935 pool != 0ULL && l2arc_vdev_present(vd))
2936 l2arc_remove_vdev(vd);
2944 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
2945 const char *history_str, nvlist_t *zplprops)
2948 char *altroot = NULL;
2953 uint64_t txg = TXG_INITIAL;
2954 nvlist_t **spares, **l2cache;
2955 uint_t nspares, nl2cache;
2956 uint64_t version, obj;
2960 * If this pool already exists, return failure.
2962 mutex_enter(&spa_namespace_lock);
2963 if (spa_lookup(pool) != NULL) {
2964 mutex_exit(&spa_namespace_lock);
2969 * Allocate a new spa_t structure.
2971 (void) nvlist_lookup_string(props,
2972 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2973 spa = spa_add(pool, NULL, altroot);
2974 spa_activate(spa, spa_mode_global);
2976 if (props && (error = spa_prop_validate(spa, props))) {
2977 spa_deactivate(spa);
2979 mutex_exit(&spa_namespace_lock);
2983 if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
2985 version = SPA_VERSION;
2986 ASSERT(version <= SPA_VERSION);
2988 spa->spa_first_txg = txg;
2989 spa->spa_uberblock.ub_txg = txg - 1;
2990 spa->spa_uberblock.ub_version = version;
2991 spa->spa_ubsync = spa->spa_uberblock;
2994 * Create "The Godfather" zio to hold all async IOs
2996 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2997 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3000 * Create the root vdev.
3002 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3004 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3006 ASSERT(error != 0 || rvd != NULL);
3007 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3009 if (error == 0 && !zfs_allocatable_devs(nvroot))
3013 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3014 (error = spa_validate_aux(spa, nvroot, txg,
3015 VDEV_ALLOC_ADD)) == 0) {
3016 for (c = 0; c < rvd->vdev_children; c++) {
3017 vdev_metaslab_set_size(rvd->vdev_child[c]);
3018 vdev_expand(rvd->vdev_child[c], txg);
3022 spa_config_exit(spa, SCL_ALL, FTAG);
3026 spa_deactivate(spa);
3028 mutex_exit(&spa_namespace_lock);
3033 * Get the list of spares, if specified.
3035 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3036 &spares, &nspares) == 0) {
3037 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3039 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3040 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3041 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3042 spa_load_spares(spa);
3043 spa_config_exit(spa, SCL_ALL, FTAG);
3044 spa->spa_spares.sav_sync = B_TRUE;
3048 * Get the list of level 2 cache devices, if specified.
3050 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3051 &l2cache, &nl2cache) == 0) {
3052 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3053 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3054 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3055 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3056 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3057 spa_load_l2cache(spa);
3058 spa_config_exit(spa, SCL_ALL, FTAG);
3059 spa->spa_l2cache.sav_sync = B_TRUE;
3062 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3063 spa->spa_meta_objset = dp->dp_meta_objset;
3066 * Create DDTs (dedup tables).
3070 spa_update_dspace(spa);
3072 tx = dmu_tx_create_assigned(dp, txg);
3075 * Create the pool config object.
3077 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3078 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3079 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3081 if (zap_add(spa->spa_meta_objset,
3082 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3083 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3084 cmn_err(CE_PANIC, "failed to add pool config");
3087 if (zap_add(spa->spa_meta_objset,
3088 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3089 sizeof (uint64_t), 1, &version, tx) != 0) {
3090 cmn_err(CE_PANIC, "failed to add pool version");
3093 /* Newly created pools with the right version are always deflated. */
3094 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3095 spa->spa_deflate = TRUE;
3096 if (zap_add(spa->spa_meta_objset,
3097 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3098 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3099 cmn_err(CE_PANIC, "failed to add deflate");
3104 * Create the deferred-free bpobj. Turn off compression
3105 * because sync-to-convergence takes longer if the blocksize
3108 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3109 dmu_object_set_compress(spa->spa_meta_objset, obj,
3110 ZIO_COMPRESS_OFF, tx);
3111 if (zap_add(spa->spa_meta_objset,
3112 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3113 sizeof (uint64_t), 1, &obj, tx) != 0) {
3114 cmn_err(CE_PANIC, "failed to add bpobj");
3116 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3117 spa->spa_meta_objset, obj));
3120 * Create the pool's history object.
3122 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3123 spa_history_create_obj(spa, tx);
3126 * Set pool properties.
3128 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3129 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3130 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3131 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3133 if (props != NULL) {
3134 spa_configfile_set(spa, props, B_FALSE);
3135 spa_sync_props(spa, props, tx);
3140 spa->spa_sync_on = B_TRUE;
3141 txg_sync_start(spa->spa_dsl_pool);
3144 * We explicitly wait for the first transaction to complete so that our
3145 * bean counters are appropriately updated.
3147 txg_wait_synced(spa->spa_dsl_pool, txg);
3149 spa_config_sync(spa, B_FALSE, B_TRUE);
3151 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3152 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3153 spa_history_log_version(spa, LOG_POOL_CREATE);
3155 spa->spa_minref = refcount_count(&spa->spa_refcount);
3157 mutex_exit(&spa_namespace_lock);
3164 * Get the root pool information from the root disk, then import the root pool
3165 * during the system boot up time.
3167 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3170 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3173 nvlist_t *nvtop, *nvroot;
3176 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3180 * Add this top-level vdev to the child array.
3182 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3184 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3186 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3189 * Put this pool's top-level vdevs into a root vdev.
3191 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3192 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3193 VDEV_TYPE_ROOT) == 0);
3194 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3195 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3196 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3200 * Replace the existing vdev_tree with the new root vdev in
3201 * this pool's configuration (remove the old, add the new).
3203 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3204 nvlist_free(nvroot);
3209 * Walk the vdev tree and see if we can find a device with "better"
3210 * configuration. A configuration is "better" if the label on that
3211 * device has a more recent txg.
3214 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3218 for (c = 0; c < vd->vdev_children; c++)
3219 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3221 if (vd->vdev_ops->vdev_op_leaf) {
3225 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3229 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3233 * Do we have a better boot device?
3235 if (label_txg > *txg) {
3244 * Import a root pool.
3246 * For x86. devpath_list will consist of devid and/or physpath name of
3247 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3248 * The GRUB "findroot" command will return the vdev we should boot.
3250 * For Sparc, devpath_list consists the physpath name of the booting device
3251 * no matter the rootpool is a single device pool or a mirrored pool.
3253 * "/pci@1f,0/ide@d/disk@0,0:a"
3256 spa_import_rootpool(char *devpath, char *devid)
3259 vdev_t *rvd, *bvd, *avd = NULL;
3260 nvlist_t *config, *nvtop;
3266 * Read the label from the boot device and generate a configuration.
3268 config = spa_generate_rootconf(devpath, devid, &guid);
3269 #if defined(_OBP) && defined(_KERNEL)
3270 if (config == NULL) {
3271 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3273 get_iscsi_bootpath_phy(devpath);
3274 config = spa_generate_rootconf(devpath, devid, &guid);
3278 if (config == NULL) {
3279 cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
3284 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3286 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3288 mutex_enter(&spa_namespace_lock);
3289 if ((spa = spa_lookup(pname)) != NULL) {
3291 * Remove the existing root pool from the namespace so that we
3292 * can replace it with the correct config we just read in.
3297 spa = spa_add(pname, config, NULL);
3298 spa->spa_is_root = B_TRUE;
3299 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3302 * Build up a vdev tree based on the boot device's label config.
3304 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3306 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3307 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3308 VDEV_ALLOC_ROOTPOOL);
3309 spa_config_exit(spa, SCL_ALL, FTAG);
3311 mutex_exit(&spa_namespace_lock);
3312 nvlist_free(config);
3313 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3319 * Get the boot vdev.
3321 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3322 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3323 (u_longlong_t)guid);
3329 * Determine if there is a better boot device.
3332 spa_alt_rootvdev(rvd, &avd, &txg);
3334 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3335 "try booting from '%s'", avd->vdev_path);
3341 * If the boot device is part of a spare vdev then ensure that
3342 * we're booting off the active spare.
3344 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3345 !bvd->vdev_isspare) {
3346 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3347 "try booting from '%s'",
3349 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3355 spa_history_log_version(spa, LOG_POOL_IMPORT);
3357 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3359 spa_config_exit(spa, SCL_ALL, FTAG);
3360 mutex_exit(&spa_namespace_lock);
3362 nvlist_free(config);
3369 * Import a non-root pool into the system.
3372 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3375 char *altroot = NULL;
3376 spa_load_state_t state = SPA_LOAD_IMPORT;
3377 zpool_rewind_policy_t policy;
3378 uint64_t mode = spa_mode_global;
3379 uint64_t readonly = B_FALSE;
3382 nvlist_t **spares, **l2cache;
3383 uint_t nspares, nl2cache;
3386 * If a pool with this name exists, return failure.
3388 mutex_enter(&spa_namespace_lock);
3389 if (spa_lookup(pool) != NULL) {
3390 mutex_exit(&spa_namespace_lock);
3395 * Create and initialize the spa structure.
3397 (void) nvlist_lookup_string(props,
3398 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3399 (void) nvlist_lookup_uint64(props,
3400 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3403 spa = spa_add(pool, config, altroot);
3404 spa->spa_import_flags = flags;
3407 * Verbatim import - Take a pool and insert it into the namespace
3408 * as if it had been loaded at boot.
3410 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3412 spa_configfile_set(spa, props, B_FALSE);
3414 spa_config_sync(spa, B_FALSE, B_TRUE);
3416 mutex_exit(&spa_namespace_lock);
3417 spa_history_log_version(spa, LOG_POOL_IMPORT);
3422 spa_activate(spa, mode);
3425 * Don't start async tasks until we know everything is healthy.
3427 spa_async_suspend(spa);
3429 zpool_get_rewind_policy(config, &policy);
3430 if (policy.zrp_request & ZPOOL_DO_REWIND)
3431 state = SPA_LOAD_RECOVER;
3434 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3435 * because the user-supplied config is actually the one to trust when
3438 if (state != SPA_LOAD_RECOVER)
3439 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3441 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3442 policy.zrp_request);
3445 * Propagate anything learned while loading the pool and pass it
3446 * back to caller (i.e. rewind info, missing devices, etc).
3448 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3449 spa->spa_load_info) == 0);
3451 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3453 * Toss any existing sparelist, as it doesn't have any validity
3454 * anymore, and conflicts with spa_has_spare().
3456 if (spa->spa_spares.sav_config) {
3457 nvlist_free(spa->spa_spares.sav_config);
3458 spa->spa_spares.sav_config = NULL;
3459 spa_load_spares(spa);
3461 if (spa->spa_l2cache.sav_config) {
3462 nvlist_free(spa->spa_l2cache.sav_config);
3463 spa->spa_l2cache.sav_config = NULL;
3464 spa_load_l2cache(spa);
3467 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3470 error = spa_validate_aux(spa, nvroot, -1ULL,
3473 error = spa_validate_aux(spa, nvroot, -1ULL,
3474 VDEV_ALLOC_L2CACHE);
3475 spa_config_exit(spa, SCL_ALL, FTAG);
3478 spa_configfile_set(spa, props, B_FALSE);
3480 if (error != 0 || (props && spa_writeable(spa) &&
3481 (error = spa_prop_set(spa, props)))) {
3483 spa_deactivate(spa);
3485 mutex_exit(&spa_namespace_lock);
3489 spa_async_resume(spa);
3492 * Override any spares and level 2 cache devices as specified by
3493 * the user, as these may have correct device names/devids, etc.
3495 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3496 &spares, &nspares) == 0) {
3497 if (spa->spa_spares.sav_config)
3498 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3499 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3501 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3502 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3503 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3504 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3505 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3506 spa_load_spares(spa);
3507 spa_config_exit(spa, SCL_ALL, FTAG);
3508 spa->spa_spares.sav_sync = B_TRUE;
3510 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3511 &l2cache, &nl2cache) == 0) {
3512 if (spa->spa_l2cache.sav_config)
3513 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3514 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3516 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3517 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3518 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3519 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3520 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3521 spa_load_l2cache(spa);
3522 spa_config_exit(spa, SCL_ALL, FTAG);
3523 spa->spa_l2cache.sav_sync = B_TRUE;
3527 * Check for any removed devices.
3529 if (spa->spa_autoreplace) {
3530 spa_aux_check_removed(&spa->spa_spares);
3531 spa_aux_check_removed(&spa->spa_l2cache);
3534 if (spa_writeable(spa)) {
3536 * Update the config cache to include the newly-imported pool.
3538 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3542 * It's possible that the pool was expanded while it was exported.
3543 * We kick off an async task to handle this for us.
3545 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3547 mutex_exit(&spa_namespace_lock);
3548 spa_history_log_version(spa, LOG_POOL_IMPORT);
3554 spa_tryimport(nvlist_t *tryconfig)
3556 nvlist_t *config = NULL;
3562 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3565 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3569 * Create and initialize the spa structure.
3571 mutex_enter(&spa_namespace_lock);
3572 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3573 spa_activate(spa, FREAD);
3576 * Pass off the heavy lifting to spa_load().
3577 * Pass TRUE for mosconfig because the user-supplied config
3578 * is actually the one to trust when doing an import.
3580 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3583 * If 'tryconfig' was at least parsable, return the current config.
3585 if (spa->spa_root_vdev != NULL) {
3586 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3587 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3589 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3591 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3592 spa->spa_uberblock.ub_timestamp) == 0);
3595 * If the bootfs property exists on this pool then we
3596 * copy it out so that external consumers can tell which
3597 * pools are bootable.
3599 if ((!error || error == EEXIST) && spa->spa_bootfs) {
3600 char *tmpname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
3603 * We have to play games with the name since the
3604 * pool was opened as TRYIMPORT_NAME.
3606 if (dsl_dsobj_to_dsname(spa_name(spa),
3607 spa->spa_bootfs, tmpname) == 0) {
3609 char *dsname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
3611 cp = strchr(tmpname, '/');
3613 (void) strlcpy(dsname, tmpname,
3616 (void) snprintf(dsname, MAXPATHLEN,
3617 "%s/%s", poolname, ++cp);
3619 VERIFY(nvlist_add_string(config,
3620 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3621 kmem_free(dsname, MAXPATHLEN);
3623 kmem_free(tmpname, MAXPATHLEN);
3627 * Add the list of hot spares and level 2 cache devices.
3629 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3630 spa_add_spares(spa, config);
3631 spa_add_l2cache(spa, config);
3632 spa_config_exit(spa, SCL_CONFIG, FTAG);
3636 spa_deactivate(spa);
3638 mutex_exit(&spa_namespace_lock);
3644 * Pool export/destroy
3646 * The act of destroying or exporting a pool is very simple. We make sure there
3647 * is no more pending I/O and any references to the pool are gone. Then, we
3648 * update the pool state and sync all the labels to disk, removing the
3649 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3650 * we don't sync the labels or remove the configuration cache.
3653 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
3654 boolean_t force, boolean_t hardforce)
3661 if (!(spa_mode_global & FWRITE))
3664 mutex_enter(&spa_namespace_lock);
3665 if ((spa = spa_lookup(pool)) == NULL) {
3666 mutex_exit(&spa_namespace_lock);
3671 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3672 * reacquire the namespace lock, and see if we can export.
3674 spa_open_ref(spa, FTAG);
3675 mutex_exit(&spa_namespace_lock);
3676 spa_async_suspend(spa);
3677 mutex_enter(&spa_namespace_lock);
3678 spa_close(spa, FTAG);
3681 * The pool will be in core if it's openable,
3682 * in which case we can modify its state.
3684 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3686 * Objsets may be open only because they're dirty, so we
3687 * have to force it to sync before checking spa_refcnt.
3689 txg_wait_synced(spa->spa_dsl_pool, 0);
3692 * A pool cannot be exported or destroyed if there are active
3693 * references. If we are resetting a pool, allow references by
3694 * fault injection handlers.
3696 if (!spa_refcount_zero(spa) ||
3697 (spa->spa_inject_ref != 0 &&
3698 new_state != POOL_STATE_UNINITIALIZED)) {
3699 spa_async_resume(spa);
3700 mutex_exit(&spa_namespace_lock);
3705 * A pool cannot be exported if it has an active shared spare.
3706 * This is to prevent other pools stealing the active spare
3707 * from an exported pool. At user's own will, such pool can
3708 * be forcedly exported.
3710 if (!force && new_state == POOL_STATE_EXPORTED &&
3711 spa_has_active_shared_spare(spa)) {
3712 spa_async_resume(spa);
3713 mutex_exit(&spa_namespace_lock);
3718 * We want this to be reflected on every label,
3719 * so mark them all dirty. spa_unload() will do the
3720 * final sync that pushes these changes out.
3722 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
3723 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3724 spa->spa_state = new_state;
3725 spa->spa_final_txg = spa_last_synced_txg(spa) +
3727 vdev_config_dirty(spa->spa_root_vdev);
3728 spa_config_exit(spa, SCL_ALL, FTAG);
3732 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_DESTROY);
3734 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3736 spa_deactivate(spa);
3739 if (oldconfig && spa->spa_config)
3740 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
3742 if (new_state != POOL_STATE_UNINITIALIZED) {
3744 spa_config_sync(spa, B_TRUE, B_TRUE);
3747 mutex_exit(&spa_namespace_lock);
3753 * Destroy a storage pool.
3756 spa_destroy(char *pool)
3758 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
3763 * Export a storage pool.
3766 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
3767 boolean_t hardforce)
3769 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
3774 * Similar to spa_export(), this unloads the spa_t without actually removing it
3775 * from the namespace in any way.
3778 spa_reset(char *pool)
3780 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
3785 * ==========================================================================
3786 * Device manipulation
3787 * ==========================================================================
3791 * Add a device to a storage pool.
3794 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3798 vdev_t *rvd = spa->spa_root_vdev;
3800 nvlist_t **spares, **l2cache;
3801 uint_t nspares, nl2cache;
3804 ASSERT(spa_writeable(spa));
3806 txg = spa_vdev_enter(spa);
3808 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
3809 VDEV_ALLOC_ADD)) != 0)
3810 return (spa_vdev_exit(spa, NULL, txg, error));
3812 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
3814 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
3818 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
3822 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
3823 return (spa_vdev_exit(spa, vd, txg, EINVAL));
3825 if (vd->vdev_children != 0 &&
3826 (error = vdev_create(vd, txg, B_FALSE)) != 0)
3827 return (spa_vdev_exit(spa, vd, txg, error));
3830 * We must validate the spares and l2cache devices after checking the
3831 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
3833 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
3834 return (spa_vdev_exit(spa, vd, txg, error));
3837 * Transfer each new top-level vdev from vd to rvd.
3839 for (c = 0; c < vd->vdev_children; c++) {
3842 * Set the vdev id to the first hole, if one exists.
3844 for (id = 0; id < rvd->vdev_children; id++) {
3845 if (rvd->vdev_child[id]->vdev_ishole) {
3846 vdev_free(rvd->vdev_child[id]);
3850 tvd = vd->vdev_child[c];
3851 vdev_remove_child(vd, tvd);
3853 vdev_add_child(rvd, tvd);
3854 vdev_config_dirty(tvd);
3858 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
3859 ZPOOL_CONFIG_SPARES);
3860 spa_load_spares(spa);
3861 spa->spa_spares.sav_sync = B_TRUE;
3864 if (nl2cache != 0) {
3865 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
3866 ZPOOL_CONFIG_L2CACHE);
3867 spa_load_l2cache(spa);
3868 spa->spa_l2cache.sav_sync = B_TRUE;
3872 * We have to be careful when adding new vdevs to an existing pool.
3873 * If other threads start allocating from these vdevs before we
3874 * sync the config cache, and we lose power, then upon reboot we may
3875 * fail to open the pool because there are DVAs that the config cache
3876 * can't translate. Therefore, we first add the vdevs without
3877 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
3878 * and then let spa_config_update() initialize the new metaslabs.
3880 * spa_load() checks for added-but-not-initialized vdevs, so that
3881 * if we lose power at any point in this sequence, the remaining
3882 * steps will be completed the next time we load the pool.
3884 (void) spa_vdev_exit(spa, vd, txg, 0);
3886 mutex_enter(&spa_namespace_lock);
3887 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3888 mutex_exit(&spa_namespace_lock);
3894 * Attach a device to a mirror. The arguments are the path to any device
3895 * in the mirror, and the nvroot for the new device. If the path specifies
3896 * a device that is not mirrored, we automatically insert the mirror vdev.
3898 * If 'replacing' is specified, the new device is intended to replace the
3899 * existing device; in this case the two devices are made into their own
3900 * mirror using the 'replacing' vdev, which is functionally identical to
3901 * the mirror vdev (it actually reuses all the same ops) but has a few
3902 * extra rules: you can't attach to it after it's been created, and upon
3903 * completion of resilvering, the first disk (the one being replaced)
3904 * is automatically detached.
3907 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
3909 uint64_t txg, dtl_max_txg;
3910 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
3911 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
3913 char *oldvdpath, *newvdpath;
3917 ASSERT(spa_writeable(spa));
3919 txg = spa_vdev_enter(spa);
3921 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
3924 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3926 if (!oldvd->vdev_ops->vdev_op_leaf)
3927 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3929 pvd = oldvd->vdev_parent;
3931 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
3932 VDEV_ALLOC_ATTACH)) != 0)
3933 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
3935 if (newrootvd->vdev_children != 1)
3936 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3938 newvd = newrootvd->vdev_child[0];
3940 if (!newvd->vdev_ops->vdev_op_leaf)
3941 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
3943 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
3944 return (spa_vdev_exit(spa, newrootvd, txg, error));
3947 * Spares can't replace logs
3949 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
3950 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3954 * For attach, the only allowable parent is a mirror or the root
3957 if (pvd->vdev_ops != &vdev_mirror_ops &&
3958 pvd->vdev_ops != &vdev_root_ops)
3959 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3961 pvops = &vdev_mirror_ops;
3964 * Active hot spares can only be replaced by inactive hot
3967 if (pvd->vdev_ops == &vdev_spare_ops &&
3968 oldvd->vdev_isspare &&
3969 !spa_has_spare(spa, newvd->vdev_guid))
3970 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3973 * If the source is a hot spare, and the parent isn't already a
3974 * spare, then we want to create a new hot spare. Otherwise, we
3975 * want to create a replacing vdev. The user is not allowed to
3976 * attach to a spared vdev child unless the 'isspare' state is
3977 * the same (spare replaces spare, non-spare replaces
3980 if (pvd->vdev_ops == &vdev_replacing_ops &&
3981 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
3982 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3983 } else if (pvd->vdev_ops == &vdev_spare_ops &&
3984 newvd->vdev_isspare != oldvd->vdev_isspare) {
3985 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
3988 if (newvd->vdev_isspare)
3989 pvops = &vdev_spare_ops;
3991 pvops = &vdev_replacing_ops;
3995 * Make sure the new device is big enough.
3997 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
3998 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4001 * The new device cannot have a higher alignment requirement
4002 * than the top-level vdev.
4004 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4005 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4008 * If this is an in-place replacement, update oldvd's path and devid
4009 * to make it distinguishable from newvd, and unopenable from now on.
4011 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4012 spa_strfree(oldvd->vdev_path);
4013 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4015 (void) sprintf(oldvd->vdev_path, "%s/%s",
4016 newvd->vdev_path, "old");
4017 if (oldvd->vdev_devid != NULL) {
4018 spa_strfree(oldvd->vdev_devid);
4019 oldvd->vdev_devid = NULL;
4023 /* mark the device being resilvered */
4024 newvd->vdev_resilvering = B_TRUE;
4027 * If the parent is not a mirror, or if we're replacing, insert the new
4028 * mirror/replacing/spare vdev above oldvd.
4030 if (pvd->vdev_ops != pvops)
4031 pvd = vdev_add_parent(oldvd, pvops);
4033 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4034 ASSERT(pvd->vdev_ops == pvops);
4035 ASSERT(oldvd->vdev_parent == pvd);
4038 * Extract the new device from its root and add it to pvd.
4040 vdev_remove_child(newrootvd, newvd);
4041 newvd->vdev_id = pvd->vdev_children;
4042 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4043 vdev_add_child(pvd, newvd);
4045 tvd = newvd->vdev_top;
4046 ASSERT(pvd->vdev_top == tvd);
4047 ASSERT(tvd->vdev_parent == rvd);
4049 vdev_config_dirty(tvd);
4052 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4053 * for any dmu_sync-ed blocks. It will propagate upward when
4054 * spa_vdev_exit() calls vdev_dtl_reassess().
4056 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4058 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4059 dtl_max_txg - TXG_INITIAL);
4061 if (newvd->vdev_isspare) {
4062 spa_spare_activate(newvd);
4063 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_DEVICE_SPARE);
4066 oldvdpath = spa_strdup(oldvd->vdev_path);
4067 newvdpath = spa_strdup(newvd->vdev_path);
4068 newvd_isspare = newvd->vdev_isspare;
4071 * Mark newvd's DTL dirty in this txg.
4073 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4076 * Restart the resilver
4078 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4083 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4085 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
4086 "%s vdev=%s %s vdev=%s",
4087 replacing && newvd_isspare ? "spare in" :
4088 replacing ? "replace" : "attach", newvdpath,
4089 replacing ? "for" : "to", oldvdpath);
4091 spa_strfree(oldvdpath);
4092 spa_strfree(newvdpath);
4094 if (spa->spa_bootfs)
4095 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH);
4101 * Detach a device from a mirror or replacing vdev.
4102 * If 'replace_done' is specified, only detach if the parent
4103 * is a replacing vdev.
4106 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4110 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
4111 vdev_t *vd, *pvd, *cvd, *tvd;
4112 boolean_t unspare = B_FALSE;
4113 uint64_t unspare_guid = 0;
4117 ASSERT(spa_writeable(spa));
4119 txg = spa_vdev_enter(spa);
4121 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4124 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4126 if (!vd->vdev_ops->vdev_op_leaf)
4127 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4129 pvd = vd->vdev_parent;
4132 * If the parent/child relationship is not as expected, don't do it.
4133 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4134 * vdev that's replacing B with C. The user's intent in replacing
4135 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4136 * the replace by detaching C, the expected behavior is to end up
4137 * M(A,B). But suppose that right after deciding to detach C,
4138 * the replacement of B completes. We would have M(A,C), and then
4139 * ask to detach C, which would leave us with just A -- not what
4140 * the user wanted. To prevent this, we make sure that the
4141 * parent/child relationship hasn't changed -- in this example,
4142 * that C's parent is still the replacing vdev R.
4144 if (pvd->vdev_guid != pguid && pguid != 0)
4145 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4148 * Only 'replacing' or 'spare' vdevs can be replaced.
4150 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4151 pvd->vdev_ops != &vdev_spare_ops)
4152 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4154 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4155 spa_version(spa) >= SPA_VERSION_SPARES);
4158 * Only mirror, replacing, and spare vdevs support detach.
4160 if (pvd->vdev_ops != &vdev_replacing_ops &&
4161 pvd->vdev_ops != &vdev_mirror_ops &&
4162 pvd->vdev_ops != &vdev_spare_ops)
4163 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4166 * If this device has the only valid copy of some data,
4167 * we cannot safely detach it.
4169 if (vdev_dtl_required(vd))
4170 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4172 ASSERT(pvd->vdev_children >= 2);
4175 * If we are detaching the second disk from a replacing vdev, then
4176 * check to see if we changed the original vdev's path to have "/old"
4177 * at the end in spa_vdev_attach(). If so, undo that change now.
4179 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4180 vd->vdev_path != NULL) {
4181 size_t len = strlen(vd->vdev_path);
4183 for (c = 0; c < pvd->vdev_children; c++) {
4184 cvd = pvd->vdev_child[c];
4186 if (cvd == vd || cvd->vdev_path == NULL)
4189 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4190 strcmp(cvd->vdev_path + len, "/old") == 0) {
4191 spa_strfree(cvd->vdev_path);
4192 cvd->vdev_path = spa_strdup(vd->vdev_path);
4199 * If we are detaching the original disk from a spare, then it implies
4200 * that the spare should become a real disk, and be removed from the
4201 * active spare list for the pool.
4203 if (pvd->vdev_ops == &vdev_spare_ops &&
4205 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4209 * Erase the disk labels so the disk can be used for other things.
4210 * This must be done after all other error cases are handled,
4211 * but before we disembowel vd (so we can still do I/O to it).
4212 * But if we can't do it, don't treat the error as fatal --
4213 * it may be that the unwritability of the disk is the reason
4214 * it's being detached!
4216 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4219 * Remove vd from its parent and compact the parent's children.
4221 vdev_remove_child(pvd, vd);
4222 vdev_compact_children(pvd);
4225 * Remember one of the remaining children so we can get tvd below.
4227 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4230 * If we need to remove the remaining child from the list of hot spares,
4231 * do it now, marking the vdev as no longer a spare in the process.
4232 * We must do this before vdev_remove_parent(), because that can
4233 * change the GUID if it creates a new toplevel GUID. For a similar
4234 * reason, we must remove the spare now, in the same txg as the detach;
4235 * otherwise someone could attach a new sibling, change the GUID, and
4236 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4239 ASSERT(cvd->vdev_isspare);
4240 spa_spare_remove(cvd);
4241 unspare_guid = cvd->vdev_guid;
4242 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4243 cvd->vdev_unspare = B_TRUE;
4247 * If the parent mirror/replacing vdev only has one child,
4248 * the parent is no longer needed. Remove it from the tree.
4250 if (pvd->vdev_children == 1) {
4251 if (pvd->vdev_ops == &vdev_spare_ops)
4252 cvd->vdev_unspare = B_FALSE;
4253 vdev_remove_parent(cvd);
4254 cvd->vdev_resilvering = B_FALSE;
4259 * We don't set tvd until now because the parent we just removed
4260 * may have been the previous top-level vdev.
4262 tvd = cvd->vdev_top;
4263 ASSERT(tvd->vdev_parent == rvd);
4266 * Reevaluate the parent vdev state.
4268 vdev_propagate_state(cvd);
4271 * If the 'autoexpand' property is set on the pool then automatically
4272 * try to expand the size of the pool. For example if the device we
4273 * just detached was smaller than the others, it may be possible to
4274 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4275 * first so that we can obtain the updated sizes of the leaf vdevs.
4277 if (spa->spa_autoexpand) {
4279 vdev_expand(tvd, txg);
4282 vdev_config_dirty(tvd);
4285 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4286 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4287 * But first make sure we're not on any *other* txg's DTL list, to
4288 * prevent vd from being accessed after it's freed.
4290 vdpath = spa_strdup(vd->vdev_path);
4291 for (t = 0; t < TXG_SIZE; t++)
4292 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4293 vd->vdev_detached = B_TRUE;
4294 vdev_dirty(tvd, VDD_DTL, vd, txg);
4296 spa_event_notify(spa, vd, FM_EREPORT_ZFS_DEVICE_REMOVE);
4298 /* hang on to the spa before we release the lock */
4299 spa_open_ref(spa, FTAG);
4301 error = spa_vdev_exit(spa, vd, txg, 0);
4303 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4305 spa_strfree(vdpath);
4308 * If this was the removal of the original device in a hot spare vdev,
4309 * then we want to go through and remove the device from the hot spare
4310 * list of every other pool.
4313 spa_t *altspa = NULL;
4315 mutex_enter(&spa_namespace_lock);
4316 while ((altspa = spa_next(altspa)) != NULL) {
4317 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4321 spa_open_ref(altspa, FTAG);
4322 mutex_exit(&spa_namespace_lock);
4323 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4324 mutex_enter(&spa_namespace_lock);
4325 spa_close(altspa, FTAG);
4327 mutex_exit(&spa_namespace_lock);
4329 /* search the rest of the vdevs for spares to remove */
4330 spa_vdev_resilver_done(spa);
4333 /* all done with the spa; OK to release */
4334 mutex_enter(&spa_namespace_lock);
4335 spa_close(spa, FTAG);
4336 mutex_exit(&spa_namespace_lock);
4342 * Split a set of devices from their mirrors, and create a new pool from them.
4345 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4346 nvlist_t *props, boolean_t exp)
4349 uint64_t txg, *glist;
4351 uint_t c, children, lastlog;
4352 nvlist_t **child, *nvl, *tmp;
4354 char *altroot = NULL;
4355 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4356 boolean_t activate_slog;
4358 ASSERT(spa_writeable(spa));
4360 txg = spa_vdev_enter(spa);
4362 /* clear the log and flush everything up to now */
4363 activate_slog = spa_passivate_log(spa);
4364 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4365 error = spa_offline_log(spa);
4366 txg = spa_vdev_config_enter(spa);
4369 spa_activate_log(spa);
4372 return (spa_vdev_exit(spa, NULL, txg, error));
4374 /* check new spa name before going any further */
4375 if (spa_lookup(newname) != NULL)
4376 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4379 * scan through all the children to ensure they're all mirrors
4381 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4382 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4384 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4386 /* first, check to ensure we've got the right child count */
4387 rvd = spa->spa_root_vdev;
4389 for (c = 0; c < rvd->vdev_children; c++) {
4390 vdev_t *vd = rvd->vdev_child[c];
4392 /* don't count the holes & logs as children */
4393 if (vd->vdev_islog || vd->vdev_ishole) {
4401 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4402 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4404 /* next, ensure no spare or cache devices are part of the split */
4405 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4406 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4407 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4409 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_PUSHPAGE);
4410 glist = kmem_zalloc(children * sizeof (uint64_t), KM_PUSHPAGE);
4412 /* then, loop over each vdev and validate it */
4413 for (c = 0; c < children; c++) {
4414 uint64_t is_hole = 0;
4416 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4420 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4421 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4429 /* which disk is going to be split? */
4430 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4436 /* look it up in the spa */
4437 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4438 if (vml[c] == NULL) {
4443 /* make sure there's nothing stopping the split */
4444 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4445 vml[c]->vdev_islog ||
4446 vml[c]->vdev_ishole ||
4447 vml[c]->vdev_isspare ||
4448 vml[c]->vdev_isl2cache ||
4449 !vdev_writeable(vml[c]) ||
4450 vml[c]->vdev_children != 0 ||
4451 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4452 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4457 if (vdev_dtl_required(vml[c])) {
4462 /* we need certain info from the top level */
4463 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4464 vml[c]->vdev_top->vdev_ms_array) == 0);
4465 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4466 vml[c]->vdev_top->vdev_ms_shift) == 0);
4467 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4468 vml[c]->vdev_top->vdev_asize) == 0);
4469 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4470 vml[c]->vdev_top->vdev_ashift) == 0);
4474 kmem_free(vml, children * sizeof (vdev_t *));
4475 kmem_free(glist, children * sizeof (uint64_t));
4476 return (spa_vdev_exit(spa, NULL, txg, error));
4479 /* stop writers from using the disks */
4480 for (c = 0; c < children; c++) {
4482 vml[c]->vdev_offline = B_TRUE;
4484 vdev_reopen(spa->spa_root_vdev);
4487 * Temporarily record the splitting vdevs in the spa config. This
4488 * will disappear once the config is regenerated.
4490 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
4491 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4492 glist, children) == 0);
4493 kmem_free(glist, children * sizeof (uint64_t));
4495 mutex_enter(&spa->spa_props_lock);
4496 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4498 mutex_exit(&spa->spa_props_lock);
4499 spa->spa_config_splitting = nvl;
4500 vdev_config_dirty(spa->spa_root_vdev);
4502 /* configure and create the new pool */
4503 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4504 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4505 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4506 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4507 spa_version(spa)) == 0);
4508 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4509 spa->spa_config_txg) == 0);
4510 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4511 spa_generate_guid(NULL)) == 0);
4512 (void) nvlist_lookup_string(props,
4513 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4515 /* add the new pool to the namespace */
4516 newspa = spa_add(newname, config, altroot);
4517 newspa->spa_config_txg = spa->spa_config_txg;
4518 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4520 /* release the spa config lock, retaining the namespace lock */
4521 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4523 if (zio_injection_enabled)
4524 zio_handle_panic_injection(spa, FTAG, 1);
4526 spa_activate(newspa, spa_mode_global);
4527 spa_async_suspend(newspa);
4529 /* create the new pool from the disks of the original pool */
4530 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4534 /* if that worked, generate a real config for the new pool */
4535 if (newspa->spa_root_vdev != NULL) {
4536 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4537 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
4538 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4539 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4540 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4545 if (props != NULL) {
4546 spa_configfile_set(newspa, props, B_FALSE);
4547 error = spa_prop_set(newspa, props);
4552 /* flush everything */
4553 txg = spa_vdev_config_enter(newspa);
4554 vdev_config_dirty(newspa->spa_root_vdev);
4555 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4557 if (zio_injection_enabled)
4558 zio_handle_panic_injection(spa, FTAG, 2);
4560 spa_async_resume(newspa);
4562 /* finally, update the original pool's config */
4563 txg = spa_vdev_config_enter(spa);
4564 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4565 error = dmu_tx_assign(tx, TXG_WAIT);
4568 for (c = 0; c < children; c++) {
4569 if (vml[c] != NULL) {
4572 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4578 vdev_config_dirty(spa->spa_root_vdev);
4579 spa->spa_config_splitting = NULL;
4583 (void) spa_vdev_exit(spa, NULL, txg, 0);
4585 if (zio_injection_enabled)
4586 zio_handle_panic_injection(spa, FTAG, 3);
4588 /* split is complete; log a history record */
4589 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
4590 "split new pool %s from pool %s", newname, spa_name(spa));
4592 kmem_free(vml, children * sizeof (vdev_t *));
4594 /* if we're not going to mount the filesystems in userland, export */
4596 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
4603 spa_deactivate(newspa);
4606 txg = spa_vdev_config_enter(spa);
4608 /* re-online all offlined disks */
4609 for (c = 0; c < children; c++) {
4611 vml[c]->vdev_offline = B_FALSE;
4613 vdev_reopen(spa->spa_root_vdev);
4615 nvlist_free(spa->spa_config_splitting);
4616 spa->spa_config_splitting = NULL;
4617 (void) spa_vdev_exit(spa, NULL, txg, error);
4619 kmem_free(vml, children * sizeof (vdev_t *));
4624 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
4628 for (i = 0; i < count; i++) {
4631 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
4634 if (guid == target_guid)
4642 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
4643 nvlist_t *dev_to_remove)
4645 nvlist_t **newdev = NULL;
4649 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_PUSHPAGE);
4651 for (i = 0, j = 0; i < count; i++) {
4652 if (dev[i] == dev_to_remove)
4654 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_PUSHPAGE) == 0);
4657 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
4658 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
4660 for (i = 0; i < count - 1; i++)
4661 nvlist_free(newdev[i]);
4664 kmem_free(newdev, (count - 1) * sizeof (void *));
4668 * Evacuate the device.
4671 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
4676 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4677 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4678 ASSERT(vd == vd->vdev_top);
4681 * Evacuate the device. We don't hold the config lock as writer
4682 * since we need to do I/O but we do keep the
4683 * spa_namespace_lock held. Once this completes the device
4684 * should no longer have any blocks allocated on it.
4686 if (vd->vdev_islog) {
4687 if (vd->vdev_stat.vs_alloc != 0)
4688 error = spa_offline_log(spa);
4697 * The evacuation succeeded. Remove any remaining MOS metadata
4698 * associated with this vdev, and wait for these changes to sync.
4700 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
4701 txg = spa_vdev_config_enter(spa);
4702 vd->vdev_removing = B_TRUE;
4703 vdev_dirty(vd, 0, NULL, txg);
4704 vdev_config_dirty(vd);
4705 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4711 * Complete the removal by cleaning up the namespace.
4714 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
4716 vdev_t *rvd = spa->spa_root_vdev;
4717 uint64_t id = vd->vdev_id;
4718 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
4720 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4721 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4722 ASSERT(vd == vd->vdev_top);
4725 * Only remove any devices which are empty.
4727 if (vd->vdev_stat.vs_alloc != 0)
4730 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4732 if (list_link_active(&vd->vdev_state_dirty_node))
4733 vdev_state_clean(vd);
4734 if (list_link_active(&vd->vdev_config_dirty_node))
4735 vdev_config_clean(vd);
4740 vdev_compact_children(rvd);
4742 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
4743 vdev_add_child(rvd, vd);
4745 vdev_config_dirty(rvd);
4748 * Reassess the health of our root vdev.
4754 * Remove a device from the pool -
4756 * Removing a device from the vdev namespace requires several steps
4757 * and can take a significant amount of time. As a result we use
4758 * the spa_vdev_config_[enter/exit] functions which allow us to
4759 * grab and release the spa_config_lock while still holding the namespace
4760 * lock. During each step the configuration is synced out.
4764 * Remove a device from the pool. Currently, this supports removing only hot
4765 * spares, slogs, and level 2 ARC devices.
4768 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
4771 metaslab_group_t *mg;
4772 nvlist_t **spares, **l2cache, *nv;
4774 uint_t nspares, nl2cache;
4776 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
4778 ASSERT(spa_writeable(spa));
4781 txg = spa_vdev_enter(spa);
4783 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4785 if (spa->spa_spares.sav_vdevs != NULL &&
4786 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
4787 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
4788 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
4790 * Only remove the hot spare if it's not currently in use
4793 if (vd == NULL || unspare) {
4794 spa_vdev_remove_aux(spa->spa_spares.sav_config,
4795 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
4796 spa_load_spares(spa);
4797 spa->spa_spares.sav_sync = B_TRUE;
4801 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
4802 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
4803 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
4804 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
4806 * Cache devices can always be removed.
4808 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
4809 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
4810 spa_load_l2cache(spa);
4811 spa->spa_l2cache.sav_sync = B_TRUE;
4812 } else if (vd != NULL && vd->vdev_islog) {
4814 ASSERT(vd == vd->vdev_top);
4817 * XXX - Once we have bp-rewrite this should
4818 * become the common case.
4824 * Stop allocating from this vdev.
4826 metaslab_group_passivate(mg);
4829 * Wait for the youngest allocations and frees to sync,
4830 * and then wait for the deferral of those frees to finish.
4832 spa_vdev_config_exit(spa, NULL,
4833 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
4836 * Attempt to evacuate the vdev.
4838 error = spa_vdev_remove_evacuate(spa, vd);
4840 txg = spa_vdev_config_enter(spa);
4843 * If we couldn't evacuate the vdev, unwind.
4846 metaslab_group_activate(mg);
4847 return (spa_vdev_exit(spa, NULL, txg, error));
4851 * Clean up the vdev namespace.
4853 spa_vdev_remove_from_namespace(spa, vd);
4855 } else if (vd != NULL) {
4857 * Normal vdevs cannot be removed (yet).
4862 * There is no vdev of any kind with the specified guid.
4868 return (spa_vdev_exit(spa, NULL, txg, error));
4874 * Find any device that's done replacing, or a vdev marked 'unspare' that's
4875 * current spared, so we can detach it.
4878 spa_vdev_resilver_done_hunt(vdev_t *vd)
4880 vdev_t *newvd, *oldvd;
4883 for (c = 0; c < vd->vdev_children; c++) {
4884 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
4890 * Check for a completed replacement. We always consider the first
4891 * vdev in the list to be the oldest vdev, and the last one to be
4892 * the newest (see spa_vdev_attach() for how that works). In
4893 * the case where the newest vdev is faulted, we will not automatically
4894 * remove it after a resilver completes. This is OK as it will require
4895 * user intervention to determine which disk the admin wishes to keep.
4897 if (vd->vdev_ops == &vdev_replacing_ops) {
4898 ASSERT(vd->vdev_children > 1);
4900 newvd = vd->vdev_child[vd->vdev_children - 1];
4901 oldvd = vd->vdev_child[0];
4903 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
4904 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4905 !vdev_dtl_required(oldvd))
4910 * Check for a completed resilver with the 'unspare' flag set.
4912 if (vd->vdev_ops == &vdev_spare_ops) {
4913 vdev_t *first = vd->vdev_child[0];
4914 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
4916 if (last->vdev_unspare) {
4919 } else if (first->vdev_unspare) {
4926 if (oldvd != NULL &&
4927 vdev_dtl_empty(newvd, DTL_MISSING) &&
4928 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4929 !vdev_dtl_required(oldvd))
4933 * If there are more than two spares attached to a disk,
4934 * and those spares are not required, then we want to
4935 * attempt to free them up now so that they can be used
4936 * by other pools. Once we're back down to a single
4937 * disk+spare, we stop removing them.
4939 if (vd->vdev_children > 2) {
4940 newvd = vd->vdev_child[1];
4942 if (newvd->vdev_isspare && last->vdev_isspare &&
4943 vdev_dtl_empty(last, DTL_MISSING) &&
4944 vdev_dtl_empty(last, DTL_OUTAGE) &&
4945 !vdev_dtl_required(newvd))
4954 spa_vdev_resilver_done(spa_t *spa)
4956 vdev_t *vd, *pvd, *ppvd;
4957 uint64_t guid, sguid, pguid, ppguid;
4959 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4961 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
4962 pvd = vd->vdev_parent;
4963 ppvd = pvd->vdev_parent;
4964 guid = vd->vdev_guid;
4965 pguid = pvd->vdev_guid;
4966 ppguid = ppvd->vdev_guid;
4969 * If we have just finished replacing a hot spared device, then
4970 * we need to detach the parent's first child (the original hot
4973 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
4974 ppvd->vdev_children == 2) {
4975 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
4976 sguid = ppvd->vdev_child[1]->vdev_guid;
4978 spa_config_exit(spa, SCL_ALL, FTAG);
4979 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
4981 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
4983 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4986 spa_config_exit(spa, SCL_ALL, FTAG);
4990 * Update the stored path or FRU for this vdev.
4993 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
4997 boolean_t sync = B_FALSE;
4999 ASSERT(spa_writeable(spa));
5001 spa_vdev_state_enter(spa, SCL_ALL);
5003 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5004 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5006 if (!vd->vdev_ops->vdev_op_leaf)
5007 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5010 if (strcmp(value, vd->vdev_path) != 0) {
5011 spa_strfree(vd->vdev_path);
5012 vd->vdev_path = spa_strdup(value);
5016 if (vd->vdev_fru == NULL) {
5017 vd->vdev_fru = spa_strdup(value);
5019 } else if (strcmp(value, vd->vdev_fru) != 0) {
5020 spa_strfree(vd->vdev_fru);
5021 vd->vdev_fru = spa_strdup(value);
5026 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5030 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5032 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5036 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5038 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5042 * ==========================================================================
5044 * ==========================================================================
5048 spa_scan_stop(spa_t *spa)
5050 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5051 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5053 return (dsl_scan_cancel(spa->spa_dsl_pool));
5057 spa_scan(spa_t *spa, pool_scan_func_t func)
5059 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5061 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5065 * If a resilver was requested, but there is no DTL on a
5066 * writeable leaf device, we have nothing to do.
5068 if (func == POOL_SCAN_RESILVER &&
5069 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5070 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5074 return (dsl_scan(spa->spa_dsl_pool, func));
5078 * ==========================================================================
5079 * SPA async task processing
5080 * ==========================================================================
5084 spa_async_remove(spa_t *spa, vdev_t *vd)
5088 if (vd->vdev_remove_wanted) {
5089 vd->vdev_remove_wanted = B_FALSE;
5090 vd->vdev_delayed_close = B_FALSE;
5091 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5094 * We want to clear the stats, but we don't want to do a full
5095 * vdev_clear() as that will cause us to throw away
5096 * degraded/faulted state as well as attempt to reopen the
5097 * device, all of which is a waste.
5099 vd->vdev_stat.vs_read_errors = 0;
5100 vd->vdev_stat.vs_write_errors = 0;
5101 vd->vdev_stat.vs_checksum_errors = 0;
5103 vdev_state_dirty(vd->vdev_top);
5106 for (c = 0; c < vd->vdev_children; c++)
5107 spa_async_remove(spa, vd->vdev_child[c]);
5111 spa_async_probe(spa_t *spa, vdev_t *vd)
5115 if (vd->vdev_probe_wanted) {
5116 vd->vdev_probe_wanted = B_FALSE;
5117 vdev_reopen(vd); /* vdev_open() does the actual probe */
5120 for (c = 0; c < vd->vdev_children; c++)
5121 spa_async_probe(spa, vd->vdev_child[c]);
5125 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5129 if (!spa->spa_autoexpand)
5132 for (c = 0; c < vd->vdev_children; c++) {
5133 vdev_t *cvd = vd->vdev_child[c];
5134 spa_async_autoexpand(spa, cvd);
5137 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5140 spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND);
5144 spa_async_thread(spa_t *spa)
5148 ASSERT(spa->spa_sync_on);
5150 mutex_enter(&spa->spa_async_lock);
5151 tasks = spa->spa_async_tasks;
5152 spa->spa_async_tasks = 0;
5153 mutex_exit(&spa->spa_async_lock);
5156 * See if the config needs to be updated.
5158 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5159 uint64_t old_space, new_space;
5161 mutex_enter(&spa_namespace_lock);
5162 old_space = metaslab_class_get_space(spa_normal_class(spa));
5163 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5164 new_space = metaslab_class_get_space(spa_normal_class(spa));
5165 mutex_exit(&spa_namespace_lock);
5168 * If the pool grew as a result of the config update,
5169 * then log an internal history event.
5171 if (new_space != old_space) {
5172 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5174 "pool '%s' size: %llu(+%llu)",
5175 spa_name(spa), new_space, new_space - old_space);
5180 * See if any devices need to be marked REMOVED.
5182 if (tasks & SPA_ASYNC_REMOVE) {
5183 spa_vdev_state_enter(spa, SCL_NONE);
5184 spa_async_remove(spa, spa->spa_root_vdev);
5185 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
5186 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5187 for (i = 0; i < spa->spa_spares.sav_count; i++)
5188 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5189 (void) spa_vdev_state_exit(spa, NULL, 0);
5192 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5193 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5194 spa_async_autoexpand(spa, spa->spa_root_vdev);
5195 spa_config_exit(spa, SCL_CONFIG, FTAG);
5199 * See if any devices need to be probed.
5201 if (tasks & SPA_ASYNC_PROBE) {
5202 spa_vdev_state_enter(spa, SCL_NONE);
5203 spa_async_probe(spa, spa->spa_root_vdev);
5204 (void) spa_vdev_state_exit(spa, NULL, 0);
5208 * If any devices are done replacing, detach them.
5210 if (tasks & SPA_ASYNC_RESILVER_DONE)
5211 spa_vdev_resilver_done(spa);
5214 * Kick off a resilver.
5216 if (tasks & SPA_ASYNC_RESILVER)
5217 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5220 * Let the world know that we're done.
5222 mutex_enter(&spa->spa_async_lock);
5223 spa->spa_async_thread = NULL;
5224 cv_broadcast(&spa->spa_async_cv);
5225 mutex_exit(&spa->spa_async_lock);
5230 spa_async_suspend(spa_t *spa)
5232 mutex_enter(&spa->spa_async_lock);
5233 spa->spa_async_suspended++;
5234 while (spa->spa_async_thread != NULL)
5235 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5236 mutex_exit(&spa->spa_async_lock);
5240 spa_async_resume(spa_t *spa)
5242 mutex_enter(&spa->spa_async_lock);
5243 ASSERT(spa->spa_async_suspended != 0);
5244 spa->spa_async_suspended--;
5245 mutex_exit(&spa->spa_async_lock);
5249 spa_async_dispatch(spa_t *spa)
5251 mutex_enter(&spa->spa_async_lock);
5252 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5253 spa->spa_async_thread == NULL &&
5254 rootdir != NULL && !vn_is_readonly(rootdir))
5255 spa->spa_async_thread = thread_create(NULL, 0,
5256 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5257 mutex_exit(&spa->spa_async_lock);
5261 spa_async_request(spa_t *spa, int task)
5263 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5264 mutex_enter(&spa->spa_async_lock);
5265 spa->spa_async_tasks |= task;
5266 mutex_exit(&spa->spa_async_lock);
5270 * ==========================================================================
5271 * SPA syncing routines
5272 * ==========================================================================
5276 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5279 bpobj_enqueue(bpo, bp, tx);
5284 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5288 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5294 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5296 char *packed = NULL;
5301 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5304 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5305 * information. This avoids the dbuf_will_dirty() path and
5306 * saves us a pre-read to get data we don't actually care about.
5308 bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
5309 packed = vmem_alloc(bufsize, KM_PUSHPAGE);
5311 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5313 bzero(packed + nvsize, bufsize - nvsize);
5315 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5317 vmem_free(packed, bufsize);
5319 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5320 dmu_buf_will_dirty(db, tx);
5321 *(uint64_t *)db->db_data = nvsize;
5322 dmu_buf_rele(db, FTAG);
5326 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5327 const char *config, const char *entry)
5337 * Update the MOS nvlist describing the list of available devices.
5338 * spa_validate_aux() will have already made sure this nvlist is
5339 * valid and the vdevs are labeled appropriately.
5341 if (sav->sav_object == 0) {
5342 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5343 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5344 sizeof (uint64_t), tx);
5345 VERIFY(zap_update(spa->spa_meta_objset,
5346 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5347 &sav->sav_object, tx) == 0);
5350 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
5351 if (sav->sav_count == 0) {
5352 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5354 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE);
5355 for (i = 0; i < sav->sav_count; i++)
5356 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5357 B_FALSE, VDEV_CONFIG_L2CACHE);
5358 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5359 sav->sav_count) == 0);
5360 for (i = 0; i < sav->sav_count; i++)
5361 nvlist_free(list[i]);
5362 kmem_free(list, sav->sav_count * sizeof (void *));
5365 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5366 nvlist_free(nvroot);
5368 sav->sav_sync = B_FALSE;
5372 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5376 if (list_is_empty(&spa->spa_config_dirty_list))
5379 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5381 config = spa_config_generate(spa, spa->spa_root_vdev,
5382 dmu_tx_get_txg(tx), B_FALSE);
5384 spa_config_exit(spa, SCL_STATE, FTAG);
5386 if (spa->spa_config_syncing)
5387 nvlist_free(spa->spa_config_syncing);
5388 spa->spa_config_syncing = config;
5390 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5394 * Set zpool properties.
5397 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5400 objset_t *mos = spa->spa_meta_objset;
5401 nvlist_t *nvp = arg2;
5406 const char *propname;
5407 zprop_type_t proptype;
5409 mutex_enter(&spa->spa_props_lock);
5412 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5413 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
5414 case ZPOOL_PROP_VERSION:
5416 * Only set version for non-zpool-creation cases
5417 * (set/import). spa_create() needs special care
5418 * for version setting.
5420 if (tx->tx_txg != TXG_INITIAL) {
5421 VERIFY(nvpair_value_uint64(elem,
5423 ASSERT(intval <= SPA_VERSION);
5424 ASSERT(intval >= spa_version(spa));
5425 spa->spa_uberblock.ub_version = intval;
5426 vdev_config_dirty(spa->spa_root_vdev);
5430 case ZPOOL_PROP_ALTROOT:
5432 * 'altroot' is a non-persistent property. It should
5433 * have been set temporarily at creation or import time.
5435 ASSERT(spa->spa_root != NULL);
5438 case ZPOOL_PROP_READONLY:
5439 case ZPOOL_PROP_CACHEFILE:
5441 * 'readonly' and 'cachefile' are also non-persisitent
5445 case ZPOOL_PROP_COMMENT:
5446 VERIFY(nvpair_value_string(elem, &strval) == 0);
5447 if (spa->spa_comment != NULL)
5448 spa_strfree(spa->spa_comment);
5449 spa->spa_comment = spa_strdup(strval);
5451 * We need to dirty the configuration on all the vdevs
5452 * so that their labels get updated. It's unnecessary
5453 * to do this for pool creation since the vdev's
5454 * configuratoin has already been dirtied.
5456 if (tx->tx_txg != TXG_INITIAL)
5457 vdev_config_dirty(spa->spa_root_vdev);
5461 * Set pool property values in the poolprops mos object.
5463 if (spa->spa_pool_props_object == 0) {
5464 VERIFY((spa->spa_pool_props_object =
5465 zap_create(mos, DMU_OT_POOL_PROPS,
5466 DMU_OT_NONE, 0, tx)) > 0);
5468 VERIFY(zap_update(mos,
5469 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5470 8, 1, &spa->spa_pool_props_object, tx)
5474 /* normalize the property name */
5475 propname = zpool_prop_to_name(prop);
5476 proptype = zpool_prop_get_type(prop);
5478 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5479 ASSERT(proptype == PROP_TYPE_STRING);
5480 VERIFY(nvpair_value_string(elem, &strval) == 0);
5481 VERIFY(zap_update(mos,
5482 spa->spa_pool_props_object, propname,
5483 1, strlen(strval) + 1, strval, tx) == 0);
5485 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5486 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5488 if (proptype == PROP_TYPE_INDEX) {
5490 VERIFY(zpool_prop_index_to_string(
5491 prop, intval, &unused) == 0);
5493 VERIFY(zap_update(mos,
5494 spa->spa_pool_props_object, propname,
5495 8, 1, &intval, tx) == 0);
5497 ASSERT(0); /* not allowed */
5501 case ZPOOL_PROP_DELEGATION:
5502 spa->spa_delegation = intval;
5504 case ZPOOL_PROP_BOOTFS:
5505 spa->spa_bootfs = intval;
5507 case ZPOOL_PROP_FAILUREMODE:
5508 spa->spa_failmode = intval;
5510 case ZPOOL_PROP_AUTOEXPAND:
5511 spa->spa_autoexpand = intval;
5512 if (tx->tx_txg != TXG_INITIAL)
5513 spa_async_request(spa,
5514 SPA_ASYNC_AUTOEXPAND);
5516 case ZPOOL_PROP_DEDUPDITTO:
5517 spa->spa_dedup_ditto = intval;
5524 /* log internal history if this is not a zpool create */
5525 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5526 tx->tx_txg != TXG_INITIAL) {
5527 spa_history_log_internal(LOG_POOL_PROPSET,
5528 spa, tx, "%s %lld %s",
5529 nvpair_name(elem), intval, spa_name(spa));
5533 mutex_exit(&spa->spa_props_lock);
5537 * Perform one-time upgrade on-disk changes. spa_version() does not
5538 * reflect the new version this txg, so there must be no changes this
5539 * txg to anything that the upgrade code depends on after it executes.
5540 * Therefore this must be called after dsl_pool_sync() does the sync
5544 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5546 dsl_pool_t *dp = spa->spa_dsl_pool;
5548 ASSERT(spa->spa_sync_pass == 1);
5550 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5551 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5552 dsl_pool_create_origin(dp, tx);
5554 /* Keeping the origin open increases spa_minref */
5555 spa->spa_minref += 3;
5558 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5559 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5560 dsl_pool_upgrade_clones(dp, tx);
5563 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5564 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5565 dsl_pool_upgrade_dir_clones(dp, tx);
5567 /* Keeping the freedir open increases spa_minref */
5568 spa->spa_minref += 3;
5573 * Sync the specified transaction group. New blocks may be dirtied as
5574 * part of the process, so we iterate until it converges.
5577 spa_sync(spa_t *spa, uint64_t txg)
5579 dsl_pool_t *dp = spa->spa_dsl_pool;
5580 objset_t *mos = spa->spa_meta_objset;
5581 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
5582 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
5583 vdev_t *rvd = spa->spa_root_vdev;
5589 VERIFY(spa_writeable(spa));
5592 * Lock out configuration changes.
5594 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5596 spa->spa_syncing_txg = txg;
5597 spa->spa_sync_pass = 0;
5600 * If there are any pending vdev state changes, convert them
5601 * into config changes that go out with this transaction group.
5603 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5604 while (list_head(&spa->spa_state_dirty_list) != NULL) {
5606 * We need the write lock here because, for aux vdevs,
5607 * calling vdev_config_dirty() modifies sav_config.
5608 * This is ugly and will become unnecessary when we
5609 * eliminate the aux vdev wart by integrating all vdevs
5610 * into the root vdev tree.
5612 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5613 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
5614 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
5615 vdev_state_clean(vd);
5616 vdev_config_dirty(vd);
5618 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5619 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
5621 spa_config_exit(spa, SCL_STATE, FTAG);
5623 tx = dmu_tx_create_assigned(dp, txg);
5626 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5627 * set spa_deflate if we have no raid-z vdevs.
5629 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
5630 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
5633 for (i = 0; i < rvd->vdev_children; i++) {
5634 vd = rvd->vdev_child[i];
5635 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
5638 if (i == rvd->vdev_children) {
5639 spa->spa_deflate = TRUE;
5640 VERIFY(0 == zap_add(spa->spa_meta_objset,
5641 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
5642 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
5647 * If anything has changed in this txg, or if someone is waiting
5648 * for this txg to sync (eg, spa_vdev_remove()), push the
5649 * deferred frees from the previous txg. If not, leave them
5650 * alone so that we don't generate work on an otherwise idle
5653 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
5654 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
5655 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
5656 ((dsl_scan_active(dp->dp_scan) ||
5657 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
5658 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5659 VERIFY3U(bpobj_iterate(defer_bpo,
5660 spa_free_sync_cb, zio, tx), ==, 0);
5661 VERIFY3U(zio_wait(zio), ==, 0);
5665 * Iterate to convergence.
5668 int pass = ++spa->spa_sync_pass;
5670 spa_sync_config_object(spa, tx);
5671 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
5672 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
5673 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
5674 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
5675 spa_errlog_sync(spa, txg);
5676 dsl_pool_sync(dp, txg);
5678 if (pass <= SYNC_PASS_DEFERRED_FREE) {
5679 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5680 bplist_iterate(free_bpl, spa_free_sync_cb,
5682 VERIFY(zio_wait(zio) == 0);
5684 bplist_iterate(free_bpl, bpobj_enqueue_cb,
5689 dsl_scan_sync(dp, tx);
5691 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)))
5695 spa_sync_upgrades(spa, tx);
5697 } while (dmu_objset_is_dirty(mos, txg));
5700 * Rewrite the vdev configuration (which includes the uberblock)
5701 * to commit the transaction group.
5703 * If there are no dirty vdevs, we sync the uberblock to a few
5704 * random top-level vdevs that are known to be visible in the
5705 * config cache (see spa_vdev_add() for a complete description).
5706 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5710 * We hold SCL_STATE to prevent vdev open/close/etc.
5711 * while we're attempting to write the vdev labels.
5713 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5715 if (list_is_empty(&spa->spa_config_dirty_list)) {
5716 vdev_t *svd[SPA_DVAS_PER_BP];
5718 int children = rvd->vdev_children;
5719 int c0 = spa_get_random(children);
5721 for (c = 0; c < children; c++) {
5722 vd = rvd->vdev_child[(c0 + c) % children];
5723 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
5725 svd[svdcount++] = vd;
5726 if (svdcount == SPA_DVAS_PER_BP)
5729 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
5731 error = vdev_config_sync(svd, svdcount, txg,
5734 error = vdev_config_sync(rvd->vdev_child,
5735 rvd->vdev_children, txg, B_FALSE);
5737 error = vdev_config_sync(rvd->vdev_child,
5738 rvd->vdev_children, txg, B_TRUE);
5741 spa_config_exit(spa, SCL_STATE, FTAG);
5745 zio_suspend(spa, NULL);
5746 zio_resume_wait(spa);
5751 * Clear the dirty config list.
5753 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
5754 vdev_config_clean(vd);
5757 * Now that the new config has synced transactionally,
5758 * let it become visible to the config cache.
5760 if (spa->spa_config_syncing != NULL) {
5761 spa_config_set(spa, spa->spa_config_syncing);
5762 spa->spa_config_txg = txg;
5763 spa->spa_config_syncing = NULL;
5766 spa->spa_ubsync = spa->spa_uberblock;
5768 dsl_pool_sync_done(dp, txg);
5771 * Update usable space statistics.
5773 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))))
5774 vdev_sync_done(vd, txg);
5776 spa_update_dspace(spa);
5779 * It had better be the case that we didn't dirty anything
5780 * since vdev_config_sync().
5782 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5783 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5784 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
5786 spa->spa_sync_pass = 0;
5788 spa_config_exit(spa, SCL_CONFIG, FTAG);
5790 spa_handle_ignored_writes(spa);
5793 * If any async tasks have been requested, kick them off.
5795 spa_async_dispatch(spa);
5799 * Sync all pools. We don't want to hold the namespace lock across these
5800 * operations, so we take a reference on the spa_t and drop the lock during the
5804 spa_sync_allpools(void)
5807 mutex_enter(&spa_namespace_lock);
5808 while ((spa = spa_next(spa)) != NULL) {
5809 if (spa_state(spa) != POOL_STATE_ACTIVE ||
5810 !spa_writeable(spa) || spa_suspended(spa))
5812 spa_open_ref(spa, FTAG);
5813 mutex_exit(&spa_namespace_lock);
5814 txg_wait_synced(spa_get_dsl(spa), 0);
5815 mutex_enter(&spa_namespace_lock);
5816 spa_close(spa, FTAG);
5818 mutex_exit(&spa_namespace_lock);
5822 * ==========================================================================
5823 * Miscellaneous routines
5824 * ==========================================================================
5828 * Remove all pools in the system.
5836 * Remove all cached state. All pools should be closed now,
5837 * so every spa in the AVL tree should be unreferenced.
5839 mutex_enter(&spa_namespace_lock);
5840 while ((spa = spa_next(NULL)) != NULL) {
5842 * Stop async tasks. The async thread may need to detach
5843 * a device that's been replaced, which requires grabbing
5844 * spa_namespace_lock, so we must drop it here.
5846 spa_open_ref(spa, FTAG);
5847 mutex_exit(&spa_namespace_lock);
5848 spa_async_suspend(spa);
5849 mutex_enter(&spa_namespace_lock);
5850 spa_close(spa, FTAG);
5852 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5854 spa_deactivate(spa);
5858 mutex_exit(&spa_namespace_lock);
5862 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
5867 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
5871 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
5872 vd = spa->spa_l2cache.sav_vdevs[i];
5873 if (vd->vdev_guid == guid)
5877 for (i = 0; i < spa->spa_spares.sav_count; i++) {
5878 vd = spa->spa_spares.sav_vdevs[i];
5879 if (vd->vdev_guid == guid)
5888 spa_upgrade(spa_t *spa, uint64_t version)
5890 ASSERT(spa_writeable(spa));
5892 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5895 * This should only be called for a non-faulted pool, and since a
5896 * future version would result in an unopenable pool, this shouldn't be
5899 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
5900 ASSERT(version >= spa->spa_uberblock.ub_version);
5902 spa->spa_uberblock.ub_version = version;
5903 vdev_config_dirty(spa->spa_root_vdev);
5905 spa_config_exit(spa, SCL_ALL, FTAG);
5907 txg_wait_synced(spa_get_dsl(spa), 0);
5911 spa_has_spare(spa_t *spa, uint64_t guid)
5915 spa_aux_vdev_t *sav = &spa->spa_spares;
5917 for (i = 0; i < sav->sav_count; i++)
5918 if (sav->sav_vdevs[i]->vdev_guid == guid)
5921 for (i = 0; i < sav->sav_npending; i++) {
5922 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
5923 &spareguid) == 0 && spareguid == guid)
5931 * Check if a pool has an active shared spare device.
5932 * Note: reference count of an active spare is 2, as a spare and as a replace
5935 spa_has_active_shared_spare(spa_t *spa)
5939 spa_aux_vdev_t *sav = &spa->spa_spares;
5941 for (i = 0; i < sav->sav_count; i++) {
5942 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
5943 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
5952 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
5953 * filled in from the spa and (optionally) the vdev. This doesn't do anything
5954 * in the userland libzpool, as we don't want consumers to misinterpret ztest
5955 * or zdb as real changes.
5958 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
5961 zfs_ereport_post(name, spa, vd, NULL, 0, 0);
5965 #if defined(_KERNEL) && defined(HAVE_SPL)
5966 /* state manipulation functions */
5967 EXPORT_SYMBOL(spa_open);
5968 EXPORT_SYMBOL(spa_open_rewind);
5969 EXPORT_SYMBOL(spa_get_stats);
5970 EXPORT_SYMBOL(spa_create);
5971 EXPORT_SYMBOL(spa_import_rootpool);
5972 EXPORT_SYMBOL(spa_import);
5973 EXPORT_SYMBOL(spa_tryimport);
5974 EXPORT_SYMBOL(spa_destroy);
5975 EXPORT_SYMBOL(spa_export);
5976 EXPORT_SYMBOL(spa_reset);
5977 EXPORT_SYMBOL(spa_async_request);
5978 EXPORT_SYMBOL(spa_async_suspend);
5979 EXPORT_SYMBOL(spa_async_resume);
5980 EXPORT_SYMBOL(spa_inject_addref);
5981 EXPORT_SYMBOL(spa_inject_delref);
5982 EXPORT_SYMBOL(spa_scan_stat_init);
5983 EXPORT_SYMBOL(spa_scan_get_stats);
5985 /* device maniion */
5986 EXPORT_SYMBOL(spa_vdev_add);
5987 EXPORT_SYMBOL(spa_vdev_attach);
5988 EXPORT_SYMBOL(spa_vdev_detach);
5989 EXPORT_SYMBOL(spa_vdev_remove);
5990 EXPORT_SYMBOL(spa_vdev_setpath);
5991 EXPORT_SYMBOL(spa_vdev_setfru);
5992 EXPORT_SYMBOL(spa_vdev_split_mirror);
5994 /* spare statech is global across all pools) */
5995 EXPORT_SYMBOL(spa_spare_add);
5996 EXPORT_SYMBOL(spa_spare_remove);
5997 EXPORT_SYMBOL(spa_spare_exists);
5998 EXPORT_SYMBOL(spa_spare_activate);
6000 /* L2ARC statech is global across all pools) */
6001 EXPORT_SYMBOL(spa_l2cache_add);
6002 EXPORT_SYMBOL(spa_l2cache_remove);
6003 EXPORT_SYMBOL(spa_l2cache_exists);
6004 EXPORT_SYMBOL(spa_l2cache_activate);
6005 EXPORT_SYMBOL(spa_l2cache_drop);
6008 EXPORT_SYMBOL(spa_scan);
6009 EXPORT_SYMBOL(spa_scan_stop);
6012 EXPORT_SYMBOL(spa_sync); /* only for DMU use */
6013 EXPORT_SYMBOL(spa_sync_allpools);
6016 EXPORT_SYMBOL(spa_prop_set);
6017 EXPORT_SYMBOL(spa_prop_get);
6018 EXPORT_SYMBOL(spa_prop_clear_bootfs);
6020 /* asynchronous event notification */
6021 EXPORT_SYMBOL(spa_event_notify);