4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011 by Delphix. All rights reserved.
28 * This file contains all the routines used when modifying on-disk SPA state.
29 * This includes opening, importing, destroying, exporting a pool, and syncing a
33 #include <sys/zfs_context.h>
34 #include <sys/fm/fs/zfs.h>
35 #include <sys/spa_impl.h>
37 #include <sys/zio_checksum.h>
39 #include <sys/dmu_tx.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/metaslab.h>
45 #include <sys/metaslab_impl.h>
46 #include <sys/uberblock_impl.h>
49 #include <sys/dmu_traverse.h>
50 #include <sys/dmu_objset.h>
51 #include <sys/unique.h>
52 #include <sys/dsl_pool.h>
53 #include <sys/dsl_dataset.h>
54 #include <sys/dsl_dir.h>
55 #include <sys/dsl_prop.h>
56 #include <sys/dsl_synctask.h>
57 #include <sys/fs/zfs.h>
59 #include <sys/callb.h>
60 #include <sys/spa_boot.h>
61 #include <sys/zfs_ioctl.h>
62 #include <sys/dsl_scan.h>
66 #include <sys/callb.h>
67 #include <sys/cpupart.h>
72 #include "zfs_comutil.h"
74 /* Check hostid on import? */
75 static int check_hostid = 1;
77 SYSCTL_DECL(_vfs_zfs);
78 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
79 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
80 "Check hostid on import?");
82 typedef enum zti_modes {
83 zti_mode_fixed, /* value is # of threads (min 1) */
84 zti_mode_online_percent, /* value is % of online CPUs */
85 zti_mode_batch, /* cpu-intensive; value is ignored */
86 zti_mode_null, /* don't create a taskq */
90 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
91 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
92 #define ZTI_BATCH { zti_mode_batch, 0 }
93 #define ZTI_NULL { zti_mode_null, 0 }
95 #define ZTI_ONE ZTI_FIX(1)
97 typedef struct zio_taskq_info {
98 enum zti_modes zti_mode;
102 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
103 "issue", "issue_high", "intr", "intr_high"
107 * Define the taskq threads for the following I/O types:
108 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
110 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
111 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
112 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
113 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
114 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
115 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
116 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
117 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
120 static dsl_syncfunc_t spa_sync_props;
121 static boolean_t spa_has_active_shared_spare(spa_t *spa);
122 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
123 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
125 static void spa_vdev_resilver_done(spa_t *spa);
127 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
129 id_t zio_taskq_psrset_bind = PS_NONE;
132 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
134 uint_t zio_taskq_basedc = 80; /* base duty cycle */
136 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
139 * This (illegal) pool name is used when temporarily importing a spa_t in order
140 * to get the vdev stats associated with the imported devices.
142 #define TRYIMPORT_NAME "$import"
145 * ==========================================================================
146 * SPA properties routines
147 * ==========================================================================
151 * Add a (source=src, propname=propval) list to an nvlist.
154 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
155 uint64_t intval, zprop_source_t src)
157 const char *propname = zpool_prop_to_name(prop);
160 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
161 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
164 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
166 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
168 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
169 nvlist_free(propval);
173 * Get property values from the spa configuration.
176 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
180 uint64_t cap, version;
181 zprop_source_t src = ZPROP_SRC_NONE;
182 spa_config_dirent_t *dp;
184 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
186 if (spa->spa_root_vdev != NULL) {
187 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
188 size = metaslab_class_get_space(spa_normal_class(spa));
189 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
190 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
191 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
192 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
194 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
195 (spa_mode(spa) == FREAD), src);
197 cap = (size == 0) ? 0 : (alloc * 100 / size);
198 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
200 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
201 ddt_get_pool_dedup_ratio(spa), src);
203 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
204 spa->spa_root_vdev->vdev_state, src);
206 version = spa_version(spa);
207 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
208 src = ZPROP_SRC_DEFAULT;
210 src = ZPROP_SRC_LOCAL;
211 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
214 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
216 if (spa->spa_comment != NULL) {
217 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
221 if (spa->spa_root != NULL)
222 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
225 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
226 if (dp->scd_path == NULL) {
227 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
228 "none", 0, ZPROP_SRC_LOCAL);
229 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
230 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
231 dp->scd_path, 0, ZPROP_SRC_LOCAL);
237 * Get zpool property values.
240 spa_prop_get(spa_t *spa, nvlist_t **nvp)
242 objset_t *mos = spa->spa_meta_objset;
247 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
249 mutex_enter(&spa->spa_props_lock);
252 * Get properties from the spa config.
254 spa_prop_get_config(spa, nvp);
256 /* If no pool property object, no more prop to get. */
257 if (mos == NULL || spa->spa_pool_props_object == 0) {
258 mutex_exit(&spa->spa_props_lock);
263 * Get properties from the MOS pool property object.
265 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
266 (err = zap_cursor_retrieve(&zc, &za)) == 0;
267 zap_cursor_advance(&zc)) {
270 zprop_source_t src = ZPROP_SRC_DEFAULT;
273 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
276 switch (za.za_integer_length) {
278 /* integer property */
279 if (za.za_first_integer !=
280 zpool_prop_default_numeric(prop))
281 src = ZPROP_SRC_LOCAL;
283 if (prop == ZPOOL_PROP_BOOTFS) {
285 dsl_dataset_t *ds = NULL;
287 dp = spa_get_dsl(spa);
288 rw_enter(&dp->dp_config_rwlock, RW_READER);
289 if (err = dsl_dataset_hold_obj(dp,
290 za.za_first_integer, FTAG, &ds)) {
291 rw_exit(&dp->dp_config_rwlock);
296 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
298 dsl_dataset_name(ds, strval);
299 dsl_dataset_rele(ds, FTAG);
300 rw_exit(&dp->dp_config_rwlock);
303 intval = za.za_first_integer;
306 spa_prop_add_list(*nvp, prop, strval, intval, src);
310 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
315 /* string property */
316 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
317 err = zap_lookup(mos, spa->spa_pool_props_object,
318 za.za_name, 1, za.za_num_integers, strval);
320 kmem_free(strval, za.za_num_integers);
323 spa_prop_add_list(*nvp, prop, strval, 0, src);
324 kmem_free(strval, za.za_num_integers);
331 zap_cursor_fini(&zc);
332 mutex_exit(&spa->spa_props_lock);
334 if (err && err != ENOENT) {
344 * Validate the given pool properties nvlist and modify the list
345 * for the property values to be set.
348 spa_prop_validate(spa_t *spa, nvlist_t *props)
351 int error = 0, reset_bootfs = 0;
355 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
357 char *propname, *strval;
362 propname = nvpair_name(elem);
364 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
368 case ZPOOL_PROP_VERSION:
369 error = nvpair_value_uint64(elem, &intval);
371 (intval < spa_version(spa) || intval > SPA_VERSION))
375 case ZPOOL_PROP_DELEGATION:
376 case ZPOOL_PROP_AUTOREPLACE:
377 case ZPOOL_PROP_LISTSNAPS:
378 case ZPOOL_PROP_AUTOEXPAND:
379 error = nvpair_value_uint64(elem, &intval);
380 if (!error && intval > 1)
384 case ZPOOL_PROP_BOOTFS:
386 * If the pool version is less than SPA_VERSION_BOOTFS,
387 * or the pool is still being created (version == 0),
388 * the bootfs property cannot be set.
390 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
396 * Make sure the vdev config is bootable
398 if (!vdev_is_bootable(spa->spa_root_vdev)) {
405 error = nvpair_value_string(elem, &strval);
410 if (strval == NULL || strval[0] == '\0') {
411 objnum = zpool_prop_default_numeric(
416 if (error = dmu_objset_hold(strval, FTAG, &os))
419 /* Must be ZPL and not gzip compressed. */
421 if (dmu_objset_type(os) != DMU_OST_ZFS) {
423 } else if ((error = dsl_prop_get_integer(strval,
424 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
425 &compress, NULL)) == 0 &&
426 !BOOTFS_COMPRESS_VALID(compress)) {
429 objnum = dmu_objset_id(os);
431 dmu_objset_rele(os, FTAG);
435 case ZPOOL_PROP_FAILUREMODE:
436 error = nvpair_value_uint64(elem, &intval);
437 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
438 intval > ZIO_FAILURE_MODE_PANIC))
442 * This is a special case which only occurs when
443 * the pool has completely failed. This allows
444 * the user to change the in-core failmode property
445 * without syncing it out to disk (I/Os might
446 * currently be blocked). We do this by returning
447 * EIO to the caller (spa_prop_set) to trick it
448 * into thinking we encountered a property validation
451 if (!error && spa_suspended(spa)) {
452 spa->spa_failmode = intval;
457 case ZPOOL_PROP_CACHEFILE:
458 if ((error = nvpair_value_string(elem, &strval)) != 0)
461 if (strval[0] == '\0')
464 if (strcmp(strval, "none") == 0)
467 if (strval[0] != '/') {
472 slash = strrchr(strval, '/');
473 ASSERT(slash != NULL);
475 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
476 strcmp(slash, "/..") == 0)
480 case ZPOOL_PROP_COMMENT:
481 if ((error = nvpair_value_string(elem, &strval)) != 0)
483 for (check = strval; *check != '\0'; check++) {
485 * The kernel doesn't have an easy isprint()
486 * check. For this kernel check, we merely
487 * check ASCII apart from DEL. Fix this if
488 * there is an easy-to-use kernel isprint().
490 if (*check >= 0x7f) {
496 if (strlen(strval) > ZPROP_MAX_COMMENT)
500 case ZPOOL_PROP_DEDUPDITTO:
501 if (spa_version(spa) < SPA_VERSION_DEDUP)
504 error = nvpair_value_uint64(elem, &intval);
506 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
515 if (!error && reset_bootfs) {
516 error = nvlist_remove(props,
517 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
520 error = nvlist_add_uint64(props,
521 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
529 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
532 spa_config_dirent_t *dp;
534 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
538 dp = kmem_alloc(sizeof (spa_config_dirent_t),
541 if (cachefile[0] == '\0')
542 dp->scd_path = spa_strdup(spa_config_path);
543 else if (strcmp(cachefile, "none") == 0)
546 dp->scd_path = spa_strdup(cachefile);
548 list_insert_head(&spa->spa_config_list, dp);
550 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
554 spa_prop_set(spa_t *spa, nvlist_t *nvp)
558 boolean_t need_sync = B_FALSE;
561 if ((error = spa_prop_validate(spa, nvp)) != 0)
565 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
566 if ((prop = zpool_name_to_prop(
567 nvpair_name(elem))) == ZPROP_INVAL)
570 if (prop == ZPOOL_PROP_CACHEFILE ||
571 prop == ZPOOL_PROP_ALTROOT ||
572 prop == ZPOOL_PROP_READONLY)
580 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
587 * If the bootfs property value is dsobj, clear it.
590 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
592 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
593 VERIFY(zap_remove(spa->spa_meta_objset,
594 spa->spa_pool_props_object,
595 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
601 * Change the GUID for the pool. This is done so that we can later
602 * re-import a pool built from a clone of our own vdevs. We will modify
603 * the root vdev's guid, our own pool guid, and then mark all of our
604 * vdevs dirty. Note that we must make sure that all our vdevs are
605 * online when we do this, or else any vdevs that weren't present
606 * would be orphaned from our pool. We are also going to issue a
607 * sysevent to update any watchers.
610 spa_change_guid(spa_t *spa)
612 uint64_t oldguid, newguid;
615 if (!(spa_mode_global & FWRITE))
618 txg = spa_vdev_enter(spa);
620 if (spa->spa_root_vdev->vdev_state != VDEV_STATE_HEALTHY)
621 return (spa_vdev_exit(spa, NULL, txg, ENXIO));
623 oldguid = spa_guid(spa);
624 newguid = spa_generate_guid(NULL);
625 ASSERT3U(oldguid, !=, newguid);
627 spa->spa_root_vdev->vdev_guid = newguid;
628 spa->spa_root_vdev->vdev_guid_sum += (newguid - oldguid);
630 vdev_config_dirty(spa->spa_root_vdev);
632 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
634 return (spa_vdev_exit(spa, NULL, txg, 0));
638 * ==========================================================================
639 * SPA state manipulation (open/create/destroy/import/export)
640 * ==========================================================================
644 spa_error_entry_compare(const void *a, const void *b)
646 spa_error_entry_t *sa = (spa_error_entry_t *)a;
647 spa_error_entry_t *sb = (spa_error_entry_t *)b;
650 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
651 sizeof (zbookmark_t));
662 * Utility function which retrieves copies of the current logs and
663 * re-initializes them in the process.
666 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
668 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
670 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
671 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
673 avl_create(&spa->spa_errlist_scrub,
674 spa_error_entry_compare, sizeof (spa_error_entry_t),
675 offsetof(spa_error_entry_t, se_avl));
676 avl_create(&spa->spa_errlist_last,
677 spa_error_entry_compare, sizeof (spa_error_entry_t),
678 offsetof(spa_error_entry_t, se_avl));
682 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
685 uint_t flags = TASKQ_PREPOPULATE;
686 boolean_t batch = B_FALSE;
690 return (NULL); /* no taskq needed */
693 ASSERT3U(value, >=, 1);
694 value = MAX(value, 1);
699 flags |= TASKQ_THREADS_CPU_PCT;
700 value = zio_taskq_batch_pct;
703 case zti_mode_online_percent:
704 flags |= TASKQ_THREADS_CPU_PCT;
708 panic("unrecognized mode for %s taskq (%u:%u) in "
715 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
717 flags |= TASKQ_DC_BATCH;
719 return (taskq_create_sysdc(name, value, 50, INT_MAX,
720 spa->spa_proc, zio_taskq_basedc, flags));
723 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
724 spa->spa_proc, flags));
728 spa_create_zio_taskqs(spa_t *spa)
730 for (int t = 0; t < ZIO_TYPES; t++) {
731 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
732 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
733 enum zti_modes mode = ztip->zti_mode;
734 uint_t value = ztip->zti_value;
737 (void) snprintf(name, sizeof (name),
738 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
740 spa->spa_zio_taskq[t][q] =
741 spa_taskq_create(spa, name, mode, value);
749 spa_thread(void *arg)
754 user_t *pu = PTOU(curproc);
756 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
759 ASSERT(curproc != &p0);
760 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
761 "zpool-%s", spa->spa_name);
762 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
765 /* bind this thread to the requested psrset */
766 if (zio_taskq_psrset_bind != PS_NONE) {
768 mutex_enter(&cpu_lock);
769 mutex_enter(&pidlock);
770 mutex_enter(&curproc->p_lock);
772 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
773 0, NULL, NULL) == 0) {
774 curthread->t_bind_pset = zio_taskq_psrset_bind;
777 "Couldn't bind process for zfs pool \"%s\" to "
778 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
781 mutex_exit(&curproc->p_lock);
782 mutex_exit(&pidlock);
783 mutex_exit(&cpu_lock);
789 if (zio_taskq_sysdc) {
790 sysdc_thread_enter(curthread, 100, 0);
794 spa->spa_proc = curproc;
795 spa->spa_did = curthread->t_did;
797 spa_create_zio_taskqs(spa);
799 mutex_enter(&spa->spa_proc_lock);
800 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
802 spa->spa_proc_state = SPA_PROC_ACTIVE;
803 cv_broadcast(&spa->spa_proc_cv);
805 CALLB_CPR_SAFE_BEGIN(&cprinfo);
806 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
807 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
808 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
810 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
811 spa->spa_proc_state = SPA_PROC_GONE;
813 cv_broadcast(&spa->spa_proc_cv);
814 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
816 mutex_enter(&curproc->p_lock);
819 #endif /* SPA_PROCESS */
823 * Activate an uninitialized pool.
826 spa_activate(spa_t *spa, int mode)
828 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
830 spa->spa_state = POOL_STATE_ACTIVE;
831 spa->spa_mode = mode;
833 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
834 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
836 /* Try to create a covering process */
837 mutex_enter(&spa->spa_proc_lock);
838 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
839 ASSERT(spa->spa_proc == &p0);
843 /* Only create a process if we're going to be around a while. */
844 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
845 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
847 spa->spa_proc_state = SPA_PROC_CREATED;
848 while (spa->spa_proc_state == SPA_PROC_CREATED) {
849 cv_wait(&spa->spa_proc_cv,
850 &spa->spa_proc_lock);
852 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
853 ASSERT(spa->spa_proc != &p0);
854 ASSERT(spa->spa_did != 0);
858 "Couldn't create process for zfs pool \"%s\"\n",
863 #endif /* SPA_PROCESS */
864 mutex_exit(&spa->spa_proc_lock);
866 /* If we didn't create a process, we need to create our taskqs. */
867 ASSERT(spa->spa_proc == &p0);
868 if (spa->spa_proc == &p0) {
869 spa_create_zio_taskqs(spa);
872 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
873 offsetof(vdev_t, vdev_config_dirty_node));
874 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
875 offsetof(vdev_t, vdev_state_dirty_node));
877 txg_list_create(&spa->spa_vdev_txg_list,
878 offsetof(struct vdev, vdev_txg_node));
880 avl_create(&spa->spa_errlist_scrub,
881 spa_error_entry_compare, sizeof (spa_error_entry_t),
882 offsetof(spa_error_entry_t, se_avl));
883 avl_create(&spa->spa_errlist_last,
884 spa_error_entry_compare, sizeof (spa_error_entry_t),
885 offsetof(spa_error_entry_t, se_avl));
889 * Opposite of spa_activate().
892 spa_deactivate(spa_t *spa)
894 ASSERT(spa->spa_sync_on == B_FALSE);
895 ASSERT(spa->spa_dsl_pool == NULL);
896 ASSERT(spa->spa_root_vdev == NULL);
897 ASSERT(spa->spa_async_zio_root == NULL);
898 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
900 txg_list_destroy(&spa->spa_vdev_txg_list);
902 list_destroy(&spa->spa_config_dirty_list);
903 list_destroy(&spa->spa_state_dirty_list);
905 for (int t = 0; t < ZIO_TYPES; t++) {
906 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
907 if (spa->spa_zio_taskq[t][q] != NULL)
908 taskq_destroy(spa->spa_zio_taskq[t][q]);
909 spa->spa_zio_taskq[t][q] = NULL;
913 metaslab_class_destroy(spa->spa_normal_class);
914 spa->spa_normal_class = NULL;
916 metaslab_class_destroy(spa->spa_log_class);
917 spa->spa_log_class = NULL;
920 * If this was part of an import or the open otherwise failed, we may
921 * still have errors left in the queues. Empty them just in case.
923 spa_errlog_drain(spa);
925 avl_destroy(&spa->spa_errlist_scrub);
926 avl_destroy(&spa->spa_errlist_last);
928 spa->spa_state = POOL_STATE_UNINITIALIZED;
930 mutex_enter(&spa->spa_proc_lock);
931 if (spa->spa_proc_state != SPA_PROC_NONE) {
932 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
933 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
934 cv_broadcast(&spa->spa_proc_cv);
935 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
936 ASSERT(spa->spa_proc != &p0);
937 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
939 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
940 spa->spa_proc_state = SPA_PROC_NONE;
942 ASSERT(spa->spa_proc == &p0);
943 mutex_exit(&spa->spa_proc_lock);
947 * We want to make sure spa_thread() has actually exited the ZFS
948 * module, so that the module can't be unloaded out from underneath
951 if (spa->spa_did != 0) {
952 thread_join(spa->spa_did);
955 #endif /* SPA_PROCESS */
959 * Verify a pool configuration, and construct the vdev tree appropriately. This
960 * will create all the necessary vdevs in the appropriate layout, with each vdev
961 * in the CLOSED state. This will prep the pool before open/creation/import.
962 * All vdev validation is done by the vdev_alloc() routine.
965 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
966 uint_t id, int atype)
972 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
975 if ((*vdp)->vdev_ops->vdev_op_leaf)
978 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
990 for (int c = 0; c < children; c++) {
992 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1000 ASSERT(*vdp != NULL);
1006 * Opposite of spa_load().
1009 spa_unload(spa_t *spa)
1013 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1018 spa_async_suspend(spa);
1023 if (spa->spa_sync_on) {
1024 txg_sync_stop(spa->spa_dsl_pool);
1025 spa->spa_sync_on = B_FALSE;
1029 * Wait for any outstanding async I/O to complete.
1031 if (spa->spa_async_zio_root != NULL) {
1032 (void) zio_wait(spa->spa_async_zio_root);
1033 spa->spa_async_zio_root = NULL;
1036 bpobj_close(&spa->spa_deferred_bpobj);
1039 * Close the dsl pool.
1041 if (spa->spa_dsl_pool) {
1042 dsl_pool_close(spa->spa_dsl_pool);
1043 spa->spa_dsl_pool = NULL;
1044 spa->spa_meta_objset = NULL;
1049 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1052 * Drop and purge level 2 cache
1054 spa_l2cache_drop(spa);
1059 if (spa->spa_root_vdev)
1060 vdev_free(spa->spa_root_vdev);
1061 ASSERT(spa->spa_root_vdev == NULL);
1063 for (i = 0; i < spa->spa_spares.sav_count; i++)
1064 vdev_free(spa->spa_spares.sav_vdevs[i]);
1065 if (spa->spa_spares.sav_vdevs) {
1066 kmem_free(spa->spa_spares.sav_vdevs,
1067 spa->spa_spares.sav_count * sizeof (void *));
1068 spa->spa_spares.sav_vdevs = NULL;
1070 if (spa->spa_spares.sav_config) {
1071 nvlist_free(spa->spa_spares.sav_config);
1072 spa->spa_spares.sav_config = NULL;
1074 spa->spa_spares.sav_count = 0;
1076 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
1077 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;
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_SLEEP);
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 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1306 pool != 0ULL && l2arc_vdev_present(vd))
1307 l2arc_remove_vdev(vd);
1308 (void) vdev_close(vd);
1309 spa_l2cache_remove(vd);
1314 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1316 if (sav->sav_config == NULL)
1319 sav->sav_vdevs = newvdevs;
1320 sav->sav_count = (int)nl2cache;
1323 * Recompute the stashed list of l2cache devices, with status
1324 * information this time.
1326 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1327 DATA_TYPE_NVLIST_ARRAY) == 0);
1329 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1330 for (i = 0; i < sav->sav_count; i++)
1331 l2cache[i] = vdev_config_generate(spa,
1332 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1333 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1334 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1336 for (i = 0; i < sav->sav_count; i++)
1337 nvlist_free(l2cache[i]);
1339 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1343 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1346 char *packed = NULL;
1351 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1352 nvsize = *(uint64_t *)db->db_data;
1353 dmu_buf_rele(db, FTAG);
1355 packed = kmem_alloc(nvsize, KM_SLEEP);
1356 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1359 error = nvlist_unpack(packed, nvsize, value, 0);
1360 kmem_free(packed, nvsize);
1366 * Checks to see if the given vdev could not be opened, in which case we post a
1367 * sysevent to notify the autoreplace code that the device has been removed.
1370 spa_check_removed(vdev_t *vd)
1372 for (int c = 0; c < vd->vdev_children; c++)
1373 spa_check_removed(vd->vdev_child[c]);
1375 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1376 zfs_post_autoreplace(vd->vdev_spa, vd);
1377 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1382 * Validate the current config against the MOS config
1385 spa_config_valid(spa_t *spa, nvlist_t *config)
1387 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1390 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1392 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1393 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1395 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1398 * If we're doing a normal import, then build up any additional
1399 * diagnostic information about missing devices in this config.
1400 * We'll pass this up to the user for further processing.
1402 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1403 nvlist_t **child, *nv;
1406 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1408 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1410 for (int c = 0; c < rvd->vdev_children; c++) {
1411 vdev_t *tvd = rvd->vdev_child[c];
1412 vdev_t *mtvd = mrvd->vdev_child[c];
1414 if (tvd->vdev_ops == &vdev_missing_ops &&
1415 mtvd->vdev_ops != &vdev_missing_ops &&
1417 child[idx++] = vdev_config_generate(spa, mtvd,
1422 VERIFY(nvlist_add_nvlist_array(nv,
1423 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1424 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1425 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1427 for (int i = 0; i < idx; i++)
1428 nvlist_free(child[i]);
1431 kmem_free(child, rvd->vdev_children * sizeof (char **));
1435 * Compare the root vdev tree with the information we have
1436 * from the MOS config (mrvd). Check each top-level vdev
1437 * with the corresponding MOS config top-level (mtvd).
1439 for (int c = 0; c < rvd->vdev_children; c++) {
1440 vdev_t *tvd = rvd->vdev_child[c];
1441 vdev_t *mtvd = mrvd->vdev_child[c];
1444 * Resolve any "missing" vdevs in the current configuration.
1445 * If we find that the MOS config has more accurate information
1446 * about the top-level vdev then use that vdev instead.
1448 if (tvd->vdev_ops == &vdev_missing_ops &&
1449 mtvd->vdev_ops != &vdev_missing_ops) {
1451 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1455 * Device specific actions.
1457 if (mtvd->vdev_islog) {
1458 spa_set_log_state(spa, SPA_LOG_CLEAR);
1461 * XXX - once we have 'readonly' pool
1462 * support we should be able to handle
1463 * missing data devices by transitioning
1464 * the pool to readonly.
1470 * Swap the missing vdev with the data we were
1471 * able to obtain from the MOS config.
1473 vdev_remove_child(rvd, tvd);
1474 vdev_remove_child(mrvd, mtvd);
1476 vdev_add_child(rvd, mtvd);
1477 vdev_add_child(mrvd, tvd);
1479 spa_config_exit(spa, SCL_ALL, FTAG);
1481 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1484 } else if (mtvd->vdev_islog) {
1486 * Load the slog device's state from the MOS config
1487 * since it's possible that the label does not
1488 * contain the most up-to-date information.
1490 vdev_load_log_state(tvd, mtvd);
1495 spa_config_exit(spa, SCL_ALL, FTAG);
1498 * Ensure we were able to validate the config.
1500 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1504 * Check for missing log devices
1507 spa_check_logs(spa_t *spa)
1509 switch (spa->spa_log_state) {
1510 case SPA_LOG_MISSING:
1511 /* need to recheck in case slog has been restored */
1512 case SPA_LOG_UNKNOWN:
1513 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1514 DS_FIND_CHILDREN)) {
1515 spa_set_log_state(spa, SPA_LOG_MISSING);
1524 spa_passivate_log(spa_t *spa)
1526 vdev_t *rvd = spa->spa_root_vdev;
1527 boolean_t slog_found = B_FALSE;
1529 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1531 if (!spa_has_slogs(spa))
1534 for (int c = 0; c < rvd->vdev_children; c++) {
1535 vdev_t *tvd = rvd->vdev_child[c];
1536 metaslab_group_t *mg = tvd->vdev_mg;
1538 if (tvd->vdev_islog) {
1539 metaslab_group_passivate(mg);
1540 slog_found = B_TRUE;
1544 return (slog_found);
1548 spa_activate_log(spa_t *spa)
1550 vdev_t *rvd = spa->spa_root_vdev;
1552 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1554 for (int c = 0; c < rvd->vdev_children; c++) {
1555 vdev_t *tvd = rvd->vdev_child[c];
1556 metaslab_group_t *mg = tvd->vdev_mg;
1558 if (tvd->vdev_islog)
1559 metaslab_group_activate(mg);
1564 spa_offline_log(spa_t *spa)
1568 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1569 NULL, DS_FIND_CHILDREN)) == 0) {
1572 * We successfully offlined the log device, sync out the
1573 * current txg so that the "stubby" block can be removed
1576 txg_wait_synced(spa->spa_dsl_pool, 0);
1582 spa_aux_check_removed(spa_aux_vdev_t *sav)
1586 for (i = 0; i < sav->sav_count; i++)
1587 spa_check_removed(sav->sav_vdevs[i]);
1591 spa_claim_notify(zio_t *zio)
1593 spa_t *spa = zio->io_spa;
1598 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1599 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1600 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1601 mutex_exit(&spa->spa_props_lock);
1604 typedef struct spa_load_error {
1605 uint64_t sle_meta_count;
1606 uint64_t sle_data_count;
1610 spa_load_verify_done(zio_t *zio)
1612 blkptr_t *bp = zio->io_bp;
1613 spa_load_error_t *sle = zio->io_private;
1614 dmu_object_type_t type = BP_GET_TYPE(bp);
1615 int error = zio->io_error;
1618 if ((BP_GET_LEVEL(bp) != 0 || dmu_ot[type].ot_metadata) &&
1619 type != DMU_OT_INTENT_LOG)
1620 atomic_add_64(&sle->sle_meta_count, 1);
1622 atomic_add_64(&sle->sle_data_count, 1);
1624 zio_data_buf_free(zio->io_data, zio->io_size);
1629 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1630 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1634 size_t size = BP_GET_PSIZE(bp);
1635 void *data = zio_data_buf_alloc(size);
1637 zio_nowait(zio_read(rio, spa, bp, data, size,
1638 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1639 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1640 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1646 spa_load_verify(spa_t *spa)
1649 spa_load_error_t sle = { 0 };
1650 zpool_rewind_policy_t policy;
1651 boolean_t verify_ok = B_FALSE;
1654 zpool_get_rewind_policy(spa->spa_config, &policy);
1656 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1659 rio = zio_root(spa, NULL, &sle,
1660 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1662 error = traverse_pool(spa, spa->spa_verify_min_txg,
1663 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1665 (void) zio_wait(rio);
1667 spa->spa_load_meta_errors = sle.sle_meta_count;
1668 spa->spa_load_data_errors = sle.sle_data_count;
1670 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1671 sle.sle_data_count <= policy.zrp_maxdata) {
1675 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1676 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1678 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1679 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1680 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1681 VERIFY(nvlist_add_int64(spa->spa_load_info,
1682 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1683 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1684 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1686 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1690 if (error != ENXIO && error != EIO)
1695 return (verify_ok ? 0 : EIO);
1699 * Find a value in the pool props object.
1702 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1704 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1705 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1709 * Find a value in the pool directory object.
1712 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1714 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1715 name, sizeof (uint64_t), 1, val));
1719 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1721 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1726 * Fix up config after a partly-completed split. This is done with the
1727 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1728 * pool have that entry in their config, but only the splitting one contains
1729 * a list of all the guids of the vdevs that are being split off.
1731 * This function determines what to do with that list: either rejoin
1732 * all the disks to the pool, or complete the splitting process. To attempt
1733 * the rejoin, each disk that is offlined is marked online again, and
1734 * we do a reopen() call. If the vdev label for every disk that was
1735 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1736 * then we call vdev_split() on each disk, and complete the split.
1738 * Otherwise we leave the config alone, with all the vdevs in place in
1739 * the original pool.
1742 spa_try_repair(spa_t *spa, nvlist_t *config)
1749 boolean_t attempt_reopen;
1751 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1754 /* check that the config is complete */
1755 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1756 &glist, &gcount) != 0)
1759 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1761 /* attempt to online all the vdevs & validate */
1762 attempt_reopen = B_TRUE;
1763 for (i = 0; i < gcount; i++) {
1764 if (glist[i] == 0) /* vdev is hole */
1767 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1768 if (vd[i] == NULL) {
1770 * Don't bother attempting to reopen the disks;
1771 * just do the split.
1773 attempt_reopen = B_FALSE;
1775 /* attempt to re-online it */
1776 vd[i]->vdev_offline = B_FALSE;
1780 if (attempt_reopen) {
1781 vdev_reopen(spa->spa_root_vdev);
1783 /* check each device to see what state it's in */
1784 for (extracted = 0, i = 0; i < gcount; i++) {
1785 if (vd[i] != NULL &&
1786 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1793 * If every disk has been moved to the new pool, or if we never
1794 * even attempted to look at them, then we split them off for
1797 if (!attempt_reopen || gcount == extracted) {
1798 for (i = 0; i < gcount; i++)
1801 vdev_reopen(spa->spa_root_vdev);
1804 kmem_free(vd, gcount * sizeof (vdev_t *));
1808 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1809 boolean_t mosconfig)
1811 nvlist_t *config = spa->spa_config;
1812 char *ereport = FM_EREPORT_ZFS_POOL;
1818 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1821 ASSERT(spa->spa_comment == NULL);
1822 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1823 spa->spa_comment = spa_strdup(comment);
1826 * Versioning wasn't explicitly added to the label until later, so if
1827 * it's not present treat it as the initial version.
1829 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1830 &spa->spa_ubsync.ub_version) != 0)
1831 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1833 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1834 &spa->spa_config_txg);
1836 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1837 spa_guid_exists(pool_guid, 0)) {
1840 spa->spa_config_guid = pool_guid;
1842 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1844 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1848 gethrestime(&spa->spa_loaded_ts);
1849 error = spa_load_impl(spa, pool_guid, config, state, type,
1850 mosconfig, &ereport);
1853 spa->spa_minref = refcount_count(&spa->spa_refcount);
1855 if (error != EEXIST) {
1856 spa->spa_loaded_ts.tv_sec = 0;
1857 spa->spa_loaded_ts.tv_nsec = 0;
1859 if (error != EBADF) {
1860 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1863 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
1870 * Load an existing storage pool, using the pool's builtin spa_config as a
1871 * source of configuration information.
1874 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
1875 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
1879 nvlist_t *nvroot = NULL;
1881 uberblock_t *ub = &spa->spa_uberblock;
1882 uint64_t children, config_cache_txg = spa->spa_config_txg;
1883 int orig_mode = spa->spa_mode;
1888 * If this is an untrusted config, access the pool in read-only mode.
1889 * This prevents things like resilvering recently removed devices.
1892 spa->spa_mode = FREAD;
1894 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1896 spa->spa_load_state = state;
1898 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
1901 parse = (type == SPA_IMPORT_EXISTING ?
1902 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
1905 * Create "The Godfather" zio to hold all async IOs
1907 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
1908 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
1911 * Parse the configuration into a vdev tree. We explicitly set the
1912 * value that will be returned by spa_version() since parsing the
1913 * configuration requires knowing the version number.
1915 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1916 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
1917 spa_config_exit(spa, SCL_ALL, FTAG);
1922 ASSERT(spa->spa_root_vdev == rvd);
1924 if (type != SPA_IMPORT_ASSEMBLE) {
1925 ASSERT(spa_guid(spa) == pool_guid);
1929 * Try to open all vdevs, loading each label in the process.
1931 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1932 error = vdev_open(rvd);
1933 spa_config_exit(spa, SCL_ALL, FTAG);
1938 * We need to validate the vdev labels against the configuration that
1939 * we have in hand, which is dependent on the setting of mosconfig. If
1940 * mosconfig is true then we're validating the vdev labels based on
1941 * that config. Otherwise, we're validating against the cached config
1942 * (zpool.cache) that was read when we loaded the zfs module, and then
1943 * later we will recursively call spa_load() and validate against
1946 * If we're assembling a new pool that's been split off from an
1947 * existing pool, the labels haven't yet been updated so we skip
1948 * validation for now.
1950 if (type != SPA_IMPORT_ASSEMBLE) {
1951 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1952 error = vdev_validate(rvd);
1953 spa_config_exit(spa, SCL_ALL, FTAG);
1958 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
1963 * Find the best uberblock.
1965 vdev_uberblock_load(NULL, rvd, ub);
1968 * If we weren't able to find a single valid uberblock, return failure.
1970 if (ub->ub_txg == 0)
1971 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
1974 * If the pool is newer than the code, we can't open it.
1976 if (ub->ub_version > SPA_VERSION)
1977 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
1980 * If the vdev guid sum doesn't match the uberblock, we have an
1981 * incomplete configuration. We first check to see if the pool
1982 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
1983 * If it is, defer the vdev_guid_sum check till later so we
1984 * can handle missing vdevs.
1986 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
1987 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
1988 rvd->vdev_guid_sum != ub->ub_guid_sum)
1989 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
1991 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
1992 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1993 spa_try_repair(spa, config);
1994 spa_config_exit(spa, SCL_ALL, FTAG);
1995 nvlist_free(spa->spa_config_splitting);
1996 spa->spa_config_splitting = NULL;
2000 * Initialize internal SPA structures.
2002 spa->spa_state = POOL_STATE_ACTIVE;
2003 spa->spa_ubsync = spa->spa_uberblock;
2004 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2005 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2006 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2007 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2008 spa->spa_claim_max_txg = spa->spa_first_txg;
2009 spa->spa_prev_software_version = ub->ub_software_version;
2011 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2013 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2014 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2016 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2017 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2021 nvlist_t *policy = NULL, *nvconfig;
2023 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2024 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2026 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2027 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2029 unsigned long myhostid = 0;
2031 VERIFY(nvlist_lookup_string(nvconfig,
2032 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2035 myhostid = zone_get_hostid(NULL);
2038 * We're emulating the system's hostid in userland, so
2039 * we can't use zone_get_hostid().
2041 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2042 #endif /* _KERNEL */
2043 if (check_hostid && hostid != 0 && myhostid != 0 &&
2044 hostid != myhostid) {
2045 nvlist_free(nvconfig);
2046 cmn_err(CE_WARN, "pool '%s' could not be "
2047 "loaded as it was last accessed by "
2048 "another system (host: %s hostid: 0x%lx). "
2049 "See: http://www.sun.com/msg/ZFS-8000-EY",
2050 spa_name(spa), hostname,
2051 (unsigned long)hostid);
2055 if (nvlist_lookup_nvlist(spa->spa_config,
2056 ZPOOL_REWIND_POLICY, &policy) == 0)
2057 VERIFY(nvlist_add_nvlist(nvconfig,
2058 ZPOOL_REWIND_POLICY, policy) == 0);
2060 spa_config_set(spa, nvconfig);
2062 spa_deactivate(spa);
2063 spa_activate(spa, orig_mode);
2065 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2068 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2069 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2070 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2072 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2075 * Load the bit that tells us to use the new accounting function
2076 * (raid-z deflation). If we have an older pool, this will not
2079 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2080 if (error != 0 && error != ENOENT)
2081 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2083 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2084 &spa->spa_creation_version);
2085 if (error != 0 && error != ENOENT)
2086 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2089 * Load the persistent error log. If we have an older pool, this will
2092 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2093 if (error != 0 && error != ENOENT)
2094 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2096 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2097 &spa->spa_errlog_scrub);
2098 if (error != 0 && error != ENOENT)
2099 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2102 * Load the history object. If we have an older pool, this
2103 * will not be present.
2105 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2106 if (error != 0 && error != ENOENT)
2107 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2110 * If we're assembling the pool from the split-off vdevs of
2111 * an existing pool, we don't want to attach the spares & cache
2116 * Load any hot spares for this pool.
2118 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2119 if (error != 0 && error != ENOENT)
2120 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2121 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2122 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2123 if (load_nvlist(spa, spa->spa_spares.sav_object,
2124 &spa->spa_spares.sav_config) != 0)
2125 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2127 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2128 spa_load_spares(spa);
2129 spa_config_exit(spa, SCL_ALL, FTAG);
2130 } else if (error == 0) {
2131 spa->spa_spares.sav_sync = B_TRUE;
2135 * Load any level 2 ARC devices for this pool.
2137 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2138 &spa->spa_l2cache.sav_object);
2139 if (error != 0 && error != ENOENT)
2140 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2141 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2142 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2143 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2144 &spa->spa_l2cache.sav_config) != 0)
2145 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2147 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2148 spa_load_l2cache(spa);
2149 spa_config_exit(spa, SCL_ALL, FTAG);
2150 } else if (error == 0) {
2151 spa->spa_l2cache.sav_sync = B_TRUE;
2154 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2156 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2157 if (error && error != ENOENT)
2158 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2161 uint64_t autoreplace;
2163 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2164 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2165 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2166 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2167 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2168 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2169 &spa->spa_dedup_ditto);
2171 spa->spa_autoreplace = (autoreplace != 0);
2175 * If the 'autoreplace' property is set, then post a resource notifying
2176 * the ZFS DE that it should not issue any faults for unopenable
2177 * devices. We also iterate over the vdevs, and post a sysevent for any
2178 * unopenable vdevs so that the normal autoreplace handler can take
2181 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2182 spa_check_removed(spa->spa_root_vdev);
2184 * For the import case, this is done in spa_import(), because
2185 * at this point we're using the spare definitions from
2186 * the MOS config, not necessarily from the userland config.
2188 if (state != SPA_LOAD_IMPORT) {
2189 spa_aux_check_removed(&spa->spa_spares);
2190 spa_aux_check_removed(&spa->spa_l2cache);
2195 * Load the vdev state for all toplevel vdevs.
2200 * Propagate the leaf DTLs we just loaded all the way up the tree.
2202 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2203 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2204 spa_config_exit(spa, SCL_ALL, FTAG);
2207 * Load the DDTs (dedup tables).
2209 error = ddt_load(spa);
2211 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2213 spa_update_dspace(spa);
2216 * Validate the config, using the MOS config to fill in any
2217 * information which might be missing. If we fail to validate
2218 * the config then declare the pool unfit for use. If we're
2219 * assembling a pool from a split, the log is not transferred
2222 if (type != SPA_IMPORT_ASSEMBLE) {
2225 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2226 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2228 if (!spa_config_valid(spa, nvconfig)) {
2229 nvlist_free(nvconfig);
2230 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2233 nvlist_free(nvconfig);
2236 * Now that we've validate the config, check the state of the
2237 * root vdev. If it can't be opened, it indicates one or
2238 * more toplevel vdevs are faulted.
2240 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2243 if (spa_check_logs(spa)) {
2244 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2245 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2250 * We've successfully opened the pool, verify that we're ready
2251 * to start pushing transactions.
2253 if (state != SPA_LOAD_TRYIMPORT) {
2254 if (error = spa_load_verify(spa))
2255 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2259 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2260 spa->spa_load_max_txg == UINT64_MAX)) {
2262 int need_update = B_FALSE;
2264 ASSERT(state != SPA_LOAD_TRYIMPORT);
2267 * Claim log blocks that haven't been committed yet.
2268 * This must all happen in a single txg.
2269 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2270 * invoked from zil_claim_log_block()'s i/o done callback.
2271 * Price of rollback is that we abandon the log.
2273 spa->spa_claiming = B_TRUE;
2275 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2276 spa_first_txg(spa));
2277 (void) dmu_objset_find(spa_name(spa),
2278 zil_claim, tx, DS_FIND_CHILDREN);
2281 spa->spa_claiming = B_FALSE;
2283 spa_set_log_state(spa, SPA_LOG_GOOD);
2284 spa->spa_sync_on = B_TRUE;
2285 txg_sync_start(spa->spa_dsl_pool);
2288 * Wait for all claims to sync. We sync up to the highest
2289 * claimed log block birth time so that claimed log blocks
2290 * don't appear to be from the future. spa_claim_max_txg
2291 * will have been set for us by either zil_check_log_chain()
2292 * (invoked from spa_check_logs()) or zil_claim() above.
2294 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2297 * If the config cache is stale, or we have uninitialized
2298 * metaslabs (see spa_vdev_add()), then update the config.
2300 * If this is a verbatim import, trust the current
2301 * in-core spa_config and update the disk labels.
2303 if (config_cache_txg != spa->spa_config_txg ||
2304 state == SPA_LOAD_IMPORT ||
2305 state == SPA_LOAD_RECOVER ||
2306 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2307 need_update = B_TRUE;
2309 for (int c = 0; c < rvd->vdev_children; c++)
2310 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2311 need_update = B_TRUE;
2314 * Update the config cache asychronously in case we're the
2315 * root pool, in which case the config cache isn't writable yet.
2318 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2321 * Check all DTLs to see if anything needs resilvering.
2323 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2324 vdev_resilver_needed(rvd, NULL, NULL))
2325 spa_async_request(spa, SPA_ASYNC_RESILVER);
2328 * Delete any inconsistent datasets.
2330 (void) dmu_objset_find(spa_name(spa),
2331 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2334 * Clean up any stale temporary dataset userrefs.
2336 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2343 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2345 int mode = spa->spa_mode;
2348 spa_deactivate(spa);
2350 spa->spa_load_max_txg--;
2352 spa_activate(spa, mode);
2353 spa_async_suspend(spa);
2355 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2359 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2360 uint64_t max_request, int rewind_flags)
2362 nvlist_t *config = NULL;
2363 int load_error, rewind_error;
2364 uint64_t safe_rewind_txg;
2367 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2368 spa->spa_load_max_txg = spa->spa_load_txg;
2369 spa_set_log_state(spa, SPA_LOG_CLEAR);
2371 spa->spa_load_max_txg = max_request;
2374 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2376 if (load_error == 0)
2379 if (spa->spa_root_vdev != NULL)
2380 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2382 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2383 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2385 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2386 nvlist_free(config);
2387 return (load_error);
2390 /* Price of rolling back is discarding txgs, including log */
2391 if (state == SPA_LOAD_RECOVER)
2392 spa_set_log_state(spa, SPA_LOG_CLEAR);
2394 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2395 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2396 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2397 TXG_INITIAL : safe_rewind_txg;
2400 * Continue as long as we're finding errors, we're still within
2401 * the acceptable rewind range, and we're still finding uberblocks
2403 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2404 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2405 if (spa->spa_load_max_txg < safe_rewind_txg)
2406 spa->spa_extreme_rewind = B_TRUE;
2407 rewind_error = spa_load_retry(spa, state, mosconfig);
2410 spa->spa_extreme_rewind = B_FALSE;
2411 spa->spa_load_max_txg = UINT64_MAX;
2413 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2414 spa_config_set(spa, config);
2416 return (state == SPA_LOAD_RECOVER ? rewind_error : load_error);
2422 * The import case is identical to an open except that the configuration is sent
2423 * down from userland, instead of grabbed from the configuration cache. For the
2424 * case of an open, the pool configuration will exist in the
2425 * POOL_STATE_UNINITIALIZED state.
2427 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2428 * the same time open the pool, without having to keep around the spa_t in some
2432 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2436 spa_load_state_t state = SPA_LOAD_OPEN;
2438 int locked = B_FALSE;
2439 int firstopen = B_FALSE;
2444 * As disgusting as this is, we need to support recursive calls to this
2445 * function because dsl_dir_open() is called during spa_load(), and ends
2446 * up calling spa_open() again. The real fix is to figure out how to
2447 * avoid dsl_dir_open() calling this in the first place.
2449 if (mutex_owner(&spa_namespace_lock) != curthread) {
2450 mutex_enter(&spa_namespace_lock);
2454 if ((spa = spa_lookup(pool)) == NULL) {
2456 mutex_exit(&spa_namespace_lock);
2460 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2461 zpool_rewind_policy_t policy;
2465 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2467 if (policy.zrp_request & ZPOOL_DO_REWIND)
2468 state = SPA_LOAD_RECOVER;
2470 spa_activate(spa, spa_mode_global);
2472 if (state != SPA_LOAD_RECOVER)
2473 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2475 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2476 policy.zrp_request);
2478 if (error == EBADF) {
2480 * If vdev_validate() returns failure (indicated by
2481 * EBADF), it indicates that one of the vdevs indicates
2482 * that the pool has been exported or destroyed. If
2483 * this is the case, the config cache is out of sync and
2484 * we should remove the pool from the namespace.
2487 spa_deactivate(spa);
2488 spa_config_sync(spa, B_TRUE, B_TRUE);
2491 mutex_exit(&spa_namespace_lock);
2497 * We can't open the pool, but we still have useful
2498 * information: the state of each vdev after the
2499 * attempted vdev_open(). Return this to the user.
2501 if (config != NULL && spa->spa_config) {
2502 VERIFY(nvlist_dup(spa->spa_config, config,
2504 VERIFY(nvlist_add_nvlist(*config,
2505 ZPOOL_CONFIG_LOAD_INFO,
2506 spa->spa_load_info) == 0);
2509 spa_deactivate(spa);
2510 spa->spa_last_open_failed = error;
2512 mutex_exit(&spa_namespace_lock);
2518 spa_open_ref(spa, tag);
2521 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2524 * If we've recovered the pool, pass back any information we
2525 * gathered while doing the load.
2527 if (state == SPA_LOAD_RECOVER) {
2528 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2529 spa->spa_load_info) == 0);
2533 spa->spa_last_open_failed = 0;
2534 spa->spa_last_ubsync_txg = 0;
2535 spa->spa_load_txg = 0;
2536 mutex_exit(&spa_namespace_lock);
2540 zvol_create_minors(pool);
2551 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2554 return (spa_open_common(name, spapp, tag, policy, config));
2558 spa_open(const char *name, spa_t **spapp, void *tag)
2560 return (spa_open_common(name, spapp, tag, NULL, NULL));
2564 * Lookup the given spa_t, incrementing the inject count in the process,
2565 * preventing it from being exported or destroyed.
2568 spa_inject_addref(char *name)
2572 mutex_enter(&spa_namespace_lock);
2573 if ((spa = spa_lookup(name)) == NULL) {
2574 mutex_exit(&spa_namespace_lock);
2577 spa->spa_inject_ref++;
2578 mutex_exit(&spa_namespace_lock);
2584 spa_inject_delref(spa_t *spa)
2586 mutex_enter(&spa_namespace_lock);
2587 spa->spa_inject_ref--;
2588 mutex_exit(&spa_namespace_lock);
2592 * Add spares device information to the nvlist.
2595 spa_add_spares(spa_t *spa, nvlist_t *config)
2605 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2607 if (spa->spa_spares.sav_count == 0)
2610 VERIFY(nvlist_lookup_nvlist(config,
2611 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2612 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2613 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2615 VERIFY(nvlist_add_nvlist_array(nvroot,
2616 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2617 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2618 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2621 * Go through and find any spares which have since been
2622 * repurposed as an active spare. If this is the case, update
2623 * their status appropriately.
2625 for (i = 0; i < nspares; i++) {
2626 VERIFY(nvlist_lookup_uint64(spares[i],
2627 ZPOOL_CONFIG_GUID, &guid) == 0);
2628 if (spa_spare_exists(guid, &pool, NULL) &&
2630 VERIFY(nvlist_lookup_uint64_array(
2631 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2632 (uint64_t **)&vs, &vsc) == 0);
2633 vs->vs_state = VDEV_STATE_CANT_OPEN;
2634 vs->vs_aux = VDEV_AUX_SPARED;
2641 * Add l2cache device information to the nvlist, including vdev stats.
2644 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2647 uint_t i, j, nl2cache;
2654 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2656 if (spa->spa_l2cache.sav_count == 0)
2659 VERIFY(nvlist_lookup_nvlist(config,
2660 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2661 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2662 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2663 if (nl2cache != 0) {
2664 VERIFY(nvlist_add_nvlist_array(nvroot,
2665 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2666 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2667 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2670 * Update level 2 cache device stats.
2673 for (i = 0; i < nl2cache; i++) {
2674 VERIFY(nvlist_lookup_uint64(l2cache[i],
2675 ZPOOL_CONFIG_GUID, &guid) == 0);
2678 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
2680 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
2681 vd = spa->spa_l2cache.sav_vdevs[j];
2687 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
2688 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
2690 vdev_get_stats(vd, vs);
2696 spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
2702 error = spa_open_common(name, &spa, FTAG, NULL, config);
2706 * This still leaves a window of inconsistency where the spares
2707 * or l2cache devices could change and the config would be
2708 * self-inconsistent.
2710 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
2712 if (*config != NULL) {
2713 uint64_t loadtimes[2];
2715 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
2716 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
2717 VERIFY(nvlist_add_uint64_array(*config,
2718 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
2720 VERIFY(nvlist_add_uint64(*config,
2721 ZPOOL_CONFIG_ERRCOUNT,
2722 spa_get_errlog_size(spa)) == 0);
2724 if (spa_suspended(spa))
2725 VERIFY(nvlist_add_uint64(*config,
2726 ZPOOL_CONFIG_SUSPENDED,
2727 spa->spa_failmode) == 0);
2729 spa_add_spares(spa, *config);
2730 spa_add_l2cache(spa, *config);
2735 * We want to get the alternate root even for faulted pools, so we cheat
2736 * and call spa_lookup() directly.
2740 mutex_enter(&spa_namespace_lock);
2741 spa = spa_lookup(name);
2743 spa_altroot(spa, altroot, buflen);
2747 mutex_exit(&spa_namespace_lock);
2749 spa_altroot(spa, altroot, buflen);
2754 spa_config_exit(spa, SCL_CONFIG, FTAG);
2755 spa_close(spa, FTAG);
2762 * Validate that the auxiliary device array is well formed. We must have an
2763 * array of nvlists, each which describes a valid leaf vdev. If this is an
2764 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
2765 * specified, as long as they are well-formed.
2768 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
2769 spa_aux_vdev_t *sav, const char *config, uint64_t version,
2770 vdev_labeltype_t label)
2777 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2780 * It's acceptable to have no devs specified.
2782 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
2789 * Make sure the pool is formatted with a version that supports this
2792 if (spa_version(spa) < version)
2796 * Set the pending device list so we correctly handle device in-use
2799 sav->sav_pending = dev;
2800 sav->sav_npending = ndev;
2802 for (i = 0; i < ndev; i++) {
2803 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
2807 if (!vd->vdev_ops->vdev_op_leaf) {
2814 * The L2ARC currently only supports disk devices in
2815 * kernel context. For user-level testing, we allow it.
2818 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
2819 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
2826 if ((error = vdev_open(vd)) == 0 &&
2827 (error = vdev_label_init(vd, crtxg, label)) == 0) {
2828 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
2829 vd->vdev_guid) == 0);
2835 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
2842 sav->sav_pending = NULL;
2843 sav->sav_npending = 0;
2848 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
2852 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
2854 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2855 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
2856 VDEV_LABEL_SPARE)) != 0) {
2860 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
2861 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
2862 VDEV_LABEL_L2CACHE));
2866 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
2871 if (sav->sav_config != NULL) {
2877 * Generate new dev list by concatentating with the
2880 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
2881 &olddevs, &oldndevs) == 0);
2883 newdevs = kmem_alloc(sizeof (void *) *
2884 (ndevs + oldndevs), KM_SLEEP);
2885 for (i = 0; i < oldndevs; i++)
2886 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
2888 for (i = 0; i < ndevs; i++)
2889 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
2892 VERIFY(nvlist_remove(sav->sav_config, config,
2893 DATA_TYPE_NVLIST_ARRAY) == 0);
2895 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
2896 config, newdevs, ndevs + oldndevs) == 0);
2897 for (i = 0; i < oldndevs + ndevs; i++)
2898 nvlist_free(newdevs[i]);
2899 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
2902 * Generate a new dev list.
2904 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
2906 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
2912 * Stop and drop level 2 ARC devices
2915 spa_l2cache_drop(spa_t *spa)
2919 spa_aux_vdev_t *sav = &spa->spa_l2cache;
2921 for (i = 0; i < sav->sav_count; i++) {
2924 vd = sav->sav_vdevs[i];
2927 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
2928 pool != 0ULL && l2arc_vdev_present(vd))
2929 l2arc_remove_vdev(vd);
2930 if (vd->vdev_isl2cache)
2931 spa_l2cache_remove(vd);
2932 vdev_clear_stats(vd);
2933 (void) vdev_close(vd);
2941 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
2942 const char *history_str, nvlist_t *zplprops)
2945 char *altroot = NULL;
2950 uint64_t txg = TXG_INITIAL;
2951 nvlist_t **spares, **l2cache;
2952 uint_t nspares, nl2cache;
2953 uint64_t version, obj;
2956 * If this pool already exists, return failure.
2958 mutex_enter(&spa_namespace_lock);
2959 if (spa_lookup(pool) != NULL) {
2960 mutex_exit(&spa_namespace_lock);
2965 * Allocate a new spa_t structure.
2967 (void) nvlist_lookup_string(props,
2968 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2969 spa = spa_add(pool, NULL, altroot);
2970 spa_activate(spa, spa_mode_global);
2972 if (props && (error = spa_prop_validate(spa, props))) {
2973 spa_deactivate(spa);
2975 mutex_exit(&spa_namespace_lock);
2979 if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
2981 version = SPA_VERSION;
2982 ASSERT(version <= SPA_VERSION);
2984 spa->spa_first_txg = txg;
2985 spa->spa_uberblock.ub_txg = txg - 1;
2986 spa->spa_uberblock.ub_version = version;
2987 spa->spa_ubsync = spa->spa_uberblock;
2990 * Create "The Godfather" zio to hold all async IOs
2992 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2993 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2996 * Create the root vdev.
2998 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3000 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3002 ASSERT(error != 0 || rvd != NULL);
3003 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3005 if (error == 0 && !zfs_allocatable_devs(nvroot))
3009 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3010 (error = spa_validate_aux(spa, nvroot, txg,
3011 VDEV_ALLOC_ADD)) == 0) {
3012 for (int c = 0; c < rvd->vdev_children; c++) {
3013 vdev_metaslab_set_size(rvd->vdev_child[c]);
3014 vdev_expand(rvd->vdev_child[c], txg);
3018 spa_config_exit(spa, SCL_ALL, FTAG);
3022 spa_deactivate(spa);
3024 mutex_exit(&spa_namespace_lock);
3029 * Get the list of spares, if specified.
3031 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3032 &spares, &nspares) == 0) {
3033 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3035 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3036 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3037 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3038 spa_load_spares(spa);
3039 spa_config_exit(spa, SCL_ALL, FTAG);
3040 spa->spa_spares.sav_sync = B_TRUE;
3044 * Get the list of level 2 cache devices, if specified.
3046 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3047 &l2cache, &nl2cache) == 0) {
3048 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3049 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3050 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3051 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3052 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3053 spa_load_l2cache(spa);
3054 spa_config_exit(spa, SCL_ALL, FTAG);
3055 spa->spa_l2cache.sav_sync = B_TRUE;
3058 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3059 spa->spa_meta_objset = dp->dp_meta_objset;
3062 * Create DDTs (dedup tables).
3066 spa_update_dspace(spa);
3068 tx = dmu_tx_create_assigned(dp, txg);
3071 * Create the pool config object.
3073 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3074 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3075 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3077 if (zap_add(spa->spa_meta_objset,
3078 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3079 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3080 cmn_err(CE_PANIC, "failed to add pool config");
3083 if (zap_add(spa->spa_meta_objset,
3084 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3085 sizeof (uint64_t), 1, &version, tx) != 0) {
3086 cmn_err(CE_PANIC, "failed to add pool version");
3089 /* Newly created pools with the right version are always deflated. */
3090 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3091 spa->spa_deflate = TRUE;
3092 if (zap_add(spa->spa_meta_objset,
3093 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3094 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3095 cmn_err(CE_PANIC, "failed to add deflate");
3100 * Create the deferred-free bpobj. Turn off compression
3101 * because sync-to-convergence takes longer if the blocksize
3104 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3105 dmu_object_set_compress(spa->spa_meta_objset, obj,
3106 ZIO_COMPRESS_OFF, tx);
3107 if (zap_add(spa->spa_meta_objset,
3108 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3109 sizeof (uint64_t), 1, &obj, tx) != 0) {
3110 cmn_err(CE_PANIC, "failed to add bpobj");
3112 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3113 spa->spa_meta_objset, obj));
3116 * Create the pool's history object.
3118 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3119 spa_history_create_obj(spa, tx);
3122 * Set pool properties.
3124 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3125 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3126 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3127 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3129 if (props != NULL) {
3130 spa_configfile_set(spa, props, B_FALSE);
3131 spa_sync_props(spa, props, tx);
3136 spa->spa_sync_on = B_TRUE;
3137 txg_sync_start(spa->spa_dsl_pool);
3140 * We explicitly wait for the first transaction to complete so that our
3141 * bean counters are appropriately updated.
3143 txg_wait_synced(spa->spa_dsl_pool, txg);
3145 spa_config_sync(spa, B_FALSE, B_TRUE);
3147 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3148 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3149 spa_history_log_version(spa, LOG_POOL_CREATE);
3151 spa->spa_minref = refcount_count(&spa->spa_refcount);
3153 mutex_exit(&spa_namespace_lock);
3161 * Get the root pool information from the root disk, then import the root pool
3162 * during the system boot up time.
3164 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3167 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3170 nvlist_t *nvtop, *nvroot;
3173 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3177 * Add this top-level vdev to the child array.
3179 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3181 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3183 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3186 * Put this pool's top-level vdevs into a root vdev.
3188 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3189 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3190 VDEV_TYPE_ROOT) == 0);
3191 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3192 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3193 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3197 * Replace the existing vdev_tree with the new root vdev in
3198 * this pool's configuration (remove the old, add the new).
3200 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3201 nvlist_free(nvroot);
3206 * Walk the vdev tree and see if we can find a device with "better"
3207 * configuration. A configuration is "better" if the label on that
3208 * device has a more recent txg.
3211 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3213 for (int c = 0; c < vd->vdev_children; c++)
3214 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3216 if (vd->vdev_ops->vdev_op_leaf) {
3220 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3224 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3228 * Do we have a better boot device?
3230 if (label_txg > *txg) {
3239 * Import a root pool.
3241 * For x86. devpath_list will consist of devid and/or physpath name of
3242 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3243 * The GRUB "findroot" command will return the vdev we should boot.
3245 * For Sparc, devpath_list consists the physpath name of the booting device
3246 * no matter the rootpool is a single device pool or a mirrored pool.
3248 * "/pci@1f,0/ide@d/disk@0,0:a"
3251 spa_import_rootpool(char *devpath, char *devid)
3254 vdev_t *rvd, *bvd, *avd = NULL;
3255 nvlist_t *config, *nvtop;
3261 * Read the label from the boot device and generate a configuration.
3263 config = spa_generate_rootconf(devpath, devid, &guid);
3264 #if defined(_OBP) && defined(_KERNEL)
3265 if (config == NULL) {
3266 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3268 get_iscsi_bootpath_phy(devpath);
3269 config = spa_generate_rootconf(devpath, devid, &guid);
3273 if (config == NULL) {
3274 cmn_err(CE_NOTE, "Can not read the pool label from '%s'",
3279 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3281 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3283 mutex_enter(&spa_namespace_lock);
3284 if ((spa = spa_lookup(pname)) != NULL) {
3286 * Remove the existing root pool from the namespace so that we
3287 * can replace it with the correct config we just read in.
3292 spa = spa_add(pname, config, NULL);
3293 spa->spa_is_root = B_TRUE;
3294 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3297 * Build up a vdev tree based on the boot device's label config.
3299 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3301 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3302 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3303 VDEV_ALLOC_ROOTPOOL);
3304 spa_config_exit(spa, SCL_ALL, FTAG);
3306 mutex_exit(&spa_namespace_lock);
3307 nvlist_free(config);
3308 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3314 * Get the boot vdev.
3316 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3317 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3318 (u_longlong_t)guid);
3324 * Determine if there is a better boot device.
3327 spa_alt_rootvdev(rvd, &avd, &txg);
3329 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3330 "try booting from '%s'", avd->vdev_path);
3336 * If the boot device is part of a spare vdev then ensure that
3337 * we're booting off the active spare.
3339 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3340 !bvd->vdev_isspare) {
3341 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3342 "try booting from '%s'",
3344 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3350 spa_history_log_version(spa, LOG_POOL_IMPORT);
3352 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3354 spa_config_exit(spa, SCL_ALL, FTAG);
3355 mutex_exit(&spa_namespace_lock);
3357 nvlist_free(config);
3365 * Import a non-root pool into the system.
3368 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3371 char *altroot = NULL;
3372 spa_load_state_t state = SPA_LOAD_IMPORT;
3373 zpool_rewind_policy_t policy;
3374 uint64_t mode = spa_mode_global;
3375 uint64_t readonly = B_FALSE;
3378 nvlist_t **spares, **l2cache;
3379 uint_t nspares, nl2cache;
3382 * If a pool with this name exists, return failure.
3384 mutex_enter(&spa_namespace_lock);
3385 if (spa_lookup(pool) != NULL) {
3386 mutex_exit(&spa_namespace_lock);
3391 * Create and initialize the spa structure.
3393 (void) nvlist_lookup_string(props,
3394 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3395 (void) nvlist_lookup_uint64(props,
3396 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3399 spa = spa_add(pool, config, altroot);
3400 spa->spa_import_flags = flags;
3403 * Verbatim import - Take a pool and insert it into the namespace
3404 * as if it had been loaded at boot.
3406 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3408 spa_configfile_set(spa, props, B_FALSE);
3410 spa_config_sync(spa, B_FALSE, B_TRUE);
3412 mutex_exit(&spa_namespace_lock);
3413 spa_history_log_version(spa, LOG_POOL_IMPORT);
3418 spa_activate(spa, mode);
3421 * Don't start async tasks until we know everything is healthy.
3423 spa_async_suspend(spa);
3425 zpool_get_rewind_policy(config, &policy);
3426 if (policy.zrp_request & ZPOOL_DO_REWIND)
3427 state = SPA_LOAD_RECOVER;
3430 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3431 * because the user-supplied config is actually the one to trust when
3434 if (state != SPA_LOAD_RECOVER)
3435 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3437 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3438 policy.zrp_request);
3441 * Propagate anything learned while loading the pool and pass it
3442 * back to caller (i.e. rewind info, missing devices, etc).
3444 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3445 spa->spa_load_info) == 0);
3447 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3449 * Toss any existing sparelist, as it doesn't have any validity
3450 * anymore, and conflicts with spa_has_spare().
3452 if (spa->spa_spares.sav_config) {
3453 nvlist_free(spa->spa_spares.sav_config);
3454 spa->spa_spares.sav_config = NULL;
3455 spa_load_spares(spa);
3457 if (spa->spa_l2cache.sav_config) {
3458 nvlist_free(spa->spa_l2cache.sav_config);
3459 spa->spa_l2cache.sav_config = NULL;
3460 spa_load_l2cache(spa);
3463 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3466 error = spa_validate_aux(spa, nvroot, -1ULL,
3469 error = spa_validate_aux(spa, nvroot, -1ULL,
3470 VDEV_ALLOC_L2CACHE);
3471 spa_config_exit(spa, SCL_ALL, FTAG);
3474 spa_configfile_set(spa, props, B_FALSE);
3476 if (error != 0 || (props && spa_writeable(spa) &&
3477 (error = spa_prop_set(spa, props)))) {
3479 spa_deactivate(spa);
3481 mutex_exit(&spa_namespace_lock);
3485 spa_async_resume(spa);
3488 * Override any spares and level 2 cache devices as specified by
3489 * the user, as these may have correct device names/devids, etc.
3491 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3492 &spares, &nspares) == 0) {
3493 if (spa->spa_spares.sav_config)
3494 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3495 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3497 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3498 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3499 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3500 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3501 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3502 spa_load_spares(spa);
3503 spa_config_exit(spa, SCL_ALL, FTAG);
3504 spa->spa_spares.sav_sync = B_TRUE;
3506 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3507 &l2cache, &nl2cache) == 0) {
3508 if (spa->spa_l2cache.sav_config)
3509 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3510 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3512 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3513 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3514 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3515 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3516 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3517 spa_load_l2cache(spa);
3518 spa_config_exit(spa, SCL_ALL, FTAG);
3519 spa->spa_l2cache.sav_sync = B_TRUE;
3523 * Check for any removed devices.
3525 if (spa->spa_autoreplace) {
3526 spa_aux_check_removed(&spa->spa_spares);
3527 spa_aux_check_removed(&spa->spa_l2cache);
3530 if (spa_writeable(spa)) {
3532 * Update the config cache to include the newly-imported pool.
3534 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3538 * It's possible that the pool was expanded while it was exported.
3539 * We kick off an async task to handle this for us.
3541 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3543 mutex_exit(&spa_namespace_lock);
3544 spa_history_log_version(spa, LOG_POOL_IMPORT);
3548 zvol_create_minors(pool);
3555 spa_tryimport(nvlist_t *tryconfig)
3557 nvlist_t *config = NULL;
3563 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
3566 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
3570 * Create and initialize the spa structure.
3572 mutex_enter(&spa_namespace_lock);
3573 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
3574 spa_activate(spa, FREAD);
3577 * Pass off the heavy lifting to spa_load().
3578 * Pass TRUE for mosconfig because the user-supplied config
3579 * is actually the one to trust when doing an import.
3581 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
3584 * If 'tryconfig' was at least parsable, return the current config.
3586 if (spa->spa_root_vdev != NULL) {
3587 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3588 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
3590 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
3592 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
3593 spa->spa_uberblock.ub_timestamp) == 0);
3596 * If the bootfs property exists on this pool then we
3597 * copy it out so that external consumers can tell which
3598 * pools are bootable.
3600 if ((!error || error == EEXIST) && spa->spa_bootfs) {
3601 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3604 * We have to play games with the name since the
3605 * pool was opened as TRYIMPORT_NAME.
3607 if (dsl_dsobj_to_dsname(spa_name(spa),
3608 spa->spa_bootfs, tmpname) == 0) {
3610 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
3612 cp = strchr(tmpname, '/');
3614 (void) strlcpy(dsname, tmpname,
3617 (void) snprintf(dsname, MAXPATHLEN,
3618 "%s/%s", poolname, ++cp);
3620 VERIFY(nvlist_add_string(config,
3621 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
3622 kmem_free(dsname, MAXPATHLEN);
3624 kmem_free(tmpname, MAXPATHLEN);
3628 * Add the list of hot spares and level 2 cache devices.
3630 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3631 spa_add_spares(spa, config);
3632 spa_add_l2cache(spa, config);
3633 spa_config_exit(spa, SCL_CONFIG, FTAG);
3637 spa_deactivate(spa);
3639 mutex_exit(&spa_namespace_lock);
3645 * Pool export/destroy
3647 * The act of destroying or exporting a pool is very simple. We make sure there
3648 * is no more pending I/O and any references to the pool are gone. Then, we
3649 * update the pool state and sync all the labels to disk, removing the
3650 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
3651 * we don't sync the labels or remove the configuration cache.
3654 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
3655 boolean_t force, boolean_t hardforce)
3662 if (!(spa_mode_global & FWRITE))
3665 mutex_enter(&spa_namespace_lock);
3666 if ((spa = spa_lookup(pool)) == NULL) {
3667 mutex_exit(&spa_namespace_lock);
3672 * Put a hold on the pool, drop the namespace lock, stop async tasks,
3673 * reacquire the namespace lock, and see if we can export.
3675 spa_open_ref(spa, FTAG);
3676 mutex_exit(&spa_namespace_lock);
3677 spa_async_suspend(spa);
3678 mutex_enter(&spa_namespace_lock);
3679 spa_close(spa, FTAG);
3682 * The pool will be in core if it's openable,
3683 * in which case we can modify its state.
3685 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
3687 * Objsets may be open only because they're dirty, so we
3688 * have to force it to sync before checking spa_refcnt.
3690 txg_wait_synced(spa->spa_dsl_pool, 0);
3693 * A pool cannot be exported or destroyed if there are active
3694 * references. If we are resetting a pool, allow references by
3695 * fault injection handlers.
3697 if (!spa_refcount_zero(spa) ||
3698 (spa->spa_inject_ref != 0 &&
3699 new_state != POOL_STATE_UNINITIALIZED)) {
3700 spa_async_resume(spa);
3701 mutex_exit(&spa_namespace_lock);
3706 * A pool cannot be exported if it has an active shared spare.
3707 * This is to prevent other pools stealing the active spare
3708 * from an exported pool. At user's own will, such pool can
3709 * be forcedly exported.
3711 if (!force && new_state == POOL_STATE_EXPORTED &&
3712 spa_has_active_shared_spare(spa)) {
3713 spa_async_resume(spa);
3714 mutex_exit(&spa_namespace_lock);
3719 * We want this to be reflected on every label,
3720 * so mark them all dirty. spa_unload() will do the
3721 * final sync that pushes these changes out.
3723 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
3724 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3725 spa->spa_state = new_state;
3726 spa->spa_final_txg = spa_last_synced_txg(spa) +
3728 vdev_config_dirty(spa->spa_root_vdev);
3729 spa_config_exit(spa, SCL_ALL, FTAG);
3733 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
3735 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
3737 spa_deactivate(spa);
3740 if (oldconfig && spa->spa_config)
3741 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
3743 if (new_state != POOL_STATE_UNINITIALIZED) {
3745 spa_config_sync(spa, B_TRUE, B_TRUE);
3748 mutex_exit(&spa_namespace_lock);
3754 * Destroy a storage pool.
3757 spa_destroy(char *pool)
3759 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
3764 * Export a storage pool.
3767 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
3768 boolean_t hardforce)
3770 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
3775 * Similar to spa_export(), this unloads the spa_t without actually removing it
3776 * from the namespace in any way.
3779 spa_reset(char *pool)
3781 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
3786 * ==========================================================================
3787 * Device manipulation
3788 * ==========================================================================
3792 * Add a device to a storage pool.
3795 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
3799 vdev_t *rvd = spa->spa_root_vdev;
3801 nvlist_t **spares, **l2cache;
3802 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 (int 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 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_ADD)) != 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, ESC_ZFS_VDEV_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, ESC_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 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;
4116 ASSERT(spa_writeable(spa));
4118 txg = spa_vdev_enter(spa);
4120 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4123 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4125 if (!vd->vdev_ops->vdev_op_leaf)
4126 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4128 pvd = vd->vdev_parent;
4131 * If the parent/child relationship is not as expected, don't do it.
4132 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4133 * vdev that's replacing B with C. The user's intent in replacing
4134 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4135 * the replace by detaching C, the expected behavior is to end up
4136 * M(A,B). But suppose that right after deciding to detach C,
4137 * the replacement of B completes. We would have M(A,C), and then
4138 * ask to detach C, which would leave us with just A -- not what
4139 * the user wanted. To prevent this, we make sure that the
4140 * parent/child relationship hasn't changed -- in this example,
4141 * that C's parent is still the replacing vdev R.
4143 if (pvd->vdev_guid != pguid && pguid != 0)
4144 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4147 * Only 'replacing' or 'spare' vdevs can be replaced.
4149 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4150 pvd->vdev_ops != &vdev_spare_ops)
4151 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4153 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4154 spa_version(spa) >= SPA_VERSION_SPARES);
4157 * Only mirror, replacing, and spare vdevs support detach.
4159 if (pvd->vdev_ops != &vdev_replacing_ops &&
4160 pvd->vdev_ops != &vdev_mirror_ops &&
4161 pvd->vdev_ops != &vdev_spare_ops)
4162 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4165 * If this device has the only valid copy of some data,
4166 * we cannot safely detach it.
4168 if (vdev_dtl_required(vd))
4169 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4171 ASSERT(pvd->vdev_children >= 2);
4174 * If we are detaching the second disk from a replacing vdev, then
4175 * check to see if we changed the original vdev's path to have "/old"
4176 * at the end in spa_vdev_attach(). If so, undo that change now.
4178 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4179 vd->vdev_path != NULL) {
4180 size_t len = strlen(vd->vdev_path);
4182 for (int c = 0; c < pvd->vdev_children; c++) {
4183 cvd = pvd->vdev_child[c];
4185 if (cvd == vd || cvd->vdev_path == NULL)
4188 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4189 strcmp(cvd->vdev_path + len, "/old") == 0) {
4190 spa_strfree(cvd->vdev_path);
4191 cvd->vdev_path = spa_strdup(vd->vdev_path);
4198 * If we are detaching the original disk from a spare, then it implies
4199 * that the spare should become a real disk, and be removed from the
4200 * active spare list for the pool.
4202 if (pvd->vdev_ops == &vdev_spare_ops &&
4204 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4208 * Erase the disk labels so the disk can be used for other things.
4209 * This must be done after all other error cases are handled,
4210 * but before we disembowel vd (so we can still do I/O to it).
4211 * But if we can't do it, don't treat the error as fatal --
4212 * it may be that the unwritability of the disk is the reason
4213 * it's being detached!
4215 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4218 * Remove vd from its parent and compact the parent's children.
4220 vdev_remove_child(pvd, vd);
4221 vdev_compact_children(pvd);
4224 * Remember one of the remaining children so we can get tvd below.
4226 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4229 * If we need to remove the remaining child from the list of hot spares,
4230 * do it now, marking the vdev as no longer a spare in the process.
4231 * We must do this before vdev_remove_parent(), because that can
4232 * change the GUID if it creates a new toplevel GUID. For a similar
4233 * reason, we must remove the spare now, in the same txg as the detach;
4234 * otherwise someone could attach a new sibling, change the GUID, and
4235 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4238 ASSERT(cvd->vdev_isspare);
4239 spa_spare_remove(cvd);
4240 unspare_guid = cvd->vdev_guid;
4241 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4242 cvd->vdev_unspare = B_TRUE;
4246 * If the parent mirror/replacing vdev only has one child,
4247 * the parent is no longer needed. Remove it from the tree.
4249 if (pvd->vdev_children == 1) {
4250 if (pvd->vdev_ops == &vdev_spare_ops)
4251 cvd->vdev_unspare = B_FALSE;
4252 vdev_remove_parent(cvd);
4253 cvd->vdev_resilvering = B_FALSE;
4258 * We don't set tvd until now because the parent we just removed
4259 * may have been the previous top-level vdev.
4261 tvd = cvd->vdev_top;
4262 ASSERT(tvd->vdev_parent == rvd);
4265 * Reevaluate the parent vdev state.
4267 vdev_propagate_state(cvd);
4270 * If the 'autoexpand' property is set on the pool then automatically
4271 * try to expand the size of the pool. For example if the device we
4272 * just detached was smaller than the others, it may be possible to
4273 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4274 * first so that we can obtain the updated sizes of the leaf vdevs.
4276 if (spa->spa_autoexpand) {
4278 vdev_expand(tvd, txg);
4281 vdev_config_dirty(tvd);
4284 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4285 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4286 * But first make sure we're not on any *other* txg's DTL list, to
4287 * prevent vd from being accessed after it's freed.
4289 vdpath = spa_strdup(vd->vdev_path);
4290 for (int t = 0; t < TXG_SIZE; t++)
4291 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4292 vd->vdev_detached = B_TRUE;
4293 vdev_dirty(tvd, VDD_DTL, vd, txg);
4295 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4297 /* hang on to the spa before we release the lock */
4298 spa_open_ref(spa, FTAG);
4300 error = spa_vdev_exit(spa, vd, txg, 0);
4302 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4304 spa_strfree(vdpath);
4307 * If this was the removal of the original device in a hot spare vdev,
4308 * then we want to go through and remove the device from the hot spare
4309 * list of every other pool.
4312 spa_t *altspa = NULL;
4314 mutex_enter(&spa_namespace_lock);
4315 while ((altspa = spa_next(altspa)) != NULL) {
4316 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4320 spa_open_ref(altspa, FTAG);
4321 mutex_exit(&spa_namespace_lock);
4322 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4323 mutex_enter(&spa_namespace_lock);
4324 spa_close(altspa, FTAG);
4326 mutex_exit(&spa_namespace_lock);
4328 /* search the rest of the vdevs for spares to remove */
4329 spa_vdev_resilver_done(spa);
4332 /* all done with the spa; OK to release */
4333 mutex_enter(&spa_namespace_lock);
4334 spa_close(spa, FTAG);
4335 mutex_exit(&spa_namespace_lock);
4341 * Split a set of devices from their mirrors, and create a new pool from them.
4344 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4345 nvlist_t *props, boolean_t exp)
4348 uint64_t txg, *glist;
4350 uint_t c, children, lastlog;
4351 nvlist_t **child, *nvl, *tmp;
4353 char *altroot = NULL;
4354 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4355 boolean_t activate_slog;
4357 ASSERT(spa_writeable(spa));
4359 txg = spa_vdev_enter(spa);
4361 /* clear the log and flush everything up to now */
4362 activate_slog = spa_passivate_log(spa);
4363 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4364 error = spa_offline_log(spa);
4365 txg = spa_vdev_config_enter(spa);
4368 spa_activate_log(spa);
4371 return (spa_vdev_exit(spa, NULL, txg, error));
4373 /* check new spa name before going any further */
4374 if (spa_lookup(newname) != NULL)
4375 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4378 * scan through all the children to ensure they're all mirrors
4380 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4381 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4383 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4385 /* first, check to ensure we've got the right child count */
4386 rvd = spa->spa_root_vdev;
4388 for (c = 0; c < rvd->vdev_children; c++) {
4389 vdev_t *vd = rvd->vdev_child[c];
4391 /* don't count the holes & logs as children */
4392 if (vd->vdev_islog || vd->vdev_ishole) {
4400 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4401 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4403 /* next, ensure no spare or cache devices are part of the split */
4404 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4405 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4406 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4408 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
4409 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
4411 /* then, loop over each vdev and validate it */
4412 for (c = 0; c < children; c++) {
4413 uint64_t is_hole = 0;
4415 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4419 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4420 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4428 /* which disk is going to be split? */
4429 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4435 /* look it up in the spa */
4436 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4437 if (vml[c] == NULL) {
4442 /* make sure there's nothing stopping the split */
4443 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4444 vml[c]->vdev_islog ||
4445 vml[c]->vdev_ishole ||
4446 vml[c]->vdev_isspare ||
4447 vml[c]->vdev_isl2cache ||
4448 !vdev_writeable(vml[c]) ||
4449 vml[c]->vdev_children != 0 ||
4450 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4451 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4456 if (vdev_dtl_required(vml[c])) {
4461 /* we need certain info from the top level */
4462 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4463 vml[c]->vdev_top->vdev_ms_array) == 0);
4464 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4465 vml[c]->vdev_top->vdev_ms_shift) == 0);
4466 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4467 vml[c]->vdev_top->vdev_asize) == 0);
4468 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4469 vml[c]->vdev_top->vdev_ashift) == 0);
4473 kmem_free(vml, children * sizeof (vdev_t *));
4474 kmem_free(glist, children * sizeof (uint64_t));
4475 return (spa_vdev_exit(spa, NULL, txg, error));
4478 /* stop writers from using the disks */
4479 for (c = 0; c < children; c++) {
4481 vml[c]->vdev_offline = B_TRUE;
4483 vdev_reopen(spa->spa_root_vdev);
4486 * Temporarily record the splitting vdevs in the spa config. This
4487 * will disappear once the config is regenerated.
4489 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4490 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4491 glist, children) == 0);
4492 kmem_free(glist, children * sizeof (uint64_t));
4494 mutex_enter(&spa->spa_props_lock);
4495 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4497 mutex_exit(&spa->spa_props_lock);
4498 spa->spa_config_splitting = nvl;
4499 vdev_config_dirty(spa->spa_root_vdev);
4501 /* configure and create the new pool */
4502 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4503 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4504 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4505 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4506 spa_version(spa)) == 0);
4507 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4508 spa->spa_config_txg) == 0);
4509 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4510 spa_generate_guid(NULL)) == 0);
4511 (void) nvlist_lookup_string(props,
4512 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4514 /* add the new pool to the namespace */
4515 newspa = spa_add(newname, config, altroot);
4516 newspa->spa_config_txg = spa->spa_config_txg;
4517 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4519 /* release the spa config lock, retaining the namespace lock */
4520 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4522 if (zio_injection_enabled)
4523 zio_handle_panic_injection(spa, FTAG, 1);
4525 spa_activate(newspa, spa_mode_global);
4526 spa_async_suspend(newspa);
4529 /* mark that we are creating new spa by splitting */
4530 newspa->spa_splitting_newspa = B_TRUE;
4532 /* create the new pool from the disks of the original pool */
4533 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4535 newspa->spa_splitting_newspa = B_FALSE;
4540 /* if that worked, generate a real config for the new pool */
4541 if (newspa->spa_root_vdev != NULL) {
4542 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4543 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4544 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4545 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4546 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4551 if (props != NULL) {
4552 spa_configfile_set(newspa, props, B_FALSE);
4553 error = spa_prop_set(newspa, props);
4558 /* flush everything */
4559 txg = spa_vdev_config_enter(newspa);
4560 vdev_config_dirty(newspa->spa_root_vdev);
4561 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4563 if (zio_injection_enabled)
4564 zio_handle_panic_injection(spa, FTAG, 2);
4566 spa_async_resume(newspa);
4568 /* finally, update the original pool's config */
4569 txg = spa_vdev_config_enter(spa);
4570 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
4571 error = dmu_tx_assign(tx, TXG_WAIT);
4574 for (c = 0; c < children; c++) {
4575 if (vml[c] != NULL) {
4578 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
4584 vdev_config_dirty(spa->spa_root_vdev);
4585 spa->spa_config_splitting = NULL;
4589 (void) spa_vdev_exit(spa, NULL, txg, 0);
4591 if (zio_injection_enabled)
4592 zio_handle_panic_injection(spa, FTAG, 3);
4594 /* split is complete; log a history record */
4595 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
4596 "split new pool %s from pool %s", newname, spa_name(spa));
4598 kmem_free(vml, children * sizeof (vdev_t *));
4600 /* if we're not going to mount the filesystems in userland, export */
4602 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
4609 spa_deactivate(newspa);
4612 txg = spa_vdev_config_enter(spa);
4614 /* re-online all offlined disks */
4615 for (c = 0; c < children; c++) {
4617 vml[c]->vdev_offline = B_FALSE;
4619 vdev_reopen(spa->spa_root_vdev);
4621 nvlist_free(spa->spa_config_splitting);
4622 spa->spa_config_splitting = NULL;
4623 (void) spa_vdev_exit(spa, NULL, txg, error);
4625 kmem_free(vml, children * sizeof (vdev_t *));
4630 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
4632 for (int i = 0; i < count; i++) {
4635 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
4638 if (guid == target_guid)
4646 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
4647 nvlist_t *dev_to_remove)
4649 nvlist_t **newdev = NULL;
4652 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
4654 for (int i = 0, j = 0; i < count; i++) {
4655 if (dev[i] == dev_to_remove)
4657 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
4660 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
4661 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
4663 for (int i = 0; i < count - 1; i++)
4664 nvlist_free(newdev[i]);
4667 kmem_free(newdev, (count - 1) * sizeof (void *));
4671 * Evacuate the device.
4674 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
4679 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4680 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
4681 ASSERT(vd == vd->vdev_top);
4684 * Evacuate the device. We don't hold the config lock as writer
4685 * since we need to do I/O but we do keep the
4686 * spa_namespace_lock held. Once this completes the device
4687 * should no longer have any blocks allocated on it.
4689 if (vd->vdev_islog) {
4690 if (vd->vdev_stat.vs_alloc != 0)
4691 error = spa_offline_log(spa);
4700 * The evacuation succeeded. Remove any remaining MOS metadata
4701 * associated with this vdev, and wait for these changes to sync.
4703 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
4704 txg = spa_vdev_config_enter(spa);
4705 vd->vdev_removing = B_TRUE;
4706 vdev_dirty(vd, 0, NULL, txg);
4707 vdev_config_dirty(vd);
4708 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4714 * Complete the removal by cleaning up the namespace.
4717 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
4719 vdev_t *rvd = spa->spa_root_vdev;
4720 uint64_t id = vd->vdev_id;
4721 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
4723 ASSERT(MUTEX_HELD(&spa_namespace_lock));
4724 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
4725 ASSERT(vd == vd->vdev_top);
4728 * Only remove any devices which are empty.
4730 if (vd->vdev_stat.vs_alloc != 0)
4733 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4735 if (list_link_active(&vd->vdev_state_dirty_node))
4736 vdev_state_clean(vd);
4737 if (list_link_active(&vd->vdev_config_dirty_node))
4738 vdev_config_clean(vd);
4743 vdev_compact_children(rvd);
4745 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
4746 vdev_add_child(rvd, vd);
4748 vdev_config_dirty(rvd);
4751 * Reassess the health of our root vdev.
4757 * Remove a device from the pool -
4759 * Removing a device from the vdev namespace requires several steps
4760 * and can take a significant amount of time. As a result we use
4761 * the spa_vdev_config_[enter/exit] functions which allow us to
4762 * grab and release the spa_config_lock while still holding the namespace
4763 * lock. During each step the configuration is synced out.
4767 * Remove a device from the pool. Currently, this supports removing only hot
4768 * spares, slogs, and level 2 ARC devices.
4771 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
4774 metaslab_group_t *mg;
4775 nvlist_t **spares, **l2cache, *nv;
4777 uint_t nspares, nl2cache;
4779 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
4781 ASSERT(spa_writeable(spa));
4784 txg = spa_vdev_enter(spa);
4786 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4788 if (spa->spa_spares.sav_vdevs != NULL &&
4789 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
4790 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
4791 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
4793 * Only remove the hot spare if it's not currently in use
4796 if (vd == NULL || unspare) {
4797 spa_vdev_remove_aux(spa->spa_spares.sav_config,
4798 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
4799 spa_load_spares(spa);
4800 spa->spa_spares.sav_sync = B_TRUE;
4804 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
4805 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
4806 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
4807 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
4809 * Cache devices can always be removed.
4811 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
4812 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
4813 spa_load_l2cache(spa);
4814 spa->spa_l2cache.sav_sync = B_TRUE;
4815 } else if (vd != NULL && vd->vdev_islog) {
4817 ASSERT(vd == vd->vdev_top);
4820 * XXX - Once we have bp-rewrite this should
4821 * become the common case.
4827 * Stop allocating from this vdev.
4829 metaslab_group_passivate(mg);
4832 * Wait for the youngest allocations and frees to sync,
4833 * and then wait for the deferral of those frees to finish.
4835 spa_vdev_config_exit(spa, NULL,
4836 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
4839 * Attempt to evacuate the vdev.
4841 error = spa_vdev_remove_evacuate(spa, vd);
4843 txg = spa_vdev_config_enter(spa);
4846 * If we couldn't evacuate the vdev, unwind.
4849 metaslab_group_activate(mg);
4850 return (spa_vdev_exit(spa, NULL, txg, error));
4854 * Clean up the vdev namespace.
4856 spa_vdev_remove_from_namespace(spa, vd);
4858 } else if (vd != NULL) {
4860 * Normal vdevs cannot be removed (yet).
4865 * There is no vdev of any kind with the specified guid.
4871 return (spa_vdev_exit(spa, NULL, txg, error));
4877 * Find any device that's done replacing, or a vdev marked 'unspare' that's
4878 * current spared, so we can detach it.
4881 spa_vdev_resilver_done_hunt(vdev_t *vd)
4883 vdev_t *newvd, *oldvd;
4885 for (int c = 0; c < vd->vdev_children; c++) {
4886 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
4892 * Check for a completed replacement. We always consider the first
4893 * vdev in the list to be the oldest vdev, and the last one to be
4894 * the newest (see spa_vdev_attach() for how that works). In
4895 * the case where the newest vdev is faulted, we will not automatically
4896 * remove it after a resilver completes. This is OK as it will require
4897 * user intervention to determine which disk the admin wishes to keep.
4899 if (vd->vdev_ops == &vdev_replacing_ops) {
4900 ASSERT(vd->vdev_children > 1);
4902 newvd = vd->vdev_child[vd->vdev_children - 1];
4903 oldvd = vd->vdev_child[0];
4905 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
4906 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4907 !vdev_dtl_required(oldvd))
4912 * Check for a completed resilver with the 'unspare' flag set.
4914 if (vd->vdev_ops == &vdev_spare_ops) {
4915 vdev_t *first = vd->vdev_child[0];
4916 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
4918 if (last->vdev_unspare) {
4921 } else if (first->vdev_unspare) {
4928 if (oldvd != NULL &&
4929 vdev_dtl_empty(newvd, DTL_MISSING) &&
4930 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
4931 !vdev_dtl_required(oldvd))
4935 * If there are more than two spares attached to a disk,
4936 * and those spares are not required, then we want to
4937 * attempt to free them up now so that they can be used
4938 * by other pools. Once we're back down to a single
4939 * disk+spare, we stop removing them.
4941 if (vd->vdev_children > 2) {
4942 newvd = vd->vdev_child[1];
4944 if (newvd->vdev_isspare && last->vdev_isspare &&
4945 vdev_dtl_empty(last, DTL_MISSING) &&
4946 vdev_dtl_empty(last, DTL_OUTAGE) &&
4947 !vdev_dtl_required(newvd))
4956 spa_vdev_resilver_done(spa_t *spa)
4958 vdev_t *vd, *pvd, *ppvd;
4959 uint64_t guid, sguid, pguid, ppguid;
4961 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4963 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
4964 pvd = vd->vdev_parent;
4965 ppvd = pvd->vdev_parent;
4966 guid = vd->vdev_guid;
4967 pguid = pvd->vdev_guid;
4968 ppguid = ppvd->vdev_guid;
4971 * If we have just finished replacing a hot spared device, then
4972 * we need to detach the parent's first child (the original hot
4975 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
4976 ppvd->vdev_children == 2) {
4977 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
4978 sguid = ppvd->vdev_child[1]->vdev_guid;
4980 spa_config_exit(spa, SCL_ALL, FTAG);
4981 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
4983 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
4985 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4988 spa_config_exit(spa, SCL_ALL, FTAG);
4992 * Update the stored path or FRU for this vdev.
4995 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
4999 boolean_t sync = B_FALSE;
5001 ASSERT(spa_writeable(spa));
5003 spa_vdev_state_enter(spa, SCL_ALL);
5005 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5006 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5008 if (!vd->vdev_ops->vdev_op_leaf)
5009 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5012 if (strcmp(value, vd->vdev_path) != 0) {
5013 spa_strfree(vd->vdev_path);
5014 vd->vdev_path = spa_strdup(value);
5018 if (vd->vdev_fru == NULL) {
5019 vd->vdev_fru = spa_strdup(value);
5021 } else if (strcmp(value, vd->vdev_fru) != 0) {
5022 spa_strfree(vd->vdev_fru);
5023 vd->vdev_fru = spa_strdup(value);
5028 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5032 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5034 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5038 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5040 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5044 * ==========================================================================
5046 * ==========================================================================
5050 spa_scan_stop(spa_t *spa)
5052 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5053 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5055 return (dsl_scan_cancel(spa->spa_dsl_pool));
5059 spa_scan(spa_t *spa, pool_scan_func_t func)
5061 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5063 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5067 * If a resilver was requested, but there is no DTL on a
5068 * writeable leaf device, we have nothing to do.
5070 if (func == POOL_SCAN_RESILVER &&
5071 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5072 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5076 return (dsl_scan(spa->spa_dsl_pool, func));
5080 * ==========================================================================
5081 * SPA async task processing
5082 * ==========================================================================
5086 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 (int 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)
5113 if (vd->vdev_probe_wanted) {
5114 vd->vdev_probe_wanted = B_FALSE;
5115 vdev_reopen(vd); /* vdev_open() does the actual probe */
5118 for (int c = 0; c < vd->vdev_children; c++)
5119 spa_async_probe(spa, vd->vdev_child[c]);
5123 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5129 if (!spa->spa_autoexpand)
5132 for (int 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 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5141 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5143 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5144 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5146 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5147 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5150 kmem_free(physpath, MAXPATHLEN);
5154 spa_async_thread(void *arg)
5159 ASSERT(spa->spa_sync_on);
5161 mutex_enter(&spa->spa_async_lock);
5162 tasks = spa->spa_async_tasks;
5163 spa->spa_async_tasks = 0;
5164 mutex_exit(&spa->spa_async_lock);
5167 * See if the config needs to be updated.
5169 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5170 uint64_t old_space, new_space;
5172 mutex_enter(&spa_namespace_lock);
5173 old_space = metaslab_class_get_space(spa_normal_class(spa));
5174 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5175 new_space = metaslab_class_get_space(spa_normal_class(spa));
5176 mutex_exit(&spa_namespace_lock);
5179 * If the pool grew as a result of the config update,
5180 * then log an internal history event.
5182 if (new_space != old_space) {
5183 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5185 "pool '%s' size: %llu(+%llu)",
5186 spa_name(spa), new_space, new_space - old_space);
5191 * See if any devices need to be marked REMOVED.
5193 if (tasks & SPA_ASYNC_REMOVE) {
5194 spa_vdev_state_enter(spa, SCL_NONE);
5195 spa_async_remove(spa, spa->spa_root_vdev);
5196 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5197 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5198 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5199 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5200 (void) spa_vdev_state_exit(spa, NULL, 0);
5203 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5204 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5205 spa_async_autoexpand(spa, spa->spa_root_vdev);
5206 spa_config_exit(spa, SCL_CONFIG, FTAG);
5210 * See if any devices need to be probed.
5212 if (tasks & SPA_ASYNC_PROBE) {
5213 spa_vdev_state_enter(spa, SCL_NONE);
5214 spa_async_probe(spa, spa->spa_root_vdev);
5215 (void) spa_vdev_state_exit(spa, NULL, 0);
5219 * If any devices are done replacing, detach them.
5221 if (tasks & SPA_ASYNC_RESILVER_DONE)
5222 spa_vdev_resilver_done(spa);
5225 * Kick off a resilver.
5227 if (tasks & SPA_ASYNC_RESILVER)
5228 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5231 * Let the world know that we're done.
5233 mutex_enter(&spa->spa_async_lock);
5234 spa->spa_async_thread = NULL;
5235 cv_broadcast(&spa->spa_async_cv);
5236 mutex_exit(&spa->spa_async_lock);
5241 spa_async_suspend(spa_t *spa)
5243 mutex_enter(&spa->spa_async_lock);
5244 spa->spa_async_suspended++;
5245 while (spa->spa_async_thread != NULL)
5246 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5247 mutex_exit(&spa->spa_async_lock);
5251 spa_async_resume(spa_t *spa)
5253 mutex_enter(&spa->spa_async_lock);
5254 ASSERT(spa->spa_async_suspended != 0);
5255 spa->spa_async_suspended--;
5256 mutex_exit(&spa->spa_async_lock);
5260 spa_async_dispatch(spa_t *spa)
5262 mutex_enter(&spa->spa_async_lock);
5263 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5264 spa->spa_async_thread == NULL &&
5265 rootdir != NULL && !vn_is_readonly(rootdir))
5266 spa->spa_async_thread = thread_create(NULL, 0,
5267 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5268 mutex_exit(&spa->spa_async_lock);
5272 spa_async_request(spa_t *spa, int task)
5274 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5275 mutex_enter(&spa->spa_async_lock);
5276 spa->spa_async_tasks |= task;
5277 mutex_exit(&spa->spa_async_lock);
5281 * ==========================================================================
5282 * SPA syncing routines
5283 * ==========================================================================
5287 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5290 bpobj_enqueue(bpo, bp, tx);
5295 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5299 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5305 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5307 char *packed = NULL;
5312 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5315 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5316 * information. This avoids the dbuf_will_dirty() path and
5317 * saves us a pre-read to get data we don't actually care about.
5319 bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
5320 packed = kmem_alloc(bufsize, KM_SLEEP);
5322 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5324 bzero(packed + nvsize, bufsize - nvsize);
5326 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5328 kmem_free(packed, bufsize);
5330 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5331 dmu_buf_will_dirty(db, tx);
5332 *(uint64_t *)db->db_data = nvsize;
5333 dmu_buf_rele(db, FTAG);
5337 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5338 const char *config, const char *entry)
5348 * Update the MOS nvlist describing the list of available devices.
5349 * spa_validate_aux() will have already made sure this nvlist is
5350 * valid and the vdevs are labeled appropriately.
5352 if (sav->sav_object == 0) {
5353 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5354 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5355 sizeof (uint64_t), tx);
5356 VERIFY(zap_update(spa->spa_meta_objset,
5357 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5358 &sav->sav_object, tx) == 0);
5361 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5362 if (sav->sav_count == 0) {
5363 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5365 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5366 for (i = 0; i < sav->sav_count; i++)
5367 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5368 B_FALSE, VDEV_CONFIG_L2CACHE);
5369 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5370 sav->sav_count) == 0);
5371 for (i = 0; i < sav->sav_count; i++)
5372 nvlist_free(list[i]);
5373 kmem_free(list, sav->sav_count * sizeof (void *));
5376 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5377 nvlist_free(nvroot);
5379 sav->sav_sync = B_FALSE;
5383 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5387 if (list_is_empty(&spa->spa_config_dirty_list))
5390 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5392 config = spa_config_generate(spa, spa->spa_root_vdev,
5393 dmu_tx_get_txg(tx), B_FALSE);
5395 spa_config_exit(spa, SCL_STATE, FTAG);
5397 if (spa->spa_config_syncing)
5398 nvlist_free(spa->spa_config_syncing);
5399 spa->spa_config_syncing = config;
5401 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5405 * Set zpool properties.
5408 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5411 objset_t *mos = spa->spa_meta_objset;
5412 nvlist_t *nvp = arg2;
5417 const char *propname;
5418 zprop_type_t proptype;
5420 mutex_enter(&spa->spa_props_lock);
5423 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5424 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
5425 case ZPOOL_PROP_VERSION:
5427 * Only set version for non-zpool-creation cases
5428 * (set/import). spa_create() needs special care
5429 * for version setting.
5431 if (tx->tx_txg != TXG_INITIAL) {
5432 VERIFY(nvpair_value_uint64(elem,
5434 ASSERT(intval <= SPA_VERSION);
5435 ASSERT(intval >= spa_version(spa));
5436 spa->spa_uberblock.ub_version = intval;
5437 vdev_config_dirty(spa->spa_root_vdev);
5441 case ZPOOL_PROP_ALTROOT:
5443 * 'altroot' is a non-persistent property. It should
5444 * have been set temporarily at creation or import time.
5446 ASSERT(spa->spa_root != NULL);
5449 case ZPOOL_PROP_READONLY:
5450 case ZPOOL_PROP_CACHEFILE:
5452 * 'readonly' and 'cachefile' are also non-persisitent
5456 case ZPOOL_PROP_COMMENT:
5457 VERIFY(nvpair_value_string(elem, &strval) == 0);
5458 if (spa->spa_comment != NULL)
5459 spa_strfree(spa->spa_comment);
5460 spa->spa_comment = spa_strdup(strval);
5462 * We need to dirty the configuration on all the vdevs
5463 * so that their labels get updated. It's unnecessary
5464 * to do this for pool creation since the vdev's
5465 * configuratoin has already been dirtied.
5467 if (tx->tx_txg != TXG_INITIAL)
5468 vdev_config_dirty(spa->spa_root_vdev);
5472 * Set pool property values in the poolprops mos object.
5474 if (spa->spa_pool_props_object == 0) {
5475 VERIFY((spa->spa_pool_props_object =
5476 zap_create(mos, DMU_OT_POOL_PROPS,
5477 DMU_OT_NONE, 0, tx)) > 0);
5479 VERIFY(zap_update(mos,
5480 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5481 8, 1, &spa->spa_pool_props_object, tx)
5485 /* normalize the property name */
5486 propname = zpool_prop_to_name(prop);
5487 proptype = zpool_prop_get_type(prop);
5489 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5490 ASSERT(proptype == PROP_TYPE_STRING);
5491 VERIFY(nvpair_value_string(elem, &strval) == 0);
5492 VERIFY(zap_update(mos,
5493 spa->spa_pool_props_object, propname,
5494 1, strlen(strval) + 1, strval, tx) == 0);
5496 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5497 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5499 if (proptype == PROP_TYPE_INDEX) {
5501 VERIFY(zpool_prop_index_to_string(
5502 prop, intval, &unused) == 0);
5504 VERIFY(zap_update(mos,
5505 spa->spa_pool_props_object, propname,
5506 8, 1, &intval, tx) == 0);
5508 ASSERT(0); /* not allowed */
5512 case ZPOOL_PROP_DELEGATION:
5513 spa->spa_delegation = intval;
5515 case ZPOOL_PROP_BOOTFS:
5516 spa->spa_bootfs = intval;
5518 case ZPOOL_PROP_FAILUREMODE:
5519 spa->spa_failmode = intval;
5521 case ZPOOL_PROP_AUTOEXPAND:
5522 spa->spa_autoexpand = intval;
5523 if (tx->tx_txg != TXG_INITIAL)
5524 spa_async_request(spa,
5525 SPA_ASYNC_AUTOEXPAND);
5527 case ZPOOL_PROP_DEDUPDITTO:
5528 spa->spa_dedup_ditto = intval;
5535 /* log internal history if this is not a zpool create */
5536 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5537 tx->tx_txg != TXG_INITIAL) {
5538 spa_history_log_internal(LOG_POOL_PROPSET,
5539 spa, tx, "%s %lld %s",
5540 nvpair_name(elem), intval, spa_name(spa));
5544 mutex_exit(&spa->spa_props_lock);
5548 * Perform one-time upgrade on-disk changes. spa_version() does not
5549 * reflect the new version this txg, so there must be no changes this
5550 * txg to anything that the upgrade code depends on after it executes.
5551 * Therefore this must be called after dsl_pool_sync() does the sync
5555 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
5557 dsl_pool_t *dp = spa->spa_dsl_pool;
5559 ASSERT(spa->spa_sync_pass == 1);
5561 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
5562 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
5563 dsl_pool_create_origin(dp, tx);
5565 /* Keeping the origin open increases spa_minref */
5566 spa->spa_minref += 3;
5569 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
5570 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
5571 dsl_pool_upgrade_clones(dp, tx);
5574 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
5575 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
5576 dsl_pool_upgrade_dir_clones(dp, tx);
5578 /* Keeping the freedir open increases spa_minref */
5579 spa->spa_minref += 3;
5584 * Sync the specified transaction group. New blocks may be dirtied as
5585 * part of the process, so we iterate until it converges.
5588 spa_sync(spa_t *spa, uint64_t txg)
5590 dsl_pool_t *dp = spa->spa_dsl_pool;
5591 objset_t *mos = spa->spa_meta_objset;
5592 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
5593 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
5594 vdev_t *rvd = spa->spa_root_vdev;
5599 VERIFY(spa_writeable(spa));
5602 * Lock out configuration changes.
5604 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5606 spa->spa_syncing_txg = txg;
5607 spa->spa_sync_pass = 0;
5610 * If there are any pending vdev state changes, convert them
5611 * into config changes that go out with this transaction group.
5613 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5614 while (list_head(&spa->spa_state_dirty_list) != NULL) {
5616 * We need the write lock here because, for aux vdevs,
5617 * calling vdev_config_dirty() modifies sav_config.
5618 * This is ugly and will become unnecessary when we
5619 * eliminate the aux vdev wart by integrating all vdevs
5620 * into the root vdev tree.
5622 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5623 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
5624 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
5625 vdev_state_clean(vd);
5626 vdev_config_dirty(vd);
5628 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
5629 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
5631 spa_config_exit(spa, SCL_STATE, FTAG);
5633 tx = dmu_tx_create_assigned(dp, txg);
5636 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
5637 * set spa_deflate if we have no raid-z vdevs.
5639 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
5640 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
5643 for (i = 0; i < rvd->vdev_children; i++) {
5644 vd = rvd->vdev_child[i];
5645 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
5648 if (i == rvd->vdev_children) {
5649 spa->spa_deflate = TRUE;
5650 VERIFY(0 == zap_add(spa->spa_meta_objset,
5651 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
5652 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
5657 * If anything has changed in this txg, or if someone is waiting
5658 * for this txg to sync (eg, spa_vdev_remove()), push the
5659 * deferred frees from the previous txg. If not, leave them
5660 * alone so that we don't generate work on an otherwise idle
5663 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
5664 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
5665 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
5666 ((dsl_scan_active(dp->dp_scan) ||
5667 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
5668 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5669 VERIFY3U(bpobj_iterate(defer_bpo,
5670 spa_free_sync_cb, zio, tx), ==, 0);
5671 VERIFY3U(zio_wait(zio), ==, 0);
5675 * Iterate to convergence.
5678 int pass = ++spa->spa_sync_pass;
5680 spa_sync_config_object(spa, tx);
5681 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
5682 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
5683 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
5684 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
5685 spa_errlog_sync(spa, txg);
5686 dsl_pool_sync(dp, txg);
5688 if (pass <= SYNC_PASS_DEFERRED_FREE) {
5689 zio_t *zio = zio_root(spa, NULL, NULL, 0);
5690 bplist_iterate(free_bpl, spa_free_sync_cb,
5692 VERIFY(zio_wait(zio) == 0);
5694 bplist_iterate(free_bpl, bpobj_enqueue_cb,
5699 dsl_scan_sync(dp, tx);
5701 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
5705 spa_sync_upgrades(spa, tx);
5707 } while (dmu_objset_is_dirty(mos, txg));
5710 * Rewrite the vdev configuration (which includes the uberblock)
5711 * to commit the transaction group.
5713 * If there are no dirty vdevs, we sync the uberblock to a few
5714 * random top-level vdevs that are known to be visible in the
5715 * config cache (see spa_vdev_add() for a complete description).
5716 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
5720 * We hold SCL_STATE to prevent vdev open/close/etc.
5721 * while we're attempting to write the vdev labels.
5723 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5725 if (list_is_empty(&spa->spa_config_dirty_list)) {
5726 vdev_t *svd[SPA_DVAS_PER_BP];
5728 int children = rvd->vdev_children;
5729 int c0 = spa_get_random(children);
5731 for (int c = 0; c < children; c++) {
5732 vd = rvd->vdev_child[(c0 + c) % children];
5733 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
5735 svd[svdcount++] = vd;
5736 if (svdcount == SPA_DVAS_PER_BP)
5739 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
5741 error = vdev_config_sync(svd, svdcount, txg,
5744 error = vdev_config_sync(rvd->vdev_child,
5745 rvd->vdev_children, txg, B_FALSE);
5747 error = vdev_config_sync(rvd->vdev_child,
5748 rvd->vdev_children, txg, B_TRUE);
5751 spa_config_exit(spa, SCL_STATE, FTAG);
5755 zio_suspend(spa, NULL);
5756 zio_resume_wait(spa);
5761 * Clear the dirty config list.
5763 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
5764 vdev_config_clean(vd);
5767 * Now that the new config has synced transactionally,
5768 * let it become visible to the config cache.
5770 if (spa->spa_config_syncing != NULL) {
5771 spa_config_set(spa, spa->spa_config_syncing);
5772 spa->spa_config_txg = txg;
5773 spa->spa_config_syncing = NULL;
5776 spa->spa_ubsync = spa->spa_uberblock;
5778 dsl_pool_sync_done(dp, txg);
5781 * Update usable space statistics.
5783 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
5784 vdev_sync_done(vd, txg);
5786 spa_update_dspace(spa);
5789 * It had better be the case that we didn't dirty anything
5790 * since vdev_config_sync().
5792 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
5793 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
5794 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
5796 spa->spa_sync_pass = 0;
5798 spa_config_exit(spa, SCL_CONFIG, FTAG);
5800 spa_handle_ignored_writes(spa);
5803 * If any async tasks have been requested, kick them off.
5805 spa_async_dispatch(spa);
5809 * Sync all pools. We don't want to hold the namespace lock across these
5810 * operations, so we take a reference on the spa_t and drop the lock during the
5814 spa_sync_allpools(void)
5817 mutex_enter(&spa_namespace_lock);
5818 while ((spa = spa_next(spa)) != NULL) {
5819 if (spa_state(spa) != POOL_STATE_ACTIVE ||
5820 !spa_writeable(spa) || spa_suspended(spa))
5822 spa_open_ref(spa, FTAG);
5823 mutex_exit(&spa_namespace_lock);
5824 txg_wait_synced(spa_get_dsl(spa), 0);
5825 mutex_enter(&spa_namespace_lock);
5826 spa_close(spa, FTAG);
5828 mutex_exit(&spa_namespace_lock);
5832 * ==========================================================================
5833 * Miscellaneous routines
5834 * ==========================================================================
5838 * Remove all pools in the system.
5846 * Remove all cached state. All pools should be closed now,
5847 * so every spa in the AVL tree should be unreferenced.
5849 mutex_enter(&spa_namespace_lock);
5850 while ((spa = spa_next(NULL)) != NULL) {
5852 * Stop async tasks. The async thread may need to detach
5853 * a device that's been replaced, which requires grabbing
5854 * spa_namespace_lock, so we must drop it here.
5856 spa_open_ref(spa, FTAG);
5857 mutex_exit(&spa_namespace_lock);
5858 spa_async_suspend(spa);
5859 mutex_enter(&spa_namespace_lock);
5860 spa_close(spa, FTAG);
5862 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
5864 spa_deactivate(spa);
5868 mutex_exit(&spa_namespace_lock);
5872 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
5877 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
5881 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
5882 vd = spa->spa_l2cache.sav_vdevs[i];
5883 if (vd->vdev_guid == guid)
5887 for (i = 0; i < spa->spa_spares.sav_count; i++) {
5888 vd = spa->spa_spares.sav_vdevs[i];
5889 if (vd->vdev_guid == guid)
5898 spa_upgrade(spa_t *spa, uint64_t version)
5900 ASSERT(spa_writeable(spa));
5902 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5905 * This should only be called for a non-faulted pool, and since a
5906 * future version would result in an unopenable pool, this shouldn't be
5909 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
5910 ASSERT(version >= spa->spa_uberblock.ub_version);
5912 spa->spa_uberblock.ub_version = version;
5913 vdev_config_dirty(spa->spa_root_vdev);
5915 spa_config_exit(spa, SCL_ALL, FTAG);
5917 txg_wait_synced(spa_get_dsl(spa), 0);
5921 spa_has_spare(spa_t *spa, uint64_t guid)
5925 spa_aux_vdev_t *sav = &spa->spa_spares;
5927 for (i = 0; i < sav->sav_count; i++)
5928 if (sav->sav_vdevs[i]->vdev_guid == guid)
5931 for (i = 0; i < sav->sav_npending; i++) {
5932 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
5933 &spareguid) == 0 && spareguid == guid)
5941 * Check if a pool has an active shared spare device.
5942 * Note: reference count of an active spare is 2, as a spare and as a replace
5945 spa_has_active_shared_spare(spa_t *spa)
5949 spa_aux_vdev_t *sav = &spa->spa_spares;
5951 for (i = 0; i < sav->sav_count; i++) {
5952 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
5953 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
5962 * Post a sysevent corresponding to the given event. The 'name' must be one of
5963 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
5964 * filled in from the spa and (optionally) the vdev. This doesn't do anything
5965 * in the userland libzpool, as we don't want consumers to misinterpret ztest
5966 * or zdb as real changes.
5969 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
5973 sysevent_attr_list_t *attr = NULL;
5974 sysevent_value_t value;
5977 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
5980 value.value_type = SE_DATA_TYPE_STRING;
5981 value.value.sv_string = spa_name(spa);
5982 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
5985 value.value_type = SE_DATA_TYPE_UINT64;
5986 value.value.sv_uint64 = spa_guid(spa);
5987 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
5991 value.value_type = SE_DATA_TYPE_UINT64;
5992 value.value.sv_uint64 = vd->vdev_guid;
5993 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
5997 if (vd->vdev_path) {
5998 value.value_type = SE_DATA_TYPE_STRING;
5999 value.value.sv_string = vd->vdev_path;
6000 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6001 &value, SE_SLEEP) != 0)
6006 if (sysevent_attach_attributes(ev, attr) != 0)
6010 (void) log_sysevent(ev, SE_SLEEP, &eid);
6014 sysevent_free_attr(attr);