4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 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.
29 * Copyright (c) 2014 Integros [integros.com]
33 * SPA: Storage Pool Allocator
35 * This file contains all the routines used when modifying on-disk SPA state.
36 * This includes opening, importing, destroying, exporting a pool, and syncing a
40 #include <sys/zfs_context.h>
41 #include <sys/fm/fs/zfs.h>
42 #include <sys/spa_impl.h>
44 #include <sys/zio_checksum.h>
46 #include <sys/dmu_tx.h>
50 #include <sys/vdev_impl.h>
51 #include <sys/metaslab.h>
52 #include <sys/metaslab_impl.h>
53 #include <sys/uberblock_impl.h>
56 #include <sys/dmu_traverse.h>
57 #include <sys/dmu_objset.h>
58 #include <sys/unique.h>
59 #include <sys/dsl_pool.h>
60 #include <sys/dsl_dataset.h>
61 #include <sys/dsl_dir.h>
62 #include <sys/dsl_prop.h>
63 #include <sys/dsl_synctask.h>
64 #include <sys/fs/zfs.h>
66 #include <sys/callb.h>
67 #include <sys/spa_boot.h>
68 #include <sys/zfs_ioctl.h>
69 #include <sys/dsl_scan.h>
70 #include <sys/dmu_send.h>
71 #include <sys/dsl_destroy.h>
72 #include <sys/dsl_userhold.h>
73 #include <sys/zfeature.h>
75 #include <sys/trim_map.h>
78 #include <sys/callb.h>
79 #include <sys/cpupart.h>
84 #include "zfs_comutil.h"
86 /* Check hostid on import? */
87 static int check_hostid = 1;
90 * The interval, in seconds, at which failed configuration cache file writes
93 static int zfs_ccw_retry_interval = 300;
95 SYSCTL_DECL(_vfs_zfs);
96 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
97 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RWTUN, &check_hostid, 0,
98 "Check hostid on import?");
99 TUNABLE_INT("vfs.zfs.ccw_retry_interval", &zfs_ccw_retry_interval);
100 SYSCTL_INT(_vfs_zfs, OID_AUTO, ccw_retry_interval, CTLFLAG_RW,
101 &zfs_ccw_retry_interval, 0,
102 "Configuration cache file write, retry after failure, interval (seconds)");
104 typedef enum zti_modes {
105 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
106 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
107 ZTI_MODE_NULL, /* don't create a taskq */
111 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
112 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
113 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
115 #define ZTI_N(n) ZTI_P(n, 1)
116 #define ZTI_ONE ZTI_N(1)
118 typedef struct zio_taskq_info {
119 zti_modes_t zti_mode;
124 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
125 "issue", "issue_high", "intr", "intr_high"
129 * This table defines the taskq settings for each ZFS I/O type. When
130 * initializing a pool, we use this table to create an appropriately sized
131 * taskq. Some operations are low volume and therefore have a small, static
132 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
133 * macros. Other operations process a large amount of data; the ZTI_BATCH
134 * macro causes us to create a taskq oriented for throughput. Some operations
135 * are so high frequency and short-lived that the taskq itself can become a a
136 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
137 * additional degree of parallelism specified by the number of threads per-
138 * taskq and the number of taskqs; when dispatching an event in this case, the
139 * particular taskq is chosen at random.
141 * The different taskq priorities are to handle the different contexts (issue
142 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
143 * need to be handled with minimum delay.
145 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
146 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
147 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
148 { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */
149 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
150 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
151 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
152 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
155 static void spa_sync_version(void *arg, dmu_tx_t *tx);
156 static void spa_sync_props(void *arg, dmu_tx_t *tx);
157 static boolean_t spa_has_active_shared_spare(spa_t *spa);
158 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
159 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
161 static void spa_vdev_resilver_done(spa_t *spa);
163 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
165 id_t zio_taskq_psrset_bind = PS_NONE;
168 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
170 uint_t zio_taskq_basedc = 80; /* base duty cycle */
172 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
173 extern int zfs_sync_pass_deferred_free;
176 extern void spa_deadman(void *arg);
180 * This (illegal) pool name is used when temporarily importing a spa_t in order
181 * to get the vdev stats associated with the imported devices.
183 #define TRYIMPORT_NAME "$import"
186 * ==========================================================================
187 * SPA properties routines
188 * ==========================================================================
192 * Add a (source=src, propname=propval) list to an nvlist.
195 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
196 uint64_t intval, zprop_source_t src)
198 const char *propname = zpool_prop_to_name(prop);
201 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
202 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
205 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
207 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
209 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
210 nvlist_free(propval);
214 * Get property values from the spa configuration.
217 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
219 vdev_t *rvd = spa->spa_root_vdev;
220 dsl_pool_t *pool = spa->spa_dsl_pool;
221 uint64_t size, alloc, cap, version;
222 zprop_source_t src = ZPROP_SRC_NONE;
223 spa_config_dirent_t *dp;
224 metaslab_class_t *mc = spa_normal_class(spa);
226 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
229 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
230 size = metaslab_class_get_space(spa_normal_class(spa));
231 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
232 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
233 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
234 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
237 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
238 metaslab_class_fragmentation(mc), src);
239 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
240 metaslab_class_expandable_space(mc), src);
241 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
242 (spa_mode(spa) == FREAD), src);
244 cap = (size == 0) ? 0 : (alloc * 100 / size);
245 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
247 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
248 ddt_get_pool_dedup_ratio(spa), src);
250 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
251 rvd->vdev_state, src);
253 version = spa_version(spa);
254 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
255 src = ZPROP_SRC_DEFAULT;
257 src = ZPROP_SRC_LOCAL;
258 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
263 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
264 * when opening pools before this version freedir will be NULL.
266 if (pool->dp_free_dir != NULL) {
267 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
268 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
271 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
275 if (pool->dp_leak_dir != NULL) {
276 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
277 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
280 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
285 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
287 if (spa->spa_comment != NULL) {
288 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
292 if (spa->spa_root != NULL)
293 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
296 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
297 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
298 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
300 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
301 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
304 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
305 if (dp->scd_path == NULL) {
306 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
307 "none", 0, ZPROP_SRC_LOCAL);
308 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
309 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
310 dp->scd_path, 0, ZPROP_SRC_LOCAL);
316 * Get zpool property values.
319 spa_prop_get(spa_t *spa, nvlist_t **nvp)
321 objset_t *mos = spa->spa_meta_objset;
326 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
328 mutex_enter(&spa->spa_props_lock);
331 * Get properties from the spa config.
333 spa_prop_get_config(spa, nvp);
335 /* If no pool property object, no more prop to get. */
336 if (mos == NULL || spa->spa_pool_props_object == 0) {
337 mutex_exit(&spa->spa_props_lock);
342 * Get properties from the MOS pool property object.
344 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
345 (err = zap_cursor_retrieve(&zc, &za)) == 0;
346 zap_cursor_advance(&zc)) {
349 zprop_source_t src = ZPROP_SRC_DEFAULT;
352 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
355 switch (za.za_integer_length) {
357 /* integer property */
358 if (za.za_first_integer !=
359 zpool_prop_default_numeric(prop))
360 src = ZPROP_SRC_LOCAL;
362 if (prop == ZPOOL_PROP_BOOTFS) {
364 dsl_dataset_t *ds = NULL;
366 dp = spa_get_dsl(spa);
367 dsl_pool_config_enter(dp, FTAG);
368 if (err = dsl_dataset_hold_obj(dp,
369 za.za_first_integer, FTAG, &ds)) {
370 dsl_pool_config_exit(dp, FTAG);
375 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
377 dsl_dataset_name(ds, strval);
378 dsl_dataset_rele(ds, FTAG);
379 dsl_pool_config_exit(dp, FTAG);
382 intval = za.za_first_integer;
385 spa_prop_add_list(*nvp, prop, strval, intval, src);
389 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
394 /* string property */
395 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
396 err = zap_lookup(mos, spa->spa_pool_props_object,
397 za.za_name, 1, za.za_num_integers, strval);
399 kmem_free(strval, za.za_num_integers);
402 spa_prop_add_list(*nvp, prop, strval, 0, src);
403 kmem_free(strval, za.za_num_integers);
410 zap_cursor_fini(&zc);
411 mutex_exit(&spa->spa_props_lock);
413 if (err && err != ENOENT) {
423 * Validate the given pool properties nvlist and modify the list
424 * for the property values to be set.
427 spa_prop_validate(spa_t *spa, nvlist_t *props)
430 int error = 0, reset_bootfs = 0;
432 boolean_t has_feature = B_FALSE;
435 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
437 char *strval, *slash, *check, *fname;
438 const char *propname = nvpair_name(elem);
439 zpool_prop_t prop = zpool_name_to_prop(propname);
443 if (!zpool_prop_feature(propname)) {
444 error = SET_ERROR(EINVAL);
449 * Sanitize the input.
451 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
452 error = SET_ERROR(EINVAL);
456 if (nvpair_value_uint64(elem, &intval) != 0) {
457 error = SET_ERROR(EINVAL);
462 error = SET_ERROR(EINVAL);
466 fname = strchr(propname, '@') + 1;
467 if (zfeature_lookup_name(fname, NULL) != 0) {
468 error = SET_ERROR(EINVAL);
472 has_feature = B_TRUE;
475 case ZPOOL_PROP_VERSION:
476 error = nvpair_value_uint64(elem, &intval);
478 (intval < spa_version(spa) ||
479 intval > SPA_VERSION_BEFORE_FEATURES ||
481 error = SET_ERROR(EINVAL);
484 case ZPOOL_PROP_DELEGATION:
485 case ZPOOL_PROP_AUTOREPLACE:
486 case ZPOOL_PROP_LISTSNAPS:
487 case ZPOOL_PROP_AUTOEXPAND:
488 error = nvpair_value_uint64(elem, &intval);
489 if (!error && intval > 1)
490 error = SET_ERROR(EINVAL);
493 case ZPOOL_PROP_BOOTFS:
495 * If the pool version is less than SPA_VERSION_BOOTFS,
496 * or the pool is still being created (version == 0),
497 * the bootfs property cannot be set.
499 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
500 error = SET_ERROR(ENOTSUP);
505 * Make sure the vdev config is bootable
507 if (!vdev_is_bootable(spa->spa_root_vdev)) {
508 error = SET_ERROR(ENOTSUP);
514 error = nvpair_value_string(elem, &strval);
520 if (strval == NULL || strval[0] == '\0') {
521 objnum = zpool_prop_default_numeric(
526 if (error = dmu_objset_hold(strval, FTAG, &os))
530 * Must be ZPL, and its property settings
531 * must be supported by GRUB (compression
532 * is not gzip, and large blocks are not used).
535 if (dmu_objset_type(os) != DMU_OST_ZFS) {
536 error = SET_ERROR(ENOTSUP);
538 dsl_prop_get_int_ds(dmu_objset_ds(os),
539 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
541 !BOOTFS_COMPRESS_VALID(propval)) {
542 error = SET_ERROR(ENOTSUP);
544 dsl_prop_get_int_ds(dmu_objset_ds(os),
545 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
547 propval > SPA_OLD_MAXBLOCKSIZE) {
548 error = SET_ERROR(ENOTSUP);
550 objnum = dmu_objset_id(os);
552 dmu_objset_rele(os, FTAG);
556 case ZPOOL_PROP_FAILUREMODE:
557 error = nvpair_value_uint64(elem, &intval);
558 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
559 intval > ZIO_FAILURE_MODE_PANIC))
560 error = SET_ERROR(EINVAL);
563 * This is a special case which only occurs when
564 * the pool has completely failed. This allows
565 * the user to change the in-core failmode property
566 * without syncing it out to disk (I/Os might
567 * currently be blocked). We do this by returning
568 * EIO to the caller (spa_prop_set) to trick it
569 * into thinking we encountered a property validation
572 if (!error && spa_suspended(spa)) {
573 spa->spa_failmode = intval;
574 error = SET_ERROR(EIO);
578 case ZPOOL_PROP_CACHEFILE:
579 if ((error = nvpair_value_string(elem, &strval)) != 0)
582 if (strval[0] == '\0')
585 if (strcmp(strval, "none") == 0)
588 if (strval[0] != '/') {
589 error = SET_ERROR(EINVAL);
593 slash = strrchr(strval, '/');
594 ASSERT(slash != NULL);
596 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
597 strcmp(slash, "/..") == 0)
598 error = SET_ERROR(EINVAL);
601 case ZPOOL_PROP_COMMENT:
602 if ((error = nvpair_value_string(elem, &strval)) != 0)
604 for (check = strval; *check != '\0'; check++) {
606 * The kernel doesn't have an easy isprint()
607 * check. For this kernel check, we merely
608 * check ASCII apart from DEL. Fix this if
609 * there is an easy-to-use kernel isprint().
611 if (*check >= 0x7f) {
612 error = SET_ERROR(EINVAL);
616 if (strlen(strval) > ZPROP_MAX_COMMENT)
620 case ZPOOL_PROP_DEDUPDITTO:
621 if (spa_version(spa) < SPA_VERSION_DEDUP)
622 error = SET_ERROR(ENOTSUP);
624 error = nvpair_value_uint64(elem, &intval);
626 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
627 error = SET_ERROR(EINVAL);
635 if (!error && reset_bootfs) {
636 error = nvlist_remove(props,
637 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
640 error = nvlist_add_uint64(props,
641 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
649 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
652 spa_config_dirent_t *dp;
654 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
658 dp = kmem_alloc(sizeof (spa_config_dirent_t),
661 if (cachefile[0] == '\0')
662 dp->scd_path = spa_strdup(spa_config_path);
663 else if (strcmp(cachefile, "none") == 0)
666 dp->scd_path = spa_strdup(cachefile);
668 list_insert_head(&spa->spa_config_list, dp);
670 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
674 spa_prop_set(spa_t *spa, nvlist_t *nvp)
677 nvpair_t *elem = NULL;
678 boolean_t need_sync = B_FALSE;
680 if ((error = spa_prop_validate(spa, nvp)) != 0)
683 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
684 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
686 if (prop == ZPOOL_PROP_CACHEFILE ||
687 prop == ZPOOL_PROP_ALTROOT ||
688 prop == ZPOOL_PROP_READONLY)
691 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
694 if (prop == ZPOOL_PROP_VERSION) {
695 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
697 ASSERT(zpool_prop_feature(nvpair_name(elem)));
698 ver = SPA_VERSION_FEATURES;
702 /* Save time if the version is already set. */
703 if (ver == spa_version(spa))
707 * In addition to the pool directory object, we might
708 * create the pool properties object, the features for
709 * read object, the features for write object, or the
710 * feature descriptions object.
712 error = dsl_sync_task(spa->spa_name, NULL,
713 spa_sync_version, &ver,
714 6, ZFS_SPACE_CHECK_RESERVED);
725 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
726 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
733 * If the bootfs property value is dsobj, clear it.
736 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
738 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
739 VERIFY(zap_remove(spa->spa_meta_objset,
740 spa->spa_pool_props_object,
741 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
748 spa_change_guid_check(void *arg, dmu_tx_t *tx)
750 uint64_t *newguid = arg;
751 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
752 vdev_t *rvd = spa->spa_root_vdev;
755 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
756 vdev_state = rvd->vdev_state;
757 spa_config_exit(spa, SCL_STATE, FTAG);
759 if (vdev_state != VDEV_STATE_HEALTHY)
760 return (SET_ERROR(ENXIO));
762 ASSERT3U(spa_guid(spa), !=, *newguid);
768 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
770 uint64_t *newguid = arg;
771 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
773 vdev_t *rvd = spa->spa_root_vdev;
775 oldguid = spa_guid(spa);
777 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
778 rvd->vdev_guid = *newguid;
779 rvd->vdev_guid_sum += (*newguid - oldguid);
780 vdev_config_dirty(rvd);
781 spa_config_exit(spa, SCL_STATE, FTAG);
783 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
788 * Change the GUID for the pool. This is done so that we can later
789 * re-import a pool built from a clone of our own vdevs. We will modify
790 * the root vdev's guid, our own pool guid, and then mark all of our
791 * vdevs dirty. Note that we must make sure that all our vdevs are
792 * online when we do this, or else any vdevs that weren't present
793 * would be orphaned from our pool. We are also going to issue a
794 * sysevent to update any watchers.
797 spa_change_guid(spa_t *spa)
802 mutex_enter(&spa->spa_vdev_top_lock);
803 mutex_enter(&spa_namespace_lock);
804 guid = spa_generate_guid(NULL);
806 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
807 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
810 spa_config_sync(spa, B_FALSE, B_TRUE);
811 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
814 mutex_exit(&spa_namespace_lock);
815 mutex_exit(&spa->spa_vdev_top_lock);
821 * ==========================================================================
822 * SPA state manipulation (open/create/destroy/import/export)
823 * ==========================================================================
827 spa_error_entry_compare(const void *a, const void *b)
829 spa_error_entry_t *sa = (spa_error_entry_t *)a;
830 spa_error_entry_t *sb = (spa_error_entry_t *)b;
833 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
834 sizeof (zbookmark_phys_t));
845 * Utility function which retrieves copies of the current logs and
846 * re-initializes them in the process.
849 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
851 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
853 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
854 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
856 avl_create(&spa->spa_errlist_scrub,
857 spa_error_entry_compare, sizeof (spa_error_entry_t),
858 offsetof(spa_error_entry_t, se_avl));
859 avl_create(&spa->spa_errlist_last,
860 spa_error_entry_compare, sizeof (spa_error_entry_t),
861 offsetof(spa_error_entry_t, se_avl));
865 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
867 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
868 enum zti_modes mode = ztip->zti_mode;
869 uint_t value = ztip->zti_value;
870 uint_t count = ztip->zti_count;
871 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
874 boolean_t batch = B_FALSE;
876 if (mode == ZTI_MODE_NULL) {
878 tqs->stqs_taskq = NULL;
882 ASSERT3U(count, >, 0);
884 tqs->stqs_count = count;
885 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
889 ASSERT3U(value, >=, 1);
890 value = MAX(value, 1);
895 flags |= TASKQ_THREADS_CPU_PCT;
896 value = zio_taskq_batch_pct;
900 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
902 zio_type_name[t], zio_taskq_types[q], mode, value);
906 for (uint_t i = 0; i < count; i++) {
910 (void) snprintf(name, sizeof (name), "%s_%s_%u",
911 zio_type_name[t], zio_taskq_types[q], i);
913 (void) snprintf(name, sizeof (name), "%s_%s",
914 zio_type_name[t], zio_taskq_types[q]);
918 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
920 flags |= TASKQ_DC_BATCH;
922 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
923 spa->spa_proc, zio_taskq_basedc, flags);
926 pri_t pri = maxclsyspri;
928 * The write issue taskq can be extremely CPU
929 * intensive. Run it at slightly lower priority
930 * than the other taskqs.
932 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
935 tq = taskq_create_proc(name, value, pri, 50,
936 INT_MAX, spa->spa_proc, flags);
941 tqs->stqs_taskq[i] = tq;
946 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
948 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
950 if (tqs->stqs_taskq == NULL) {
951 ASSERT0(tqs->stqs_count);
955 for (uint_t i = 0; i < tqs->stqs_count; i++) {
956 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
957 taskq_destroy(tqs->stqs_taskq[i]);
960 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
961 tqs->stqs_taskq = NULL;
965 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
966 * Note that a type may have multiple discrete taskqs to avoid lock contention
967 * on the taskq itself. In that case we choose which taskq at random by using
968 * the low bits of gethrtime().
971 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
972 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
974 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
977 ASSERT3P(tqs->stqs_taskq, !=, NULL);
978 ASSERT3U(tqs->stqs_count, !=, 0);
980 if (tqs->stqs_count == 1) {
981 tq = tqs->stqs_taskq[0];
984 tq = tqs->stqs_taskq[cpu_ticks() % tqs->stqs_count];
986 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
990 taskq_dispatch_ent(tq, func, arg, flags, ent);
994 spa_create_zio_taskqs(spa_t *spa)
996 for (int t = 0; t < ZIO_TYPES; t++) {
997 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
998 spa_taskqs_init(spa, t, q);
1006 spa_thread(void *arg)
1008 callb_cpr_t cprinfo;
1011 user_t *pu = PTOU(curproc);
1013 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1016 ASSERT(curproc != &p0);
1017 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1018 "zpool-%s", spa->spa_name);
1019 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1022 /* bind this thread to the requested psrset */
1023 if (zio_taskq_psrset_bind != PS_NONE) {
1025 mutex_enter(&cpu_lock);
1026 mutex_enter(&pidlock);
1027 mutex_enter(&curproc->p_lock);
1029 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1030 0, NULL, NULL) == 0) {
1031 curthread->t_bind_pset = zio_taskq_psrset_bind;
1034 "Couldn't bind process for zfs pool \"%s\" to "
1035 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1038 mutex_exit(&curproc->p_lock);
1039 mutex_exit(&pidlock);
1040 mutex_exit(&cpu_lock);
1046 if (zio_taskq_sysdc) {
1047 sysdc_thread_enter(curthread, 100, 0);
1051 spa->spa_proc = curproc;
1052 spa->spa_did = curthread->t_did;
1054 spa_create_zio_taskqs(spa);
1056 mutex_enter(&spa->spa_proc_lock);
1057 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1059 spa->spa_proc_state = SPA_PROC_ACTIVE;
1060 cv_broadcast(&spa->spa_proc_cv);
1062 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1063 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1064 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1065 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1067 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1068 spa->spa_proc_state = SPA_PROC_GONE;
1069 spa->spa_proc = &p0;
1070 cv_broadcast(&spa->spa_proc_cv);
1071 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1073 mutex_enter(&curproc->p_lock);
1076 #endif /* SPA_PROCESS */
1080 * Activate an uninitialized pool.
1083 spa_activate(spa_t *spa, int mode)
1085 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1087 spa->spa_state = POOL_STATE_ACTIVE;
1088 spa->spa_mode = mode;
1090 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1091 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1093 /* Try to create a covering process */
1094 mutex_enter(&spa->spa_proc_lock);
1095 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1096 ASSERT(spa->spa_proc == &p0);
1100 /* Only create a process if we're going to be around a while. */
1101 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1102 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1104 spa->spa_proc_state = SPA_PROC_CREATED;
1105 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1106 cv_wait(&spa->spa_proc_cv,
1107 &spa->spa_proc_lock);
1109 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1110 ASSERT(spa->spa_proc != &p0);
1111 ASSERT(spa->spa_did != 0);
1115 "Couldn't create process for zfs pool \"%s\"\n",
1120 #endif /* SPA_PROCESS */
1121 mutex_exit(&spa->spa_proc_lock);
1123 /* If we didn't create a process, we need to create our taskqs. */
1124 ASSERT(spa->spa_proc == &p0);
1125 if (spa->spa_proc == &p0) {
1126 spa_create_zio_taskqs(spa);
1130 * Start TRIM thread.
1132 trim_thread_create(spa);
1134 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1135 offsetof(vdev_t, vdev_config_dirty_node));
1136 list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1137 offsetof(objset_t, os_evicting_node));
1138 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1139 offsetof(vdev_t, vdev_state_dirty_node));
1141 txg_list_create(&spa->spa_vdev_txg_list,
1142 offsetof(struct vdev, vdev_txg_node));
1144 avl_create(&spa->spa_errlist_scrub,
1145 spa_error_entry_compare, sizeof (spa_error_entry_t),
1146 offsetof(spa_error_entry_t, se_avl));
1147 avl_create(&spa->spa_errlist_last,
1148 spa_error_entry_compare, sizeof (spa_error_entry_t),
1149 offsetof(spa_error_entry_t, se_avl));
1153 * Opposite of spa_activate().
1156 spa_deactivate(spa_t *spa)
1158 ASSERT(spa->spa_sync_on == B_FALSE);
1159 ASSERT(spa->spa_dsl_pool == NULL);
1160 ASSERT(spa->spa_root_vdev == NULL);
1161 ASSERT(spa->spa_async_zio_root == NULL);
1162 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1165 * Stop TRIM thread in case spa_unload() wasn't called directly
1166 * before spa_deactivate().
1168 trim_thread_destroy(spa);
1170 spa_evicting_os_wait(spa);
1172 txg_list_destroy(&spa->spa_vdev_txg_list);
1174 list_destroy(&spa->spa_config_dirty_list);
1175 list_destroy(&spa->spa_evicting_os_list);
1176 list_destroy(&spa->spa_state_dirty_list);
1178 for (int t = 0; t < ZIO_TYPES; t++) {
1179 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1180 spa_taskqs_fini(spa, t, q);
1184 metaslab_class_destroy(spa->spa_normal_class);
1185 spa->spa_normal_class = NULL;
1187 metaslab_class_destroy(spa->spa_log_class);
1188 spa->spa_log_class = NULL;
1191 * If this was part of an import or the open otherwise failed, we may
1192 * still have errors left in the queues. Empty them just in case.
1194 spa_errlog_drain(spa);
1196 avl_destroy(&spa->spa_errlist_scrub);
1197 avl_destroy(&spa->spa_errlist_last);
1199 spa->spa_state = POOL_STATE_UNINITIALIZED;
1201 mutex_enter(&spa->spa_proc_lock);
1202 if (spa->spa_proc_state != SPA_PROC_NONE) {
1203 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1204 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1205 cv_broadcast(&spa->spa_proc_cv);
1206 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1207 ASSERT(spa->spa_proc != &p0);
1208 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1210 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1211 spa->spa_proc_state = SPA_PROC_NONE;
1213 ASSERT(spa->spa_proc == &p0);
1214 mutex_exit(&spa->spa_proc_lock);
1218 * We want to make sure spa_thread() has actually exited the ZFS
1219 * module, so that the module can't be unloaded out from underneath
1222 if (spa->spa_did != 0) {
1223 thread_join(spa->spa_did);
1226 #endif /* SPA_PROCESS */
1230 * Verify a pool configuration, and construct the vdev tree appropriately. This
1231 * will create all the necessary vdevs in the appropriate layout, with each vdev
1232 * in the CLOSED state. This will prep the pool before open/creation/import.
1233 * All vdev validation is done by the vdev_alloc() routine.
1236 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1237 uint_t id, int atype)
1243 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1246 if ((*vdp)->vdev_ops->vdev_op_leaf)
1249 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1252 if (error == ENOENT)
1258 return (SET_ERROR(EINVAL));
1261 for (int c = 0; c < children; c++) {
1263 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1271 ASSERT(*vdp != NULL);
1277 * Opposite of spa_load().
1280 spa_unload(spa_t *spa)
1284 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1289 trim_thread_destroy(spa);
1294 spa_async_suspend(spa);
1299 if (spa->spa_sync_on) {
1300 txg_sync_stop(spa->spa_dsl_pool);
1301 spa->spa_sync_on = B_FALSE;
1305 * Wait for any outstanding async I/O to complete.
1307 if (spa->spa_async_zio_root != NULL) {
1308 for (int i = 0; i < max_ncpus; i++)
1309 (void) zio_wait(spa->spa_async_zio_root[i]);
1310 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
1311 spa->spa_async_zio_root = NULL;
1314 bpobj_close(&spa->spa_deferred_bpobj);
1316 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1321 if (spa->spa_root_vdev)
1322 vdev_free(spa->spa_root_vdev);
1323 ASSERT(spa->spa_root_vdev == NULL);
1326 * Close the dsl pool.
1328 if (spa->spa_dsl_pool) {
1329 dsl_pool_close(spa->spa_dsl_pool);
1330 spa->spa_dsl_pool = NULL;
1331 spa->spa_meta_objset = NULL;
1338 * Drop and purge level 2 cache
1340 spa_l2cache_drop(spa);
1342 for (i = 0; i < spa->spa_spares.sav_count; i++)
1343 vdev_free(spa->spa_spares.sav_vdevs[i]);
1344 if (spa->spa_spares.sav_vdevs) {
1345 kmem_free(spa->spa_spares.sav_vdevs,
1346 spa->spa_spares.sav_count * sizeof (void *));
1347 spa->spa_spares.sav_vdevs = NULL;
1349 if (spa->spa_spares.sav_config) {
1350 nvlist_free(spa->spa_spares.sav_config);
1351 spa->spa_spares.sav_config = NULL;
1353 spa->spa_spares.sav_count = 0;
1355 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1356 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1357 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1359 if (spa->spa_l2cache.sav_vdevs) {
1360 kmem_free(spa->spa_l2cache.sav_vdevs,
1361 spa->spa_l2cache.sav_count * sizeof (void *));
1362 spa->spa_l2cache.sav_vdevs = NULL;
1364 if (spa->spa_l2cache.sav_config) {
1365 nvlist_free(spa->spa_l2cache.sav_config);
1366 spa->spa_l2cache.sav_config = NULL;
1368 spa->spa_l2cache.sav_count = 0;
1370 spa->spa_async_suspended = 0;
1372 if (spa->spa_comment != NULL) {
1373 spa_strfree(spa->spa_comment);
1374 spa->spa_comment = NULL;
1377 spa_config_exit(spa, SCL_ALL, FTAG);
1381 * Load (or re-load) the current list of vdevs describing the active spares for
1382 * this pool. When this is called, we have some form of basic information in
1383 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1384 * then re-generate a more complete list including status information.
1387 spa_load_spares(spa_t *spa)
1394 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1397 * First, close and free any existing spare vdevs.
1399 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1400 vd = spa->spa_spares.sav_vdevs[i];
1402 /* Undo the call to spa_activate() below */
1403 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1404 B_FALSE)) != NULL && tvd->vdev_isspare)
1405 spa_spare_remove(tvd);
1410 if (spa->spa_spares.sav_vdevs)
1411 kmem_free(spa->spa_spares.sav_vdevs,
1412 spa->spa_spares.sav_count * sizeof (void *));
1414 if (spa->spa_spares.sav_config == NULL)
1417 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1418 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1420 spa->spa_spares.sav_count = (int)nspares;
1421 spa->spa_spares.sav_vdevs = NULL;
1427 * Construct the array of vdevs, opening them to get status in the
1428 * process. For each spare, there is potentially two different vdev_t
1429 * structures associated with it: one in the list of spares (used only
1430 * for basic validation purposes) and one in the active vdev
1431 * configuration (if it's spared in). During this phase we open and
1432 * validate each vdev on the spare list. If the vdev also exists in the
1433 * active configuration, then we also mark this vdev as an active spare.
1435 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1437 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1438 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1439 VDEV_ALLOC_SPARE) == 0);
1442 spa->spa_spares.sav_vdevs[i] = vd;
1444 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1445 B_FALSE)) != NULL) {
1446 if (!tvd->vdev_isspare)
1450 * We only mark the spare active if we were successfully
1451 * able to load the vdev. Otherwise, importing a pool
1452 * with a bad active spare would result in strange
1453 * behavior, because multiple pool would think the spare
1454 * is actively in use.
1456 * There is a vulnerability here to an equally bizarre
1457 * circumstance, where a dead active spare is later
1458 * brought back to life (onlined or otherwise). Given
1459 * the rarity of this scenario, and the extra complexity
1460 * it adds, we ignore the possibility.
1462 if (!vdev_is_dead(tvd))
1463 spa_spare_activate(tvd);
1467 vd->vdev_aux = &spa->spa_spares;
1469 if (vdev_open(vd) != 0)
1472 if (vdev_validate_aux(vd) == 0)
1477 * Recompute the stashed list of spares, with status information
1480 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1481 DATA_TYPE_NVLIST_ARRAY) == 0);
1483 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1485 for (i = 0; i < spa->spa_spares.sav_count; i++)
1486 spares[i] = vdev_config_generate(spa,
1487 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1488 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1489 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1490 for (i = 0; i < spa->spa_spares.sav_count; i++)
1491 nvlist_free(spares[i]);
1492 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1496 * Load (or re-load) the current list of vdevs describing the active l2cache for
1497 * this pool. When this is called, we have some form of basic information in
1498 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1499 * then re-generate a more complete list including status information.
1500 * Devices which are already active have their details maintained, and are
1504 spa_load_l2cache(spa_t *spa)
1508 int i, j, oldnvdevs;
1510 vdev_t *vd, **oldvdevs, **newvdevs;
1511 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1513 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1515 if (sav->sav_config != NULL) {
1516 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1517 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1518 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1524 oldvdevs = sav->sav_vdevs;
1525 oldnvdevs = sav->sav_count;
1526 sav->sav_vdevs = NULL;
1530 * Process new nvlist of vdevs.
1532 for (i = 0; i < nl2cache; i++) {
1533 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1537 for (j = 0; j < oldnvdevs; j++) {
1539 if (vd != NULL && guid == vd->vdev_guid) {
1541 * Retain previous vdev for add/remove ops.
1549 if (newvdevs[i] == NULL) {
1553 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1554 VDEV_ALLOC_L2CACHE) == 0);
1559 * Commit this vdev as an l2cache device,
1560 * even if it fails to open.
1562 spa_l2cache_add(vd);
1567 spa_l2cache_activate(vd);
1569 if (vdev_open(vd) != 0)
1572 (void) vdev_validate_aux(vd);
1574 if (!vdev_is_dead(vd))
1575 l2arc_add_vdev(spa, vd);
1580 * Purge vdevs that were dropped
1582 for (i = 0; i < oldnvdevs; i++) {
1587 ASSERT(vd->vdev_isl2cache);
1589 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1590 pool != 0ULL && l2arc_vdev_present(vd))
1591 l2arc_remove_vdev(vd);
1592 vdev_clear_stats(vd);
1598 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1600 if (sav->sav_config == NULL)
1603 sav->sav_vdevs = newvdevs;
1604 sav->sav_count = (int)nl2cache;
1607 * Recompute the stashed list of l2cache devices, with status
1608 * information this time.
1610 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1611 DATA_TYPE_NVLIST_ARRAY) == 0);
1613 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1614 for (i = 0; i < sav->sav_count; i++)
1615 l2cache[i] = vdev_config_generate(spa,
1616 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1617 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1618 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1620 for (i = 0; i < sav->sav_count; i++)
1621 nvlist_free(l2cache[i]);
1623 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1627 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1630 char *packed = NULL;
1635 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1639 nvsize = *(uint64_t *)db->db_data;
1640 dmu_buf_rele(db, FTAG);
1642 packed = kmem_alloc(nvsize, KM_SLEEP);
1643 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1646 error = nvlist_unpack(packed, nvsize, value, 0);
1647 kmem_free(packed, nvsize);
1653 * Checks to see if the given vdev could not be opened, in which case we post a
1654 * sysevent to notify the autoreplace code that the device has been removed.
1657 spa_check_removed(vdev_t *vd)
1659 for (int c = 0; c < vd->vdev_children; c++)
1660 spa_check_removed(vd->vdev_child[c]);
1662 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1664 zfs_post_autoreplace(vd->vdev_spa, vd);
1665 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1670 * Validate the current config against the MOS config
1673 spa_config_valid(spa_t *spa, nvlist_t *config)
1675 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1678 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1680 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1681 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1683 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1686 * If we're doing a normal import, then build up any additional
1687 * diagnostic information about missing devices in this config.
1688 * We'll pass this up to the user for further processing.
1690 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1691 nvlist_t **child, *nv;
1694 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1696 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1698 for (int c = 0; c < rvd->vdev_children; c++) {
1699 vdev_t *tvd = rvd->vdev_child[c];
1700 vdev_t *mtvd = mrvd->vdev_child[c];
1702 if (tvd->vdev_ops == &vdev_missing_ops &&
1703 mtvd->vdev_ops != &vdev_missing_ops &&
1705 child[idx++] = vdev_config_generate(spa, mtvd,
1710 VERIFY(nvlist_add_nvlist_array(nv,
1711 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1712 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1713 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1715 for (int i = 0; i < idx; i++)
1716 nvlist_free(child[i]);
1719 kmem_free(child, rvd->vdev_children * sizeof (char **));
1723 * Compare the root vdev tree with the information we have
1724 * from the MOS config (mrvd). Check each top-level vdev
1725 * with the corresponding MOS config top-level (mtvd).
1727 for (int c = 0; c < rvd->vdev_children; c++) {
1728 vdev_t *tvd = rvd->vdev_child[c];
1729 vdev_t *mtvd = mrvd->vdev_child[c];
1732 * Resolve any "missing" vdevs in the current configuration.
1733 * If we find that the MOS config has more accurate information
1734 * about the top-level vdev then use that vdev instead.
1736 if (tvd->vdev_ops == &vdev_missing_ops &&
1737 mtvd->vdev_ops != &vdev_missing_ops) {
1739 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1743 * Device specific actions.
1745 if (mtvd->vdev_islog) {
1746 spa_set_log_state(spa, SPA_LOG_CLEAR);
1749 * XXX - once we have 'readonly' pool
1750 * support we should be able to handle
1751 * missing data devices by transitioning
1752 * the pool to readonly.
1758 * Swap the missing vdev with the data we were
1759 * able to obtain from the MOS config.
1761 vdev_remove_child(rvd, tvd);
1762 vdev_remove_child(mrvd, mtvd);
1764 vdev_add_child(rvd, mtvd);
1765 vdev_add_child(mrvd, tvd);
1767 spa_config_exit(spa, SCL_ALL, FTAG);
1769 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1772 } else if (mtvd->vdev_islog) {
1774 * Load the slog device's state from the MOS config
1775 * since it's possible that the label does not
1776 * contain the most up-to-date information.
1778 vdev_load_log_state(tvd, mtvd);
1783 spa_config_exit(spa, SCL_ALL, FTAG);
1786 * Ensure we were able to validate the config.
1788 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1792 * Check for missing log devices
1795 spa_check_logs(spa_t *spa)
1797 boolean_t rv = B_FALSE;
1798 dsl_pool_t *dp = spa_get_dsl(spa);
1800 switch (spa->spa_log_state) {
1801 case SPA_LOG_MISSING:
1802 /* need to recheck in case slog has been restored */
1803 case SPA_LOG_UNKNOWN:
1804 rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1805 zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
1807 spa_set_log_state(spa, SPA_LOG_MISSING);
1814 spa_passivate_log(spa_t *spa)
1816 vdev_t *rvd = spa->spa_root_vdev;
1817 boolean_t slog_found = B_FALSE;
1819 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1821 if (!spa_has_slogs(spa))
1824 for (int c = 0; c < rvd->vdev_children; c++) {
1825 vdev_t *tvd = rvd->vdev_child[c];
1826 metaslab_group_t *mg = tvd->vdev_mg;
1828 if (tvd->vdev_islog) {
1829 metaslab_group_passivate(mg);
1830 slog_found = B_TRUE;
1834 return (slog_found);
1838 spa_activate_log(spa_t *spa)
1840 vdev_t *rvd = spa->spa_root_vdev;
1842 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1844 for (int c = 0; c < rvd->vdev_children; c++) {
1845 vdev_t *tvd = rvd->vdev_child[c];
1846 metaslab_group_t *mg = tvd->vdev_mg;
1848 if (tvd->vdev_islog)
1849 metaslab_group_activate(mg);
1854 spa_offline_log(spa_t *spa)
1858 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1859 NULL, DS_FIND_CHILDREN);
1862 * We successfully offlined the log device, sync out the
1863 * current txg so that the "stubby" block can be removed
1866 txg_wait_synced(spa->spa_dsl_pool, 0);
1872 spa_aux_check_removed(spa_aux_vdev_t *sav)
1876 for (i = 0; i < sav->sav_count; i++)
1877 spa_check_removed(sav->sav_vdevs[i]);
1881 spa_claim_notify(zio_t *zio)
1883 spa_t *spa = zio->io_spa;
1888 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1889 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1890 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1891 mutex_exit(&spa->spa_props_lock);
1894 typedef struct spa_load_error {
1895 uint64_t sle_meta_count;
1896 uint64_t sle_data_count;
1900 spa_load_verify_done(zio_t *zio)
1902 blkptr_t *bp = zio->io_bp;
1903 spa_load_error_t *sle = zio->io_private;
1904 dmu_object_type_t type = BP_GET_TYPE(bp);
1905 int error = zio->io_error;
1906 spa_t *spa = zio->io_spa;
1909 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1910 type != DMU_OT_INTENT_LOG)
1911 atomic_inc_64(&sle->sle_meta_count);
1913 atomic_inc_64(&sle->sle_data_count);
1915 zio_data_buf_free(zio->io_data, zio->io_size);
1917 mutex_enter(&spa->spa_scrub_lock);
1918 spa->spa_scrub_inflight--;
1919 cv_broadcast(&spa->spa_scrub_io_cv);
1920 mutex_exit(&spa->spa_scrub_lock);
1924 * Maximum number of concurrent scrub i/os to create while verifying
1925 * a pool while importing it.
1927 int spa_load_verify_maxinflight = 10000;
1928 boolean_t spa_load_verify_metadata = B_TRUE;
1929 boolean_t spa_load_verify_data = B_TRUE;
1931 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1932 &spa_load_verify_maxinflight, 0,
1933 "Maximum number of concurrent scrub I/Os to create while verifying a "
1934 "pool while importing it");
1936 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1937 &spa_load_verify_metadata, 0,
1938 "Check metadata on import?");
1940 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1941 &spa_load_verify_data, 0,
1942 "Check user data on import?");
1946 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1947 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1949 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1952 * Note: normally this routine will not be called if
1953 * spa_load_verify_metadata is not set. However, it may be useful
1954 * to manually set the flag after the traversal has begun.
1956 if (!spa_load_verify_metadata)
1958 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1962 size_t size = BP_GET_PSIZE(bp);
1963 void *data = zio_data_buf_alloc(size);
1965 mutex_enter(&spa->spa_scrub_lock);
1966 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1967 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1968 spa->spa_scrub_inflight++;
1969 mutex_exit(&spa->spa_scrub_lock);
1971 zio_nowait(zio_read(rio, spa, bp, data, size,
1972 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1973 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1974 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1980 verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1982 char namebuf[MAXPATHLEN];
1983 dsl_dataset_name(ds, namebuf);
1984 if (strlen(namebuf) > MAXNAMELEN) {
1985 return (SET_ERROR(ENAMETOOLONG));
1992 spa_load_verify(spa_t *spa)
1995 spa_load_error_t sle = { 0 };
1996 zpool_rewind_policy_t policy;
1997 boolean_t verify_ok = B_FALSE;
2000 zpool_get_rewind_policy(spa->spa_config, &policy);
2002 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
2005 dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
2006 error = dmu_objset_find_dp(spa->spa_dsl_pool,
2007 spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
2009 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
2013 rio = zio_root(spa, NULL, &sle,
2014 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
2016 if (spa_load_verify_metadata) {
2017 error = traverse_pool(spa, spa->spa_verify_min_txg,
2018 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2019 spa_load_verify_cb, rio);
2022 (void) zio_wait(rio);
2024 spa->spa_load_meta_errors = sle.sle_meta_count;
2025 spa->spa_load_data_errors = sle.sle_data_count;
2027 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
2028 sle.sle_data_count <= policy.zrp_maxdata) {
2032 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2033 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2035 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2036 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2037 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2038 VERIFY(nvlist_add_int64(spa->spa_load_info,
2039 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2040 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2041 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2043 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2047 if (error != ENXIO && error != EIO)
2048 error = SET_ERROR(EIO);
2052 return (verify_ok ? 0 : EIO);
2056 * Find a value in the pool props object.
2059 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2061 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2062 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2066 * Find a value in the pool directory object.
2069 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2071 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2072 name, sizeof (uint64_t), 1, val));
2076 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2078 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2083 * Fix up config after a partly-completed split. This is done with the
2084 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2085 * pool have that entry in their config, but only the splitting one contains
2086 * a list of all the guids of the vdevs that are being split off.
2088 * This function determines what to do with that list: either rejoin
2089 * all the disks to the pool, or complete the splitting process. To attempt
2090 * the rejoin, each disk that is offlined is marked online again, and
2091 * we do a reopen() call. If the vdev label for every disk that was
2092 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2093 * then we call vdev_split() on each disk, and complete the split.
2095 * Otherwise we leave the config alone, with all the vdevs in place in
2096 * the original pool.
2099 spa_try_repair(spa_t *spa, nvlist_t *config)
2106 boolean_t attempt_reopen;
2108 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2111 /* check that the config is complete */
2112 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2113 &glist, &gcount) != 0)
2116 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2118 /* attempt to online all the vdevs & validate */
2119 attempt_reopen = B_TRUE;
2120 for (i = 0; i < gcount; i++) {
2121 if (glist[i] == 0) /* vdev is hole */
2124 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2125 if (vd[i] == NULL) {
2127 * Don't bother attempting to reopen the disks;
2128 * just do the split.
2130 attempt_reopen = B_FALSE;
2132 /* attempt to re-online it */
2133 vd[i]->vdev_offline = B_FALSE;
2137 if (attempt_reopen) {
2138 vdev_reopen(spa->spa_root_vdev);
2140 /* check each device to see what state it's in */
2141 for (extracted = 0, i = 0; i < gcount; i++) {
2142 if (vd[i] != NULL &&
2143 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2150 * If every disk has been moved to the new pool, or if we never
2151 * even attempted to look at them, then we split them off for
2154 if (!attempt_reopen || gcount == extracted) {
2155 for (i = 0; i < gcount; i++)
2158 vdev_reopen(spa->spa_root_vdev);
2161 kmem_free(vd, gcount * sizeof (vdev_t *));
2165 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2166 boolean_t mosconfig)
2168 nvlist_t *config = spa->spa_config;
2169 char *ereport = FM_EREPORT_ZFS_POOL;
2175 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2176 return (SET_ERROR(EINVAL));
2178 ASSERT(spa->spa_comment == NULL);
2179 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2180 spa->spa_comment = spa_strdup(comment);
2183 * Versioning wasn't explicitly added to the label until later, so if
2184 * it's not present treat it as the initial version.
2186 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2187 &spa->spa_ubsync.ub_version) != 0)
2188 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2190 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2191 &spa->spa_config_txg);
2193 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2194 spa_guid_exists(pool_guid, 0)) {
2195 error = SET_ERROR(EEXIST);
2197 spa->spa_config_guid = pool_guid;
2199 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2201 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2205 nvlist_free(spa->spa_load_info);
2206 spa->spa_load_info = fnvlist_alloc();
2208 gethrestime(&spa->spa_loaded_ts);
2209 error = spa_load_impl(spa, pool_guid, config, state, type,
2210 mosconfig, &ereport);
2214 * Don't count references from objsets that are already closed
2215 * and are making their way through the eviction process.
2217 spa_evicting_os_wait(spa);
2218 spa->spa_minref = refcount_count(&spa->spa_refcount);
2220 if (error != EEXIST) {
2221 spa->spa_loaded_ts.tv_sec = 0;
2222 spa->spa_loaded_ts.tv_nsec = 0;
2224 if (error != EBADF) {
2225 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2228 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2235 * Load an existing storage pool, using the pool's builtin spa_config as a
2236 * source of configuration information.
2239 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2240 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2244 nvlist_t *nvroot = NULL;
2247 uberblock_t *ub = &spa->spa_uberblock;
2248 uint64_t children, config_cache_txg = spa->spa_config_txg;
2249 int orig_mode = spa->spa_mode;
2252 boolean_t missing_feat_write = B_FALSE;
2255 * If this is an untrusted config, access the pool in read-only mode.
2256 * This prevents things like resilvering recently removed devices.
2259 spa->spa_mode = FREAD;
2261 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2263 spa->spa_load_state = state;
2265 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2266 return (SET_ERROR(EINVAL));
2268 parse = (type == SPA_IMPORT_EXISTING ?
2269 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2272 * Create "The Godfather" zio to hold all async IOs
2274 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2276 for (int i = 0; i < max_ncpus; i++) {
2277 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2278 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2279 ZIO_FLAG_GODFATHER);
2283 * Parse the configuration into a vdev tree. We explicitly set the
2284 * value that will be returned by spa_version() since parsing the
2285 * configuration requires knowing the version number.
2287 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2288 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2289 spa_config_exit(spa, SCL_ALL, FTAG);
2294 ASSERT(spa->spa_root_vdev == rvd);
2295 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2296 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
2298 if (type != SPA_IMPORT_ASSEMBLE) {
2299 ASSERT(spa_guid(spa) == pool_guid);
2303 * Try to open all vdevs, loading each label in the process.
2305 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2306 error = vdev_open(rvd);
2307 spa_config_exit(spa, SCL_ALL, FTAG);
2312 * We need to validate the vdev labels against the configuration that
2313 * we have in hand, which is dependent on the setting of mosconfig. If
2314 * mosconfig is true then we're validating the vdev labels based on
2315 * that config. Otherwise, we're validating against the cached config
2316 * (zpool.cache) that was read when we loaded the zfs module, and then
2317 * later we will recursively call spa_load() and validate against
2320 * If we're assembling a new pool that's been split off from an
2321 * existing pool, the labels haven't yet been updated so we skip
2322 * validation for now.
2324 if (type != SPA_IMPORT_ASSEMBLE) {
2325 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2326 error = vdev_validate(rvd, mosconfig);
2327 spa_config_exit(spa, SCL_ALL, FTAG);
2332 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2333 return (SET_ERROR(ENXIO));
2337 * Find the best uberblock.
2339 vdev_uberblock_load(rvd, ub, &label);
2342 * If we weren't able to find a single valid uberblock, return failure.
2344 if (ub->ub_txg == 0) {
2346 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2350 * If the pool has an unsupported version we can't open it.
2352 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2354 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2357 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2361 * If we weren't able to find what's necessary for reading the
2362 * MOS in the label, return failure.
2364 if (label == NULL || nvlist_lookup_nvlist(label,
2365 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2367 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2372 * Update our in-core representation with the definitive values
2375 nvlist_free(spa->spa_label_features);
2376 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2382 * Look through entries in the label nvlist's features_for_read. If
2383 * there is a feature listed there which we don't understand then we
2384 * cannot open a pool.
2386 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2387 nvlist_t *unsup_feat;
2389 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2392 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2394 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2395 if (!zfeature_is_supported(nvpair_name(nvp))) {
2396 VERIFY(nvlist_add_string(unsup_feat,
2397 nvpair_name(nvp), "") == 0);
2401 if (!nvlist_empty(unsup_feat)) {
2402 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2403 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2404 nvlist_free(unsup_feat);
2405 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2409 nvlist_free(unsup_feat);
2413 * If the vdev guid sum doesn't match the uberblock, we have an
2414 * incomplete configuration. We first check to see if the pool
2415 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2416 * If it is, defer the vdev_guid_sum check till later so we
2417 * can handle missing vdevs.
2419 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2420 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2421 rvd->vdev_guid_sum != ub->ub_guid_sum)
2422 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2424 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2425 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2426 spa_try_repair(spa, config);
2427 spa_config_exit(spa, SCL_ALL, FTAG);
2428 nvlist_free(spa->spa_config_splitting);
2429 spa->spa_config_splitting = NULL;
2433 * Initialize internal SPA structures.
2435 spa->spa_state = POOL_STATE_ACTIVE;
2436 spa->spa_ubsync = spa->spa_uberblock;
2437 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2438 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2439 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2440 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2441 spa->spa_claim_max_txg = spa->spa_first_txg;
2442 spa->spa_prev_software_version = ub->ub_software_version;
2444 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2446 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2447 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2449 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2450 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2452 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2453 boolean_t missing_feat_read = B_FALSE;
2454 nvlist_t *unsup_feat, *enabled_feat;
2456 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2457 &spa->spa_feat_for_read_obj) != 0) {
2458 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2461 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2462 &spa->spa_feat_for_write_obj) != 0) {
2463 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2466 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2467 &spa->spa_feat_desc_obj) != 0) {
2468 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2471 enabled_feat = fnvlist_alloc();
2472 unsup_feat = fnvlist_alloc();
2474 if (!spa_features_check(spa, B_FALSE,
2475 unsup_feat, enabled_feat))
2476 missing_feat_read = B_TRUE;
2478 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2479 if (!spa_features_check(spa, B_TRUE,
2480 unsup_feat, enabled_feat)) {
2481 missing_feat_write = B_TRUE;
2485 fnvlist_add_nvlist(spa->spa_load_info,
2486 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2488 if (!nvlist_empty(unsup_feat)) {
2489 fnvlist_add_nvlist(spa->spa_load_info,
2490 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2493 fnvlist_free(enabled_feat);
2494 fnvlist_free(unsup_feat);
2496 if (!missing_feat_read) {
2497 fnvlist_add_boolean(spa->spa_load_info,
2498 ZPOOL_CONFIG_CAN_RDONLY);
2502 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2503 * twofold: to determine whether the pool is available for
2504 * import in read-write mode and (if it is not) whether the
2505 * pool is available for import in read-only mode. If the pool
2506 * is available for import in read-write mode, it is displayed
2507 * as available in userland; if it is not available for import
2508 * in read-only mode, it is displayed as unavailable in
2509 * userland. If the pool is available for import in read-only
2510 * mode but not read-write mode, it is displayed as unavailable
2511 * in userland with a special note that the pool is actually
2512 * available for open in read-only mode.
2514 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2515 * missing a feature for write, we must first determine whether
2516 * the pool can be opened read-only before returning to
2517 * userland in order to know whether to display the
2518 * abovementioned note.
2520 if (missing_feat_read || (missing_feat_write &&
2521 spa_writeable(spa))) {
2522 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2527 * Load refcounts for ZFS features from disk into an in-memory
2528 * cache during SPA initialization.
2530 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2533 error = feature_get_refcount_from_disk(spa,
2534 &spa_feature_table[i], &refcount);
2536 spa->spa_feat_refcount_cache[i] = refcount;
2537 } else if (error == ENOTSUP) {
2538 spa->spa_feat_refcount_cache[i] =
2539 SPA_FEATURE_DISABLED;
2541 return (spa_vdev_err(rvd,
2542 VDEV_AUX_CORRUPT_DATA, EIO));
2547 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2548 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2549 &spa->spa_feat_enabled_txg_obj) != 0)
2550 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2553 spa->spa_is_initializing = B_TRUE;
2554 error = dsl_pool_open(spa->spa_dsl_pool);
2555 spa->spa_is_initializing = B_FALSE;
2557 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2561 nvlist_t *policy = NULL, *nvconfig;
2563 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2564 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2566 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2567 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2569 unsigned long myhostid = 0;
2571 VERIFY(nvlist_lookup_string(nvconfig,
2572 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2575 myhostid = zone_get_hostid(NULL);
2578 * We're emulating the system's hostid in userland, so
2579 * we can't use zone_get_hostid().
2581 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2582 #endif /* _KERNEL */
2583 if (check_hostid && hostid != 0 && myhostid != 0 &&
2584 hostid != myhostid) {
2585 nvlist_free(nvconfig);
2586 cmn_err(CE_WARN, "pool '%s' could not be "
2587 "loaded as it was last accessed by "
2588 "another system (host: %s hostid: 0x%lx). "
2589 "See: http://illumos.org/msg/ZFS-8000-EY",
2590 spa_name(spa), hostname,
2591 (unsigned long)hostid);
2592 return (SET_ERROR(EBADF));
2595 if (nvlist_lookup_nvlist(spa->spa_config,
2596 ZPOOL_REWIND_POLICY, &policy) == 0)
2597 VERIFY(nvlist_add_nvlist(nvconfig,
2598 ZPOOL_REWIND_POLICY, policy) == 0);
2600 spa_config_set(spa, nvconfig);
2602 spa_deactivate(spa);
2603 spa_activate(spa, orig_mode);
2605 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2608 /* Grab the secret checksum salt from the MOS. */
2609 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2610 DMU_POOL_CHECKSUM_SALT, 1,
2611 sizeof (spa->spa_cksum_salt.zcs_bytes),
2612 spa->spa_cksum_salt.zcs_bytes);
2613 if (error == ENOENT) {
2614 /* Generate a new salt for subsequent use */
2615 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
2616 sizeof (spa->spa_cksum_salt.zcs_bytes));
2617 } else if (error != 0) {
2618 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2621 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2622 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2623 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2625 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2628 * Load the bit that tells us to use the new accounting function
2629 * (raid-z deflation). If we have an older pool, this will not
2632 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2633 if (error != 0 && error != ENOENT)
2634 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2636 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2637 &spa->spa_creation_version);
2638 if (error != 0 && error != ENOENT)
2639 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2642 * Load the persistent error log. If we have an older pool, this will
2645 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2646 if (error != 0 && error != ENOENT)
2647 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2649 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2650 &spa->spa_errlog_scrub);
2651 if (error != 0 && error != ENOENT)
2652 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2655 * Load the history object. If we have an older pool, this
2656 * will not be present.
2658 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2659 if (error != 0 && error != ENOENT)
2660 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2663 * If we're assembling the pool from the split-off vdevs of
2664 * an existing pool, we don't want to attach the spares & cache
2669 * Load any hot spares for this pool.
2671 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2672 if (error != 0 && error != ENOENT)
2673 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2674 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2675 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2676 if (load_nvlist(spa, spa->spa_spares.sav_object,
2677 &spa->spa_spares.sav_config) != 0)
2678 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2680 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2681 spa_load_spares(spa);
2682 spa_config_exit(spa, SCL_ALL, FTAG);
2683 } else if (error == 0) {
2684 spa->spa_spares.sav_sync = B_TRUE;
2688 * Load any level 2 ARC devices for this pool.
2690 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2691 &spa->spa_l2cache.sav_object);
2692 if (error != 0 && error != ENOENT)
2693 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2694 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2695 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2696 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2697 &spa->spa_l2cache.sav_config) != 0)
2698 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2700 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2701 spa_load_l2cache(spa);
2702 spa_config_exit(spa, SCL_ALL, FTAG);
2703 } else if (error == 0) {
2704 spa->spa_l2cache.sav_sync = B_TRUE;
2707 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2709 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2710 if (error && error != ENOENT)
2711 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2714 uint64_t autoreplace;
2716 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2717 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2718 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2719 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2720 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2721 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2722 &spa->spa_dedup_ditto);
2724 spa->spa_autoreplace = (autoreplace != 0);
2728 * If the 'autoreplace' property is set, then post a resource notifying
2729 * the ZFS DE that it should not issue any faults for unopenable
2730 * devices. We also iterate over the vdevs, and post a sysevent for any
2731 * unopenable vdevs so that the normal autoreplace handler can take
2734 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2735 spa_check_removed(spa->spa_root_vdev);
2737 * For the import case, this is done in spa_import(), because
2738 * at this point we're using the spare definitions from
2739 * the MOS config, not necessarily from the userland config.
2741 if (state != SPA_LOAD_IMPORT) {
2742 spa_aux_check_removed(&spa->spa_spares);
2743 spa_aux_check_removed(&spa->spa_l2cache);
2748 * Load the vdev state for all toplevel vdevs.
2753 * Propagate the leaf DTLs we just loaded all the way up the tree.
2755 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2756 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2757 spa_config_exit(spa, SCL_ALL, FTAG);
2760 * Load the DDTs (dedup tables).
2762 error = ddt_load(spa);
2764 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2766 spa_update_dspace(spa);
2769 * Validate the config, using the MOS config to fill in any
2770 * information which might be missing. If we fail to validate
2771 * the config then declare the pool unfit for use. If we're
2772 * assembling a pool from a split, the log is not transferred
2775 if (type != SPA_IMPORT_ASSEMBLE) {
2778 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2779 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2781 if (!spa_config_valid(spa, nvconfig)) {
2782 nvlist_free(nvconfig);
2783 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2786 nvlist_free(nvconfig);
2789 * Now that we've validated the config, check the state of the
2790 * root vdev. If it can't be opened, it indicates one or
2791 * more toplevel vdevs are faulted.
2793 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2794 return (SET_ERROR(ENXIO));
2796 if (spa_writeable(spa) && spa_check_logs(spa)) {
2797 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2798 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2802 if (missing_feat_write) {
2803 ASSERT(state == SPA_LOAD_TRYIMPORT);
2806 * At this point, we know that we can open the pool in
2807 * read-only mode but not read-write mode. We now have enough
2808 * information and can return to userland.
2810 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2814 * We've successfully opened the pool, verify that we're ready
2815 * to start pushing transactions.
2817 if (state != SPA_LOAD_TRYIMPORT) {
2818 if (error = spa_load_verify(spa))
2819 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2823 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2824 spa->spa_load_max_txg == UINT64_MAX)) {
2826 int need_update = B_FALSE;
2827 dsl_pool_t *dp = spa_get_dsl(spa);
2829 ASSERT(state != SPA_LOAD_TRYIMPORT);
2832 * Claim log blocks that haven't been committed yet.
2833 * This must all happen in a single txg.
2834 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2835 * invoked from zil_claim_log_block()'s i/o done callback.
2836 * Price of rollback is that we abandon the log.
2838 spa->spa_claiming = B_TRUE;
2840 tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
2841 (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2842 zil_claim, tx, DS_FIND_CHILDREN);
2845 spa->spa_claiming = B_FALSE;
2847 spa_set_log_state(spa, SPA_LOG_GOOD);
2848 spa->spa_sync_on = B_TRUE;
2849 txg_sync_start(spa->spa_dsl_pool);
2852 * Wait for all claims to sync. We sync up to the highest
2853 * claimed log block birth time so that claimed log blocks
2854 * don't appear to be from the future. spa_claim_max_txg
2855 * will have been set for us by either zil_check_log_chain()
2856 * (invoked from spa_check_logs()) or zil_claim() above.
2858 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2861 * If the config cache is stale, or we have uninitialized
2862 * metaslabs (see spa_vdev_add()), then update the config.
2864 * If this is a verbatim import, trust the current
2865 * in-core spa_config and update the disk labels.
2867 if (config_cache_txg != spa->spa_config_txg ||
2868 state == SPA_LOAD_IMPORT ||
2869 state == SPA_LOAD_RECOVER ||
2870 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2871 need_update = B_TRUE;
2873 for (int c = 0; c < rvd->vdev_children; c++)
2874 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2875 need_update = B_TRUE;
2878 * Update the config cache asychronously in case we're the
2879 * root pool, in which case the config cache isn't writable yet.
2882 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2885 * Check all DTLs to see if anything needs resilvering.
2887 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2888 vdev_resilver_needed(rvd, NULL, NULL))
2889 spa_async_request(spa, SPA_ASYNC_RESILVER);
2892 * Log the fact that we booted up (so that we can detect if
2893 * we rebooted in the middle of an operation).
2895 spa_history_log_version(spa, "open");
2898 * Delete any inconsistent datasets.
2900 (void) dmu_objset_find(spa_name(spa),
2901 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2904 * Clean up any stale temporary dataset userrefs.
2906 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2913 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2915 int mode = spa->spa_mode;
2918 spa_deactivate(spa);
2920 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2922 spa_activate(spa, mode);
2923 spa_async_suspend(spa);
2925 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2929 * If spa_load() fails this function will try loading prior txg's. If
2930 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2931 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2932 * function will not rewind the pool and will return the same error as
2936 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2937 uint64_t max_request, int rewind_flags)
2939 nvlist_t *loadinfo = NULL;
2940 nvlist_t *config = NULL;
2941 int load_error, rewind_error;
2942 uint64_t safe_rewind_txg;
2945 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2946 spa->spa_load_max_txg = spa->spa_load_txg;
2947 spa_set_log_state(spa, SPA_LOG_CLEAR);
2949 spa->spa_load_max_txg = max_request;
2950 if (max_request != UINT64_MAX)
2951 spa->spa_extreme_rewind = B_TRUE;
2954 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2956 if (load_error == 0)
2959 if (spa->spa_root_vdev != NULL)
2960 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2962 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2963 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2965 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2966 nvlist_free(config);
2967 return (load_error);
2970 if (state == SPA_LOAD_RECOVER) {
2971 /* Price of rolling back is discarding txgs, including log */
2972 spa_set_log_state(spa, SPA_LOG_CLEAR);
2975 * If we aren't rolling back save the load info from our first
2976 * import attempt so that we can restore it after attempting
2979 loadinfo = spa->spa_load_info;
2980 spa->spa_load_info = fnvlist_alloc();
2983 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2984 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2985 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2986 TXG_INITIAL : safe_rewind_txg;
2989 * Continue as long as we're finding errors, we're still within
2990 * the acceptable rewind range, and we're still finding uberblocks
2992 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2993 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2994 if (spa->spa_load_max_txg < safe_rewind_txg)
2995 spa->spa_extreme_rewind = B_TRUE;
2996 rewind_error = spa_load_retry(spa, state, mosconfig);
2999 spa->spa_extreme_rewind = B_FALSE;
3000 spa->spa_load_max_txg = UINT64_MAX;
3002 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
3003 spa_config_set(spa, config);
3005 if (state == SPA_LOAD_RECOVER) {
3006 ASSERT3P(loadinfo, ==, NULL);
3007 return (rewind_error);
3009 /* Store the rewind info as part of the initial load info */
3010 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
3011 spa->spa_load_info);
3013 /* Restore the initial load info */
3014 fnvlist_free(spa->spa_load_info);
3015 spa->spa_load_info = loadinfo;
3017 return (load_error);
3024 * The import case is identical to an open except that the configuration is sent
3025 * down from userland, instead of grabbed from the configuration cache. For the
3026 * case of an open, the pool configuration will exist in the
3027 * POOL_STATE_UNINITIALIZED state.
3029 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3030 * the same time open the pool, without having to keep around the spa_t in some
3034 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
3038 spa_load_state_t state = SPA_LOAD_OPEN;
3040 int locked = B_FALSE;
3041 int firstopen = B_FALSE;
3046 * As disgusting as this is, we need to support recursive calls to this
3047 * function because dsl_dir_open() is called during spa_load(), and ends
3048 * up calling spa_open() again. The real fix is to figure out how to
3049 * avoid dsl_dir_open() calling this in the first place.
3051 if (mutex_owner(&spa_namespace_lock) != curthread) {
3052 mutex_enter(&spa_namespace_lock);
3056 if ((spa = spa_lookup(pool)) == NULL) {
3058 mutex_exit(&spa_namespace_lock);
3059 return (SET_ERROR(ENOENT));
3062 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
3063 zpool_rewind_policy_t policy;
3067 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
3069 if (policy.zrp_request & ZPOOL_DO_REWIND)
3070 state = SPA_LOAD_RECOVER;
3072 spa_activate(spa, spa_mode_global);
3074 if (state != SPA_LOAD_RECOVER)
3075 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3077 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3078 policy.zrp_request);
3080 if (error == EBADF) {
3082 * If vdev_validate() returns failure (indicated by
3083 * EBADF), it indicates that one of the vdevs indicates
3084 * that the pool has been exported or destroyed. If
3085 * this is the case, the config cache is out of sync and
3086 * we should remove the pool from the namespace.
3089 spa_deactivate(spa);
3090 spa_config_sync(spa, B_TRUE, B_TRUE);
3093 mutex_exit(&spa_namespace_lock);
3094 return (SET_ERROR(ENOENT));
3099 * We can't open the pool, but we still have useful
3100 * information: the state of each vdev after the
3101 * attempted vdev_open(). Return this to the user.
3103 if (config != NULL && spa->spa_config) {
3104 VERIFY(nvlist_dup(spa->spa_config, config,
3106 VERIFY(nvlist_add_nvlist(*config,
3107 ZPOOL_CONFIG_LOAD_INFO,
3108 spa->spa_load_info) == 0);
3111 spa_deactivate(spa);
3112 spa->spa_last_open_failed = error;
3114 mutex_exit(&spa_namespace_lock);
3120 spa_open_ref(spa, tag);
3123 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3126 * If we've recovered the pool, pass back any information we
3127 * gathered while doing the load.
3129 if (state == SPA_LOAD_RECOVER) {
3130 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3131 spa->spa_load_info) == 0);
3135 spa->spa_last_open_failed = 0;
3136 spa->spa_last_ubsync_txg = 0;
3137 spa->spa_load_txg = 0;
3138 mutex_exit(&spa_namespace_lock);
3142 zvol_create_minors(spa->spa_name);
3153 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3156 return (spa_open_common(name, spapp, tag, policy, config));
3160 spa_open(const char *name, spa_t **spapp, void *tag)
3162 return (spa_open_common(name, spapp, tag, NULL, NULL));
3166 * Lookup the given spa_t, incrementing the inject count in the process,
3167 * preventing it from being exported or destroyed.
3170 spa_inject_addref(char *name)
3174 mutex_enter(&spa_namespace_lock);
3175 if ((spa = spa_lookup(name)) == NULL) {
3176 mutex_exit(&spa_namespace_lock);
3179 spa->spa_inject_ref++;
3180 mutex_exit(&spa_namespace_lock);
3186 spa_inject_delref(spa_t *spa)
3188 mutex_enter(&spa_namespace_lock);
3189 spa->spa_inject_ref--;
3190 mutex_exit(&spa_namespace_lock);
3194 * Add spares device information to the nvlist.
3197 spa_add_spares(spa_t *spa, nvlist_t *config)
3207 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3209 if (spa->spa_spares.sav_count == 0)
3212 VERIFY(nvlist_lookup_nvlist(config,
3213 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3214 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3215 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3217 VERIFY(nvlist_add_nvlist_array(nvroot,
3218 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3219 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3220 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3223 * Go through and find any spares which have since been
3224 * repurposed as an active spare. If this is the case, update
3225 * their status appropriately.
3227 for (i = 0; i < nspares; i++) {
3228 VERIFY(nvlist_lookup_uint64(spares[i],
3229 ZPOOL_CONFIG_GUID, &guid) == 0);
3230 if (spa_spare_exists(guid, &pool, NULL) &&
3232 VERIFY(nvlist_lookup_uint64_array(
3233 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3234 (uint64_t **)&vs, &vsc) == 0);
3235 vs->vs_state = VDEV_STATE_CANT_OPEN;
3236 vs->vs_aux = VDEV_AUX_SPARED;
3243 * Add l2cache device information to the nvlist, including vdev stats.
3246 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3249 uint_t i, j, nl2cache;
3256 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3258 if (spa->spa_l2cache.sav_count == 0)
3261 VERIFY(nvlist_lookup_nvlist(config,
3262 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3263 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3264 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3265 if (nl2cache != 0) {
3266 VERIFY(nvlist_add_nvlist_array(nvroot,
3267 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3268 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3269 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3272 * Update level 2 cache device stats.
3275 for (i = 0; i < nl2cache; i++) {
3276 VERIFY(nvlist_lookup_uint64(l2cache[i],
3277 ZPOOL_CONFIG_GUID, &guid) == 0);
3280 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3282 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3283 vd = spa->spa_l2cache.sav_vdevs[j];
3289 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3290 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3292 vdev_get_stats(vd, vs);
3298 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3304 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3305 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3307 /* We may be unable to read features if pool is suspended. */
3308 if (spa_suspended(spa))
3311 if (spa->spa_feat_for_read_obj != 0) {
3312 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3313 spa->spa_feat_for_read_obj);
3314 zap_cursor_retrieve(&zc, &za) == 0;
3315 zap_cursor_advance(&zc)) {
3316 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3317 za.za_num_integers == 1);
3318 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3319 za.za_first_integer));
3321 zap_cursor_fini(&zc);
3324 if (spa->spa_feat_for_write_obj != 0) {
3325 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3326 spa->spa_feat_for_write_obj);
3327 zap_cursor_retrieve(&zc, &za) == 0;
3328 zap_cursor_advance(&zc)) {
3329 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3330 za.za_num_integers == 1);
3331 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3332 za.za_first_integer));
3334 zap_cursor_fini(&zc);
3338 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3340 nvlist_free(features);
3344 spa_get_stats(const char *name, nvlist_t **config,
3345 char *altroot, size_t buflen)
3351 error = spa_open_common(name, &spa, FTAG, NULL, config);
3355 * This still leaves a window of inconsistency where the spares
3356 * or l2cache devices could change and the config would be
3357 * self-inconsistent.
3359 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3361 if (*config != NULL) {
3362 uint64_t loadtimes[2];
3364 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3365 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3366 VERIFY(nvlist_add_uint64_array(*config,
3367 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3369 VERIFY(nvlist_add_uint64(*config,
3370 ZPOOL_CONFIG_ERRCOUNT,
3371 spa_get_errlog_size(spa)) == 0);
3373 if (spa_suspended(spa))
3374 VERIFY(nvlist_add_uint64(*config,
3375 ZPOOL_CONFIG_SUSPENDED,
3376 spa->spa_failmode) == 0);
3378 spa_add_spares(spa, *config);
3379 spa_add_l2cache(spa, *config);
3380 spa_add_feature_stats(spa, *config);
3385 * We want to get the alternate root even for faulted pools, so we cheat
3386 * and call spa_lookup() directly.
3390 mutex_enter(&spa_namespace_lock);
3391 spa = spa_lookup(name);
3393 spa_altroot(spa, altroot, buflen);
3397 mutex_exit(&spa_namespace_lock);
3399 spa_altroot(spa, altroot, buflen);
3404 spa_config_exit(spa, SCL_CONFIG, FTAG);
3405 spa_close(spa, FTAG);
3412 * Validate that the auxiliary device array is well formed. We must have an
3413 * array of nvlists, each which describes a valid leaf vdev. If this is an
3414 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3415 * specified, as long as they are well-formed.
3418 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3419 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3420 vdev_labeltype_t label)
3427 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3430 * It's acceptable to have no devs specified.
3432 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3436 return (SET_ERROR(EINVAL));
3439 * Make sure the pool is formatted with a version that supports this
3442 if (spa_version(spa) < version)
3443 return (SET_ERROR(ENOTSUP));
3446 * Set the pending device list so we correctly handle device in-use
3449 sav->sav_pending = dev;
3450 sav->sav_npending = ndev;
3452 for (i = 0; i < ndev; i++) {
3453 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3457 if (!vd->vdev_ops->vdev_op_leaf) {
3459 error = SET_ERROR(EINVAL);
3464 * The L2ARC currently only supports disk devices in
3465 * kernel context. For user-level testing, we allow it.
3468 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3469 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3470 error = SET_ERROR(ENOTBLK);
3477 if ((error = vdev_open(vd)) == 0 &&
3478 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3479 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3480 vd->vdev_guid) == 0);
3486 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3493 sav->sav_pending = NULL;
3494 sav->sav_npending = 0;
3499 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3503 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3505 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3506 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3507 VDEV_LABEL_SPARE)) != 0) {
3511 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3512 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3513 VDEV_LABEL_L2CACHE));
3517 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3522 if (sav->sav_config != NULL) {
3528 * Generate new dev list by concatentating with the
3531 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3532 &olddevs, &oldndevs) == 0);
3534 newdevs = kmem_alloc(sizeof (void *) *
3535 (ndevs + oldndevs), KM_SLEEP);
3536 for (i = 0; i < oldndevs; i++)
3537 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3539 for (i = 0; i < ndevs; i++)
3540 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3543 VERIFY(nvlist_remove(sav->sav_config, config,
3544 DATA_TYPE_NVLIST_ARRAY) == 0);
3546 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3547 config, newdevs, ndevs + oldndevs) == 0);
3548 for (i = 0; i < oldndevs + ndevs; i++)
3549 nvlist_free(newdevs[i]);
3550 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3553 * Generate a new dev list.
3555 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3557 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3563 * Stop and drop level 2 ARC devices
3566 spa_l2cache_drop(spa_t *spa)
3570 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3572 for (i = 0; i < sav->sav_count; i++) {
3575 vd = sav->sav_vdevs[i];
3578 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3579 pool != 0ULL && l2arc_vdev_present(vd))
3580 l2arc_remove_vdev(vd);
3588 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3592 char *altroot = NULL;
3597 uint64_t txg = TXG_INITIAL;
3598 nvlist_t **spares, **l2cache;
3599 uint_t nspares, nl2cache;
3600 uint64_t version, obj;
3601 boolean_t has_features;
3604 * If this pool already exists, return failure.
3606 mutex_enter(&spa_namespace_lock);
3607 if (spa_lookup(pool) != NULL) {
3608 mutex_exit(&spa_namespace_lock);
3609 return (SET_ERROR(EEXIST));
3613 * Allocate a new spa_t structure.
3615 (void) nvlist_lookup_string(props,
3616 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3617 spa = spa_add(pool, NULL, altroot);
3618 spa_activate(spa, spa_mode_global);
3620 if (props && (error = spa_prop_validate(spa, props))) {
3621 spa_deactivate(spa);
3623 mutex_exit(&spa_namespace_lock);
3627 has_features = B_FALSE;
3628 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3629 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3630 if (zpool_prop_feature(nvpair_name(elem)))
3631 has_features = B_TRUE;
3634 if (has_features || nvlist_lookup_uint64(props,
3635 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3636 version = SPA_VERSION;
3638 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3640 spa->spa_first_txg = txg;
3641 spa->spa_uberblock.ub_txg = txg - 1;
3642 spa->spa_uberblock.ub_version = version;
3643 spa->spa_ubsync = spa->spa_uberblock;
3646 * Create "The Godfather" zio to hold all async IOs
3648 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3650 for (int i = 0; i < max_ncpus; i++) {
3651 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3652 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3653 ZIO_FLAG_GODFATHER);
3657 * Create the root vdev.
3659 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3661 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3663 ASSERT(error != 0 || rvd != NULL);
3664 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3666 if (error == 0 && !zfs_allocatable_devs(nvroot))
3667 error = SET_ERROR(EINVAL);
3670 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3671 (error = spa_validate_aux(spa, nvroot, txg,
3672 VDEV_ALLOC_ADD)) == 0) {
3673 for (int c = 0; c < rvd->vdev_children; c++) {
3674 vdev_ashift_optimize(rvd->vdev_child[c]);
3675 vdev_metaslab_set_size(rvd->vdev_child[c]);
3676 vdev_expand(rvd->vdev_child[c], txg);
3680 spa_config_exit(spa, SCL_ALL, FTAG);
3684 spa_deactivate(spa);
3686 mutex_exit(&spa_namespace_lock);
3691 * Get the list of spares, if specified.
3693 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3694 &spares, &nspares) == 0) {
3695 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3697 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3698 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3699 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3700 spa_load_spares(spa);
3701 spa_config_exit(spa, SCL_ALL, FTAG);
3702 spa->spa_spares.sav_sync = B_TRUE;
3706 * Get the list of level 2 cache devices, if specified.
3708 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3709 &l2cache, &nl2cache) == 0) {
3710 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3711 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3712 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3713 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3714 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3715 spa_load_l2cache(spa);
3716 spa_config_exit(spa, SCL_ALL, FTAG);
3717 spa->spa_l2cache.sav_sync = B_TRUE;
3720 spa->spa_is_initializing = B_TRUE;
3721 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3722 spa->spa_meta_objset = dp->dp_meta_objset;
3723 spa->spa_is_initializing = B_FALSE;
3726 * Create DDTs (dedup tables).
3730 spa_update_dspace(spa);
3732 tx = dmu_tx_create_assigned(dp, txg);
3735 * Create the pool config object.
3737 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3738 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3739 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3741 if (zap_add(spa->spa_meta_objset,
3742 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3743 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3744 cmn_err(CE_PANIC, "failed to add pool config");
3747 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3748 spa_feature_create_zap_objects(spa, tx);
3750 if (zap_add(spa->spa_meta_objset,
3751 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3752 sizeof (uint64_t), 1, &version, tx) != 0) {
3753 cmn_err(CE_PANIC, "failed to add pool version");
3756 /* Newly created pools with the right version are always deflated. */
3757 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3758 spa->spa_deflate = TRUE;
3759 if (zap_add(spa->spa_meta_objset,
3760 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3761 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3762 cmn_err(CE_PANIC, "failed to add deflate");
3767 * Create the deferred-free bpobj. Turn off compression
3768 * because sync-to-convergence takes longer if the blocksize
3771 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3772 dmu_object_set_compress(spa->spa_meta_objset, obj,
3773 ZIO_COMPRESS_OFF, tx);
3774 if (zap_add(spa->spa_meta_objset,
3775 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3776 sizeof (uint64_t), 1, &obj, tx) != 0) {
3777 cmn_err(CE_PANIC, "failed to add bpobj");
3779 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3780 spa->spa_meta_objset, obj));
3783 * Create the pool's history object.
3785 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3786 spa_history_create_obj(spa, tx);
3789 * Generate some random noise for salted checksums to operate on.
3791 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
3792 sizeof (spa->spa_cksum_salt.zcs_bytes));
3795 * Set pool properties.
3797 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3798 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3799 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3800 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3802 if (props != NULL) {
3803 spa_configfile_set(spa, props, B_FALSE);
3804 spa_sync_props(props, tx);
3809 spa->spa_sync_on = B_TRUE;
3810 txg_sync_start(spa->spa_dsl_pool);
3813 * We explicitly wait for the first transaction to complete so that our
3814 * bean counters are appropriately updated.
3816 txg_wait_synced(spa->spa_dsl_pool, txg);
3818 spa_config_sync(spa, B_FALSE, B_TRUE);
3819 spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE);
3821 spa_history_log_version(spa, "create");
3824 * Don't count references from objsets that are already closed
3825 * and are making their way through the eviction process.
3827 spa_evicting_os_wait(spa);
3828 spa->spa_minref = refcount_count(&spa->spa_refcount);
3830 mutex_exit(&spa_namespace_lock);
3838 * Get the root pool information from the root disk, then import the root pool
3839 * during the system boot up time.
3841 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3844 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3847 nvlist_t *nvtop, *nvroot;
3850 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3854 * Add this top-level vdev to the child array.
3856 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3858 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3860 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3863 * Put this pool's top-level vdevs into a root vdev.
3865 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3866 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3867 VDEV_TYPE_ROOT) == 0);
3868 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3869 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3870 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3874 * Replace the existing vdev_tree with the new root vdev in
3875 * this pool's configuration (remove the old, add the new).
3877 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3878 nvlist_free(nvroot);
3883 * Walk the vdev tree and see if we can find a device with "better"
3884 * configuration. A configuration is "better" if the label on that
3885 * device has a more recent txg.
3888 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3890 for (int c = 0; c < vd->vdev_children; c++)
3891 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3893 if (vd->vdev_ops->vdev_op_leaf) {
3897 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3901 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3905 * Do we have a better boot device?
3907 if (label_txg > *txg) {
3916 * Import a root pool.
3918 * For x86. devpath_list will consist of devid and/or physpath name of
3919 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3920 * The GRUB "findroot" command will return the vdev we should boot.
3922 * For Sparc, devpath_list consists the physpath name of the booting device
3923 * no matter the rootpool is a single device pool or a mirrored pool.
3925 * "/pci@1f,0/ide@d/disk@0,0:a"
3928 spa_import_rootpool(char *devpath, char *devid)
3931 vdev_t *rvd, *bvd, *avd = NULL;
3932 nvlist_t *config, *nvtop;
3938 * Read the label from the boot device and generate a configuration.
3940 config = spa_generate_rootconf(devpath, devid, &guid);
3941 #if defined(_OBP) && defined(_KERNEL)
3942 if (config == NULL) {
3943 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3945 get_iscsi_bootpath_phy(devpath);
3946 config = spa_generate_rootconf(devpath, devid, &guid);
3950 if (config == NULL) {
3951 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3953 return (SET_ERROR(EIO));
3956 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3958 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3960 mutex_enter(&spa_namespace_lock);
3961 if ((spa = spa_lookup(pname)) != NULL) {
3963 * Remove the existing root pool from the namespace so that we
3964 * can replace it with the correct config we just read in.
3969 spa = spa_add(pname, config, NULL);
3970 spa->spa_is_root = B_TRUE;
3971 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3974 * Build up a vdev tree based on the boot device's label config.
3976 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3978 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3979 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3980 VDEV_ALLOC_ROOTPOOL);
3981 spa_config_exit(spa, SCL_ALL, FTAG);
3983 mutex_exit(&spa_namespace_lock);
3984 nvlist_free(config);
3985 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3991 * Get the boot vdev.
3993 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3994 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3995 (u_longlong_t)guid);
3996 error = SET_ERROR(ENOENT);
4001 * Determine if there is a better boot device.
4004 spa_alt_rootvdev(rvd, &avd, &txg);
4006 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
4007 "try booting from '%s'", avd->vdev_path);
4008 error = SET_ERROR(EINVAL);
4013 * If the boot device is part of a spare vdev then ensure that
4014 * we're booting off the active spare.
4016 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4017 !bvd->vdev_isspare) {
4018 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
4019 "try booting from '%s'",
4021 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
4022 error = SET_ERROR(EINVAL);
4028 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4030 spa_config_exit(spa, SCL_ALL, FTAG);
4031 mutex_exit(&spa_namespace_lock);
4033 nvlist_free(config);
4037 #else /* !illumos */
4039 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
4043 spa_generate_rootconf(const char *name)
4045 nvlist_t **configs, **tops;
4047 nvlist_t *best_cfg, *nvtop, *nvroot;
4056 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
4059 ASSERT3U(count, !=, 0);
4061 for (i = 0; i < count; i++) {
4064 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
4066 if (txg > best_txg) {
4068 best_cfg = configs[i];
4073 * Multi-vdev root pool configuration discovery is not supported yet.
4076 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
4078 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
4081 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
4082 for (i = 0; i < nchildren; i++) {
4085 if (configs[i] == NULL)
4087 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
4089 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
4091 for (i = 0; holes != NULL && i < nholes; i++) {
4094 if (tops[holes[i]] != NULL)
4096 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
4097 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
4098 VDEV_TYPE_HOLE) == 0);
4099 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
4101 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4104 for (i = 0; i < nchildren; i++) {
4105 if (tops[i] != NULL)
4107 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4108 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4109 VDEV_TYPE_MISSING) == 0);
4110 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4112 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4117 * Create pool config based on the best vdev config.
4119 nvlist_dup(best_cfg, &config, KM_SLEEP);
4122 * Put this pool's top-level vdevs into a root vdev.
4124 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4126 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4127 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4128 VDEV_TYPE_ROOT) == 0);
4129 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4130 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4131 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4132 tops, nchildren) == 0);
4135 * Replace the existing vdev_tree with the new root vdev in
4136 * this pool's configuration (remove the old, add the new).
4138 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4141 * Drop vdev config elements that should not be present at pool level.
4143 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4144 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4146 for (i = 0; i < count; i++)
4147 nvlist_free(configs[i]);
4148 kmem_free(configs, count * sizeof(void *));
4149 for (i = 0; i < nchildren; i++)
4150 nvlist_free(tops[i]);
4151 kmem_free(tops, nchildren * sizeof(void *));
4152 nvlist_free(nvroot);
4157 spa_import_rootpool(const char *name)
4160 vdev_t *rvd, *bvd, *avd = NULL;
4161 nvlist_t *config, *nvtop;
4167 * Read the label from the boot device and generate a configuration.
4169 config = spa_generate_rootconf(name);
4171 mutex_enter(&spa_namespace_lock);
4172 if (config != NULL) {
4173 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4174 &pname) == 0 && strcmp(name, pname) == 0);
4175 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4178 if ((spa = spa_lookup(pname)) != NULL) {
4180 * Remove the existing root pool from the namespace so
4181 * that we can replace it with the correct config
4186 spa = spa_add(pname, config, NULL);
4189 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4190 * via spa_version().
4192 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4193 &spa->spa_ubsync.ub_version) != 0)
4194 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4195 } else if ((spa = spa_lookup(name)) == NULL) {
4196 mutex_exit(&spa_namespace_lock);
4197 nvlist_free(config);
4198 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4202 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4204 spa->spa_is_root = B_TRUE;
4205 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4208 * Build up a vdev tree based on the boot device's label config.
4210 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4212 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4213 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4214 VDEV_ALLOC_ROOTPOOL);
4215 spa_config_exit(spa, SCL_ALL, FTAG);
4217 mutex_exit(&spa_namespace_lock);
4218 nvlist_free(config);
4219 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4224 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4226 spa_config_exit(spa, SCL_ALL, FTAG);
4227 mutex_exit(&spa_namespace_lock);
4229 nvlist_free(config);
4233 #endif /* illumos */
4234 #endif /* _KERNEL */
4237 * Import a non-root pool into the system.
4240 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4243 char *altroot = NULL;
4244 spa_load_state_t state = SPA_LOAD_IMPORT;
4245 zpool_rewind_policy_t policy;
4246 uint64_t mode = spa_mode_global;
4247 uint64_t readonly = B_FALSE;
4250 nvlist_t **spares, **l2cache;
4251 uint_t nspares, nl2cache;
4254 * If a pool with this name exists, return failure.
4256 mutex_enter(&spa_namespace_lock);
4257 if (spa_lookup(pool) != NULL) {
4258 mutex_exit(&spa_namespace_lock);
4259 return (SET_ERROR(EEXIST));
4263 * Create and initialize the spa structure.
4265 (void) nvlist_lookup_string(props,
4266 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4267 (void) nvlist_lookup_uint64(props,
4268 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4271 spa = spa_add(pool, config, altroot);
4272 spa->spa_import_flags = flags;
4275 * Verbatim import - Take a pool and insert it into the namespace
4276 * as if it had been loaded at boot.
4278 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4280 spa_configfile_set(spa, props, B_FALSE);
4282 spa_config_sync(spa, B_FALSE, B_TRUE);
4283 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4285 mutex_exit(&spa_namespace_lock);
4289 spa_activate(spa, mode);
4292 * Don't start async tasks until we know everything is healthy.
4294 spa_async_suspend(spa);
4296 zpool_get_rewind_policy(config, &policy);
4297 if (policy.zrp_request & ZPOOL_DO_REWIND)
4298 state = SPA_LOAD_RECOVER;
4301 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4302 * because the user-supplied config is actually the one to trust when
4305 if (state != SPA_LOAD_RECOVER)
4306 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4308 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4309 policy.zrp_request);
4312 * Propagate anything learned while loading the pool and pass it
4313 * back to caller (i.e. rewind info, missing devices, etc).
4315 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4316 spa->spa_load_info) == 0);
4318 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4320 * Toss any existing sparelist, as it doesn't have any validity
4321 * anymore, and conflicts with spa_has_spare().
4323 if (spa->spa_spares.sav_config) {
4324 nvlist_free(spa->spa_spares.sav_config);
4325 spa->spa_spares.sav_config = NULL;
4326 spa_load_spares(spa);
4328 if (spa->spa_l2cache.sav_config) {
4329 nvlist_free(spa->spa_l2cache.sav_config);
4330 spa->spa_l2cache.sav_config = NULL;
4331 spa_load_l2cache(spa);
4334 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4337 error = spa_validate_aux(spa, nvroot, -1ULL,
4340 error = spa_validate_aux(spa, nvroot, -1ULL,
4341 VDEV_ALLOC_L2CACHE);
4342 spa_config_exit(spa, SCL_ALL, FTAG);
4345 spa_configfile_set(spa, props, B_FALSE);
4347 if (error != 0 || (props && spa_writeable(spa) &&
4348 (error = spa_prop_set(spa, props)))) {
4350 spa_deactivate(spa);
4352 mutex_exit(&spa_namespace_lock);
4356 spa_async_resume(spa);
4359 * Override any spares and level 2 cache devices as specified by
4360 * the user, as these may have correct device names/devids, etc.
4362 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4363 &spares, &nspares) == 0) {
4364 if (spa->spa_spares.sav_config)
4365 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4366 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4368 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4369 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4370 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4371 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4372 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4373 spa_load_spares(spa);
4374 spa_config_exit(spa, SCL_ALL, FTAG);
4375 spa->spa_spares.sav_sync = B_TRUE;
4377 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4378 &l2cache, &nl2cache) == 0) {
4379 if (spa->spa_l2cache.sav_config)
4380 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4381 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4383 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4384 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4385 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4386 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4387 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4388 spa_load_l2cache(spa);
4389 spa_config_exit(spa, SCL_ALL, FTAG);
4390 spa->spa_l2cache.sav_sync = B_TRUE;
4394 * Check for any removed devices.
4396 if (spa->spa_autoreplace) {
4397 spa_aux_check_removed(&spa->spa_spares);
4398 spa_aux_check_removed(&spa->spa_l2cache);
4401 if (spa_writeable(spa)) {
4403 * Update the config cache to include the newly-imported pool.
4405 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4409 * It's possible that the pool was expanded while it was exported.
4410 * We kick off an async task to handle this for us.
4412 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4414 spa_history_log_version(spa, "import");
4416 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4418 mutex_exit(&spa_namespace_lock);
4422 zvol_create_minors(pool);
4429 spa_tryimport(nvlist_t *tryconfig)
4431 nvlist_t *config = NULL;
4437 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4440 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4444 * Create and initialize the spa structure.
4446 mutex_enter(&spa_namespace_lock);
4447 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4448 spa_activate(spa, FREAD);
4451 * Pass off the heavy lifting to spa_load().
4452 * Pass TRUE for mosconfig because the user-supplied config
4453 * is actually the one to trust when doing an import.
4455 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4458 * If 'tryconfig' was at least parsable, return the current config.
4460 if (spa->spa_root_vdev != NULL) {
4461 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4462 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4464 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4466 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4467 spa->spa_uberblock.ub_timestamp) == 0);
4468 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4469 spa->spa_load_info) == 0);
4472 * If the bootfs property exists on this pool then we
4473 * copy it out so that external consumers can tell which
4474 * pools are bootable.
4476 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4477 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4480 * We have to play games with the name since the
4481 * pool was opened as TRYIMPORT_NAME.
4483 if (dsl_dsobj_to_dsname(spa_name(spa),
4484 spa->spa_bootfs, tmpname) == 0) {
4486 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4488 cp = strchr(tmpname, '/');
4490 (void) strlcpy(dsname, tmpname,
4493 (void) snprintf(dsname, MAXPATHLEN,
4494 "%s/%s", poolname, ++cp);
4496 VERIFY(nvlist_add_string(config,
4497 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4498 kmem_free(dsname, MAXPATHLEN);
4500 kmem_free(tmpname, MAXPATHLEN);
4504 * Add the list of hot spares and level 2 cache devices.
4506 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4507 spa_add_spares(spa, config);
4508 spa_add_l2cache(spa, config);
4509 spa_config_exit(spa, SCL_CONFIG, FTAG);
4513 spa_deactivate(spa);
4515 mutex_exit(&spa_namespace_lock);
4521 * Pool export/destroy
4523 * The act of destroying or exporting a pool is very simple. We make sure there
4524 * is no more pending I/O and any references to the pool are gone. Then, we
4525 * update the pool state and sync all the labels to disk, removing the
4526 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4527 * we don't sync the labels or remove the configuration cache.
4530 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4531 boolean_t force, boolean_t hardforce)
4538 if (!(spa_mode_global & FWRITE))
4539 return (SET_ERROR(EROFS));
4541 mutex_enter(&spa_namespace_lock);
4542 if ((spa = spa_lookup(pool)) == NULL) {
4543 mutex_exit(&spa_namespace_lock);
4544 return (SET_ERROR(ENOENT));
4548 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4549 * reacquire the namespace lock, and see if we can export.
4551 spa_open_ref(spa, FTAG);
4552 mutex_exit(&spa_namespace_lock);
4553 spa_async_suspend(spa);
4554 mutex_enter(&spa_namespace_lock);
4555 spa_close(spa, FTAG);
4558 * The pool will be in core if it's openable,
4559 * in which case we can modify its state.
4561 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4563 * Objsets may be open only because they're dirty, so we
4564 * have to force it to sync before checking spa_refcnt.
4566 txg_wait_synced(spa->spa_dsl_pool, 0);
4567 spa_evicting_os_wait(spa);
4570 * A pool cannot be exported or destroyed if there are active
4571 * references. If we are resetting a pool, allow references by
4572 * fault injection handlers.
4574 if (!spa_refcount_zero(spa) ||
4575 (spa->spa_inject_ref != 0 &&
4576 new_state != POOL_STATE_UNINITIALIZED)) {
4577 spa_async_resume(spa);
4578 mutex_exit(&spa_namespace_lock);
4579 return (SET_ERROR(EBUSY));
4583 * A pool cannot be exported if it has an active shared spare.
4584 * This is to prevent other pools stealing the active spare
4585 * from an exported pool. At user's own will, such pool can
4586 * be forcedly exported.
4588 if (!force && new_state == POOL_STATE_EXPORTED &&
4589 spa_has_active_shared_spare(spa)) {
4590 spa_async_resume(spa);
4591 mutex_exit(&spa_namespace_lock);
4592 return (SET_ERROR(EXDEV));
4596 * We want this to be reflected on every label,
4597 * so mark them all dirty. spa_unload() will do the
4598 * final sync that pushes these changes out.
4600 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4601 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4602 spa->spa_state = new_state;
4603 spa->spa_final_txg = spa_last_synced_txg(spa) +
4605 vdev_config_dirty(spa->spa_root_vdev);
4606 spa_config_exit(spa, SCL_ALL, FTAG);
4610 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4612 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4614 spa_deactivate(spa);
4617 if (oldconfig && spa->spa_config)
4618 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4620 if (new_state != POOL_STATE_UNINITIALIZED) {
4622 spa_config_sync(spa, B_TRUE, B_TRUE);
4625 mutex_exit(&spa_namespace_lock);
4631 * Destroy a storage pool.
4634 spa_destroy(char *pool)
4636 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4641 * Export a storage pool.
4644 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4645 boolean_t hardforce)
4647 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4652 * Similar to spa_export(), this unloads the spa_t without actually removing it
4653 * from the namespace in any way.
4656 spa_reset(char *pool)
4658 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4663 * ==========================================================================
4664 * Device manipulation
4665 * ==========================================================================
4669 * Add a device to a storage pool.
4672 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4676 vdev_t *rvd = spa->spa_root_vdev;
4678 nvlist_t **spares, **l2cache;
4679 uint_t nspares, nl2cache;
4681 ASSERT(spa_writeable(spa));
4683 txg = spa_vdev_enter(spa);
4685 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4686 VDEV_ALLOC_ADD)) != 0)
4687 return (spa_vdev_exit(spa, NULL, txg, error));
4689 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4691 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4695 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4699 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4700 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4702 if (vd->vdev_children != 0 &&
4703 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4704 return (spa_vdev_exit(spa, vd, txg, error));
4707 * We must validate the spares and l2cache devices after checking the
4708 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4710 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4711 return (spa_vdev_exit(spa, vd, txg, error));
4714 * Transfer each new top-level vdev from vd to rvd.
4716 for (int c = 0; c < vd->vdev_children; c++) {
4719 * Set the vdev id to the first hole, if one exists.
4721 for (id = 0; id < rvd->vdev_children; id++) {
4722 if (rvd->vdev_child[id]->vdev_ishole) {
4723 vdev_free(rvd->vdev_child[id]);
4727 tvd = vd->vdev_child[c];
4728 vdev_remove_child(vd, tvd);
4730 vdev_add_child(rvd, tvd);
4731 vdev_config_dirty(tvd);
4735 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4736 ZPOOL_CONFIG_SPARES);
4737 spa_load_spares(spa);
4738 spa->spa_spares.sav_sync = B_TRUE;
4741 if (nl2cache != 0) {
4742 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4743 ZPOOL_CONFIG_L2CACHE);
4744 spa_load_l2cache(spa);
4745 spa->spa_l2cache.sav_sync = B_TRUE;
4749 * We have to be careful when adding new vdevs to an existing pool.
4750 * If other threads start allocating from these vdevs before we
4751 * sync the config cache, and we lose power, then upon reboot we may
4752 * fail to open the pool because there are DVAs that the config cache
4753 * can't translate. Therefore, we first add the vdevs without
4754 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4755 * and then let spa_config_update() initialize the new metaslabs.
4757 * spa_load() checks for added-but-not-initialized vdevs, so that
4758 * if we lose power at any point in this sequence, the remaining
4759 * steps will be completed the next time we load the pool.
4761 (void) spa_vdev_exit(spa, vd, txg, 0);
4763 mutex_enter(&spa_namespace_lock);
4764 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4765 spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD);
4766 mutex_exit(&spa_namespace_lock);
4772 * Attach a device to a mirror. The arguments are the path to any device
4773 * in the mirror, and the nvroot for the new device. If the path specifies
4774 * a device that is not mirrored, we automatically insert the mirror vdev.
4776 * If 'replacing' is specified, the new device is intended to replace the
4777 * existing device; in this case the two devices are made into their own
4778 * mirror using the 'replacing' vdev, which is functionally identical to
4779 * the mirror vdev (it actually reuses all the same ops) but has a few
4780 * extra rules: you can't attach to it after it's been created, and upon
4781 * completion of resilvering, the first disk (the one being replaced)
4782 * is automatically detached.
4785 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4787 uint64_t txg, dtl_max_txg;
4788 vdev_t *rvd = spa->spa_root_vdev;
4789 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4791 char *oldvdpath, *newvdpath;
4795 ASSERT(spa_writeable(spa));
4797 txg = spa_vdev_enter(spa);
4799 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4802 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4804 if (!oldvd->vdev_ops->vdev_op_leaf)
4805 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4807 pvd = oldvd->vdev_parent;
4809 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4810 VDEV_ALLOC_ATTACH)) != 0)
4811 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4813 if (newrootvd->vdev_children != 1)
4814 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4816 newvd = newrootvd->vdev_child[0];
4818 if (!newvd->vdev_ops->vdev_op_leaf)
4819 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4821 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4822 return (spa_vdev_exit(spa, newrootvd, txg, error));
4825 * Spares can't replace logs
4827 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4828 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4832 * For attach, the only allowable parent is a mirror or the root
4835 if (pvd->vdev_ops != &vdev_mirror_ops &&
4836 pvd->vdev_ops != &vdev_root_ops)
4837 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4839 pvops = &vdev_mirror_ops;
4842 * Active hot spares can only be replaced by inactive hot
4845 if (pvd->vdev_ops == &vdev_spare_ops &&
4846 oldvd->vdev_isspare &&
4847 !spa_has_spare(spa, newvd->vdev_guid))
4848 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4851 * If the source is a hot spare, and the parent isn't already a
4852 * spare, then we want to create a new hot spare. Otherwise, we
4853 * want to create a replacing vdev. The user is not allowed to
4854 * attach to a spared vdev child unless the 'isspare' state is
4855 * the same (spare replaces spare, non-spare replaces
4858 if (pvd->vdev_ops == &vdev_replacing_ops &&
4859 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4860 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4861 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4862 newvd->vdev_isspare != oldvd->vdev_isspare) {
4863 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4866 if (newvd->vdev_isspare)
4867 pvops = &vdev_spare_ops;
4869 pvops = &vdev_replacing_ops;
4873 * Make sure the new device is big enough.
4875 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4876 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4879 * The new device cannot have a higher alignment requirement
4880 * than the top-level vdev.
4882 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4883 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4886 * If this is an in-place replacement, update oldvd's path and devid
4887 * to make it distinguishable from newvd, and unopenable from now on.
4889 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4890 spa_strfree(oldvd->vdev_path);
4891 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4893 (void) sprintf(oldvd->vdev_path, "%s/%s",
4894 newvd->vdev_path, "old");
4895 if (oldvd->vdev_devid != NULL) {
4896 spa_strfree(oldvd->vdev_devid);
4897 oldvd->vdev_devid = NULL;
4901 /* mark the device being resilvered */
4902 newvd->vdev_resilver_txg = txg;
4905 * If the parent is not a mirror, or if we're replacing, insert the new
4906 * mirror/replacing/spare vdev above oldvd.
4908 if (pvd->vdev_ops != pvops)
4909 pvd = vdev_add_parent(oldvd, pvops);
4911 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4912 ASSERT(pvd->vdev_ops == pvops);
4913 ASSERT(oldvd->vdev_parent == pvd);
4916 * Extract the new device from its root and add it to pvd.
4918 vdev_remove_child(newrootvd, newvd);
4919 newvd->vdev_id = pvd->vdev_children;
4920 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4921 vdev_add_child(pvd, newvd);
4923 tvd = newvd->vdev_top;
4924 ASSERT(pvd->vdev_top == tvd);
4925 ASSERT(tvd->vdev_parent == rvd);
4927 vdev_config_dirty(tvd);
4930 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4931 * for any dmu_sync-ed blocks. It will propagate upward when
4932 * spa_vdev_exit() calls vdev_dtl_reassess().
4934 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4936 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4937 dtl_max_txg - TXG_INITIAL);
4939 if (newvd->vdev_isspare) {
4940 spa_spare_activate(newvd);
4941 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4944 oldvdpath = spa_strdup(oldvd->vdev_path);
4945 newvdpath = spa_strdup(newvd->vdev_path);
4946 newvd_isspare = newvd->vdev_isspare;
4949 * Mark newvd's DTL dirty in this txg.
4951 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4954 * Schedule the resilver to restart in the future. We do this to
4955 * ensure that dmu_sync-ed blocks have been stitched into the
4956 * respective datasets.
4958 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4960 if (spa->spa_bootfs)
4961 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4963 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH);
4968 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4970 spa_history_log_internal(spa, "vdev attach", NULL,
4971 "%s vdev=%s %s vdev=%s",
4972 replacing && newvd_isspare ? "spare in" :
4973 replacing ? "replace" : "attach", newvdpath,
4974 replacing ? "for" : "to", oldvdpath);
4976 spa_strfree(oldvdpath);
4977 spa_strfree(newvdpath);
4983 * Detach a device from a mirror or replacing vdev.
4985 * If 'replace_done' is specified, only detach if the parent
4986 * is a replacing vdev.
4989 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4993 vdev_t *rvd = spa->spa_root_vdev;
4994 vdev_t *vd, *pvd, *cvd, *tvd;
4995 boolean_t unspare = B_FALSE;
4996 uint64_t unspare_guid = 0;
4999 ASSERT(spa_writeable(spa));
5001 txg = spa_vdev_enter(spa);
5003 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5006 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
5008 if (!vd->vdev_ops->vdev_op_leaf)
5009 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5011 pvd = vd->vdev_parent;
5014 * If the parent/child relationship is not as expected, don't do it.
5015 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
5016 * vdev that's replacing B with C. The user's intent in replacing
5017 * is to go from M(A,B) to M(A,C). If the user decides to cancel
5018 * the replace by detaching C, the expected behavior is to end up
5019 * M(A,B). But suppose that right after deciding to detach C,
5020 * the replacement of B completes. We would have M(A,C), and then
5021 * ask to detach C, which would leave us with just A -- not what
5022 * the user wanted. To prevent this, we make sure that the
5023 * parent/child relationship hasn't changed -- in this example,
5024 * that C's parent is still the replacing vdev R.
5026 if (pvd->vdev_guid != pguid && pguid != 0)
5027 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5030 * Only 'replacing' or 'spare' vdevs can be replaced.
5032 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
5033 pvd->vdev_ops != &vdev_spare_ops)
5034 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5036 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
5037 spa_version(spa) >= SPA_VERSION_SPARES);
5040 * Only mirror, replacing, and spare vdevs support detach.
5042 if (pvd->vdev_ops != &vdev_replacing_ops &&
5043 pvd->vdev_ops != &vdev_mirror_ops &&
5044 pvd->vdev_ops != &vdev_spare_ops)
5045 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5048 * If this device has the only valid copy of some data,
5049 * we cannot safely detach it.
5051 if (vdev_dtl_required(vd))
5052 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5054 ASSERT(pvd->vdev_children >= 2);
5057 * If we are detaching the second disk from a replacing vdev, then
5058 * check to see if we changed the original vdev's path to have "/old"
5059 * at the end in spa_vdev_attach(). If so, undo that change now.
5061 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
5062 vd->vdev_path != NULL) {
5063 size_t len = strlen(vd->vdev_path);
5065 for (int c = 0; c < pvd->vdev_children; c++) {
5066 cvd = pvd->vdev_child[c];
5068 if (cvd == vd || cvd->vdev_path == NULL)
5071 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
5072 strcmp(cvd->vdev_path + len, "/old") == 0) {
5073 spa_strfree(cvd->vdev_path);
5074 cvd->vdev_path = spa_strdup(vd->vdev_path);
5081 * If we are detaching the original disk from a spare, then it implies
5082 * that the spare should become a real disk, and be removed from the
5083 * active spare list for the pool.
5085 if (pvd->vdev_ops == &vdev_spare_ops &&
5087 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
5091 * Erase the disk labels so the disk can be used for other things.
5092 * This must be done after all other error cases are handled,
5093 * but before we disembowel vd (so we can still do I/O to it).
5094 * But if we can't do it, don't treat the error as fatal --
5095 * it may be that the unwritability of the disk is the reason
5096 * it's being detached!
5098 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5101 * Remove vd from its parent and compact the parent's children.
5103 vdev_remove_child(pvd, vd);
5104 vdev_compact_children(pvd);
5107 * Remember one of the remaining children so we can get tvd below.
5109 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5112 * If we need to remove the remaining child from the list of hot spares,
5113 * do it now, marking the vdev as no longer a spare in the process.
5114 * We must do this before vdev_remove_parent(), because that can
5115 * change the GUID if it creates a new toplevel GUID. For a similar
5116 * reason, we must remove the spare now, in the same txg as the detach;
5117 * otherwise someone could attach a new sibling, change the GUID, and
5118 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5121 ASSERT(cvd->vdev_isspare);
5122 spa_spare_remove(cvd);
5123 unspare_guid = cvd->vdev_guid;
5124 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5125 cvd->vdev_unspare = B_TRUE;
5129 * If the parent mirror/replacing vdev only has one child,
5130 * the parent is no longer needed. Remove it from the tree.
5132 if (pvd->vdev_children == 1) {
5133 if (pvd->vdev_ops == &vdev_spare_ops)
5134 cvd->vdev_unspare = B_FALSE;
5135 vdev_remove_parent(cvd);
5140 * We don't set tvd until now because the parent we just removed
5141 * may have been the previous top-level vdev.
5143 tvd = cvd->vdev_top;
5144 ASSERT(tvd->vdev_parent == rvd);
5147 * Reevaluate the parent vdev state.
5149 vdev_propagate_state(cvd);
5152 * If the 'autoexpand' property is set on the pool then automatically
5153 * try to expand the size of the pool. For example if the device we
5154 * just detached was smaller than the others, it may be possible to
5155 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5156 * first so that we can obtain the updated sizes of the leaf vdevs.
5158 if (spa->spa_autoexpand) {
5160 vdev_expand(tvd, txg);
5163 vdev_config_dirty(tvd);
5166 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5167 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5168 * But first make sure we're not on any *other* txg's DTL list, to
5169 * prevent vd from being accessed after it's freed.
5171 vdpath = spa_strdup(vd->vdev_path);
5172 for (int t = 0; t < TXG_SIZE; t++)
5173 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5174 vd->vdev_detached = B_TRUE;
5175 vdev_dirty(tvd, VDD_DTL, vd, txg);
5177 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5179 /* hang on to the spa before we release the lock */
5180 spa_open_ref(spa, FTAG);
5182 error = spa_vdev_exit(spa, vd, txg, 0);
5184 spa_history_log_internal(spa, "detach", NULL,
5186 spa_strfree(vdpath);
5189 * If this was the removal of the original device in a hot spare vdev,
5190 * then we want to go through and remove the device from the hot spare
5191 * list of every other pool.
5194 spa_t *altspa = NULL;
5196 mutex_enter(&spa_namespace_lock);
5197 while ((altspa = spa_next(altspa)) != NULL) {
5198 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5202 spa_open_ref(altspa, FTAG);
5203 mutex_exit(&spa_namespace_lock);
5204 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5205 mutex_enter(&spa_namespace_lock);
5206 spa_close(altspa, FTAG);
5208 mutex_exit(&spa_namespace_lock);
5210 /* search the rest of the vdevs for spares to remove */
5211 spa_vdev_resilver_done(spa);
5214 /* all done with the spa; OK to release */
5215 mutex_enter(&spa_namespace_lock);
5216 spa_close(spa, FTAG);
5217 mutex_exit(&spa_namespace_lock);
5223 * Split a set of devices from their mirrors, and create a new pool from them.
5226 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5227 nvlist_t *props, boolean_t exp)
5230 uint64_t txg, *glist;
5232 uint_t c, children, lastlog;
5233 nvlist_t **child, *nvl, *tmp;
5235 char *altroot = NULL;
5236 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5237 boolean_t activate_slog;
5239 ASSERT(spa_writeable(spa));
5241 txg = spa_vdev_enter(spa);
5243 /* clear the log and flush everything up to now */
5244 activate_slog = spa_passivate_log(spa);
5245 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5246 error = spa_offline_log(spa);
5247 txg = spa_vdev_config_enter(spa);
5250 spa_activate_log(spa);
5253 return (spa_vdev_exit(spa, NULL, txg, error));
5255 /* check new spa name before going any further */
5256 if (spa_lookup(newname) != NULL)
5257 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5260 * scan through all the children to ensure they're all mirrors
5262 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5263 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5265 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5267 /* first, check to ensure we've got the right child count */
5268 rvd = spa->spa_root_vdev;
5270 for (c = 0; c < rvd->vdev_children; c++) {
5271 vdev_t *vd = rvd->vdev_child[c];
5273 /* don't count the holes & logs as children */
5274 if (vd->vdev_islog || vd->vdev_ishole) {
5282 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5283 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5285 /* next, ensure no spare or cache devices are part of the split */
5286 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5287 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5288 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5290 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5291 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5293 /* then, loop over each vdev and validate it */
5294 for (c = 0; c < children; c++) {
5295 uint64_t is_hole = 0;
5297 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5301 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5302 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5305 error = SET_ERROR(EINVAL);
5310 /* which disk is going to be split? */
5311 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5313 error = SET_ERROR(EINVAL);
5317 /* look it up in the spa */
5318 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5319 if (vml[c] == NULL) {
5320 error = SET_ERROR(ENODEV);
5324 /* make sure there's nothing stopping the split */
5325 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5326 vml[c]->vdev_islog ||
5327 vml[c]->vdev_ishole ||
5328 vml[c]->vdev_isspare ||
5329 vml[c]->vdev_isl2cache ||
5330 !vdev_writeable(vml[c]) ||
5331 vml[c]->vdev_children != 0 ||
5332 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5333 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5334 error = SET_ERROR(EINVAL);
5338 if (vdev_dtl_required(vml[c])) {
5339 error = SET_ERROR(EBUSY);
5343 /* we need certain info from the top level */
5344 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5345 vml[c]->vdev_top->vdev_ms_array) == 0);
5346 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5347 vml[c]->vdev_top->vdev_ms_shift) == 0);
5348 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5349 vml[c]->vdev_top->vdev_asize) == 0);
5350 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5351 vml[c]->vdev_top->vdev_ashift) == 0);
5355 kmem_free(vml, children * sizeof (vdev_t *));
5356 kmem_free(glist, children * sizeof (uint64_t));
5357 return (spa_vdev_exit(spa, NULL, txg, error));
5360 /* stop writers from using the disks */
5361 for (c = 0; c < children; c++) {
5363 vml[c]->vdev_offline = B_TRUE;
5365 vdev_reopen(spa->spa_root_vdev);
5368 * Temporarily record the splitting vdevs in the spa config. This
5369 * will disappear once the config is regenerated.
5371 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5372 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5373 glist, children) == 0);
5374 kmem_free(glist, children * sizeof (uint64_t));
5376 mutex_enter(&spa->spa_props_lock);
5377 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5379 mutex_exit(&spa->spa_props_lock);
5380 spa->spa_config_splitting = nvl;
5381 vdev_config_dirty(spa->spa_root_vdev);
5383 /* configure and create the new pool */
5384 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5385 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5386 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5387 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5388 spa_version(spa)) == 0);
5389 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5390 spa->spa_config_txg) == 0);
5391 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5392 spa_generate_guid(NULL)) == 0);
5393 (void) nvlist_lookup_string(props,
5394 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5396 /* add the new pool to the namespace */
5397 newspa = spa_add(newname, config, altroot);
5398 newspa->spa_config_txg = spa->spa_config_txg;
5399 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5401 /* release the spa config lock, retaining the namespace lock */
5402 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5404 if (zio_injection_enabled)
5405 zio_handle_panic_injection(spa, FTAG, 1);
5407 spa_activate(newspa, spa_mode_global);
5408 spa_async_suspend(newspa);
5411 /* mark that we are creating new spa by splitting */
5412 newspa->spa_splitting_newspa = B_TRUE;
5414 /* create the new pool from the disks of the original pool */
5415 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5417 newspa->spa_splitting_newspa = B_FALSE;
5422 /* if that worked, generate a real config for the new pool */
5423 if (newspa->spa_root_vdev != NULL) {
5424 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5425 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5426 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5427 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5428 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5433 if (props != NULL) {
5434 spa_configfile_set(newspa, props, B_FALSE);
5435 error = spa_prop_set(newspa, props);
5440 /* flush everything */
5441 txg = spa_vdev_config_enter(newspa);
5442 vdev_config_dirty(newspa->spa_root_vdev);
5443 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5445 if (zio_injection_enabled)
5446 zio_handle_panic_injection(spa, FTAG, 2);
5448 spa_async_resume(newspa);
5450 /* finally, update the original pool's config */
5451 txg = spa_vdev_config_enter(spa);
5452 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5453 error = dmu_tx_assign(tx, TXG_WAIT);
5456 for (c = 0; c < children; c++) {
5457 if (vml[c] != NULL) {
5460 spa_history_log_internal(spa, "detach", tx,
5461 "vdev=%s", vml[c]->vdev_path);
5465 vdev_config_dirty(spa->spa_root_vdev);
5466 spa->spa_config_splitting = NULL;
5470 (void) spa_vdev_exit(spa, NULL, txg, 0);
5472 if (zio_injection_enabled)
5473 zio_handle_panic_injection(spa, FTAG, 3);
5475 /* split is complete; log a history record */
5476 spa_history_log_internal(newspa, "split", NULL,
5477 "from pool %s", spa_name(spa));
5479 kmem_free(vml, children * sizeof (vdev_t *));
5481 /* if we're not going to mount the filesystems in userland, export */
5483 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5490 spa_deactivate(newspa);
5493 txg = spa_vdev_config_enter(spa);
5495 /* re-online all offlined disks */
5496 for (c = 0; c < children; c++) {
5498 vml[c]->vdev_offline = B_FALSE;
5500 vdev_reopen(spa->spa_root_vdev);
5502 nvlist_free(spa->spa_config_splitting);
5503 spa->spa_config_splitting = NULL;
5504 (void) spa_vdev_exit(spa, NULL, txg, error);
5506 kmem_free(vml, children * sizeof (vdev_t *));
5511 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5513 for (int i = 0; i < count; i++) {
5516 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5519 if (guid == target_guid)
5527 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5528 nvlist_t *dev_to_remove)
5530 nvlist_t **newdev = NULL;
5533 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5535 for (int i = 0, j = 0; i < count; i++) {
5536 if (dev[i] == dev_to_remove)
5538 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5541 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5542 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5544 for (int i = 0; i < count - 1; i++)
5545 nvlist_free(newdev[i]);
5548 kmem_free(newdev, (count - 1) * sizeof (void *));
5552 * Evacuate the device.
5555 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5560 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5561 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5562 ASSERT(vd == vd->vdev_top);
5565 * Evacuate the device. We don't hold the config lock as writer
5566 * since we need to do I/O but we do keep the
5567 * spa_namespace_lock held. Once this completes the device
5568 * should no longer have any blocks allocated on it.
5570 if (vd->vdev_islog) {
5571 if (vd->vdev_stat.vs_alloc != 0)
5572 error = spa_offline_log(spa);
5574 error = SET_ERROR(ENOTSUP);
5581 * The evacuation succeeded. Remove any remaining MOS metadata
5582 * associated with this vdev, and wait for these changes to sync.
5584 ASSERT0(vd->vdev_stat.vs_alloc);
5585 txg = spa_vdev_config_enter(spa);
5586 vd->vdev_removing = B_TRUE;
5587 vdev_dirty_leaves(vd, VDD_DTL, txg);
5588 vdev_config_dirty(vd);
5589 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5595 * Complete the removal by cleaning up the namespace.
5598 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5600 vdev_t *rvd = spa->spa_root_vdev;
5601 uint64_t id = vd->vdev_id;
5602 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5604 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5605 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5606 ASSERT(vd == vd->vdev_top);
5609 * Only remove any devices which are empty.
5611 if (vd->vdev_stat.vs_alloc != 0)
5614 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5616 if (list_link_active(&vd->vdev_state_dirty_node))
5617 vdev_state_clean(vd);
5618 if (list_link_active(&vd->vdev_config_dirty_node))
5619 vdev_config_clean(vd);
5624 vdev_compact_children(rvd);
5626 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5627 vdev_add_child(rvd, vd);
5629 vdev_config_dirty(rvd);
5632 * Reassess the health of our root vdev.
5638 * Remove a device from the pool -
5640 * Removing a device from the vdev namespace requires several steps
5641 * and can take a significant amount of time. As a result we use
5642 * the spa_vdev_config_[enter/exit] functions which allow us to
5643 * grab and release the spa_config_lock while still holding the namespace
5644 * lock. During each step the configuration is synced out.
5646 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5650 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5653 metaslab_group_t *mg;
5654 nvlist_t **spares, **l2cache, *nv;
5656 uint_t nspares, nl2cache;
5658 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5660 ASSERT(spa_writeable(spa));
5663 txg = spa_vdev_enter(spa);
5665 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5667 if (spa->spa_spares.sav_vdevs != NULL &&
5668 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5669 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5670 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5672 * Only remove the hot spare if it's not currently in use
5675 if (vd == NULL || unspare) {
5676 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5677 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5678 spa_load_spares(spa);
5679 spa->spa_spares.sav_sync = B_TRUE;
5681 error = SET_ERROR(EBUSY);
5683 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5684 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5685 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5686 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5688 * Cache devices can always be removed.
5690 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5691 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5692 spa_load_l2cache(spa);
5693 spa->spa_l2cache.sav_sync = B_TRUE;
5694 } else if (vd != NULL && vd->vdev_islog) {
5696 ASSERT(vd == vd->vdev_top);
5701 * Stop allocating from this vdev.
5703 metaslab_group_passivate(mg);
5706 * Wait for the youngest allocations and frees to sync,
5707 * and then wait for the deferral of those frees to finish.
5709 spa_vdev_config_exit(spa, NULL,
5710 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5713 * Attempt to evacuate the vdev.
5715 error = spa_vdev_remove_evacuate(spa, vd);
5717 txg = spa_vdev_config_enter(spa);
5720 * If we couldn't evacuate the vdev, unwind.
5723 metaslab_group_activate(mg);
5724 return (spa_vdev_exit(spa, NULL, txg, error));
5728 * Clean up the vdev namespace.
5730 spa_vdev_remove_from_namespace(spa, vd);
5732 } else if (vd != NULL) {
5734 * Normal vdevs cannot be removed (yet).
5736 error = SET_ERROR(ENOTSUP);
5739 * There is no vdev of any kind with the specified guid.
5741 error = SET_ERROR(ENOENT);
5745 return (spa_vdev_exit(spa, NULL, txg, error));
5751 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5752 * currently spared, so we can detach it.
5755 spa_vdev_resilver_done_hunt(vdev_t *vd)
5757 vdev_t *newvd, *oldvd;
5759 for (int c = 0; c < vd->vdev_children; c++) {
5760 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5766 * Check for a completed replacement. We always consider the first
5767 * vdev in the list to be the oldest vdev, and the last one to be
5768 * the newest (see spa_vdev_attach() for how that works). In
5769 * the case where the newest vdev is faulted, we will not automatically
5770 * remove it after a resilver completes. This is OK as it will require
5771 * user intervention to determine which disk the admin wishes to keep.
5773 if (vd->vdev_ops == &vdev_replacing_ops) {
5774 ASSERT(vd->vdev_children > 1);
5776 newvd = vd->vdev_child[vd->vdev_children - 1];
5777 oldvd = vd->vdev_child[0];
5779 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5780 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5781 !vdev_dtl_required(oldvd))
5786 * Check for a completed resilver with the 'unspare' flag set.
5788 if (vd->vdev_ops == &vdev_spare_ops) {
5789 vdev_t *first = vd->vdev_child[0];
5790 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5792 if (last->vdev_unspare) {
5795 } else if (first->vdev_unspare) {
5802 if (oldvd != NULL &&
5803 vdev_dtl_empty(newvd, DTL_MISSING) &&
5804 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5805 !vdev_dtl_required(oldvd))
5809 * If there are more than two spares attached to a disk,
5810 * and those spares are not required, then we want to
5811 * attempt to free them up now so that they can be used
5812 * by other pools. Once we're back down to a single
5813 * disk+spare, we stop removing them.
5815 if (vd->vdev_children > 2) {
5816 newvd = vd->vdev_child[1];
5818 if (newvd->vdev_isspare && last->vdev_isspare &&
5819 vdev_dtl_empty(last, DTL_MISSING) &&
5820 vdev_dtl_empty(last, DTL_OUTAGE) &&
5821 !vdev_dtl_required(newvd))
5830 spa_vdev_resilver_done(spa_t *spa)
5832 vdev_t *vd, *pvd, *ppvd;
5833 uint64_t guid, sguid, pguid, ppguid;
5835 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5837 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5838 pvd = vd->vdev_parent;
5839 ppvd = pvd->vdev_parent;
5840 guid = vd->vdev_guid;
5841 pguid = pvd->vdev_guid;
5842 ppguid = ppvd->vdev_guid;
5845 * If we have just finished replacing a hot spared device, then
5846 * we need to detach the parent's first child (the original hot
5849 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5850 ppvd->vdev_children == 2) {
5851 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5852 sguid = ppvd->vdev_child[1]->vdev_guid;
5854 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5856 spa_config_exit(spa, SCL_ALL, FTAG);
5857 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5859 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5861 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5864 spa_config_exit(spa, SCL_ALL, FTAG);
5868 * Update the stored path or FRU for this vdev.
5871 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5875 boolean_t sync = B_FALSE;
5877 ASSERT(spa_writeable(spa));
5879 spa_vdev_state_enter(spa, SCL_ALL);
5881 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5882 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5884 if (!vd->vdev_ops->vdev_op_leaf)
5885 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5888 if (strcmp(value, vd->vdev_path) != 0) {
5889 spa_strfree(vd->vdev_path);
5890 vd->vdev_path = spa_strdup(value);
5894 if (vd->vdev_fru == NULL) {
5895 vd->vdev_fru = spa_strdup(value);
5897 } else if (strcmp(value, vd->vdev_fru) != 0) {
5898 spa_strfree(vd->vdev_fru);
5899 vd->vdev_fru = spa_strdup(value);
5904 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5908 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5910 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5914 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5916 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5920 * ==========================================================================
5922 * ==========================================================================
5926 spa_scan_stop(spa_t *spa)
5928 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5929 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5930 return (SET_ERROR(EBUSY));
5931 return (dsl_scan_cancel(spa->spa_dsl_pool));
5935 spa_scan(spa_t *spa, pool_scan_func_t func)
5937 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5939 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5940 return (SET_ERROR(ENOTSUP));
5943 * If a resilver was requested, but there is no DTL on a
5944 * writeable leaf device, we have nothing to do.
5946 if (func == POOL_SCAN_RESILVER &&
5947 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5948 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5952 return (dsl_scan(spa->spa_dsl_pool, func));
5956 * ==========================================================================
5957 * SPA async task processing
5958 * ==========================================================================
5962 spa_async_remove(spa_t *spa, vdev_t *vd)
5964 if (vd->vdev_remove_wanted) {
5965 vd->vdev_remove_wanted = B_FALSE;
5966 vd->vdev_delayed_close = B_FALSE;
5967 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5970 * We want to clear the stats, but we don't want to do a full
5971 * vdev_clear() as that will cause us to throw away
5972 * degraded/faulted state as well as attempt to reopen the
5973 * device, all of which is a waste.
5975 vd->vdev_stat.vs_read_errors = 0;
5976 vd->vdev_stat.vs_write_errors = 0;
5977 vd->vdev_stat.vs_checksum_errors = 0;
5979 vdev_state_dirty(vd->vdev_top);
5980 /* Tell userspace that the vdev is gone. */
5981 zfs_post_remove(spa, vd);
5984 for (int c = 0; c < vd->vdev_children; c++)
5985 spa_async_remove(spa, vd->vdev_child[c]);
5989 spa_async_probe(spa_t *spa, vdev_t *vd)
5991 if (vd->vdev_probe_wanted) {
5992 vd->vdev_probe_wanted = B_FALSE;
5993 vdev_reopen(vd); /* vdev_open() does the actual probe */
5996 for (int c = 0; c < vd->vdev_children; c++)
5997 spa_async_probe(spa, vd->vdev_child[c]);
6001 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
6007 if (!spa->spa_autoexpand)
6010 for (int c = 0; c < vd->vdev_children; c++) {
6011 vdev_t *cvd = vd->vdev_child[c];
6012 spa_async_autoexpand(spa, cvd);
6015 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
6018 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
6019 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
6021 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6022 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
6024 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
6025 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
6028 kmem_free(physpath, MAXPATHLEN);
6032 spa_async_thread(void *arg)
6037 ASSERT(spa->spa_sync_on);
6039 mutex_enter(&spa->spa_async_lock);
6040 tasks = spa->spa_async_tasks;
6041 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
6042 mutex_exit(&spa->spa_async_lock);
6045 * See if the config needs to be updated.
6047 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
6048 uint64_t old_space, new_space;
6050 mutex_enter(&spa_namespace_lock);
6051 old_space = metaslab_class_get_space(spa_normal_class(spa));
6052 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
6053 new_space = metaslab_class_get_space(spa_normal_class(spa));
6054 mutex_exit(&spa_namespace_lock);
6057 * If the pool grew as a result of the config update,
6058 * then log an internal history event.
6060 if (new_space != old_space) {
6061 spa_history_log_internal(spa, "vdev online", NULL,
6062 "pool '%s' size: %llu(+%llu)",
6063 spa_name(spa), new_space, new_space - old_space);
6067 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
6068 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6069 spa_async_autoexpand(spa, spa->spa_root_vdev);
6070 spa_config_exit(spa, SCL_CONFIG, FTAG);
6074 * See if any devices need to be probed.
6076 if (tasks & SPA_ASYNC_PROBE) {
6077 spa_vdev_state_enter(spa, SCL_NONE);
6078 spa_async_probe(spa, spa->spa_root_vdev);
6079 (void) spa_vdev_state_exit(spa, NULL, 0);
6083 * If any devices are done replacing, detach them.
6085 if (tasks & SPA_ASYNC_RESILVER_DONE)
6086 spa_vdev_resilver_done(spa);
6089 * Kick off a resilver.
6091 if (tasks & SPA_ASYNC_RESILVER)
6092 dsl_resilver_restart(spa->spa_dsl_pool, 0);
6095 * Let the world know that we're done.
6097 mutex_enter(&spa->spa_async_lock);
6098 spa->spa_async_thread = NULL;
6099 cv_broadcast(&spa->spa_async_cv);
6100 mutex_exit(&spa->spa_async_lock);
6105 spa_async_thread_vd(void *arg)
6110 ASSERT(spa->spa_sync_on);
6112 mutex_enter(&spa->spa_async_lock);
6113 tasks = spa->spa_async_tasks;
6115 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6116 mutex_exit(&spa->spa_async_lock);
6119 * See if any devices need to be marked REMOVED.
6121 if (tasks & SPA_ASYNC_REMOVE) {
6122 spa_vdev_state_enter(spa, SCL_NONE);
6123 spa_async_remove(spa, spa->spa_root_vdev);
6124 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6125 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6126 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6127 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6128 (void) spa_vdev_state_exit(spa, NULL, 0);
6132 * Let the world know that we're done.
6134 mutex_enter(&spa->spa_async_lock);
6135 tasks = spa->spa_async_tasks;
6136 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6138 spa->spa_async_thread_vd = NULL;
6139 cv_broadcast(&spa->spa_async_cv);
6140 mutex_exit(&spa->spa_async_lock);
6145 spa_async_suspend(spa_t *spa)
6147 mutex_enter(&spa->spa_async_lock);
6148 spa->spa_async_suspended++;
6149 while (spa->spa_async_thread != NULL &&
6150 spa->spa_async_thread_vd != NULL)
6151 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6152 mutex_exit(&spa->spa_async_lock);
6156 spa_async_resume(spa_t *spa)
6158 mutex_enter(&spa->spa_async_lock);
6159 ASSERT(spa->spa_async_suspended != 0);
6160 spa->spa_async_suspended--;
6161 mutex_exit(&spa->spa_async_lock);
6165 spa_async_tasks_pending(spa_t *spa)
6167 uint_t non_config_tasks;
6169 boolean_t config_task_suspended;
6171 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6173 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6174 if (spa->spa_ccw_fail_time == 0) {
6175 config_task_suspended = B_FALSE;
6177 config_task_suspended =
6178 (gethrtime() - spa->spa_ccw_fail_time) <
6179 (zfs_ccw_retry_interval * NANOSEC);
6182 return (non_config_tasks || (config_task && !config_task_suspended));
6186 spa_async_dispatch(spa_t *spa)
6188 mutex_enter(&spa->spa_async_lock);
6189 if (spa_async_tasks_pending(spa) &&
6190 !spa->spa_async_suspended &&
6191 spa->spa_async_thread == NULL &&
6193 spa->spa_async_thread = thread_create(NULL, 0,
6194 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6195 mutex_exit(&spa->spa_async_lock);
6199 spa_async_dispatch_vd(spa_t *spa)
6201 mutex_enter(&spa->spa_async_lock);
6202 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6203 !spa->spa_async_suspended &&
6204 spa->spa_async_thread_vd == NULL &&
6206 spa->spa_async_thread_vd = thread_create(NULL, 0,
6207 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6208 mutex_exit(&spa->spa_async_lock);
6212 spa_async_request(spa_t *spa, int task)
6214 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6215 mutex_enter(&spa->spa_async_lock);
6216 spa->spa_async_tasks |= task;
6217 mutex_exit(&spa->spa_async_lock);
6218 spa_async_dispatch_vd(spa);
6222 * ==========================================================================
6223 * SPA syncing routines
6224 * ==========================================================================
6228 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6231 bpobj_enqueue(bpo, bp, tx);
6236 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6240 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6241 BP_GET_PSIZE(bp), zio->io_flags));
6246 * Note: this simple function is not inlined to make it easier to dtrace the
6247 * amount of time spent syncing frees.
6250 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6252 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6253 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6254 VERIFY(zio_wait(zio) == 0);
6258 * Note: this simple function is not inlined to make it easier to dtrace the
6259 * amount of time spent syncing deferred frees.
6262 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6264 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6265 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6266 spa_free_sync_cb, zio, tx), ==, 0);
6267 VERIFY0(zio_wait(zio));
6272 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6274 char *packed = NULL;
6279 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6282 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6283 * information. This avoids the dmu_buf_will_dirty() path and
6284 * saves us a pre-read to get data we don't actually care about.
6286 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6287 packed = kmem_alloc(bufsize, KM_SLEEP);
6289 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6291 bzero(packed + nvsize, bufsize - nvsize);
6293 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6295 kmem_free(packed, bufsize);
6297 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6298 dmu_buf_will_dirty(db, tx);
6299 *(uint64_t *)db->db_data = nvsize;
6300 dmu_buf_rele(db, FTAG);
6304 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6305 const char *config, const char *entry)
6315 * Update the MOS nvlist describing the list of available devices.
6316 * spa_validate_aux() will have already made sure this nvlist is
6317 * valid and the vdevs are labeled appropriately.
6319 if (sav->sav_object == 0) {
6320 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6321 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6322 sizeof (uint64_t), tx);
6323 VERIFY(zap_update(spa->spa_meta_objset,
6324 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6325 &sav->sav_object, tx) == 0);
6328 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6329 if (sav->sav_count == 0) {
6330 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6332 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6333 for (i = 0; i < sav->sav_count; i++)
6334 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6335 B_FALSE, VDEV_CONFIG_L2CACHE);
6336 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6337 sav->sav_count) == 0);
6338 for (i = 0; i < sav->sav_count; i++)
6339 nvlist_free(list[i]);
6340 kmem_free(list, sav->sav_count * sizeof (void *));
6343 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6344 nvlist_free(nvroot);
6346 sav->sav_sync = B_FALSE;
6350 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6354 if (list_is_empty(&spa->spa_config_dirty_list))
6357 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6359 config = spa_config_generate(spa, spa->spa_root_vdev,
6360 dmu_tx_get_txg(tx), B_FALSE);
6363 * If we're upgrading the spa version then make sure that
6364 * the config object gets updated with the correct version.
6366 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6367 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6368 spa->spa_uberblock.ub_version);
6370 spa_config_exit(spa, SCL_STATE, FTAG);
6372 nvlist_free(spa->spa_config_syncing);
6373 spa->spa_config_syncing = config;
6375 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6379 spa_sync_version(void *arg, dmu_tx_t *tx)
6381 uint64_t *versionp = arg;
6382 uint64_t version = *versionp;
6383 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6386 * Setting the version is special cased when first creating the pool.
6388 ASSERT(tx->tx_txg != TXG_INITIAL);
6390 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6391 ASSERT(version >= spa_version(spa));
6393 spa->spa_uberblock.ub_version = version;
6394 vdev_config_dirty(spa->spa_root_vdev);
6395 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6399 * Set zpool properties.
6402 spa_sync_props(void *arg, dmu_tx_t *tx)
6404 nvlist_t *nvp = arg;
6405 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6406 objset_t *mos = spa->spa_meta_objset;
6407 nvpair_t *elem = NULL;
6409 mutex_enter(&spa->spa_props_lock);
6411 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6413 char *strval, *fname;
6415 const char *propname;
6416 zprop_type_t proptype;
6419 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6422 * We checked this earlier in spa_prop_validate().
6424 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6426 fname = strchr(nvpair_name(elem), '@') + 1;
6427 VERIFY0(zfeature_lookup_name(fname, &fid));
6429 spa_feature_enable(spa, fid, tx);
6430 spa_history_log_internal(spa, "set", tx,
6431 "%s=enabled", nvpair_name(elem));
6434 case ZPOOL_PROP_VERSION:
6435 intval = fnvpair_value_uint64(elem);
6437 * The version is synced seperatly before other
6438 * properties and should be correct by now.
6440 ASSERT3U(spa_version(spa), >=, intval);
6443 case ZPOOL_PROP_ALTROOT:
6445 * 'altroot' is a non-persistent property. It should
6446 * have been set temporarily at creation or import time.
6448 ASSERT(spa->spa_root != NULL);
6451 case ZPOOL_PROP_READONLY:
6452 case ZPOOL_PROP_CACHEFILE:
6454 * 'readonly' and 'cachefile' are also non-persisitent
6458 case ZPOOL_PROP_COMMENT:
6459 strval = fnvpair_value_string(elem);
6460 if (spa->spa_comment != NULL)
6461 spa_strfree(spa->spa_comment);
6462 spa->spa_comment = spa_strdup(strval);
6464 * We need to dirty the configuration on all the vdevs
6465 * so that their labels get updated. It's unnecessary
6466 * to do this for pool creation since the vdev's
6467 * configuratoin has already been dirtied.
6469 if (tx->tx_txg != TXG_INITIAL)
6470 vdev_config_dirty(spa->spa_root_vdev);
6471 spa_history_log_internal(spa, "set", tx,
6472 "%s=%s", nvpair_name(elem), strval);
6476 * Set pool property values in the poolprops mos object.
6478 if (spa->spa_pool_props_object == 0) {
6479 spa->spa_pool_props_object =
6480 zap_create_link(mos, DMU_OT_POOL_PROPS,
6481 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6485 /* normalize the property name */
6486 propname = zpool_prop_to_name(prop);
6487 proptype = zpool_prop_get_type(prop);
6489 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6490 ASSERT(proptype == PROP_TYPE_STRING);
6491 strval = fnvpair_value_string(elem);
6492 VERIFY0(zap_update(mos,
6493 spa->spa_pool_props_object, propname,
6494 1, strlen(strval) + 1, strval, tx));
6495 spa_history_log_internal(spa, "set", tx,
6496 "%s=%s", nvpair_name(elem), strval);
6497 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6498 intval = fnvpair_value_uint64(elem);
6500 if (proptype == PROP_TYPE_INDEX) {
6502 VERIFY0(zpool_prop_index_to_string(
6503 prop, intval, &unused));
6505 VERIFY0(zap_update(mos,
6506 spa->spa_pool_props_object, propname,
6507 8, 1, &intval, tx));
6508 spa_history_log_internal(spa, "set", tx,
6509 "%s=%lld", nvpair_name(elem), intval);
6511 ASSERT(0); /* not allowed */
6515 case ZPOOL_PROP_DELEGATION:
6516 spa->spa_delegation = intval;
6518 case ZPOOL_PROP_BOOTFS:
6519 spa->spa_bootfs = intval;
6521 case ZPOOL_PROP_FAILUREMODE:
6522 spa->spa_failmode = intval;
6524 case ZPOOL_PROP_AUTOEXPAND:
6525 spa->spa_autoexpand = intval;
6526 if (tx->tx_txg != TXG_INITIAL)
6527 spa_async_request(spa,
6528 SPA_ASYNC_AUTOEXPAND);
6530 case ZPOOL_PROP_DEDUPDITTO:
6531 spa->spa_dedup_ditto = intval;
6540 mutex_exit(&spa->spa_props_lock);
6544 * Perform one-time upgrade on-disk changes. spa_version() does not
6545 * reflect the new version this txg, so there must be no changes this
6546 * txg to anything that the upgrade code depends on after it executes.
6547 * Therefore this must be called after dsl_pool_sync() does the sync
6551 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6553 dsl_pool_t *dp = spa->spa_dsl_pool;
6555 ASSERT(spa->spa_sync_pass == 1);
6557 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6559 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6560 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6561 dsl_pool_create_origin(dp, tx);
6563 /* Keeping the origin open increases spa_minref */
6564 spa->spa_minref += 3;
6567 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6568 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6569 dsl_pool_upgrade_clones(dp, tx);
6572 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6573 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6574 dsl_pool_upgrade_dir_clones(dp, tx);
6576 /* Keeping the freedir open increases spa_minref */
6577 spa->spa_minref += 3;
6580 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6581 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6582 spa_feature_create_zap_objects(spa, tx);
6586 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6587 * when possibility to use lz4 compression for metadata was added
6588 * Old pools that have this feature enabled must be upgraded to have
6589 * this feature active
6591 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6592 boolean_t lz4_en = spa_feature_is_enabled(spa,
6593 SPA_FEATURE_LZ4_COMPRESS);
6594 boolean_t lz4_ac = spa_feature_is_active(spa,
6595 SPA_FEATURE_LZ4_COMPRESS);
6597 if (lz4_en && !lz4_ac)
6598 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6602 * If we haven't written the salt, do so now. Note that the
6603 * feature may not be activated yet, but that's fine since
6604 * the presence of this ZAP entry is backwards compatible.
6606 if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
6607 DMU_POOL_CHECKSUM_SALT) == ENOENT) {
6608 VERIFY0(zap_add(spa->spa_meta_objset,
6609 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
6610 sizeof (spa->spa_cksum_salt.zcs_bytes),
6611 spa->spa_cksum_salt.zcs_bytes, tx));
6614 rrw_exit(&dp->dp_config_rwlock, FTAG);
6618 * Sync the specified transaction group. New blocks may be dirtied as
6619 * part of the process, so we iterate until it converges.
6622 spa_sync(spa_t *spa, uint64_t txg)
6624 dsl_pool_t *dp = spa->spa_dsl_pool;
6625 objset_t *mos = spa->spa_meta_objset;
6626 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6627 vdev_t *rvd = spa->spa_root_vdev;
6632 VERIFY(spa_writeable(spa));
6635 * Lock out configuration changes.
6637 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6639 spa->spa_syncing_txg = txg;
6640 spa->spa_sync_pass = 0;
6643 * If there are any pending vdev state changes, convert them
6644 * into config changes that go out with this transaction group.
6646 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6647 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6649 * We need the write lock here because, for aux vdevs,
6650 * calling vdev_config_dirty() modifies sav_config.
6651 * This is ugly and will become unnecessary when we
6652 * eliminate the aux vdev wart by integrating all vdevs
6653 * into the root vdev tree.
6655 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6656 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6657 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6658 vdev_state_clean(vd);
6659 vdev_config_dirty(vd);
6661 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6662 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6664 spa_config_exit(spa, SCL_STATE, FTAG);
6666 tx = dmu_tx_create_assigned(dp, txg);
6668 spa->spa_sync_starttime = gethrtime();
6670 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6671 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6672 #else /* !illumos */
6674 callout_reset(&spa->spa_deadman_cycid,
6675 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6677 #endif /* illumos */
6680 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6681 * set spa_deflate if we have no raid-z vdevs.
6683 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6684 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6687 for (i = 0; i < rvd->vdev_children; i++) {
6688 vd = rvd->vdev_child[i];
6689 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6692 if (i == rvd->vdev_children) {
6693 spa->spa_deflate = TRUE;
6694 VERIFY(0 == zap_add(spa->spa_meta_objset,
6695 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6696 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6701 * Iterate to convergence.
6704 int pass = ++spa->spa_sync_pass;
6706 spa_sync_config_object(spa, tx);
6707 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6708 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6709 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6710 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6711 spa_errlog_sync(spa, txg);
6712 dsl_pool_sync(dp, txg);
6714 if (pass < zfs_sync_pass_deferred_free) {
6715 spa_sync_frees(spa, free_bpl, tx);
6718 * We can not defer frees in pass 1, because
6719 * we sync the deferred frees later in pass 1.
6721 ASSERT3U(pass, >, 1);
6722 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6723 &spa->spa_deferred_bpobj, tx);
6727 dsl_scan_sync(dp, tx);
6729 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6733 spa_sync_upgrades(spa, tx);
6735 spa->spa_uberblock.ub_rootbp.blk_birth);
6737 * Note: We need to check if the MOS is dirty
6738 * because we could have marked the MOS dirty
6739 * without updating the uberblock (e.g. if we
6740 * have sync tasks but no dirty user data). We
6741 * need to check the uberblock's rootbp because
6742 * it is updated if we have synced out dirty
6743 * data (though in this case the MOS will most
6744 * likely also be dirty due to second order
6745 * effects, we don't want to rely on that here).
6747 if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6748 !dmu_objset_is_dirty(mos, txg)) {
6750 * Nothing changed on the first pass,
6751 * therefore this TXG is a no-op. Avoid
6752 * syncing deferred frees, so that we
6753 * can keep this TXG as a no-op.
6755 ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6757 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6758 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
6761 spa_sync_deferred_frees(spa, tx);
6764 } while (dmu_objset_is_dirty(mos, txg));
6767 * Rewrite the vdev configuration (which includes the uberblock)
6768 * to commit the transaction group.
6770 * If there are no dirty vdevs, we sync the uberblock to a few
6771 * random top-level vdevs that are known to be visible in the
6772 * config cache (see spa_vdev_add() for a complete description).
6773 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6777 * We hold SCL_STATE to prevent vdev open/close/etc.
6778 * while we're attempting to write the vdev labels.
6780 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6782 if (list_is_empty(&spa->spa_config_dirty_list)) {
6783 vdev_t *svd[SPA_DVAS_PER_BP];
6785 int children = rvd->vdev_children;
6786 int c0 = spa_get_random(children);
6788 for (int c = 0; c < children; c++) {
6789 vd = rvd->vdev_child[(c0 + c) % children];
6790 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6792 svd[svdcount++] = vd;
6793 if (svdcount == SPA_DVAS_PER_BP)
6796 error = vdev_config_sync(svd, svdcount, txg);
6798 error = vdev_config_sync(rvd->vdev_child,
6799 rvd->vdev_children, txg);
6803 spa->spa_last_synced_guid = rvd->vdev_guid;
6805 spa_config_exit(spa, SCL_STATE, FTAG);
6809 zio_suspend(spa, NULL);
6810 zio_resume_wait(spa);
6815 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6816 #else /* !illumos */
6818 callout_drain(&spa->spa_deadman_cycid);
6820 #endif /* illumos */
6823 * Clear the dirty config list.
6825 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6826 vdev_config_clean(vd);
6829 * Now that the new config has synced transactionally,
6830 * let it become visible to the config cache.
6832 if (spa->spa_config_syncing != NULL) {
6833 spa_config_set(spa, spa->spa_config_syncing);
6834 spa->spa_config_txg = txg;
6835 spa->spa_config_syncing = NULL;
6838 spa->spa_ubsync = spa->spa_uberblock;
6840 dsl_pool_sync_done(dp, txg);
6843 * Update usable space statistics.
6845 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6846 vdev_sync_done(vd, txg);
6848 spa_update_dspace(spa);
6851 * It had better be the case that we didn't dirty anything
6852 * since vdev_config_sync().
6854 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6855 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6856 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6858 spa->spa_sync_pass = 0;
6860 spa_config_exit(spa, SCL_CONFIG, FTAG);
6862 spa_handle_ignored_writes(spa);
6865 * If any async tasks have been requested, kick them off.
6867 spa_async_dispatch(spa);
6868 spa_async_dispatch_vd(spa);
6872 * Sync all pools. We don't want to hold the namespace lock across these
6873 * operations, so we take a reference on the spa_t and drop the lock during the
6877 spa_sync_allpools(void)
6880 mutex_enter(&spa_namespace_lock);
6881 while ((spa = spa_next(spa)) != NULL) {
6882 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6883 !spa_writeable(spa) || spa_suspended(spa))
6885 spa_open_ref(spa, FTAG);
6886 mutex_exit(&spa_namespace_lock);
6887 txg_wait_synced(spa_get_dsl(spa), 0);
6888 mutex_enter(&spa_namespace_lock);
6889 spa_close(spa, FTAG);
6891 mutex_exit(&spa_namespace_lock);
6895 * ==========================================================================
6896 * Miscellaneous routines
6897 * ==========================================================================
6901 * Remove all pools in the system.
6909 * Remove all cached state. All pools should be closed now,
6910 * so every spa in the AVL tree should be unreferenced.
6912 mutex_enter(&spa_namespace_lock);
6913 while ((spa = spa_next(NULL)) != NULL) {
6915 * Stop async tasks. The async thread may need to detach
6916 * a device that's been replaced, which requires grabbing
6917 * spa_namespace_lock, so we must drop it here.
6919 spa_open_ref(spa, FTAG);
6920 mutex_exit(&spa_namespace_lock);
6921 spa_async_suspend(spa);
6922 mutex_enter(&spa_namespace_lock);
6923 spa_close(spa, FTAG);
6925 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6927 spa_deactivate(spa);
6931 mutex_exit(&spa_namespace_lock);
6935 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6940 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6944 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6945 vd = spa->spa_l2cache.sav_vdevs[i];
6946 if (vd->vdev_guid == guid)
6950 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6951 vd = spa->spa_spares.sav_vdevs[i];
6952 if (vd->vdev_guid == guid)
6961 spa_upgrade(spa_t *spa, uint64_t version)
6963 ASSERT(spa_writeable(spa));
6965 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6968 * This should only be called for a non-faulted pool, and since a
6969 * future version would result in an unopenable pool, this shouldn't be
6972 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6973 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6975 spa->spa_uberblock.ub_version = version;
6976 vdev_config_dirty(spa->spa_root_vdev);
6978 spa_config_exit(spa, SCL_ALL, FTAG);
6980 txg_wait_synced(spa_get_dsl(spa), 0);
6984 spa_has_spare(spa_t *spa, uint64_t guid)
6988 spa_aux_vdev_t *sav = &spa->spa_spares;
6990 for (i = 0; i < sav->sav_count; i++)
6991 if (sav->sav_vdevs[i]->vdev_guid == guid)
6994 for (i = 0; i < sav->sav_npending; i++) {
6995 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6996 &spareguid) == 0 && spareguid == guid)
7004 * Check if a pool has an active shared spare device.
7005 * Note: reference count of an active spare is 2, as a spare and as a replace
7008 spa_has_active_shared_spare(spa_t *spa)
7012 spa_aux_vdev_t *sav = &spa->spa_spares;
7014 for (i = 0; i < sav->sav_count; i++) {
7015 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
7016 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
7025 * Post a sysevent corresponding to the given event. The 'name' must be one of
7026 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
7027 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7028 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7029 * or zdb as real changes.
7032 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
7036 sysevent_attr_list_t *attr = NULL;
7037 sysevent_value_t value;
7040 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
7043 value.value_type = SE_DATA_TYPE_STRING;
7044 value.value.sv_string = spa_name(spa);
7045 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
7048 value.value_type = SE_DATA_TYPE_UINT64;
7049 value.value.sv_uint64 = spa_guid(spa);
7050 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
7054 value.value_type = SE_DATA_TYPE_UINT64;
7055 value.value.sv_uint64 = vd->vdev_guid;
7056 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
7060 if (vd->vdev_path) {
7061 value.value_type = SE_DATA_TYPE_STRING;
7062 value.value.sv_string = vd->vdev_path;
7063 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
7064 &value, SE_SLEEP) != 0)
7069 if (sysevent_attach_attributes(ev, attr) != 0)
7073 (void) log_sysevent(ev, SE_SLEEP, &eid);
7077 sysevent_free_attr(attr);