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 (c) 2015, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright 2013 Saso Kiselkov. All rights reserved.
32 * SPA: Storage Pool Allocator
34 * This file contains all the routines used when modifying on-disk SPA state.
35 * This includes opening, importing, destroying, exporting a pool, and syncing a
39 #include <sys/zfs_context.h>
40 #include <sys/fm/fs/zfs.h>
41 #include <sys/spa_impl.h>
43 #include <sys/zio_checksum.h>
45 #include <sys/dmu_tx.h>
49 #include <sys/vdev_impl.h>
50 #include <sys/metaslab.h>
51 #include <sys/metaslab_impl.h>
52 #include <sys/uberblock_impl.h>
55 #include <sys/dmu_traverse.h>
56 #include <sys/dmu_objset.h>
57 #include <sys/unique.h>
58 #include <sys/dsl_pool.h>
59 #include <sys/dsl_dataset.h>
60 #include <sys/dsl_dir.h>
61 #include <sys/dsl_prop.h>
62 #include <sys/dsl_synctask.h>
63 #include <sys/fs/zfs.h>
65 #include <sys/callb.h>
66 #include <sys/spa_boot.h>
67 #include <sys/zfs_ioctl.h>
68 #include <sys/dsl_scan.h>
69 #include <sys/dmu_send.h>
70 #include <sys/dsl_destroy.h>
71 #include <sys/dsl_userhold.h>
72 #include <sys/zfeature.h>
74 #include <sys/trim_map.h>
77 #include <sys/callb.h>
78 #include <sys/cpupart.h>
83 #include "zfs_comutil.h"
85 /* Check hostid on import? */
86 static int check_hostid = 1;
88 SYSCTL_DECL(_vfs_zfs);
89 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
90 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
91 "Check hostid on import?");
94 * The interval, in seconds, at which failed configuration cache file writes
97 static int zfs_ccw_retry_interval = 300;
99 typedef enum zti_modes {
100 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
101 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
102 ZTI_MODE_NULL, /* don't create a taskq */
106 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
107 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
108 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
110 #define ZTI_N(n) ZTI_P(n, 1)
111 #define ZTI_ONE ZTI_N(1)
113 typedef struct zio_taskq_info {
114 zti_modes_t zti_mode;
119 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
120 "issue", "issue_high", "intr", "intr_high"
124 * This table defines the taskq settings for each ZFS I/O type. When
125 * initializing a pool, we use this table to create an appropriately sized
126 * taskq. Some operations are low volume and therefore have a small, static
127 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
128 * macros. Other operations process a large amount of data; the ZTI_BATCH
129 * macro causes us to create a taskq oriented for throughput. Some operations
130 * are so high frequency and short-lived that the taskq itself can become a a
131 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
132 * additional degree of parallelism specified by the number of threads per-
133 * taskq and the number of taskqs; when dispatching an event in this case, the
134 * particular taskq is chosen at random.
136 * The different taskq priorities are to handle the different contexts (issue
137 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
138 * need to be handled with minimum delay.
140 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
141 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
142 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
143 { ZTI_N(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, /* READ */
144 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
145 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
146 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
147 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
150 static void spa_sync_version(void *arg, dmu_tx_t *tx);
151 static void spa_sync_props(void *arg, dmu_tx_t *tx);
152 static boolean_t spa_has_active_shared_spare(spa_t *spa);
153 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
154 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
156 static void spa_vdev_resilver_done(spa_t *spa);
158 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
160 id_t zio_taskq_psrset_bind = PS_NONE;
163 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
165 uint_t zio_taskq_basedc = 80; /* base duty cycle */
167 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
168 extern int zfs_sync_pass_deferred_free;
171 extern void spa_deadman(void *arg);
175 * This (illegal) pool name is used when temporarily importing a spa_t in order
176 * to get the vdev stats associated with the imported devices.
178 #define TRYIMPORT_NAME "$import"
181 * ==========================================================================
182 * SPA properties routines
183 * ==========================================================================
187 * Add a (source=src, propname=propval) list to an nvlist.
190 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
191 uint64_t intval, zprop_source_t src)
193 const char *propname = zpool_prop_to_name(prop);
196 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
197 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
200 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
202 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
204 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
205 nvlist_free(propval);
209 * Get property values from the spa configuration.
212 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
214 vdev_t *rvd = spa->spa_root_vdev;
215 dsl_pool_t *pool = spa->spa_dsl_pool;
216 uint64_t size, alloc, cap, version;
217 zprop_source_t src = ZPROP_SRC_NONE;
218 spa_config_dirent_t *dp;
219 metaslab_class_t *mc = spa_normal_class(spa);
221 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
224 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
225 size = metaslab_class_get_space(spa_normal_class(spa));
226 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
227 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
228 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
229 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
232 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
233 metaslab_class_fragmentation(mc), src);
234 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
235 metaslab_class_expandable_space(mc), src);
236 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
237 (spa_mode(spa) == FREAD), src);
239 cap = (size == 0) ? 0 : (alloc * 100 / size);
240 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
242 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
243 ddt_get_pool_dedup_ratio(spa), src);
245 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
246 rvd->vdev_state, src);
248 version = spa_version(spa);
249 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
250 src = ZPROP_SRC_DEFAULT;
252 src = ZPROP_SRC_LOCAL;
253 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
258 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
259 * when opening pools before this version freedir will be NULL.
261 if (pool->dp_free_dir != NULL) {
262 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
263 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
266 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
270 if (pool->dp_leak_dir != NULL) {
271 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
272 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
275 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
280 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
282 if (spa->spa_comment != NULL) {
283 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
287 if (spa->spa_root != NULL)
288 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
291 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
292 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
293 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
295 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
296 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
299 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
300 if (dp->scd_path == NULL) {
301 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
302 "none", 0, ZPROP_SRC_LOCAL);
303 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
304 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
305 dp->scd_path, 0, ZPROP_SRC_LOCAL);
311 * Get zpool property values.
314 spa_prop_get(spa_t *spa, nvlist_t **nvp)
316 objset_t *mos = spa->spa_meta_objset;
321 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
323 mutex_enter(&spa->spa_props_lock);
326 * Get properties from the spa config.
328 spa_prop_get_config(spa, nvp);
330 /* If no pool property object, no more prop to get. */
331 if (mos == NULL || spa->spa_pool_props_object == 0) {
332 mutex_exit(&spa->spa_props_lock);
337 * Get properties from the MOS pool property object.
339 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
340 (err = zap_cursor_retrieve(&zc, &za)) == 0;
341 zap_cursor_advance(&zc)) {
344 zprop_source_t src = ZPROP_SRC_DEFAULT;
347 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
350 switch (za.za_integer_length) {
352 /* integer property */
353 if (za.za_first_integer !=
354 zpool_prop_default_numeric(prop))
355 src = ZPROP_SRC_LOCAL;
357 if (prop == ZPOOL_PROP_BOOTFS) {
359 dsl_dataset_t *ds = NULL;
361 dp = spa_get_dsl(spa);
362 dsl_pool_config_enter(dp, FTAG);
363 if (err = dsl_dataset_hold_obj(dp,
364 za.za_first_integer, FTAG, &ds)) {
365 dsl_pool_config_exit(dp, FTAG);
370 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
372 dsl_dataset_name(ds, strval);
373 dsl_dataset_rele(ds, FTAG);
374 dsl_pool_config_exit(dp, FTAG);
377 intval = za.za_first_integer;
380 spa_prop_add_list(*nvp, prop, strval, intval, src);
384 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
389 /* string property */
390 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
391 err = zap_lookup(mos, spa->spa_pool_props_object,
392 za.za_name, 1, za.za_num_integers, strval);
394 kmem_free(strval, za.za_num_integers);
397 spa_prop_add_list(*nvp, prop, strval, 0, src);
398 kmem_free(strval, za.za_num_integers);
405 zap_cursor_fini(&zc);
406 mutex_exit(&spa->spa_props_lock);
408 if (err && err != ENOENT) {
418 * Validate the given pool properties nvlist and modify the list
419 * for the property values to be set.
422 spa_prop_validate(spa_t *spa, nvlist_t *props)
425 int error = 0, reset_bootfs = 0;
427 boolean_t has_feature = B_FALSE;
430 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
432 char *strval, *slash, *check, *fname;
433 const char *propname = nvpair_name(elem);
434 zpool_prop_t prop = zpool_name_to_prop(propname);
438 if (!zpool_prop_feature(propname)) {
439 error = SET_ERROR(EINVAL);
444 * Sanitize the input.
446 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
447 error = SET_ERROR(EINVAL);
451 if (nvpair_value_uint64(elem, &intval) != 0) {
452 error = SET_ERROR(EINVAL);
457 error = SET_ERROR(EINVAL);
461 fname = strchr(propname, '@') + 1;
462 if (zfeature_lookup_name(fname, NULL) != 0) {
463 error = SET_ERROR(EINVAL);
467 has_feature = B_TRUE;
470 case ZPOOL_PROP_VERSION:
471 error = nvpair_value_uint64(elem, &intval);
473 (intval < spa_version(spa) ||
474 intval > SPA_VERSION_BEFORE_FEATURES ||
476 error = SET_ERROR(EINVAL);
479 case ZPOOL_PROP_DELEGATION:
480 case ZPOOL_PROP_AUTOREPLACE:
481 case ZPOOL_PROP_LISTSNAPS:
482 case ZPOOL_PROP_AUTOEXPAND:
483 error = nvpair_value_uint64(elem, &intval);
484 if (!error && intval > 1)
485 error = SET_ERROR(EINVAL);
488 case ZPOOL_PROP_BOOTFS:
490 * If the pool version is less than SPA_VERSION_BOOTFS,
491 * or the pool is still being created (version == 0),
492 * the bootfs property cannot be set.
494 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
495 error = SET_ERROR(ENOTSUP);
500 * Make sure the vdev config is bootable
502 if (!vdev_is_bootable(spa->spa_root_vdev)) {
503 error = SET_ERROR(ENOTSUP);
509 error = nvpair_value_string(elem, &strval);
515 if (strval == NULL || strval[0] == '\0') {
516 objnum = zpool_prop_default_numeric(
521 if (error = dmu_objset_hold(strval, FTAG, &os))
525 * Must be ZPL, and its property settings
526 * must be supported by GRUB (compression
527 * is not gzip, and large blocks are not used).
530 if (dmu_objset_type(os) != DMU_OST_ZFS) {
531 error = SET_ERROR(ENOTSUP);
533 dsl_prop_get_int_ds(dmu_objset_ds(os),
534 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
536 !BOOTFS_COMPRESS_VALID(propval)) {
537 error = SET_ERROR(ENOTSUP);
539 dsl_prop_get_int_ds(dmu_objset_ds(os),
540 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
542 propval > SPA_OLD_MAXBLOCKSIZE) {
543 error = SET_ERROR(ENOTSUP);
545 objnum = dmu_objset_id(os);
547 dmu_objset_rele(os, FTAG);
551 case ZPOOL_PROP_FAILUREMODE:
552 error = nvpair_value_uint64(elem, &intval);
553 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
554 intval > ZIO_FAILURE_MODE_PANIC))
555 error = SET_ERROR(EINVAL);
558 * This is a special case which only occurs when
559 * the pool has completely failed. This allows
560 * the user to change the in-core failmode property
561 * without syncing it out to disk (I/Os might
562 * currently be blocked). We do this by returning
563 * EIO to the caller (spa_prop_set) to trick it
564 * into thinking we encountered a property validation
567 if (!error && spa_suspended(spa)) {
568 spa->spa_failmode = intval;
569 error = SET_ERROR(EIO);
573 case ZPOOL_PROP_CACHEFILE:
574 if ((error = nvpair_value_string(elem, &strval)) != 0)
577 if (strval[0] == '\0')
580 if (strcmp(strval, "none") == 0)
583 if (strval[0] != '/') {
584 error = SET_ERROR(EINVAL);
588 slash = strrchr(strval, '/');
589 ASSERT(slash != NULL);
591 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
592 strcmp(slash, "/..") == 0)
593 error = SET_ERROR(EINVAL);
596 case ZPOOL_PROP_COMMENT:
597 if ((error = nvpair_value_string(elem, &strval)) != 0)
599 for (check = strval; *check != '\0'; check++) {
601 * The kernel doesn't have an easy isprint()
602 * check. For this kernel check, we merely
603 * check ASCII apart from DEL. Fix this if
604 * there is an easy-to-use kernel isprint().
606 if (*check >= 0x7f) {
607 error = SET_ERROR(EINVAL);
611 if (strlen(strval) > ZPROP_MAX_COMMENT)
615 case ZPOOL_PROP_DEDUPDITTO:
616 if (spa_version(spa) < SPA_VERSION_DEDUP)
617 error = SET_ERROR(ENOTSUP);
619 error = nvpair_value_uint64(elem, &intval);
621 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
622 error = SET_ERROR(EINVAL);
630 if (!error && reset_bootfs) {
631 error = nvlist_remove(props,
632 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
635 error = nvlist_add_uint64(props,
636 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
644 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
647 spa_config_dirent_t *dp;
649 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
653 dp = kmem_alloc(sizeof (spa_config_dirent_t),
656 if (cachefile[0] == '\0')
657 dp->scd_path = spa_strdup(spa_config_path);
658 else if (strcmp(cachefile, "none") == 0)
661 dp->scd_path = spa_strdup(cachefile);
663 list_insert_head(&spa->spa_config_list, dp);
665 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
669 spa_prop_set(spa_t *spa, nvlist_t *nvp)
672 nvpair_t *elem = NULL;
673 boolean_t need_sync = B_FALSE;
675 if ((error = spa_prop_validate(spa, nvp)) != 0)
678 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
679 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
681 if (prop == ZPOOL_PROP_CACHEFILE ||
682 prop == ZPOOL_PROP_ALTROOT ||
683 prop == ZPOOL_PROP_READONLY)
686 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
689 if (prop == ZPOOL_PROP_VERSION) {
690 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
692 ASSERT(zpool_prop_feature(nvpair_name(elem)));
693 ver = SPA_VERSION_FEATURES;
697 /* Save time if the version is already set. */
698 if (ver == spa_version(spa))
702 * In addition to the pool directory object, we might
703 * create the pool properties object, the features for
704 * read object, the features for write object, or the
705 * feature descriptions object.
707 error = dsl_sync_task(spa->spa_name, NULL,
708 spa_sync_version, &ver,
709 6, ZFS_SPACE_CHECK_RESERVED);
720 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
721 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
728 * If the bootfs property value is dsobj, clear it.
731 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
733 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
734 VERIFY(zap_remove(spa->spa_meta_objset,
735 spa->spa_pool_props_object,
736 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
743 spa_change_guid_check(void *arg, dmu_tx_t *tx)
745 uint64_t *newguid = arg;
746 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
747 vdev_t *rvd = spa->spa_root_vdev;
750 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
751 vdev_state = rvd->vdev_state;
752 spa_config_exit(spa, SCL_STATE, FTAG);
754 if (vdev_state != VDEV_STATE_HEALTHY)
755 return (SET_ERROR(ENXIO));
757 ASSERT3U(spa_guid(spa), !=, *newguid);
763 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
765 uint64_t *newguid = arg;
766 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
768 vdev_t *rvd = spa->spa_root_vdev;
770 oldguid = spa_guid(spa);
772 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
773 rvd->vdev_guid = *newguid;
774 rvd->vdev_guid_sum += (*newguid - oldguid);
775 vdev_config_dirty(rvd);
776 spa_config_exit(spa, SCL_STATE, FTAG);
778 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
783 * Change the GUID for the pool. This is done so that we can later
784 * re-import a pool built from a clone of our own vdevs. We will modify
785 * the root vdev's guid, our own pool guid, and then mark all of our
786 * vdevs dirty. Note that we must make sure that all our vdevs are
787 * online when we do this, or else any vdevs that weren't present
788 * would be orphaned from our pool. We are also going to issue a
789 * sysevent to update any watchers.
792 spa_change_guid(spa_t *spa)
797 mutex_enter(&spa->spa_vdev_top_lock);
798 mutex_enter(&spa_namespace_lock);
799 guid = spa_generate_guid(NULL);
801 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
802 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
805 spa_config_sync(spa, B_FALSE, B_TRUE);
806 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
809 mutex_exit(&spa_namespace_lock);
810 mutex_exit(&spa->spa_vdev_top_lock);
816 * ==========================================================================
817 * SPA state manipulation (open/create/destroy/import/export)
818 * ==========================================================================
822 spa_error_entry_compare(const void *a, const void *b)
824 spa_error_entry_t *sa = (spa_error_entry_t *)a;
825 spa_error_entry_t *sb = (spa_error_entry_t *)b;
828 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
829 sizeof (zbookmark_phys_t));
840 * Utility function which retrieves copies of the current logs and
841 * re-initializes them in the process.
844 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
846 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
848 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
849 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
851 avl_create(&spa->spa_errlist_scrub,
852 spa_error_entry_compare, sizeof (spa_error_entry_t),
853 offsetof(spa_error_entry_t, se_avl));
854 avl_create(&spa->spa_errlist_last,
855 spa_error_entry_compare, sizeof (spa_error_entry_t),
856 offsetof(spa_error_entry_t, se_avl));
860 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
862 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
863 enum zti_modes mode = ztip->zti_mode;
864 uint_t value = ztip->zti_value;
865 uint_t count = ztip->zti_count;
866 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
869 boolean_t batch = B_FALSE;
871 if (mode == ZTI_MODE_NULL) {
873 tqs->stqs_taskq = NULL;
877 ASSERT3U(count, >, 0);
879 tqs->stqs_count = count;
880 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
884 ASSERT3U(value, >=, 1);
885 value = MAX(value, 1);
890 flags |= TASKQ_THREADS_CPU_PCT;
891 value = zio_taskq_batch_pct;
895 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
897 zio_type_name[t], zio_taskq_types[q], mode, value);
901 for (uint_t i = 0; i < count; i++) {
905 (void) snprintf(name, sizeof (name), "%s_%s_%u",
906 zio_type_name[t], zio_taskq_types[q], i);
908 (void) snprintf(name, sizeof (name), "%s_%s",
909 zio_type_name[t], zio_taskq_types[q]);
913 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
915 flags |= TASKQ_DC_BATCH;
917 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
918 spa->spa_proc, zio_taskq_basedc, flags);
921 pri_t pri = maxclsyspri;
923 * The write issue taskq can be extremely CPU
924 * intensive. Run it at slightly lower priority
925 * than the other taskqs.
927 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
930 tq = taskq_create_proc(name, value, pri, 50,
931 INT_MAX, spa->spa_proc, flags);
936 tqs->stqs_taskq[i] = tq;
941 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
943 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
945 if (tqs->stqs_taskq == NULL) {
946 ASSERT0(tqs->stqs_count);
950 for (uint_t i = 0; i < tqs->stqs_count; i++) {
951 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
952 taskq_destroy(tqs->stqs_taskq[i]);
955 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
956 tqs->stqs_taskq = NULL;
960 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
961 * Note that a type may have multiple discrete taskqs to avoid lock contention
962 * on the taskq itself. In that case we choose which taskq at random by using
963 * the low bits of gethrtime().
966 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
967 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
969 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
972 ASSERT3P(tqs->stqs_taskq, !=, NULL);
973 ASSERT3U(tqs->stqs_count, !=, 0);
975 if (tqs->stqs_count == 1) {
976 tq = tqs->stqs_taskq[0];
979 tq = tqs->stqs_taskq[cpu_ticks() % tqs->stqs_count];
981 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
985 taskq_dispatch_ent(tq, func, arg, flags, ent);
989 spa_create_zio_taskqs(spa_t *spa)
991 for (int t = 0; t < ZIO_TYPES; t++) {
992 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
993 spa_taskqs_init(spa, t, q);
1001 spa_thread(void *arg)
1003 callb_cpr_t cprinfo;
1006 user_t *pu = PTOU(curproc);
1008 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1011 ASSERT(curproc != &p0);
1012 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1013 "zpool-%s", spa->spa_name);
1014 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1017 /* bind this thread to the requested psrset */
1018 if (zio_taskq_psrset_bind != PS_NONE) {
1020 mutex_enter(&cpu_lock);
1021 mutex_enter(&pidlock);
1022 mutex_enter(&curproc->p_lock);
1024 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1025 0, NULL, NULL) == 0) {
1026 curthread->t_bind_pset = zio_taskq_psrset_bind;
1029 "Couldn't bind process for zfs pool \"%s\" to "
1030 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1033 mutex_exit(&curproc->p_lock);
1034 mutex_exit(&pidlock);
1035 mutex_exit(&cpu_lock);
1041 if (zio_taskq_sysdc) {
1042 sysdc_thread_enter(curthread, 100, 0);
1046 spa->spa_proc = curproc;
1047 spa->spa_did = curthread->t_did;
1049 spa_create_zio_taskqs(spa);
1051 mutex_enter(&spa->spa_proc_lock);
1052 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1054 spa->spa_proc_state = SPA_PROC_ACTIVE;
1055 cv_broadcast(&spa->spa_proc_cv);
1057 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1058 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1059 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1060 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1062 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1063 spa->spa_proc_state = SPA_PROC_GONE;
1064 spa->spa_proc = &p0;
1065 cv_broadcast(&spa->spa_proc_cv);
1066 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1068 mutex_enter(&curproc->p_lock);
1071 #endif /* SPA_PROCESS */
1075 * Activate an uninitialized pool.
1078 spa_activate(spa_t *spa, int mode)
1080 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1082 spa->spa_state = POOL_STATE_ACTIVE;
1083 spa->spa_mode = mode;
1085 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1086 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1088 /* Try to create a covering process */
1089 mutex_enter(&spa->spa_proc_lock);
1090 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1091 ASSERT(spa->spa_proc == &p0);
1095 /* Only create a process if we're going to be around a while. */
1096 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1097 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1099 spa->spa_proc_state = SPA_PROC_CREATED;
1100 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1101 cv_wait(&spa->spa_proc_cv,
1102 &spa->spa_proc_lock);
1104 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1105 ASSERT(spa->spa_proc != &p0);
1106 ASSERT(spa->spa_did != 0);
1110 "Couldn't create process for zfs pool \"%s\"\n",
1115 #endif /* SPA_PROCESS */
1116 mutex_exit(&spa->spa_proc_lock);
1118 /* If we didn't create a process, we need to create our taskqs. */
1119 ASSERT(spa->spa_proc == &p0);
1120 if (spa->spa_proc == &p0) {
1121 spa_create_zio_taskqs(spa);
1125 * Start TRIM thread.
1127 trim_thread_create(spa);
1129 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1130 offsetof(vdev_t, vdev_config_dirty_node));
1131 list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1132 offsetof(objset_t, os_evicting_node));
1133 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1134 offsetof(vdev_t, vdev_state_dirty_node));
1136 txg_list_create(&spa->spa_vdev_txg_list,
1137 offsetof(struct vdev, vdev_txg_node));
1139 avl_create(&spa->spa_errlist_scrub,
1140 spa_error_entry_compare, sizeof (spa_error_entry_t),
1141 offsetof(spa_error_entry_t, se_avl));
1142 avl_create(&spa->spa_errlist_last,
1143 spa_error_entry_compare, sizeof (spa_error_entry_t),
1144 offsetof(spa_error_entry_t, se_avl));
1148 * Opposite of spa_activate().
1151 spa_deactivate(spa_t *spa)
1153 ASSERT(spa->spa_sync_on == B_FALSE);
1154 ASSERT(spa->spa_dsl_pool == NULL);
1155 ASSERT(spa->spa_root_vdev == NULL);
1156 ASSERT(spa->spa_async_zio_root == NULL);
1157 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1160 * Stop TRIM thread in case spa_unload() wasn't called directly
1161 * before spa_deactivate().
1163 trim_thread_destroy(spa);
1165 spa_evicting_os_wait(spa);
1167 txg_list_destroy(&spa->spa_vdev_txg_list);
1169 list_destroy(&spa->spa_config_dirty_list);
1170 list_destroy(&spa->spa_evicting_os_list);
1171 list_destroy(&spa->spa_state_dirty_list);
1173 for (int t = 0; t < ZIO_TYPES; t++) {
1174 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1175 spa_taskqs_fini(spa, t, q);
1179 metaslab_class_destroy(spa->spa_normal_class);
1180 spa->spa_normal_class = NULL;
1182 metaslab_class_destroy(spa->spa_log_class);
1183 spa->spa_log_class = NULL;
1186 * If this was part of an import or the open otherwise failed, we may
1187 * still have errors left in the queues. Empty them just in case.
1189 spa_errlog_drain(spa);
1191 avl_destroy(&spa->spa_errlist_scrub);
1192 avl_destroy(&spa->spa_errlist_last);
1194 spa->spa_state = POOL_STATE_UNINITIALIZED;
1196 mutex_enter(&spa->spa_proc_lock);
1197 if (spa->spa_proc_state != SPA_PROC_NONE) {
1198 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1199 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1200 cv_broadcast(&spa->spa_proc_cv);
1201 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1202 ASSERT(spa->spa_proc != &p0);
1203 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1205 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1206 spa->spa_proc_state = SPA_PROC_NONE;
1208 ASSERT(spa->spa_proc == &p0);
1209 mutex_exit(&spa->spa_proc_lock);
1213 * We want to make sure spa_thread() has actually exited the ZFS
1214 * module, so that the module can't be unloaded out from underneath
1217 if (spa->spa_did != 0) {
1218 thread_join(spa->spa_did);
1221 #endif /* SPA_PROCESS */
1225 * Verify a pool configuration, and construct the vdev tree appropriately. This
1226 * will create all the necessary vdevs in the appropriate layout, with each vdev
1227 * in the CLOSED state. This will prep the pool before open/creation/import.
1228 * All vdev validation is done by the vdev_alloc() routine.
1231 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1232 uint_t id, int atype)
1238 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1241 if ((*vdp)->vdev_ops->vdev_op_leaf)
1244 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1247 if (error == ENOENT)
1253 return (SET_ERROR(EINVAL));
1256 for (int c = 0; c < children; c++) {
1258 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1266 ASSERT(*vdp != NULL);
1272 * Opposite of spa_load().
1275 spa_unload(spa_t *spa)
1279 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1284 trim_thread_destroy(spa);
1289 spa_async_suspend(spa);
1294 if (spa->spa_sync_on) {
1295 txg_sync_stop(spa->spa_dsl_pool);
1296 spa->spa_sync_on = B_FALSE;
1300 * Wait for any outstanding async I/O to complete.
1302 if (spa->spa_async_zio_root != NULL) {
1303 for (int i = 0; i < max_ncpus; i++)
1304 (void) zio_wait(spa->spa_async_zio_root[i]);
1305 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
1306 spa->spa_async_zio_root = NULL;
1309 bpobj_close(&spa->spa_deferred_bpobj);
1311 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1316 if (spa->spa_root_vdev)
1317 vdev_free(spa->spa_root_vdev);
1318 ASSERT(spa->spa_root_vdev == NULL);
1321 * Close the dsl pool.
1323 if (spa->spa_dsl_pool) {
1324 dsl_pool_close(spa->spa_dsl_pool);
1325 spa->spa_dsl_pool = NULL;
1326 spa->spa_meta_objset = NULL;
1333 * Drop and purge level 2 cache
1335 spa_l2cache_drop(spa);
1337 for (i = 0; i < spa->spa_spares.sav_count; i++)
1338 vdev_free(spa->spa_spares.sav_vdevs[i]);
1339 if (spa->spa_spares.sav_vdevs) {
1340 kmem_free(spa->spa_spares.sav_vdevs,
1341 spa->spa_spares.sav_count * sizeof (void *));
1342 spa->spa_spares.sav_vdevs = NULL;
1344 if (spa->spa_spares.sav_config) {
1345 nvlist_free(spa->spa_spares.sav_config);
1346 spa->spa_spares.sav_config = NULL;
1348 spa->spa_spares.sav_count = 0;
1350 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1351 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1352 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1354 if (spa->spa_l2cache.sav_vdevs) {
1355 kmem_free(spa->spa_l2cache.sav_vdevs,
1356 spa->spa_l2cache.sav_count * sizeof (void *));
1357 spa->spa_l2cache.sav_vdevs = NULL;
1359 if (spa->spa_l2cache.sav_config) {
1360 nvlist_free(spa->spa_l2cache.sav_config);
1361 spa->spa_l2cache.sav_config = NULL;
1363 spa->spa_l2cache.sav_count = 0;
1365 spa->spa_async_suspended = 0;
1367 if (spa->spa_comment != NULL) {
1368 spa_strfree(spa->spa_comment);
1369 spa->spa_comment = NULL;
1372 spa_config_exit(spa, SCL_ALL, FTAG);
1376 * Load (or re-load) the current list of vdevs describing the active spares for
1377 * this pool. When this is called, we have some form of basic information in
1378 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1379 * then re-generate a more complete list including status information.
1382 spa_load_spares(spa_t *spa)
1389 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1392 * First, close and free any existing spare vdevs.
1394 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1395 vd = spa->spa_spares.sav_vdevs[i];
1397 /* Undo the call to spa_activate() below */
1398 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1399 B_FALSE)) != NULL && tvd->vdev_isspare)
1400 spa_spare_remove(tvd);
1405 if (spa->spa_spares.sav_vdevs)
1406 kmem_free(spa->spa_spares.sav_vdevs,
1407 spa->spa_spares.sav_count * sizeof (void *));
1409 if (spa->spa_spares.sav_config == NULL)
1412 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1413 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1415 spa->spa_spares.sav_count = (int)nspares;
1416 spa->spa_spares.sav_vdevs = NULL;
1422 * Construct the array of vdevs, opening them to get status in the
1423 * process. For each spare, there is potentially two different vdev_t
1424 * structures associated with it: one in the list of spares (used only
1425 * for basic validation purposes) and one in the active vdev
1426 * configuration (if it's spared in). During this phase we open and
1427 * validate each vdev on the spare list. If the vdev also exists in the
1428 * active configuration, then we also mark this vdev as an active spare.
1430 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1432 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1433 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1434 VDEV_ALLOC_SPARE) == 0);
1437 spa->spa_spares.sav_vdevs[i] = vd;
1439 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1440 B_FALSE)) != NULL) {
1441 if (!tvd->vdev_isspare)
1445 * We only mark the spare active if we were successfully
1446 * able to load the vdev. Otherwise, importing a pool
1447 * with a bad active spare would result in strange
1448 * behavior, because multiple pool would think the spare
1449 * is actively in use.
1451 * There is a vulnerability here to an equally bizarre
1452 * circumstance, where a dead active spare is later
1453 * brought back to life (onlined or otherwise). Given
1454 * the rarity of this scenario, and the extra complexity
1455 * it adds, we ignore the possibility.
1457 if (!vdev_is_dead(tvd))
1458 spa_spare_activate(tvd);
1462 vd->vdev_aux = &spa->spa_spares;
1464 if (vdev_open(vd) != 0)
1467 if (vdev_validate_aux(vd) == 0)
1472 * Recompute the stashed list of spares, with status information
1475 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1476 DATA_TYPE_NVLIST_ARRAY) == 0);
1478 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1480 for (i = 0; i < spa->spa_spares.sav_count; i++)
1481 spares[i] = vdev_config_generate(spa,
1482 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1483 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1484 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1485 for (i = 0; i < spa->spa_spares.sav_count; i++)
1486 nvlist_free(spares[i]);
1487 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1491 * Load (or re-load) the current list of vdevs describing the active l2cache for
1492 * this pool. When this is called, we have some form of basic information in
1493 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1494 * then re-generate a more complete list including status information.
1495 * Devices which are already active have their details maintained, and are
1499 spa_load_l2cache(spa_t *spa)
1503 int i, j, oldnvdevs;
1505 vdev_t *vd, **oldvdevs, **newvdevs;
1506 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1508 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1510 if (sav->sav_config != NULL) {
1511 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1512 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1513 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1519 oldvdevs = sav->sav_vdevs;
1520 oldnvdevs = sav->sav_count;
1521 sav->sav_vdevs = NULL;
1525 * Process new nvlist of vdevs.
1527 for (i = 0; i < nl2cache; i++) {
1528 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1532 for (j = 0; j < oldnvdevs; j++) {
1534 if (vd != NULL && guid == vd->vdev_guid) {
1536 * Retain previous vdev for add/remove ops.
1544 if (newvdevs[i] == NULL) {
1548 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1549 VDEV_ALLOC_L2CACHE) == 0);
1554 * Commit this vdev as an l2cache device,
1555 * even if it fails to open.
1557 spa_l2cache_add(vd);
1562 spa_l2cache_activate(vd);
1564 if (vdev_open(vd) != 0)
1567 (void) vdev_validate_aux(vd);
1569 if (!vdev_is_dead(vd))
1570 l2arc_add_vdev(spa, vd);
1575 * Purge vdevs that were dropped
1577 for (i = 0; i < oldnvdevs; i++) {
1582 ASSERT(vd->vdev_isl2cache);
1584 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1585 pool != 0ULL && l2arc_vdev_present(vd))
1586 l2arc_remove_vdev(vd);
1587 vdev_clear_stats(vd);
1593 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1595 if (sav->sav_config == NULL)
1598 sav->sav_vdevs = newvdevs;
1599 sav->sav_count = (int)nl2cache;
1602 * Recompute the stashed list of l2cache devices, with status
1603 * information this time.
1605 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1606 DATA_TYPE_NVLIST_ARRAY) == 0);
1608 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1609 for (i = 0; i < sav->sav_count; i++)
1610 l2cache[i] = vdev_config_generate(spa,
1611 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1612 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1613 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1615 for (i = 0; i < sav->sav_count; i++)
1616 nvlist_free(l2cache[i]);
1618 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1622 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1625 char *packed = NULL;
1630 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1634 nvsize = *(uint64_t *)db->db_data;
1635 dmu_buf_rele(db, FTAG);
1637 packed = kmem_alloc(nvsize, KM_SLEEP);
1638 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1641 error = nvlist_unpack(packed, nvsize, value, 0);
1642 kmem_free(packed, nvsize);
1648 * Checks to see if the given vdev could not be opened, in which case we post a
1649 * sysevent to notify the autoreplace code that the device has been removed.
1652 spa_check_removed(vdev_t *vd)
1654 for (int c = 0; c < vd->vdev_children; c++)
1655 spa_check_removed(vd->vdev_child[c]);
1657 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1659 zfs_post_autoreplace(vd->vdev_spa, vd);
1660 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1665 * Validate the current config against the MOS config
1668 spa_config_valid(spa_t *spa, nvlist_t *config)
1670 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1673 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1675 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1676 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1678 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1681 * If we're doing a normal import, then build up any additional
1682 * diagnostic information about missing devices in this config.
1683 * We'll pass this up to the user for further processing.
1685 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1686 nvlist_t **child, *nv;
1689 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1691 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1693 for (int c = 0; c < rvd->vdev_children; c++) {
1694 vdev_t *tvd = rvd->vdev_child[c];
1695 vdev_t *mtvd = mrvd->vdev_child[c];
1697 if (tvd->vdev_ops == &vdev_missing_ops &&
1698 mtvd->vdev_ops != &vdev_missing_ops &&
1700 child[idx++] = vdev_config_generate(spa, mtvd,
1705 VERIFY(nvlist_add_nvlist_array(nv,
1706 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1707 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1708 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1710 for (int i = 0; i < idx; i++)
1711 nvlist_free(child[i]);
1714 kmem_free(child, rvd->vdev_children * sizeof (char **));
1718 * Compare the root vdev tree with the information we have
1719 * from the MOS config (mrvd). Check each top-level vdev
1720 * with the corresponding MOS config top-level (mtvd).
1722 for (int c = 0; c < rvd->vdev_children; c++) {
1723 vdev_t *tvd = rvd->vdev_child[c];
1724 vdev_t *mtvd = mrvd->vdev_child[c];
1727 * Resolve any "missing" vdevs in the current configuration.
1728 * If we find that the MOS config has more accurate information
1729 * about the top-level vdev then use that vdev instead.
1731 if (tvd->vdev_ops == &vdev_missing_ops &&
1732 mtvd->vdev_ops != &vdev_missing_ops) {
1734 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1738 * Device specific actions.
1740 if (mtvd->vdev_islog) {
1741 spa_set_log_state(spa, SPA_LOG_CLEAR);
1744 * XXX - once we have 'readonly' pool
1745 * support we should be able to handle
1746 * missing data devices by transitioning
1747 * the pool to readonly.
1753 * Swap the missing vdev with the data we were
1754 * able to obtain from the MOS config.
1756 vdev_remove_child(rvd, tvd);
1757 vdev_remove_child(mrvd, mtvd);
1759 vdev_add_child(rvd, mtvd);
1760 vdev_add_child(mrvd, tvd);
1762 spa_config_exit(spa, SCL_ALL, FTAG);
1764 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1767 } else if (mtvd->vdev_islog) {
1769 * Load the slog device's state from the MOS config
1770 * since it's possible that the label does not
1771 * contain the most up-to-date information.
1773 vdev_load_log_state(tvd, mtvd);
1778 spa_config_exit(spa, SCL_ALL, FTAG);
1781 * Ensure we were able to validate the config.
1783 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1787 * Check for missing log devices
1790 spa_check_logs(spa_t *spa)
1792 boolean_t rv = B_FALSE;
1793 dsl_pool_t *dp = spa_get_dsl(spa);
1795 switch (spa->spa_log_state) {
1796 case SPA_LOG_MISSING:
1797 /* need to recheck in case slog has been restored */
1798 case SPA_LOG_UNKNOWN:
1799 rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1800 zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
1802 spa_set_log_state(spa, SPA_LOG_MISSING);
1809 spa_passivate_log(spa_t *spa)
1811 vdev_t *rvd = spa->spa_root_vdev;
1812 boolean_t slog_found = B_FALSE;
1814 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1816 if (!spa_has_slogs(spa))
1819 for (int c = 0; c < rvd->vdev_children; c++) {
1820 vdev_t *tvd = rvd->vdev_child[c];
1821 metaslab_group_t *mg = tvd->vdev_mg;
1823 if (tvd->vdev_islog) {
1824 metaslab_group_passivate(mg);
1825 slog_found = B_TRUE;
1829 return (slog_found);
1833 spa_activate_log(spa_t *spa)
1835 vdev_t *rvd = spa->spa_root_vdev;
1837 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1839 for (int c = 0; c < rvd->vdev_children; c++) {
1840 vdev_t *tvd = rvd->vdev_child[c];
1841 metaslab_group_t *mg = tvd->vdev_mg;
1843 if (tvd->vdev_islog)
1844 metaslab_group_activate(mg);
1849 spa_offline_log(spa_t *spa)
1853 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1854 NULL, DS_FIND_CHILDREN);
1857 * We successfully offlined the log device, sync out the
1858 * current txg so that the "stubby" block can be removed
1861 txg_wait_synced(spa->spa_dsl_pool, 0);
1867 spa_aux_check_removed(spa_aux_vdev_t *sav)
1871 for (i = 0; i < sav->sav_count; i++)
1872 spa_check_removed(sav->sav_vdevs[i]);
1876 spa_claim_notify(zio_t *zio)
1878 spa_t *spa = zio->io_spa;
1883 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1884 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1885 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1886 mutex_exit(&spa->spa_props_lock);
1889 typedef struct spa_load_error {
1890 uint64_t sle_meta_count;
1891 uint64_t sle_data_count;
1895 spa_load_verify_done(zio_t *zio)
1897 blkptr_t *bp = zio->io_bp;
1898 spa_load_error_t *sle = zio->io_private;
1899 dmu_object_type_t type = BP_GET_TYPE(bp);
1900 int error = zio->io_error;
1901 spa_t *spa = zio->io_spa;
1904 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1905 type != DMU_OT_INTENT_LOG)
1906 atomic_inc_64(&sle->sle_meta_count);
1908 atomic_inc_64(&sle->sle_data_count);
1910 zio_data_buf_free(zio->io_data, zio->io_size);
1912 mutex_enter(&spa->spa_scrub_lock);
1913 spa->spa_scrub_inflight--;
1914 cv_broadcast(&spa->spa_scrub_io_cv);
1915 mutex_exit(&spa->spa_scrub_lock);
1919 * Maximum number of concurrent scrub i/os to create while verifying
1920 * a pool while importing it.
1922 int spa_load_verify_maxinflight = 10000;
1923 boolean_t spa_load_verify_metadata = B_TRUE;
1924 boolean_t spa_load_verify_data = B_TRUE;
1926 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1927 &spa_load_verify_maxinflight, 0,
1928 "Maximum number of concurrent scrub I/Os to create while verifying a "
1929 "pool while importing it");
1931 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1932 &spa_load_verify_metadata, 0,
1933 "Check metadata on import?");
1935 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1936 &spa_load_verify_data, 0,
1937 "Check user data on import?");
1941 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1942 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1944 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1947 * Note: normally this routine will not be called if
1948 * spa_load_verify_metadata is not set. However, it may be useful
1949 * to manually set the flag after the traversal has begun.
1951 if (!spa_load_verify_metadata)
1953 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1957 size_t size = BP_GET_PSIZE(bp);
1958 void *data = zio_data_buf_alloc(size);
1960 mutex_enter(&spa->spa_scrub_lock);
1961 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1962 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1963 spa->spa_scrub_inflight++;
1964 mutex_exit(&spa->spa_scrub_lock);
1966 zio_nowait(zio_read(rio, spa, bp, data, size,
1967 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1968 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1969 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1974 spa_load_verify(spa_t *spa)
1977 spa_load_error_t sle = { 0 };
1978 zpool_rewind_policy_t policy;
1979 boolean_t verify_ok = B_FALSE;
1982 zpool_get_rewind_policy(spa->spa_config, &policy);
1984 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1987 rio = zio_root(spa, NULL, &sle,
1988 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1990 if (spa_load_verify_metadata) {
1991 error = traverse_pool(spa, spa->spa_verify_min_txg,
1992 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
1993 spa_load_verify_cb, rio);
1996 (void) zio_wait(rio);
1998 spa->spa_load_meta_errors = sle.sle_meta_count;
1999 spa->spa_load_data_errors = sle.sle_data_count;
2001 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
2002 sle.sle_data_count <= policy.zrp_maxdata) {
2006 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2007 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2009 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2010 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2011 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2012 VERIFY(nvlist_add_int64(spa->spa_load_info,
2013 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2014 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2015 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2017 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2021 if (error != ENXIO && error != EIO)
2022 error = SET_ERROR(EIO);
2026 return (verify_ok ? 0 : EIO);
2030 * Find a value in the pool props object.
2033 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2035 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2036 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2040 * Find a value in the pool directory object.
2043 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2045 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2046 name, sizeof (uint64_t), 1, val));
2050 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2052 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2057 * Fix up config after a partly-completed split. This is done with the
2058 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2059 * pool have that entry in their config, but only the splitting one contains
2060 * a list of all the guids of the vdevs that are being split off.
2062 * This function determines what to do with that list: either rejoin
2063 * all the disks to the pool, or complete the splitting process. To attempt
2064 * the rejoin, each disk that is offlined is marked online again, and
2065 * we do a reopen() call. If the vdev label for every disk that was
2066 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2067 * then we call vdev_split() on each disk, and complete the split.
2069 * Otherwise we leave the config alone, with all the vdevs in place in
2070 * the original pool.
2073 spa_try_repair(spa_t *spa, nvlist_t *config)
2080 boolean_t attempt_reopen;
2082 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2085 /* check that the config is complete */
2086 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2087 &glist, &gcount) != 0)
2090 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2092 /* attempt to online all the vdevs & validate */
2093 attempt_reopen = B_TRUE;
2094 for (i = 0; i < gcount; i++) {
2095 if (glist[i] == 0) /* vdev is hole */
2098 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2099 if (vd[i] == NULL) {
2101 * Don't bother attempting to reopen the disks;
2102 * just do the split.
2104 attempt_reopen = B_FALSE;
2106 /* attempt to re-online it */
2107 vd[i]->vdev_offline = B_FALSE;
2111 if (attempt_reopen) {
2112 vdev_reopen(spa->spa_root_vdev);
2114 /* check each device to see what state it's in */
2115 for (extracted = 0, i = 0; i < gcount; i++) {
2116 if (vd[i] != NULL &&
2117 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2124 * If every disk has been moved to the new pool, or if we never
2125 * even attempted to look at them, then we split them off for
2128 if (!attempt_reopen || gcount == extracted) {
2129 for (i = 0; i < gcount; i++)
2132 vdev_reopen(spa->spa_root_vdev);
2135 kmem_free(vd, gcount * sizeof (vdev_t *));
2139 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2140 boolean_t mosconfig)
2142 nvlist_t *config = spa->spa_config;
2143 char *ereport = FM_EREPORT_ZFS_POOL;
2149 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2150 return (SET_ERROR(EINVAL));
2152 ASSERT(spa->spa_comment == NULL);
2153 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2154 spa->spa_comment = spa_strdup(comment);
2157 * Versioning wasn't explicitly added to the label until later, so if
2158 * it's not present treat it as the initial version.
2160 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2161 &spa->spa_ubsync.ub_version) != 0)
2162 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2164 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2165 &spa->spa_config_txg);
2167 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2168 spa_guid_exists(pool_guid, 0)) {
2169 error = SET_ERROR(EEXIST);
2171 spa->spa_config_guid = pool_guid;
2173 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2175 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2179 nvlist_free(spa->spa_load_info);
2180 spa->spa_load_info = fnvlist_alloc();
2182 gethrestime(&spa->spa_loaded_ts);
2183 error = spa_load_impl(spa, pool_guid, config, state, type,
2184 mosconfig, &ereport);
2188 * Don't count references from objsets that are already closed
2189 * and are making their way through the eviction process.
2191 spa_evicting_os_wait(spa);
2192 spa->spa_minref = refcount_count(&spa->spa_refcount);
2194 if (error != EEXIST) {
2195 spa->spa_loaded_ts.tv_sec = 0;
2196 spa->spa_loaded_ts.tv_nsec = 0;
2198 if (error != EBADF) {
2199 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2202 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2209 * Load an existing storage pool, using the pool's builtin spa_config as a
2210 * source of configuration information.
2213 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2214 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2218 nvlist_t *nvroot = NULL;
2221 uberblock_t *ub = &spa->spa_uberblock;
2222 uint64_t children, config_cache_txg = spa->spa_config_txg;
2223 int orig_mode = spa->spa_mode;
2226 boolean_t missing_feat_write = B_FALSE;
2229 * If this is an untrusted config, access the pool in read-only mode.
2230 * This prevents things like resilvering recently removed devices.
2233 spa->spa_mode = FREAD;
2235 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2237 spa->spa_load_state = state;
2239 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2240 return (SET_ERROR(EINVAL));
2242 parse = (type == SPA_IMPORT_EXISTING ?
2243 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2246 * Create "The Godfather" zio to hold all async IOs
2248 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2250 for (int i = 0; i < max_ncpus; i++) {
2251 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2252 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2253 ZIO_FLAG_GODFATHER);
2257 * Parse the configuration into a vdev tree. We explicitly set the
2258 * value that will be returned by spa_version() since parsing the
2259 * configuration requires knowing the version number.
2261 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2262 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2263 spa_config_exit(spa, SCL_ALL, FTAG);
2268 ASSERT(spa->spa_root_vdev == rvd);
2269 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2270 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
2272 if (type != SPA_IMPORT_ASSEMBLE) {
2273 ASSERT(spa_guid(spa) == pool_guid);
2277 * Try to open all vdevs, loading each label in the process.
2279 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2280 error = vdev_open(rvd);
2281 spa_config_exit(spa, SCL_ALL, FTAG);
2286 * We need to validate the vdev labels against the configuration that
2287 * we have in hand, which is dependent on the setting of mosconfig. If
2288 * mosconfig is true then we're validating the vdev labels based on
2289 * that config. Otherwise, we're validating against the cached config
2290 * (zpool.cache) that was read when we loaded the zfs module, and then
2291 * later we will recursively call spa_load() and validate against
2294 * If we're assembling a new pool that's been split off from an
2295 * existing pool, the labels haven't yet been updated so we skip
2296 * validation for now.
2298 if (type != SPA_IMPORT_ASSEMBLE) {
2299 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2300 error = vdev_validate(rvd, mosconfig);
2301 spa_config_exit(spa, SCL_ALL, FTAG);
2306 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2307 return (SET_ERROR(ENXIO));
2311 * Find the best uberblock.
2313 vdev_uberblock_load(rvd, ub, &label);
2316 * If we weren't able to find a single valid uberblock, return failure.
2318 if (ub->ub_txg == 0) {
2320 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2324 * If the pool has an unsupported version we can't open it.
2326 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2328 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2331 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2335 * If we weren't able to find what's necessary for reading the
2336 * MOS in the label, return failure.
2338 if (label == NULL || nvlist_lookup_nvlist(label,
2339 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2341 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2346 * Update our in-core representation with the definitive values
2349 nvlist_free(spa->spa_label_features);
2350 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2356 * Look through entries in the label nvlist's features_for_read. If
2357 * there is a feature listed there which we don't understand then we
2358 * cannot open a pool.
2360 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2361 nvlist_t *unsup_feat;
2363 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2366 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2368 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2369 if (!zfeature_is_supported(nvpair_name(nvp))) {
2370 VERIFY(nvlist_add_string(unsup_feat,
2371 nvpair_name(nvp), "") == 0);
2375 if (!nvlist_empty(unsup_feat)) {
2376 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2377 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2378 nvlist_free(unsup_feat);
2379 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2383 nvlist_free(unsup_feat);
2387 * If the vdev guid sum doesn't match the uberblock, we have an
2388 * incomplete configuration. We first check to see if the pool
2389 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2390 * If it is, defer the vdev_guid_sum check till later so we
2391 * can handle missing vdevs.
2393 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2394 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2395 rvd->vdev_guid_sum != ub->ub_guid_sum)
2396 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2398 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2399 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2400 spa_try_repair(spa, config);
2401 spa_config_exit(spa, SCL_ALL, FTAG);
2402 nvlist_free(spa->spa_config_splitting);
2403 spa->spa_config_splitting = NULL;
2407 * Initialize internal SPA structures.
2409 spa->spa_state = POOL_STATE_ACTIVE;
2410 spa->spa_ubsync = spa->spa_uberblock;
2411 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2412 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2413 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2414 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2415 spa->spa_claim_max_txg = spa->spa_first_txg;
2416 spa->spa_prev_software_version = ub->ub_software_version;
2418 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2420 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2421 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2423 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2424 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2426 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2427 boolean_t missing_feat_read = B_FALSE;
2428 nvlist_t *unsup_feat, *enabled_feat;
2430 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2431 &spa->spa_feat_for_read_obj) != 0) {
2432 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2435 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2436 &spa->spa_feat_for_write_obj) != 0) {
2437 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2440 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2441 &spa->spa_feat_desc_obj) != 0) {
2442 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2445 enabled_feat = fnvlist_alloc();
2446 unsup_feat = fnvlist_alloc();
2448 if (!spa_features_check(spa, B_FALSE,
2449 unsup_feat, enabled_feat))
2450 missing_feat_read = B_TRUE;
2452 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2453 if (!spa_features_check(spa, B_TRUE,
2454 unsup_feat, enabled_feat)) {
2455 missing_feat_write = B_TRUE;
2459 fnvlist_add_nvlist(spa->spa_load_info,
2460 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2462 if (!nvlist_empty(unsup_feat)) {
2463 fnvlist_add_nvlist(spa->spa_load_info,
2464 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2467 fnvlist_free(enabled_feat);
2468 fnvlist_free(unsup_feat);
2470 if (!missing_feat_read) {
2471 fnvlist_add_boolean(spa->spa_load_info,
2472 ZPOOL_CONFIG_CAN_RDONLY);
2476 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2477 * twofold: to determine whether the pool is available for
2478 * import in read-write mode and (if it is not) whether the
2479 * pool is available for import in read-only mode. If the pool
2480 * is available for import in read-write mode, it is displayed
2481 * as available in userland; if it is not available for import
2482 * in read-only mode, it is displayed as unavailable in
2483 * userland. If the pool is available for import in read-only
2484 * mode but not read-write mode, it is displayed as unavailable
2485 * in userland with a special note that the pool is actually
2486 * available for open in read-only mode.
2488 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2489 * missing a feature for write, we must first determine whether
2490 * the pool can be opened read-only before returning to
2491 * userland in order to know whether to display the
2492 * abovementioned note.
2494 if (missing_feat_read || (missing_feat_write &&
2495 spa_writeable(spa))) {
2496 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2501 * Load refcounts for ZFS features from disk into an in-memory
2502 * cache during SPA initialization.
2504 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2507 error = feature_get_refcount_from_disk(spa,
2508 &spa_feature_table[i], &refcount);
2510 spa->spa_feat_refcount_cache[i] = refcount;
2511 } else if (error == ENOTSUP) {
2512 spa->spa_feat_refcount_cache[i] =
2513 SPA_FEATURE_DISABLED;
2515 return (spa_vdev_err(rvd,
2516 VDEV_AUX_CORRUPT_DATA, EIO));
2521 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2522 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2523 &spa->spa_feat_enabled_txg_obj) != 0)
2524 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2527 spa->spa_is_initializing = B_TRUE;
2528 error = dsl_pool_open(spa->spa_dsl_pool);
2529 spa->spa_is_initializing = B_FALSE;
2531 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2535 nvlist_t *policy = NULL, *nvconfig;
2537 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2538 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2540 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2541 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2543 unsigned long myhostid = 0;
2545 VERIFY(nvlist_lookup_string(nvconfig,
2546 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2549 myhostid = zone_get_hostid(NULL);
2552 * We're emulating the system's hostid in userland, so
2553 * we can't use zone_get_hostid().
2555 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2556 #endif /* _KERNEL */
2557 if (check_hostid && hostid != 0 && myhostid != 0 &&
2558 hostid != myhostid) {
2559 nvlist_free(nvconfig);
2560 cmn_err(CE_WARN, "pool '%s' could not be "
2561 "loaded as it was last accessed by "
2562 "another system (host: %s hostid: 0x%lx). "
2563 "See: http://illumos.org/msg/ZFS-8000-EY",
2564 spa_name(spa), hostname,
2565 (unsigned long)hostid);
2566 return (SET_ERROR(EBADF));
2569 if (nvlist_lookup_nvlist(spa->spa_config,
2570 ZPOOL_REWIND_POLICY, &policy) == 0)
2571 VERIFY(nvlist_add_nvlist(nvconfig,
2572 ZPOOL_REWIND_POLICY, policy) == 0);
2574 spa_config_set(spa, nvconfig);
2576 spa_deactivate(spa);
2577 spa_activate(spa, orig_mode);
2579 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2582 /* Grab the secret checksum salt from the MOS. */
2583 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2584 DMU_POOL_CHECKSUM_SALT, 1,
2585 sizeof (spa->spa_cksum_salt.zcs_bytes),
2586 spa->spa_cksum_salt.zcs_bytes);
2587 if (error == ENOENT) {
2588 /* Generate a new salt for subsequent use */
2589 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
2590 sizeof (spa->spa_cksum_salt.zcs_bytes));
2591 } else if (error != 0) {
2592 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2595 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2596 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2597 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2599 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2602 * Load the bit that tells us to use the new accounting function
2603 * (raid-z deflation). If we have an older pool, this will not
2606 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2607 if (error != 0 && error != ENOENT)
2608 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2610 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2611 &spa->spa_creation_version);
2612 if (error != 0 && error != ENOENT)
2613 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2616 * Load the persistent error log. If we have an older pool, this will
2619 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2620 if (error != 0 && error != ENOENT)
2621 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2623 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2624 &spa->spa_errlog_scrub);
2625 if (error != 0 && error != ENOENT)
2626 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2629 * Load the history object. If we have an older pool, this
2630 * will not be present.
2632 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2633 if (error != 0 && error != ENOENT)
2634 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2637 * If we're assembling the pool from the split-off vdevs of
2638 * an existing pool, we don't want to attach the spares & cache
2643 * Load any hot spares for this pool.
2645 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2646 if (error != 0 && error != ENOENT)
2647 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2648 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2649 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2650 if (load_nvlist(spa, spa->spa_spares.sav_object,
2651 &spa->spa_spares.sav_config) != 0)
2652 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2654 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2655 spa_load_spares(spa);
2656 spa_config_exit(spa, SCL_ALL, FTAG);
2657 } else if (error == 0) {
2658 spa->spa_spares.sav_sync = B_TRUE;
2662 * Load any level 2 ARC devices for this pool.
2664 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2665 &spa->spa_l2cache.sav_object);
2666 if (error != 0 && error != ENOENT)
2667 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2668 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2669 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2670 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2671 &spa->spa_l2cache.sav_config) != 0)
2672 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2674 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2675 spa_load_l2cache(spa);
2676 spa_config_exit(spa, SCL_ALL, FTAG);
2677 } else if (error == 0) {
2678 spa->spa_l2cache.sav_sync = B_TRUE;
2681 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2683 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2684 if (error && error != ENOENT)
2685 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2688 uint64_t autoreplace;
2690 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2691 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2692 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2693 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2694 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2695 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2696 &spa->spa_dedup_ditto);
2698 spa->spa_autoreplace = (autoreplace != 0);
2702 * If the 'autoreplace' property is set, then post a resource notifying
2703 * the ZFS DE that it should not issue any faults for unopenable
2704 * devices. We also iterate over the vdevs, and post a sysevent for any
2705 * unopenable vdevs so that the normal autoreplace handler can take
2708 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2709 spa_check_removed(spa->spa_root_vdev);
2711 * For the import case, this is done in spa_import(), because
2712 * at this point we're using the spare definitions from
2713 * the MOS config, not necessarily from the userland config.
2715 if (state != SPA_LOAD_IMPORT) {
2716 spa_aux_check_removed(&spa->spa_spares);
2717 spa_aux_check_removed(&spa->spa_l2cache);
2722 * Load the vdev state for all toplevel vdevs.
2727 * Propagate the leaf DTLs we just loaded all the way up the tree.
2729 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2730 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2731 spa_config_exit(spa, SCL_ALL, FTAG);
2734 * Load the DDTs (dedup tables).
2736 error = ddt_load(spa);
2738 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2740 spa_update_dspace(spa);
2743 * Validate the config, using the MOS config to fill in any
2744 * information which might be missing. If we fail to validate
2745 * the config then declare the pool unfit for use. If we're
2746 * assembling a pool from a split, the log is not transferred
2749 if (type != SPA_IMPORT_ASSEMBLE) {
2752 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2753 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2755 if (!spa_config_valid(spa, nvconfig)) {
2756 nvlist_free(nvconfig);
2757 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2760 nvlist_free(nvconfig);
2763 * Now that we've validated the config, check the state of the
2764 * root vdev. If it can't be opened, it indicates one or
2765 * more toplevel vdevs are faulted.
2767 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2768 return (SET_ERROR(ENXIO));
2770 if (spa_writeable(spa) && spa_check_logs(spa)) {
2771 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2772 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2776 if (missing_feat_write) {
2777 ASSERT(state == SPA_LOAD_TRYIMPORT);
2780 * At this point, we know that we can open the pool in
2781 * read-only mode but not read-write mode. We now have enough
2782 * information and can return to userland.
2784 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2788 * We've successfully opened the pool, verify that we're ready
2789 * to start pushing transactions.
2791 if (state != SPA_LOAD_TRYIMPORT) {
2792 if (error = spa_load_verify(spa))
2793 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2797 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2798 spa->spa_load_max_txg == UINT64_MAX)) {
2800 int need_update = B_FALSE;
2801 dsl_pool_t *dp = spa_get_dsl(spa);
2803 ASSERT(state != SPA_LOAD_TRYIMPORT);
2806 * Claim log blocks that haven't been committed yet.
2807 * This must all happen in a single txg.
2808 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2809 * invoked from zil_claim_log_block()'s i/o done callback.
2810 * Price of rollback is that we abandon the log.
2812 spa->spa_claiming = B_TRUE;
2814 tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
2815 (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2816 zil_claim, tx, DS_FIND_CHILDREN);
2819 spa->spa_claiming = B_FALSE;
2821 spa_set_log_state(spa, SPA_LOG_GOOD);
2822 spa->spa_sync_on = B_TRUE;
2823 txg_sync_start(spa->spa_dsl_pool);
2826 * Wait for all claims to sync. We sync up to the highest
2827 * claimed log block birth time so that claimed log blocks
2828 * don't appear to be from the future. spa_claim_max_txg
2829 * will have been set for us by either zil_check_log_chain()
2830 * (invoked from spa_check_logs()) or zil_claim() above.
2832 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2835 * If the config cache is stale, or we have uninitialized
2836 * metaslabs (see spa_vdev_add()), then update the config.
2838 * If this is a verbatim import, trust the current
2839 * in-core spa_config and update the disk labels.
2841 if (config_cache_txg != spa->spa_config_txg ||
2842 state == SPA_LOAD_IMPORT ||
2843 state == SPA_LOAD_RECOVER ||
2844 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2845 need_update = B_TRUE;
2847 for (int c = 0; c < rvd->vdev_children; c++)
2848 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2849 need_update = B_TRUE;
2852 * Update the config cache asychronously in case we're the
2853 * root pool, in which case the config cache isn't writable yet.
2856 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2859 * Check all DTLs to see if anything needs resilvering.
2861 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2862 vdev_resilver_needed(rvd, NULL, NULL))
2863 spa_async_request(spa, SPA_ASYNC_RESILVER);
2866 * Log the fact that we booted up (so that we can detect if
2867 * we rebooted in the middle of an operation).
2869 spa_history_log_version(spa, "open");
2872 * Delete any inconsistent datasets.
2874 (void) dmu_objset_find(spa_name(spa),
2875 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2878 * Clean up any stale temporary dataset userrefs.
2880 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2887 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2889 int mode = spa->spa_mode;
2892 spa_deactivate(spa);
2894 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2896 spa_activate(spa, mode);
2897 spa_async_suspend(spa);
2899 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2903 * If spa_load() fails this function will try loading prior txg's. If
2904 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2905 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2906 * function will not rewind the pool and will return the same error as
2910 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2911 uint64_t max_request, int rewind_flags)
2913 nvlist_t *loadinfo = NULL;
2914 nvlist_t *config = NULL;
2915 int load_error, rewind_error;
2916 uint64_t safe_rewind_txg;
2919 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2920 spa->spa_load_max_txg = spa->spa_load_txg;
2921 spa_set_log_state(spa, SPA_LOG_CLEAR);
2923 spa->spa_load_max_txg = max_request;
2924 if (max_request != UINT64_MAX)
2925 spa->spa_extreme_rewind = B_TRUE;
2928 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2930 if (load_error == 0)
2933 if (spa->spa_root_vdev != NULL)
2934 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2936 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2937 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2939 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2940 nvlist_free(config);
2941 return (load_error);
2944 if (state == SPA_LOAD_RECOVER) {
2945 /* Price of rolling back is discarding txgs, including log */
2946 spa_set_log_state(spa, SPA_LOG_CLEAR);
2949 * If we aren't rolling back save the load info from our first
2950 * import attempt so that we can restore it after attempting
2953 loadinfo = spa->spa_load_info;
2954 spa->spa_load_info = fnvlist_alloc();
2957 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2958 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2959 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2960 TXG_INITIAL : safe_rewind_txg;
2963 * Continue as long as we're finding errors, we're still within
2964 * the acceptable rewind range, and we're still finding uberblocks
2966 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2967 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2968 if (spa->spa_load_max_txg < safe_rewind_txg)
2969 spa->spa_extreme_rewind = B_TRUE;
2970 rewind_error = spa_load_retry(spa, state, mosconfig);
2973 spa->spa_extreme_rewind = B_FALSE;
2974 spa->spa_load_max_txg = UINT64_MAX;
2976 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2977 spa_config_set(spa, config);
2979 if (state == SPA_LOAD_RECOVER) {
2980 ASSERT3P(loadinfo, ==, NULL);
2981 return (rewind_error);
2983 /* Store the rewind info as part of the initial load info */
2984 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2985 spa->spa_load_info);
2987 /* Restore the initial load info */
2988 fnvlist_free(spa->spa_load_info);
2989 spa->spa_load_info = loadinfo;
2991 return (load_error);
2998 * The import case is identical to an open except that the configuration is sent
2999 * down from userland, instead of grabbed from the configuration cache. For the
3000 * case of an open, the pool configuration will exist in the
3001 * POOL_STATE_UNINITIALIZED state.
3003 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3004 * the same time open the pool, without having to keep around the spa_t in some
3008 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
3012 spa_load_state_t state = SPA_LOAD_OPEN;
3014 int locked = B_FALSE;
3015 int firstopen = B_FALSE;
3020 * As disgusting as this is, we need to support recursive calls to this
3021 * function because dsl_dir_open() is called during spa_load(), and ends
3022 * up calling spa_open() again. The real fix is to figure out how to
3023 * avoid dsl_dir_open() calling this in the first place.
3025 if (mutex_owner(&spa_namespace_lock) != curthread) {
3026 mutex_enter(&spa_namespace_lock);
3030 if ((spa = spa_lookup(pool)) == NULL) {
3032 mutex_exit(&spa_namespace_lock);
3033 return (SET_ERROR(ENOENT));
3036 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
3037 zpool_rewind_policy_t policy;
3041 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
3043 if (policy.zrp_request & ZPOOL_DO_REWIND)
3044 state = SPA_LOAD_RECOVER;
3046 spa_activate(spa, spa_mode_global);
3048 if (state != SPA_LOAD_RECOVER)
3049 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3051 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3052 policy.zrp_request);
3054 if (error == EBADF) {
3056 * If vdev_validate() returns failure (indicated by
3057 * EBADF), it indicates that one of the vdevs indicates
3058 * that the pool has been exported or destroyed. If
3059 * this is the case, the config cache is out of sync and
3060 * we should remove the pool from the namespace.
3063 spa_deactivate(spa);
3064 spa_config_sync(spa, B_TRUE, B_TRUE);
3067 mutex_exit(&spa_namespace_lock);
3068 return (SET_ERROR(ENOENT));
3073 * We can't open the pool, but we still have useful
3074 * information: the state of each vdev after the
3075 * attempted vdev_open(). Return this to the user.
3077 if (config != NULL && spa->spa_config) {
3078 VERIFY(nvlist_dup(spa->spa_config, config,
3080 VERIFY(nvlist_add_nvlist(*config,
3081 ZPOOL_CONFIG_LOAD_INFO,
3082 spa->spa_load_info) == 0);
3085 spa_deactivate(spa);
3086 spa->spa_last_open_failed = error;
3088 mutex_exit(&spa_namespace_lock);
3094 spa_open_ref(spa, tag);
3097 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3100 * If we've recovered the pool, pass back any information we
3101 * gathered while doing the load.
3103 if (state == SPA_LOAD_RECOVER) {
3104 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3105 spa->spa_load_info) == 0);
3109 spa->spa_last_open_failed = 0;
3110 spa->spa_last_ubsync_txg = 0;
3111 spa->spa_load_txg = 0;
3112 mutex_exit(&spa_namespace_lock);
3116 zvol_create_minors(spa->spa_name);
3127 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3130 return (spa_open_common(name, spapp, tag, policy, config));
3134 spa_open(const char *name, spa_t **spapp, void *tag)
3136 return (spa_open_common(name, spapp, tag, NULL, NULL));
3140 * Lookup the given spa_t, incrementing the inject count in the process,
3141 * preventing it from being exported or destroyed.
3144 spa_inject_addref(char *name)
3148 mutex_enter(&spa_namespace_lock);
3149 if ((spa = spa_lookup(name)) == NULL) {
3150 mutex_exit(&spa_namespace_lock);
3153 spa->spa_inject_ref++;
3154 mutex_exit(&spa_namespace_lock);
3160 spa_inject_delref(spa_t *spa)
3162 mutex_enter(&spa_namespace_lock);
3163 spa->spa_inject_ref--;
3164 mutex_exit(&spa_namespace_lock);
3168 * Add spares device information to the nvlist.
3171 spa_add_spares(spa_t *spa, nvlist_t *config)
3181 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3183 if (spa->spa_spares.sav_count == 0)
3186 VERIFY(nvlist_lookup_nvlist(config,
3187 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3188 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3189 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3191 VERIFY(nvlist_add_nvlist_array(nvroot,
3192 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3193 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3194 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3197 * Go through and find any spares which have since been
3198 * repurposed as an active spare. If this is the case, update
3199 * their status appropriately.
3201 for (i = 0; i < nspares; i++) {
3202 VERIFY(nvlist_lookup_uint64(spares[i],
3203 ZPOOL_CONFIG_GUID, &guid) == 0);
3204 if (spa_spare_exists(guid, &pool, NULL) &&
3206 VERIFY(nvlist_lookup_uint64_array(
3207 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3208 (uint64_t **)&vs, &vsc) == 0);
3209 vs->vs_state = VDEV_STATE_CANT_OPEN;
3210 vs->vs_aux = VDEV_AUX_SPARED;
3217 * Add l2cache device information to the nvlist, including vdev stats.
3220 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3223 uint_t i, j, nl2cache;
3230 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3232 if (spa->spa_l2cache.sav_count == 0)
3235 VERIFY(nvlist_lookup_nvlist(config,
3236 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3237 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3238 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3239 if (nl2cache != 0) {
3240 VERIFY(nvlist_add_nvlist_array(nvroot,
3241 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3242 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3243 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3246 * Update level 2 cache device stats.
3249 for (i = 0; i < nl2cache; i++) {
3250 VERIFY(nvlist_lookup_uint64(l2cache[i],
3251 ZPOOL_CONFIG_GUID, &guid) == 0);
3254 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3256 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3257 vd = spa->spa_l2cache.sav_vdevs[j];
3263 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3264 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3266 vdev_get_stats(vd, vs);
3272 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3278 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3279 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3281 /* We may be unable to read features if pool is suspended. */
3282 if (spa_suspended(spa))
3285 if (spa->spa_feat_for_read_obj != 0) {
3286 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3287 spa->spa_feat_for_read_obj);
3288 zap_cursor_retrieve(&zc, &za) == 0;
3289 zap_cursor_advance(&zc)) {
3290 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3291 za.za_num_integers == 1);
3292 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3293 za.za_first_integer));
3295 zap_cursor_fini(&zc);
3298 if (spa->spa_feat_for_write_obj != 0) {
3299 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3300 spa->spa_feat_for_write_obj);
3301 zap_cursor_retrieve(&zc, &za) == 0;
3302 zap_cursor_advance(&zc)) {
3303 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3304 za.za_num_integers == 1);
3305 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3306 za.za_first_integer));
3308 zap_cursor_fini(&zc);
3312 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3314 nvlist_free(features);
3318 spa_get_stats(const char *name, nvlist_t **config,
3319 char *altroot, size_t buflen)
3325 error = spa_open_common(name, &spa, FTAG, NULL, config);
3329 * This still leaves a window of inconsistency where the spares
3330 * or l2cache devices could change and the config would be
3331 * self-inconsistent.
3333 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3335 if (*config != NULL) {
3336 uint64_t loadtimes[2];
3338 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3339 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3340 VERIFY(nvlist_add_uint64_array(*config,
3341 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3343 VERIFY(nvlist_add_uint64(*config,
3344 ZPOOL_CONFIG_ERRCOUNT,
3345 spa_get_errlog_size(spa)) == 0);
3347 if (spa_suspended(spa))
3348 VERIFY(nvlist_add_uint64(*config,
3349 ZPOOL_CONFIG_SUSPENDED,
3350 spa->spa_failmode) == 0);
3352 spa_add_spares(spa, *config);
3353 spa_add_l2cache(spa, *config);
3354 spa_add_feature_stats(spa, *config);
3359 * We want to get the alternate root even for faulted pools, so we cheat
3360 * and call spa_lookup() directly.
3364 mutex_enter(&spa_namespace_lock);
3365 spa = spa_lookup(name);
3367 spa_altroot(spa, altroot, buflen);
3371 mutex_exit(&spa_namespace_lock);
3373 spa_altroot(spa, altroot, buflen);
3378 spa_config_exit(spa, SCL_CONFIG, FTAG);
3379 spa_close(spa, FTAG);
3386 * Validate that the auxiliary device array is well formed. We must have an
3387 * array of nvlists, each which describes a valid leaf vdev. If this is an
3388 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3389 * specified, as long as they are well-formed.
3392 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3393 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3394 vdev_labeltype_t label)
3401 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3404 * It's acceptable to have no devs specified.
3406 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3410 return (SET_ERROR(EINVAL));
3413 * Make sure the pool is formatted with a version that supports this
3416 if (spa_version(spa) < version)
3417 return (SET_ERROR(ENOTSUP));
3420 * Set the pending device list so we correctly handle device in-use
3423 sav->sav_pending = dev;
3424 sav->sav_npending = ndev;
3426 for (i = 0; i < ndev; i++) {
3427 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3431 if (!vd->vdev_ops->vdev_op_leaf) {
3433 error = SET_ERROR(EINVAL);
3438 * The L2ARC currently only supports disk devices in
3439 * kernel context. For user-level testing, we allow it.
3442 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3443 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3444 error = SET_ERROR(ENOTBLK);
3451 if ((error = vdev_open(vd)) == 0 &&
3452 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3453 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3454 vd->vdev_guid) == 0);
3460 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3467 sav->sav_pending = NULL;
3468 sav->sav_npending = 0;
3473 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3477 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3479 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3480 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3481 VDEV_LABEL_SPARE)) != 0) {
3485 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3486 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3487 VDEV_LABEL_L2CACHE));
3491 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3496 if (sav->sav_config != NULL) {
3502 * Generate new dev list by concatentating with the
3505 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3506 &olddevs, &oldndevs) == 0);
3508 newdevs = kmem_alloc(sizeof (void *) *
3509 (ndevs + oldndevs), KM_SLEEP);
3510 for (i = 0; i < oldndevs; i++)
3511 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3513 for (i = 0; i < ndevs; i++)
3514 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3517 VERIFY(nvlist_remove(sav->sav_config, config,
3518 DATA_TYPE_NVLIST_ARRAY) == 0);
3520 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3521 config, newdevs, ndevs + oldndevs) == 0);
3522 for (i = 0; i < oldndevs + ndevs; i++)
3523 nvlist_free(newdevs[i]);
3524 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3527 * Generate a new dev list.
3529 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3531 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3537 * Stop and drop level 2 ARC devices
3540 spa_l2cache_drop(spa_t *spa)
3544 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3546 for (i = 0; i < sav->sav_count; i++) {
3549 vd = sav->sav_vdevs[i];
3552 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3553 pool != 0ULL && l2arc_vdev_present(vd))
3554 l2arc_remove_vdev(vd);
3562 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3566 char *altroot = NULL;
3571 uint64_t txg = TXG_INITIAL;
3572 nvlist_t **spares, **l2cache;
3573 uint_t nspares, nl2cache;
3574 uint64_t version, obj;
3575 boolean_t has_features;
3578 * If this pool already exists, return failure.
3580 mutex_enter(&spa_namespace_lock);
3581 if (spa_lookup(pool) != NULL) {
3582 mutex_exit(&spa_namespace_lock);
3583 return (SET_ERROR(EEXIST));
3587 * Allocate a new spa_t structure.
3589 (void) nvlist_lookup_string(props,
3590 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3591 spa = spa_add(pool, NULL, altroot);
3592 spa_activate(spa, spa_mode_global);
3594 if (props && (error = spa_prop_validate(spa, props))) {
3595 spa_deactivate(spa);
3597 mutex_exit(&spa_namespace_lock);
3601 has_features = B_FALSE;
3602 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3603 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3604 if (zpool_prop_feature(nvpair_name(elem)))
3605 has_features = B_TRUE;
3608 if (has_features || nvlist_lookup_uint64(props,
3609 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3610 version = SPA_VERSION;
3612 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3614 spa->spa_first_txg = txg;
3615 spa->spa_uberblock.ub_txg = txg - 1;
3616 spa->spa_uberblock.ub_version = version;
3617 spa->spa_ubsync = spa->spa_uberblock;
3620 * Create "The Godfather" zio to hold all async IOs
3622 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3624 for (int i = 0; i < max_ncpus; i++) {
3625 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3626 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3627 ZIO_FLAG_GODFATHER);
3631 * Create the root vdev.
3633 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3635 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3637 ASSERT(error != 0 || rvd != NULL);
3638 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3640 if (error == 0 && !zfs_allocatable_devs(nvroot))
3641 error = SET_ERROR(EINVAL);
3644 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3645 (error = spa_validate_aux(spa, nvroot, txg,
3646 VDEV_ALLOC_ADD)) == 0) {
3647 for (int c = 0; c < rvd->vdev_children; c++) {
3648 vdev_ashift_optimize(rvd->vdev_child[c]);
3649 vdev_metaslab_set_size(rvd->vdev_child[c]);
3650 vdev_expand(rvd->vdev_child[c], txg);
3654 spa_config_exit(spa, SCL_ALL, FTAG);
3658 spa_deactivate(spa);
3660 mutex_exit(&spa_namespace_lock);
3665 * Get the list of spares, if specified.
3667 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3668 &spares, &nspares) == 0) {
3669 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3671 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3672 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3673 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3674 spa_load_spares(spa);
3675 spa_config_exit(spa, SCL_ALL, FTAG);
3676 spa->spa_spares.sav_sync = B_TRUE;
3680 * Get the list of level 2 cache devices, if specified.
3682 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3683 &l2cache, &nl2cache) == 0) {
3684 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3685 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3686 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3687 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3688 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3689 spa_load_l2cache(spa);
3690 spa_config_exit(spa, SCL_ALL, FTAG);
3691 spa->spa_l2cache.sav_sync = B_TRUE;
3694 spa->spa_is_initializing = B_TRUE;
3695 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3696 spa->spa_meta_objset = dp->dp_meta_objset;
3697 spa->spa_is_initializing = B_FALSE;
3700 * Create DDTs (dedup tables).
3704 spa_update_dspace(spa);
3706 tx = dmu_tx_create_assigned(dp, txg);
3709 * Create the pool config object.
3711 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3712 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3713 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3715 if (zap_add(spa->spa_meta_objset,
3716 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3717 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3718 cmn_err(CE_PANIC, "failed to add pool config");
3721 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3722 spa_feature_create_zap_objects(spa, tx);
3724 if (zap_add(spa->spa_meta_objset,
3725 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3726 sizeof (uint64_t), 1, &version, tx) != 0) {
3727 cmn_err(CE_PANIC, "failed to add pool version");
3730 /* Newly created pools with the right version are always deflated. */
3731 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3732 spa->spa_deflate = TRUE;
3733 if (zap_add(spa->spa_meta_objset,
3734 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3735 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3736 cmn_err(CE_PANIC, "failed to add deflate");
3741 * Create the deferred-free bpobj. Turn off compression
3742 * because sync-to-convergence takes longer if the blocksize
3745 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3746 dmu_object_set_compress(spa->spa_meta_objset, obj,
3747 ZIO_COMPRESS_OFF, tx);
3748 if (zap_add(spa->spa_meta_objset,
3749 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3750 sizeof (uint64_t), 1, &obj, tx) != 0) {
3751 cmn_err(CE_PANIC, "failed to add bpobj");
3753 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3754 spa->spa_meta_objset, obj));
3757 * Create the pool's history object.
3759 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3760 spa_history_create_obj(spa, tx);
3763 * Generate some random noise for salted checksums to operate on.
3765 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
3766 sizeof (spa->spa_cksum_salt.zcs_bytes));
3769 * Set pool properties.
3771 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3772 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3773 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3774 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3776 if (props != NULL) {
3777 spa_configfile_set(spa, props, B_FALSE);
3778 spa_sync_props(props, tx);
3783 spa->spa_sync_on = B_TRUE;
3784 txg_sync_start(spa->spa_dsl_pool);
3787 * We explicitly wait for the first transaction to complete so that our
3788 * bean counters are appropriately updated.
3790 txg_wait_synced(spa->spa_dsl_pool, txg);
3792 spa_config_sync(spa, B_FALSE, B_TRUE);
3793 spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE);
3795 spa_history_log_version(spa, "create");
3798 * Don't count references from objsets that are already closed
3799 * and are making their way through the eviction process.
3801 spa_evicting_os_wait(spa);
3802 spa->spa_minref = refcount_count(&spa->spa_refcount);
3804 mutex_exit(&spa_namespace_lock);
3812 * Get the root pool information from the root disk, then import the root pool
3813 * during the system boot up time.
3815 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3818 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3821 nvlist_t *nvtop, *nvroot;
3824 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3828 * Add this top-level vdev to the child array.
3830 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3832 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3834 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3837 * Put this pool's top-level vdevs into a root vdev.
3839 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3840 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3841 VDEV_TYPE_ROOT) == 0);
3842 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3843 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3844 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3848 * Replace the existing vdev_tree with the new root vdev in
3849 * this pool's configuration (remove the old, add the new).
3851 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3852 nvlist_free(nvroot);
3857 * Walk the vdev tree and see if we can find a device with "better"
3858 * configuration. A configuration is "better" if the label on that
3859 * device has a more recent txg.
3862 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3864 for (int c = 0; c < vd->vdev_children; c++)
3865 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3867 if (vd->vdev_ops->vdev_op_leaf) {
3871 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3875 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3879 * Do we have a better boot device?
3881 if (label_txg > *txg) {
3890 * Import a root pool.
3892 * For x86. devpath_list will consist of devid and/or physpath name of
3893 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3894 * The GRUB "findroot" command will return the vdev we should boot.
3896 * For Sparc, devpath_list consists the physpath name of the booting device
3897 * no matter the rootpool is a single device pool or a mirrored pool.
3899 * "/pci@1f,0/ide@d/disk@0,0:a"
3902 spa_import_rootpool(char *devpath, char *devid)
3905 vdev_t *rvd, *bvd, *avd = NULL;
3906 nvlist_t *config, *nvtop;
3912 * Read the label from the boot device and generate a configuration.
3914 config = spa_generate_rootconf(devpath, devid, &guid);
3915 #if defined(_OBP) && defined(_KERNEL)
3916 if (config == NULL) {
3917 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3919 get_iscsi_bootpath_phy(devpath);
3920 config = spa_generate_rootconf(devpath, devid, &guid);
3924 if (config == NULL) {
3925 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3927 return (SET_ERROR(EIO));
3930 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3932 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3934 mutex_enter(&spa_namespace_lock);
3935 if ((spa = spa_lookup(pname)) != NULL) {
3937 * Remove the existing root pool from the namespace so that we
3938 * can replace it with the correct config we just read in.
3943 spa = spa_add(pname, config, NULL);
3944 spa->spa_is_root = B_TRUE;
3945 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3948 * Build up a vdev tree based on the boot device's label config.
3950 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3952 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3953 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3954 VDEV_ALLOC_ROOTPOOL);
3955 spa_config_exit(spa, SCL_ALL, FTAG);
3957 mutex_exit(&spa_namespace_lock);
3958 nvlist_free(config);
3959 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3965 * Get the boot vdev.
3967 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3968 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3969 (u_longlong_t)guid);
3970 error = SET_ERROR(ENOENT);
3975 * Determine if there is a better boot device.
3978 spa_alt_rootvdev(rvd, &avd, &txg);
3980 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3981 "try booting from '%s'", avd->vdev_path);
3982 error = SET_ERROR(EINVAL);
3987 * If the boot device is part of a spare vdev then ensure that
3988 * we're booting off the active spare.
3990 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3991 !bvd->vdev_isspare) {
3992 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3993 "try booting from '%s'",
3995 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3996 error = SET_ERROR(EINVAL);
4002 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4004 spa_config_exit(spa, SCL_ALL, FTAG);
4005 mutex_exit(&spa_namespace_lock);
4007 nvlist_free(config);
4013 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
4017 spa_generate_rootconf(const char *name)
4019 nvlist_t **configs, **tops;
4021 nvlist_t *best_cfg, *nvtop, *nvroot;
4030 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
4033 ASSERT3U(count, !=, 0);
4035 for (i = 0; i < count; i++) {
4038 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
4040 if (txg > best_txg) {
4042 best_cfg = configs[i];
4047 * Multi-vdev root pool configuration discovery is not supported yet.
4050 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
4052 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
4055 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
4056 for (i = 0; i < nchildren; i++) {
4059 if (configs[i] == NULL)
4061 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
4063 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
4065 for (i = 0; holes != NULL && i < nholes; i++) {
4068 if (tops[holes[i]] != NULL)
4070 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
4071 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
4072 VDEV_TYPE_HOLE) == 0);
4073 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
4075 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4078 for (i = 0; i < nchildren; i++) {
4079 if (tops[i] != NULL)
4081 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4082 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4083 VDEV_TYPE_MISSING) == 0);
4084 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4086 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4091 * Create pool config based on the best vdev config.
4093 nvlist_dup(best_cfg, &config, KM_SLEEP);
4096 * Put this pool's top-level vdevs into a root vdev.
4098 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4100 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4101 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4102 VDEV_TYPE_ROOT) == 0);
4103 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4104 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4105 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4106 tops, nchildren) == 0);
4109 * Replace the existing vdev_tree with the new root vdev in
4110 * this pool's configuration (remove the old, add the new).
4112 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4115 * Drop vdev config elements that should not be present at pool level.
4117 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4118 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4120 for (i = 0; i < count; i++)
4121 nvlist_free(configs[i]);
4122 kmem_free(configs, count * sizeof(void *));
4123 for (i = 0; i < nchildren; i++)
4124 nvlist_free(tops[i]);
4125 kmem_free(tops, nchildren * sizeof(void *));
4126 nvlist_free(nvroot);
4131 spa_import_rootpool(const char *name)
4134 vdev_t *rvd, *bvd, *avd = NULL;
4135 nvlist_t *config, *nvtop;
4141 * Read the label from the boot device and generate a configuration.
4143 config = spa_generate_rootconf(name);
4145 mutex_enter(&spa_namespace_lock);
4146 if (config != NULL) {
4147 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4148 &pname) == 0 && strcmp(name, pname) == 0);
4149 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4152 if ((spa = spa_lookup(pname)) != NULL) {
4154 * Remove the existing root pool from the namespace so
4155 * that we can replace it with the correct config
4160 spa = spa_add(pname, config, NULL);
4163 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4164 * via spa_version().
4166 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4167 &spa->spa_ubsync.ub_version) != 0)
4168 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4169 } else if ((spa = spa_lookup(name)) == NULL) {
4170 mutex_exit(&spa_namespace_lock);
4171 nvlist_free(config);
4172 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4176 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4178 spa->spa_is_root = B_TRUE;
4179 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4182 * Build up a vdev tree based on the boot device's label config.
4184 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4186 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4187 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4188 VDEV_ALLOC_ROOTPOOL);
4189 spa_config_exit(spa, SCL_ALL, FTAG);
4191 mutex_exit(&spa_namespace_lock);
4192 nvlist_free(config);
4193 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4198 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4200 spa_config_exit(spa, SCL_ALL, FTAG);
4201 mutex_exit(&spa_namespace_lock);
4203 nvlist_free(config);
4211 * Import a non-root pool into the system.
4214 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4217 char *altroot = NULL;
4218 spa_load_state_t state = SPA_LOAD_IMPORT;
4219 zpool_rewind_policy_t policy;
4220 uint64_t mode = spa_mode_global;
4221 uint64_t readonly = B_FALSE;
4224 nvlist_t **spares, **l2cache;
4225 uint_t nspares, nl2cache;
4228 * If a pool with this name exists, return failure.
4230 mutex_enter(&spa_namespace_lock);
4231 if (spa_lookup(pool) != NULL) {
4232 mutex_exit(&spa_namespace_lock);
4233 return (SET_ERROR(EEXIST));
4237 * Create and initialize the spa structure.
4239 (void) nvlist_lookup_string(props,
4240 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4241 (void) nvlist_lookup_uint64(props,
4242 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4245 spa = spa_add(pool, config, altroot);
4246 spa->spa_import_flags = flags;
4249 * Verbatim import - Take a pool and insert it into the namespace
4250 * as if it had been loaded at boot.
4252 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4254 spa_configfile_set(spa, props, B_FALSE);
4256 spa_config_sync(spa, B_FALSE, B_TRUE);
4257 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4259 mutex_exit(&spa_namespace_lock);
4263 spa_activate(spa, mode);
4266 * Don't start async tasks until we know everything is healthy.
4268 spa_async_suspend(spa);
4270 zpool_get_rewind_policy(config, &policy);
4271 if (policy.zrp_request & ZPOOL_DO_REWIND)
4272 state = SPA_LOAD_RECOVER;
4275 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4276 * because the user-supplied config is actually the one to trust when
4279 if (state != SPA_LOAD_RECOVER)
4280 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4282 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4283 policy.zrp_request);
4286 * Propagate anything learned while loading the pool and pass it
4287 * back to caller (i.e. rewind info, missing devices, etc).
4289 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4290 spa->spa_load_info) == 0);
4292 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4294 * Toss any existing sparelist, as it doesn't have any validity
4295 * anymore, and conflicts with spa_has_spare().
4297 if (spa->spa_spares.sav_config) {
4298 nvlist_free(spa->spa_spares.sav_config);
4299 spa->spa_spares.sav_config = NULL;
4300 spa_load_spares(spa);
4302 if (spa->spa_l2cache.sav_config) {
4303 nvlist_free(spa->spa_l2cache.sav_config);
4304 spa->spa_l2cache.sav_config = NULL;
4305 spa_load_l2cache(spa);
4308 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4311 error = spa_validate_aux(spa, nvroot, -1ULL,
4314 error = spa_validate_aux(spa, nvroot, -1ULL,
4315 VDEV_ALLOC_L2CACHE);
4316 spa_config_exit(spa, SCL_ALL, FTAG);
4319 spa_configfile_set(spa, props, B_FALSE);
4321 if (error != 0 || (props && spa_writeable(spa) &&
4322 (error = spa_prop_set(spa, props)))) {
4324 spa_deactivate(spa);
4326 mutex_exit(&spa_namespace_lock);
4330 spa_async_resume(spa);
4333 * Override any spares and level 2 cache devices as specified by
4334 * the user, as these may have correct device names/devids, etc.
4336 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4337 &spares, &nspares) == 0) {
4338 if (spa->spa_spares.sav_config)
4339 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4340 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4342 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4343 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4344 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4345 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4346 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4347 spa_load_spares(spa);
4348 spa_config_exit(spa, SCL_ALL, FTAG);
4349 spa->spa_spares.sav_sync = B_TRUE;
4351 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4352 &l2cache, &nl2cache) == 0) {
4353 if (spa->spa_l2cache.sav_config)
4354 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4355 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4357 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4358 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4359 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4360 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4361 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4362 spa_load_l2cache(spa);
4363 spa_config_exit(spa, SCL_ALL, FTAG);
4364 spa->spa_l2cache.sav_sync = B_TRUE;
4368 * Check for any removed devices.
4370 if (spa->spa_autoreplace) {
4371 spa_aux_check_removed(&spa->spa_spares);
4372 spa_aux_check_removed(&spa->spa_l2cache);
4375 if (spa_writeable(spa)) {
4377 * Update the config cache to include the newly-imported pool.
4379 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4383 * It's possible that the pool was expanded while it was exported.
4384 * We kick off an async task to handle this for us.
4386 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4388 spa_history_log_version(spa, "import");
4390 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4392 mutex_exit(&spa_namespace_lock);
4396 zvol_create_minors(pool);
4403 spa_tryimport(nvlist_t *tryconfig)
4405 nvlist_t *config = NULL;
4411 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4414 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4418 * Create and initialize the spa structure.
4420 mutex_enter(&spa_namespace_lock);
4421 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4422 spa_activate(spa, FREAD);
4425 * Pass off the heavy lifting to spa_load().
4426 * Pass TRUE for mosconfig because the user-supplied config
4427 * is actually the one to trust when doing an import.
4429 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4432 * If 'tryconfig' was at least parsable, return the current config.
4434 if (spa->spa_root_vdev != NULL) {
4435 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4436 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4438 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4440 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4441 spa->spa_uberblock.ub_timestamp) == 0);
4442 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4443 spa->spa_load_info) == 0);
4446 * If the bootfs property exists on this pool then we
4447 * copy it out so that external consumers can tell which
4448 * pools are bootable.
4450 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4451 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4454 * We have to play games with the name since the
4455 * pool was opened as TRYIMPORT_NAME.
4457 if (dsl_dsobj_to_dsname(spa_name(spa),
4458 spa->spa_bootfs, tmpname) == 0) {
4460 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4462 cp = strchr(tmpname, '/');
4464 (void) strlcpy(dsname, tmpname,
4467 (void) snprintf(dsname, MAXPATHLEN,
4468 "%s/%s", poolname, ++cp);
4470 VERIFY(nvlist_add_string(config,
4471 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4472 kmem_free(dsname, MAXPATHLEN);
4474 kmem_free(tmpname, MAXPATHLEN);
4478 * Add the list of hot spares and level 2 cache devices.
4480 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4481 spa_add_spares(spa, config);
4482 spa_add_l2cache(spa, config);
4483 spa_config_exit(spa, SCL_CONFIG, FTAG);
4487 spa_deactivate(spa);
4489 mutex_exit(&spa_namespace_lock);
4495 * Pool export/destroy
4497 * The act of destroying or exporting a pool is very simple. We make sure there
4498 * is no more pending I/O and any references to the pool are gone. Then, we
4499 * update the pool state and sync all the labels to disk, removing the
4500 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4501 * we don't sync the labels or remove the configuration cache.
4504 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4505 boolean_t force, boolean_t hardforce)
4512 if (!(spa_mode_global & FWRITE))
4513 return (SET_ERROR(EROFS));
4515 mutex_enter(&spa_namespace_lock);
4516 if ((spa = spa_lookup(pool)) == NULL) {
4517 mutex_exit(&spa_namespace_lock);
4518 return (SET_ERROR(ENOENT));
4522 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4523 * reacquire the namespace lock, and see if we can export.
4525 spa_open_ref(spa, FTAG);
4526 mutex_exit(&spa_namespace_lock);
4527 spa_async_suspend(spa);
4528 mutex_enter(&spa_namespace_lock);
4529 spa_close(spa, FTAG);
4532 * The pool will be in core if it's openable,
4533 * in which case we can modify its state.
4535 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4537 * Objsets may be open only because they're dirty, so we
4538 * have to force it to sync before checking spa_refcnt.
4540 txg_wait_synced(spa->spa_dsl_pool, 0);
4541 spa_evicting_os_wait(spa);
4544 * A pool cannot be exported or destroyed if there are active
4545 * references. If we are resetting a pool, allow references by
4546 * fault injection handlers.
4548 if (!spa_refcount_zero(spa) ||
4549 (spa->spa_inject_ref != 0 &&
4550 new_state != POOL_STATE_UNINITIALIZED)) {
4551 spa_async_resume(spa);
4552 mutex_exit(&spa_namespace_lock);
4553 return (SET_ERROR(EBUSY));
4557 * A pool cannot be exported if it has an active shared spare.
4558 * This is to prevent other pools stealing the active spare
4559 * from an exported pool. At user's own will, such pool can
4560 * be forcedly exported.
4562 if (!force && new_state == POOL_STATE_EXPORTED &&
4563 spa_has_active_shared_spare(spa)) {
4564 spa_async_resume(spa);
4565 mutex_exit(&spa_namespace_lock);
4566 return (SET_ERROR(EXDEV));
4570 * We want this to be reflected on every label,
4571 * so mark them all dirty. spa_unload() will do the
4572 * final sync that pushes these changes out.
4574 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4575 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4576 spa->spa_state = new_state;
4577 spa->spa_final_txg = spa_last_synced_txg(spa) +
4579 vdev_config_dirty(spa->spa_root_vdev);
4580 spa_config_exit(spa, SCL_ALL, FTAG);
4584 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4586 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4588 spa_deactivate(spa);
4591 if (oldconfig && spa->spa_config)
4592 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4594 if (new_state != POOL_STATE_UNINITIALIZED) {
4596 spa_config_sync(spa, B_TRUE, B_TRUE);
4599 mutex_exit(&spa_namespace_lock);
4605 * Destroy a storage pool.
4608 spa_destroy(char *pool)
4610 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4615 * Export a storage pool.
4618 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4619 boolean_t hardforce)
4621 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4626 * Similar to spa_export(), this unloads the spa_t without actually removing it
4627 * from the namespace in any way.
4630 spa_reset(char *pool)
4632 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4637 * ==========================================================================
4638 * Device manipulation
4639 * ==========================================================================
4643 * Add a device to a storage pool.
4646 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4650 vdev_t *rvd = spa->spa_root_vdev;
4652 nvlist_t **spares, **l2cache;
4653 uint_t nspares, nl2cache;
4655 ASSERT(spa_writeable(spa));
4657 txg = spa_vdev_enter(spa);
4659 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4660 VDEV_ALLOC_ADD)) != 0)
4661 return (spa_vdev_exit(spa, NULL, txg, error));
4663 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4665 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4669 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4673 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4674 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4676 if (vd->vdev_children != 0 &&
4677 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4678 return (spa_vdev_exit(spa, vd, txg, error));
4681 * We must validate the spares and l2cache devices after checking the
4682 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4684 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4685 return (spa_vdev_exit(spa, vd, txg, error));
4688 * Transfer each new top-level vdev from vd to rvd.
4690 for (int c = 0; c < vd->vdev_children; c++) {
4693 * Set the vdev id to the first hole, if one exists.
4695 for (id = 0; id < rvd->vdev_children; id++) {
4696 if (rvd->vdev_child[id]->vdev_ishole) {
4697 vdev_free(rvd->vdev_child[id]);
4701 tvd = vd->vdev_child[c];
4702 vdev_remove_child(vd, tvd);
4704 vdev_add_child(rvd, tvd);
4705 vdev_config_dirty(tvd);
4709 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4710 ZPOOL_CONFIG_SPARES);
4711 spa_load_spares(spa);
4712 spa->spa_spares.sav_sync = B_TRUE;
4715 if (nl2cache != 0) {
4716 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4717 ZPOOL_CONFIG_L2CACHE);
4718 spa_load_l2cache(spa);
4719 spa->spa_l2cache.sav_sync = B_TRUE;
4723 * We have to be careful when adding new vdevs to an existing pool.
4724 * If other threads start allocating from these vdevs before we
4725 * sync the config cache, and we lose power, then upon reboot we may
4726 * fail to open the pool because there are DVAs that the config cache
4727 * can't translate. Therefore, we first add the vdevs without
4728 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4729 * and then let spa_config_update() initialize the new metaslabs.
4731 * spa_load() checks for added-but-not-initialized vdevs, so that
4732 * if we lose power at any point in this sequence, the remaining
4733 * steps will be completed the next time we load the pool.
4735 (void) spa_vdev_exit(spa, vd, txg, 0);
4737 mutex_enter(&spa_namespace_lock);
4738 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4739 spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD);
4740 mutex_exit(&spa_namespace_lock);
4746 * Attach a device to a mirror. The arguments are the path to any device
4747 * in the mirror, and the nvroot for the new device. If the path specifies
4748 * a device that is not mirrored, we automatically insert the mirror vdev.
4750 * If 'replacing' is specified, the new device is intended to replace the
4751 * existing device; in this case the two devices are made into their own
4752 * mirror using the 'replacing' vdev, which is functionally identical to
4753 * the mirror vdev (it actually reuses all the same ops) but has a few
4754 * extra rules: you can't attach to it after it's been created, and upon
4755 * completion of resilvering, the first disk (the one being replaced)
4756 * is automatically detached.
4759 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4761 uint64_t txg, dtl_max_txg;
4762 vdev_t *rvd = spa->spa_root_vdev;
4763 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4765 char *oldvdpath, *newvdpath;
4769 ASSERT(spa_writeable(spa));
4771 txg = spa_vdev_enter(spa);
4773 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4776 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4778 if (!oldvd->vdev_ops->vdev_op_leaf)
4779 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4781 pvd = oldvd->vdev_parent;
4783 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4784 VDEV_ALLOC_ATTACH)) != 0)
4785 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4787 if (newrootvd->vdev_children != 1)
4788 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4790 newvd = newrootvd->vdev_child[0];
4792 if (!newvd->vdev_ops->vdev_op_leaf)
4793 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4795 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4796 return (spa_vdev_exit(spa, newrootvd, txg, error));
4799 * Spares can't replace logs
4801 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4802 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4806 * For attach, the only allowable parent is a mirror or the root
4809 if (pvd->vdev_ops != &vdev_mirror_ops &&
4810 pvd->vdev_ops != &vdev_root_ops)
4811 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4813 pvops = &vdev_mirror_ops;
4816 * Active hot spares can only be replaced by inactive hot
4819 if (pvd->vdev_ops == &vdev_spare_ops &&
4820 oldvd->vdev_isspare &&
4821 !spa_has_spare(spa, newvd->vdev_guid))
4822 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4825 * If the source is a hot spare, and the parent isn't already a
4826 * spare, then we want to create a new hot spare. Otherwise, we
4827 * want to create a replacing vdev. The user is not allowed to
4828 * attach to a spared vdev child unless the 'isspare' state is
4829 * the same (spare replaces spare, non-spare replaces
4832 if (pvd->vdev_ops == &vdev_replacing_ops &&
4833 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4834 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4835 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4836 newvd->vdev_isspare != oldvd->vdev_isspare) {
4837 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4840 if (newvd->vdev_isspare)
4841 pvops = &vdev_spare_ops;
4843 pvops = &vdev_replacing_ops;
4847 * Make sure the new device is big enough.
4849 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4850 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4853 * The new device cannot have a higher alignment requirement
4854 * than the top-level vdev.
4856 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4857 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4860 * If this is an in-place replacement, update oldvd's path and devid
4861 * to make it distinguishable from newvd, and unopenable from now on.
4863 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4864 spa_strfree(oldvd->vdev_path);
4865 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4867 (void) sprintf(oldvd->vdev_path, "%s/%s",
4868 newvd->vdev_path, "old");
4869 if (oldvd->vdev_devid != NULL) {
4870 spa_strfree(oldvd->vdev_devid);
4871 oldvd->vdev_devid = NULL;
4875 /* mark the device being resilvered */
4876 newvd->vdev_resilver_txg = txg;
4879 * If the parent is not a mirror, or if we're replacing, insert the new
4880 * mirror/replacing/spare vdev above oldvd.
4882 if (pvd->vdev_ops != pvops)
4883 pvd = vdev_add_parent(oldvd, pvops);
4885 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4886 ASSERT(pvd->vdev_ops == pvops);
4887 ASSERT(oldvd->vdev_parent == pvd);
4890 * Extract the new device from its root and add it to pvd.
4892 vdev_remove_child(newrootvd, newvd);
4893 newvd->vdev_id = pvd->vdev_children;
4894 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4895 vdev_add_child(pvd, newvd);
4897 tvd = newvd->vdev_top;
4898 ASSERT(pvd->vdev_top == tvd);
4899 ASSERT(tvd->vdev_parent == rvd);
4901 vdev_config_dirty(tvd);
4904 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4905 * for any dmu_sync-ed blocks. It will propagate upward when
4906 * spa_vdev_exit() calls vdev_dtl_reassess().
4908 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4910 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4911 dtl_max_txg - TXG_INITIAL);
4913 if (newvd->vdev_isspare) {
4914 spa_spare_activate(newvd);
4915 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4918 oldvdpath = spa_strdup(oldvd->vdev_path);
4919 newvdpath = spa_strdup(newvd->vdev_path);
4920 newvd_isspare = newvd->vdev_isspare;
4923 * Mark newvd's DTL dirty in this txg.
4925 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4928 * Schedule the resilver to restart in the future. We do this to
4929 * ensure that dmu_sync-ed blocks have been stitched into the
4930 * respective datasets.
4932 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4934 if (spa->spa_bootfs)
4935 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4937 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH);
4942 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4944 spa_history_log_internal(spa, "vdev attach", NULL,
4945 "%s vdev=%s %s vdev=%s",
4946 replacing && newvd_isspare ? "spare in" :
4947 replacing ? "replace" : "attach", newvdpath,
4948 replacing ? "for" : "to", oldvdpath);
4950 spa_strfree(oldvdpath);
4951 spa_strfree(newvdpath);
4957 * Detach a device from a mirror or replacing vdev.
4959 * If 'replace_done' is specified, only detach if the parent
4960 * is a replacing vdev.
4963 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4967 vdev_t *rvd = spa->spa_root_vdev;
4968 vdev_t *vd, *pvd, *cvd, *tvd;
4969 boolean_t unspare = B_FALSE;
4970 uint64_t unspare_guid = 0;
4973 ASSERT(spa_writeable(spa));
4975 txg = spa_vdev_enter(spa);
4977 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4980 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4982 if (!vd->vdev_ops->vdev_op_leaf)
4983 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4985 pvd = vd->vdev_parent;
4988 * If the parent/child relationship is not as expected, don't do it.
4989 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4990 * vdev that's replacing B with C. The user's intent in replacing
4991 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4992 * the replace by detaching C, the expected behavior is to end up
4993 * M(A,B). But suppose that right after deciding to detach C,
4994 * the replacement of B completes. We would have M(A,C), and then
4995 * ask to detach C, which would leave us with just A -- not what
4996 * the user wanted. To prevent this, we make sure that the
4997 * parent/child relationship hasn't changed -- in this example,
4998 * that C's parent is still the replacing vdev R.
5000 if (pvd->vdev_guid != pguid && pguid != 0)
5001 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5004 * Only 'replacing' or 'spare' vdevs can be replaced.
5006 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
5007 pvd->vdev_ops != &vdev_spare_ops)
5008 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5010 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
5011 spa_version(spa) >= SPA_VERSION_SPARES);
5014 * Only mirror, replacing, and spare vdevs support detach.
5016 if (pvd->vdev_ops != &vdev_replacing_ops &&
5017 pvd->vdev_ops != &vdev_mirror_ops &&
5018 pvd->vdev_ops != &vdev_spare_ops)
5019 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5022 * If this device has the only valid copy of some data,
5023 * we cannot safely detach it.
5025 if (vdev_dtl_required(vd))
5026 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5028 ASSERT(pvd->vdev_children >= 2);
5031 * If we are detaching the second disk from a replacing vdev, then
5032 * check to see if we changed the original vdev's path to have "/old"
5033 * at the end in spa_vdev_attach(). If so, undo that change now.
5035 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
5036 vd->vdev_path != NULL) {
5037 size_t len = strlen(vd->vdev_path);
5039 for (int c = 0; c < pvd->vdev_children; c++) {
5040 cvd = pvd->vdev_child[c];
5042 if (cvd == vd || cvd->vdev_path == NULL)
5045 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
5046 strcmp(cvd->vdev_path + len, "/old") == 0) {
5047 spa_strfree(cvd->vdev_path);
5048 cvd->vdev_path = spa_strdup(vd->vdev_path);
5055 * If we are detaching the original disk from a spare, then it implies
5056 * that the spare should become a real disk, and be removed from the
5057 * active spare list for the pool.
5059 if (pvd->vdev_ops == &vdev_spare_ops &&
5061 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
5065 * Erase the disk labels so the disk can be used for other things.
5066 * This must be done after all other error cases are handled,
5067 * but before we disembowel vd (so we can still do I/O to it).
5068 * But if we can't do it, don't treat the error as fatal --
5069 * it may be that the unwritability of the disk is the reason
5070 * it's being detached!
5072 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5075 * Remove vd from its parent and compact the parent's children.
5077 vdev_remove_child(pvd, vd);
5078 vdev_compact_children(pvd);
5081 * Remember one of the remaining children so we can get tvd below.
5083 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5086 * If we need to remove the remaining child from the list of hot spares,
5087 * do it now, marking the vdev as no longer a spare in the process.
5088 * We must do this before vdev_remove_parent(), because that can
5089 * change the GUID if it creates a new toplevel GUID. For a similar
5090 * reason, we must remove the spare now, in the same txg as the detach;
5091 * otherwise someone could attach a new sibling, change the GUID, and
5092 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5095 ASSERT(cvd->vdev_isspare);
5096 spa_spare_remove(cvd);
5097 unspare_guid = cvd->vdev_guid;
5098 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5099 cvd->vdev_unspare = B_TRUE;
5103 * If the parent mirror/replacing vdev only has one child,
5104 * the parent is no longer needed. Remove it from the tree.
5106 if (pvd->vdev_children == 1) {
5107 if (pvd->vdev_ops == &vdev_spare_ops)
5108 cvd->vdev_unspare = B_FALSE;
5109 vdev_remove_parent(cvd);
5114 * We don't set tvd until now because the parent we just removed
5115 * may have been the previous top-level vdev.
5117 tvd = cvd->vdev_top;
5118 ASSERT(tvd->vdev_parent == rvd);
5121 * Reevaluate the parent vdev state.
5123 vdev_propagate_state(cvd);
5126 * If the 'autoexpand' property is set on the pool then automatically
5127 * try to expand the size of the pool. For example if the device we
5128 * just detached was smaller than the others, it may be possible to
5129 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5130 * first so that we can obtain the updated sizes of the leaf vdevs.
5132 if (spa->spa_autoexpand) {
5134 vdev_expand(tvd, txg);
5137 vdev_config_dirty(tvd);
5140 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5141 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5142 * But first make sure we're not on any *other* txg's DTL list, to
5143 * prevent vd from being accessed after it's freed.
5145 vdpath = spa_strdup(vd->vdev_path);
5146 for (int t = 0; t < TXG_SIZE; t++)
5147 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5148 vd->vdev_detached = B_TRUE;
5149 vdev_dirty(tvd, VDD_DTL, vd, txg);
5151 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5153 /* hang on to the spa before we release the lock */
5154 spa_open_ref(spa, FTAG);
5156 error = spa_vdev_exit(spa, vd, txg, 0);
5158 spa_history_log_internal(spa, "detach", NULL,
5160 spa_strfree(vdpath);
5163 * If this was the removal of the original device in a hot spare vdev,
5164 * then we want to go through and remove the device from the hot spare
5165 * list of every other pool.
5168 spa_t *altspa = NULL;
5170 mutex_enter(&spa_namespace_lock);
5171 while ((altspa = spa_next(altspa)) != NULL) {
5172 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5176 spa_open_ref(altspa, FTAG);
5177 mutex_exit(&spa_namespace_lock);
5178 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5179 mutex_enter(&spa_namespace_lock);
5180 spa_close(altspa, FTAG);
5182 mutex_exit(&spa_namespace_lock);
5184 /* search the rest of the vdevs for spares to remove */
5185 spa_vdev_resilver_done(spa);
5188 /* all done with the spa; OK to release */
5189 mutex_enter(&spa_namespace_lock);
5190 spa_close(spa, FTAG);
5191 mutex_exit(&spa_namespace_lock);
5197 * Split a set of devices from their mirrors, and create a new pool from them.
5200 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5201 nvlist_t *props, boolean_t exp)
5204 uint64_t txg, *glist;
5206 uint_t c, children, lastlog;
5207 nvlist_t **child, *nvl, *tmp;
5209 char *altroot = NULL;
5210 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5211 boolean_t activate_slog;
5213 ASSERT(spa_writeable(spa));
5215 txg = spa_vdev_enter(spa);
5217 /* clear the log and flush everything up to now */
5218 activate_slog = spa_passivate_log(spa);
5219 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5220 error = spa_offline_log(spa);
5221 txg = spa_vdev_config_enter(spa);
5224 spa_activate_log(spa);
5227 return (spa_vdev_exit(spa, NULL, txg, error));
5229 /* check new spa name before going any further */
5230 if (spa_lookup(newname) != NULL)
5231 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5234 * scan through all the children to ensure they're all mirrors
5236 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5237 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5239 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5241 /* first, check to ensure we've got the right child count */
5242 rvd = spa->spa_root_vdev;
5244 for (c = 0; c < rvd->vdev_children; c++) {
5245 vdev_t *vd = rvd->vdev_child[c];
5247 /* don't count the holes & logs as children */
5248 if (vd->vdev_islog || vd->vdev_ishole) {
5256 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5257 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5259 /* next, ensure no spare or cache devices are part of the split */
5260 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5261 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5262 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5264 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5265 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5267 /* then, loop over each vdev and validate it */
5268 for (c = 0; c < children; c++) {
5269 uint64_t is_hole = 0;
5271 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5275 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5276 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5279 error = SET_ERROR(EINVAL);
5284 /* which disk is going to be split? */
5285 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5287 error = SET_ERROR(EINVAL);
5291 /* look it up in the spa */
5292 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5293 if (vml[c] == NULL) {
5294 error = SET_ERROR(ENODEV);
5298 /* make sure there's nothing stopping the split */
5299 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5300 vml[c]->vdev_islog ||
5301 vml[c]->vdev_ishole ||
5302 vml[c]->vdev_isspare ||
5303 vml[c]->vdev_isl2cache ||
5304 !vdev_writeable(vml[c]) ||
5305 vml[c]->vdev_children != 0 ||
5306 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5307 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5308 error = SET_ERROR(EINVAL);
5312 if (vdev_dtl_required(vml[c])) {
5313 error = SET_ERROR(EBUSY);
5317 /* we need certain info from the top level */
5318 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5319 vml[c]->vdev_top->vdev_ms_array) == 0);
5320 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5321 vml[c]->vdev_top->vdev_ms_shift) == 0);
5322 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5323 vml[c]->vdev_top->vdev_asize) == 0);
5324 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5325 vml[c]->vdev_top->vdev_ashift) == 0);
5329 kmem_free(vml, children * sizeof (vdev_t *));
5330 kmem_free(glist, children * sizeof (uint64_t));
5331 return (spa_vdev_exit(spa, NULL, txg, error));
5334 /* stop writers from using the disks */
5335 for (c = 0; c < children; c++) {
5337 vml[c]->vdev_offline = B_TRUE;
5339 vdev_reopen(spa->spa_root_vdev);
5342 * Temporarily record the splitting vdevs in the spa config. This
5343 * will disappear once the config is regenerated.
5345 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5346 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5347 glist, children) == 0);
5348 kmem_free(glist, children * sizeof (uint64_t));
5350 mutex_enter(&spa->spa_props_lock);
5351 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5353 mutex_exit(&spa->spa_props_lock);
5354 spa->spa_config_splitting = nvl;
5355 vdev_config_dirty(spa->spa_root_vdev);
5357 /* configure and create the new pool */
5358 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5359 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5360 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5361 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5362 spa_version(spa)) == 0);
5363 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5364 spa->spa_config_txg) == 0);
5365 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5366 spa_generate_guid(NULL)) == 0);
5367 (void) nvlist_lookup_string(props,
5368 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5370 /* add the new pool to the namespace */
5371 newspa = spa_add(newname, config, altroot);
5372 newspa->spa_config_txg = spa->spa_config_txg;
5373 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5375 /* release the spa config lock, retaining the namespace lock */
5376 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5378 if (zio_injection_enabled)
5379 zio_handle_panic_injection(spa, FTAG, 1);
5381 spa_activate(newspa, spa_mode_global);
5382 spa_async_suspend(newspa);
5385 /* mark that we are creating new spa by splitting */
5386 newspa->spa_splitting_newspa = B_TRUE;
5388 /* create the new pool from the disks of the original pool */
5389 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5391 newspa->spa_splitting_newspa = B_FALSE;
5396 /* if that worked, generate a real config for the new pool */
5397 if (newspa->spa_root_vdev != NULL) {
5398 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5399 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5400 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5401 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5402 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5407 if (props != NULL) {
5408 spa_configfile_set(newspa, props, B_FALSE);
5409 error = spa_prop_set(newspa, props);
5414 /* flush everything */
5415 txg = spa_vdev_config_enter(newspa);
5416 vdev_config_dirty(newspa->spa_root_vdev);
5417 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5419 if (zio_injection_enabled)
5420 zio_handle_panic_injection(spa, FTAG, 2);
5422 spa_async_resume(newspa);
5424 /* finally, update the original pool's config */
5425 txg = spa_vdev_config_enter(spa);
5426 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5427 error = dmu_tx_assign(tx, TXG_WAIT);
5430 for (c = 0; c < children; c++) {
5431 if (vml[c] != NULL) {
5434 spa_history_log_internal(spa, "detach", tx,
5435 "vdev=%s", vml[c]->vdev_path);
5439 vdev_config_dirty(spa->spa_root_vdev);
5440 spa->spa_config_splitting = NULL;
5444 (void) spa_vdev_exit(spa, NULL, txg, 0);
5446 if (zio_injection_enabled)
5447 zio_handle_panic_injection(spa, FTAG, 3);
5449 /* split is complete; log a history record */
5450 spa_history_log_internal(newspa, "split", NULL,
5451 "from pool %s", spa_name(spa));
5453 kmem_free(vml, children * sizeof (vdev_t *));
5455 /* if we're not going to mount the filesystems in userland, export */
5457 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5464 spa_deactivate(newspa);
5467 txg = spa_vdev_config_enter(spa);
5469 /* re-online all offlined disks */
5470 for (c = 0; c < children; c++) {
5472 vml[c]->vdev_offline = B_FALSE;
5474 vdev_reopen(spa->spa_root_vdev);
5476 nvlist_free(spa->spa_config_splitting);
5477 spa->spa_config_splitting = NULL;
5478 (void) spa_vdev_exit(spa, NULL, txg, error);
5480 kmem_free(vml, children * sizeof (vdev_t *));
5485 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5487 for (int i = 0; i < count; i++) {
5490 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5493 if (guid == target_guid)
5501 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5502 nvlist_t *dev_to_remove)
5504 nvlist_t **newdev = NULL;
5507 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5509 for (int i = 0, j = 0; i < count; i++) {
5510 if (dev[i] == dev_to_remove)
5512 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5515 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5516 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5518 for (int i = 0; i < count - 1; i++)
5519 nvlist_free(newdev[i]);
5522 kmem_free(newdev, (count - 1) * sizeof (void *));
5526 * Evacuate the device.
5529 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5534 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5535 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5536 ASSERT(vd == vd->vdev_top);
5539 * Evacuate the device. We don't hold the config lock as writer
5540 * since we need to do I/O but we do keep the
5541 * spa_namespace_lock held. Once this completes the device
5542 * should no longer have any blocks allocated on it.
5544 if (vd->vdev_islog) {
5545 if (vd->vdev_stat.vs_alloc != 0)
5546 error = spa_offline_log(spa);
5548 error = SET_ERROR(ENOTSUP);
5555 * The evacuation succeeded. Remove any remaining MOS metadata
5556 * associated with this vdev, and wait for these changes to sync.
5558 ASSERT0(vd->vdev_stat.vs_alloc);
5559 txg = spa_vdev_config_enter(spa);
5560 vd->vdev_removing = B_TRUE;
5561 vdev_dirty_leaves(vd, VDD_DTL, txg);
5562 vdev_config_dirty(vd);
5563 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5569 * Complete the removal by cleaning up the namespace.
5572 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5574 vdev_t *rvd = spa->spa_root_vdev;
5575 uint64_t id = vd->vdev_id;
5576 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5578 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5579 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5580 ASSERT(vd == vd->vdev_top);
5583 * Only remove any devices which are empty.
5585 if (vd->vdev_stat.vs_alloc != 0)
5588 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5590 if (list_link_active(&vd->vdev_state_dirty_node))
5591 vdev_state_clean(vd);
5592 if (list_link_active(&vd->vdev_config_dirty_node))
5593 vdev_config_clean(vd);
5598 vdev_compact_children(rvd);
5600 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5601 vdev_add_child(rvd, vd);
5603 vdev_config_dirty(rvd);
5606 * Reassess the health of our root vdev.
5612 * Remove a device from the pool -
5614 * Removing a device from the vdev namespace requires several steps
5615 * and can take a significant amount of time. As a result we use
5616 * the spa_vdev_config_[enter/exit] functions which allow us to
5617 * grab and release the spa_config_lock while still holding the namespace
5618 * lock. During each step the configuration is synced out.
5620 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5624 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5627 metaslab_group_t *mg;
5628 nvlist_t **spares, **l2cache, *nv;
5630 uint_t nspares, nl2cache;
5632 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5634 ASSERT(spa_writeable(spa));
5637 txg = spa_vdev_enter(spa);
5639 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5641 if (spa->spa_spares.sav_vdevs != NULL &&
5642 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5643 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5644 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5646 * Only remove the hot spare if it's not currently in use
5649 if (vd == NULL || unspare) {
5650 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5651 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5652 spa_load_spares(spa);
5653 spa->spa_spares.sav_sync = B_TRUE;
5655 error = SET_ERROR(EBUSY);
5657 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5658 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5659 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5660 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5662 * Cache devices can always be removed.
5664 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5665 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5666 spa_load_l2cache(spa);
5667 spa->spa_l2cache.sav_sync = B_TRUE;
5668 } else if (vd != NULL && vd->vdev_islog) {
5670 ASSERT(vd == vd->vdev_top);
5675 * Stop allocating from this vdev.
5677 metaslab_group_passivate(mg);
5680 * Wait for the youngest allocations and frees to sync,
5681 * and then wait for the deferral of those frees to finish.
5683 spa_vdev_config_exit(spa, NULL,
5684 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5687 * Attempt to evacuate the vdev.
5689 error = spa_vdev_remove_evacuate(spa, vd);
5691 txg = spa_vdev_config_enter(spa);
5694 * If we couldn't evacuate the vdev, unwind.
5697 metaslab_group_activate(mg);
5698 return (spa_vdev_exit(spa, NULL, txg, error));
5702 * Clean up the vdev namespace.
5704 spa_vdev_remove_from_namespace(spa, vd);
5706 } else if (vd != NULL) {
5708 * Normal vdevs cannot be removed (yet).
5710 error = SET_ERROR(ENOTSUP);
5713 * There is no vdev of any kind with the specified guid.
5715 error = SET_ERROR(ENOENT);
5719 return (spa_vdev_exit(spa, NULL, txg, error));
5725 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5726 * currently spared, so we can detach it.
5729 spa_vdev_resilver_done_hunt(vdev_t *vd)
5731 vdev_t *newvd, *oldvd;
5733 for (int c = 0; c < vd->vdev_children; c++) {
5734 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5740 * Check for a completed replacement. We always consider the first
5741 * vdev in the list to be the oldest vdev, and the last one to be
5742 * the newest (see spa_vdev_attach() for how that works). In
5743 * the case where the newest vdev is faulted, we will not automatically
5744 * remove it after a resilver completes. This is OK as it will require
5745 * user intervention to determine which disk the admin wishes to keep.
5747 if (vd->vdev_ops == &vdev_replacing_ops) {
5748 ASSERT(vd->vdev_children > 1);
5750 newvd = vd->vdev_child[vd->vdev_children - 1];
5751 oldvd = vd->vdev_child[0];
5753 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5754 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5755 !vdev_dtl_required(oldvd))
5760 * Check for a completed resilver with the 'unspare' flag set.
5762 if (vd->vdev_ops == &vdev_spare_ops) {
5763 vdev_t *first = vd->vdev_child[0];
5764 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5766 if (last->vdev_unspare) {
5769 } else if (first->vdev_unspare) {
5776 if (oldvd != NULL &&
5777 vdev_dtl_empty(newvd, DTL_MISSING) &&
5778 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5779 !vdev_dtl_required(oldvd))
5783 * If there are more than two spares attached to a disk,
5784 * and those spares are not required, then we want to
5785 * attempt to free them up now so that they can be used
5786 * by other pools. Once we're back down to a single
5787 * disk+spare, we stop removing them.
5789 if (vd->vdev_children > 2) {
5790 newvd = vd->vdev_child[1];
5792 if (newvd->vdev_isspare && last->vdev_isspare &&
5793 vdev_dtl_empty(last, DTL_MISSING) &&
5794 vdev_dtl_empty(last, DTL_OUTAGE) &&
5795 !vdev_dtl_required(newvd))
5804 spa_vdev_resilver_done(spa_t *spa)
5806 vdev_t *vd, *pvd, *ppvd;
5807 uint64_t guid, sguid, pguid, ppguid;
5809 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5811 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5812 pvd = vd->vdev_parent;
5813 ppvd = pvd->vdev_parent;
5814 guid = vd->vdev_guid;
5815 pguid = pvd->vdev_guid;
5816 ppguid = ppvd->vdev_guid;
5819 * If we have just finished replacing a hot spared device, then
5820 * we need to detach the parent's first child (the original hot
5823 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5824 ppvd->vdev_children == 2) {
5825 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5826 sguid = ppvd->vdev_child[1]->vdev_guid;
5828 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5830 spa_config_exit(spa, SCL_ALL, FTAG);
5831 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5833 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5835 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5838 spa_config_exit(spa, SCL_ALL, FTAG);
5842 * Update the stored path or FRU for this vdev.
5845 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5849 boolean_t sync = B_FALSE;
5851 ASSERT(spa_writeable(spa));
5853 spa_vdev_state_enter(spa, SCL_ALL);
5855 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5856 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5858 if (!vd->vdev_ops->vdev_op_leaf)
5859 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5862 if (strcmp(value, vd->vdev_path) != 0) {
5863 spa_strfree(vd->vdev_path);
5864 vd->vdev_path = spa_strdup(value);
5868 if (vd->vdev_fru == NULL) {
5869 vd->vdev_fru = spa_strdup(value);
5871 } else if (strcmp(value, vd->vdev_fru) != 0) {
5872 spa_strfree(vd->vdev_fru);
5873 vd->vdev_fru = spa_strdup(value);
5878 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5882 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5884 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5888 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5890 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5894 * ==========================================================================
5896 * ==========================================================================
5900 spa_scan_stop(spa_t *spa)
5902 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5903 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5904 return (SET_ERROR(EBUSY));
5905 return (dsl_scan_cancel(spa->spa_dsl_pool));
5909 spa_scan(spa_t *spa, pool_scan_func_t func)
5911 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5913 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5914 return (SET_ERROR(ENOTSUP));
5917 * If a resilver was requested, but there is no DTL on a
5918 * writeable leaf device, we have nothing to do.
5920 if (func == POOL_SCAN_RESILVER &&
5921 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5922 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5926 return (dsl_scan(spa->spa_dsl_pool, func));
5930 * ==========================================================================
5931 * SPA async task processing
5932 * ==========================================================================
5936 spa_async_remove(spa_t *spa, vdev_t *vd)
5938 if (vd->vdev_remove_wanted) {
5939 vd->vdev_remove_wanted = B_FALSE;
5940 vd->vdev_delayed_close = B_FALSE;
5941 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5944 * We want to clear the stats, but we don't want to do a full
5945 * vdev_clear() as that will cause us to throw away
5946 * degraded/faulted state as well as attempt to reopen the
5947 * device, all of which is a waste.
5949 vd->vdev_stat.vs_read_errors = 0;
5950 vd->vdev_stat.vs_write_errors = 0;
5951 vd->vdev_stat.vs_checksum_errors = 0;
5953 vdev_state_dirty(vd->vdev_top);
5954 /* Tell userspace that the vdev is gone. */
5955 zfs_post_remove(spa, vd);
5958 for (int c = 0; c < vd->vdev_children; c++)
5959 spa_async_remove(spa, vd->vdev_child[c]);
5963 spa_async_probe(spa_t *spa, vdev_t *vd)
5965 if (vd->vdev_probe_wanted) {
5966 vd->vdev_probe_wanted = B_FALSE;
5967 vdev_reopen(vd); /* vdev_open() does the actual probe */
5970 for (int c = 0; c < vd->vdev_children; c++)
5971 spa_async_probe(spa, vd->vdev_child[c]);
5975 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5981 if (!spa->spa_autoexpand)
5984 for (int c = 0; c < vd->vdev_children; c++) {
5985 vdev_t *cvd = vd->vdev_child[c];
5986 spa_async_autoexpand(spa, cvd);
5989 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5992 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5993 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5995 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5996 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5998 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5999 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
6002 kmem_free(physpath, MAXPATHLEN);
6006 spa_async_thread(void *arg)
6011 ASSERT(spa->spa_sync_on);
6013 mutex_enter(&spa->spa_async_lock);
6014 tasks = spa->spa_async_tasks;
6015 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
6016 mutex_exit(&spa->spa_async_lock);
6019 * See if the config needs to be updated.
6021 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
6022 uint64_t old_space, new_space;
6024 mutex_enter(&spa_namespace_lock);
6025 old_space = metaslab_class_get_space(spa_normal_class(spa));
6026 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
6027 new_space = metaslab_class_get_space(spa_normal_class(spa));
6028 mutex_exit(&spa_namespace_lock);
6031 * If the pool grew as a result of the config update,
6032 * then log an internal history event.
6034 if (new_space != old_space) {
6035 spa_history_log_internal(spa, "vdev online", NULL,
6036 "pool '%s' size: %llu(+%llu)",
6037 spa_name(spa), new_space, new_space - old_space);
6041 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
6042 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6043 spa_async_autoexpand(spa, spa->spa_root_vdev);
6044 spa_config_exit(spa, SCL_CONFIG, FTAG);
6048 * See if any devices need to be probed.
6050 if (tasks & SPA_ASYNC_PROBE) {
6051 spa_vdev_state_enter(spa, SCL_NONE);
6052 spa_async_probe(spa, spa->spa_root_vdev);
6053 (void) spa_vdev_state_exit(spa, NULL, 0);
6057 * If any devices are done replacing, detach them.
6059 if (tasks & SPA_ASYNC_RESILVER_DONE)
6060 spa_vdev_resilver_done(spa);
6063 * Kick off a resilver.
6065 if (tasks & SPA_ASYNC_RESILVER)
6066 dsl_resilver_restart(spa->spa_dsl_pool, 0);
6069 * Let the world know that we're done.
6071 mutex_enter(&spa->spa_async_lock);
6072 spa->spa_async_thread = NULL;
6073 cv_broadcast(&spa->spa_async_cv);
6074 mutex_exit(&spa->spa_async_lock);
6079 spa_async_thread_vd(void *arg)
6084 ASSERT(spa->spa_sync_on);
6086 mutex_enter(&spa->spa_async_lock);
6087 tasks = spa->spa_async_tasks;
6089 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6090 mutex_exit(&spa->spa_async_lock);
6093 * See if any devices need to be marked REMOVED.
6095 if (tasks & SPA_ASYNC_REMOVE) {
6096 spa_vdev_state_enter(spa, SCL_NONE);
6097 spa_async_remove(spa, spa->spa_root_vdev);
6098 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6099 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6100 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6101 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6102 (void) spa_vdev_state_exit(spa, NULL, 0);
6106 * Let the world know that we're done.
6108 mutex_enter(&spa->spa_async_lock);
6109 tasks = spa->spa_async_tasks;
6110 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6112 spa->spa_async_thread_vd = NULL;
6113 cv_broadcast(&spa->spa_async_cv);
6114 mutex_exit(&spa->spa_async_lock);
6119 spa_async_suspend(spa_t *spa)
6121 mutex_enter(&spa->spa_async_lock);
6122 spa->spa_async_suspended++;
6123 while (spa->spa_async_thread != NULL &&
6124 spa->spa_async_thread_vd != NULL)
6125 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6126 mutex_exit(&spa->spa_async_lock);
6130 spa_async_resume(spa_t *spa)
6132 mutex_enter(&spa->spa_async_lock);
6133 ASSERT(spa->spa_async_suspended != 0);
6134 spa->spa_async_suspended--;
6135 mutex_exit(&spa->spa_async_lock);
6139 spa_async_tasks_pending(spa_t *spa)
6141 uint_t non_config_tasks;
6143 boolean_t config_task_suspended;
6145 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6147 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6148 if (spa->spa_ccw_fail_time == 0) {
6149 config_task_suspended = B_FALSE;
6151 config_task_suspended =
6152 (gethrtime() - spa->spa_ccw_fail_time) <
6153 (zfs_ccw_retry_interval * NANOSEC);
6156 return (non_config_tasks || (config_task && !config_task_suspended));
6160 spa_async_dispatch(spa_t *spa)
6162 mutex_enter(&spa->spa_async_lock);
6163 if (spa_async_tasks_pending(spa) &&
6164 !spa->spa_async_suspended &&
6165 spa->spa_async_thread == NULL &&
6167 spa->spa_async_thread = thread_create(NULL, 0,
6168 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6169 mutex_exit(&spa->spa_async_lock);
6173 spa_async_dispatch_vd(spa_t *spa)
6175 mutex_enter(&spa->spa_async_lock);
6176 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6177 !spa->spa_async_suspended &&
6178 spa->spa_async_thread_vd == NULL &&
6180 spa->spa_async_thread_vd = thread_create(NULL, 0,
6181 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6182 mutex_exit(&spa->spa_async_lock);
6186 spa_async_request(spa_t *spa, int task)
6188 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6189 mutex_enter(&spa->spa_async_lock);
6190 spa->spa_async_tasks |= task;
6191 mutex_exit(&spa->spa_async_lock);
6192 spa_async_dispatch_vd(spa);
6196 * ==========================================================================
6197 * SPA syncing routines
6198 * ==========================================================================
6202 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6205 bpobj_enqueue(bpo, bp, tx);
6210 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6214 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6215 BP_GET_PSIZE(bp), zio->io_flags));
6220 * Note: this simple function is not inlined to make it easier to dtrace the
6221 * amount of time spent syncing frees.
6224 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6226 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6227 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6228 VERIFY(zio_wait(zio) == 0);
6232 * Note: this simple function is not inlined to make it easier to dtrace the
6233 * amount of time spent syncing deferred frees.
6236 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6238 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6239 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6240 spa_free_sync_cb, zio, tx), ==, 0);
6241 VERIFY0(zio_wait(zio));
6246 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6248 char *packed = NULL;
6253 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6256 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6257 * information. This avoids the dmu_buf_will_dirty() path and
6258 * saves us a pre-read to get data we don't actually care about.
6260 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6261 packed = kmem_alloc(bufsize, KM_SLEEP);
6263 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6265 bzero(packed + nvsize, bufsize - nvsize);
6267 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6269 kmem_free(packed, bufsize);
6271 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6272 dmu_buf_will_dirty(db, tx);
6273 *(uint64_t *)db->db_data = nvsize;
6274 dmu_buf_rele(db, FTAG);
6278 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6279 const char *config, const char *entry)
6289 * Update the MOS nvlist describing the list of available devices.
6290 * spa_validate_aux() will have already made sure this nvlist is
6291 * valid and the vdevs are labeled appropriately.
6293 if (sav->sav_object == 0) {
6294 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6295 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6296 sizeof (uint64_t), tx);
6297 VERIFY(zap_update(spa->spa_meta_objset,
6298 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6299 &sav->sav_object, tx) == 0);
6302 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6303 if (sav->sav_count == 0) {
6304 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6306 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6307 for (i = 0; i < sav->sav_count; i++)
6308 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6309 B_FALSE, VDEV_CONFIG_L2CACHE);
6310 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6311 sav->sav_count) == 0);
6312 for (i = 0; i < sav->sav_count; i++)
6313 nvlist_free(list[i]);
6314 kmem_free(list, sav->sav_count * sizeof (void *));
6317 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6318 nvlist_free(nvroot);
6320 sav->sav_sync = B_FALSE;
6324 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6328 if (list_is_empty(&spa->spa_config_dirty_list))
6331 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6333 config = spa_config_generate(spa, spa->spa_root_vdev,
6334 dmu_tx_get_txg(tx), B_FALSE);
6337 * If we're upgrading the spa version then make sure that
6338 * the config object gets updated with the correct version.
6340 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6341 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6342 spa->spa_uberblock.ub_version);
6344 spa_config_exit(spa, SCL_STATE, FTAG);
6346 if (spa->spa_config_syncing)
6347 nvlist_free(spa->spa_config_syncing);
6348 spa->spa_config_syncing = config;
6350 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6354 spa_sync_version(void *arg, dmu_tx_t *tx)
6356 uint64_t *versionp = arg;
6357 uint64_t version = *versionp;
6358 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6361 * Setting the version is special cased when first creating the pool.
6363 ASSERT(tx->tx_txg != TXG_INITIAL);
6365 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6366 ASSERT(version >= spa_version(spa));
6368 spa->spa_uberblock.ub_version = version;
6369 vdev_config_dirty(spa->spa_root_vdev);
6370 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6374 * Set zpool properties.
6377 spa_sync_props(void *arg, dmu_tx_t *tx)
6379 nvlist_t *nvp = arg;
6380 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6381 objset_t *mos = spa->spa_meta_objset;
6382 nvpair_t *elem = NULL;
6384 mutex_enter(&spa->spa_props_lock);
6386 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6388 char *strval, *fname;
6390 const char *propname;
6391 zprop_type_t proptype;
6394 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6397 * We checked this earlier in spa_prop_validate().
6399 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6401 fname = strchr(nvpair_name(elem), '@') + 1;
6402 VERIFY0(zfeature_lookup_name(fname, &fid));
6404 spa_feature_enable(spa, fid, tx);
6405 spa_history_log_internal(spa, "set", tx,
6406 "%s=enabled", nvpair_name(elem));
6409 case ZPOOL_PROP_VERSION:
6410 intval = fnvpair_value_uint64(elem);
6412 * The version is synced seperatly before other
6413 * properties and should be correct by now.
6415 ASSERT3U(spa_version(spa), >=, intval);
6418 case ZPOOL_PROP_ALTROOT:
6420 * 'altroot' is a non-persistent property. It should
6421 * have been set temporarily at creation or import time.
6423 ASSERT(spa->spa_root != NULL);
6426 case ZPOOL_PROP_READONLY:
6427 case ZPOOL_PROP_CACHEFILE:
6429 * 'readonly' and 'cachefile' are also non-persisitent
6433 case ZPOOL_PROP_COMMENT:
6434 strval = fnvpair_value_string(elem);
6435 if (spa->spa_comment != NULL)
6436 spa_strfree(spa->spa_comment);
6437 spa->spa_comment = spa_strdup(strval);
6439 * We need to dirty the configuration on all the vdevs
6440 * so that their labels get updated. It's unnecessary
6441 * to do this for pool creation since the vdev's
6442 * configuratoin has already been dirtied.
6444 if (tx->tx_txg != TXG_INITIAL)
6445 vdev_config_dirty(spa->spa_root_vdev);
6446 spa_history_log_internal(spa, "set", tx,
6447 "%s=%s", nvpair_name(elem), strval);
6451 * Set pool property values in the poolprops mos object.
6453 if (spa->spa_pool_props_object == 0) {
6454 spa->spa_pool_props_object =
6455 zap_create_link(mos, DMU_OT_POOL_PROPS,
6456 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6460 /* normalize the property name */
6461 propname = zpool_prop_to_name(prop);
6462 proptype = zpool_prop_get_type(prop);
6464 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6465 ASSERT(proptype == PROP_TYPE_STRING);
6466 strval = fnvpair_value_string(elem);
6467 VERIFY0(zap_update(mos,
6468 spa->spa_pool_props_object, propname,
6469 1, strlen(strval) + 1, strval, tx));
6470 spa_history_log_internal(spa, "set", tx,
6471 "%s=%s", nvpair_name(elem), strval);
6472 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6473 intval = fnvpair_value_uint64(elem);
6475 if (proptype == PROP_TYPE_INDEX) {
6477 VERIFY0(zpool_prop_index_to_string(
6478 prop, intval, &unused));
6480 VERIFY0(zap_update(mos,
6481 spa->spa_pool_props_object, propname,
6482 8, 1, &intval, tx));
6483 spa_history_log_internal(spa, "set", tx,
6484 "%s=%lld", nvpair_name(elem), intval);
6486 ASSERT(0); /* not allowed */
6490 case ZPOOL_PROP_DELEGATION:
6491 spa->spa_delegation = intval;
6493 case ZPOOL_PROP_BOOTFS:
6494 spa->spa_bootfs = intval;
6496 case ZPOOL_PROP_FAILUREMODE:
6497 spa->spa_failmode = intval;
6499 case ZPOOL_PROP_AUTOEXPAND:
6500 spa->spa_autoexpand = intval;
6501 if (tx->tx_txg != TXG_INITIAL)
6502 spa_async_request(spa,
6503 SPA_ASYNC_AUTOEXPAND);
6505 case ZPOOL_PROP_DEDUPDITTO:
6506 spa->spa_dedup_ditto = intval;
6515 mutex_exit(&spa->spa_props_lock);
6519 * Perform one-time upgrade on-disk changes. spa_version() does not
6520 * reflect the new version this txg, so there must be no changes this
6521 * txg to anything that the upgrade code depends on after it executes.
6522 * Therefore this must be called after dsl_pool_sync() does the sync
6526 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6528 dsl_pool_t *dp = spa->spa_dsl_pool;
6530 ASSERT(spa->spa_sync_pass == 1);
6532 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6534 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6535 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6536 dsl_pool_create_origin(dp, tx);
6538 /* Keeping the origin open increases spa_minref */
6539 spa->spa_minref += 3;
6542 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6543 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6544 dsl_pool_upgrade_clones(dp, tx);
6547 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6548 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6549 dsl_pool_upgrade_dir_clones(dp, tx);
6551 /* Keeping the freedir open increases spa_minref */
6552 spa->spa_minref += 3;
6555 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6556 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6557 spa_feature_create_zap_objects(spa, tx);
6561 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6562 * when possibility to use lz4 compression for metadata was added
6563 * Old pools that have this feature enabled must be upgraded to have
6564 * this feature active
6566 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6567 boolean_t lz4_en = spa_feature_is_enabled(spa,
6568 SPA_FEATURE_LZ4_COMPRESS);
6569 boolean_t lz4_ac = spa_feature_is_active(spa,
6570 SPA_FEATURE_LZ4_COMPRESS);
6572 if (lz4_en && !lz4_ac)
6573 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6577 * If we haven't written the salt, do so now. Note that the
6578 * feature may not be activated yet, but that's fine since
6579 * the presence of this ZAP entry is backwards compatible.
6581 if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
6582 DMU_POOL_CHECKSUM_SALT) == ENOENT) {
6583 VERIFY0(zap_add(spa->spa_meta_objset,
6584 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
6585 sizeof (spa->spa_cksum_salt.zcs_bytes),
6586 spa->spa_cksum_salt.zcs_bytes, tx));
6589 rrw_exit(&dp->dp_config_rwlock, FTAG);
6593 * Sync the specified transaction group. New blocks may be dirtied as
6594 * part of the process, so we iterate until it converges.
6597 spa_sync(spa_t *spa, uint64_t txg)
6599 dsl_pool_t *dp = spa->spa_dsl_pool;
6600 objset_t *mos = spa->spa_meta_objset;
6601 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6602 vdev_t *rvd = spa->spa_root_vdev;
6607 VERIFY(spa_writeable(spa));
6610 * Lock out configuration changes.
6612 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6614 spa->spa_syncing_txg = txg;
6615 spa->spa_sync_pass = 0;
6618 * If there are any pending vdev state changes, convert them
6619 * into config changes that go out with this transaction group.
6621 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6622 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6624 * We need the write lock here because, for aux vdevs,
6625 * calling vdev_config_dirty() modifies sav_config.
6626 * This is ugly and will become unnecessary when we
6627 * eliminate the aux vdev wart by integrating all vdevs
6628 * into the root vdev tree.
6630 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6631 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6632 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6633 vdev_state_clean(vd);
6634 vdev_config_dirty(vd);
6636 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6637 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6639 spa_config_exit(spa, SCL_STATE, FTAG);
6641 tx = dmu_tx_create_assigned(dp, txg);
6643 spa->spa_sync_starttime = gethrtime();
6645 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6646 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6649 callout_reset(&spa->spa_deadman_cycid,
6650 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6655 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6656 * set spa_deflate if we have no raid-z vdevs.
6658 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6659 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6662 for (i = 0; i < rvd->vdev_children; i++) {
6663 vd = rvd->vdev_child[i];
6664 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6667 if (i == rvd->vdev_children) {
6668 spa->spa_deflate = TRUE;
6669 VERIFY(0 == zap_add(spa->spa_meta_objset,
6670 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6671 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6676 * Iterate to convergence.
6679 int pass = ++spa->spa_sync_pass;
6681 spa_sync_config_object(spa, tx);
6682 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6683 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6684 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6685 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6686 spa_errlog_sync(spa, txg);
6687 dsl_pool_sync(dp, txg);
6689 if (pass < zfs_sync_pass_deferred_free) {
6690 spa_sync_frees(spa, free_bpl, tx);
6693 * We can not defer frees in pass 1, because
6694 * we sync the deferred frees later in pass 1.
6696 ASSERT3U(pass, >, 1);
6697 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6698 &spa->spa_deferred_bpobj, tx);
6702 dsl_scan_sync(dp, tx);
6704 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6708 spa_sync_upgrades(spa, tx);
6710 spa->spa_uberblock.ub_rootbp.blk_birth);
6712 * Note: We need to check if the MOS is dirty
6713 * because we could have marked the MOS dirty
6714 * without updating the uberblock (e.g. if we
6715 * have sync tasks but no dirty user data). We
6716 * need to check the uberblock's rootbp because
6717 * it is updated if we have synced out dirty
6718 * data (though in this case the MOS will most
6719 * likely also be dirty due to second order
6720 * effects, we don't want to rely on that here).
6722 if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6723 !dmu_objset_is_dirty(mos, txg)) {
6725 * Nothing changed on the first pass,
6726 * therefore this TXG is a no-op. Avoid
6727 * syncing deferred frees, so that we
6728 * can keep this TXG as a no-op.
6730 ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6732 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6733 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
6736 spa_sync_deferred_frees(spa, tx);
6739 } while (dmu_objset_is_dirty(mos, txg));
6742 * Rewrite the vdev configuration (which includes the uberblock)
6743 * to commit the transaction group.
6745 * If there are no dirty vdevs, we sync the uberblock to a few
6746 * random top-level vdevs that are known to be visible in the
6747 * config cache (see spa_vdev_add() for a complete description).
6748 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6752 * We hold SCL_STATE to prevent vdev open/close/etc.
6753 * while we're attempting to write the vdev labels.
6755 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6757 if (list_is_empty(&spa->spa_config_dirty_list)) {
6758 vdev_t *svd[SPA_DVAS_PER_BP];
6760 int children = rvd->vdev_children;
6761 int c0 = spa_get_random(children);
6763 for (int c = 0; c < children; c++) {
6764 vd = rvd->vdev_child[(c0 + c) % children];
6765 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6767 svd[svdcount++] = vd;
6768 if (svdcount == SPA_DVAS_PER_BP)
6771 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6773 error = vdev_config_sync(svd, svdcount, txg,
6776 error = vdev_config_sync(rvd->vdev_child,
6777 rvd->vdev_children, txg, B_FALSE);
6779 error = vdev_config_sync(rvd->vdev_child,
6780 rvd->vdev_children, txg, B_TRUE);
6784 spa->spa_last_synced_guid = rvd->vdev_guid;
6786 spa_config_exit(spa, SCL_STATE, FTAG);
6790 zio_suspend(spa, NULL);
6791 zio_resume_wait(spa);
6796 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6799 callout_drain(&spa->spa_deadman_cycid);
6804 * Clear the dirty config list.
6806 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6807 vdev_config_clean(vd);
6810 * Now that the new config has synced transactionally,
6811 * let it become visible to the config cache.
6813 if (spa->spa_config_syncing != NULL) {
6814 spa_config_set(spa, spa->spa_config_syncing);
6815 spa->spa_config_txg = txg;
6816 spa->spa_config_syncing = NULL;
6819 spa->spa_ubsync = spa->spa_uberblock;
6821 dsl_pool_sync_done(dp, txg);
6824 * Update usable space statistics.
6826 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6827 vdev_sync_done(vd, txg);
6829 spa_update_dspace(spa);
6832 * It had better be the case that we didn't dirty anything
6833 * since vdev_config_sync().
6835 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6836 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6837 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6839 spa->spa_sync_pass = 0;
6841 spa_config_exit(spa, SCL_CONFIG, FTAG);
6843 spa_handle_ignored_writes(spa);
6846 * If any async tasks have been requested, kick them off.
6848 spa_async_dispatch(spa);
6849 spa_async_dispatch_vd(spa);
6853 * Sync all pools. We don't want to hold the namespace lock across these
6854 * operations, so we take a reference on the spa_t and drop the lock during the
6858 spa_sync_allpools(void)
6861 mutex_enter(&spa_namespace_lock);
6862 while ((spa = spa_next(spa)) != NULL) {
6863 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6864 !spa_writeable(spa) || spa_suspended(spa))
6866 spa_open_ref(spa, FTAG);
6867 mutex_exit(&spa_namespace_lock);
6868 txg_wait_synced(spa_get_dsl(spa), 0);
6869 mutex_enter(&spa_namespace_lock);
6870 spa_close(spa, FTAG);
6872 mutex_exit(&spa_namespace_lock);
6876 * ==========================================================================
6877 * Miscellaneous routines
6878 * ==========================================================================
6882 * Remove all pools in the system.
6890 * Remove all cached state. All pools should be closed now,
6891 * so every spa in the AVL tree should be unreferenced.
6893 mutex_enter(&spa_namespace_lock);
6894 while ((spa = spa_next(NULL)) != NULL) {
6896 * Stop async tasks. The async thread may need to detach
6897 * a device that's been replaced, which requires grabbing
6898 * spa_namespace_lock, so we must drop it here.
6900 spa_open_ref(spa, FTAG);
6901 mutex_exit(&spa_namespace_lock);
6902 spa_async_suspend(spa);
6903 mutex_enter(&spa_namespace_lock);
6904 spa_close(spa, FTAG);
6906 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6908 spa_deactivate(spa);
6912 mutex_exit(&spa_namespace_lock);
6916 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6921 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6925 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6926 vd = spa->spa_l2cache.sav_vdevs[i];
6927 if (vd->vdev_guid == guid)
6931 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6932 vd = spa->spa_spares.sav_vdevs[i];
6933 if (vd->vdev_guid == guid)
6942 spa_upgrade(spa_t *spa, uint64_t version)
6944 ASSERT(spa_writeable(spa));
6946 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6949 * This should only be called for a non-faulted pool, and since a
6950 * future version would result in an unopenable pool, this shouldn't be
6953 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6954 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6956 spa->spa_uberblock.ub_version = version;
6957 vdev_config_dirty(spa->spa_root_vdev);
6959 spa_config_exit(spa, SCL_ALL, FTAG);
6961 txg_wait_synced(spa_get_dsl(spa), 0);
6965 spa_has_spare(spa_t *spa, uint64_t guid)
6969 spa_aux_vdev_t *sav = &spa->spa_spares;
6971 for (i = 0; i < sav->sav_count; i++)
6972 if (sav->sav_vdevs[i]->vdev_guid == guid)
6975 for (i = 0; i < sav->sav_npending; i++) {
6976 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6977 &spareguid) == 0 && spareguid == guid)
6985 * Check if a pool has an active shared spare device.
6986 * Note: reference count of an active spare is 2, as a spare and as a replace
6989 spa_has_active_shared_spare(spa_t *spa)
6993 spa_aux_vdev_t *sav = &spa->spa_spares;
6995 for (i = 0; i < sav->sav_count; i++) {
6996 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6997 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
7006 * Post a sysevent corresponding to the given event. The 'name' must be one of
7007 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
7008 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7009 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7010 * or zdb as real changes.
7013 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
7017 sysevent_attr_list_t *attr = NULL;
7018 sysevent_value_t value;
7021 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
7024 value.value_type = SE_DATA_TYPE_STRING;
7025 value.value.sv_string = spa_name(spa);
7026 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
7029 value.value_type = SE_DATA_TYPE_UINT64;
7030 value.value.sv_uint64 = spa_guid(spa);
7031 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
7035 value.value_type = SE_DATA_TYPE_UINT64;
7036 value.value.sv_uint64 = vd->vdev_guid;
7037 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
7041 if (vd->vdev_path) {
7042 value.value_type = SE_DATA_TYPE_STRING;
7043 value.value.sv_string = vd->vdev_path;
7044 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
7045 &value, SE_SLEEP) != 0)
7050 if (sysevent_attach_attributes(ev, attr) != 0)
7054 (void) log_sysevent(ev, SE_SLEEP, &eid);
7058 sysevent_free_attr(attr);