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) 2013 by Delphix. All rights reserved.
25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * SPA: Storage Pool Allocator
32 * This file contains all the routines used when modifying on-disk SPA state.
33 * This includes opening, importing, destroying, exporting a pool, and syncing a
37 #include <sys/zfs_context.h>
38 #include <sys/fm/fs/zfs.h>
39 #include <sys/spa_impl.h>
41 #include <sys/zio_checksum.h>
43 #include <sys/dmu_tx.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/metaslab.h>
49 #include <sys/metaslab_impl.h>
50 #include <sys/uberblock_impl.h>
53 #include <sys/dmu_traverse.h>
54 #include <sys/dmu_objset.h>
55 #include <sys/unique.h>
56 #include <sys/dsl_pool.h>
57 #include <sys/dsl_dataset.h>
58 #include <sys/dsl_dir.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/dsl_synctask.h>
61 #include <sys/fs/zfs.h>
63 #include <sys/callb.h>
64 #include <sys/spa_boot.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/dsl_scan.h>
67 #include <sys/dmu_send.h>
68 #include <sys/dsl_destroy.h>
69 #include <sys/dsl_userhold.h>
70 #include <sys/zfeature.h>
72 #include <sys/trim_map.h>
75 #include <sys/callb.h>
76 #include <sys/cpupart.h>
81 #include "zfs_comutil.h"
83 /* Check hostid on import? */
84 static int check_hostid = 1;
86 SYSCTL_DECL(_vfs_zfs);
87 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
88 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
89 "Check hostid on import?");
91 typedef enum zti_modes {
92 zti_mode_fixed, /* value is # of threads (min 1) */
93 zti_mode_online_percent, /* value is % of online CPUs */
94 zti_mode_batch, /* cpu-intensive; value is ignored */
95 zti_mode_null, /* don't create a taskq */
99 #define ZTI_FIX(n) { zti_mode_fixed, (n) }
100 #define ZTI_PCT(n) { zti_mode_online_percent, (n) }
101 #define ZTI_BATCH { zti_mode_batch, 0 }
102 #define ZTI_NULL { zti_mode_null, 0 }
104 #define ZTI_ONE ZTI_FIX(1)
106 typedef struct zio_taskq_info {
107 enum zti_modes zti_mode;
111 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
112 "issue", "issue_high", "intr", "intr_high"
116 * Define the taskq threads for the following I/O types:
117 * NULL, READ, WRITE, FREE, CLAIM, and IOCTL
119 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
120 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
121 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
122 { ZTI_FIX(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL },
123 { ZTI_BATCH, ZTI_FIX(5), ZTI_FIX(8), ZTI_FIX(5) },
124 { ZTI_FIX(100), ZTI_NULL, ZTI_ONE, ZTI_NULL },
125 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
126 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL },
129 static void spa_sync_version(void *arg, dmu_tx_t *tx);
130 static void spa_sync_props(void *arg, dmu_tx_t *tx);
131 static boolean_t spa_has_active_shared_spare(spa_t *spa);
132 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
133 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
135 static void spa_vdev_resilver_done(spa_t *spa);
137 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
139 id_t zio_taskq_psrset_bind = PS_NONE;
142 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
144 uint_t zio_taskq_basedc = 80; /* base duty cycle */
146 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
147 extern int zfs_sync_pass_deferred_free;
150 extern void spa_deadman(void *arg);
154 * This (illegal) pool name is used when temporarily importing a spa_t in order
155 * to get the vdev stats associated with the imported devices.
157 #define TRYIMPORT_NAME "$import"
160 * ==========================================================================
161 * SPA properties routines
162 * ==========================================================================
166 * Add a (source=src, propname=propval) list to an nvlist.
169 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
170 uint64_t intval, zprop_source_t src)
172 const char *propname = zpool_prop_to_name(prop);
175 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
176 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
179 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
181 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
183 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
184 nvlist_free(propval);
188 * Get property values from the spa configuration.
191 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
193 vdev_t *rvd = spa->spa_root_vdev;
194 dsl_pool_t *pool = spa->spa_dsl_pool;
198 uint64_t cap, version;
199 zprop_source_t src = ZPROP_SRC_NONE;
200 spa_config_dirent_t *dp;
202 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
205 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
206 size = metaslab_class_get_space(spa_normal_class(spa));
207 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
208 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
209 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
210 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
214 for (int c = 0; c < rvd->vdev_children; c++) {
215 vdev_t *tvd = rvd->vdev_child[c];
216 space += tvd->vdev_max_asize - tvd->vdev_asize;
218 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
221 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
222 (spa_mode(spa) == FREAD), src);
224 cap = (size == 0) ? 0 : (alloc * 100 / size);
225 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
227 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
228 ddt_get_pool_dedup_ratio(spa), src);
230 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
231 rvd->vdev_state, src);
233 version = spa_version(spa);
234 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
235 src = ZPROP_SRC_DEFAULT;
237 src = ZPROP_SRC_LOCAL;
238 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
242 dsl_dir_t *freedir = pool->dp_free_dir;
245 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
246 * when opening pools before this version freedir will be NULL.
248 if (freedir != NULL) {
249 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
250 freedir->dd_phys->dd_used_bytes, src);
252 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
257 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
259 if (spa->spa_comment != NULL) {
260 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
264 if (spa->spa_root != NULL)
265 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
268 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
269 if (dp->scd_path == NULL) {
270 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
271 "none", 0, ZPROP_SRC_LOCAL);
272 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
273 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
274 dp->scd_path, 0, ZPROP_SRC_LOCAL);
280 * Get zpool property values.
283 spa_prop_get(spa_t *spa, nvlist_t **nvp)
285 objset_t *mos = spa->spa_meta_objset;
290 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
292 mutex_enter(&spa->spa_props_lock);
295 * Get properties from the spa config.
297 spa_prop_get_config(spa, nvp);
299 /* If no pool property object, no more prop to get. */
300 if (mos == NULL || spa->spa_pool_props_object == 0) {
301 mutex_exit(&spa->spa_props_lock);
306 * Get properties from the MOS pool property object.
308 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
309 (err = zap_cursor_retrieve(&zc, &za)) == 0;
310 zap_cursor_advance(&zc)) {
313 zprop_source_t src = ZPROP_SRC_DEFAULT;
316 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
319 switch (za.za_integer_length) {
321 /* integer property */
322 if (za.za_first_integer !=
323 zpool_prop_default_numeric(prop))
324 src = ZPROP_SRC_LOCAL;
326 if (prop == ZPOOL_PROP_BOOTFS) {
328 dsl_dataset_t *ds = NULL;
330 dp = spa_get_dsl(spa);
331 dsl_pool_config_enter(dp, FTAG);
332 if (err = dsl_dataset_hold_obj(dp,
333 za.za_first_integer, FTAG, &ds)) {
334 dsl_pool_config_exit(dp, FTAG);
339 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
341 dsl_dataset_name(ds, strval);
342 dsl_dataset_rele(ds, FTAG);
343 dsl_pool_config_exit(dp, FTAG);
346 intval = za.za_first_integer;
349 spa_prop_add_list(*nvp, prop, strval, intval, src);
353 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
358 /* string property */
359 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
360 err = zap_lookup(mos, spa->spa_pool_props_object,
361 za.za_name, 1, za.za_num_integers, strval);
363 kmem_free(strval, za.za_num_integers);
366 spa_prop_add_list(*nvp, prop, strval, 0, src);
367 kmem_free(strval, za.za_num_integers);
374 zap_cursor_fini(&zc);
375 mutex_exit(&spa->spa_props_lock);
377 if (err && err != ENOENT) {
387 * Validate the given pool properties nvlist and modify the list
388 * for the property values to be set.
391 spa_prop_validate(spa_t *spa, nvlist_t *props)
394 int error = 0, reset_bootfs = 0;
396 boolean_t has_feature = B_FALSE;
399 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
401 char *strval, *slash, *check, *fname;
402 const char *propname = nvpair_name(elem);
403 zpool_prop_t prop = zpool_name_to_prop(propname);
407 if (!zpool_prop_feature(propname)) {
408 error = SET_ERROR(EINVAL);
413 * Sanitize the input.
415 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
416 error = SET_ERROR(EINVAL);
420 if (nvpair_value_uint64(elem, &intval) != 0) {
421 error = SET_ERROR(EINVAL);
426 error = SET_ERROR(EINVAL);
430 fname = strchr(propname, '@') + 1;
431 if (zfeature_lookup_name(fname, NULL) != 0) {
432 error = SET_ERROR(EINVAL);
436 has_feature = B_TRUE;
439 case ZPOOL_PROP_VERSION:
440 error = nvpair_value_uint64(elem, &intval);
442 (intval < spa_version(spa) ||
443 intval > SPA_VERSION_BEFORE_FEATURES ||
445 error = SET_ERROR(EINVAL);
448 case ZPOOL_PROP_DELEGATION:
449 case ZPOOL_PROP_AUTOREPLACE:
450 case ZPOOL_PROP_LISTSNAPS:
451 case ZPOOL_PROP_AUTOEXPAND:
452 error = nvpair_value_uint64(elem, &intval);
453 if (!error && intval > 1)
454 error = SET_ERROR(EINVAL);
457 case ZPOOL_PROP_BOOTFS:
459 * If the pool version is less than SPA_VERSION_BOOTFS,
460 * or the pool is still being created (version == 0),
461 * the bootfs property cannot be set.
463 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
464 error = SET_ERROR(ENOTSUP);
469 * Make sure the vdev config is bootable
471 if (!vdev_is_bootable(spa->spa_root_vdev)) {
472 error = SET_ERROR(ENOTSUP);
478 error = nvpair_value_string(elem, &strval);
484 if (strval == NULL || strval[0] == '\0') {
485 objnum = zpool_prop_default_numeric(
490 if (error = dmu_objset_hold(strval, FTAG, &os))
493 /* Must be ZPL and not gzip compressed. */
495 if (dmu_objset_type(os) != DMU_OST_ZFS) {
496 error = SET_ERROR(ENOTSUP);
498 dsl_prop_get_int_ds(dmu_objset_ds(os),
499 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
501 !BOOTFS_COMPRESS_VALID(compress)) {
502 error = SET_ERROR(ENOTSUP);
504 objnum = dmu_objset_id(os);
506 dmu_objset_rele(os, FTAG);
510 case ZPOOL_PROP_FAILUREMODE:
511 error = nvpair_value_uint64(elem, &intval);
512 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
513 intval > ZIO_FAILURE_MODE_PANIC))
514 error = SET_ERROR(EINVAL);
517 * This is a special case which only occurs when
518 * the pool has completely failed. This allows
519 * the user to change the in-core failmode property
520 * without syncing it out to disk (I/Os might
521 * currently be blocked). We do this by returning
522 * EIO to the caller (spa_prop_set) to trick it
523 * into thinking we encountered a property validation
526 if (!error && spa_suspended(spa)) {
527 spa->spa_failmode = intval;
528 error = SET_ERROR(EIO);
532 case ZPOOL_PROP_CACHEFILE:
533 if ((error = nvpair_value_string(elem, &strval)) != 0)
536 if (strval[0] == '\0')
539 if (strcmp(strval, "none") == 0)
542 if (strval[0] != '/') {
543 error = SET_ERROR(EINVAL);
547 slash = strrchr(strval, '/');
548 ASSERT(slash != NULL);
550 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
551 strcmp(slash, "/..") == 0)
552 error = SET_ERROR(EINVAL);
555 case ZPOOL_PROP_COMMENT:
556 if ((error = nvpair_value_string(elem, &strval)) != 0)
558 for (check = strval; *check != '\0'; check++) {
560 * The kernel doesn't have an easy isprint()
561 * check. For this kernel check, we merely
562 * check ASCII apart from DEL. Fix this if
563 * there is an easy-to-use kernel isprint().
565 if (*check >= 0x7f) {
566 error = SET_ERROR(EINVAL);
571 if (strlen(strval) > ZPROP_MAX_COMMENT)
575 case ZPOOL_PROP_DEDUPDITTO:
576 if (spa_version(spa) < SPA_VERSION_DEDUP)
577 error = SET_ERROR(ENOTSUP);
579 error = nvpair_value_uint64(elem, &intval);
581 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
582 error = SET_ERROR(EINVAL);
590 if (!error && reset_bootfs) {
591 error = nvlist_remove(props,
592 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
595 error = nvlist_add_uint64(props,
596 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
604 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
607 spa_config_dirent_t *dp;
609 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
613 dp = kmem_alloc(sizeof (spa_config_dirent_t),
616 if (cachefile[0] == '\0')
617 dp->scd_path = spa_strdup(spa_config_path);
618 else if (strcmp(cachefile, "none") == 0)
621 dp->scd_path = spa_strdup(cachefile);
623 list_insert_head(&spa->spa_config_list, dp);
625 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
629 spa_prop_set(spa_t *spa, nvlist_t *nvp)
632 nvpair_t *elem = NULL;
633 boolean_t need_sync = B_FALSE;
635 if ((error = spa_prop_validate(spa, nvp)) != 0)
638 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
639 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
641 if (prop == ZPOOL_PROP_CACHEFILE ||
642 prop == ZPOOL_PROP_ALTROOT ||
643 prop == ZPOOL_PROP_READONLY)
646 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
649 if (prop == ZPOOL_PROP_VERSION) {
650 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
652 ASSERT(zpool_prop_feature(nvpair_name(elem)));
653 ver = SPA_VERSION_FEATURES;
657 /* Save time if the version is already set. */
658 if (ver == spa_version(spa))
662 * In addition to the pool directory object, we might
663 * create the pool properties object, the features for
664 * read object, the features for write object, or the
665 * feature descriptions object.
667 error = dsl_sync_task(spa->spa_name, NULL,
668 spa_sync_version, &ver, 6);
679 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
687 * If the bootfs property value is dsobj, clear it.
690 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
692 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
693 VERIFY(zap_remove(spa->spa_meta_objset,
694 spa->spa_pool_props_object,
695 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
702 spa_change_guid_check(void *arg, dmu_tx_t *tx)
704 uint64_t *newguid = arg;
705 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
706 vdev_t *rvd = spa->spa_root_vdev;
709 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
710 vdev_state = rvd->vdev_state;
711 spa_config_exit(spa, SCL_STATE, FTAG);
713 if (vdev_state != VDEV_STATE_HEALTHY)
714 return (SET_ERROR(ENXIO));
716 ASSERT3U(spa_guid(spa), !=, *newguid);
722 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
724 uint64_t *newguid = arg;
725 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
727 vdev_t *rvd = spa->spa_root_vdev;
729 oldguid = spa_guid(spa);
731 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
732 rvd->vdev_guid = *newguid;
733 rvd->vdev_guid_sum += (*newguid - oldguid);
734 vdev_config_dirty(rvd);
735 spa_config_exit(spa, SCL_STATE, FTAG);
737 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
742 * Change the GUID for the pool. This is done so that we can later
743 * re-import a pool built from a clone of our own vdevs. We will modify
744 * the root vdev's guid, our own pool guid, and then mark all of our
745 * vdevs dirty. Note that we must make sure that all our vdevs are
746 * online when we do this, or else any vdevs that weren't present
747 * would be orphaned from our pool. We are also going to issue a
748 * sysevent to update any watchers.
751 spa_change_guid(spa_t *spa)
756 mutex_enter(&spa_namespace_lock);
757 guid = spa_generate_guid(NULL);
759 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
760 spa_change_guid_sync, &guid, 5);
763 spa_config_sync(spa, B_FALSE, B_TRUE);
764 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
767 mutex_exit(&spa_namespace_lock);
773 * ==========================================================================
774 * SPA state manipulation (open/create/destroy/import/export)
775 * ==========================================================================
779 spa_error_entry_compare(const void *a, const void *b)
781 spa_error_entry_t *sa = (spa_error_entry_t *)a;
782 spa_error_entry_t *sb = (spa_error_entry_t *)b;
785 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
786 sizeof (zbookmark_t));
797 * Utility function which retrieves copies of the current logs and
798 * re-initializes them in the process.
801 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
803 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
805 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
806 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
808 avl_create(&spa->spa_errlist_scrub,
809 spa_error_entry_compare, sizeof (spa_error_entry_t),
810 offsetof(spa_error_entry_t, se_avl));
811 avl_create(&spa->spa_errlist_last,
812 spa_error_entry_compare, sizeof (spa_error_entry_t),
813 offsetof(spa_error_entry_t, se_avl));
817 spa_taskq_create(spa_t *spa, const char *name, enum zti_modes mode,
820 uint_t flags = TASKQ_PREPOPULATE;
821 boolean_t batch = B_FALSE;
825 return (NULL); /* no taskq needed */
828 ASSERT3U(value, >=, 1);
829 value = MAX(value, 1);
834 flags |= TASKQ_THREADS_CPU_PCT;
835 value = zio_taskq_batch_pct;
838 case zti_mode_online_percent:
839 flags |= TASKQ_THREADS_CPU_PCT;
843 panic("unrecognized mode for %s taskq (%u:%u) in "
850 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
852 flags |= TASKQ_DC_BATCH;
854 return (taskq_create_sysdc(name, value, 50, INT_MAX,
855 spa->spa_proc, zio_taskq_basedc, flags));
858 return (taskq_create_proc(name, value, maxclsyspri, 50, INT_MAX,
859 spa->spa_proc, flags));
863 spa_create_zio_taskqs(spa_t *spa)
865 for (int t = 0; t < ZIO_TYPES; t++) {
866 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
867 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
868 enum zti_modes mode = ztip->zti_mode;
869 uint_t value = ztip->zti_value;
872 (void) snprintf(name, sizeof (name),
873 "%s_%s", zio_type_name[t], zio_taskq_types[q]);
875 spa->spa_zio_taskq[t][q] =
876 spa_taskq_create(spa, name, mode, value);
884 spa_thread(void *arg)
889 user_t *pu = PTOU(curproc);
891 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
894 ASSERT(curproc != &p0);
895 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
896 "zpool-%s", spa->spa_name);
897 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
900 /* bind this thread to the requested psrset */
901 if (zio_taskq_psrset_bind != PS_NONE) {
903 mutex_enter(&cpu_lock);
904 mutex_enter(&pidlock);
905 mutex_enter(&curproc->p_lock);
907 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
908 0, NULL, NULL) == 0) {
909 curthread->t_bind_pset = zio_taskq_psrset_bind;
912 "Couldn't bind process for zfs pool \"%s\" to "
913 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
916 mutex_exit(&curproc->p_lock);
917 mutex_exit(&pidlock);
918 mutex_exit(&cpu_lock);
924 if (zio_taskq_sysdc) {
925 sysdc_thread_enter(curthread, 100, 0);
929 spa->spa_proc = curproc;
930 spa->spa_did = curthread->t_did;
932 spa_create_zio_taskqs(spa);
934 mutex_enter(&spa->spa_proc_lock);
935 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
937 spa->spa_proc_state = SPA_PROC_ACTIVE;
938 cv_broadcast(&spa->spa_proc_cv);
940 CALLB_CPR_SAFE_BEGIN(&cprinfo);
941 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
942 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
943 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
945 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
946 spa->spa_proc_state = SPA_PROC_GONE;
948 cv_broadcast(&spa->spa_proc_cv);
949 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
951 mutex_enter(&curproc->p_lock);
954 #endif /* SPA_PROCESS */
958 * Activate an uninitialized pool.
961 spa_activate(spa_t *spa, int mode)
963 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
965 spa->spa_state = POOL_STATE_ACTIVE;
966 spa->spa_mode = mode;
968 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
969 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
971 /* Try to create a covering process */
972 mutex_enter(&spa->spa_proc_lock);
973 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
974 ASSERT(spa->spa_proc == &p0);
978 /* Only create a process if we're going to be around a while. */
979 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
980 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
982 spa->spa_proc_state = SPA_PROC_CREATED;
983 while (spa->spa_proc_state == SPA_PROC_CREATED) {
984 cv_wait(&spa->spa_proc_cv,
985 &spa->spa_proc_lock);
987 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
988 ASSERT(spa->spa_proc != &p0);
989 ASSERT(spa->spa_did != 0);
993 "Couldn't create process for zfs pool \"%s\"\n",
998 #endif /* SPA_PROCESS */
999 mutex_exit(&spa->spa_proc_lock);
1001 /* If we didn't create a process, we need to create our taskqs. */
1002 ASSERT(spa->spa_proc == &p0);
1003 if (spa->spa_proc == &p0) {
1004 spa_create_zio_taskqs(spa);
1008 * Start TRIM thread.
1010 trim_thread_create(spa);
1012 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1013 offsetof(vdev_t, vdev_config_dirty_node));
1014 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1015 offsetof(vdev_t, vdev_state_dirty_node));
1017 txg_list_create(&spa->spa_vdev_txg_list,
1018 offsetof(struct vdev, vdev_txg_node));
1020 avl_create(&spa->spa_errlist_scrub,
1021 spa_error_entry_compare, sizeof (spa_error_entry_t),
1022 offsetof(spa_error_entry_t, se_avl));
1023 avl_create(&spa->spa_errlist_last,
1024 spa_error_entry_compare, sizeof (spa_error_entry_t),
1025 offsetof(spa_error_entry_t, se_avl));
1029 * Opposite of spa_activate().
1032 spa_deactivate(spa_t *spa)
1034 ASSERT(spa->spa_sync_on == B_FALSE);
1035 ASSERT(spa->spa_dsl_pool == NULL);
1036 ASSERT(spa->spa_root_vdev == NULL);
1037 ASSERT(spa->spa_async_zio_root == NULL);
1038 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1041 * Stop TRIM thread in case spa_unload() wasn't called directly
1042 * before spa_deactivate().
1044 trim_thread_destroy(spa);
1046 txg_list_destroy(&spa->spa_vdev_txg_list);
1048 list_destroy(&spa->spa_config_dirty_list);
1049 list_destroy(&spa->spa_state_dirty_list);
1051 for (int t = 0; t < ZIO_TYPES; t++) {
1052 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1053 if (spa->spa_zio_taskq[t][q] != NULL)
1054 taskq_destroy(spa->spa_zio_taskq[t][q]);
1055 spa->spa_zio_taskq[t][q] = NULL;
1059 metaslab_class_destroy(spa->spa_normal_class);
1060 spa->spa_normal_class = NULL;
1062 metaslab_class_destroy(spa->spa_log_class);
1063 spa->spa_log_class = NULL;
1066 * If this was part of an import or the open otherwise failed, we may
1067 * still have errors left in the queues. Empty them just in case.
1069 spa_errlog_drain(spa);
1071 avl_destroy(&spa->spa_errlist_scrub);
1072 avl_destroy(&spa->spa_errlist_last);
1074 spa->spa_state = POOL_STATE_UNINITIALIZED;
1076 mutex_enter(&spa->spa_proc_lock);
1077 if (spa->spa_proc_state != SPA_PROC_NONE) {
1078 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1079 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1080 cv_broadcast(&spa->spa_proc_cv);
1081 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1082 ASSERT(spa->spa_proc != &p0);
1083 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1085 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1086 spa->spa_proc_state = SPA_PROC_NONE;
1088 ASSERT(spa->spa_proc == &p0);
1089 mutex_exit(&spa->spa_proc_lock);
1093 * We want to make sure spa_thread() has actually exited the ZFS
1094 * module, so that the module can't be unloaded out from underneath
1097 if (spa->spa_did != 0) {
1098 thread_join(spa->spa_did);
1101 #endif /* SPA_PROCESS */
1105 * Verify a pool configuration, and construct the vdev tree appropriately. This
1106 * will create all the necessary vdevs in the appropriate layout, with each vdev
1107 * in the CLOSED state. This will prep the pool before open/creation/import.
1108 * All vdev validation is done by the vdev_alloc() routine.
1111 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1112 uint_t id, int atype)
1118 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1121 if ((*vdp)->vdev_ops->vdev_op_leaf)
1124 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1127 if (error == ENOENT)
1133 return (SET_ERROR(EINVAL));
1136 for (int c = 0; c < children; c++) {
1138 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1146 ASSERT(*vdp != NULL);
1152 * Opposite of spa_load().
1155 spa_unload(spa_t *spa)
1159 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1164 trim_thread_destroy(spa);
1169 spa_async_suspend(spa);
1174 if (spa->spa_sync_on) {
1175 txg_sync_stop(spa->spa_dsl_pool);
1176 spa->spa_sync_on = B_FALSE;
1180 * Wait for any outstanding async I/O to complete.
1182 if (spa->spa_async_zio_root != NULL) {
1183 (void) zio_wait(spa->spa_async_zio_root);
1184 spa->spa_async_zio_root = NULL;
1187 bpobj_close(&spa->spa_deferred_bpobj);
1190 * Close the dsl pool.
1192 if (spa->spa_dsl_pool) {
1193 dsl_pool_close(spa->spa_dsl_pool);
1194 spa->spa_dsl_pool = NULL;
1195 spa->spa_meta_objset = NULL;
1200 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1203 * Drop and purge level 2 cache
1205 spa_l2cache_drop(spa);
1210 if (spa->spa_root_vdev)
1211 vdev_free(spa->spa_root_vdev);
1212 ASSERT(spa->spa_root_vdev == NULL);
1214 for (i = 0; i < spa->spa_spares.sav_count; i++)
1215 vdev_free(spa->spa_spares.sav_vdevs[i]);
1216 if (spa->spa_spares.sav_vdevs) {
1217 kmem_free(spa->spa_spares.sav_vdevs,
1218 spa->spa_spares.sav_count * sizeof (void *));
1219 spa->spa_spares.sav_vdevs = NULL;
1221 if (spa->spa_spares.sav_config) {
1222 nvlist_free(spa->spa_spares.sav_config);
1223 spa->spa_spares.sav_config = NULL;
1225 spa->spa_spares.sav_count = 0;
1227 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1228 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1229 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1231 if (spa->spa_l2cache.sav_vdevs) {
1232 kmem_free(spa->spa_l2cache.sav_vdevs,
1233 spa->spa_l2cache.sav_count * sizeof (void *));
1234 spa->spa_l2cache.sav_vdevs = NULL;
1236 if (spa->spa_l2cache.sav_config) {
1237 nvlist_free(spa->spa_l2cache.sav_config);
1238 spa->spa_l2cache.sav_config = NULL;
1240 spa->spa_l2cache.sav_count = 0;
1242 spa->spa_async_suspended = 0;
1244 if (spa->spa_comment != NULL) {
1245 spa_strfree(spa->spa_comment);
1246 spa->spa_comment = NULL;
1249 spa_config_exit(spa, SCL_ALL, FTAG);
1253 * Load (or re-load) the current list of vdevs describing the active spares for
1254 * this pool. When this is called, we have some form of basic information in
1255 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1256 * then re-generate a more complete list including status information.
1259 spa_load_spares(spa_t *spa)
1266 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1269 * First, close and free any existing spare vdevs.
1271 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1272 vd = spa->spa_spares.sav_vdevs[i];
1274 /* Undo the call to spa_activate() below */
1275 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1276 B_FALSE)) != NULL && tvd->vdev_isspare)
1277 spa_spare_remove(tvd);
1282 if (spa->spa_spares.sav_vdevs)
1283 kmem_free(spa->spa_spares.sav_vdevs,
1284 spa->spa_spares.sav_count * sizeof (void *));
1286 if (spa->spa_spares.sav_config == NULL)
1289 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1290 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1292 spa->spa_spares.sav_count = (int)nspares;
1293 spa->spa_spares.sav_vdevs = NULL;
1299 * Construct the array of vdevs, opening them to get status in the
1300 * process. For each spare, there is potentially two different vdev_t
1301 * structures associated with it: one in the list of spares (used only
1302 * for basic validation purposes) and one in the active vdev
1303 * configuration (if it's spared in). During this phase we open and
1304 * validate each vdev on the spare list. If the vdev also exists in the
1305 * active configuration, then we also mark this vdev as an active spare.
1307 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1309 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1310 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1311 VDEV_ALLOC_SPARE) == 0);
1314 spa->spa_spares.sav_vdevs[i] = vd;
1316 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1317 B_FALSE)) != NULL) {
1318 if (!tvd->vdev_isspare)
1322 * We only mark the spare active if we were successfully
1323 * able to load the vdev. Otherwise, importing a pool
1324 * with a bad active spare would result in strange
1325 * behavior, because multiple pool would think the spare
1326 * is actively in use.
1328 * There is a vulnerability here to an equally bizarre
1329 * circumstance, where a dead active spare is later
1330 * brought back to life (onlined or otherwise). Given
1331 * the rarity of this scenario, and the extra complexity
1332 * it adds, we ignore the possibility.
1334 if (!vdev_is_dead(tvd))
1335 spa_spare_activate(tvd);
1339 vd->vdev_aux = &spa->spa_spares;
1341 if (vdev_open(vd) != 0)
1344 if (vdev_validate_aux(vd) == 0)
1349 * Recompute the stashed list of spares, with status information
1352 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1353 DATA_TYPE_NVLIST_ARRAY) == 0);
1355 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1357 for (i = 0; i < spa->spa_spares.sav_count; i++)
1358 spares[i] = vdev_config_generate(spa,
1359 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1360 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1361 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1362 for (i = 0; i < spa->spa_spares.sav_count; i++)
1363 nvlist_free(spares[i]);
1364 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1368 * Load (or re-load) the current list of vdevs describing the active l2cache for
1369 * this pool. When this is called, we have some form of basic information in
1370 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1371 * then re-generate a more complete list including status information.
1372 * Devices which are already active have their details maintained, and are
1376 spa_load_l2cache(spa_t *spa)
1380 int i, j, oldnvdevs;
1382 vdev_t *vd, **oldvdevs, **newvdevs;
1383 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1385 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1387 if (sav->sav_config != NULL) {
1388 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1389 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1390 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1396 oldvdevs = sav->sav_vdevs;
1397 oldnvdevs = sav->sav_count;
1398 sav->sav_vdevs = NULL;
1402 * Process new nvlist of vdevs.
1404 for (i = 0; i < nl2cache; i++) {
1405 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1409 for (j = 0; j < oldnvdevs; j++) {
1411 if (vd != NULL && guid == vd->vdev_guid) {
1413 * Retain previous vdev for add/remove ops.
1421 if (newvdevs[i] == NULL) {
1425 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1426 VDEV_ALLOC_L2CACHE) == 0);
1431 * Commit this vdev as an l2cache device,
1432 * even if it fails to open.
1434 spa_l2cache_add(vd);
1439 spa_l2cache_activate(vd);
1441 if (vdev_open(vd) != 0)
1444 (void) vdev_validate_aux(vd);
1446 if (!vdev_is_dead(vd))
1447 l2arc_add_vdev(spa, vd);
1452 * Purge vdevs that were dropped
1454 for (i = 0; i < oldnvdevs; i++) {
1459 ASSERT(vd->vdev_isl2cache);
1461 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1462 pool != 0ULL && l2arc_vdev_present(vd))
1463 l2arc_remove_vdev(vd);
1464 vdev_clear_stats(vd);
1470 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1472 if (sav->sav_config == NULL)
1475 sav->sav_vdevs = newvdevs;
1476 sav->sav_count = (int)nl2cache;
1479 * Recompute the stashed list of l2cache devices, with status
1480 * information this time.
1482 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1483 DATA_TYPE_NVLIST_ARRAY) == 0);
1485 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1486 for (i = 0; i < sav->sav_count; i++)
1487 l2cache[i] = vdev_config_generate(spa,
1488 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1489 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1490 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1492 for (i = 0; i < sav->sav_count; i++)
1493 nvlist_free(l2cache[i]);
1495 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1499 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1502 char *packed = NULL;
1507 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1508 nvsize = *(uint64_t *)db->db_data;
1509 dmu_buf_rele(db, FTAG);
1511 packed = kmem_alloc(nvsize, KM_SLEEP);
1512 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1515 error = nvlist_unpack(packed, nvsize, value, 0);
1516 kmem_free(packed, nvsize);
1522 * Checks to see if the given vdev could not be opened, in which case we post a
1523 * sysevent to notify the autoreplace code that the device has been removed.
1526 spa_check_removed(vdev_t *vd)
1528 for (int c = 0; c < vd->vdev_children; c++)
1529 spa_check_removed(vd->vdev_child[c]);
1531 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1533 zfs_post_autoreplace(vd->vdev_spa, vd);
1534 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1539 * Validate the current config against the MOS config
1542 spa_config_valid(spa_t *spa, nvlist_t *config)
1544 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1547 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1549 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1550 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1552 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1555 * If we're doing a normal import, then build up any additional
1556 * diagnostic information about missing devices in this config.
1557 * We'll pass this up to the user for further processing.
1559 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1560 nvlist_t **child, *nv;
1563 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1565 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1567 for (int c = 0; c < rvd->vdev_children; c++) {
1568 vdev_t *tvd = rvd->vdev_child[c];
1569 vdev_t *mtvd = mrvd->vdev_child[c];
1571 if (tvd->vdev_ops == &vdev_missing_ops &&
1572 mtvd->vdev_ops != &vdev_missing_ops &&
1574 child[idx++] = vdev_config_generate(spa, mtvd,
1579 VERIFY(nvlist_add_nvlist_array(nv,
1580 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1581 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1582 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1584 for (int i = 0; i < idx; i++)
1585 nvlist_free(child[i]);
1588 kmem_free(child, rvd->vdev_children * sizeof (char **));
1592 * Compare the root vdev tree with the information we have
1593 * from the MOS config (mrvd). Check each top-level vdev
1594 * with the corresponding MOS config top-level (mtvd).
1596 for (int c = 0; c < rvd->vdev_children; c++) {
1597 vdev_t *tvd = rvd->vdev_child[c];
1598 vdev_t *mtvd = mrvd->vdev_child[c];
1601 * Resolve any "missing" vdevs in the current configuration.
1602 * If we find that the MOS config has more accurate information
1603 * about the top-level vdev then use that vdev instead.
1605 if (tvd->vdev_ops == &vdev_missing_ops &&
1606 mtvd->vdev_ops != &vdev_missing_ops) {
1608 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1612 * Device specific actions.
1614 if (mtvd->vdev_islog) {
1615 spa_set_log_state(spa, SPA_LOG_CLEAR);
1618 * XXX - once we have 'readonly' pool
1619 * support we should be able to handle
1620 * missing data devices by transitioning
1621 * the pool to readonly.
1627 * Swap the missing vdev with the data we were
1628 * able to obtain from the MOS config.
1630 vdev_remove_child(rvd, tvd);
1631 vdev_remove_child(mrvd, mtvd);
1633 vdev_add_child(rvd, mtvd);
1634 vdev_add_child(mrvd, tvd);
1636 spa_config_exit(spa, SCL_ALL, FTAG);
1638 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1641 } else if (mtvd->vdev_islog) {
1643 * Load the slog device's state from the MOS config
1644 * since it's possible that the label does not
1645 * contain the most up-to-date information.
1647 vdev_load_log_state(tvd, mtvd);
1652 spa_config_exit(spa, SCL_ALL, FTAG);
1655 * Ensure we were able to validate the config.
1657 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1661 * Check for missing log devices
1664 spa_check_logs(spa_t *spa)
1666 boolean_t rv = B_FALSE;
1668 switch (spa->spa_log_state) {
1669 case SPA_LOG_MISSING:
1670 /* need to recheck in case slog has been restored */
1671 case SPA_LOG_UNKNOWN:
1672 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1673 NULL, DS_FIND_CHILDREN) != 0);
1675 spa_set_log_state(spa, SPA_LOG_MISSING);
1682 spa_passivate_log(spa_t *spa)
1684 vdev_t *rvd = spa->spa_root_vdev;
1685 boolean_t slog_found = B_FALSE;
1687 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1689 if (!spa_has_slogs(spa))
1692 for (int c = 0; c < rvd->vdev_children; c++) {
1693 vdev_t *tvd = rvd->vdev_child[c];
1694 metaslab_group_t *mg = tvd->vdev_mg;
1696 if (tvd->vdev_islog) {
1697 metaslab_group_passivate(mg);
1698 slog_found = B_TRUE;
1702 return (slog_found);
1706 spa_activate_log(spa_t *spa)
1708 vdev_t *rvd = spa->spa_root_vdev;
1710 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1712 for (int c = 0; c < rvd->vdev_children; c++) {
1713 vdev_t *tvd = rvd->vdev_child[c];
1714 metaslab_group_t *mg = tvd->vdev_mg;
1716 if (tvd->vdev_islog)
1717 metaslab_group_activate(mg);
1722 spa_offline_log(spa_t *spa)
1726 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1727 NULL, DS_FIND_CHILDREN);
1730 * We successfully offlined the log device, sync out the
1731 * current txg so that the "stubby" block can be removed
1734 txg_wait_synced(spa->spa_dsl_pool, 0);
1740 spa_aux_check_removed(spa_aux_vdev_t *sav)
1744 for (i = 0; i < sav->sav_count; i++)
1745 spa_check_removed(sav->sav_vdevs[i]);
1749 spa_claim_notify(zio_t *zio)
1751 spa_t *spa = zio->io_spa;
1756 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1757 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1758 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1759 mutex_exit(&spa->spa_props_lock);
1762 typedef struct spa_load_error {
1763 uint64_t sle_meta_count;
1764 uint64_t sle_data_count;
1768 spa_load_verify_done(zio_t *zio)
1770 blkptr_t *bp = zio->io_bp;
1771 spa_load_error_t *sle = zio->io_private;
1772 dmu_object_type_t type = BP_GET_TYPE(bp);
1773 int error = zio->io_error;
1776 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1777 type != DMU_OT_INTENT_LOG)
1778 atomic_add_64(&sle->sle_meta_count, 1);
1780 atomic_add_64(&sle->sle_data_count, 1);
1782 zio_data_buf_free(zio->io_data, zio->io_size);
1787 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1788 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1792 size_t size = BP_GET_PSIZE(bp);
1793 void *data = zio_data_buf_alloc(size);
1795 zio_nowait(zio_read(rio, spa, bp, data, size,
1796 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1797 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1798 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1804 spa_load_verify(spa_t *spa)
1807 spa_load_error_t sle = { 0 };
1808 zpool_rewind_policy_t policy;
1809 boolean_t verify_ok = B_FALSE;
1812 zpool_get_rewind_policy(spa->spa_config, &policy);
1814 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1817 rio = zio_root(spa, NULL, &sle,
1818 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1820 error = traverse_pool(spa, spa->spa_verify_min_txg,
1821 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1823 (void) zio_wait(rio);
1825 spa->spa_load_meta_errors = sle.sle_meta_count;
1826 spa->spa_load_data_errors = sle.sle_data_count;
1828 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1829 sle.sle_data_count <= policy.zrp_maxdata) {
1833 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1834 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1836 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1837 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1838 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1839 VERIFY(nvlist_add_int64(spa->spa_load_info,
1840 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1841 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1842 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1844 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1848 if (error != ENXIO && error != EIO)
1849 error = SET_ERROR(EIO);
1853 return (verify_ok ? 0 : EIO);
1857 * Find a value in the pool props object.
1860 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1862 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1863 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1867 * Find a value in the pool directory object.
1870 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1872 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1873 name, sizeof (uint64_t), 1, val));
1877 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1879 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1884 * Fix up config after a partly-completed split. This is done with the
1885 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1886 * pool have that entry in their config, but only the splitting one contains
1887 * a list of all the guids of the vdevs that are being split off.
1889 * This function determines what to do with that list: either rejoin
1890 * all the disks to the pool, or complete the splitting process. To attempt
1891 * the rejoin, each disk that is offlined is marked online again, and
1892 * we do a reopen() call. If the vdev label for every disk that was
1893 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1894 * then we call vdev_split() on each disk, and complete the split.
1896 * Otherwise we leave the config alone, with all the vdevs in place in
1897 * the original pool.
1900 spa_try_repair(spa_t *spa, nvlist_t *config)
1907 boolean_t attempt_reopen;
1909 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1912 /* check that the config is complete */
1913 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1914 &glist, &gcount) != 0)
1917 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
1919 /* attempt to online all the vdevs & validate */
1920 attempt_reopen = B_TRUE;
1921 for (i = 0; i < gcount; i++) {
1922 if (glist[i] == 0) /* vdev is hole */
1925 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1926 if (vd[i] == NULL) {
1928 * Don't bother attempting to reopen the disks;
1929 * just do the split.
1931 attempt_reopen = B_FALSE;
1933 /* attempt to re-online it */
1934 vd[i]->vdev_offline = B_FALSE;
1938 if (attempt_reopen) {
1939 vdev_reopen(spa->spa_root_vdev);
1941 /* check each device to see what state it's in */
1942 for (extracted = 0, i = 0; i < gcount; i++) {
1943 if (vd[i] != NULL &&
1944 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1951 * If every disk has been moved to the new pool, or if we never
1952 * even attempted to look at them, then we split them off for
1955 if (!attempt_reopen || gcount == extracted) {
1956 for (i = 0; i < gcount; i++)
1959 vdev_reopen(spa->spa_root_vdev);
1962 kmem_free(vd, gcount * sizeof (vdev_t *));
1966 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
1967 boolean_t mosconfig)
1969 nvlist_t *config = spa->spa_config;
1970 char *ereport = FM_EREPORT_ZFS_POOL;
1976 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
1977 return (SET_ERROR(EINVAL));
1979 ASSERT(spa->spa_comment == NULL);
1980 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
1981 spa->spa_comment = spa_strdup(comment);
1984 * Versioning wasn't explicitly added to the label until later, so if
1985 * it's not present treat it as the initial version.
1987 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1988 &spa->spa_ubsync.ub_version) != 0)
1989 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
1991 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1992 &spa->spa_config_txg);
1994 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1995 spa_guid_exists(pool_guid, 0)) {
1996 error = SET_ERROR(EEXIST);
1998 spa->spa_config_guid = pool_guid;
2000 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2002 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2006 nvlist_free(spa->spa_load_info);
2007 spa->spa_load_info = fnvlist_alloc();
2009 gethrestime(&spa->spa_loaded_ts);
2010 error = spa_load_impl(spa, pool_guid, config, state, type,
2011 mosconfig, &ereport);
2014 spa->spa_minref = refcount_count(&spa->spa_refcount);
2016 if (error != EEXIST) {
2017 spa->spa_loaded_ts.tv_sec = 0;
2018 spa->spa_loaded_ts.tv_nsec = 0;
2020 if (error != EBADF) {
2021 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2024 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2031 * Load an existing storage pool, using the pool's builtin spa_config as a
2032 * source of configuration information.
2035 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2036 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2040 nvlist_t *nvroot = NULL;
2043 uberblock_t *ub = &spa->spa_uberblock;
2044 uint64_t children, config_cache_txg = spa->spa_config_txg;
2045 int orig_mode = spa->spa_mode;
2048 boolean_t missing_feat_write = B_FALSE;
2051 * If this is an untrusted config, access the pool in read-only mode.
2052 * This prevents things like resilvering recently removed devices.
2055 spa->spa_mode = FREAD;
2057 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2059 spa->spa_load_state = state;
2061 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2062 return (SET_ERROR(EINVAL));
2064 parse = (type == SPA_IMPORT_EXISTING ?
2065 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2068 * Create "The Godfather" zio to hold all async IOs
2070 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2071 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2074 * Parse the configuration into a vdev tree. We explicitly set the
2075 * value that will be returned by spa_version() since parsing the
2076 * configuration requires knowing the version number.
2078 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2079 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2080 spa_config_exit(spa, SCL_ALL, FTAG);
2085 ASSERT(spa->spa_root_vdev == rvd);
2087 if (type != SPA_IMPORT_ASSEMBLE) {
2088 ASSERT(spa_guid(spa) == pool_guid);
2092 * Try to open all vdevs, loading each label in the process.
2094 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2095 error = vdev_open(rvd);
2096 spa_config_exit(spa, SCL_ALL, FTAG);
2101 * We need to validate the vdev labels against the configuration that
2102 * we have in hand, which is dependent on the setting of mosconfig. If
2103 * mosconfig is true then we're validating the vdev labels based on
2104 * that config. Otherwise, we're validating against the cached config
2105 * (zpool.cache) that was read when we loaded the zfs module, and then
2106 * later we will recursively call spa_load() and validate against
2109 * If we're assembling a new pool that's been split off from an
2110 * existing pool, the labels haven't yet been updated so we skip
2111 * validation for now.
2113 if (type != SPA_IMPORT_ASSEMBLE) {
2114 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2115 error = vdev_validate(rvd, mosconfig);
2116 spa_config_exit(spa, SCL_ALL, FTAG);
2121 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2122 return (SET_ERROR(ENXIO));
2126 * Find the best uberblock.
2128 vdev_uberblock_load(rvd, ub, &label);
2131 * If we weren't able to find a single valid uberblock, return failure.
2133 if (ub->ub_txg == 0) {
2135 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2139 * If the pool has an unsupported version we can't open it.
2141 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2143 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2146 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2150 * If we weren't able to find what's necessary for reading the
2151 * MOS in the label, return failure.
2153 if (label == NULL || nvlist_lookup_nvlist(label,
2154 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2156 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2161 * Update our in-core representation with the definitive values
2164 nvlist_free(spa->spa_label_features);
2165 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2171 * Look through entries in the label nvlist's features_for_read. If
2172 * there is a feature listed there which we don't understand then we
2173 * cannot open a pool.
2175 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2176 nvlist_t *unsup_feat;
2178 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2181 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2183 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2184 if (!zfeature_is_supported(nvpair_name(nvp))) {
2185 VERIFY(nvlist_add_string(unsup_feat,
2186 nvpair_name(nvp), "") == 0);
2190 if (!nvlist_empty(unsup_feat)) {
2191 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2192 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2193 nvlist_free(unsup_feat);
2194 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2198 nvlist_free(unsup_feat);
2202 * If the vdev guid sum doesn't match the uberblock, we have an
2203 * incomplete configuration. We first check to see if the pool
2204 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2205 * If it is, defer the vdev_guid_sum check till later so we
2206 * can handle missing vdevs.
2208 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2209 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2210 rvd->vdev_guid_sum != ub->ub_guid_sum)
2211 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2213 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2214 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2215 spa_try_repair(spa, config);
2216 spa_config_exit(spa, SCL_ALL, FTAG);
2217 nvlist_free(spa->spa_config_splitting);
2218 spa->spa_config_splitting = NULL;
2222 * Initialize internal SPA structures.
2224 spa->spa_state = POOL_STATE_ACTIVE;
2225 spa->spa_ubsync = spa->spa_uberblock;
2226 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2227 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2228 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2229 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2230 spa->spa_claim_max_txg = spa->spa_first_txg;
2231 spa->spa_prev_software_version = ub->ub_software_version;
2233 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2235 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2236 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2238 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2239 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2241 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2242 boolean_t missing_feat_read = B_FALSE;
2243 nvlist_t *unsup_feat, *enabled_feat;
2245 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2246 &spa->spa_feat_for_read_obj) != 0) {
2247 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2250 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2251 &spa->spa_feat_for_write_obj) != 0) {
2252 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2255 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2256 &spa->spa_feat_desc_obj) != 0) {
2257 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2260 enabled_feat = fnvlist_alloc();
2261 unsup_feat = fnvlist_alloc();
2263 if (!feature_is_supported(spa->spa_meta_objset,
2264 spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj,
2265 unsup_feat, enabled_feat))
2266 missing_feat_read = B_TRUE;
2268 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2269 if (!feature_is_supported(spa->spa_meta_objset,
2270 spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj,
2271 unsup_feat, enabled_feat)) {
2272 missing_feat_write = B_TRUE;
2276 fnvlist_add_nvlist(spa->spa_load_info,
2277 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2279 if (!nvlist_empty(unsup_feat)) {
2280 fnvlist_add_nvlist(spa->spa_load_info,
2281 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2284 fnvlist_free(enabled_feat);
2285 fnvlist_free(unsup_feat);
2287 if (!missing_feat_read) {
2288 fnvlist_add_boolean(spa->spa_load_info,
2289 ZPOOL_CONFIG_CAN_RDONLY);
2293 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2294 * twofold: to determine whether the pool is available for
2295 * import in read-write mode and (if it is not) whether the
2296 * pool is available for import in read-only mode. If the pool
2297 * is available for import in read-write mode, it is displayed
2298 * as available in userland; if it is not available for import
2299 * in read-only mode, it is displayed as unavailable in
2300 * userland. If the pool is available for import in read-only
2301 * mode but not read-write mode, it is displayed as unavailable
2302 * in userland with a special note that the pool is actually
2303 * available for open in read-only mode.
2305 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2306 * missing a feature for write, we must first determine whether
2307 * the pool can be opened read-only before returning to
2308 * userland in order to know whether to display the
2309 * abovementioned note.
2311 if (missing_feat_read || (missing_feat_write &&
2312 spa_writeable(spa))) {
2313 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2318 spa->spa_is_initializing = B_TRUE;
2319 error = dsl_pool_open(spa->spa_dsl_pool);
2320 spa->spa_is_initializing = B_FALSE;
2322 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2326 nvlist_t *policy = NULL, *nvconfig;
2328 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2329 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2331 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2332 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2334 unsigned long myhostid = 0;
2336 VERIFY(nvlist_lookup_string(nvconfig,
2337 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2340 myhostid = zone_get_hostid(NULL);
2343 * We're emulating the system's hostid in userland, so
2344 * we can't use zone_get_hostid().
2346 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2347 #endif /* _KERNEL */
2348 if (check_hostid && hostid != 0 && myhostid != 0 &&
2349 hostid != myhostid) {
2350 nvlist_free(nvconfig);
2351 cmn_err(CE_WARN, "pool '%s' could not be "
2352 "loaded as it was last accessed by "
2353 "another system (host: %s hostid: 0x%lx). "
2354 "See: http://illumos.org/msg/ZFS-8000-EY",
2355 spa_name(spa), hostname,
2356 (unsigned long)hostid);
2357 return (SET_ERROR(EBADF));
2360 if (nvlist_lookup_nvlist(spa->spa_config,
2361 ZPOOL_REWIND_POLICY, &policy) == 0)
2362 VERIFY(nvlist_add_nvlist(nvconfig,
2363 ZPOOL_REWIND_POLICY, policy) == 0);
2365 spa_config_set(spa, nvconfig);
2367 spa_deactivate(spa);
2368 spa_activate(spa, orig_mode);
2370 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2373 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2374 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2375 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2377 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2380 * Load the bit that tells us to use the new accounting function
2381 * (raid-z deflation). If we have an older pool, this will not
2384 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2385 if (error != 0 && error != ENOENT)
2386 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2388 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2389 &spa->spa_creation_version);
2390 if (error != 0 && error != ENOENT)
2391 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2394 * Load the persistent error log. If we have an older pool, this will
2397 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2398 if (error != 0 && error != ENOENT)
2399 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2401 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2402 &spa->spa_errlog_scrub);
2403 if (error != 0 && error != ENOENT)
2404 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2407 * Load the history object. If we have an older pool, this
2408 * will not be present.
2410 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2411 if (error != 0 && error != ENOENT)
2412 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2415 * If we're assembling the pool from the split-off vdevs of
2416 * an existing pool, we don't want to attach the spares & cache
2421 * Load any hot spares for this pool.
2423 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2424 if (error != 0 && error != ENOENT)
2425 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2426 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2427 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2428 if (load_nvlist(spa, spa->spa_spares.sav_object,
2429 &spa->spa_spares.sav_config) != 0)
2430 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2432 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2433 spa_load_spares(spa);
2434 spa_config_exit(spa, SCL_ALL, FTAG);
2435 } else if (error == 0) {
2436 spa->spa_spares.sav_sync = B_TRUE;
2440 * Load any level 2 ARC devices for this pool.
2442 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2443 &spa->spa_l2cache.sav_object);
2444 if (error != 0 && error != ENOENT)
2445 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2446 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2447 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2448 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2449 &spa->spa_l2cache.sav_config) != 0)
2450 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2452 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2453 spa_load_l2cache(spa);
2454 spa_config_exit(spa, SCL_ALL, FTAG);
2455 } else if (error == 0) {
2456 spa->spa_l2cache.sav_sync = B_TRUE;
2459 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2461 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2462 if (error && error != ENOENT)
2463 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2466 uint64_t autoreplace;
2468 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2469 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2470 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2471 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2472 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2473 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2474 &spa->spa_dedup_ditto);
2476 spa->spa_autoreplace = (autoreplace != 0);
2480 * If the 'autoreplace' property is set, then post a resource notifying
2481 * the ZFS DE that it should not issue any faults for unopenable
2482 * devices. We also iterate over the vdevs, and post a sysevent for any
2483 * unopenable vdevs so that the normal autoreplace handler can take
2486 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2487 spa_check_removed(spa->spa_root_vdev);
2489 * For the import case, this is done in spa_import(), because
2490 * at this point we're using the spare definitions from
2491 * the MOS config, not necessarily from the userland config.
2493 if (state != SPA_LOAD_IMPORT) {
2494 spa_aux_check_removed(&spa->spa_spares);
2495 spa_aux_check_removed(&spa->spa_l2cache);
2500 * Load the vdev state for all toplevel vdevs.
2505 * Propagate the leaf DTLs we just loaded all the way up the tree.
2507 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2508 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2509 spa_config_exit(spa, SCL_ALL, FTAG);
2512 * Load the DDTs (dedup tables).
2514 error = ddt_load(spa);
2516 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2518 spa_update_dspace(spa);
2521 * Validate the config, using the MOS config to fill in any
2522 * information which might be missing. If we fail to validate
2523 * the config then declare the pool unfit for use. If we're
2524 * assembling a pool from a split, the log is not transferred
2527 if (type != SPA_IMPORT_ASSEMBLE) {
2530 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2531 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2533 if (!spa_config_valid(spa, nvconfig)) {
2534 nvlist_free(nvconfig);
2535 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2538 nvlist_free(nvconfig);
2541 * Now that we've validated the config, check the state of the
2542 * root vdev. If it can't be opened, it indicates one or
2543 * more toplevel vdevs are faulted.
2545 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2546 return (SET_ERROR(ENXIO));
2548 if (spa_check_logs(spa)) {
2549 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2550 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2554 if (missing_feat_write) {
2555 ASSERT(state == SPA_LOAD_TRYIMPORT);
2558 * At this point, we know that we can open the pool in
2559 * read-only mode but not read-write mode. We now have enough
2560 * information and can return to userland.
2562 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2566 * We've successfully opened the pool, verify that we're ready
2567 * to start pushing transactions.
2569 if (state != SPA_LOAD_TRYIMPORT) {
2570 if (error = spa_load_verify(spa))
2571 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2575 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2576 spa->spa_load_max_txg == UINT64_MAX)) {
2578 int need_update = B_FALSE;
2580 ASSERT(state != SPA_LOAD_TRYIMPORT);
2583 * Claim log blocks that haven't been committed yet.
2584 * This must all happen in a single txg.
2585 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2586 * invoked from zil_claim_log_block()'s i/o done callback.
2587 * Price of rollback is that we abandon the log.
2589 spa->spa_claiming = B_TRUE;
2591 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2592 spa_first_txg(spa));
2593 (void) dmu_objset_find(spa_name(spa),
2594 zil_claim, tx, DS_FIND_CHILDREN);
2597 spa->spa_claiming = B_FALSE;
2599 spa_set_log_state(spa, SPA_LOG_GOOD);
2600 spa->spa_sync_on = B_TRUE;
2601 txg_sync_start(spa->spa_dsl_pool);
2604 * Wait for all claims to sync. We sync up to the highest
2605 * claimed log block birth time so that claimed log blocks
2606 * don't appear to be from the future. spa_claim_max_txg
2607 * will have been set for us by either zil_check_log_chain()
2608 * (invoked from spa_check_logs()) or zil_claim() above.
2610 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2613 * If the config cache is stale, or we have uninitialized
2614 * metaslabs (see spa_vdev_add()), then update the config.
2616 * If this is a verbatim import, trust the current
2617 * in-core spa_config and update the disk labels.
2619 if (config_cache_txg != spa->spa_config_txg ||
2620 state == SPA_LOAD_IMPORT ||
2621 state == SPA_LOAD_RECOVER ||
2622 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2623 need_update = B_TRUE;
2625 for (int c = 0; c < rvd->vdev_children; c++)
2626 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2627 need_update = B_TRUE;
2630 * Update the config cache asychronously in case we're the
2631 * root pool, in which case the config cache isn't writable yet.
2634 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2637 * Check all DTLs to see if anything needs resilvering.
2639 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2640 vdev_resilver_needed(rvd, NULL, NULL))
2641 spa_async_request(spa, SPA_ASYNC_RESILVER);
2644 * Log the fact that we booted up (so that we can detect if
2645 * we rebooted in the middle of an operation).
2647 spa_history_log_version(spa, "open");
2650 * Delete any inconsistent datasets.
2652 (void) dmu_objset_find(spa_name(spa),
2653 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2656 * Clean up any stale temporary dataset userrefs.
2658 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2665 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2667 int mode = spa->spa_mode;
2670 spa_deactivate(spa);
2672 spa->spa_load_max_txg--;
2674 spa_activate(spa, mode);
2675 spa_async_suspend(spa);
2677 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2681 * If spa_load() fails this function will try loading prior txg's. If
2682 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2683 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2684 * function will not rewind the pool and will return the same error as
2688 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2689 uint64_t max_request, int rewind_flags)
2691 nvlist_t *loadinfo = NULL;
2692 nvlist_t *config = NULL;
2693 int load_error, rewind_error;
2694 uint64_t safe_rewind_txg;
2697 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2698 spa->spa_load_max_txg = spa->spa_load_txg;
2699 spa_set_log_state(spa, SPA_LOG_CLEAR);
2701 spa->spa_load_max_txg = max_request;
2704 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2706 if (load_error == 0)
2709 if (spa->spa_root_vdev != NULL)
2710 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2712 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2713 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2715 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2716 nvlist_free(config);
2717 return (load_error);
2720 if (state == SPA_LOAD_RECOVER) {
2721 /* Price of rolling back is discarding txgs, including log */
2722 spa_set_log_state(spa, SPA_LOG_CLEAR);
2725 * If we aren't rolling back save the load info from our first
2726 * import attempt so that we can restore it after attempting
2729 loadinfo = spa->spa_load_info;
2730 spa->spa_load_info = fnvlist_alloc();
2733 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2734 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2735 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2736 TXG_INITIAL : safe_rewind_txg;
2739 * Continue as long as we're finding errors, we're still within
2740 * the acceptable rewind range, and we're still finding uberblocks
2742 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2743 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2744 if (spa->spa_load_max_txg < safe_rewind_txg)
2745 spa->spa_extreme_rewind = B_TRUE;
2746 rewind_error = spa_load_retry(spa, state, mosconfig);
2749 spa->spa_extreme_rewind = B_FALSE;
2750 spa->spa_load_max_txg = UINT64_MAX;
2752 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2753 spa_config_set(spa, config);
2755 if (state == SPA_LOAD_RECOVER) {
2756 ASSERT3P(loadinfo, ==, NULL);
2757 return (rewind_error);
2759 /* Store the rewind info as part of the initial load info */
2760 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2761 spa->spa_load_info);
2763 /* Restore the initial load info */
2764 fnvlist_free(spa->spa_load_info);
2765 spa->spa_load_info = loadinfo;
2767 return (load_error);
2774 * The import case is identical to an open except that the configuration is sent
2775 * down from userland, instead of grabbed from the configuration cache. For the
2776 * case of an open, the pool configuration will exist in the
2777 * POOL_STATE_UNINITIALIZED state.
2779 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2780 * the same time open the pool, without having to keep around the spa_t in some
2784 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2788 spa_load_state_t state = SPA_LOAD_OPEN;
2790 int locked = B_FALSE;
2791 int firstopen = B_FALSE;
2796 * As disgusting as this is, we need to support recursive calls to this
2797 * function because dsl_dir_open() is called during spa_load(), and ends
2798 * up calling spa_open() again. The real fix is to figure out how to
2799 * avoid dsl_dir_open() calling this in the first place.
2801 if (mutex_owner(&spa_namespace_lock) != curthread) {
2802 mutex_enter(&spa_namespace_lock);
2806 if ((spa = spa_lookup(pool)) == NULL) {
2808 mutex_exit(&spa_namespace_lock);
2809 return (SET_ERROR(ENOENT));
2812 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2813 zpool_rewind_policy_t policy;
2817 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2819 if (policy.zrp_request & ZPOOL_DO_REWIND)
2820 state = SPA_LOAD_RECOVER;
2822 spa_activate(spa, spa_mode_global);
2824 if (state != SPA_LOAD_RECOVER)
2825 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2827 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2828 policy.zrp_request);
2830 if (error == EBADF) {
2832 * If vdev_validate() returns failure (indicated by
2833 * EBADF), it indicates that one of the vdevs indicates
2834 * that the pool has been exported or destroyed. If
2835 * this is the case, the config cache is out of sync and
2836 * we should remove the pool from the namespace.
2839 spa_deactivate(spa);
2840 spa_config_sync(spa, B_TRUE, B_TRUE);
2843 mutex_exit(&spa_namespace_lock);
2844 return (SET_ERROR(ENOENT));
2849 * We can't open the pool, but we still have useful
2850 * information: the state of each vdev after the
2851 * attempted vdev_open(). Return this to the user.
2853 if (config != NULL && spa->spa_config) {
2854 VERIFY(nvlist_dup(spa->spa_config, config,
2856 VERIFY(nvlist_add_nvlist(*config,
2857 ZPOOL_CONFIG_LOAD_INFO,
2858 spa->spa_load_info) == 0);
2861 spa_deactivate(spa);
2862 spa->spa_last_open_failed = error;
2864 mutex_exit(&spa_namespace_lock);
2870 spa_open_ref(spa, tag);
2873 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2876 * If we've recovered the pool, pass back any information we
2877 * gathered while doing the load.
2879 if (state == SPA_LOAD_RECOVER) {
2880 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2881 spa->spa_load_info) == 0);
2885 spa->spa_last_open_failed = 0;
2886 spa->spa_last_ubsync_txg = 0;
2887 spa->spa_load_txg = 0;
2888 mutex_exit(&spa_namespace_lock);
2892 zvol_create_minors(spa->spa_name);
2903 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2906 return (spa_open_common(name, spapp, tag, policy, config));
2910 spa_open(const char *name, spa_t **spapp, void *tag)
2912 return (spa_open_common(name, spapp, tag, NULL, NULL));
2916 * Lookup the given spa_t, incrementing the inject count in the process,
2917 * preventing it from being exported or destroyed.
2920 spa_inject_addref(char *name)
2924 mutex_enter(&spa_namespace_lock);
2925 if ((spa = spa_lookup(name)) == NULL) {
2926 mutex_exit(&spa_namespace_lock);
2929 spa->spa_inject_ref++;
2930 mutex_exit(&spa_namespace_lock);
2936 spa_inject_delref(spa_t *spa)
2938 mutex_enter(&spa_namespace_lock);
2939 spa->spa_inject_ref--;
2940 mutex_exit(&spa_namespace_lock);
2944 * Add spares device information to the nvlist.
2947 spa_add_spares(spa_t *spa, nvlist_t *config)
2957 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2959 if (spa->spa_spares.sav_count == 0)
2962 VERIFY(nvlist_lookup_nvlist(config,
2963 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2964 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2965 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2967 VERIFY(nvlist_add_nvlist_array(nvroot,
2968 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2969 VERIFY(nvlist_lookup_nvlist_array(nvroot,
2970 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
2973 * Go through and find any spares which have since been
2974 * repurposed as an active spare. If this is the case, update
2975 * their status appropriately.
2977 for (i = 0; i < nspares; i++) {
2978 VERIFY(nvlist_lookup_uint64(spares[i],
2979 ZPOOL_CONFIG_GUID, &guid) == 0);
2980 if (spa_spare_exists(guid, &pool, NULL) &&
2982 VERIFY(nvlist_lookup_uint64_array(
2983 spares[i], ZPOOL_CONFIG_VDEV_STATS,
2984 (uint64_t **)&vs, &vsc) == 0);
2985 vs->vs_state = VDEV_STATE_CANT_OPEN;
2986 vs->vs_aux = VDEV_AUX_SPARED;
2993 * Add l2cache device information to the nvlist, including vdev stats.
2996 spa_add_l2cache(spa_t *spa, nvlist_t *config)
2999 uint_t i, j, nl2cache;
3006 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3008 if (spa->spa_l2cache.sav_count == 0)
3011 VERIFY(nvlist_lookup_nvlist(config,
3012 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3013 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3014 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3015 if (nl2cache != 0) {
3016 VERIFY(nvlist_add_nvlist_array(nvroot,
3017 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3018 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3019 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3022 * Update level 2 cache device stats.
3025 for (i = 0; i < nl2cache; i++) {
3026 VERIFY(nvlist_lookup_uint64(l2cache[i],
3027 ZPOOL_CONFIG_GUID, &guid) == 0);
3030 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3032 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3033 vd = spa->spa_l2cache.sav_vdevs[j];
3039 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3040 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3042 vdev_get_stats(vd, vs);
3048 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3054 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3055 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3057 if (spa->spa_feat_for_read_obj != 0) {
3058 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3059 spa->spa_feat_for_read_obj);
3060 zap_cursor_retrieve(&zc, &za) == 0;
3061 zap_cursor_advance(&zc)) {
3062 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3063 za.za_num_integers == 1);
3064 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3065 za.za_first_integer));
3067 zap_cursor_fini(&zc);
3070 if (spa->spa_feat_for_write_obj != 0) {
3071 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3072 spa->spa_feat_for_write_obj);
3073 zap_cursor_retrieve(&zc, &za) == 0;
3074 zap_cursor_advance(&zc)) {
3075 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3076 za.za_num_integers == 1);
3077 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3078 za.za_first_integer));
3080 zap_cursor_fini(&zc);
3083 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3085 nvlist_free(features);
3089 spa_get_stats(const char *name, nvlist_t **config,
3090 char *altroot, size_t buflen)
3096 error = spa_open_common(name, &spa, FTAG, NULL, config);
3100 * This still leaves a window of inconsistency where the spares
3101 * or l2cache devices could change and the config would be
3102 * self-inconsistent.
3104 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3106 if (*config != NULL) {
3107 uint64_t loadtimes[2];
3109 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3110 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3111 VERIFY(nvlist_add_uint64_array(*config,
3112 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3114 VERIFY(nvlist_add_uint64(*config,
3115 ZPOOL_CONFIG_ERRCOUNT,
3116 spa_get_errlog_size(spa)) == 0);
3118 if (spa_suspended(spa))
3119 VERIFY(nvlist_add_uint64(*config,
3120 ZPOOL_CONFIG_SUSPENDED,
3121 spa->spa_failmode) == 0);
3123 spa_add_spares(spa, *config);
3124 spa_add_l2cache(spa, *config);
3125 spa_add_feature_stats(spa, *config);
3130 * We want to get the alternate root even for faulted pools, so we cheat
3131 * and call spa_lookup() directly.
3135 mutex_enter(&spa_namespace_lock);
3136 spa = spa_lookup(name);
3138 spa_altroot(spa, altroot, buflen);
3142 mutex_exit(&spa_namespace_lock);
3144 spa_altroot(spa, altroot, buflen);
3149 spa_config_exit(spa, SCL_CONFIG, FTAG);
3150 spa_close(spa, FTAG);
3157 * Validate that the auxiliary device array is well formed. We must have an
3158 * array of nvlists, each which describes a valid leaf vdev. If this is an
3159 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3160 * specified, as long as they are well-formed.
3163 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3164 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3165 vdev_labeltype_t label)
3172 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3175 * It's acceptable to have no devs specified.
3177 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3181 return (SET_ERROR(EINVAL));
3184 * Make sure the pool is formatted with a version that supports this
3187 if (spa_version(spa) < version)
3188 return (SET_ERROR(ENOTSUP));
3191 * Set the pending device list so we correctly handle device in-use
3194 sav->sav_pending = dev;
3195 sav->sav_npending = ndev;
3197 for (i = 0; i < ndev; i++) {
3198 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3202 if (!vd->vdev_ops->vdev_op_leaf) {
3204 error = SET_ERROR(EINVAL);
3209 * The L2ARC currently only supports disk devices in
3210 * kernel context. For user-level testing, we allow it.
3213 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3214 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3215 error = SET_ERROR(ENOTBLK);
3222 if ((error = vdev_open(vd)) == 0 &&
3223 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3224 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3225 vd->vdev_guid) == 0);
3231 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3238 sav->sav_pending = NULL;
3239 sav->sav_npending = 0;
3244 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3248 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3250 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3251 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3252 VDEV_LABEL_SPARE)) != 0) {
3256 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3257 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3258 VDEV_LABEL_L2CACHE));
3262 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3267 if (sav->sav_config != NULL) {
3273 * Generate new dev list by concatentating with the
3276 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3277 &olddevs, &oldndevs) == 0);
3279 newdevs = kmem_alloc(sizeof (void *) *
3280 (ndevs + oldndevs), KM_SLEEP);
3281 for (i = 0; i < oldndevs; i++)
3282 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3284 for (i = 0; i < ndevs; i++)
3285 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3288 VERIFY(nvlist_remove(sav->sav_config, config,
3289 DATA_TYPE_NVLIST_ARRAY) == 0);
3291 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3292 config, newdevs, ndevs + oldndevs) == 0);
3293 for (i = 0; i < oldndevs + ndevs; i++)
3294 nvlist_free(newdevs[i]);
3295 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3298 * Generate a new dev list.
3300 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3302 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3308 * Stop and drop level 2 ARC devices
3311 spa_l2cache_drop(spa_t *spa)
3315 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3317 for (i = 0; i < sav->sav_count; i++) {
3320 vd = sav->sav_vdevs[i];
3323 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3324 pool != 0ULL && l2arc_vdev_present(vd))
3325 l2arc_remove_vdev(vd);
3333 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3337 char *altroot = NULL;
3342 uint64_t txg = TXG_INITIAL;
3343 nvlist_t **spares, **l2cache;
3344 uint_t nspares, nl2cache;
3345 uint64_t version, obj;
3346 boolean_t has_features;
3349 * If this pool already exists, return failure.
3351 mutex_enter(&spa_namespace_lock);
3352 if (spa_lookup(pool) != NULL) {
3353 mutex_exit(&spa_namespace_lock);
3354 return (SET_ERROR(EEXIST));
3358 * Allocate a new spa_t structure.
3360 (void) nvlist_lookup_string(props,
3361 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3362 spa = spa_add(pool, NULL, altroot);
3363 spa_activate(spa, spa_mode_global);
3365 if (props && (error = spa_prop_validate(spa, props))) {
3366 spa_deactivate(spa);
3368 mutex_exit(&spa_namespace_lock);
3372 has_features = B_FALSE;
3373 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3374 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3375 if (zpool_prop_feature(nvpair_name(elem)))
3376 has_features = B_TRUE;
3379 if (has_features || nvlist_lookup_uint64(props,
3380 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3381 version = SPA_VERSION;
3383 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3385 spa->spa_first_txg = txg;
3386 spa->spa_uberblock.ub_txg = txg - 1;
3387 spa->spa_uberblock.ub_version = version;
3388 spa->spa_ubsync = spa->spa_uberblock;
3391 * Create "The Godfather" zio to hold all async IOs
3393 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3394 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3397 * Create the root vdev.
3399 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3401 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3403 ASSERT(error != 0 || rvd != NULL);
3404 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3406 if (error == 0 && !zfs_allocatable_devs(nvroot))
3407 error = SET_ERROR(EINVAL);
3410 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3411 (error = spa_validate_aux(spa, nvroot, txg,
3412 VDEV_ALLOC_ADD)) == 0) {
3413 for (int c = 0; c < rvd->vdev_children; c++) {
3414 vdev_metaslab_set_size(rvd->vdev_child[c]);
3415 vdev_expand(rvd->vdev_child[c], txg);
3419 spa_config_exit(spa, SCL_ALL, FTAG);
3423 spa_deactivate(spa);
3425 mutex_exit(&spa_namespace_lock);
3430 * Get the list of spares, if specified.
3432 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3433 &spares, &nspares) == 0) {
3434 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3436 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3437 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3438 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3439 spa_load_spares(spa);
3440 spa_config_exit(spa, SCL_ALL, FTAG);
3441 spa->spa_spares.sav_sync = B_TRUE;
3445 * Get the list of level 2 cache devices, if specified.
3447 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3448 &l2cache, &nl2cache) == 0) {
3449 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3450 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3451 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3452 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3453 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3454 spa_load_l2cache(spa);
3455 spa_config_exit(spa, SCL_ALL, FTAG);
3456 spa->spa_l2cache.sav_sync = B_TRUE;
3459 spa->spa_is_initializing = B_TRUE;
3460 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3461 spa->spa_meta_objset = dp->dp_meta_objset;
3462 spa->spa_is_initializing = B_FALSE;
3465 * Create DDTs (dedup tables).
3469 spa_update_dspace(spa);
3471 tx = dmu_tx_create_assigned(dp, txg);
3474 * Create the pool config object.
3476 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3477 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3478 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3480 if (zap_add(spa->spa_meta_objset,
3481 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3482 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3483 cmn_err(CE_PANIC, "failed to add pool config");
3486 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3487 spa_feature_create_zap_objects(spa, tx);
3489 if (zap_add(spa->spa_meta_objset,
3490 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3491 sizeof (uint64_t), 1, &version, tx) != 0) {
3492 cmn_err(CE_PANIC, "failed to add pool version");
3495 /* Newly created pools with the right version are always deflated. */
3496 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3497 spa->spa_deflate = TRUE;
3498 if (zap_add(spa->spa_meta_objset,
3499 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3500 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3501 cmn_err(CE_PANIC, "failed to add deflate");
3506 * Create the deferred-free bpobj. Turn off compression
3507 * because sync-to-convergence takes longer if the blocksize
3510 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3511 dmu_object_set_compress(spa->spa_meta_objset, obj,
3512 ZIO_COMPRESS_OFF, tx);
3513 if (zap_add(spa->spa_meta_objset,
3514 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3515 sizeof (uint64_t), 1, &obj, tx) != 0) {
3516 cmn_err(CE_PANIC, "failed to add bpobj");
3518 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3519 spa->spa_meta_objset, obj));
3522 * Create the pool's history object.
3524 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3525 spa_history_create_obj(spa, tx);
3528 * Set pool properties.
3530 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3531 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3532 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3533 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3535 if (props != NULL) {
3536 spa_configfile_set(spa, props, B_FALSE);
3537 spa_sync_props(props, tx);
3542 spa->spa_sync_on = B_TRUE;
3543 txg_sync_start(spa->spa_dsl_pool);
3546 * We explicitly wait for the first transaction to complete so that our
3547 * bean counters are appropriately updated.
3549 txg_wait_synced(spa->spa_dsl_pool, txg);
3551 spa_config_sync(spa, B_FALSE, B_TRUE);
3553 spa_history_log_version(spa, "create");
3555 spa->spa_minref = refcount_count(&spa->spa_refcount);
3557 mutex_exit(&spa_namespace_lock);
3565 * Get the root pool information from the root disk, then import the root pool
3566 * during the system boot up time.
3568 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3571 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3574 nvlist_t *nvtop, *nvroot;
3577 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3581 * Add this top-level vdev to the child array.
3583 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3585 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3587 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3590 * Put this pool's top-level vdevs into a root vdev.
3592 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3593 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3594 VDEV_TYPE_ROOT) == 0);
3595 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3596 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3597 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3601 * Replace the existing vdev_tree with the new root vdev in
3602 * this pool's configuration (remove the old, add the new).
3604 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3605 nvlist_free(nvroot);
3610 * Walk the vdev tree and see if we can find a device with "better"
3611 * configuration. A configuration is "better" if the label on that
3612 * device has a more recent txg.
3615 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3617 for (int c = 0; c < vd->vdev_children; c++)
3618 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3620 if (vd->vdev_ops->vdev_op_leaf) {
3624 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3628 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3632 * Do we have a better boot device?
3634 if (label_txg > *txg) {
3643 * Import a root pool.
3645 * For x86. devpath_list will consist of devid and/or physpath name of
3646 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3647 * The GRUB "findroot" command will return the vdev we should boot.
3649 * For Sparc, devpath_list consists the physpath name of the booting device
3650 * no matter the rootpool is a single device pool or a mirrored pool.
3652 * "/pci@1f,0/ide@d/disk@0,0:a"
3655 spa_import_rootpool(char *devpath, char *devid)
3658 vdev_t *rvd, *bvd, *avd = NULL;
3659 nvlist_t *config, *nvtop;
3665 * Read the label from the boot device and generate a configuration.
3667 config = spa_generate_rootconf(devpath, devid, &guid);
3668 #if defined(_OBP) && defined(_KERNEL)
3669 if (config == NULL) {
3670 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3672 get_iscsi_bootpath_phy(devpath);
3673 config = spa_generate_rootconf(devpath, devid, &guid);
3677 if (config == NULL) {
3678 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3680 return (SET_ERROR(EIO));
3683 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3685 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3687 mutex_enter(&spa_namespace_lock);
3688 if ((spa = spa_lookup(pname)) != NULL) {
3690 * Remove the existing root pool from the namespace so that we
3691 * can replace it with the correct config we just read in.
3696 spa = spa_add(pname, config, NULL);
3697 spa->spa_is_root = B_TRUE;
3698 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3701 * Build up a vdev tree based on the boot device's label config.
3703 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3705 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3706 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3707 VDEV_ALLOC_ROOTPOOL);
3708 spa_config_exit(spa, SCL_ALL, FTAG);
3710 mutex_exit(&spa_namespace_lock);
3711 nvlist_free(config);
3712 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3718 * Get the boot vdev.
3720 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3721 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3722 (u_longlong_t)guid);
3723 error = SET_ERROR(ENOENT);
3728 * Determine if there is a better boot device.
3731 spa_alt_rootvdev(rvd, &avd, &txg);
3733 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3734 "try booting from '%s'", avd->vdev_path);
3735 error = SET_ERROR(EINVAL);
3740 * If the boot device is part of a spare vdev then ensure that
3741 * we're booting off the active spare.
3743 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3744 !bvd->vdev_isspare) {
3745 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3746 "try booting from '%s'",
3748 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3749 error = SET_ERROR(EINVAL);
3755 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3757 spa_config_exit(spa, SCL_ALL, FTAG);
3758 mutex_exit(&spa_namespace_lock);
3760 nvlist_free(config);
3766 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3770 spa_generate_rootconf(const char *name)
3772 nvlist_t **configs, **tops;
3774 nvlist_t *best_cfg, *nvtop, *nvroot;
3783 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3786 ASSERT3U(count, !=, 0);
3788 for (i = 0; i < count; i++) {
3791 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
3793 if (txg > best_txg) {
3795 best_cfg = configs[i];
3800 * Multi-vdev root pool configuration discovery is not supported yet.
3803 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3805 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
3808 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
3809 for (i = 0; i < nchildren; i++) {
3812 if (configs[i] == NULL)
3814 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
3816 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
3818 for (i = 0; holes != NULL && i < nholes; i++) {
3821 if (tops[holes[i]] != NULL)
3823 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
3824 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
3825 VDEV_TYPE_HOLE) == 0);
3826 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
3828 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
3831 for (i = 0; i < nchildren; i++) {
3832 if (tops[i] != NULL)
3834 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
3835 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
3836 VDEV_TYPE_MISSING) == 0);
3837 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
3839 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
3844 * Create pool config based on the best vdev config.
3846 nvlist_dup(best_cfg, &config, KM_SLEEP);
3849 * Put this pool's top-level vdevs into a root vdev.
3851 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3853 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3854 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3855 VDEV_TYPE_ROOT) == 0);
3856 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3857 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3858 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3859 tops, nchildren) == 0);
3862 * Replace the existing vdev_tree with the new root vdev in
3863 * this pool's configuration (remove the old, add the new).
3865 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3868 * Drop vdev config elements that should not be present at pool level.
3870 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
3871 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
3873 for (i = 0; i < count; i++)
3874 nvlist_free(configs[i]);
3875 kmem_free(configs, count * sizeof(void *));
3876 for (i = 0; i < nchildren; i++)
3877 nvlist_free(tops[i]);
3878 kmem_free(tops, nchildren * sizeof(void *));
3879 nvlist_free(nvroot);
3884 spa_import_rootpool(const char *name)
3887 vdev_t *rvd, *bvd, *avd = NULL;
3888 nvlist_t *config, *nvtop;
3894 * Read the label from the boot device and generate a configuration.
3896 config = spa_generate_rootconf(name);
3898 mutex_enter(&spa_namespace_lock);
3899 if (config != NULL) {
3900 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3901 &pname) == 0 && strcmp(name, pname) == 0);
3902 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
3905 if ((spa = spa_lookup(pname)) != NULL) {
3907 * Remove the existing root pool from the namespace so
3908 * that we can replace it with the correct config
3913 spa = spa_add(pname, config, NULL);
3916 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
3917 * via spa_version().
3919 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
3920 &spa->spa_ubsync.ub_version) != 0)
3921 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
3922 } else if ((spa = spa_lookup(name)) == NULL) {
3923 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
3927 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
3929 spa->spa_is_root = B_TRUE;
3930 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3933 * Build up a vdev tree based on the boot device's label config.
3935 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3937 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3938 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3939 VDEV_ALLOC_ROOTPOOL);
3940 spa_config_exit(spa, SCL_ALL, FTAG);
3942 mutex_exit(&spa_namespace_lock);
3943 nvlist_free(config);
3944 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3949 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3951 spa_config_exit(spa, SCL_ALL, FTAG);
3952 mutex_exit(&spa_namespace_lock);
3954 nvlist_free(config);
3962 * Import a non-root pool into the system.
3965 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3968 char *altroot = NULL;
3969 spa_load_state_t state = SPA_LOAD_IMPORT;
3970 zpool_rewind_policy_t policy;
3971 uint64_t mode = spa_mode_global;
3972 uint64_t readonly = B_FALSE;
3975 nvlist_t **spares, **l2cache;
3976 uint_t nspares, nl2cache;
3979 * If a pool with this name exists, return failure.
3981 mutex_enter(&spa_namespace_lock);
3982 if (spa_lookup(pool) != NULL) {
3983 mutex_exit(&spa_namespace_lock);
3984 return (SET_ERROR(EEXIST));
3988 * Create and initialize the spa structure.
3990 (void) nvlist_lookup_string(props,
3991 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3992 (void) nvlist_lookup_uint64(props,
3993 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3996 spa = spa_add(pool, config, altroot);
3997 spa->spa_import_flags = flags;
4000 * Verbatim import - Take a pool and insert it into the namespace
4001 * as if it had been loaded at boot.
4003 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4005 spa_configfile_set(spa, props, B_FALSE);
4007 spa_config_sync(spa, B_FALSE, B_TRUE);
4009 mutex_exit(&spa_namespace_lock);
4010 spa_history_log_version(spa, "import");
4015 spa_activate(spa, mode);
4018 * Don't start async tasks until we know everything is healthy.
4020 spa_async_suspend(spa);
4022 zpool_get_rewind_policy(config, &policy);
4023 if (policy.zrp_request & ZPOOL_DO_REWIND)
4024 state = SPA_LOAD_RECOVER;
4027 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4028 * because the user-supplied config is actually the one to trust when
4031 if (state != SPA_LOAD_RECOVER)
4032 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4034 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4035 policy.zrp_request);
4038 * Propagate anything learned while loading the pool and pass it
4039 * back to caller (i.e. rewind info, missing devices, etc).
4041 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4042 spa->spa_load_info) == 0);
4044 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4046 * Toss any existing sparelist, as it doesn't have any validity
4047 * anymore, and conflicts with spa_has_spare().
4049 if (spa->spa_spares.sav_config) {
4050 nvlist_free(spa->spa_spares.sav_config);
4051 spa->spa_spares.sav_config = NULL;
4052 spa_load_spares(spa);
4054 if (spa->spa_l2cache.sav_config) {
4055 nvlist_free(spa->spa_l2cache.sav_config);
4056 spa->spa_l2cache.sav_config = NULL;
4057 spa_load_l2cache(spa);
4060 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4063 error = spa_validate_aux(spa, nvroot, -1ULL,
4066 error = spa_validate_aux(spa, nvroot, -1ULL,
4067 VDEV_ALLOC_L2CACHE);
4068 spa_config_exit(spa, SCL_ALL, FTAG);
4071 spa_configfile_set(spa, props, B_FALSE);
4073 if (error != 0 || (props && spa_writeable(spa) &&
4074 (error = spa_prop_set(spa, props)))) {
4076 spa_deactivate(spa);
4078 mutex_exit(&spa_namespace_lock);
4082 spa_async_resume(spa);
4085 * Override any spares and level 2 cache devices as specified by
4086 * the user, as these may have correct device names/devids, etc.
4088 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4089 &spares, &nspares) == 0) {
4090 if (spa->spa_spares.sav_config)
4091 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4092 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4094 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4095 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4096 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4097 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4098 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4099 spa_load_spares(spa);
4100 spa_config_exit(spa, SCL_ALL, FTAG);
4101 spa->spa_spares.sav_sync = B_TRUE;
4103 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4104 &l2cache, &nl2cache) == 0) {
4105 if (spa->spa_l2cache.sav_config)
4106 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4107 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4109 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4110 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4111 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4112 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4113 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4114 spa_load_l2cache(spa);
4115 spa_config_exit(spa, SCL_ALL, FTAG);
4116 spa->spa_l2cache.sav_sync = B_TRUE;
4120 * Check for any removed devices.
4122 if (spa->spa_autoreplace) {
4123 spa_aux_check_removed(&spa->spa_spares);
4124 spa_aux_check_removed(&spa->spa_l2cache);
4127 if (spa_writeable(spa)) {
4129 * Update the config cache to include the newly-imported pool.
4131 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4135 * It's possible that the pool was expanded while it was exported.
4136 * We kick off an async task to handle this for us.
4138 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4140 mutex_exit(&spa_namespace_lock);
4141 spa_history_log_version(spa, "import");
4145 zvol_create_minors(pool);
4152 spa_tryimport(nvlist_t *tryconfig)
4154 nvlist_t *config = NULL;
4160 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4163 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4167 * Create and initialize the spa structure.
4169 mutex_enter(&spa_namespace_lock);
4170 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4171 spa_activate(spa, FREAD);
4174 * Pass off the heavy lifting to spa_load().
4175 * Pass TRUE for mosconfig because the user-supplied config
4176 * is actually the one to trust when doing an import.
4178 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4181 * If 'tryconfig' was at least parsable, return the current config.
4183 if (spa->spa_root_vdev != NULL) {
4184 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4185 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4187 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4189 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4190 spa->spa_uberblock.ub_timestamp) == 0);
4191 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4192 spa->spa_load_info) == 0);
4195 * If the bootfs property exists on this pool then we
4196 * copy it out so that external consumers can tell which
4197 * pools are bootable.
4199 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4200 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4203 * We have to play games with the name since the
4204 * pool was opened as TRYIMPORT_NAME.
4206 if (dsl_dsobj_to_dsname(spa_name(spa),
4207 spa->spa_bootfs, tmpname) == 0) {
4209 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4211 cp = strchr(tmpname, '/');
4213 (void) strlcpy(dsname, tmpname,
4216 (void) snprintf(dsname, MAXPATHLEN,
4217 "%s/%s", poolname, ++cp);
4219 VERIFY(nvlist_add_string(config,
4220 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4221 kmem_free(dsname, MAXPATHLEN);
4223 kmem_free(tmpname, MAXPATHLEN);
4227 * Add the list of hot spares and level 2 cache devices.
4229 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4230 spa_add_spares(spa, config);
4231 spa_add_l2cache(spa, config);
4232 spa_config_exit(spa, SCL_CONFIG, FTAG);
4236 spa_deactivate(spa);
4238 mutex_exit(&spa_namespace_lock);
4244 * Pool export/destroy
4246 * The act of destroying or exporting a pool is very simple. We make sure there
4247 * is no more pending I/O and any references to the pool are gone. Then, we
4248 * update the pool state and sync all the labels to disk, removing the
4249 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4250 * we don't sync the labels or remove the configuration cache.
4253 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4254 boolean_t force, boolean_t hardforce)
4261 if (!(spa_mode_global & FWRITE))
4262 return (SET_ERROR(EROFS));
4264 mutex_enter(&spa_namespace_lock);
4265 if ((spa = spa_lookup(pool)) == NULL) {
4266 mutex_exit(&spa_namespace_lock);
4267 return (SET_ERROR(ENOENT));
4271 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4272 * reacquire the namespace lock, and see if we can export.
4274 spa_open_ref(spa, FTAG);
4275 mutex_exit(&spa_namespace_lock);
4276 spa_async_suspend(spa);
4277 mutex_enter(&spa_namespace_lock);
4278 spa_close(spa, FTAG);
4281 * The pool will be in core if it's openable,
4282 * in which case we can modify its state.
4284 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4286 * Objsets may be open only because they're dirty, so we
4287 * have to force it to sync before checking spa_refcnt.
4289 txg_wait_synced(spa->spa_dsl_pool, 0);
4292 * A pool cannot be exported or destroyed if there are active
4293 * references. If we are resetting a pool, allow references by
4294 * fault injection handlers.
4296 if (!spa_refcount_zero(spa) ||
4297 (spa->spa_inject_ref != 0 &&
4298 new_state != POOL_STATE_UNINITIALIZED)) {
4299 spa_async_resume(spa);
4300 mutex_exit(&spa_namespace_lock);
4301 return (SET_ERROR(EBUSY));
4305 * A pool cannot be exported if it has an active shared spare.
4306 * This is to prevent other pools stealing the active spare
4307 * from an exported pool. At user's own will, such pool can
4308 * be forcedly exported.
4310 if (!force && new_state == POOL_STATE_EXPORTED &&
4311 spa_has_active_shared_spare(spa)) {
4312 spa_async_resume(spa);
4313 mutex_exit(&spa_namespace_lock);
4314 return (SET_ERROR(EXDEV));
4318 * We want this to be reflected on every label,
4319 * so mark them all dirty. spa_unload() will do the
4320 * final sync that pushes these changes out.
4322 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4323 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4324 spa->spa_state = new_state;
4325 spa->spa_final_txg = spa_last_synced_txg(spa) +
4327 vdev_config_dirty(spa->spa_root_vdev);
4328 spa_config_exit(spa, SCL_ALL, FTAG);
4332 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4334 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4336 spa_deactivate(spa);
4339 if (oldconfig && spa->spa_config)
4340 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4342 if (new_state != POOL_STATE_UNINITIALIZED) {
4344 spa_config_sync(spa, B_TRUE, B_TRUE);
4347 mutex_exit(&spa_namespace_lock);
4353 * Destroy a storage pool.
4356 spa_destroy(char *pool)
4358 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4363 * Export a storage pool.
4366 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4367 boolean_t hardforce)
4369 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4374 * Similar to spa_export(), this unloads the spa_t without actually removing it
4375 * from the namespace in any way.
4378 spa_reset(char *pool)
4380 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4385 * ==========================================================================
4386 * Device manipulation
4387 * ==========================================================================
4391 * Add a device to a storage pool.
4394 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4398 vdev_t *rvd = spa->spa_root_vdev;
4400 nvlist_t **spares, **l2cache;
4401 uint_t nspares, nl2cache;
4403 ASSERT(spa_writeable(spa));
4405 txg = spa_vdev_enter(spa);
4407 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4408 VDEV_ALLOC_ADD)) != 0)
4409 return (spa_vdev_exit(spa, NULL, txg, error));
4411 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4413 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4417 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4421 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4422 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4424 if (vd->vdev_children != 0 &&
4425 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4426 return (spa_vdev_exit(spa, vd, txg, error));
4429 * We must validate the spares and l2cache devices after checking the
4430 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4432 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4433 return (spa_vdev_exit(spa, vd, txg, error));
4436 * Transfer each new top-level vdev from vd to rvd.
4438 for (int c = 0; c < vd->vdev_children; c++) {
4441 * Set the vdev id to the first hole, if one exists.
4443 for (id = 0; id < rvd->vdev_children; id++) {
4444 if (rvd->vdev_child[id]->vdev_ishole) {
4445 vdev_free(rvd->vdev_child[id]);
4449 tvd = vd->vdev_child[c];
4450 vdev_remove_child(vd, tvd);
4452 vdev_add_child(rvd, tvd);
4453 vdev_config_dirty(tvd);
4457 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4458 ZPOOL_CONFIG_SPARES);
4459 spa_load_spares(spa);
4460 spa->spa_spares.sav_sync = B_TRUE;
4463 if (nl2cache != 0) {
4464 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4465 ZPOOL_CONFIG_L2CACHE);
4466 spa_load_l2cache(spa);
4467 spa->spa_l2cache.sav_sync = B_TRUE;
4471 * We have to be careful when adding new vdevs to an existing pool.
4472 * If other threads start allocating from these vdevs before we
4473 * sync the config cache, and we lose power, then upon reboot we may
4474 * fail to open the pool because there are DVAs that the config cache
4475 * can't translate. Therefore, we first add the vdevs without
4476 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4477 * and then let spa_config_update() initialize the new metaslabs.
4479 * spa_load() checks for added-but-not-initialized vdevs, so that
4480 * if we lose power at any point in this sequence, the remaining
4481 * steps will be completed the next time we load the pool.
4483 (void) spa_vdev_exit(spa, vd, txg, 0);
4485 mutex_enter(&spa_namespace_lock);
4486 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4487 mutex_exit(&spa_namespace_lock);
4493 * Attach a device to a mirror. The arguments are the path to any device
4494 * in the mirror, and the nvroot for the new device. If the path specifies
4495 * a device that is not mirrored, we automatically insert the mirror vdev.
4497 * If 'replacing' is specified, the new device is intended to replace the
4498 * existing device; in this case the two devices are made into their own
4499 * mirror using the 'replacing' vdev, which is functionally identical to
4500 * the mirror vdev (it actually reuses all the same ops) but has a few
4501 * extra rules: you can't attach to it after it's been created, and upon
4502 * completion of resilvering, the first disk (the one being replaced)
4503 * is automatically detached.
4506 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4508 uint64_t txg, dtl_max_txg;
4509 vdev_t *rvd = spa->spa_root_vdev;
4510 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4512 char *oldvdpath, *newvdpath;
4516 ASSERT(spa_writeable(spa));
4518 txg = spa_vdev_enter(spa);
4520 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4523 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4525 if (!oldvd->vdev_ops->vdev_op_leaf)
4526 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4528 pvd = oldvd->vdev_parent;
4530 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4531 VDEV_ALLOC_ATTACH)) != 0)
4532 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4534 if (newrootvd->vdev_children != 1)
4535 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4537 newvd = newrootvd->vdev_child[0];
4539 if (!newvd->vdev_ops->vdev_op_leaf)
4540 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4542 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4543 return (spa_vdev_exit(spa, newrootvd, txg, error));
4546 * Spares can't replace logs
4548 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4549 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4553 * For attach, the only allowable parent is a mirror or the root
4556 if (pvd->vdev_ops != &vdev_mirror_ops &&
4557 pvd->vdev_ops != &vdev_root_ops)
4558 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4560 pvops = &vdev_mirror_ops;
4563 * Active hot spares can only be replaced by inactive hot
4566 if (pvd->vdev_ops == &vdev_spare_ops &&
4567 oldvd->vdev_isspare &&
4568 !spa_has_spare(spa, newvd->vdev_guid))
4569 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4572 * If the source is a hot spare, and the parent isn't already a
4573 * spare, then we want to create a new hot spare. Otherwise, we
4574 * want to create a replacing vdev. The user is not allowed to
4575 * attach to a spared vdev child unless the 'isspare' state is
4576 * the same (spare replaces spare, non-spare replaces
4579 if (pvd->vdev_ops == &vdev_replacing_ops &&
4580 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4581 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4582 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4583 newvd->vdev_isspare != oldvd->vdev_isspare) {
4584 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4587 if (newvd->vdev_isspare)
4588 pvops = &vdev_spare_ops;
4590 pvops = &vdev_replacing_ops;
4594 * Make sure the new device is big enough.
4596 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4597 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4600 * The new device cannot have a higher alignment requirement
4601 * than the top-level vdev.
4603 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4604 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4607 * If this is an in-place replacement, update oldvd's path and devid
4608 * to make it distinguishable from newvd, and unopenable from now on.
4610 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4611 spa_strfree(oldvd->vdev_path);
4612 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4614 (void) sprintf(oldvd->vdev_path, "%s/%s",
4615 newvd->vdev_path, "old");
4616 if (oldvd->vdev_devid != NULL) {
4617 spa_strfree(oldvd->vdev_devid);
4618 oldvd->vdev_devid = NULL;
4622 /* mark the device being resilvered */
4623 newvd->vdev_resilvering = B_TRUE;
4626 * If the parent is not a mirror, or if we're replacing, insert the new
4627 * mirror/replacing/spare vdev above oldvd.
4629 if (pvd->vdev_ops != pvops)
4630 pvd = vdev_add_parent(oldvd, pvops);
4632 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4633 ASSERT(pvd->vdev_ops == pvops);
4634 ASSERT(oldvd->vdev_parent == pvd);
4637 * Extract the new device from its root and add it to pvd.
4639 vdev_remove_child(newrootvd, newvd);
4640 newvd->vdev_id = pvd->vdev_children;
4641 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4642 vdev_add_child(pvd, newvd);
4644 tvd = newvd->vdev_top;
4645 ASSERT(pvd->vdev_top == tvd);
4646 ASSERT(tvd->vdev_parent == rvd);
4648 vdev_config_dirty(tvd);
4651 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4652 * for any dmu_sync-ed blocks. It will propagate upward when
4653 * spa_vdev_exit() calls vdev_dtl_reassess().
4655 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4657 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4658 dtl_max_txg - TXG_INITIAL);
4660 if (newvd->vdev_isspare) {
4661 spa_spare_activate(newvd);
4662 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4665 oldvdpath = spa_strdup(oldvd->vdev_path);
4666 newvdpath = spa_strdup(newvd->vdev_path);
4667 newvd_isspare = newvd->vdev_isspare;
4670 * Mark newvd's DTL dirty in this txg.
4672 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4675 * Restart the resilver
4677 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4682 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4684 spa_history_log_internal(spa, "vdev attach", NULL,
4685 "%s vdev=%s %s vdev=%s",
4686 replacing && newvd_isspare ? "spare in" :
4687 replacing ? "replace" : "attach", newvdpath,
4688 replacing ? "for" : "to", oldvdpath);
4690 spa_strfree(oldvdpath);
4691 spa_strfree(newvdpath);
4693 if (spa->spa_bootfs)
4694 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4700 * Detach a device from a mirror or replacing vdev.
4701 * If 'replace_done' is specified, only detach if the parent
4702 * is a replacing vdev.
4705 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4709 vdev_t *rvd = spa->spa_root_vdev;
4710 vdev_t *vd, *pvd, *cvd, *tvd;
4711 boolean_t unspare = B_FALSE;
4712 uint64_t unspare_guid = 0;
4715 ASSERT(spa_writeable(spa));
4717 txg = spa_vdev_enter(spa);
4719 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4722 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4724 if (!vd->vdev_ops->vdev_op_leaf)
4725 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4727 pvd = vd->vdev_parent;
4730 * If the parent/child relationship is not as expected, don't do it.
4731 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4732 * vdev that's replacing B with C. The user's intent in replacing
4733 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4734 * the replace by detaching C, the expected behavior is to end up
4735 * M(A,B). But suppose that right after deciding to detach C,
4736 * the replacement of B completes. We would have M(A,C), and then
4737 * ask to detach C, which would leave us with just A -- not what
4738 * the user wanted. To prevent this, we make sure that the
4739 * parent/child relationship hasn't changed -- in this example,
4740 * that C's parent is still the replacing vdev R.
4742 if (pvd->vdev_guid != pguid && pguid != 0)
4743 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4746 * Only 'replacing' or 'spare' vdevs can be replaced.
4748 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4749 pvd->vdev_ops != &vdev_spare_ops)
4750 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4752 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4753 spa_version(spa) >= SPA_VERSION_SPARES);
4756 * Only mirror, replacing, and spare vdevs support detach.
4758 if (pvd->vdev_ops != &vdev_replacing_ops &&
4759 pvd->vdev_ops != &vdev_mirror_ops &&
4760 pvd->vdev_ops != &vdev_spare_ops)
4761 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4764 * If this device has the only valid copy of some data,
4765 * we cannot safely detach it.
4767 if (vdev_dtl_required(vd))
4768 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4770 ASSERT(pvd->vdev_children >= 2);
4773 * If we are detaching the second disk from a replacing vdev, then
4774 * check to see if we changed the original vdev's path to have "/old"
4775 * at the end in spa_vdev_attach(). If so, undo that change now.
4777 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4778 vd->vdev_path != NULL) {
4779 size_t len = strlen(vd->vdev_path);
4781 for (int c = 0; c < pvd->vdev_children; c++) {
4782 cvd = pvd->vdev_child[c];
4784 if (cvd == vd || cvd->vdev_path == NULL)
4787 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4788 strcmp(cvd->vdev_path + len, "/old") == 0) {
4789 spa_strfree(cvd->vdev_path);
4790 cvd->vdev_path = spa_strdup(vd->vdev_path);
4797 * If we are detaching the original disk from a spare, then it implies
4798 * that the spare should become a real disk, and be removed from the
4799 * active spare list for the pool.
4801 if (pvd->vdev_ops == &vdev_spare_ops &&
4803 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4807 * Erase the disk labels so the disk can be used for other things.
4808 * This must be done after all other error cases are handled,
4809 * but before we disembowel vd (so we can still do I/O to it).
4810 * But if we can't do it, don't treat the error as fatal --
4811 * it may be that the unwritability of the disk is the reason
4812 * it's being detached!
4814 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4817 * Remove vd from its parent and compact the parent's children.
4819 vdev_remove_child(pvd, vd);
4820 vdev_compact_children(pvd);
4823 * Remember one of the remaining children so we can get tvd below.
4825 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4828 * If we need to remove the remaining child from the list of hot spares,
4829 * do it now, marking the vdev as no longer a spare in the process.
4830 * We must do this before vdev_remove_parent(), because that can
4831 * change the GUID if it creates a new toplevel GUID. For a similar
4832 * reason, we must remove the spare now, in the same txg as the detach;
4833 * otherwise someone could attach a new sibling, change the GUID, and
4834 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4837 ASSERT(cvd->vdev_isspare);
4838 spa_spare_remove(cvd);
4839 unspare_guid = cvd->vdev_guid;
4840 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4841 cvd->vdev_unspare = B_TRUE;
4845 * If the parent mirror/replacing vdev only has one child,
4846 * the parent is no longer needed. Remove it from the tree.
4848 if (pvd->vdev_children == 1) {
4849 if (pvd->vdev_ops == &vdev_spare_ops)
4850 cvd->vdev_unspare = B_FALSE;
4851 vdev_remove_parent(cvd);
4852 cvd->vdev_resilvering = B_FALSE;
4857 * We don't set tvd until now because the parent we just removed
4858 * may have been the previous top-level vdev.
4860 tvd = cvd->vdev_top;
4861 ASSERT(tvd->vdev_parent == rvd);
4864 * Reevaluate the parent vdev state.
4866 vdev_propagate_state(cvd);
4869 * If the 'autoexpand' property is set on the pool then automatically
4870 * try to expand the size of the pool. For example if the device we
4871 * just detached was smaller than the others, it may be possible to
4872 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4873 * first so that we can obtain the updated sizes of the leaf vdevs.
4875 if (spa->spa_autoexpand) {
4877 vdev_expand(tvd, txg);
4880 vdev_config_dirty(tvd);
4883 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4884 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4885 * But first make sure we're not on any *other* txg's DTL list, to
4886 * prevent vd from being accessed after it's freed.
4888 vdpath = spa_strdup(vd->vdev_path);
4889 for (int t = 0; t < TXG_SIZE; t++)
4890 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4891 vd->vdev_detached = B_TRUE;
4892 vdev_dirty(tvd, VDD_DTL, vd, txg);
4894 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4896 /* hang on to the spa before we release the lock */
4897 spa_open_ref(spa, FTAG);
4899 error = spa_vdev_exit(spa, vd, txg, 0);
4901 spa_history_log_internal(spa, "detach", NULL,
4903 spa_strfree(vdpath);
4906 * If this was the removal of the original device in a hot spare vdev,
4907 * then we want to go through and remove the device from the hot spare
4908 * list of every other pool.
4911 spa_t *altspa = NULL;
4913 mutex_enter(&spa_namespace_lock);
4914 while ((altspa = spa_next(altspa)) != NULL) {
4915 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4919 spa_open_ref(altspa, FTAG);
4920 mutex_exit(&spa_namespace_lock);
4921 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4922 mutex_enter(&spa_namespace_lock);
4923 spa_close(altspa, FTAG);
4925 mutex_exit(&spa_namespace_lock);
4927 /* search the rest of the vdevs for spares to remove */
4928 spa_vdev_resilver_done(spa);
4931 /* all done with the spa; OK to release */
4932 mutex_enter(&spa_namespace_lock);
4933 spa_close(spa, FTAG);
4934 mutex_exit(&spa_namespace_lock);
4940 * Split a set of devices from their mirrors, and create a new pool from them.
4943 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4944 nvlist_t *props, boolean_t exp)
4947 uint64_t txg, *glist;
4949 uint_t c, children, lastlog;
4950 nvlist_t **child, *nvl, *tmp;
4952 char *altroot = NULL;
4953 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4954 boolean_t activate_slog;
4956 ASSERT(spa_writeable(spa));
4958 txg = spa_vdev_enter(spa);
4960 /* clear the log and flush everything up to now */
4961 activate_slog = spa_passivate_log(spa);
4962 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4963 error = spa_offline_log(spa);
4964 txg = spa_vdev_config_enter(spa);
4967 spa_activate_log(spa);
4970 return (spa_vdev_exit(spa, NULL, txg, error));
4972 /* check new spa name before going any further */
4973 if (spa_lookup(newname) != NULL)
4974 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4977 * scan through all the children to ensure they're all mirrors
4979 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4980 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4982 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4984 /* first, check to ensure we've got the right child count */
4985 rvd = spa->spa_root_vdev;
4987 for (c = 0; c < rvd->vdev_children; c++) {
4988 vdev_t *vd = rvd->vdev_child[c];
4990 /* don't count the holes & logs as children */
4991 if (vd->vdev_islog || vd->vdev_ishole) {
4999 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5000 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5002 /* next, ensure no spare or cache devices are part of the split */
5003 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5004 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5005 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5007 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5008 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5010 /* then, loop over each vdev and validate it */
5011 for (c = 0; c < children; c++) {
5012 uint64_t is_hole = 0;
5014 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5018 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5019 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5022 error = SET_ERROR(EINVAL);
5027 /* which disk is going to be split? */
5028 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5030 error = SET_ERROR(EINVAL);
5034 /* look it up in the spa */
5035 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5036 if (vml[c] == NULL) {
5037 error = SET_ERROR(ENODEV);
5041 /* make sure there's nothing stopping the split */
5042 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5043 vml[c]->vdev_islog ||
5044 vml[c]->vdev_ishole ||
5045 vml[c]->vdev_isspare ||
5046 vml[c]->vdev_isl2cache ||
5047 !vdev_writeable(vml[c]) ||
5048 vml[c]->vdev_children != 0 ||
5049 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5050 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5051 error = SET_ERROR(EINVAL);
5055 if (vdev_dtl_required(vml[c])) {
5056 error = SET_ERROR(EBUSY);
5060 /* we need certain info from the top level */
5061 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5062 vml[c]->vdev_top->vdev_ms_array) == 0);
5063 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5064 vml[c]->vdev_top->vdev_ms_shift) == 0);
5065 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5066 vml[c]->vdev_top->vdev_asize) == 0);
5067 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5068 vml[c]->vdev_top->vdev_ashift) == 0);
5072 kmem_free(vml, children * sizeof (vdev_t *));
5073 kmem_free(glist, children * sizeof (uint64_t));
5074 return (spa_vdev_exit(spa, NULL, txg, error));
5077 /* stop writers from using the disks */
5078 for (c = 0; c < children; c++) {
5080 vml[c]->vdev_offline = B_TRUE;
5082 vdev_reopen(spa->spa_root_vdev);
5085 * Temporarily record the splitting vdevs in the spa config. This
5086 * will disappear once the config is regenerated.
5088 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5089 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5090 glist, children) == 0);
5091 kmem_free(glist, children * sizeof (uint64_t));
5093 mutex_enter(&spa->spa_props_lock);
5094 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5096 mutex_exit(&spa->spa_props_lock);
5097 spa->spa_config_splitting = nvl;
5098 vdev_config_dirty(spa->spa_root_vdev);
5100 /* configure and create the new pool */
5101 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5102 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5103 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5104 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5105 spa_version(spa)) == 0);
5106 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5107 spa->spa_config_txg) == 0);
5108 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5109 spa_generate_guid(NULL)) == 0);
5110 (void) nvlist_lookup_string(props,
5111 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5113 /* add the new pool to the namespace */
5114 newspa = spa_add(newname, config, altroot);
5115 newspa->spa_config_txg = spa->spa_config_txg;
5116 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5118 /* release the spa config lock, retaining the namespace lock */
5119 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5121 if (zio_injection_enabled)
5122 zio_handle_panic_injection(spa, FTAG, 1);
5124 spa_activate(newspa, spa_mode_global);
5125 spa_async_suspend(newspa);
5128 /* mark that we are creating new spa by splitting */
5129 newspa->spa_splitting_newspa = B_TRUE;
5131 /* create the new pool from the disks of the original pool */
5132 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5134 newspa->spa_splitting_newspa = B_FALSE;
5139 /* if that worked, generate a real config for the new pool */
5140 if (newspa->spa_root_vdev != NULL) {
5141 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5142 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5143 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5144 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5145 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5150 if (props != NULL) {
5151 spa_configfile_set(newspa, props, B_FALSE);
5152 error = spa_prop_set(newspa, props);
5157 /* flush everything */
5158 txg = spa_vdev_config_enter(newspa);
5159 vdev_config_dirty(newspa->spa_root_vdev);
5160 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5162 if (zio_injection_enabled)
5163 zio_handle_panic_injection(spa, FTAG, 2);
5165 spa_async_resume(newspa);
5167 /* finally, update the original pool's config */
5168 txg = spa_vdev_config_enter(spa);
5169 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5170 error = dmu_tx_assign(tx, TXG_WAIT);
5173 for (c = 0; c < children; c++) {
5174 if (vml[c] != NULL) {
5177 spa_history_log_internal(spa, "detach", tx,
5178 "vdev=%s", vml[c]->vdev_path);
5182 vdev_config_dirty(spa->spa_root_vdev);
5183 spa->spa_config_splitting = NULL;
5187 (void) spa_vdev_exit(spa, NULL, txg, 0);
5189 if (zio_injection_enabled)
5190 zio_handle_panic_injection(spa, FTAG, 3);
5192 /* split is complete; log a history record */
5193 spa_history_log_internal(newspa, "split", NULL,
5194 "from pool %s", spa_name(spa));
5196 kmem_free(vml, children * sizeof (vdev_t *));
5198 /* if we're not going to mount the filesystems in userland, export */
5200 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5207 spa_deactivate(newspa);
5210 txg = spa_vdev_config_enter(spa);
5212 /* re-online all offlined disks */
5213 for (c = 0; c < children; c++) {
5215 vml[c]->vdev_offline = B_FALSE;
5217 vdev_reopen(spa->spa_root_vdev);
5219 nvlist_free(spa->spa_config_splitting);
5220 spa->spa_config_splitting = NULL;
5221 (void) spa_vdev_exit(spa, NULL, txg, error);
5223 kmem_free(vml, children * sizeof (vdev_t *));
5228 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5230 for (int i = 0; i < count; i++) {
5233 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5236 if (guid == target_guid)
5244 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5245 nvlist_t *dev_to_remove)
5247 nvlist_t **newdev = NULL;
5250 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5252 for (int i = 0, j = 0; i < count; i++) {
5253 if (dev[i] == dev_to_remove)
5255 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5258 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5259 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5261 for (int i = 0; i < count - 1; i++)
5262 nvlist_free(newdev[i]);
5265 kmem_free(newdev, (count - 1) * sizeof (void *));
5269 * Evacuate the device.
5272 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5277 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5278 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5279 ASSERT(vd == vd->vdev_top);
5282 * Evacuate the device. We don't hold the config lock as writer
5283 * since we need to do I/O but we do keep the
5284 * spa_namespace_lock held. Once this completes the device
5285 * should no longer have any blocks allocated on it.
5287 if (vd->vdev_islog) {
5288 if (vd->vdev_stat.vs_alloc != 0)
5289 error = spa_offline_log(spa);
5291 error = SET_ERROR(ENOTSUP);
5298 * The evacuation succeeded. Remove any remaining MOS metadata
5299 * associated with this vdev, and wait for these changes to sync.
5301 ASSERT0(vd->vdev_stat.vs_alloc);
5302 txg = spa_vdev_config_enter(spa);
5303 vd->vdev_removing = B_TRUE;
5304 vdev_dirty(vd, 0, NULL, txg);
5305 vdev_config_dirty(vd);
5306 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5312 * Complete the removal by cleaning up the namespace.
5315 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5317 vdev_t *rvd = spa->spa_root_vdev;
5318 uint64_t id = vd->vdev_id;
5319 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5321 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5322 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5323 ASSERT(vd == vd->vdev_top);
5326 * Only remove any devices which are empty.
5328 if (vd->vdev_stat.vs_alloc != 0)
5331 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5333 if (list_link_active(&vd->vdev_state_dirty_node))
5334 vdev_state_clean(vd);
5335 if (list_link_active(&vd->vdev_config_dirty_node))
5336 vdev_config_clean(vd);
5341 vdev_compact_children(rvd);
5343 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5344 vdev_add_child(rvd, vd);
5346 vdev_config_dirty(rvd);
5349 * Reassess the health of our root vdev.
5355 * Remove a device from the pool -
5357 * Removing a device from the vdev namespace requires several steps
5358 * and can take a significant amount of time. As a result we use
5359 * the spa_vdev_config_[enter/exit] functions which allow us to
5360 * grab and release the spa_config_lock while still holding the namespace
5361 * lock. During each step the configuration is synced out.
5365 * Remove a device from the pool. Currently, this supports removing only hot
5366 * spares, slogs, and level 2 ARC devices.
5369 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5372 metaslab_group_t *mg;
5373 nvlist_t **spares, **l2cache, *nv;
5375 uint_t nspares, nl2cache;
5377 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5379 ASSERT(spa_writeable(spa));
5382 txg = spa_vdev_enter(spa);
5384 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5386 if (spa->spa_spares.sav_vdevs != NULL &&
5387 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5388 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5389 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5391 * Only remove the hot spare if it's not currently in use
5394 if (vd == NULL || unspare) {
5395 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5396 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5397 spa_load_spares(spa);
5398 spa->spa_spares.sav_sync = B_TRUE;
5400 error = SET_ERROR(EBUSY);
5402 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5403 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5404 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5405 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5407 * Cache devices can always be removed.
5409 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5410 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5411 spa_load_l2cache(spa);
5412 spa->spa_l2cache.sav_sync = B_TRUE;
5413 } else if (vd != NULL && vd->vdev_islog) {
5415 ASSERT(vd == vd->vdev_top);
5418 * XXX - Once we have bp-rewrite this should
5419 * become the common case.
5425 * Stop allocating from this vdev.
5427 metaslab_group_passivate(mg);
5430 * Wait for the youngest allocations and frees to sync,
5431 * and then wait for the deferral of those frees to finish.
5433 spa_vdev_config_exit(spa, NULL,
5434 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5437 * Attempt to evacuate the vdev.
5439 error = spa_vdev_remove_evacuate(spa, vd);
5441 txg = spa_vdev_config_enter(spa);
5444 * If we couldn't evacuate the vdev, unwind.
5447 metaslab_group_activate(mg);
5448 return (spa_vdev_exit(spa, NULL, txg, error));
5452 * Clean up the vdev namespace.
5454 spa_vdev_remove_from_namespace(spa, vd);
5456 } else if (vd != NULL) {
5458 * Normal vdevs cannot be removed (yet).
5460 error = SET_ERROR(ENOTSUP);
5463 * There is no vdev of any kind with the specified guid.
5465 error = SET_ERROR(ENOENT);
5469 return (spa_vdev_exit(spa, NULL, txg, error));
5475 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5476 * current spared, so we can detach it.
5479 spa_vdev_resilver_done_hunt(vdev_t *vd)
5481 vdev_t *newvd, *oldvd;
5483 for (int c = 0; c < vd->vdev_children; c++) {
5484 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5490 * Check for a completed replacement. We always consider the first
5491 * vdev in the list to be the oldest vdev, and the last one to be
5492 * the newest (see spa_vdev_attach() for how that works). In
5493 * the case where the newest vdev is faulted, we will not automatically
5494 * remove it after a resilver completes. This is OK as it will require
5495 * user intervention to determine which disk the admin wishes to keep.
5497 if (vd->vdev_ops == &vdev_replacing_ops) {
5498 ASSERT(vd->vdev_children > 1);
5500 newvd = vd->vdev_child[vd->vdev_children - 1];
5501 oldvd = vd->vdev_child[0];
5503 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5504 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5505 !vdev_dtl_required(oldvd))
5510 * Check for a completed resilver with the 'unspare' flag set.
5512 if (vd->vdev_ops == &vdev_spare_ops) {
5513 vdev_t *first = vd->vdev_child[0];
5514 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5516 if (last->vdev_unspare) {
5519 } else if (first->vdev_unspare) {
5526 if (oldvd != NULL &&
5527 vdev_dtl_empty(newvd, DTL_MISSING) &&
5528 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5529 !vdev_dtl_required(oldvd))
5533 * If there are more than two spares attached to a disk,
5534 * and those spares are not required, then we want to
5535 * attempt to free them up now so that they can be used
5536 * by other pools. Once we're back down to a single
5537 * disk+spare, we stop removing them.
5539 if (vd->vdev_children > 2) {
5540 newvd = vd->vdev_child[1];
5542 if (newvd->vdev_isspare && last->vdev_isspare &&
5543 vdev_dtl_empty(last, DTL_MISSING) &&
5544 vdev_dtl_empty(last, DTL_OUTAGE) &&
5545 !vdev_dtl_required(newvd))
5554 spa_vdev_resilver_done(spa_t *spa)
5556 vdev_t *vd, *pvd, *ppvd;
5557 uint64_t guid, sguid, pguid, ppguid;
5559 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5561 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5562 pvd = vd->vdev_parent;
5563 ppvd = pvd->vdev_parent;
5564 guid = vd->vdev_guid;
5565 pguid = pvd->vdev_guid;
5566 ppguid = ppvd->vdev_guid;
5569 * If we have just finished replacing a hot spared device, then
5570 * we need to detach the parent's first child (the original hot
5573 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5574 ppvd->vdev_children == 2) {
5575 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5576 sguid = ppvd->vdev_child[1]->vdev_guid;
5578 spa_config_exit(spa, SCL_ALL, FTAG);
5579 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5581 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5583 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5586 spa_config_exit(spa, SCL_ALL, FTAG);
5590 * Update the stored path or FRU for this vdev.
5593 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5597 boolean_t sync = B_FALSE;
5599 ASSERT(spa_writeable(spa));
5601 spa_vdev_state_enter(spa, SCL_ALL);
5603 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5604 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5606 if (!vd->vdev_ops->vdev_op_leaf)
5607 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5610 if (strcmp(value, vd->vdev_path) != 0) {
5611 spa_strfree(vd->vdev_path);
5612 vd->vdev_path = spa_strdup(value);
5616 if (vd->vdev_fru == NULL) {
5617 vd->vdev_fru = spa_strdup(value);
5619 } else if (strcmp(value, vd->vdev_fru) != 0) {
5620 spa_strfree(vd->vdev_fru);
5621 vd->vdev_fru = spa_strdup(value);
5626 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5630 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5632 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5636 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5638 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5642 * ==========================================================================
5644 * ==========================================================================
5648 spa_scan_stop(spa_t *spa)
5650 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5651 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5652 return (SET_ERROR(EBUSY));
5653 return (dsl_scan_cancel(spa->spa_dsl_pool));
5657 spa_scan(spa_t *spa, pool_scan_func_t func)
5659 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5661 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5662 return (SET_ERROR(ENOTSUP));
5665 * If a resilver was requested, but there is no DTL on a
5666 * writeable leaf device, we have nothing to do.
5668 if (func == POOL_SCAN_RESILVER &&
5669 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5670 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5674 return (dsl_scan(spa->spa_dsl_pool, func));
5678 * ==========================================================================
5679 * SPA async task processing
5680 * ==========================================================================
5684 spa_async_remove(spa_t *spa, vdev_t *vd)
5686 if (vd->vdev_remove_wanted) {
5687 vd->vdev_remove_wanted = B_FALSE;
5688 vd->vdev_delayed_close = B_FALSE;
5689 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5692 * We want to clear the stats, but we don't want to do a full
5693 * vdev_clear() as that will cause us to throw away
5694 * degraded/faulted state as well as attempt to reopen the
5695 * device, all of which is a waste.
5697 vd->vdev_stat.vs_read_errors = 0;
5698 vd->vdev_stat.vs_write_errors = 0;
5699 vd->vdev_stat.vs_checksum_errors = 0;
5701 vdev_state_dirty(vd->vdev_top);
5704 for (int c = 0; c < vd->vdev_children; c++)
5705 spa_async_remove(spa, vd->vdev_child[c]);
5709 spa_async_probe(spa_t *spa, vdev_t *vd)
5711 if (vd->vdev_probe_wanted) {
5712 vd->vdev_probe_wanted = B_FALSE;
5713 vdev_reopen(vd); /* vdev_open() does the actual probe */
5716 for (int c = 0; c < vd->vdev_children; c++)
5717 spa_async_probe(spa, vd->vdev_child[c]);
5721 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5727 if (!spa->spa_autoexpand)
5730 for (int c = 0; c < vd->vdev_children; c++) {
5731 vdev_t *cvd = vd->vdev_child[c];
5732 spa_async_autoexpand(spa, cvd);
5735 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5738 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5739 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5741 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5742 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5744 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5745 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5748 kmem_free(physpath, MAXPATHLEN);
5752 spa_async_thread(void *arg)
5757 ASSERT(spa->spa_sync_on);
5759 mutex_enter(&spa->spa_async_lock);
5760 tasks = spa->spa_async_tasks;
5761 spa->spa_async_tasks = 0;
5762 mutex_exit(&spa->spa_async_lock);
5765 * See if the config needs to be updated.
5767 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5768 uint64_t old_space, new_space;
5770 mutex_enter(&spa_namespace_lock);
5771 old_space = metaslab_class_get_space(spa_normal_class(spa));
5772 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5773 new_space = metaslab_class_get_space(spa_normal_class(spa));
5774 mutex_exit(&spa_namespace_lock);
5777 * If the pool grew as a result of the config update,
5778 * then log an internal history event.
5780 if (new_space != old_space) {
5781 spa_history_log_internal(spa, "vdev online", NULL,
5782 "pool '%s' size: %llu(+%llu)",
5783 spa_name(spa), new_space, new_space - old_space);
5788 * See if any devices need to be marked REMOVED.
5790 if (tasks & SPA_ASYNC_REMOVE) {
5791 spa_vdev_state_enter(spa, SCL_NONE);
5792 spa_async_remove(spa, spa->spa_root_vdev);
5793 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5794 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5795 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5796 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5797 (void) spa_vdev_state_exit(spa, NULL, 0);
5800 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5801 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5802 spa_async_autoexpand(spa, spa->spa_root_vdev);
5803 spa_config_exit(spa, SCL_CONFIG, FTAG);
5807 * See if any devices need to be probed.
5809 if (tasks & SPA_ASYNC_PROBE) {
5810 spa_vdev_state_enter(spa, SCL_NONE);
5811 spa_async_probe(spa, spa->spa_root_vdev);
5812 (void) spa_vdev_state_exit(spa, NULL, 0);
5816 * If any devices are done replacing, detach them.
5818 if (tasks & SPA_ASYNC_RESILVER_DONE)
5819 spa_vdev_resilver_done(spa);
5822 * Kick off a resilver.
5824 if (tasks & SPA_ASYNC_RESILVER)
5825 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5828 * Let the world know that we're done.
5830 mutex_enter(&spa->spa_async_lock);
5831 spa->spa_async_thread = NULL;
5832 cv_broadcast(&spa->spa_async_cv);
5833 mutex_exit(&spa->spa_async_lock);
5838 spa_async_suspend(spa_t *spa)
5840 mutex_enter(&spa->spa_async_lock);
5841 spa->spa_async_suspended++;
5842 while (spa->spa_async_thread != NULL)
5843 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5844 mutex_exit(&spa->spa_async_lock);
5848 spa_async_resume(spa_t *spa)
5850 mutex_enter(&spa->spa_async_lock);
5851 ASSERT(spa->spa_async_suspended != 0);
5852 spa->spa_async_suspended--;
5853 mutex_exit(&spa->spa_async_lock);
5857 spa_async_dispatch(spa_t *spa)
5859 mutex_enter(&spa->spa_async_lock);
5860 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5861 spa->spa_async_thread == NULL &&
5862 rootdir != NULL && !vn_is_readonly(rootdir))
5863 spa->spa_async_thread = thread_create(NULL, 0,
5864 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5865 mutex_exit(&spa->spa_async_lock);
5869 spa_async_request(spa_t *spa, int task)
5871 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5872 mutex_enter(&spa->spa_async_lock);
5873 spa->spa_async_tasks |= task;
5874 mutex_exit(&spa->spa_async_lock);
5878 * ==========================================================================
5879 * SPA syncing routines
5880 * ==========================================================================
5884 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5887 bpobj_enqueue(bpo, bp, tx);
5892 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5896 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5897 BP_GET_PSIZE(bp), zio->io_flags));
5902 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5904 char *packed = NULL;
5909 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5912 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5913 * information. This avoids the dbuf_will_dirty() path and
5914 * saves us a pre-read to get data we don't actually care about.
5916 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
5917 packed = kmem_alloc(bufsize, KM_SLEEP);
5919 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5921 bzero(packed + nvsize, bufsize - nvsize);
5923 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5925 kmem_free(packed, bufsize);
5927 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5928 dmu_buf_will_dirty(db, tx);
5929 *(uint64_t *)db->db_data = nvsize;
5930 dmu_buf_rele(db, FTAG);
5934 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5935 const char *config, const char *entry)
5945 * Update the MOS nvlist describing the list of available devices.
5946 * spa_validate_aux() will have already made sure this nvlist is
5947 * valid and the vdevs are labeled appropriately.
5949 if (sav->sav_object == 0) {
5950 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5951 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5952 sizeof (uint64_t), tx);
5953 VERIFY(zap_update(spa->spa_meta_objset,
5954 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5955 &sav->sav_object, tx) == 0);
5958 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5959 if (sav->sav_count == 0) {
5960 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5962 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
5963 for (i = 0; i < sav->sav_count; i++)
5964 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5965 B_FALSE, VDEV_CONFIG_L2CACHE);
5966 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5967 sav->sav_count) == 0);
5968 for (i = 0; i < sav->sav_count; i++)
5969 nvlist_free(list[i]);
5970 kmem_free(list, sav->sav_count * sizeof (void *));
5973 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5974 nvlist_free(nvroot);
5976 sav->sav_sync = B_FALSE;
5980 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5984 if (list_is_empty(&spa->spa_config_dirty_list))
5987 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5989 config = spa_config_generate(spa, spa->spa_root_vdev,
5990 dmu_tx_get_txg(tx), B_FALSE);
5993 * If we're upgrading the spa version then make sure that
5994 * the config object gets updated with the correct version.
5996 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
5997 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5998 spa->spa_uberblock.ub_version);
6000 spa_config_exit(spa, SCL_STATE, FTAG);
6002 if (spa->spa_config_syncing)
6003 nvlist_free(spa->spa_config_syncing);
6004 spa->spa_config_syncing = config;
6006 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6010 spa_sync_version(void *arg, dmu_tx_t *tx)
6012 uint64_t *versionp = arg;
6013 uint64_t version = *versionp;
6014 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6017 * Setting the version is special cased when first creating the pool.
6019 ASSERT(tx->tx_txg != TXG_INITIAL);
6021 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6022 ASSERT(version >= spa_version(spa));
6024 spa->spa_uberblock.ub_version = version;
6025 vdev_config_dirty(spa->spa_root_vdev);
6026 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6030 * Set zpool properties.
6033 spa_sync_props(void *arg, dmu_tx_t *tx)
6035 nvlist_t *nvp = arg;
6036 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6037 objset_t *mos = spa->spa_meta_objset;
6038 nvpair_t *elem = NULL;
6040 mutex_enter(&spa->spa_props_lock);
6042 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6044 char *strval, *fname;
6046 const char *propname;
6047 zprop_type_t proptype;
6048 zfeature_info_t *feature;
6050 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6053 * We checked this earlier in spa_prop_validate().
6055 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6057 fname = strchr(nvpair_name(elem), '@') + 1;
6058 VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature));
6060 spa_feature_enable(spa, feature, tx);
6061 spa_history_log_internal(spa, "set", tx,
6062 "%s=enabled", nvpair_name(elem));
6065 case ZPOOL_PROP_VERSION:
6066 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
6068 * The version is synced seperatly before other
6069 * properties and should be correct by now.
6071 ASSERT3U(spa_version(spa), >=, intval);
6074 case ZPOOL_PROP_ALTROOT:
6076 * 'altroot' is a non-persistent property. It should
6077 * have been set temporarily at creation or import time.
6079 ASSERT(spa->spa_root != NULL);
6082 case ZPOOL_PROP_READONLY:
6083 case ZPOOL_PROP_CACHEFILE:
6085 * 'readonly' and 'cachefile' are also non-persisitent
6089 case ZPOOL_PROP_COMMENT:
6090 VERIFY(nvpair_value_string(elem, &strval) == 0);
6091 if (spa->spa_comment != NULL)
6092 spa_strfree(spa->spa_comment);
6093 spa->spa_comment = spa_strdup(strval);
6095 * We need to dirty the configuration on all the vdevs
6096 * so that their labels get updated. It's unnecessary
6097 * to do this for pool creation since the vdev's
6098 * configuratoin has already been dirtied.
6100 if (tx->tx_txg != TXG_INITIAL)
6101 vdev_config_dirty(spa->spa_root_vdev);
6102 spa_history_log_internal(spa, "set", tx,
6103 "%s=%s", nvpair_name(elem), strval);
6107 * Set pool property values in the poolprops mos object.
6109 if (spa->spa_pool_props_object == 0) {
6110 spa->spa_pool_props_object =
6111 zap_create_link(mos, DMU_OT_POOL_PROPS,
6112 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6116 /* normalize the property name */
6117 propname = zpool_prop_to_name(prop);
6118 proptype = zpool_prop_get_type(prop);
6120 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6121 ASSERT(proptype == PROP_TYPE_STRING);
6122 VERIFY(nvpair_value_string(elem, &strval) == 0);
6123 VERIFY(zap_update(mos,
6124 spa->spa_pool_props_object, propname,
6125 1, strlen(strval) + 1, strval, tx) == 0);
6126 spa_history_log_internal(spa, "set", tx,
6127 "%s=%s", nvpair_name(elem), strval);
6128 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6129 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
6131 if (proptype == PROP_TYPE_INDEX) {
6133 VERIFY(zpool_prop_index_to_string(
6134 prop, intval, &unused) == 0);
6136 VERIFY(zap_update(mos,
6137 spa->spa_pool_props_object, propname,
6138 8, 1, &intval, tx) == 0);
6139 spa_history_log_internal(spa, "set", tx,
6140 "%s=%lld", nvpair_name(elem), intval);
6142 ASSERT(0); /* not allowed */
6146 case ZPOOL_PROP_DELEGATION:
6147 spa->spa_delegation = intval;
6149 case ZPOOL_PROP_BOOTFS:
6150 spa->spa_bootfs = intval;
6152 case ZPOOL_PROP_FAILUREMODE:
6153 spa->spa_failmode = intval;
6155 case ZPOOL_PROP_AUTOEXPAND:
6156 spa->spa_autoexpand = intval;
6157 if (tx->tx_txg != TXG_INITIAL)
6158 spa_async_request(spa,
6159 SPA_ASYNC_AUTOEXPAND);
6161 case ZPOOL_PROP_DEDUPDITTO:
6162 spa->spa_dedup_ditto = intval;
6171 mutex_exit(&spa->spa_props_lock);
6175 * Perform one-time upgrade on-disk changes. spa_version() does not
6176 * reflect the new version this txg, so there must be no changes this
6177 * txg to anything that the upgrade code depends on after it executes.
6178 * Therefore this must be called after dsl_pool_sync() does the sync
6182 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6184 dsl_pool_t *dp = spa->spa_dsl_pool;
6186 ASSERT(spa->spa_sync_pass == 1);
6188 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6190 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6191 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6192 dsl_pool_create_origin(dp, tx);
6194 /* Keeping the origin open increases spa_minref */
6195 spa->spa_minref += 3;
6198 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6199 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6200 dsl_pool_upgrade_clones(dp, tx);
6203 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6204 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6205 dsl_pool_upgrade_dir_clones(dp, tx);
6207 /* Keeping the freedir open increases spa_minref */
6208 spa->spa_minref += 3;
6211 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6212 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6213 spa_feature_create_zap_objects(spa, tx);
6215 rrw_exit(&dp->dp_config_rwlock, FTAG);
6219 * Sync the specified transaction group. New blocks may be dirtied as
6220 * part of the process, so we iterate until it converges.
6223 spa_sync(spa_t *spa, uint64_t txg)
6225 dsl_pool_t *dp = spa->spa_dsl_pool;
6226 objset_t *mos = spa->spa_meta_objset;
6227 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
6228 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6229 vdev_t *rvd = spa->spa_root_vdev;
6234 VERIFY(spa_writeable(spa));
6237 * Lock out configuration changes.
6239 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6241 spa->spa_syncing_txg = txg;
6242 spa->spa_sync_pass = 0;
6245 * If there are any pending vdev state changes, convert them
6246 * into config changes that go out with this transaction group.
6248 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6249 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6251 * We need the write lock here because, for aux vdevs,
6252 * calling vdev_config_dirty() modifies sav_config.
6253 * This is ugly and will become unnecessary when we
6254 * eliminate the aux vdev wart by integrating all vdevs
6255 * into the root vdev tree.
6257 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6258 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6259 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6260 vdev_state_clean(vd);
6261 vdev_config_dirty(vd);
6263 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6264 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6266 spa_config_exit(spa, SCL_STATE, FTAG);
6268 tx = dmu_tx_create_assigned(dp, txg);
6270 spa->spa_sync_starttime = gethrtime();
6272 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6273 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6276 callout_reset(&spa->spa_deadman_cycid,
6277 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6282 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6283 * set spa_deflate if we have no raid-z vdevs.
6285 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6286 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6289 for (i = 0; i < rvd->vdev_children; i++) {
6290 vd = rvd->vdev_child[i];
6291 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6294 if (i == rvd->vdev_children) {
6295 spa->spa_deflate = TRUE;
6296 VERIFY(0 == zap_add(spa->spa_meta_objset,
6297 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6298 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6303 * If anything has changed in this txg, or if someone is waiting
6304 * for this txg to sync (eg, spa_vdev_remove()), push the
6305 * deferred frees from the previous txg. If not, leave them
6306 * alone so that we don't generate work on an otherwise idle
6309 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6310 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6311 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6312 ((dsl_scan_active(dp->dp_scan) ||
6313 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6314 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6315 VERIFY3U(bpobj_iterate(defer_bpo,
6316 spa_free_sync_cb, zio, tx), ==, 0);
6317 VERIFY0(zio_wait(zio));
6321 * Iterate to convergence.
6324 int pass = ++spa->spa_sync_pass;
6326 spa_sync_config_object(spa, tx);
6327 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6328 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6329 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6330 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6331 spa_errlog_sync(spa, txg);
6332 dsl_pool_sync(dp, txg);
6334 if (pass < zfs_sync_pass_deferred_free) {
6335 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6336 bplist_iterate(free_bpl, spa_free_sync_cb,
6338 VERIFY(zio_wait(zio) == 0);
6340 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6345 dsl_scan_sync(dp, tx);
6347 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6351 spa_sync_upgrades(spa, tx);
6353 } while (dmu_objset_is_dirty(mos, txg));
6356 * Rewrite the vdev configuration (which includes the uberblock)
6357 * to commit the transaction group.
6359 * If there are no dirty vdevs, we sync the uberblock to a few
6360 * random top-level vdevs that are known to be visible in the
6361 * config cache (see spa_vdev_add() for a complete description).
6362 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6366 * We hold SCL_STATE to prevent vdev open/close/etc.
6367 * while we're attempting to write the vdev labels.
6369 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6371 if (list_is_empty(&spa->spa_config_dirty_list)) {
6372 vdev_t *svd[SPA_DVAS_PER_BP];
6374 int children = rvd->vdev_children;
6375 int c0 = spa_get_random(children);
6377 for (int c = 0; c < children; c++) {
6378 vd = rvd->vdev_child[(c0 + c) % children];
6379 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6381 svd[svdcount++] = vd;
6382 if (svdcount == SPA_DVAS_PER_BP)
6385 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6387 error = vdev_config_sync(svd, svdcount, txg,
6390 error = vdev_config_sync(rvd->vdev_child,
6391 rvd->vdev_children, txg, B_FALSE);
6393 error = vdev_config_sync(rvd->vdev_child,
6394 rvd->vdev_children, txg, B_TRUE);
6398 spa->spa_last_synced_guid = rvd->vdev_guid;
6400 spa_config_exit(spa, SCL_STATE, FTAG);
6404 zio_suspend(spa, NULL);
6405 zio_resume_wait(spa);
6410 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6413 callout_drain(&spa->spa_deadman_cycid);
6418 * Clear the dirty config list.
6420 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6421 vdev_config_clean(vd);
6424 * Now that the new config has synced transactionally,
6425 * let it become visible to the config cache.
6427 if (spa->spa_config_syncing != NULL) {
6428 spa_config_set(spa, spa->spa_config_syncing);
6429 spa->spa_config_txg = txg;
6430 spa->spa_config_syncing = NULL;
6433 spa->spa_ubsync = spa->spa_uberblock;
6435 dsl_pool_sync_done(dp, txg);
6438 * Update usable space statistics.
6440 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6441 vdev_sync_done(vd, txg);
6443 spa_update_dspace(spa);
6446 * It had better be the case that we didn't dirty anything
6447 * since vdev_config_sync().
6449 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6450 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6451 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6453 spa->spa_sync_pass = 0;
6455 spa_config_exit(spa, SCL_CONFIG, FTAG);
6457 spa_handle_ignored_writes(spa);
6460 * If any async tasks have been requested, kick them off.
6462 spa_async_dispatch(spa);
6466 * Sync all pools. We don't want to hold the namespace lock across these
6467 * operations, so we take a reference on the spa_t and drop the lock during the
6471 spa_sync_allpools(void)
6474 mutex_enter(&spa_namespace_lock);
6475 while ((spa = spa_next(spa)) != NULL) {
6476 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6477 !spa_writeable(spa) || spa_suspended(spa))
6479 spa_open_ref(spa, FTAG);
6480 mutex_exit(&spa_namespace_lock);
6481 txg_wait_synced(spa_get_dsl(spa), 0);
6482 mutex_enter(&spa_namespace_lock);
6483 spa_close(spa, FTAG);
6485 mutex_exit(&spa_namespace_lock);
6489 * ==========================================================================
6490 * Miscellaneous routines
6491 * ==========================================================================
6495 * Remove all pools in the system.
6503 * Remove all cached state. All pools should be closed now,
6504 * so every spa in the AVL tree should be unreferenced.
6506 mutex_enter(&spa_namespace_lock);
6507 while ((spa = spa_next(NULL)) != NULL) {
6509 * Stop async tasks. The async thread may need to detach
6510 * a device that's been replaced, which requires grabbing
6511 * spa_namespace_lock, so we must drop it here.
6513 spa_open_ref(spa, FTAG);
6514 mutex_exit(&spa_namespace_lock);
6515 spa_async_suspend(spa);
6516 mutex_enter(&spa_namespace_lock);
6517 spa_close(spa, FTAG);
6519 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6521 spa_deactivate(spa);
6525 mutex_exit(&spa_namespace_lock);
6529 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6534 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6538 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6539 vd = spa->spa_l2cache.sav_vdevs[i];
6540 if (vd->vdev_guid == guid)
6544 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6545 vd = spa->spa_spares.sav_vdevs[i];
6546 if (vd->vdev_guid == guid)
6555 spa_upgrade(spa_t *spa, uint64_t version)
6557 ASSERT(spa_writeable(spa));
6559 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6562 * This should only be called for a non-faulted pool, and since a
6563 * future version would result in an unopenable pool, this shouldn't be
6566 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6567 ASSERT(version >= spa->spa_uberblock.ub_version);
6569 spa->spa_uberblock.ub_version = version;
6570 vdev_config_dirty(spa->spa_root_vdev);
6572 spa_config_exit(spa, SCL_ALL, FTAG);
6574 txg_wait_synced(spa_get_dsl(spa), 0);
6578 spa_has_spare(spa_t *spa, uint64_t guid)
6582 spa_aux_vdev_t *sav = &spa->spa_spares;
6584 for (i = 0; i < sav->sav_count; i++)
6585 if (sav->sav_vdevs[i]->vdev_guid == guid)
6588 for (i = 0; i < sav->sav_npending; i++) {
6589 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6590 &spareguid) == 0 && spareguid == guid)
6598 * Check if a pool has an active shared spare device.
6599 * Note: reference count of an active spare is 2, as a spare and as a replace
6602 spa_has_active_shared_spare(spa_t *spa)
6606 spa_aux_vdev_t *sav = &spa->spa_spares;
6608 for (i = 0; i < sav->sav_count; i++) {
6609 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6610 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6619 * Post a sysevent corresponding to the given event. The 'name' must be one of
6620 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6621 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6622 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6623 * or zdb as real changes.
6626 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6630 sysevent_attr_list_t *attr = NULL;
6631 sysevent_value_t value;
6634 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6637 value.value_type = SE_DATA_TYPE_STRING;
6638 value.value.sv_string = spa_name(spa);
6639 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6642 value.value_type = SE_DATA_TYPE_UINT64;
6643 value.value.sv_uint64 = spa_guid(spa);
6644 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6648 value.value_type = SE_DATA_TYPE_UINT64;
6649 value.value.sv_uint64 = vd->vdev_guid;
6650 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6654 if (vd->vdev_path) {
6655 value.value_type = SE_DATA_TYPE_STRING;
6656 value.value.sv_string = vd->vdev_path;
6657 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6658 &value, SE_SLEEP) != 0)
6663 if (sysevent_attach_attributes(ev, attr) != 0)
6667 (void) log_sysevent(ev, SE_SLEEP, &eid);
6671 sysevent_free_attr(attr);