4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012 by Delphix. All rights reserved.
28 * This file contains all the routines used when modifying on-disk SPA state.
29 * This includes opening, importing, destroying, exporting a pool, and syncing a
33 #include <sys/zfs_context.h>
34 #include <sys/fm/fs/zfs.h>
35 #include <sys/spa_impl.h>
37 #include <sys/zio_checksum.h>
39 #include <sys/dmu_tx.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/metaslab.h>
45 #include <sys/metaslab_impl.h>
46 #include <sys/uberblock_impl.h>
49 #include <sys/dmu_traverse.h>
50 #include <sys/dmu_objset.h>
51 #include <sys/unique.h>
52 #include <sys/dsl_pool.h>
53 #include <sys/dsl_dataset.h>
54 #include <sys/dsl_dir.h>
55 #include <sys/dsl_prop.h>
56 #include <sys/dsl_synctask.h>
57 #include <sys/fs/zfs.h>
59 #include <sys/callb.h>
60 #include <sys/spa_boot.h>
61 #include <sys/zfs_ioctl.h>
62 #include <sys/dsl_scan.h>
63 #include <sys/zfeature.h>
67 #include <sys/callb.h>
68 #include <sys/cpupart.h>
73 #include "zfs_comutil.h"
75 /* Check hostid on import? */
76 static int check_hostid = 1;
78 SYSCTL_DECL(_vfs_zfs);
79 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
80 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
81 "Check hostid on import?");
83 typedef enum zti_modes {
84 zti_mode_fixed, /* value is # of threads (min 1) */
85 zti_mode_online_percent, /* value is % of online CPUs */
86 zti_mode_batch, /* cpu-intensive; value is ignored */
87 zti_mode_null, /* don't create a taskq */
91 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
92 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
93 #define ZTI_BATCH { zti_mode_batch, 0 }
94 #define ZTI_NULL { zti_mode_null, 0 }
96 #define ZTI_ONE ZTI_FIX(1)
98 typedef struct zio_taskq_info {
99 enum zti_modes zti_mode;
103 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
104 "issue", "issue_high", "intr", "intr_high"
108 * Define the taskq threads for the following I/O types:
109 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
111 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
112 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
113 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
114 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
115 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
116 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
117 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
118 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
121 static dsl_syncfunc_t spa_sync_version;
122 static dsl_syncfunc_t spa_sync_props;
123 static dsl_checkfunc_t spa_change_guid_check;
124 static dsl_syncfunc_t spa_change_guid_sync;
125 static boolean_t spa_has_active_shared_spare(spa_t *spa);
126 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
127 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
129 static void spa_vdev_resilver_done(spa_t *spa);
131 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
133 id_t zio_taskq_psrset_bind = PS_NONE;
136 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
138 uint_t zio_taskq_basedc = 80; /* base duty cycle */
140 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
143 * This (illegal) pool name is used when temporarily importing a spa_t in order
144 * to get the vdev stats associated with the imported devices.
146 #define TRYIMPORT_NAME "$import"
149 * ==========================================================================
150 * SPA properties routines
151 * ==========================================================================
155 * Add a (source=src, propname=propval) list to an nvlist.
158 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
159 uint64_t intval, zprop_source_t src)
161 const char *propname = zpool_prop_to_name(prop);
164 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
165 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
168 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
170 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
172 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
173 nvlist_free(propval);
177 * Get property values from the spa configuration.
180 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
182 vdev_t *rvd = spa->spa_root_vdev;
183 dsl_pool_t *pool = spa->spa_dsl_pool;
187 uint64_t cap, version;
188 zprop_source_t src = ZPROP_SRC_NONE;
189 spa_config_dirent_t *dp;
191 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
194 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
195 size = metaslab_class_get_space(spa_normal_class(spa));
196 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
197 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
198 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
199 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
203 for (int c = 0; c < rvd->vdev_children; c++) {
204 vdev_t *tvd = rvd->vdev_child[c];
205 space += tvd->vdev_max_asize - tvd->vdev_asize;
207 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
210 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
211 (spa_mode(spa) == FREAD), src);
213 cap = (size == 0) ? 0 : (alloc * 100 / size);
214 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
216 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
217 ddt_get_pool_dedup_ratio(spa), src);
219 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
220 rvd->vdev_state, src);
222 version = spa_version(spa);
223 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
224 src = ZPROP_SRC_DEFAULT;
226 src = ZPROP_SRC_LOCAL;
227 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
231 dsl_dir_t *freedir = pool->dp_free_dir;
234 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
235 * when opening pools before this version freedir will be NULL.
237 if (freedir != NULL) {
238 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
239 freedir->dd_phys->dd_used_bytes, src);
241 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
246 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
248 if (spa->spa_comment != NULL) {
249 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
253 if (spa->spa_root != NULL)
254 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
257 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
258 if (dp->scd_path == NULL) {
259 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
260 "none", 0, ZPROP_SRC_LOCAL);
261 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
262 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
263 dp->scd_path, 0, ZPROP_SRC_LOCAL);
269 * Get zpool property values.
272 spa_prop_get(spa_t *spa, nvlist_t **nvp)
274 objset_t *mos = spa->spa_meta_objset;
279 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
281 mutex_enter(&spa->spa_props_lock);
284 * Get properties from the spa config.
286 spa_prop_get_config(spa, nvp);
288 /* If no pool property object, no more prop to get. */
289 if (mos == NULL || spa->spa_pool_props_object == 0) {
290 mutex_exit(&spa->spa_props_lock);
295 * Get properties from the MOS pool property object.
297 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
298 (err = zap_cursor_retrieve(&zc, &za)) == 0;
299 zap_cursor_advance(&zc)) {
302 zprop_source_t src = ZPROP_SRC_DEFAULT;
305 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
308 switch (za.za_integer_length) {
310 /* integer property */
311 if (za.za_first_integer !=
312 zpool_prop_default_numeric(prop))
313 src = ZPROP_SRC_LOCAL;
315 if (prop == ZPOOL_PROP_BOOTFS) {
317 dsl_dataset_t *ds = NULL;
319 dp = spa_get_dsl(spa);
320 rw_enter(&dp->dp_config_rwlock, RW_READER);
321 if (err = dsl_dataset_hold_obj(dp,
322 za.za_first_integer, FTAG, &ds)) {
323 rw_exit(&dp->dp_config_rwlock);
328 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
330 dsl_dataset_name(ds, strval);
331 dsl_dataset_rele(ds, FTAG);
332 rw_exit(&dp->dp_config_rwlock);
335 intval = za.za_first_integer;
338 spa_prop_add_list(*nvp, prop, strval, intval, src);
342 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
347 /* string property */
348 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
349 err = zap_lookup(mos, spa->spa_pool_props_object,
350 za.za_name, 1, za.za_num_integers, strval);
352 kmem_free(strval, za.za_num_integers);
355 spa_prop_add_list(*nvp, prop, strval, 0, src);
356 kmem_free(strval, za.za_num_integers);
363 zap_cursor_fini(&zc);
364 mutex_exit(&spa->spa_props_lock);
366 if (err && err != ENOENT) {
376 * Validate the given pool properties nvlist and modify the list
377 * for the property values to be set.
380 spa_prop_validate(spa_t *spa, nvlist_t *props)
383 int error = 0, reset_bootfs = 0;
385 boolean_t has_feature = B_FALSE;
388 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
390 char *strval, *slash, *check, *fname;
391 const char *propname = nvpair_name(elem);
392 zpool_prop_t prop = zpool_name_to_prop(propname);
396 if (!zpool_prop_feature(propname)) {
402 * Sanitize the input.
404 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
409 if (nvpair_value_uint64(elem, &intval) != 0) {
419 fname = strchr(propname, '@') + 1;
420 if (zfeature_lookup_name(fname, NULL) != 0) {
425 has_feature = B_TRUE;
428 case ZPOOL_PROP_VERSION:
429 error = nvpair_value_uint64(elem, &intval);
431 (intval < spa_version(spa) ||
432 intval > SPA_VERSION_BEFORE_FEATURES ||
437 case ZPOOL_PROP_DELEGATION:
438 case ZPOOL_PROP_AUTOREPLACE:
439 case ZPOOL_PROP_LISTSNAPS:
440 case ZPOOL_PROP_AUTOEXPAND:
441 error = nvpair_value_uint64(elem, &intval);
442 if (!error && intval > 1)
446 case ZPOOL_PROP_BOOTFS:
448 * If the pool version is less than SPA_VERSION_BOOTFS,
449 * or the pool is still being created (version == 0),
450 * the bootfs property cannot be set.
452 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
458 * Make sure the vdev config is bootable
460 if (!vdev_is_bootable(spa->spa_root_vdev)) {
467 error = nvpair_value_string(elem, &strval);
473 if (strval == NULL || strval[0] == '\0') {
474 objnum = zpool_prop_default_numeric(
479 if (error = dmu_objset_hold(strval, FTAG, &os))
482 /* Must be ZPL and not gzip compressed. */
484 if (dmu_objset_type(os) != DMU_OST_ZFS) {
486 } else if ((error = dsl_prop_get_integer(strval,
487 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
488 &compress, NULL)) == 0 &&
489 !BOOTFS_COMPRESS_VALID(compress)) {
492 objnum = dmu_objset_id(os);
494 dmu_objset_rele(os, FTAG);
498 case ZPOOL_PROP_FAILUREMODE:
499 error = nvpair_value_uint64(elem, &intval);
500 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
501 intval > ZIO_FAILURE_MODE_PANIC))
505 * This is a special case which only occurs when
506 * the pool has completely failed. This allows
507 * the user to change the in-core failmode property
508 * without syncing it out to disk (I/Os might
509 * currently be blocked). We do this by returning
510 * EIO to the caller (spa_prop_set) to trick it
511 * into thinking we encountered a property validation
514 if (!error && spa_suspended(spa)) {
515 spa->spa_failmode = intval;
520 case ZPOOL_PROP_CACHEFILE:
521 if ((error = nvpair_value_string(elem, &strval)) != 0)
524 if (strval[0] == '\0')
527 if (strcmp(strval, "none") == 0)
530 if (strval[0] != '/') {
535 slash = strrchr(strval, '/');
536 ASSERT(slash != NULL);
538 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
539 strcmp(slash, "/..") == 0)
543 case ZPOOL_PROP_COMMENT:
544 if ((error = nvpair_value_string(elem, &strval)) != 0)
546 for (check = strval; *check != '\0'; check++) {
548 * The kernel doesn't have an easy isprint()
549 * check. For this kernel check, we merely
550 * check ASCII apart from DEL. Fix this if
551 * there is an easy-to-use kernel isprint().
553 if (*check >= 0x7f) {
559 if (strlen(strval) > ZPROP_MAX_COMMENT)
563 case ZPOOL_PROP_DEDUPDITTO:
564 if (spa_version(spa) < SPA_VERSION_DEDUP)
567 error = nvpair_value_uint64(elem, &intval);
569 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
578 if (!error && reset_bootfs) {
579 error = nvlist_remove(props,
580 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
583 error = nvlist_add_uint64(props,
584 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
592 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
595 spa_config_dirent_t *dp;
597 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
601 dp = kmem_alloc(sizeof (spa_config_dirent_t),
604 if (cachefile[0] == '\0')
605 dp->scd_path = spa_strdup(spa_config_path);
606 else if (strcmp(cachefile, "none") == 0)
609 dp->scd_path = spa_strdup(cachefile);
611 list_insert_head(&spa->spa_config_list, dp);
613 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
617 spa_prop_set(spa_t *spa, nvlist_t *nvp)
620 nvpair_t *elem = NULL;
621 boolean_t need_sync = B_FALSE;
623 if ((error = spa_prop_validate(spa, nvp)) != 0)
626 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
627 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
629 if (prop == ZPOOL_PROP_CACHEFILE ||
630 prop == ZPOOL_PROP_ALTROOT ||
631 prop == ZPOOL_PROP_READONLY)
634 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
637 if (prop == ZPOOL_PROP_VERSION) {
638 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
640 ASSERT(zpool_prop_feature(nvpair_name(elem)));
641 ver = SPA_VERSION_FEATURES;
645 /* Save time if the version is already set. */
646 if (ver == spa_version(spa))
650 * In addition to the pool directory object, we might
651 * create the pool properties object, the features for
652 * read object, the features for write object, or the
653 * feature descriptions object.
655 error = dsl_sync_task_do(spa_get_dsl(spa), NULL,
656 spa_sync_version, spa, &ver, 6);
667 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
675 * If the bootfs property value is dsobj, clear it.
678 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
680 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
681 VERIFY(zap_remove(spa->spa_meta_objset,
682 spa->spa_pool_props_object,
683 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
690 spa_change_guid_check(void *arg1, void *arg2, dmu_tx_t *tx)
693 uint64_t *newguid = arg2;
694 vdev_t *rvd = spa->spa_root_vdev;
697 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
698 vdev_state = rvd->vdev_state;
699 spa_config_exit(spa, SCL_STATE, FTAG);
701 if (vdev_state != VDEV_STATE_HEALTHY)
704 ASSERT3U(spa_guid(spa), !=, *newguid);
710 spa_change_guid_sync(void *arg1, void *arg2, dmu_tx_t *tx)
713 uint64_t *newguid = arg2;
715 vdev_t *rvd = spa->spa_root_vdev;
717 oldguid = spa_guid(spa);
719 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
720 rvd->vdev_guid = *newguid;
721 rvd->vdev_guid_sum += (*newguid - oldguid);
722 vdev_config_dirty(rvd);
723 spa_config_exit(spa, SCL_STATE, FTAG);
727 * TODO: until recent illumos logging changes are merged
728 * log reguid as pool property change
730 spa_history_log_internal(LOG_POOL_PROPSET, spa, tx,
731 "guid change old=%llu new=%llu", oldguid, *newguid);
733 spa_history_log_internal(spa, "guid change", tx, "old=%lld new=%lld",
739 * Change the GUID for the pool. This is done so that we can later
740 * re-import a pool built from a clone of our own vdevs. We will modify
741 * the root vdev's guid, our own pool guid, and then mark all of our
742 * vdevs dirty. Note that we must make sure that all our vdevs are
743 * online when we do this, or else any vdevs that weren't present
744 * would be orphaned from our pool. We are also going to issue a
745 * sysevent to update any watchers.
748 spa_change_guid(spa_t *spa)
753 mutex_enter(&spa_namespace_lock);
754 guid = spa_generate_guid(NULL);
756 error = dsl_sync_task_do(spa_get_dsl(spa), spa_change_guid_check,
757 spa_change_guid_sync, spa, &guid, 5);
760 spa_config_sync(spa, B_FALSE, B_TRUE);
761 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
764 mutex_exit(&spa_namespace_lock);
770 * ==========================================================================
771 * SPA state manipulation (open/create/destroy/import/export)
772 * ==========================================================================
776 spa_error_entry_compare(const void *a, const void *b)
778 spa_error_entry_t *sa = (spa_error_entry_t *)a;
779 spa_error_entry_t *sb = (spa_error_entry_t *)b;
782 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
783 sizeof (zbookmark_t));
794 * Utility function which retrieves copies of the current logs and
795 * re-initializes them in the process.
798 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
800 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
802 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
803 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
805 avl_create(&spa->spa_errlist_scrub,
806 spa_error_entry_compare, sizeof (spa_error_entry_t),
807 offsetof(spa_error_entry_t, se_avl));
808 avl_create(&spa->spa_errlist_last,
809 spa_error_entry_compare, sizeof (spa_error_entry_t),
810 offsetof(spa_error_entry_t, se_avl));
814 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
817 uint_t flags = TASKQ_PREPOPULATE;
818 boolean_t batch = B_FALSE;
822 return (NULL); /* no taskq needed */
825 ASSERT3U(value, >=, 1);
826 value = MAX(value, 1);
831 flags |= TASKQ_THREADS_CPU_PCT;
832 value = zio_taskq_batch_pct;
835 case zti_mode_online_percent:
836 flags |= TASKQ_THREADS_CPU_PCT;
840 panic("unrecognized mode for %s taskq (%u:%u) in "
847 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
849 flags |= TASKQ_DC_BATCH;
851 return (taskq_create_sysdc(name, value, 50, INT_MAX,
852 spa->spa_proc, zio_taskq_basedc, flags));
855 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
856 spa->spa_proc, flags));
860 spa_create_zio_taskqs(spa_t *spa)
862 for (int t = 0; t < ZIO_TYPES; t++) {
863 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
864 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
865 enum zti_modes mode = ztip->zti_mode;
866 uint_t value = ztip->zti_value;
869 (void) snprintf(name, sizeof (name),
870 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
872 spa->spa_zio_taskq[t][q] =
873 spa_taskq_create(spa, name, mode, value);
881 spa_thread(void *arg)
886 user_t *pu = PTOU(curproc);
888 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
891 ASSERT(curproc != &p0);
892 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
893 "zpool-%s", spa->spa_name);
894 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
897 /* bind this thread to the requested psrset */
898 if (zio_taskq_psrset_bind != PS_NONE) {
900 mutex_enter(&cpu_lock);
901 mutex_enter(&pidlock);
902 mutex_enter(&curproc->p_lock);
904 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
905 0, NULL, NULL) == 0) {
906 curthread->t_bind_pset = zio_taskq_psrset_bind;
909 "Couldn't bind process for zfs pool \"%s\" to "
910 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
913 mutex_exit(&curproc->p_lock);
914 mutex_exit(&pidlock);
915 mutex_exit(&cpu_lock);
921 if (zio_taskq_sysdc) {
922 sysdc_thread_enter(curthread, 100, 0);
926 spa->spa_proc = curproc;
927 spa->spa_did = curthread->t_did;
929 spa_create_zio_taskqs(spa);
931 mutex_enter(&spa->spa_proc_lock);
932 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
934 spa->spa_proc_state = SPA_PROC_ACTIVE;
935 cv_broadcast(&spa->spa_proc_cv);
937 CALLB_CPR_SAFE_BEGIN(&cprinfo);
938 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
939 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
940 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
942 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
943 spa->spa_proc_state = SPA_PROC_GONE;
945 cv_broadcast(&spa->spa_proc_cv);
946 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
948 mutex_enter(&curproc->p_lock);
951 #endif /* SPA_PROCESS */
955 * Activate an uninitialized pool.
958 spa_activate(spa_t *spa, int mode)
960 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
962 spa->spa_state = POOL_STATE_ACTIVE;
963 spa->spa_mode = mode;
965 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
966 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
968 /* Try to create a covering process */
969 mutex_enter(&spa->spa_proc_lock);
970 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
971 ASSERT(spa->spa_proc == &p0);
975 /* Only create a process if we're going to be around a while. */
976 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
977 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
979 spa->spa_proc_state = SPA_PROC_CREATED;
980 while (spa->spa_proc_state == SPA_PROC_CREATED) {
981 cv_wait(&spa->spa_proc_cv,
982 &spa->spa_proc_lock);
984 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
985 ASSERT(spa->spa_proc != &p0);
986 ASSERT(spa->spa_did != 0);
990 "Couldn't create process for zfs pool \"%s\"\n",
995 #endif /* SPA_PROCESS */
996 mutex_exit(&spa->spa_proc_lock);
998 /* If we didn't create a process, we need to create our taskqs. */
999 ASSERT(spa->spa_proc == &p0);
1000 if (spa->spa_proc == &p0) {
1001 spa_create_zio_taskqs(spa);
1004 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1005 offsetof(vdev_t, vdev_config_dirty_node));
1006 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1007 offsetof(vdev_t, vdev_state_dirty_node));
1009 txg_list_create(&spa->spa_vdev_txg_list,
1010 offsetof(struct vdev, vdev_txg_node));
1012 avl_create(&spa->spa_errlist_scrub,
1013 spa_error_entry_compare, sizeof (spa_error_entry_t),
1014 offsetof(spa_error_entry_t, se_avl));
1015 avl_create(&spa->spa_errlist_last,
1016 spa_error_entry_compare, sizeof (spa_error_entry_t),
1017 offsetof(spa_error_entry_t, se_avl));
1021 * Opposite of spa_activate().
1024 spa_deactivate(spa_t *spa)
1026 ASSERT(spa->spa_sync_on == B_FALSE);
1027 ASSERT(spa->spa_dsl_pool == NULL);
1028 ASSERT(spa->spa_root_vdev == NULL);
1029 ASSERT(spa->spa_async_zio_root == NULL);
1030 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1032 txg_list_destroy(&spa->spa_vdev_txg_list);
1034 list_destroy(&spa->spa_config_dirty_list);
1035 list_destroy(&spa->spa_state_dirty_list);
1037 for (int t = 0; t < ZIO_TYPES; t++) {
1038 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1039 if (spa->spa_zio_taskq[t][q] != NULL)
1040 taskq_destroy(spa->spa_zio_taskq[t][q]);
1041 spa->spa_zio_taskq[t][q] = NULL;
1045 metaslab_class_destroy(spa->spa_normal_class);
1046 spa->spa_normal_class = NULL;
1048 metaslab_class_destroy(spa->spa_log_class);
1049 spa->spa_log_class = NULL;
1052 * If this was part of an import or the open otherwise failed, we may
1053 * still have errors left in the queues. Empty them just in case.
1055 spa_errlog_drain(spa);
1057 avl_destroy(&spa->spa_errlist_scrub);
1058 avl_destroy(&spa->spa_errlist_last);
1060 spa->spa_state = POOL_STATE_UNINITIALIZED;
1062 mutex_enter(&spa->spa_proc_lock);
1063 if (spa->spa_proc_state != SPA_PROC_NONE) {
1064 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1065 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1066 cv_broadcast(&spa->spa_proc_cv);
1067 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1068 ASSERT(spa->spa_proc != &p0);
1069 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1071 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1072 spa->spa_proc_state = SPA_PROC_NONE;
1074 ASSERT(spa->spa_proc == &p0);
1075 mutex_exit(&spa->spa_proc_lock);
1079 * We want to make sure spa_thread() has actually exited the ZFS
1080 * module, so that the module can't be unloaded out from underneath
1083 if (spa->spa_did != 0) {
1084 thread_join(spa->spa_did);
1087 #endif /* SPA_PROCESS */
1091 * Verify a pool configuration, and construct the vdev tree appropriately. This
1092 * will create all the necessary vdevs in the appropriate layout, with each vdev
1093 * in the CLOSED state. This will prep the pool before open/creation/import.
1094 * All vdev validation is done by the vdev_alloc() routine.
1097 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1098 uint_t id, int atype)
1104 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1107 if ((*vdp)->vdev_ops->vdev_op_leaf)
1110 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1113 if (error == ENOENT)
1122 for (int c = 0; c < children; c++) {
1124 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1132 ASSERT(*vdp != NULL);
1138 * Opposite of spa_load().
1141 spa_unload(spa_t *spa)
1145 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1150 spa_async_suspend(spa);
1155 if (spa->spa_sync_on) {
1156 txg_sync_stop(spa->spa_dsl_pool);
1157 spa->spa_sync_on = B_FALSE;
1161 * Wait for any outstanding async I/O to complete.
1163 if (spa->spa_async_zio_root != NULL) {
1164 (void) zio_wait(spa->spa_async_zio_root);
1165 spa->spa_async_zio_root = NULL;
1168 bpobj_close(&spa->spa_deferred_bpobj);
1171 * Close the dsl pool.
1173 if (spa->spa_dsl_pool) {
1174 dsl_pool_close(spa->spa_dsl_pool);
1175 spa->spa_dsl_pool = NULL;
1176 spa->spa_meta_objset = NULL;
1181 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1184 * Drop and purge level 2 cache
1186 spa_l2cache_drop(spa);
1191 if (spa->spa_root_vdev)
1192 vdev_free(spa->spa_root_vdev);
1193 ASSERT(spa->spa_root_vdev == NULL);
1195 for (i = 0; i < spa->spa_spares.sav_count; i++)
1196 vdev_free(spa->spa_spares.sav_vdevs[i]);
1197 if (spa->spa_spares.sav_vdevs) {
1198 kmem_free(spa->spa_spares.sav_vdevs,
1199 spa->spa_spares.sav_count * sizeof (void *));
1200 spa->spa_spares.sav_vdevs = NULL;
1202 if (spa->spa_spares.sav_config) {
1203 nvlist_free(spa->spa_spares.sav_config);
1204 spa->spa_spares.sav_config = NULL;
1206 spa->spa_spares.sav_count = 0;
1208 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1209 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1210 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1212 if (spa->spa_l2cache.sav_vdevs) {
1213 kmem_free(spa->spa_l2cache.sav_vdevs,
1214 spa->spa_l2cache.sav_count * sizeof (void *));
1215 spa->spa_l2cache.sav_vdevs = NULL;
1217 if (spa->spa_l2cache.sav_config) {
1218 nvlist_free(spa->spa_l2cache.sav_config);
1219 spa->spa_l2cache.sav_config = NULL;
1221 spa->spa_l2cache.sav_count = 0;
1223 spa->spa_async_suspended = 0;
1225 if (spa->spa_comment != NULL) {
1226 spa_strfree(spa->spa_comment);
1227 spa->spa_comment = NULL;
1230 spa_config_exit(spa, SCL_ALL, FTAG);
1234 * Load (or re-load) the current list of vdevs describing the active spares for
1235 * this pool. When this is called, we have some form of basic information in
1236 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1237 * then re-generate a more complete list including status information.
1240 spa_load_spares(spa_t *spa)
1247 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1250 * First, close and free any existing spare vdevs.
1252 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1253 vd = spa->spa_spares.sav_vdevs[i];
1255 /* Undo the call to spa_activate() below */
1256 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1257 B_FALSE)) != NULL && tvd->vdev_isspare)
1258 spa_spare_remove(tvd);
1263 if (spa->spa_spares.sav_vdevs)
1264 kmem_free(spa->spa_spares.sav_vdevs,
1265 spa->spa_spares.sav_count * sizeof (void *));
1267 if (spa->spa_spares.sav_config == NULL)
1270 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1271 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1273 spa->spa_spares.sav_count = (int)nspares;
1274 spa->spa_spares.sav_vdevs = NULL;
1280 * Construct the array of vdevs, opening them to get status in the
1281 * process. For each spare, there is potentially two different vdev_t
1282 * structures associated with it: one in the list of spares (used only
1283 * for basic validation purposes) and one in the active vdev
1284 * configuration (if it's spared in). During this phase we open and
1285 * validate each vdev on the spare list. If the vdev also exists in the
1286 * active configuration, then we also mark this vdev as an active spare.
1288 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1290 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1291 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1292 VDEV_ALLOC_SPARE) == 0);
1295 spa->spa_spares.sav_vdevs[i] = vd;
1297 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1298 B_FALSE)) != NULL) {
1299 if (!tvd->vdev_isspare)
1303 * We only mark the spare active if we were successfully
1304 * able to load the vdev. Otherwise, importing a pool
1305 * with a bad active spare would result in strange
1306 * behavior, because multiple pool would think the spare
1307 * is actively in use.
1309 * There is a vulnerability here to an equally bizarre
1310 * circumstance, where a dead active spare is later
1311 * brought back to life (onlined or otherwise). Given
1312 * the rarity of this scenario, and the extra complexity
1313 * it adds, we ignore the possibility.
1315 if (!vdev_is_dead(tvd))
1316 spa_spare_activate(tvd);
1320 vd->vdev_aux = &spa->spa_spares;
1322 if (vdev_open(vd) != 0)
1325 if (vdev_validate_aux(vd) == 0)
1330 * Recompute the stashed list of spares, with status information
1333 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1334 DATA_TYPE_NVLIST_ARRAY) == 0);
1336 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1338 for (i = 0; i < spa->spa_spares.sav_count; i++)
1339 spares[i] = vdev_config_generate(spa,
1340 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1341 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1342 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1343 for (i = 0; i < spa->spa_spares.sav_count; i++)
1344 nvlist_free(spares[i]);
1345 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1349 * Load (or re-load) the current list of vdevs describing the active l2cache for
1350 * this pool. When this is called, we have some form of basic information in
1351 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1352 * then re-generate a more complete list including status information.
1353 * Devices which are already active have their details maintained, and are
1357 spa_load_l2cache(spa_t *spa)
1361 int i, j, oldnvdevs;
1363 vdev_t *vd, **oldvdevs, **newvdevs;
1364 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1366 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1368 if (sav->sav_config != NULL) {
1369 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1370 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1371 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1376 oldvdevs = sav->sav_vdevs;
1377 oldnvdevs = sav->sav_count;
1378 sav->sav_vdevs = NULL;
1382 * Process new nvlist of vdevs.
1384 for (i = 0; i < nl2cache; i++) {
1385 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1389 for (j = 0; j < oldnvdevs; j++) {
1391 if (vd != NULL && guid == vd->vdev_guid) {
1393 * Retain previous vdev for add/remove ops.
1401 if (newvdevs[i] == NULL) {
1405 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1406 VDEV_ALLOC_L2CACHE) == 0);
1411 * Commit this vdev as an l2cache device,
1412 * even if it fails to open.
1414 spa_l2cache_add(vd);
1419 spa_l2cache_activate(vd);
1421 if (vdev_open(vd) != 0)
1424 (void) vdev_validate_aux(vd);
1426 if (!vdev_is_dead(vd))
1427 l2arc_add_vdev(spa, vd);
1432 * Purge vdevs that were dropped
1434 for (i = 0; i < oldnvdevs; i++) {
1439 ASSERT(vd->vdev_isl2cache);
1441 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1442 pool != 0ULL && l2arc_vdev_present(vd))
1443 l2arc_remove_vdev(vd);
1444 vdev_clear_stats(vd);
1450 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1452 if (sav->sav_config == NULL)
1455 sav->sav_vdevs = newvdevs;
1456 sav->sav_count = (int)nl2cache;
1459 * Recompute the stashed list of l2cache devices, with status
1460 * information this time.
1462 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1463 DATA_TYPE_NVLIST_ARRAY) == 0);
1465 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1466 for (i = 0; i < sav->sav_count; i++)
1467 l2cache[i] = vdev_config_generate(spa,
1468 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1469 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1470 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1472 for (i = 0; i < sav->sav_count; i++)
1473 nvlist_free(l2cache[i]);
1475 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1479 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1482 char *packed = NULL;
1487 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1488 nvsize = *(uint64_t *)db->db_data;
1489 dmu_buf_rele(db, FTAG);
1491 packed = kmem_alloc(nvsize, KM_SLEEP);
1492 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1495 error = nvlist_unpack(packed, nvsize, value, 0);
1496 kmem_free(packed, nvsize);
1502 * Checks to see if the given vdev could not be opened, in which case we post a
1503 * sysevent to notify the autoreplace code that the device has been removed.
1506 spa_check_removed(vdev_t *vd)
1508 for (int c = 0; c < vd->vdev_children; c++)
1509 spa_check_removed(vd->vdev_child[c]);
1511 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1512 zfs_post_autoreplace(vd->vdev_spa, vd);
1513 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1518 * Validate the current config against the MOS config
1521 spa_config_valid(spa_t *spa, nvlist_t *config)
1523 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1526 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1528 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1529 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1531 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1534 * If we're doing a normal import, then build up any additional
1535 * diagnostic information about missing devices in this config.
1536 * We'll pass this up to the user for further processing.
1538 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1539 nvlist_t **child, *nv;
1542 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1544 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1546 for (int c = 0; c < rvd->vdev_children; c++) {
1547 vdev_t *tvd = rvd->vdev_child[c];
1548 vdev_t *mtvd = mrvd->vdev_child[c];
1550 if (tvd->vdev_ops == &vdev_missing_ops &&
1551 mtvd->vdev_ops != &vdev_missing_ops &&
1553 child[idx++] = vdev_config_generate(spa, mtvd,
1558 VERIFY(nvlist_add_nvlist_array(nv,
1559 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1560 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1561 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1563 for (int i = 0; i < idx; i++)
1564 nvlist_free(child[i]);
1567 kmem_free(child, rvd->vdev_children * sizeof (char **));
1571 * Compare the root vdev tree with the information we have
1572 * from the MOS config (mrvd). Check each top-level vdev
1573 * with the corresponding MOS config top-level (mtvd).
1575 for (int c = 0; c < rvd->vdev_children; c++) {
1576 vdev_t *tvd = rvd->vdev_child[c];
1577 vdev_t *mtvd = mrvd->vdev_child[c];
1580 * Resolve any "missing" vdevs in the current configuration.
1581 * If we find that the MOS config has more accurate information
1582 * about the top-level vdev then use that vdev instead.
1584 if (tvd->vdev_ops == &vdev_missing_ops &&
1585 mtvd->vdev_ops != &vdev_missing_ops) {
1587 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1591 * Device specific actions.
1593 if (mtvd->vdev_islog) {
1594 spa_set_log_state(spa, SPA_LOG_CLEAR);
1597 * XXX - once we have 'readonly' pool
1598 * support we should be able to handle
1599 * missing data devices by transitioning
1600 * the pool to readonly.
1606 * Swap the missing vdev with the data we were
1607 * able to obtain from the MOS config.
1609 vdev_remove_child(rvd, tvd);
1610 vdev_remove_child(mrvd, mtvd);
1612 vdev_add_child(rvd, mtvd);
1613 vdev_add_child(mrvd, tvd);
1615 spa_config_exit(spa, SCL_ALL, FTAG);
1617 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1620 } else if (mtvd->vdev_islog) {
1622 * Load the slog device's state from the MOS config
1623 * since it's possible that the label does not
1624 * contain the most up-to-date information.
1626 vdev_load_log_state(tvd, mtvd);
1631 spa_config_exit(spa, SCL_ALL, FTAG);
1634 * Ensure we were able to validate the config.
1636 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1640 * Check for missing log devices
1643 spa_check_logs(spa_t *spa)
1645 switch (spa->spa_log_state) {
1646 case SPA_LOG_MISSING:
1647 /* need to recheck in case slog has been restored */
1648 case SPA_LOG_UNKNOWN:
1649 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1650 DS_FIND_CHILDREN)) {
1651 spa_set_log_state(spa, SPA_LOG_MISSING);
1660 spa_passivate_log(spa_t *spa)
1662 vdev_t *rvd = spa->spa_root_vdev;
1663 boolean_t slog_found = B_FALSE;
1665 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1667 if (!spa_has_slogs(spa))
1670 for (int c = 0; c < rvd->vdev_children; c++) {
1671 vdev_t *tvd = rvd->vdev_child[c];
1672 metaslab_group_t *mg = tvd->vdev_mg;
1674 if (tvd->vdev_islog) {
1675 metaslab_group_passivate(mg);
1676 slog_found = B_TRUE;
1680 return (slog_found);
1684 spa_activate_log(spa_t *spa)
1686 vdev_t *rvd = spa->spa_root_vdev;
1688 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1690 for (int c = 0; c < rvd->vdev_children; c++) {
1691 vdev_t *tvd = rvd->vdev_child[c];
1692 metaslab_group_t *mg = tvd->vdev_mg;
1694 if (tvd->vdev_islog)
1695 metaslab_group_activate(mg);
1700 spa_offline_log(spa_t *spa)
1704 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1705 NULL, DS_FIND_CHILDREN)) == 0) {
1708 * We successfully offlined the log device, sync out the
1709 * current txg so that the "stubby" block can be removed
1712 txg_wait_synced(spa->spa_dsl_pool, 0);
1718 spa_aux_check_removed(spa_aux_vdev_t *sav)
1722 for (i = 0; i < sav->sav_count; i++)
1723 spa_check_removed(sav->sav_vdevs[i]);
1727 spa_claim_notify(zio_t *zio)
1729 spa_t *spa = zio->io_spa;
1734 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1735 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1736 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1737 mutex_exit(&spa->spa_props_lock);
1740 typedef struct spa_load_error {
1741 uint64_t sle_meta_count;
1742 uint64_t sle_data_count;
1746 spa_load_verify_done(zio_t *zio)
1748 blkptr_t *bp = zio->io_bp;
1749 spa_load_error_t *sle = zio->io_private;
1750 dmu_object_type_t type = BP_GET_TYPE(bp);
1751 int error = zio->io_error;
1754 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1755 type != DMU_OT_INTENT_LOG)
1756 atomic_add_64(&sle->sle_meta_count, 1);
1758 atomic_add_64(&sle->sle_data_count, 1);
1760 zio_data_buf_free(zio->io_data, zio->io_size);
1765 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1766 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1770 size_t size = BP_GET_PSIZE(bp);
1771 void *data = zio_data_buf_alloc(size);
1773 zio_nowait(zio_read(rio, spa, bp, data, size,
1774 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1775 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1776 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1782 spa_load_verify(spa_t *spa)
1785 spa_load_error_t sle = { 0 };
1786 zpool_rewind_policy_t policy;
1787 boolean_t verify_ok = B_FALSE;
1790 zpool_get_rewind_policy(spa->spa_config, &policy);
1792 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1795 rio = zio_root(spa, NULL, &sle,
1796 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1798 error = traverse_pool(spa, spa->spa_verify_min_txg,
1799 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1801 (void) zio_wait(rio);
1803 spa->spa_load_meta_errors = sle.sle_meta_count;
1804 spa->spa_load_data_errors = sle.sle_data_count;
1806 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1807 sle.sle_data_count <= policy.zrp_maxdata) {
1811 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1812 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1814 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1815 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1816 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1817 VERIFY(nvlist_add_int64(spa->spa_load_info,
1818 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1819 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1820 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1822 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1826 if (error != ENXIO && error != EIO)
1831 return (verify_ok ? 0 : EIO);
1835 * Find a value in the pool props object.
1838 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1840 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1841 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1845 * Find a value in the pool directory object.
1848 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1850 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1851 name, sizeof (uint64_t), 1, val));
1855 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1857 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1862 * Fix up config after a partly-completed split. This is done with the
1863 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1864 * pool have that entry in their config, but only the splitting one contains
1865 * a list of all the guids of the vdevs that are being split off.
1867 * This function determines what to do with that list: either rejoin
1868 * all the disks to the pool, or complete the splitting process. To attempt
1869 * the rejoin, each disk that is offlined is marked online again, and
1870 * we do a reopen() call. If the vdev label for every disk that was
1871 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1872 * then we call vdev_split() on each disk, and complete the split.
1874 * Otherwise we leave the config alone, with all the vdevs in place in
1875 * the original pool.
1878 spa_try_repair(spa_t *spa, nvlist_t *config)
1885 boolean_t attempt_reopen;
1887 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1890 /* check that the config is complete */
1891 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1892 &glist, &gcount) != 0)
1895 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1897 /* attempt to online all the vdevs & validate */
1898 attempt_reopen = B_TRUE;
1899 for (i = 0; i < gcount; i++) {
1900 if (glist[i] == 0) /* vdev is hole */
1903 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1904 if (vd[i] == NULL) {
1906 * Don't bother attempting to reopen the disks;
1907 * just do the split.
1909 attempt_reopen = B_FALSE;
1911 /* attempt to re-online it */
1912 vd[i]->vdev_offline = B_FALSE;
1916 if (attempt_reopen) {
1917 vdev_reopen(spa->spa_root_vdev);
1919 /* check each device to see what state it's in */
1920 for (extracted = 0, i = 0; i < gcount; i++) {
1921 if (vd[i] != NULL &&
1922 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1929 * If every disk has been moved to the new pool, or if we never
1930 * even attempted to look at them, then we split them off for
1933 if (!attempt_reopen || gcount == extracted) {
1934 for (i = 0; i < gcount; i++)
1937 vdev_reopen(spa->spa_root_vdev);
1940 kmem_free(vd, gcount * sizeof (vdev_t *));
1944 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1945 boolean_t mosconfig)
1947 nvlist_t *config = spa->spa_config;
1948 char *ereport = FM_EREPORT_ZFS_POOL;
1954 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1957 ASSERT(spa->spa_comment == NULL);
1958 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1959 spa->spa_comment = spa_strdup(comment);
1962 * Versioning wasn't explicitly added to the label until later, so if
1963 * it's not present treat it as the initial version.
1965 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1966 &spa->spa_ubsync.ub_version) != 0)
1967 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1969 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1970 &spa->spa_config_txg);
1972 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1973 spa_guid_exists(pool_guid, 0)) {
1976 spa->spa_config_guid = pool_guid;
1978 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
1980 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
1984 nvlist_free(spa->spa_load_info);
1985 spa->spa_load_info = fnvlist_alloc();
1987 gethrestime(&spa->spa_loaded_ts);
1988 error = spa_load_impl(spa, pool_guid, config, state, type,
1989 mosconfig, &ereport);
1992 spa->spa_minref = refcount_count(&spa->spa_refcount);
1994 if (error != EEXIST) {
1995 spa->spa_loaded_ts.tv_sec = 0;
1996 spa->spa_loaded_ts.tv_nsec = 0;
1998 if (error != EBADF) {
1999 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2002 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2009 * Load an existing storage pool, using the pool's builtin spa_config as a
2010 * source of configuration information.
2013 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2014 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2018 nvlist_t *nvroot = NULL;
2021 uberblock_t *ub = &spa->spa_uberblock;
2022 uint64_t children, config_cache_txg = spa->spa_config_txg;
2023 int orig_mode = spa->spa_mode;
2026 boolean_t missing_feat_write = B_FALSE;
2029 * If this is an untrusted config, access the pool in read-only mode.
2030 * This prevents things like resilvering recently removed devices.
2033 spa->spa_mode = FREAD;
2035 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2037 spa->spa_load_state = state;
2039 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2042 parse = (type == SPA_IMPORT_EXISTING ?
2043 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2046 * Create "The Godfather" zio to hold all async IOs
2048 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2049 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2052 * Parse the configuration into a vdev tree. We explicitly set the
2053 * value that will be returned by spa_version() since parsing the
2054 * configuration requires knowing the version number.
2056 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2057 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2058 spa_config_exit(spa, SCL_ALL, FTAG);
2063 ASSERT(spa->spa_root_vdev == rvd);
2065 if (type != SPA_IMPORT_ASSEMBLE) {
2066 ASSERT(spa_guid(spa) == pool_guid);
2070 * Try to open all vdevs, loading each label in the process.
2072 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2073 error = vdev_open(rvd);
2074 spa_config_exit(spa, SCL_ALL, FTAG);
2079 * We need to validate the vdev labels against the configuration that
2080 * we have in hand, which is dependent on the setting of mosconfig. If
2081 * mosconfig is true then we're validating the vdev labels based on
2082 * that config. Otherwise, we're validating against the cached config
2083 * (zpool.cache) that was read when we loaded the zfs module, and then
2084 * later we will recursively call spa_load() and validate against
2087 * If we're assembling a new pool that's been split off from an
2088 * existing pool, the labels haven't yet been updated so we skip
2089 * validation for now.
2091 if (type != SPA_IMPORT_ASSEMBLE) {
2092 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2093 error = vdev_validate(rvd, mosconfig);
2094 spa_config_exit(spa, SCL_ALL, FTAG);
2099 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2104 * Find the best uberblock.
2106 vdev_uberblock_load(rvd, ub, &label);
2109 * If we weren't able to find a single valid uberblock, return failure.
2111 if (ub->ub_txg == 0) {
2113 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2117 * If the pool has an unsupported version we can't open it.
2119 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2121 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2124 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2128 * If we weren't able to find what's necessary for reading the
2129 * MOS in the label, return failure.
2131 if (label == NULL || nvlist_lookup_nvlist(label,
2132 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2134 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2139 * Update our in-core representation with the definitive values
2142 nvlist_free(spa->spa_label_features);
2143 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2149 * Look through entries in the label nvlist's features_for_read. If
2150 * there is a feature listed there which we don't understand then we
2151 * cannot open a pool.
2153 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2154 nvlist_t *unsup_feat;
2156 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2159 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2161 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2162 if (!zfeature_is_supported(nvpair_name(nvp))) {
2163 VERIFY(nvlist_add_string(unsup_feat,
2164 nvpair_name(nvp), "") == 0);
2168 if (!nvlist_empty(unsup_feat)) {
2169 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2170 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2171 nvlist_free(unsup_feat);
2172 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2176 nvlist_free(unsup_feat);
2180 * If the vdev guid sum doesn't match the uberblock, we have an
2181 * incomplete configuration. We first check to see if the pool
2182 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2183 * If it is, defer the vdev_guid_sum check till later so we
2184 * can handle missing vdevs.
2186 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2187 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2188 rvd->vdev_guid_sum != ub->ub_guid_sum)
2189 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2191 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2192 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2193 spa_try_repair(spa, config);
2194 spa_config_exit(spa, SCL_ALL, FTAG);
2195 nvlist_free(spa->spa_config_splitting);
2196 spa->spa_config_splitting = NULL;
2200 * Initialize internal SPA structures.
2202 spa->spa_state = POOL_STATE_ACTIVE;
2203 spa->spa_ubsync = spa->spa_uberblock;
2204 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2205 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2206 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2207 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2208 spa->spa_claim_max_txg = spa->spa_first_txg;
2209 spa->spa_prev_software_version = ub->ub_software_version;
2211 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2213 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2214 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2216 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2217 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2219 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2220 boolean_t missing_feat_read = B_FALSE;
2221 nvlist_t *unsup_feat, *enabled_feat;
2223 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2224 &spa->spa_feat_for_read_obj) != 0) {
2225 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2228 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2229 &spa->spa_feat_for_write_obj) != 0) {
2230 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2233 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2234 &spa->spa_feat_desc_obj) != 0) {
2235 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2238 enabled_feat = fnvlist_alloc();
2239 unsup_feat = fnvlist_alloc();
2241 if (!feature_is_supported(spa->spa_meta_objset,
2242 spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj,
2243 unsup_feat, enabled_feat))
2244 missing_feat_read = B_TRUE;
2246 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2247 if (!feature_is_supported(spa->spa_meta_objset,
2248 spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj,
2249 unsup_feat, enabled_feat)) {
2250 missing_feat_write = B_TRUE;
2254 fnvlist_add_nvlist(spa->spa_load_info,
2255 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2257 if (!nvlist_empty(unsup_feat)) {
2258 fnvlist_add_nvlist(spa->spa_load_info,
2259 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2262 fnvlist_free(enabled_feat);
2263 fnvlist_free(unsup_feat);
2265 if (!missing_feat_read) {
2266 fnvlist_add_boolean(spa->spa_load_info,
2267 ZPOOL_CONFIG_CAN_RDONLY);
2271 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2272 * twofold: to determine whether the pool is available for
2273 * import in read-write mode and (if it is not) whether the
2274 * pool is available for import in read-only mode. If the pool
2275 * is available for import in read-write mode, it is displayed
2276 * as available in userland; if it is not available for import
2277 * in read-only mode, it is displayed as unavailable in
2278 * userland. If the pool is available for import in read-only
2279 * mode but not read-write mode, it is displayed as unavailable
2280 * in userland with a special note that the pool is actually
2281 * available for open in read-only mode.
2283 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2284 * missing a feature for write, we must first determine whether
2285 * the pool can be opened read-only before returning to
2286 * userland in order to know whether to display the
2287 * abovementioned note.
2289 if (missing_feat_read || (missing_feat_write &&
2290 spa_writeable(spa))) {
2291 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2296 spa->spa_is_initializing = B_TRUE;
2297 error = dsl_pool_open(spa->spa_dsl_pool);
2298 spa->spa_is_initializing = B_FALSE;
2300 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2304 nvlist_t *policy = NULL, *nvconfig;
2306 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2307 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2309 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2310 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2312 unsigned long myhostid = 0;
2314 VERIFY(nvlist_lookup_string(nvconfig,
2315 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2318 myhostid = zone_get_hostid(NULL);
2321 * We're emulating the system's hostid in userland, so
2322 * we can't use zone_get_hostid().
2324 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2325 #endif /* _KERNEL */
2326 if (check_hostid && hostid != 0 && myhostid != 0 &&
2327 hostid != myhostid) {
2328 nvlist_free(nvconfig);
2329 cmn_err(CE_WARN, "pool '%s' could not be "
2330 "loaded as it was last accessed by "
2331 "another system (host: %s hostid: 0x%lx). "
2332 "See: http://illumos.org/msg/ZFS-8000-EY",
2333 spa_name(spa), hostname,
2334 (unsigned long)hostid);
2338 if (nvlist_lookup_nvlist(spa->spa_config,
2339 ZPOOL_REWIND_POLICY, &policy) == 0)
2340 VERIFY(nvlist_add_nvlist(nvconfig,
2341 ZPOOL_REWIND_POLICY, policy) == 0);
2343 spa_config_set(spa, nvconfig);
2345 spa_deactivate(spa);
2346 spa_activate(spa, orig_mode);
2348 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2351 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2352 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2353 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2355 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2358 * Load the bit that tells us to use the new accounting function
2359 * (raid-z deflation). If we have an older pool, this will not
2362 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2363 if (error != 0 && error != ENOENT)
2364 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2366 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2367 &spa->spa_creation_version);
2368 if (error != 0 && error != ENOENT)
2369 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2372 * Load the persistent error log. If we have an older pool, this will
2375 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2376 if (error != 0 && error != ENOENT)
2377 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2379 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2380 &spa->spa_errlog_scrub);
2381 if (error != 0 && error != ENOENT)
2382 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2385 * Load the history object. If we have an older pool, this
2386 * will not be present.
2388 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2389 if (error != 0 && error != ENOENT)
2390 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2393 * If we're assembling the pool from the split-off vdevs of
2394 * an existing pool, we don't want to attach the spares & cache
2399 * Load any hot spares for this pool.
2401 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2402 if (error != 0 && error != ENOENT)
2403 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2404 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2405 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2406 if (load_nvlist(spa, spa->spa_spares.sav_object,
2407 &spa->spa_spares.sav_config) != 0)
2408 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2410 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2411 spa_load_spares(spa);
2412 spa_config_exit(spa, SCL_ALL, FTAG);
2413 } else if (error == 0) {
2414 spa->spa_spares.sav_sync = B_TRUE;
2418 * Load any level 2 ARC devices for this pool.
2420 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2421 &spa->spa_l2cache.sav_object);
2422 if (error != 0 && error != ENOENT)
2423 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2424 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2425 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2426 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2427 &spa->spa_l2cache.sav_config) != 0)
2428 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2430 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2431 spa_load_l2cache(spa);
2432 spa_config_exit(spa, SCL_ALL, FTAG);
2433 } else if (error == 0) {
2434 spa->spa_l2cache.sav_sync = B_TRUE;
2437 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2439 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2440 if (error && error != ENOENT)
2441 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2444 uint64_t autoreplace;
2446 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2447 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2448 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2449 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2450 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2451 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2452 &spa->spa_dedup_ditto);
2454 spa->spa_autoreplace = (autoreplace != 0);
2458 * If the 'autoreplace' property is set, then post a resource notifying
2459 * the ZFS DE that it should not issue any faults for unopenable
2460 * devices. We also iterate over the vdevs, and post a sysevent for any
2461 * unopenable vdevs so that the normal autoreplace handler can take
2464 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2465 spa_check_removed(spa->spa_root_vdev);
2467 * For the import case, this is done in spa_import(), because
2468 * at this point we're using the spare definitions from
2469 * the MOS config, not necessarily from the userland config.
2471 if (state != SPA_LOAD_IMPORT) {
2472 spa_aux_check_removed(&spa->spa_spares);
2473 spa_aux_check_removed(&spa->spa_l2cache);
2478 * Load the vdev state for all toplevel vdevs.
2483 * Propagate the leaf DTLs we just loaded all the way up the tree.
2485 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2486 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2487 spa_config_exit(spa, SCL_ALL, FTAG);
2490 * Load the DDTs (dedup tables).
2492 error = ddt_load(spa);
2494 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2496 spa_update_dspace(spa);
2499 * Validate the config, using the MOS config to fill in any
2500 * information which might be missing. If we fail to validate
2501 * the config then declare the pool unfit for use. If we're
2502 * assembling a pool from a split, the log is not transferred
2505 if (type != SPA_IMPORT_ASSEMBLE) {
2508 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2509 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2511 if (!spa_config_valid(spa, nvconfig)) {
2512 nvlist_free(nvconfig);
2513 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2516 nvlist_free(nvconfig);
2519 * Now that we've validated the config, check the state of the
2520 * root vdev. If it can't be opened, it indicates one or
2521 * more toplevel vdevs are faulted.
2523 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2526 if (spa_check_logs(spa)) {
2527 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2528 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2532 if (missing_feat_write) {
2533 ASSERT(state == SPA_LOAD_TRYIMPORT);
2536 * At this point, we know that we can open the pool in
2537 * read-only mode but not read-write mode. We now have enough
2538 * information and can return to userland.
2540 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2544 * We've successfully opened the pool, verify that we're ready
2545 * to start pushing transactions.
2547 if (state != SPA_LOAD_TRYIMPORT) {
2548 if (error = spa_load_verify(spa))
2549 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2553 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2554 spa->spa_load_max_txg == UINT64_MAX)) {
2556 int need_update = B_FALSE;
2558 ASSERT(state != SPA_LOAD_TRYIMPORT);
2561 * Claim log blocks that haven't been committed yet.
2562 * This must all happen in a single txg.
2563 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2564 * invoked from zil_claim_log_block()'s i/o done callback.
2565 * Price of rollback is that we abandon the log.
2567 spa->spa_claiming = B_TRUE;
2569 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2570 spa_first_txg(spa));
2571 (void) dmu_objset_find(spa_name(spa),
2572 zil_claim, tx, DS_FIND_CHILDREN);
2575 spa->spa_claiming = B_FALSE;
2577 spa_set_log_state(spa, SPA_LOG_GOOD);
2578 spa->spa_sync_on = B_TRUE;
2579 txg_sync_start(spa->spa_dsl_pool);
2582 * Wait for all claims to sync. We sync up to the highest
2583 * claimed log block birth time so that claimed log blocks
2584 * don't appear to be from the future. spa_claim_max_txg
2585 * will have been set for us by either zil_check_log_chain()
2586 * (invoked from spa_check_logs()) or zil_claim() above.
2588 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2591 * If the config cache is stale, or we have uninitialized
2592 * metaslabs (see spa_vdev_add()), then update the config.
2594 * If this is a verbatim import, trust the current
2595 * in-core spa_config and update the disk labels.
2597 if (config_cache_txg != spa->spa_config_txg ||
2598 state == SPA_LOAD_IMPORT ||
2599 state == SPA_LOAD_RECOVER ||
2600 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2601 need_update = B_TRUE;
2603 for (int c = 0; c < rvd->vdev_children; c++)
2604 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2605 need_update = B_TRUE;
2608 * Update the config cache asychronously in case we're the
2609 * root pool, in which case the config cache isn't writable yet.
2612 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2615 * Check all DTLs to see if anything needs resilvering.
2617 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2618 vdev_resilver_needed(rvd, NULL, NULL))
2619 spa_async_request(spa, SPA_ASYNC_RESILVER);
2622 * Delete any inconsistent datasets.
2624 (void) dmu_objset_find(spa_name(spa),
2625 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2628 * Clean up any stale temporary dataset userrefs.
2630 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2637 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2639 int mode = spa->spa_mode;
2642 spa_deactivate(spa);
2644 spa->spa_load_max_txg--;
2646 spa_activate(spa, mode);
2647 spa_async_suspend(spa);
2649 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2653 * If spa_load() fails this function will try loading prior txg's. If
2654 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2655 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2656 * function will not rewind the pool and will return the same error as
2660 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2661 uint64_t max_request, int rewind_flags)
2663 nvlist_t *loadinfo = NULL;
2664 nvlist_t *config = NULL;
2665 int load_error, rewind_error;
2666 uint64_t safe_rewind_txg;
2669 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2670 spa->spa_load_max_txg = spa->spa_load_txg;
2671 spa_set_log_state(spa, SPA_LOG_CLEAR);
2673 spa->spa_load_max_txg = max_request;
2676 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2678 if (load_error == 0)
2681 if (spa->spa_root_vdev != NULL)
2682 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2684 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2685 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2687 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2688 nvlist_free(config);
2689 return (load_error);
2692 if (state == SPA_LOAD_RECOVER) {
2693 /* Price of rolling back is discarding txgs, including log */
2694 spa_set_log_state(spa, SPA_LOG_CLEAR);
2697 * If we aren't rolling back save the load info from our first
2698 * import attempt so that we can restore it after attempting
2701 loadinfo = spa->spa_load_info;
2702 spa->spa_load_info = fnvlist_alloc();
2705 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2706 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2707 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2708 TXG_INITIAL : safe_rewind_txg;
2711 * Continue as long as we're finding errors, we're still within
2712 * the acceptable rewind range, and we're still finding uberblocks
2714 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2715 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2716 if (spa->spa_load_max_txg < safe_rewind_txg)
2717 spa->spa_extreme_rewind = B_TRUE;
2718 rewind_error = spa_load_retry(spa, state, mosconfig);
2721 spa->spa_extreme_rewind = B_FALSE;
2722 spa->spa_load_max_txg = UINT64_MAX;
2724 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2725 spa_config_set(spa, config);
2727 if (state == SPA_LOAD_RECOVER) {
2728 ASSERT3P(loadinfo, ==, NULL);
2729 return (rewind_error);
2731 /* Store the rewind info as part of the initial load info */
2732 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2733 spa->spa_load_info);
2735 /* Restore the initial load info */
2736 fnvlist_free(spa->spa_load_info);
2737 spa->spa_load_info = loadinfo;
2739 return (load_error);
2746 * The import case is identical to an open except that the configuration is sent
2747 * down from userland, instead of grabbed from the configuration cache. For the
2748 * case of an open, the pool configuration will exist in the
2749 * POOL_STATE_UNINITIALIZED state.
2751 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2752 * the same time open the pool, without having to keep around the spa_t in some
2756 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2760 spa_load_state_t state = SPA_LOAD_OPEN;
2762 int locked = B_FALSE;
2763 int firstopen = B_FALSE;
2768 * As disgusting as this is, we need to support recursive calls to this
2769 * function because dsl_dir_open() is called during spa_load(), and ends
2770 * up calling spa_open() again. The real fix is to figure out how to
2771 * avoid dsl_dir_open() calling this in the first place.
2773 if (mutex_owner(&spa_namespace_lock) != curthread) {
2774 mutex_enter(&spa_namespace_lock);
2778 if ((spa = spa_lookup(pool)) == NULL) {
2780 mutex_exit(&spa_namespace_lock);
2784 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2785 zpool_rewind_policy_t policy;
2789 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2791 if (policy.zrp_request & ZPOOL_DO_REWIND)
2792 state = SPA_LOAD_RECOVER;
2794 spa_activate(spa, spa_mode_global);
2796 if (state != SPA_LOAD_RECOVER)
2797 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2799 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2800 policy.zrp_request);
2802 if (error == EBADF) {
2804 * If vdev_validate() returns failure (indicated by
2805 * EBADF), it indicates that one of the vdevs indicates
2806 * that the pool has been exported or destroyed. If
2807 * this is the case, the config cache is out of sync and
2808 * we should remove the pool from the namespace.
2811 spa_deactivate(spa);
2812 spa_config_sync(spa, B_TRUE, B_TRUE);
2815 mutex_exit(&spa_namespace_lock);
2821 * We can't open the pool, but we still have useful
2822 * information: the state of each vdev after the
2823 * attempted vdev_open(). Return this to the user.
2825 if (config != NULL && spa->spa_config) {
2826 VERIFY(nvlist_dup(spa->spa_config, config,
2828 VERIFY(nvlist_add_nvlist(*config,
2829 ZPOOL_CONFIG_LOAD_INFO,
2830 spa->spa_load_info) == 0);
2833 spa_deactivate(spa);
2834 spa->spa_last_open_failed = error;
2836 mutex_exit(&spa_namespace_lock);
2842 spa_open_ref(spa, tag);
2845 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2848 * If we've recovered the pool, pass back any information we
2849 * gathered while doing the load.
2851 if (state == SPA_LOAD_RECOVER) {
2852 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2853 spa->spa_load_info) == 0);
2857 spa->spa_last_open_failed = 0;
2858 spa->spa_last_ubsync_txg = 0;
2859 spa->spa_load_txg = 0;
2860 mutex_exit(&spa_namespace_lock);
2864 zvol_create_minors(pool);
2875 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2878 return (spa_open_common(name, spapp, tag, policy, config));
2882 spa_open(const char *name, spa_t **spapp, void *tag)
2884 return (spa_open_common(name, spapp, tag, NULL, NULL));
2888 * Lookup the given spa_t, incrementing the inject count in the process,
2889 * preventing it from being exported or destroyed.
2892 spa_inject_addref(char *name)
2896 mutex_enter(&spa_namespace_lock);
2897 if ((spa = spa_lookup(name)) == NULL) {
2898 mutex_exit(&spa_namespace_lock);
2901 spa->spa_inject_ref++;
2902 mutex_exit(&spa_namespace_lock);
2908 spa_inject_delref(spa_t *spa)
2910 mutex_enter(&spa_namespace_lock);
2911 spa->spa_inject_ref--;
2912 mutex_exit(&spa_namespace_lock);
2916 * Add spares device information to the nvlist.
2919 spa_add_spares(spa_t *spa, nvlist_t *config)
2929 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2931 if (spa->spa_spares.sav_count == 0)
2934 VERIFY(nvlist_lookup_nvlist(config,
2935 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2936 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2937 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2939 VERIFY(nvlist_add_nvlist_array(nvroot,
2940 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2941 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2942 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2945 * Go through and find any spares which have since been
2946 * repurposed as an active spare. If this is the case, update
2947 * their status appropriately.
2949 for (i = 0; i < nspares; i++) {
2950 VERIFY(nvlist_lookup_uint64(spares[i],
2951 ZPOOL_CONFIG_GUID, &guid) == 0);
2952 if (spa_spare_exists(guid, &pool, NULL) &&
2954 VERIFY(nvlist_lookup_uint64_array(
2955 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2956 (uint64_t **)&vs, &vsc) == 0);
2957 vs->vs_state = VDEV_STATE_CANT_OPEN;
2958 vs->vs_aux = VDEV_AUX_SPARED;
2965 * Add l2cache device information to the nvlist, including vdev stats.
2968 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2971 uint_t i, j, nl2cache;
2978 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2980 if (spa->spa_l2cache.sav_count == 0)
2983 VERIFY(nvlist_lookup_nvlist(config,
2984 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2985 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
2986 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2987 if (nl2cache != 0) {
2988 VERIFY(nvlist_add_nvlist_array(nvroot,
2989 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2990 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2991 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
2994 * Update level 2 cache device stats.
2997 for (i = 0; i < nl2cache; i++) {
2998 VERIFY(nvlist_lookup_uint64(l2cache[i],
2999 ZPOOL_CONFIG_GUID, &guid) == 0);
3002 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3004 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3005 vd = spa->spa_l2cache.sav_vdevs[j];
3011 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3012 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3014 vdev_get_stats(vd, vs);
3020 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3026 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3027 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3029 if (spa->spa_feat_for_read_obj != 0) {
3030 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3031 spa->spa_feat_for_read_obj);
3032 zap_cursor_retrieve(&zc, &za) == 0;
3033 zap_cursor_advance(&zc)) {
3034 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3035 za.za_num_integers == 1);
3036 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3037 za.za_first_integer));
3039 zap_cursor_fini(&zc);
3042 if (spa->spa_feat_for_write_obj != 0) {
3043 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3044 spa->spa_feat_for_write_obj);
3045 zap_cursor_retrieve(&zc, &za) == 0;
3046 zap_cursor_advance(&zc)) {
3047 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3048 za.za_num_integers == 1);
3049 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3050 za.za_first_integer));
3052 zap_cursor_fini(&zc);
3055 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3057 nvlist_free(features);
3061 spa_get_stats(const char *name, nvlist_t **config,
3062 char *altroot, size_t buflen)
3068 error = spa_open_common(name, &spa, FTAG, NULL, config);
3072 * This still leaves a window of inconsistency where the spares
3073 * or l2cache devices could change and the config would be
3074 * self-inconsistent.
3076 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3078 if (*config != NULL) {
3079 uint64_t loadtimes[2];
3081 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3082 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3083 VERIFY(nvlist_add_uint64_array(*config,
3084 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3086 VERIFY(nvlist_add_uint64(*config,
3087 ZPOOL_CONFIG_ERRCOUNT,
3088 spa_get_errlog_size(spa)) == 0);
3090 if (spa_suspended(spa))
3091 VERIFY(nvlist_add_uint64(*config,
3092 ZPOOL_CONFIG_SUSPENDED,
3093 spa->spa_failmode) == 0);
3095 spa_add_spares(spa, *config);
3096 spa_add_l2cache(spa, *config);
3097 spa_add_feature_stats(spa, *config);
3102 * We want to get the alternate root even for faulted pools, so we cheat
3103 * and call spa_lookup() directly.
3107 mutex_enter(&spa_namespace_lock);
3108 spa = spa_lookup(name);
3110 spa_altroot(spa, altroot, buflen);
3114 mutex_exit(&spa_namespace_lock);
3116 spa_altroot(spa, altroot, buflen);
3121 spa_config_exit(spa, SCL_CONFIG, FTAG);
3122 spa_close(spa, FTAG);
3129 * Validate that the auxiliary device array is well formed. We must have an
3130 * array of nvlists, each which describes a valid leaf vdev. If this is an
3131 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3132 * specified, as long as they are well-formed.
3135 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3136 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3137 vdev_labeltype_t label)
3144 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3147 * It's acceptable to have no devs specified.
3149 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3156 * Make sure the pool is formatted with a version that supports this
3159 if (spa_version(spa) < version)
3163 * Set the pending device list so we correctly handle device in-use
3166 sav->sav_pending = dev;
3167 sav->sav_npending = ndev;
3169 for (i = 0; i < ndev; i++) {
3170 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3174 if (!vd->vdev_ops->vdev_op_leaf) {
3181 * The L2ARC currently only supports disk devices in
3182 * kernel context. For user-level testing, we allow it.
3185 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3186 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3194 if ((error = vdev_open(vd)) == 0 &&
3195 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3196 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3197 vd->vdev_guid) == 0);
3203 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3210 sav->sav_pending = NULL;
3211 sav->sav_npending = 0;
3216 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3220 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3222 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3223 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3224 VDEV_LABEL_SPARE)) != 0) {
3228 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3229 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3230 VDEV_LABEL_L2CACHE));
3234 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3239 if (sav->sav_config != NULL) {
3245 * Generate new dev list by concatentating with the
3248 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3249 &olddevs, &oldndevs) == 0);
3251 newdevs = kmem_alloc(sizeof (void *) *
3252 (ndevs + oldndevs), KM_SLEEP);
3253 for (i = 0; i < oldndevs; i++)
3254 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3256 for (i = 0; i < ndevs; i++)
3257 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3260 VERIFY(nvlist_remove(sav->sav_config, config,
3261 DATA_TYPE_NVLIST_ARRAY) == 0);
3263 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3264 config, newdevs, ndevs + oldndevs) == 0);
3265 for (i = 0; i < oldndevs + ndevs; i++)
3266 nvlist_free(newdevs[i]);
3267 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3270 * Generate a new dev list.
3272 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3274 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3280 * Stop and drop level 2 ARC devices
3283 spa_l2cache_drop(spa_t *spa)
3287 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3289 for (i = 0; i < sav->sav_count; i++) {
3292 vd = sav->sav_vdevs[i];
3295 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3296 pool != 0ULL && l2arc_vdev_present(vd))
3297 l2arc_remove_vdev(vd);
3305 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3306 const char *history_str, nvlist_t *zplprops)
3309 char *altroot = NULL;
3314 uint64_t txg = TXG_INITIAL;
3315 nvlist_t **spares, **l2cache;
3316 uint_t nspares, nl2cache;
3317 uint64_t version, obj;
3318 boolean_t has_features;
3321 * If this pool already exists, return failure.
3323 mutex_enter(&spa_namespace_lock);
3324 if (spa_lookup(pool) != NULL) {
3325 mutex_exit(&spa_namespace_lock);
3330 * Allocate a new spa_t structure.
3332 (void) nvlist_lookup_string(props,
3333 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3334 spa = spa_add(pool, NULL, altroot);
3335 spa_activate(spa, spa_mode_global);
3337 if (props && (error = spa_prop_validate(spa, props))) {
3338 spa_deactivate(spa);
3340 mutex_exit(&spa_namespace_lock);
3344 has_features = B_FALSE;
3345 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3346 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3347 if (zpool_prop_feature(nvpair_name(elem)))
3348 has_features = B_TRUE;
3351 if (has_features || nvlist_lookup_uint64(props,
3352 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3353 version = SPA_VERSION;
3355 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3357 spa->spa_first_txg = txg;
3358 spa->spa_uberblock.ub_txg = txg - 1;
3359 spa->spa_uberblock.ub_version = version;
3360 spa->spa_ubsync = spa->spa_uberblock;
3363 * Create "The Godfather" zio to hold all async IOs
3365 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3366 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3369 * Create the root vdev.
3371 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3373 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3375 ASSERT(error != 0 || rvd != NULL);
3376 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3378 if (error == 0 && !zfs_allocatable_devs(nvroot))
3382 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3383 (error = spa_validate_aux(spa, nvroot, txg,
3384 VDEV_ALLOC_ADD)) == 0) {
3385 for (int c = 0; c < rvd->vdev_children; c++) {
3386 vdev_metaslab_set_size(rvd->vdev_child[c]);
3387 vdev_expand(rvd->vdev_child[c], txg);
3391 spa_config_exit(spa, SCL_ALL, FTAG);
3395 spa_deactivate(spa);
3397 mutex_exit(&spa_namespace_lock);
3402 * Get the list of spares, if specified.
3404 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3405 &spares, &nspares) == 0) {
3406 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3408 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3409 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3410 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3411 spa_load_spares(spa);
3412 spa_config_exit(spa, SCL_ALL, FTAG);
3413 spa->spa_spares.sav_sync = B_TRUE;
3417 * Get the list of level 2 cache devices, if specified.
3419 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3420 &l2cache, &nl2cache) == 0) {
3421 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3422 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3423 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3424 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3425 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3426 spa_load_l2cache(spa);
3427 spa_config_exit(spa, SCL_ALL, FTAG);
3428 spa->spa_l2cache.sav_sync = B_TRUE;
3431 spa->spa_is_initializing = B_TRUE;
3432 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3433 spa->spa_meta_objset = dp->dp_meta_objset;
3434 spa->spa_is_initializing = B_FALSE;
3437 * Create DDTs (dedup tables).
3441 spa_update_dspace(spa);
3443 tx = dmu_tx_create_assigned(dp, txg);
3446 * Create the pool config object.
3448 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3449 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3450 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3452 if (zap_add(spa->spa_meta_objset,
3453 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3454 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3455 cmn_err(CE_PANIC, "failed to add pool config");
3458 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3459 spa_feature_create_zap_objects(spa, tx);
3461 if (zap_add(spa->spa_meta_objset,
3462 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3463 sizeof (uint64_t), 1, &version, tx) != 0) {
3464 cmn_err(CE_PANIC, "failed to add pool version");
3467 /* Newly created pools with the right version are always deflated. */
3468 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3469 spa->spa_deflate = TRUE;
3470 if (zap_add(spa->spa_meta_objset,
3471 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3472 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3473 cmn_err(CE_PANIC, "failed to add deflate");
3478 * Create the deferred-free bpobj. Turn off compression
3479 * because sync-to-convergence takes longer if the blocksize
3482 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3483 dmu_object_set_compress(spa->spa_meta_objset, obj,
3484 ZIO_COMPRESS_OFF, tx);
3485 if (zap_add(spa->spa_meta_objset,
3486 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3487 sizeof (uint64_t), 1, &obj, tx) != 0) {
3488 cmn_err(CE_PANIC, "failed to add bpobj");
3490 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3491 spa->spa_meta_objset, obj));
3494 * Create the pool's history object.
3496 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3497 spa_history_create_obj(spa, tx);
3500 * Set pool properties.
3502 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3503 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3504 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3505 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3507 if (props != NULL) {
3508 spa_configfile_set(spa, props, B_FALSE);
3509 spa_sync_props(spa, props, tx);
3514 spa->spa_sync_on = B_TRUE;
3515 txg_sync_start(spa->spa_dsl_pool);
3518 * We explicitly wait for the first transaction to complete so that our
3519 * bean counters are appropriately updated.
3521 txg_wait_synced(spa->spa_dsl_pool, txg);
3523 spa_config_sync(spa, B_FALSE, B_TRUE);
3525 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3526 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3527 spa_history_log_version(spa, LOG_POOL_CREATE);
3529 spa->spa_minref = refcount_count(&spa->spa_refcount);
3531 mutex_exit(&spa_namespace_lock);
3539 * Get the root pool information from the root disk, then import the root pool
3540 * during the system boot up time.
3542 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3545 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3548 nvlist_t *nvtop, *nvroot;
3551 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3555 * Add this top-level vdev to the child array.
3557 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3559 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3561 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3564 * Put this pool's top-level vdevs into a root vdev.
3566 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3567 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3568 VDEV_TYPE_ROOT) == 0);
3569 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3570 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3571 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3575 * Replace the existing vdev_tree with the new root vdev in
3576 * this pool's configuration (remove the old, add the new).
3578 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3579 nvlist_free(nvroot);
3584 * Walk the vdev tree and see if we can find a device with "better"
3585 * configuration. A configuration is "better" if the label on that
3586 * device has a more recent txg.
3589 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3591 for (int c = 0; c < vd->vdev_children; c++)
3592 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3594 if (vd->vdev_ops->vdev_op_leaf) {
3598 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3602 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3606 * Do we have a better boot device?
3608 if (label_txg > *txg) {
3617 * Import a root pool.
3619 * For x86. devpath_list will consist of devid and/or physpath name of
3620 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3621 * The GRUB "findroot" command will return the vdev we should boot.
3623 * For Sparc, devpath_list consists the physpath name of the booting device
3624 * no matter the rootpool is a single device pool or a mirrored pool.
3626 * "/pci@1f,0/ide@d/disk@0,0:a"
3629 spa_import_rootpool(char *devpath, char *devid)
3632 vdev_t *rvd, *bvd, *avd = NULL;
3633 nvlist_t *config, *nvtop;
3639 * Read the label from the boot device and generate a configuration.
3641 config = spa_generate_rootconf(devpath, devid, &guid);
3642 #if defined(_OBP) && defined(_KERNEL)
3643 if (config == NULL) {
3644 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3646 get_iscsi_bootpath_phy(devpath);
3647 config = spa_generate_rootconf(devpath, devid, &guid);
3651 if (config == NULL) {
3652 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3657 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3659 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3661 mutex_enter(&spa_namespace_lock);
3662 if ((spa = spa_lookup(pname)) != NULL) {
3664 * Remove the existing root pool from the namespace so that we
3665 * can replace it with the correct config we just read in.
3670 spa = spa_add(pname, config, NULL);
3671 spa->spa_is_root = B_TRUE;
3672 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3675 * Build up a vdev tree based on the boot device's label config.
3677 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3679 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3680 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3681 VDEV_ALLOC_ROOTPOOL);
3682 spa_config_exit(spa, SCL_ALL, FTAG);
3684 mutex_exit(&spa_namespace_lock);
3685 nvlist_free(config);
3686 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3692 * Get the boot vdev.
3694 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3695 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3696 (u_longlong_t)guid);
3702 * Determine if there is a better boot device.
3705 spa_alt_rootvdev(rvd, &avd, &txg);
3707 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3708 "try booting from '%s'", avd->vdev_path);
3714 * If the boot device is part of a spare vdev then ensure that
3715 * we're booting off the active spare.
3717 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3718 !bvd->vdev_isspare) {
3719 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3720 "try booting from '%s'",
3722 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3728 spa_history_log_version(spa, LOG_POOL_IMPORT);
3730 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3732 spa_config_exit(spa, SCL_ALL, FTAG);
3733 mutex_exit(&spa_namespace_lock);
3735 nvlist_free(config);
3742 vdev_geom_read_pool_label(const char *name, nvlist_t **config);
3745 spa_generate_rootconf(const char *name)
3748 nvlist_t *nvtop, *nvroot;
3752 if (vdev_geom_read_pool_label(name, &config) != 0)
3756 * Multi-vdev root pool configuration discovery is not supported yet.
3759 nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3760 if (nchildren != 1) {
3761 nvlist_free(config);
3766 * Add this top-level vdev to the child array.
3768 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3770 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3774 * Put this pool's top-level vdevs into a root vdev.
3776 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3777 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3778 VDEV_TYPE_ROOT) == 0);
3779 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3780 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3781 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3785 * Replace the existing vdev_tree with the new root vdev in
3786 * this pool's configuration (remove the old, add the new).
3788 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3789 nvlist_free(nvroot);
3794 spa_import_rootpool(const char *name)
3797 vdev_t *rvd, *bvd, *avd = NULL;
3798 nvlist_t *config, *nvtop;
3804 * Read the label from the boot device and generate a configuration.
3806 config = spa_generate_rootconf(name);
3808 mutex_enter(&spa_namespace_lock);
3809 if (config != NULL) {
3810 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3811 &pname) == 0 && strcmp(name, pname) == 0);
3812 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
3815 if ((spa = spa_lookup(pname)) != NULL) {
3817 * Remove the existing root pool from the namespace so
3818 * that we can replace it with the correct config
3823 spa = spa_add(pname, config, NULL);
3824 } else if ((spa = spa_lookup(name)) == NULL) {
3825 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
3829 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
3831 spa->spa_is_root = B_TRUE;
3832 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3835 * Build up a vdev tree based on the boot device's label config.
3837 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3839 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3840 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3841 VDEV_ALLOC_ROOTPOOL);
3842 spa_config_exit(spa, SCL_ALL, FTAG);
3844 mutex_exit(&spa_namespace_lock);
3845 nvlist_free(config);
3846 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3851 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3853 spa_config_exit(spa, SCL_ALL, FTAG);
3854 mutex_exit(&spa_namespace_lock);
3856 nvlist_free(config);
3864 * Import a non-root pool into the system.
3867 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3870 char *altroot = NULL;
3871 spa_load_state_t state = SPA_LOAD_IMPORT;
3872 zpool_rewind_policy_t policy;
3873 uint64_t mode = spa_mode_global;
3874 uint64_t readonly = B_FALSE;
3877 nvlist_t **spares, **l2cache;
3878 uint_t nspares, nl2cache;
3881 * If a pool with this name exists, return failure.
3883 mutex_enter(&spa_namespace_lock);
3884 if (spa_lookup(pool) != NULL) {
3885 mutex_exit(&spa_namespace_lock);
3890 * Create and initialize the spa structure.
3892 (void) nvlist_lookup_string(props,
3893 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3894 (void) nvlist_lookup_uint64(props,
3895 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3898 spa = spa_add(pool, config, altroot);
3899 spa->spa_import_flags = flags;
3902 * Verbatim import - Take a pool and insert it into the namespace
3903 * as if it had been loaded at boot.
3905 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3907 spa_configfile_set(spa, props, B_FALSE);
3909 spa_config_sync(spa, B_FALSE, B_TRUE);
3911 mutex_exit(&spa_namespace_lock);
3912 spa_history_log_version(spa, LOG_POOL_IMPORT);
3917 spa_activate(spa, mode);
3920 * Don't start async tasks until we know everything is healthy.
3922 spa_async_suspend(spa);
3924 zpool_get_rewind_policy(config, &policy);
3925 if (policy.zrp_request & ZPOOL_DO_REWIND)
3926 state = SPA_LOAD_RECOVER;
3929 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3930 * because the user-supplied config is actually the one to trust when
3933 if (state != SPA_LOAD_RECOVER)
3934 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3936 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3937 policy.zrp_request);
3940 * Propagate anything learned while loading the pool and pass it
3941 * back to caller (i.e. rewind info, missing devices, etc).
3943 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3944 spa->spa_load_info) == 0);
3946 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3948 * Toss any existing sparelist, as it doesn't have any validity
3949 * anymore, and conflicts with spa_has_spare().
3951 if (spa->spa_spares.sav_config) {
3952 nvlist_free(spa->spa_spares.sav_config);
3953 spa->spa_spares.sav_config = NULL;
3954 spa_load_spares(spa);
3956 if (spa->spa_l2cache.sav_config) {
3957 nvlist_free(spa->spa_l2cache.sav_config);
3958 spa->spa_l2cache.sav_config = NULL;
3959 spa_load_l2cache(spa);
3962 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3965 error = spa_validate_aux(spa, nvroot, -1ULL,
3968 error = spa_validate_aux(spa, nvroot, -1ULL,
3969 VDEV_ALLOC_L2CACHE);
3970 spa_config_exit(spa, SCL_ALL, FTAG);
3973 spa_configfile_set(spa, props, B_FALSE);
3975 if (error != 0 || (props && spa_writeable(spa) &&
3976 (error = spa_prop_set(spa, props)))) {
3978 spa_deactivate(spa);
3980 mutex_exit(&spa_namespace_lock);
3984 spa_async_resume(spa);
3987 * Override any spares and level 2 cache devices as specified by
3988 * the user, as these may have correct device names/devids, etc.
3990 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3991 &spares, &nspares) == 0) {
3992 if (spa->spa_spares.sav_config)
3993 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3994 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3996 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3997 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3998 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3999 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4000 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4001 spa_load_spares(spa);
4002 spa_config_exit(spa, SCL_ALL, FTAG);
4003 spa->spa_spares.sav_sync = B_TRUE;
4005 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4006 &l2cache, &nl2cache) == 0) {
4007 if (spa->spa_l2cache.sav_config)
4008 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4009 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4011 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4012 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4013 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4014 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4015 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4016 spa_load_l2cache(spa);
4017 spa_config_exit(spa, SCL_ALL, FTAG);
4018 spa->spa_l2cache.sav_sync = B_TRUE;
4022 * Check for any removed devices.
4024 if (spa->spa_autoreplace) {
4025 spa_aux_check_removed(&spa->spa_spares);
4026 spa_aux_check_removed(&spa->spa_l2cache);
4029 if (spa_writeable(spa)) {
4031 * Update the config cache to include the newly-imported pool.
4033 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4037 * It's possible that the pool was expanded while it was exported.
4038 * We kick off an async task to handle this for us.
4040 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4042 mutex_exit(&spa_namespace_lock);
4043 spa_history_log_version(spa, LOG_POOL_IMPORT);
4047 zvol_create_minors(pool);
4054 spa_tryimport(nvlist_t *tryconfig)
4056 nvlist_t *config = NULL;
4062 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4065 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4069 * Create and initialize the spa structure.
4071 mutex_enter(&spa_namespace_lock);
4072 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4073 spa_activate(spa, FREAD);
4076 * Pass off the heavy lifting to spa_load().
4077 * Pass TRUE for mosconfig because the user-supplied config
4078 * is actually the one to trust when doing an import.
4080 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4083 * If 'tryconfig' was at least parsable, return the current config.
4085 if (spa->spa_root_vdev != NULL) {
4086 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4087 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4089 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4091 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4092 spa->spa_uberblock.ub_timestamp) == 0);
4093 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4094 spa->spa_load_info) == 0);
4097 * If the bootfs property exists on this pool then we
4098 * copy it out so that external consumers can tell which
4099 * pools are bootable.
4101 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4102 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4105 * We have to play games with the name since the
4106 * pool was opened as TRYIMPORT_NAME.
4108 if (dsl_dsobj_to_dsname(spa_name(spa),
4109 spa->spa_bootfs, tmpname) == 0) {
4111 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4113 cp = strchr(tmpname, '/');
4115 (void) strlcpy(dsname, tmpname,
4118 (void) snprintf(dsname, MAXPATHLEN,
4119 "%s/%s", poolname, ++cp);
4121 VERIFY(nvlist_add_string(config,
4122 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4123 kmem_free(dsname, MAXPATHLEN);
4125 kmem_free(tmpname, MAXPATHLEN);
4129 * Add the list of hot spares and level 2 cache devices.
4131 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4132 spa_add_spares(spa, config);
4133 spa_add_l2cache(spa, config);
4134 spa_config_exit(spa, SCL_CONFIG, FTAG);
4138 spa_deactivate(spa);
4140 mutex_exit(&spa_namespace_lock);
4146 * Pool export/destroy
4148 * The act of destroying or exporting a pool is very simple. We make sure there
4149 * is no more pending I/O and any references to the pool are gone. Then, we
4150 * update the pool state and sync all the labels to disk, removing the
4151 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4152 * we don't sync the labels or remove the configuration cache.
4155 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4156 boolean_t force, boolean_t hardforce)
4163 if (!(spa_mode_global & FWRITE))
4166 mutex_enter(&spa_namespace_lock);
4167 if ((spa = spa_lookup(pool)) == NULL) {
4168 mutex_exit(&spa_namespace_lock);
4173 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4174 * reacquire the namespace lock, and see if we can export.
4176 spa_open_ref(spa, FTAG);
4177 mutex_exit(&spa_namespace_lock);
4178 spa_async_suspend(spa);
4179 mutex_enter(&spa_namespace_lock);
4180 spa_close(spa, FTAG);
4183 * The pool will be in core if it's openable,
4184 * in which case we can modify its state.
4186 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4188 * Objsets may be open only because they're dirty, so we
4189 * have to force it to sync before checking spa_refcnt.
4191 txg_wait_synced(spa->spa_dsl_pool, 0);
4194 * A pool cannot be exported or destroyed if there are active
4195 * references. If we are resetting a pool, allow references by
4196 * fault injection handlers.
4198 if (!spa_refcount_zero(spa) ||
4199 (spa->spa_inject_ref != 0 &&
4200 new_state != POOL_STATE_UNINITIALIZED)) {
4201 spa_async_resume(spa);
4202 mutex_exit(&spa_namespace_lock);
4207 * A pool cannot be exported if it has an active shared spare.
4208 * This is to prevent other pools stealing the active spare
4209 * from an exported pool. At user's own will, such pool can
4210 * be forcedly exported.
4212 if (!force && new_state == POOL_STATE_EXPORTED &&
4213 spa_has_active_shared_spare(spa)) {
4214 spa_async_resume(spa);
4215 mutex_exit(&spa_namespace_lock);
4220 * We want this to be reflected on every label,
4221 * so mark them all dirty. spa_unload() will do the
4222 * final sync that pushes these changes out.
4224 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4225 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4226 spa->spa_state = new_state;
4227 spa->spa_final_txg = spa_last_synced_txg(spa) +
4229 vdev_config_dirty(spa->spa_root_vdev);
4230 spa_config_exit(spa, SCL_ALL, FTAG);
4234 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4236 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4238 spa_deactivate(spa);
4241 if (oldconfig && spa->spa_config)
4242 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4244 if (new_state != POOL_STATE_UNINITIALIZED) {
4246 spa_config_sync(spa, B_TRUE, B_TRUE);
4249 mutex_exit(&spa_namespace_lock);
4255 * Destroy a storage pool.
4258 spa_destroy(char *pool)
4260 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4265 * Export a storage pool.
4268 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4269 boolean_t hardforce)
4271 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4276 * Similar to spa_export(), this unloads the spa_t without actually removing it
4277 * from the namespace in any way.
4280 spa_reset(char *pool)
4282 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4287 * ==========================================================================
4288 * Device manipulation
4289 * ==========================================================================
4293 * Add a device to a storage pool.
4296 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4300 vdev_t *rvd = spa->spa_root_vdev;
4302 nvlist_t **spares, **l2cache;
4303 uint_t nspares, nl2cache;
4305 ASSERT(spa_writeable(spa));
4307 txg = spa_vdev_enter(spa);
4309 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4310 VDEV_ALLOC_ADD)) != 0)
4311 return (spa_vdev_exit(spa, NULL, txg, error));
4313 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4315 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4319 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4323 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4324 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4326 if (vd->vdev_children != 0 &&
4327 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4328 return (spa_vdev_exit(spa, vd, txg, error));
4331 * We must validate the spares and l2cache devices after checking the
4332 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4334 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4335 return (spa_vdev_exit(spa, vd, txg, error));
4338 * Transfer each new top-level vdev from vd to rvd.
4340 for (int c = 0; c < vd->vdev_children; c++) {
4343 * Set the vdev id to the first hole, if one exists.
4345 for (id = 0; id < rvd->vdev_children; id++) {
4346 if (rvd->vdev_child[id]->vdev_ishole) {
4347 vdev_free(rvd->vdev_child[id]);
4351 tvd = vd->vdev_child[c];
4352 vdev_remove_child(vd, tvd);
4354 vdev_add_child(rvd, tvd);
4355 vdev_config_dirty(tvd);
4359 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4360 ZPOOL_CONFIG_SPARES);
4361 spa_load_spares(spa);
4362 spa->spa_spares.sav_sync = B_TRUE;
4365 if (nl2cache != 0) {
4366 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4367 ZPOOL_CONFIG_L2CACHE);
4368 spa_load_l2cache(spa);
4369 spa->spa_l2cache.sav_sync = B_TRUE;
4373 * We have to be careful when adding new vdevs to an existing pool.
4374 * If other threads start allocating from these vdevs before we
4375 * sync the config cache, and we lose power, then upon reboot we may
4376 * fail to open the pool because there are DVAs that the config cache
4377 * can't translate. Therefore, we first add the vdevs without
4378 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4379 * and then let spa_config_update() initialize the new metaslabs.
4381 * spa_load() checks for added-but-not-initialized vdevs, so that
4382 * if we lose power at any point in this sequence, the remaining
4383 * steps will be completed the next time we load the pool.
4385 (void) spa_vdev_exit(spa, vd, txg, 0);
4387 mutex_enter(&spa_namespace_lock);
4388 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4389 mutex_exit(&spa_namespace_lock);
4395 * Attach a device to a mirror. The arguments are the path to any device
4396 * in the mirror, and the nvroot for the new device. If the path specifies
4397 * a device that is not mirrored, we automatically insert the mirror vdev.
4399 * If 'replacing' is specified, the new device is intended to replace the
4400 * existing device; in this case the two devices are made into their own
4401 * mirror using the 'replacing' vdev, which is functionally identical to
4402 * the mirror vdev (it actually reuses all the same ops) but has a few
4403 * extra rules: you can't attach to it after it's been created, and upon
4404 * completion of resilvering, the first disk (the one being replaced)
4405 * is automatically detached.
4408 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4410 uint64_t txg, dtl_max_txg;
4411 vdev_t *rvd = spa->spa_root_vdev;
4412 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4414 char *oldvdpath, *newvdpath;
4418 ASSERT(spa_writeable(spa));
4420 txg = spa_vdev_enter(spa);
4422 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4425 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4427 if (!oldvd->vdev_ops->vdev_op_leaf)
4428 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4430 pvd = oldvd->vdev_parent;
4432 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4433 VDEV_ALLOC_ATTACH)) != 0)
4434 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4436 if (newrootvd->vdev_children != 1)
4437 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4439 newvd = newrootvd->vdev_child[0];
4441 if (!newvd->vdev_ops->vdev_op_leaf)
4442 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4444 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4445 return (spa_vdev_exit(spa, newrootvd, txg, error));
4448 * Spares can't replace logs
4450 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4451 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4455 * For attach, the only allowable parent is a mirror or the root
4458 if (pvd->vdev_ops != &vdev_mirror_ops &&
4459 pvd->vdev_ops != &vdev_root_ops)
4460 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4462 pvops = &vdev_mirror_ops;
4465 * Active hot spares can only be replaced by inactive hot
4468 if (pvd->vdev_ops == &vdev_spare_ops &&
4469 oldvd->vdev_isspare &&
4470 !spa_has_spare(spa, newvd->vdev_guid))
4471 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4474 * If the source is a hot spare, and the parent isn't already a
4475 * spare, then we want to create a new hot spare. Otherwise, we
4476 * want to create a replacing vdev. The user is not allowed to
4477 * attach to a spared vdev child unless the 'isspare' state is
4478 * the same (spare replaces spare, non-spare replaces
4481 if (pvd->vdev_ops == &vdev_replacing_ops &&
4482 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4483 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4484 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4485 newvd->vdev_isspare != oldvd->vdev_isspare) {
4486 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4489 if (newvd->vdev_isspare)
4490 pvops = &vdev_spare_ops;
4492 pvops = &vdev_replacing_ops;
4496 * Make sure the new device is big enough.
4498 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4499 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4502 * The new device cannot have a higher alignment requirement
4503 * than the top-level vdev.
4505 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4506 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4509 * If this is an in-place replacement, update oldvd's path and devid
4510 * to make it distinguishable from newvd, and unopenable from now on.
4512 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4513 spa_strfree(oldvd->vdev_path);
4514 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4516 (void) sprintf(oldvd->vdev_path, "%s/%s",
4517 newvd->vdev_path, "old");
4518 if (oldvd->vdev_devid != NULL) {
4519 spa_strfree(oldvd->vdev_devid);
4520 oldvd->vdev_devid = NULL;
4524 /* mark the device being resilvered */
4525 newvd->vdev_resilvering = B_TRUE;
4528 * If the parent is not a mirror, or if we're replacing, insert the new
4529 * mirror/replacing/spare vdev above oldvd.
4531 if (pvd->vdev_ops != pvops)
4532 pvd = vdev_add_parent(oldvd, pvops);
4534 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4535 ASSERT(pvd->vdev_ops == pvops);
4536 ASSERT(oldvd->vdev_parent == pvd);
4539 * Extract the new device from its root and add it to pvd.
4541 vdev_remove_child(newrootvd, newvd);
4542 newvd->vdev_id = pvd->vdev_children;
4543 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4544 vdev_add_child(pvd, newvd);
4546 tvd = newvd->vdev_top;
4547 ASSERT(pvd->vdev_top == tvd);
4548 ASSERT(tvd->vdev_parent == rvd);
4550 vdev_config_dirty(tvd);
4553 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4554 * for any dmu_sync-ed blocks. It will propagate upward when
4555 * spa_vdev_exit() calls vdev_dtl_reassess().
4557 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4559 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4560 dtl_max_txg - TXG_INITIAL);
4562 if (newvd->vdev_isspare) {
4563 spa_spare_activate(newvd);
4564 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4567 oldvdpath = spa_strdup(oldvd->vdev_path);
4568 newvdpath = spa_strdup(newvd->vdev_path);
4569 newvd_isspare = newvd->vdev_isspare;
4572 * Mark newvd's DTL dirty in this txg.
4574 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4577 * Restart the resilver
4579 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4584 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4586 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
4587 "%s vdev=%s %s vdev=%s",
4588 replacing && newvd_isspare ? "spare in" :
4589 replacing ? "replace" : "attach", newvdpath,
4590 replacing ? "for" : "to", oldvdpath);
4592 spa_strfree(oldvdpath);
4593 spa_strfree(newvdpath);
4595 if (spa->spa_bootfs)
4596 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4602 * Detach a device from a mirror or replacing vdev.
4603 * If 'replace_done' is specified, only detach if the parent
4604 * is a replacing vdev.
4607 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4611 vdev_t *rvd = spa->spa_root_vdev;
4612 vdev_t *vd, *pvd, *cvd, *tvd;
4613 boolean_t unspare = B_FALSE;
4614 uint64_t unspare_guid;
4617 ASSERT(spa_writeable(spa));
4619 txg = spa_vdev_enter(spa);
4621 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4624 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4626 if (!vd->vdev_ops->vdev_op_leaf)
4627 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4629 pvd = vd->vdev_parent;
4632 * If the parent/child relationship is not as expected, don't do it.
4633 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4634 * vdev that's replacing B with C. The user's intent in replacing
4635 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4636 * the replace by detaching C, the expected behavior is to end up
4637 * M(A,B). But suppose that right after deciding to detach C,
4638 * the replacement of B completes. We would have M(A,C), and then
4639 * ask to detach C, which would leave us with just A -- not what
4640 * the user wanted. To prevent this, we make sure that the
4641 * parent/child relationship hasn't changed -- in this example,
4642 * that C's parent is still the replacing vdev R.
4644 if (pvd->vdev_guid != pguid && pguid != 0)
4645 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4648 * Only 'replacing' or 'spare' vdevs can be replaced.
4650 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4651 pvd->vdev_ops != &vdev_spare_ops)
4652 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4654 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4655 spa_version(spa) >= SPA_VERSION_SPARES);
4658 * Only mirror, replacing, and spare vdevs support detach.
4660 if (pvd->vdev_ops != &vdev_replacing_ops &&
4661 pvd->vdev_ops != &vdev_mirror_ops &&
4662 pvd->vdev_ops != &vdev_spare_ops)
4663 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4666 * If this device has the only valid copy of some data,
4667 * we cannot safely detach it.
4669 if (vdev_dtl_required(vd))
4670 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4672 ASSERT(pvd->vdev_children >= 2);
4675 * If we are detaching the second disk from a replacing vdev, then
4676 * check to see if we changed the original vdev's path to have "/old"
4677 * at the end in spa_vdev_attach(). If so, undo that change now.
4679 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4680 vd->vdev_path != NULL) {
4681 size_t len = strlen(vd->vdev_path);
4683 for (int c = 0; c < pvd->vdev_children; c++) {
4684 cvd = pvd->vdev_child[c];
4686 if (cvd == vd || cvd->vdev_path == NULL)
4689 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4690 strcmp(cvd->vdev_path + len, "/old") == 0) {
4691 spa_strfree(cvd->vdev_path);
4692 cvd->vdev_path = spa_strdup(vd->vdev_path);
4699 * If we are detaching the original disk from a spare, then it implies
4700 * that the spare should become a real disk, and be removed from the
4701 * active spare list for the pool.
4703 if (pvd->vdev_ops == &vdev_spare_ops &&
4705 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4709 * Erase the disk labels so the disk can be used for other things.
4710 * This must be done after all other error cases are handled,
4711 * but before we disembowel vd (so we can still do I/O to it).
4712 * But if we can't do it, don't treat the error as fatal --
4713 * it may be that the unwritability of the disk is the reason
4714 * it's being detached!
4716 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4719 * Remove vd from its parent and compact the parent's children.
4721 vdev_remove_child(pvd, vd);
4722 vdev_compact_children(pvd);
4725 * Remember one of the remaining children so we can get tvd below.
4727 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4730 * If we need to remove the remaining child from the list of hot spares,
4731 * do it now, marking the vdev as no longer a spare in the process.
4732 * We must do this before vdev_remove_parent(), because that can
4733 * change the GUID if it creates a new toplevel GUID. For a similar
4734 * reason, we must remove the spare now, in the same txg as the detach;
4735 * otherwise someone could attach a new sibling, change the GUID, and
4736 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4739 ASSERT(cvd->vdev_isspare);
4740 spa_spare_remove(cvd);
4741 unspare_guid = cvd->vdev_guid;
4742 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4743 cvd->vdev_unspare = B_TRUE;
4747 * If the parent mirror/replacing vdev only has one child,
4748 * the parent is no longer needed. Remove it from the tree.
4750 if (pvd->vdev_children == 1) {
4751 if (pvd->vdev_ops == &vdev_spare_ops)
4752 cvd->vdev_unspare = B_FALSE;
4753 vdev_remove_parent(cvd);
4754 cvd->vdev_resilvering = B_FALSE;
4759 * We don't set tvd until now because the parent we just removed
4760 * may have been the previous top-level vdev.
4762 tvd = cvd->vdev_top;
4763 ASSERT(tvd->vdev_parent == rvd);
4766 * Reevaluate the parent vdev state.
4768 vdev_propagate_state(cvd);
4771 * If the 'autoexpand' property is set on the pool then automatically
4772 * try to expand the size of the pool. For example if the device we
4773 * just detached was smaller than the others, it may be possible to
4774 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4775 * first so that we can obtain the updated sizes of the leaf vdevs.
4777 if (spa->spa_autoexpand) {
4779 vdev_expand(tvd, txg);
4782 vdev_config_dirty(tvd);
4785 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4786 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4787 * But first make sure we're not on any *other* txg's DTL list, to
4788 * prevent vd from being accessed after it's freed.
4790 vdpath = spa_strdup(vd->vdev_path);
4791 for (int t = 0; t < TXG_SIZE; t++)
4792 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4793 vd->vdev_detached = B_TRUE;
4794 vdev_dirty(tvd, VDD_DTL, vd, txg);
4796 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4798 /* hang on to the spa before we release the lock */
4799 spa_open_ref(spa, FTAG);
4801 error = spa_vdev_exit(spa, vd, txg, 0);
4803 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4805 spa_strfree(vdpath);
4808 * If this was the removal of the original device in a hot spare vdev,
4809 * then we want to go through and remove the device from the hot spare
4810 * list of every other pool.
4813 spa_t *altspa = NULL;
4815 mutex_enter(&spa_namespace_lock);
4816 while ((altspa = spa_next(altspa)) != NULL) {
4817 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4821 spa_open_ref(altspa, FTAG);
4822 mutex_exit(&spa_namespace_lock);
4823 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4824 mutex_enter(&spa_namespace_lock);
4825 spa_close(altspa, FTAG);
4827 mutex_exit(&spa_namespace_lock);
4829 /* search the rest of the vdevs for spares to remove */
4830 spa_vdev_resilver_done(spa);
4833 /* all done with the spa; OK to release */
4834 mutex_enter(&spa_namespace_lock);
4835 spa_close(spa, FTAG);
4836 mutex_exit(&spa_namespace_lock);
4842 * Split a set of devices from their mirrors, and create a new pool from them.
4845 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4846 nvlist_t *props, boolean_t exp)
4849 uint64_t txg, *glist;
4851 uint_t c, children, lastlog;
4852 nvlist_t **child, *nvl, *tmp;
4854 char *altroot = NULL;
4855 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4856 boolean_t activate_slog;
4858 ASSERT(spa_writeable(spa));
4860 txg = spa_vdev_enter(spa);
4862 /* clear the log and flush everything up to now */
4863 activate_slog = spa_passivate_log(spa);
4864 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4865 error = spa_offline_log(spa);
4866 txg = spa_vdev_config_enter(spa);
4869 spa_activate_log(spa);
4872 return (spa_vdev_exit(spa, NULL, txg, error));
4874 /* check new spa name before going any further */
4875 if (spa_lookup(newname) != NULL)
4876 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4879 * scan through all the children to ensure they're all mirrors
4881 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4882 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4884 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4886 /* first, check to ensure we've got the right child count */
4887 rvd = spa->spa_root_vdev;
4889 for (c = 0; c < rvd->vdev_children; c++) {
4890 vdev_t *vd = rvd->vdev_child[c];
4892 /* don't count the holes & logs as children */
4893 if (vd->vdev_islog || vd->vdev_ishole) {
4901 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4902 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4904 /* next, ensure no spare or cache devices are part of the split */
4905 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4906 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4907 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4909 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
4910 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
4912 /* then, loop over each vdev and validate it */
4913 for (c = 0; c < children; c++) {
4914 uint64_t is_hole = 0;
4916 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4920 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4921 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4929 /* which disk is going to be split? */
4930 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4936 /* look it up in the spa */
4937 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4938 if (vml[c] == NULL) {
4943 /* make sure there's nothing stopping the split */
4944 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4945 vml[c]->vdev_islog ||
4946 vml[c]->vdev_ishole ||
4947 vml[c]->vdev_isspare ||
4948 vml[c]->vdev_isl2cache ||
4949 !vdev_writeable(vml[c]) ||
4950 vml[c]->vdev_children != 0 ||
4951 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4952 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4957 if (vdev_dtl_required(vml[c])) {
4962 /* we need certain info from the top level */
4963 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4964 vml[c]->vdev_top->vdev_ms_array) == 0);
4965 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4966 vml[c]->vdev_top->vdev_ms_shift) == 0);
4967 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4968 vml[c]->vdev_top->vdev_asize) == 0);
4969 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4970 vml[c]->vdev_top->vdev_ashift) == 0);
4974 kmem_free(vml, children * sizeof (vdev_t *));
4975 kmem_free(glist, children * sizeof (uint64_t));
4976 return (spa_vdev_exit(spa, NULL, txg, error));
4979 /* stop writers from using the disks */
4980 for (c = 0; c < children; c++) {
4982 vml[c]->vdev_offline = B_TRUE;
4984 vdev_reopen(spa->spa_root_vdev);
4987 * Temporarily record the splitting vdevs in the spa config. This
4988 * will disappear once the config is regenerated.
4990 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4991 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4992 glist, children) == 0);
4993 kmem_free(glist, children * sizeof (uint64_t));
4995 mutex_enter(&spa->spa_props_lock);
4996 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4998 mutex_exit(&spa->spa_props_lock);
4999 spa->spa_config_splitting = nvl;
5000 vdev_config_dirty(spa->spa_root_vdev);
5002 /* configure and create the new pool */
5003 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5004 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5005 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5006 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5007 spa_version(spa)) == 0);
5008 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5009 spa->spa_config_txg) == 0);
5010 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5011 spa_generate_guid(NULL)) == 0);
5012 (void) nvlist_lookup_string(props,
5013 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5015 /* add the new pool to the namespace */
5016 newspa = spa_add(newname, config, altroot);
5017 newspa->spa_config_txg = spa->spa_config_txg;
5018 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5020 /* release the spa config lock, retaining the namespace lock */
5021 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5023 if (zio_injection_enabled)
5024 zio_handle_panic_injection(spa, FTAG, 1);
5026 spa_activate(newspa, spa_mode_global);
5027 spa_async_suspend(newspa);
5030 /* mark that we are creating new spa by splitting */
5031 newspa->spa_splitting_newspa = B_TRUE;
5033 /* create the new pool from the disks of the original pool */
5034 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5036 newspa->spa_splitting_newspa = B_FALSE;
5041 /* if that worked, generate a real config for the new pool */
5042 if (newspa->spa_root_vdev != NULL) {
5043 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5044 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5045 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5046 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5047 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5052 if (props != NULL) {
5053 spa_configfile_set(newspa, props, B_FALSE);
5054 error = spa_prop_set(newspa, props);
5059 /* flush everything */
5060 txg = spa_vdev_config_enter(newspa);
5061 vdev_config_dirty(newspa->spa_root_vdev);
5062 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5064 if (zio_injection_enabled)
5065 zio_handle_panic_injection(spa, FTAG, 2);
5067 spa_async_resume(newspa);
5069 /* finally, update the original pool's config */
5070 txg = spa_vdev_config_enter(spa);
5071 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5072 error = dmu_tx_assign(tx, TXG_WAIT);
5075 for (c = 0; c < children; c++) {
5076 if (vml[c] != NULL) {
5079 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
5085 vdev_config_dirty(spa->spa_root_vdev);
5086 spa->spa_config_splitting = NULL;
5090 (void) spa_vdev_exit(spa, NULL, txg, 0);
5092 if (zio_injection_enabled)
5093 zio_handle_panic_injection(spa, FTAG, 3);
5095 /* split is complete; log a history record */
5096 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
5097 "split new pool %s from pool %s", newname, spa_name(spa));
5099 kmem_free(vml, children * sizeof (vdev_t *));
5101 /* if we're not going to mount the filesystems in userland, export */
5103 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5110 spa_deactivate(newspa);
5113 txg = spa_vdev_config_enter(spa);
5115 /* re-online all offlined disks */
5116 for (c = 0; c < children; c++) {
5118 vml[c]->vdev_offline = B_FALSE;
5120 vdev_reopen(spa->spa_root_vdev);
5122 nvlist_free(spa->spa_config_splitting);
5123 spa->spa_config_splitting = NULL;
5124 (void) spa_vdev_exit(spa, NULL, txg, error);
5126 kmem_free(vml, children * sizeof (vdev_t *));
5131 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5133 for (int i = 0; i < count; i++) {
5136 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5139 if (guid == target_guid)
5147 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5148 nvlist_t *dev_to_remove)
5150 nvlist_t **newdev = NULL;
5153 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5155 for (int i = 0, j = 0; i < count; i++) {
5156 if (dev[i] == dev_to_remove)
5158 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5161 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5162 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5164 for (int i = 0; i < count - 1; i++)
5165 nvlist_free(newdev[i]);
5168 kmem_free(newdev, (count - 1) * sizeof (void *));
5172 * Evacuate the device.
5175 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5180 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5181 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5182 ASSERT(vd == vd->vdev_top);
5185 * Evacuate the device. We don't hold the config lock as writer
5186 * since we need to do I/O but we do keep the
5187 * spa_namespace_lock held. Once this completes the device
5188 * should no longer have any blocks allocated on it.
5190 if (vd->vdev_islog) {
5191 if (vd->vdev_stat.vs_alloc != 0)
5192 error = spa_offline_log(spa);
5201 * The evacuation succeeded. Remove any remaining MOS metadata
5202 * associated with this vdev, and wait for these changes to sync.
5204 ASSERT0(vd->vdev_stat.vs_alloc);
5205 txg = spa_vdev_config_enter(spa);
5206 vd->vdev_removing = B_TRUE;
5207 vdev_dirty(vd, 0, NULL, txg);
5208 vdev_config_dirty(vd);
5209 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5215 * Complete the removal by cleaning up the namespace.
5218 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5220 vdev_t *rvd = spa->spa_root_vdev;
5221 uint64_t id = vd->vdev_id;
5222 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5224 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5225 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5226 ASSERT(vd == vd->vdev_top);
5229 * Only remove any devices which are empty.
5231 if (vd->vdev_stat.vs_alloc != 0)
5234 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5236 if (list_link_active(&vd->vdev_state_dirty_node))
5237 vdev_state_clean(vd);
5238 if (list_link_active(&vd->vdev_config_dirty_node))
5239 vdev_config_clean(vd);
5244 vdev_compact_children(rvd);
5246 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5247 vdev_add_child(rvd, vd);
5249 vdev_config_dirty(rvd);
5252 * Reassess the health of our root vdev.
5258 * Remove a device from the pool -
5260 * Removing a device from the vdev namespace requires several steps
5261 * and can take a significant amount of time. As a result we use
5262 * the spa_vdev_config_[enter/exit] functions which allow us to
5263 * grab and release the spa_config_lock while still holding the namespace
5264 * lock. During each step the configuration is synced out.
5268 * Remove a device from the pool. Currently, this supports removing only hot
5269 * spares, slogs, and level 2 ARC devices.
5272 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5275 metaslab_group_t *mg;
5276 nvlist_t **spares, **l2cache, *nv;
5278 uint_t nspares, nl2cache;
5280 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5282 ASSERT(spa_writeable(spa));
5285 txg = spa_vdev_enter(spa);
5287 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5289 if (spa->spa_spares.sav_vdevs != NULL &&
5290 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5291 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5292 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5294 * Only remove the hot spare if it's not currently in use
5297 if (vd == NULL || unspare) {
5298 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5299 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5300 spa_load_spares(spa);
5301 spa->spa_spares.sav_sync = B_TRUE;
5305 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5306 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5307 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5308 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5310 * Cache devices can always be removed.
5312 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5313 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5314 spa_load_l2cache(spa);
5315 spa->spa_l2cache.sav_sync = B_TRUE;
5316 } else if (vd != NULL && vd->vdev_islog) {
5318 ASSERT(vd == vd->vdev_top);
5321 * XXX - Once we have bp-rewrite this should
5322 * become the common case.
5328 * Stop allocating from this vdev.
5330 metaslab_group_passivate(mg);
5333 * Wait for the youngest allocations and frees to sync,
5334 * and then wait for the deferral of those frees to finish.
5336 spa_vdev_config_exit(spa, NULL,
5337 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5340 * Attempt to evacuate the vdev.
5342 error = spa_vdev_remove_evacuate(spa, vd);
5344 txg = spa_vdev_config_enter(spa);
5347 * If we couldn't evacuate the vdev, unwind.
5350 metaslab_group_activate(mg);
5351 return (spa_vdev_exit(spa, NULL, txg, error));
5355 * Clean up the vdev namespace.
5357 spa_vdev_remove_from_namespace(spa, vd);
5359 } else if (vd != NULL) {
5361 * Normal vdevs cannot be removed (yet).
5366 * There is no vdev of any kind with the specified guid.
5372 return (spa_vdev_exit(spa, NULL, txg, error));
5378 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5379 * current spared, so we can detach it.
5382 spa_vdev_resilver_done_hunt(vdev_t *vd)
5384 vdev_t *newvd, *oldvd;
5386 for (int c = 0; c < vd->vdev_children; c++) {
5387 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5393 * Check for a completed replacement. We always consider the first
5394 * vdev in the list to be the oldest vdev, and the last one to be
5395 * the newest (see spa_vdev_attach() for how that works). In
5396 * the case where the newest vdev is faulted, we will not automatically
5397 * remove it after a resilver completes. This is OK as it will require
5398 * user intervention to determine which disk the admin wishes to keep.
5400 if (vd->vdev_ops == &vdev_replacing_ops) {
5401 ASSERT(vd->vdev_children > 1);
5403 newvd = vd->vdev_child[vd->vdev_children - 1];
5404 oldvd = vd->vdev_child[0];
5406 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5407 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5408 !vdev_dtl_required(oldvd))
5413 * Check for a completed resilver with the 'unspare' flag set.
5415 if (vd->vdev_ops == &vdev_spare_ops) {
5416 vdev_t *first = vd->vdev_child[0];
5417 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5419 if (last->vdev_unspare) {
5422 } else if (first->vdev_unspare) {
5429 if (oldvd != NULL &&
5430 vdev_dtl_empty(newvd, DTL_MISSING) &&
5431 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5432 !vdev_dtl_required(oldvd))
5436 * If there are more than two spares attached to a disk,
5437 * and those spares are not required, then we want to
5438 * attempt to free them up now so that they can be used
5439 * by other pools. Once we're back down to a single
5440 * disk+spare, we stop removing them.
5442 if (vd->vdev_children > 2) {
5443 newvd = vd->vdev_child[1];
5445 if (newvd->vdev_isspare && last->vdev_isspare &&
5446 vdev_dtl_empty(last, DTL_MISSING) &&
5447 vdev_dtl_empty(last, DTL_OUTAGE) &&
5448 !vdev_dtl_required(newvd))
5457 spa_vdev_resilver_done(spa_t *spa)
5459 vdev_t *vd, *pvd, *ppvd;
5460 uint64_t guid, sguid, pguid, ppguid;
5462 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5464 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5465 pvd = vd->vdev_parent;
5466 ppvd = pvd->vdev_parent;
5467 guid = vd->vdev_guid;
5468 pguid = pvd->vdev_guid;
5469 ppguid = ppvd->vdev_guid;
5472 * If we have just finished replacing a hot spared device, then
5473 * we need to detach the parent's first child (the original hot
5476 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5477 ppvd->vdev_children == 2) {
5478 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5479 sguid = ppvd->vdev_child[1]->vdev_guid;
5481 spa_config_exit(spa, SCL_ALL, FTAG);
5482 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5484 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5486 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5489 spa_config_exit(spa, SCL_ALL, FTAG);
5493 * Update the stored path or FRU for this vdev.
5496 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5500 boolean_t sync = B_FALSE;
5502 ASSERT(spa_writeable(spa));
5504 spa_vdev_state_enter(spa, SCL_ALL);
5506 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5507 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5509 if (!vd->vdev_ops->vdev_op_leaf)
5510 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5513 if (strcmp(value, vd->vdev_path) != 0) {
5514 spa_strfree(vd->vdev_path);
5515 vd->vdev_path = spa_strdup(value);
5519 if (vd->vdev_fru == NULL) {
5520 vd->vdev_fru = spa_strdup(value);
5522 } else if (strcmp(value, vd->vdev_fru) != 0) {
5523 spa_strfree(vd->vdev_fru);
5524 vd->vdev_fru = spa_strdup(value);
5529 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5533 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5535 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5539 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5541 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5545 * ==========================================================================
5547 * ==========================================================================
5551 spa_scan_stop(spa_t *spa)
5553 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5554 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5556 return (dsl_scan_cancel(spa->spa_dsl_pool));
5560 spa_scan(spa_t *spa, pool_scan_func_t func)
5562 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5564 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5568 * If a resilver was requested, but there is no DTL on a
5569 * writeable leaf device, we have nothing to do.
5571 if (func == POOL_SCAN_RESILVER &&
5572 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5573 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5577 return (dsl_scan(spa->spa_dsl_pool, func));
5581 * ==========================================================================
5582 * SPA async task processing
5583 * ==========================================================================
5587 spa_async_remove(spa_t *spa, vdev_t *vd)
5589 if (vd->vdev_remove_wanted) {
5590 vd->vdev_remove_wanted = B_FALSE;
5591 vd->vdev_delayed_close = B_FALSE;
5592 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5595 * We want to clear the stats, but we don't want to do a full
5596 * vdev_clear() as that will cause us to throw away
5597 * degraded/faulted state as well as attempt to reopen the
5598 * device, all of which is a waste.
5600 vd->vdev_stat.vs_read_errors = 0;
5601 vd->vdev_stat.vs_write_errors = 0;
5602 vd->vdev_stat.vs_checksum_errors = 0;
5604 vdev_state_dirty(vd->vdev_top);
5607 for (int c = 0; c < vd->vdev_children; c++)
5608 spa_async_remove(spa, vd->vdev_child[c]);
5612 spa_async_probe(spa_t *spa, vdev_t *vd)
5614 if (vd->vdev_probe_wanted) {
5615 vd->vdev_probe_wanted = B_FALSE;
5616 vdev_reopen(vd); /* vdev_open() does the actual probe */
5619 for (int c = 0; c < vd->vdev_children; c++)
5620 spa_async_probe(spa, vd->vdev_child[c]);
5624 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5630 if (!spa->spa_autoexpand)
5633 for (int c = 0; c < vd->vdev_children; c++) {
5634 vdev_t *cvd = vd->vdev_child[c];
5635 spa_async_autoexpand(spa, cvd);
5638 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5641 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5642 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5644 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5645 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5647 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5648 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5651 kmem_free(physpath, MAXPATHLEN);
5655 spa_async_thread(void *arg)
5660 ASSERT(spa->spa_sync_on);
5662 mutex_enter(&spa->spa_async_lock);
5663 tasks = spa->spa_async_tasks;
5664 spa->spa_async_tasks = 0;
5665 mutex_exit(&spa->spa_async_lock);
5668 * See if the config needs to be updated.
5670 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5671 uint64_t old_space, new_space;
5673 mutex_enter(&spa_namespace_lock);
5674 old_space = metaslab_class_get_space(spa_normal_class(spa));
5675 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5676 new_space = metaslab_class_get_space(spa_normal_class(spa));
5677 mutex_exit(&spa_namespace_lock);
5680 * If the pool grew as a result of the config update,
5681 * then log an internal history event.
5683 if (new_space != old_space) {
5684 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5686 "pool '%s' size: %llu(+%llu)",
5687 spa_name(spa), new_space, new_space - old_space);
5692 * See if any devices need to be marked REMOVED.
5694 if (tasks & SPA_ASYNC_REMOVE) {
5695 spa_vdev_state_enter(spa, SCL_NONE);
5696 spa_async_remove(spa, spa->spa_root_vdev);
5697 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5698 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5699 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5700 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5701 (void) spa_vdev_state_exit(spa, NULL, 0);
5704 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5705 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5706 spa_async_autoexpand(spa, spa->spa_root_vdev);
5707 spa_config_exit(spa, SCL_CONFIG, FTAG);
5711 * See if any devices need to be probed.
5713 if (tasks & SPA_ASYNC_PROBE) {
5714 spa_vdev_state_enter(spa, SCL_NONE);
5715 spa_async_probe(spa, spa->spa_root_vdev);
5716 (void) spa_vdev_state_exit(spa, NULL, 0);
5720 * If any devices are done replacing, detach them.
5722 if (tasks & SPA_ASYNC_RESILVER_DONE)
5723 spa_vdev_resilver_done(spa);
5726 * Kick off a resilver.
5728 if (tasks & SPA_ASYNC_RESILVER)
5729 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5732 * Let the world know that we're done.
5734 mutex_enter(&spa->spa_async_lock);
5735 spa->spa_async_thread = NULL;
5736 cv_broadcast(&spa->spa_async_cv);
5737 mutex_exit(&spa->spa_async_lock);
5742 spa_async_suspend(spa_t *spa)
5744 mutex_enter(&spa->spa_async_lock);
5745 spa->spa_async_suspended++;
5746 while (spa->spa_async_thread != NULL)
5747 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5748 mutex_exit(&spa->spa_async_lock);
5752 spa_async_resume(spa_t *spa)
5754 mutex_enter(&spa->spa_async_lock);
5755 ASSERT(spa->spa_async_suspended != 0);
5756 spa->spa_async_suspended--;
5757 mutex_exit(&spa->spa_async_lock);
5761 spa_async_dispatch(spa_t *spa)
5763 mutex_enter(&spa->spa_async_lock);
5764 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5765 spa->spa_async_thread == NULL &&
5766 rootdir != NULL && !vn_is_readonly(rootdir))
5767 spa->spa_async_thread = thread_create(NULL, 0,
5768 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5769 mutex_exit(&spa->spa_async_lock);
5773 spa_async_request(spa_t *spa, int task)
5775 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5776 mutex_enter(&spa->spa_async_lock);
5777 spa->spa_async_tasks |= task;
5778 mutex_exit(&spa->spa_async_lock);
5782 * ==========================================================================
5783 * SPA syncing routines
5784 * ==========================================================================
5788 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5791 bpobj_enqueue(bpo, bp, tx);
5796 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5800 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5806 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5808 char *packed = NULL;
5813 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5816 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5817 * information. This avoids the dbuf_will_dirty() path and
5818 * saves us a pre-read to get data we don't actually care about.
5820 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
5821 packed = kmem_alloc(bufsize, KM_SLEEP);
5823 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5825 bzero(packed + nvsize, bufsize - nvsize);
5827 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5829 kmem_free(packed, bufsize);
5831 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5832 dmu_buf_will_dirty(db, tx);
5833 *(uint64_t *)db->db_data = nvsize;
5834 dmu_buf_rele(db, FTAG);
5838 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5839 const char *config, const char *entry)
5849 * Update the MOS nvlist describing the list of available devices.
5850 * spa_validate_aux() will have already made sure this nvlist is
5851 * valid and the vdevs are labeled appropriately.
5853 if (sav->sav_object == 0) {
5854 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5855 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5856 sizeof (uint64_t), tx);
5857 VERIFY(zap_update(spa->spa_meta_objset,
5858 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5859 &sav->sav_object, tx) == 0);
5862 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5863 if (sav->sav_count == 0) {
5864 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5866 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5867 for (i = 0; i < sav->sav_count; i++)
5868 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5869 B_FALSE, VDEV_CONFIG_L2CACHE);
5870 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5871 sav->sav_count) == 0);
5872 for (i = 0; i < sav->sav_count; i++)
5873 nvlist_free(list[i]);
5874 kmem_free(list, sav->sav_count * sizeof (void *));
5877 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5878 nvlist_free(nvroot);
5880 sav->sav_sync = B_FALSE;
5884 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5888 if (list_is_empty(&spa->spa_config_dirty_list))
5891 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5893 config = spa_config_generate(spa, spa->spa_root_vdev,
5894 dmu_tx_get_txg(tx), B_FALSE);
5897 * If we're upgrading the spa version then make sure that
5898 * the config object gets updated with the correct version.
5900 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
5901 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5902 spa->spa_uberblock.ub_version);
5904 spa_config_exit(spa, SCL_STATE, FTAG);
5906 if (spa->spa_config_syncing)
5907 nvlist_free(spa->spa_config_syncing);
5908 spa->spa_config_syncing = config;
5910 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5914 spa_sync_version(void *arg1, void *arg2, dmu_tx_t *tx)
5917 uint64_t version = *(uint64_t *)arg2;
5920 * Setting the version is special cased when first creating the pool.
5922 ASSERT(tx->tx_txg != TXG_INITIAL);
5924 ASSERT(version <= SPA_VERSION);
5925 ASSERT(version >= spa_version(spa));
5927 spa->spa_uberblock.ub_version = version;
5928 vdev_config_dirty(spa->spa_root_vdev);
5932 * Set zpool properties.
5935 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5938 objset_t *mos = spa->spa_meta_objset;
5939 nvlist_t *nvp = arg2;
5940 nvpair_t *elem = NULL;
5942 mutex_enter(&spa->spa_props_lock);
5944 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5946 char *strval, *fname;
5948 const char *propname;
5949 zprop_type_t proptype;
5950 zfeature_info_t *feature;
5952 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
5955 * We checked this earlier in spa_prop_validate().
5957 ASSERT(zpool_prop_feature(nvpair_name(elem)));
5959 fname = strchr(nvpair_name(elem), '@') + 1;
5960 VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature));
5962 spa_feature_enable(spa, feature, tx);
5965 case ZPOOL_PROP_VERSION:
5966 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5968 * The version is synced seperatly before other
5969 * properties and should be correct by now.
5971 ASSERT3U(spa_version(spa), >=, intval);
5974 case ZPOOL_PROP_ALTROOT:
5976 * 'altroot' is a non-persistent property. It should
5977 * have been set temporarily at creation or import time.
5979 ASSERT(spa->spa_root != NULL);
5982 case ZPOOL_PROP_READONLY:
5983 case ZPOOL_PROP_CACHEFILE:
5985 * 'readonly' and 'cachefile' are also non-persisitent
5989 case ZPOOL_PROP_COMMENT:
5990 VERIFY(nvpair_value_string(elem, &strval) == 0);
5991 if (spa->spa_comment != NULL)
5992 spa_strfree(spa->spa_comment);
5993 spa->spa_comment = spa_strdup(strval);
5995 * We need to dirty the configuration on all the vdevs
5996 * so that their labels get updated. It's unnecessary
5997 * to do this for pool creation since the vdev's
5998 * configuratoin has already been dirtied.
6000 if (tx->tx_txg != TXG_INITIAL)
6001 vdev_config_dirty(spa->spa_root_vdev);
6005 * Set pool property values in the poolprops mos object.
6007 if (spa->spa_pool_props_object == 0) {
6008 spa->spa_pool_props_object =
6009 zap_create_link(mos, DMU_OT_POOL_PROPS,
6010 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6014 /* normalize the property name */
6015 propname = zpool_prop_to_name(prop);
6016 proptype = zpool_prop_get_type(prop);
6018 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6019 ASSERT(proptype == PROP_TYPE_STRING);
6020 VERIFY(nvpair_value_string(elem, &strval) == 0);
6021 VERIFY(zap_update(mos,
6022 spa->spa_pool_props_object, propname,
6023 1, strlen(strval) + 1, strval, tx) == 0);
6025 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6026 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
6028 if (proptype == PROP_TYPE_INDEX) {
6030 VERIFY(zpool_prop_index_to_string(
6031 prop, intval, &unused) == 0);
6033 VERIFY(zap_update(mos,
6034 spa->spa_pool_props_object, propname,
6035 8, 1, &intval, tx) == 0);
6037 ASSERT(0); /* not allowed */
6041 case ZPOOL_PROP_DELEGATION:
6042 spa->spa_delegation = intval;
6044 case ZPOOL_PROP_BOOTFS:
6045 spa->spa_bootfs = intval;
6047 case ZPOOL_PROP_FAILUREMODE:
6048 spa->spa_failmode = intval;
6050 case ZPOOL_PROP_AUTOEXPAND:
6051 spa->spa_autoexpand = intval;
6052 if (tx->tx_txg != TXG_INITIAL)
6053 spa_async_request(spa,
6054 SPA_ASYNC_AUTOEXPAND);
6056 case ZPOOL_PROP_DEDUPDITTO:
6057 spa->spa_dedup_ditto = intval;
6064 /* log internal history if this is not a zpool create */
6065 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
6066 tx->tx_txg != TXG_INITIAL) {
6067 spa_history_log_internal(LOG_POOL_PROPSET,
6068 spa, tx, "%s %lld %s",
6069 nvpair_name(elem), intval, spa_name(spa));
6073 mutex_exit(&spa->spa_props_lock);
6077 * Perform one-time upgrade on-disk changes. spa_version() does not
6078 * reflect the new version this txg, so there must be no changes this
6079 * txg to anything that the upgrade code depends on after it executes.
6080 * Therefore this must be called after dsl_pool_sync() does the sync
6084 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6086 dsl_pool_t *dp = spa->spa_dsl_pool;
6088 ASSERT(spa->spa_sync_pass == 1);
6090 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6091 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6092 dsl_pool_create_origin(dp, tx);
6094 /* Keeping the origin open increases spa_minref */
6095 spa->spa_minref += 3;
6098 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6099 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6100 dsl_pool_upgrade_clones(dp, tx);
6103 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6104 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6105 dsl_pool_upgrade_dir_clones(dp, tx);
6107 /* Keeping the freedir open increases spa_minref */
6108 spa->spa_minref += 3;
6111 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6112 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6113 spa_feature_create_zap_objects(spa, tx);
6118 * Sync the specified transaction group. New blocks may be dirtied as
6119 * part of the process, so we iterate until it converges.
6122 spa_sync(spa_t *spa, uint64_t txg)
6124 dsl_pool_t *dp = spa->spa_dsl_pool;
6125 objset_t *mos = spa->spa_meta_objset;
6126 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
6127 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6128 vdev_t *rvd = spa->spa_root_vdev;
6133 VERIFY(spa_writeable(spa));
6136 * Lock out configuration changes.
6138 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6140 spa->spa_syncing_txg = txg;
6141 spa->spa_sync_pass = 0;
6144 * If there are any pending vdev state changes, convert them
6145 * into config changes that go out with this transaction group.
6147 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6148 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6150 * We need the write lock here because, for aux vdevs,
6151 * calling vdev_config_dirty() modifies sav_config.
6152 * This is ugly and will become unnecessary when we
6153 * eliminate the aux vdev wart by integrating all vdevs
6154 * into the root vdev tree.
6156 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6157 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6158 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6159 vdev_state_clean(vd);
6160 vdev_config_dirty(vd);
6162 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6163 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6165 spa_config_exit(spa, SCL_STATE, FTAG);
6167 tx = dmu_tx_create_assigned(dp, txg);
6170 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6171 * set spa_deflate if we have no raid-z vdevs.
6173 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6174 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6177 for (i = 0; i < rvd->vdev_children; i++) {
6178 vd = rvd->vdev_child[i];
6179 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6182 if (i == rvd->vdev_children) {
6183 spa->spa_deflate = TRUE;
6184 VERIFY(0 == zap_add(spa->spa_meta_objset,
6185 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6186 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6191 * If anything has changed in this txg, or if someone is waiting
6192 * for this txg to sync (eg, spa_vdev_remove()), push the
6193 * deferred frees from the previous txg. If not, leave them
6194 * alone so that we don't generate work on an otherwise idle
6197 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6198 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6199 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6200 ((dsl_scan_active(dp->dp_scan) ||
6201 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6202 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6203 VERIFY3U(bpobj_iterate(defer_bpo,
6204 spa_free_sync_cb, zio, tx), ==, 0);
6205 VERIFY0(zio_wait(zio));
6209 * Iterate to convergence.
6212 int pass = ++spa->spa_sync_pass;
6214 spa_sync_config_object(spa, tx);
6215 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6216 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6217 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6218 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6219 spa_errlog_sync(spa, txg);
6220 dsl_pool_sync(dp, txg);
6222 if (pass <= SYNC_PASS_DEFERRED_FREE) {
6223 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6224 bplist_iterate(free_bpl, spa_free_sync_cb,
6226 VERIFY(zio_wait(zio) == 0);
6228 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6233 dsl_scan_sync(dp, tx);
6235 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6239 spa_sync_upgrades(spa, tx);
6241 } while (dmu_objset_is_dirty(mos, txg));
6244 * Rewrite the vdev configuration (which includes the uberblock)
6245 * to commit the transaction group.
6247 * If there are no dirty vdevs, we sync the uberblock to a few
6248 * random top-level vdevs that are known to be visible in the
6249 * config cache (see spa_vdev_add() for a complete description).
6250 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6254 * We hold SCL_STATE to prevent vdev open/close/etc.
6255 * while we're attempting to write the vdev labels.
6257 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6259 if (list_is_empty(&spa->spa_config_dirty_list)) {
6260 vdev_t *svd[SPA_DVAS_PER_BP];
6262 int children = rvd->vdev_children;
6263 int c0 = spa_get_random(children);
6265 for (int c = 0; c < children; c++) {
6266 vd = rvd->vdev_child[(c0 + c) % children];
6267 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6269 svd[svdcount++] = vd;
6270 if (svdcount == SPA_DVAS_PER_BP)
6273 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6275 error = vdev_config_sync(svd, svdcount, txg,
6278 error = vdev_config_sync(rvd->vdev_child,
6279 rvd->vdev_children, txg, B_FALSE);
6281 error = vdev_config_sync(rvd->vdev_child,
6282 rvd->vdev_children, txg, B_TRUE);
6286 spa->spa_last_synced_guid = rvd->vdev_guid;
6288 spa_config_exit(spa, SCL_STATE, FTAG);
6292 zio_suspend(spa, NULL);
6293 zio_resume_wait(spa);
6298 * Clear the dirty config list.
6300 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6301 vdev_config_clean(vd);
6304 * Now that the new config has synced transactionally,
6305 * let it become visible to the config cache.
6307 if (spa->spa_config_syncing != NULL) {
6308 spa_config_set(spa, spa->spa_config_syncing);
6309 spa->spa_config_txg = txg;
6310 spa->spa_config_syncing = NULL;
6313 spa->spa_ubsync = spa->spa_uberblock;
6315 dsl_pool_sync_done(dp, txg);
6318 * Update usable space statistics.
6320 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6321 vdev_sync_done(vd, txg);
6323 spa_update_dspace(spa);
6326 * It had better be the case that we didn't dirty anything
6327 * since vdev_config_sync().
6329 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6330 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6331 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6333 spa->spa_sync_pass = 0;
6335 spa_config_exit(spa, SCL_CONFIG, FTAG);
6337 spa_handle_ignored_writes(spa);
6340 * If any async tasks have been requested, kick them off.
6342 spa_async_dispatch(spa);
6346 * Sync all pools. We don't want to hold the namespace lock across these
6347 * operations, so we take a reference on the spa_t and drop the lock during the
6351 spa_sync_allpools(void)
6354 mutex_enter(&spa_namespace_lock);
6355 while ((spa = spa_next(spa)) != NULL) {
6356 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6357 !spa_writeable(spa) || spa_suspended(spa))
6359 spa_open_ref(spa, FTAG);
6360 mutex_exit(&spa_namespace_lock);
6361 txg_wait_synced(spa_get_dsl(spa), 0);
6362 mutex_enter(&spa_namespace_lock);
6363 spa_close(spa, FTAG);
6365 mutex_exit(&spa_namespace_lock);
6369 * ==========================================================================
6370 * Miscellaneous routines
6371 * ==========================================================================
6375 * Remove all pools in the system.
6383 * Remove all cached state. All pools should be closed now,
6384 * so every spa in the AVL tree should be unreferenced.
6386 mutex_enter(&spa_namespace_lock);
6387 while ((spa = spa_next(NULL)) != NULL) {
6389 * Stop async tasks. The async thread may need to detach
6390 * a device that's been replaced, which requires grabbing
6391 * spa_namespace_lock, so we must drop it here.
6393 spa_open_ref(spa, FTAG);
6394 mutex_exit(&spa_namespace_lock);
6395 spa_async_suspend(spa);
6396 mutex_enter(&spa_namespace_lock);
6397 spa_close(spa, FTAG);
6399 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6401 spa_deactivate(spa);
6405 mutex_exit(&spa_namespace_lock);
6409 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6414 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6418 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6419 vd = spa->spa_l2cache.sav_vdevs[i];
6420 if (vd->vdev_guid == guid)
6424 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6425 vd = spa->spa_spares.sav_vdevs[i];
6426 if (vd->vdev_guid == guid)
6435 spa_upgrade(spa_t *spa, uint64_t version)
6437 ASSERT(spa_writeable(spa));
6439 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6442 * This should only be called for a non-faulted pool, and since a
6443 * future version would result in an unopenable pool, this shouldn't be
6446 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
6447 ASSERT(version >= spa->spa_uberblock.ub_version);
6449 spa->spa_uberblock.ub_version = version;
6450 vdev_config_dirty(spa->spa_root_vdev);
6452 spa_config_exit(spa, SCL_ALL, FTAG);
6454 txg_wait_synced(spa_get_dsl(spa), 0);
6458 spa_has_spare(spa_t *spa, uint64_t guid)
6462 spa_aux_vdev_t *sav = &spa->spa_spares;
6464 for (i = 0; i < sav->sav_count; i++)
6465 if (sav->sav_vdevs[i]->vdev_guid == guid)
6468 for (i = 0; i < sav->sav_npending; i++) {
6469 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6470 &spareguid) == 0 && spareguid == guid)
6478 * Check if a pool has an active shared spare device.
6479 * Note: reference count of an active spare is 2, as a spare and as a replace
6482 spa_has_active_shared_spare(spa_t *spa)
6486 spa_aux_vdev_t *sav = &spa->spa_spares;
6488 for (i = 0; i < sav->sav_count; i++) {
6489 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6490 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6499 * Post a sysevent corresponding to the given event. The 'name' must be one of
6500 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6501 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6502 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6503 * or zdb as real changes.
6506 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6510 sysevent_attr_list_t *attr = NULL;
6511 sysevent_value_t value;
6514 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6517 value.value_type = SE_DATA_TYPE_STRING;
6518 value.value.sv_string = spa_name(spa);
6519 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6522 value.value_type = SE_DATA_TYPE_UINT64;
6523 value.value.sv_uint64 = spa_guid(spa);
6524 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6528 value.value_type = SE_DATA_TYPE_UINT64;
6529 value.value.sv_uint64 = vd->vdev_guid;
6530 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6534 if (vd->vdev_path) {
6535 value.value_type = SE_DATA_TYPE_STRING;
6536 value.value.sv_string = vd->vdev_path;
6537 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6538 &value, SE_SLEEP) != 0)
6543 if (sysevent_attach_attributes(ev, attr) != 0)
6547 (void) log_sysevent(ev, SE_SLEEP, &eid);
6551 sysevent_free_attr(attr);