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 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RWTUN, &check_hostid, 0,
97 "Check hostid on import?");
98 TUNABLE_INT("vfs.zfs.ccw_retry_interval", &zfs_ccw_retry_interval);
99 SYSCTL_INT(_vfs_zfs, OID_AUTO, ccw_retry_interval, CTLFLAG_RW,
100 &zfs_ccw_retry_interval, 0,
101 "Configuration cache file write, retry after failure, interval (seconds)");
103 typedef enum zti_modes {
104 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
105 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
106 ZTI_MODE_NULL, /* don't create a taskq */
110 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
111 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
112 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
114 #define ZTI_N(n) ZTI_P(n, 1)
115 #define ZTI_ONE ZTI_N(1)
117 typedef struct zio_taskq_info {
118 zti_modes_t zti_mode;
123 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
124 "issue", "issue_high", "intr", "intr_high"
128 * This table defines the taskq settings for each ZFS I/O type. When
129 * initializing a pool, we use this table to create an appropriately sized
130 * taskq. Some operations are low volume and therefore have a small, static
131 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
132 * macros. Other operations process a large amount of data; the ZTI_BATCH
133 * macro causes us to create a taskq oriented for throughput. Some operations
134 * are so high frequency and short-lived that the taskq itself can become a a
135 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
136 * additional degree of parallelism specified by the number of threads per-
137 * taskq and the number of taskqs; when dispatching an event in this case, the
138 * particular taskq is chosen at random.
140 * The different taskq priorities are to handle the different contexts (issue
141 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
142 * need to be handled with minimum delay.
144 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
145 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
146 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
147 { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */
148 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
149 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
150 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
151 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
154 static void spa_sync_version(void *arg, dmu_tx_t *tx);
155 static void spa_sync_props(void *arg, dmu_tx_t *tx);
156 static boolean_t spa_has_active_shared_spare(spa_t *spa);
157 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
158 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
160 static void spa_vdev_resilver_done(spa_t *spa);
162 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
164 id_t zio_taskq_psrset_bind = PS_NONE;
167 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
169 uint_t zio_taskq_basedc = 80; /* base duty cycle */
171 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
172 extern int zfs_sync_pass_deferred_free;
175 extern void spa_deadman(void *arg);
179 * This (illegal) pool name is used when temporarily importing a spa_t in order
180 * to get the vdev stats associated with the imported devices.
182 #define TRYIMPORT_NAME "$import"
185 * ==========================================================================
186 * SPA properties routines
187 * ==========================================================================
191 * Add a (source=src, propname=propval) list to an nvlist.
194 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
195 uint64_t intval, zprop_source_t src)
197 const char *propname = zpool_prop_to_name(prop);
200 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
201 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
204 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
206 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
208 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
209 nvlist_free(propval);
213 * Get property values from the spa configuration.
216 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
218 vdev_t *rvd = spa->spa_root_vdev;
219 dsl_pool_t *pool = spa->spa_dsl_pool;
220 uint64_t size, alloc, cap, version;
221 zprop_source_t src = ZPROP_SRC_NONE;
222 spa_config_dirent_t *dp;
223 metaslab_class_t *mc = spa_normal_class(spa);
225 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
228 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
229 size = metaslab_class_get_space(spa_normal_class(spa));
230 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
231 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
232 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
233 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
236 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
237 metaslab_class_fragmentation(mc), src);
238 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
239 metaslab_class_expandable_space(mc), src);
240 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
241 (spa_mode(spa) == FREAD), src);
243 cap = (size == 0) ? 0 : (alloc * 100 / size);
244 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
246 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
247 ddt_get_pool_dedup_ratio(spa), src);
249 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
250 rvd->vdev_state, src);
252 version = spa_version(spa);
253 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
254 src = ZPROP_SRC_DEFAULT;
256 src = ZPROP_SRC_LOCAL;
257 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
262 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
263 * when opening pools before this version freedir will be NULL.
265 if (pool->dp_free_dir != NULL) {
266 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
267 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
270 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
274 if (pool->dp_leak_dir != NULL) {
275 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
276 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
279 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
284 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
286 if (spa->spa_comment != NULL) {
287 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
291 if (spa->spa_root != NULL)
292 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
295 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
296 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
297 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
299 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
300 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
303 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
304 if (dp->scd_path == NULL) {
305 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
306 "none", 0, ZPROP_SRC_LOCAL);
307 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
308 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
309 dp->scd_path, 0, ZPROP_SRC_LOCAL);
315 * Get zpool property values.
318 spa_prop_get(spa_t *spa, nvlist_t **nvp)
320 objset_t *mos = spa->spa_meta_objset;
325 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
327 mutex_enter(&spa->spa_props_lock);
330 * Get properties from the spa config.
332 spa_prop_get_config(spa, nvp);
334 /* If no pool property object, no more prop to get. */
335 if (mos == NULL || spa->spa_pool_props_object == 0) {
336 mutex_exit(&spa->spa_props_lock);
341 * Get properties from the MOS pool property object.
343 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
344 (err = zap_cursor_retrieve(&zc, &za)) == 0;
345 zap_cursor_advance(&zc)) {
348 zprop_source_t src = ZPROP_SRC_DEFAULT;
351 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
354 switch (za.za_integer_length) {
356 /* integer property */
357 if (za.za_first_integer !=
358 zpool_prop_default_numeric(prop))
359 src = ZPROP_SRC_LOCAL;
361 if (prop == ZPOOL_PROP_BOOTFS) {
363 dsl_dataset_t *ds = NULL;
365 dp = spa_get_dsl(spa);
366 dsl_pool_config_enter(dp, FTAG);
367 if (err = dsl_dataset_hold_obj(dp,
368 za.za_first_integer, FTAG, &ds)) {
369 dsl_pool_config_exit(dp, FTAG);
374 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
376 dsl_dataset_name(ds, strval);
377 dsl_dataset_rele(ds, FTAG);
378 dsl_pool_config_exit(dp, FTAG);
381 intval = za.za_first_integer;
384 spa_prop_add_list(*nvp, prop, strval, intval, src);
388 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
393 /* string property */
394 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
395 err = zap_lookup(mos, spa->spa_pool_props_object,
396 za.za_name, 1, za.za_num_integers, strval);
398 kmem_free(strval, za.za_num_integers);
401 spa_prop_add_list(*nvp, prop, strval, 0, src);
402 kmem_free(strval, za.za_num_integers);
409 zap_cursor_fini(&zc);
410 mutex_exit(&spa->spa_props_lock);
412 if (err && err != ENOENT) {
422 * Validate the given pool properties nvlist and modify the list
423 * for the property values to be set.
426 spa_prop_validate(spa_t *spa, nvlist_t *props)
429 int error = 0, reset_bootfs = 0;
431 boolean_t has_feature = B_FALSE;
434 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
436 char *strval, *slash, *check, *fname;
437 const char *propname = nvpair_name(elem);
438 zpool_prop_t prop = zpool_name_to_prop(propname);
442 if (!zpool_prop_feature(propname)) {
443 error = SET_ERROR(EINVAL);
448 * Sanitize the input.
450 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
451 error = SET_ERROR(EINVAL);
455 if (nvpair_value_uint64(elem, &intval) != 0) {
456 error = SET_ERROR(EINVAL);
461 error = SET_ERROR(EINVAL);
465 fname = strchr(propname, '@') + 1;
466 if (zfeature_lookup_name(fname, NULL) != 0) {
467 error = SET_ERROR(EINVAL);
471 has_feature = B_TRUE;
474 case ZPOOL_PROP_VERSION:
475 error = nvpair_value_uint64(elem, &intval);
477 (intval < spa_version(spa) ||
478 intval > SPA_VERSION_BEFORE_FEATURES ||
480 error = SET_ERROR(EINVAL);
483 case ZPOOL_PROP_DELEGATION:
484 case ZPOOL_PROP_AUTOREPLACE:
485 case ZPOOL_PROP_LISTSNAPS:
486 case ZPOOL_PROP_AUTOEXPAND:
487 error = nvpair_value_uint64(elem, &intval);
488 if (!error && intval > 1)
489 error = SET_ERROR(EINVAL);
492 case ZPOOL_PROP_BOOTFS:
494 * If the pool version is less than SPA_VERSION_BOOTFS,
495 * or the pool is still being created (version == 0),
496 * the bootfs property cannot be set.
498 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
499 error = SET_ERROR(ENOTSUP);
504 * Make sure the vdev config is bootable
506 if (!vdev_is_bootable(spa->spa_root_vdev)) {
507 error = SET_ERROR(ENOTSUP);
513 error = nvpair_value_string(elem, &strval);
519 if (strval == NULL || strval[0] == '\0') {
520 objnum = zpool_prop_default_numeric(
525 if (error = dmu_objset_hold(strval, FTAG, &os))
529 * Must be ZPL, and its property settings
530 * must be supported by GRUB (compression
531 * is not gzip, and large blocks are not used).
534 if (dmu_objset_type(os) != DMU_OST_ZFS) {
535 error = SET_ERROR(ENOTSUP);
537 dsl_prop_get_int_ds(dmu_objset_ds(os),
538 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
540 !BOOTFS_COMPRESS_VALID(propval)) {
541 error = SET_ERROR(ENOTSUP);
543 dsl_prop_get_int_ds(dmu_objset_ds(os),
544 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
546 propval > SPA_OLD_MAXBLOCKSIZE) {
547 error = SET_ERROR(ENOTSUP);
549 objnum = dmu_objset_id(os);
551 dmu_objset_rele(os, FTAG);
555 case ZPOOL_PROP_FAILUREMODE:
556 error = nvpair_value_uint64(elem, &intval);
557 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
558 intval > ZIO_FAILURE_MODE_PANIC))
559 error = SET_ERROR(EINVAL);
562 * This is a special case which only occurs when
563 * the pool has completely failed. This allows
564 * the user to change the in-core failmode property
565 * without syncing it out to disk (I/Os might
566 * currently be blocked). We do this by returning
567 * EIO to the caller (spa_prop_set) to trick it
568 * into thinking we encountered a property validation
571 if (!error && spa_suspended(spa)) {
572 spa->spa_failmode = intval;
573 error = SET_ERROR(EIO);
577 case ZPOOL_PROP_CACHEFILE:
578 if ((error = nvpair_value_string(elem, &strval)) != 0)
581 if (strval[0] == '\0')
584 if (strcmp(strval, "none") == 0)
587 if (strval[0] != '/') {
588 error = SET_ERROR(EINVAL);
592 slash = strrchr(strval, '/');
593 ASSERT(slash != NULL);
595 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
596 strcmp(slash, "/..") == 0)
597 error = SET_ERROR(EINVAL);
600 case ZPOOL_PROP_COMMENT:
601 if ((error = nvpair_value_string(elem, &strval)) != 0)
603 for (check = strval; *check != '\0'; check++) {
605 * The kernel doesn't have an easy isprint()
606 * check. For this kernel check, we merely
607 * check ASCII apart from DEL. Fix this if
608 * there is an easy-to-use kernel isprint().
610 if (*check >= 0x7f) {
611 error = SET_ERROR(EINVAL);
615 if (strlen(strval) > ZPROP_MAX_COMMENT)
619 case ZPOOL_PROP_DEDUPDITTO:
620 if (spa_version(spa) < SPA_VERSION_DEDUP)
621 error = SET_ERROR(ENOTSUP);
623 error = nvpair_value_uint64(elem, &intval);
625 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
626 error = SET_ERROR(EINVAL);
634 if (!error && reset_bootfs) {
635 error = nvlist_remove(props,
636 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
639 error = nvlist_add_uint64(props,
640 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
648 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
651 spa_config_dirent_t *dp;
653 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
657 dp = kmem_alloc(sizeof (spa_config_dirent_t),
660 if (cachefile[0] == '\0')
661 dp->scd_path = spa_strdup(spa_config_path);
662 else if (strcmp(cachefile, "none") == 0)
665 dp->scd_path = spa_strdup(cachefile);
667 list_insert_head(&spa->spa_config_list, dp);
669 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
673 spa_prop_set(spa_t *spa, nvlist_t *nvp)
676 nvpair_t *elem = NULL;
677 boolean_t need_sync = B_FALSE;
679 if ((error = spa_prop_validate(spa, nvp)) != 0)
682 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
683 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
685 if (prop == ZPOOL_PROP_CACHEFILE ||
686 prop == ZPOOL_PROP_ALTROOT ||
687 prop == ZPOOL_PROP_READONLY)
690 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
693 if (prop == ZPOOL_PROP_VERSION) {
694 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
696 ASSERT(zpool_prop_feature(nvpair_name(elem)));
697 ver = SPA_VERSION_FEATURES;
701 /* Save time if the version is already set. */
702 if (ver == spa_version(spa))
706 * In addition to the pool directory object, we might
707 * create the pool properties object, the features for
708 * read object, the features for write object, or the
709 * feature descriptions object.
711 error = dsl_sync_task(spa->spa_name, NULL,
712 spa_sync_version, &ver,
713 6, ZFS_SPACE_CHECK_RESERVED);
724 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
725 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
732 * If the bootfs property value is dsobj, clear it.
735 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
737 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
738 VERIFY(zap_remove(spa->spa_meta_objset,
739 spa->spa_pool_props_object,
740 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
747 spa_change_guid_check(void *arg, dmu_tx_t *tx)
749 uint64_t *newguid = arg;
750 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
751 vdev_t *rvd = spa->spa_root_vdev;
754 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
755 vdev_state = rvd->vdev_state;
756 spa_config_exit(spa, SCL_STATE, FTAG);
758 if (vdev_state != VDEV_STATE_HEALTHY)
759 return (SET_ERROR(ENXIO));
761 ASSERT3U(spa_guid(spa), !=, *newguid);
767 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
769 uint64_t *newguid = arg;
770 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
772 vdev_t *rvd = spa->spa_root_vdev;
774 oldguid = spa_guid(spa);
776 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
777 rvd->vdev_guid = *newguid;
778 rvd->vdev_guid_sum += (*newguid - oldguid);
779 vdev_config_dirty(rvd);
780 spa_config_exit(spa, SCL_STATE, FTAG);
782 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
787 * Change the GUID for the pool. This is done so that we can later
788 * re-import a pool built from a clone of our own vdevs. We will modify
789 * the root vdev's guid, our own pool guid, and then mark all of our
790 * vdevs dirty. Note that we must make sure that all our vdevs are
791 * online when we do this, or else any vdevs that weren't present
792 * would be orphaned from our pool. We are also going to issue a
793 * sysevent to update any watchers.
796 spa_change_guid(spa_t *spa)
801 mutex_enter(&spa->spa_vdev_top_lock);
802 mutex_enter(&spa_namespace_lock);
803 guid = spa_generate_guid(NULL);
805 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
806 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
809 spa_config_sync(spa, B_FALSE, B_TRUE);
810 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
813 mutex_exit(&spa_namespace_lock);
814 mutex_exit(&spa->spa_vdev_top_lock);
820 * ==========================================================================
821 * SPA state manipulation (open/create/destroy/import/export)
822 * ==========================================================================
826 spa_error_entry_compare(const void *a, const void *b)
828 spa_error_entry_t *sa = (spa_error_entry_t *)a;
829 spa_error_entry_t *sb = (spa_error_entry_t *)b;
832 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
833 sizeof (zbookmark_phys_t));
844 * Utility function which retrieves copies of the current logs and
845 * re-initializes them in the process.
848 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
850 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
852 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
853 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
855 avl_create(&spa->spa_errlist_scrub,
856 spa_error_entry_compare, sizeof (spa_error_entry_t),
857 offsetof(spa_error_entry_t, se_avl));
858 avl_create(&spa->spa_errlist_last,
859 spa_error_entry_compare, sizeof (spa_error_entry_t),
860 offsetof(spa_error_entry_t, se_avl));
864 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
866 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
867 enum zti_modes mode = ztip->zti_mode;
868 uint_t value = ztip->zti_value;
869 uint_t count = ztip->zti_count;
870 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
873 boolean_t batch = B_FALSE;
875 if (mode == ZTI_MODE_NULL) {
877 tqs->stqs_taskq = NULL;
881 ASSERT3U(count, >, 0);
883 tqs->stqs_count = count;
884 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
888 ASSERT3U(value, >=, 1);
889 value = MAX(value, 1);
894 flags |= TASKQ_THREADS_CPU_PCT;
895 value = zio_taskq_batch_pct;
899 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
901 zio_type_name[t], zio_taskq_types[q], mode, value);
905 for (uint_t i = 0; i < count; i++) {
909 (void) snprintf(name, sizeof (name), "%s_%s_%u",
910 zio_type_name[t], zio_taskq_types[q], i);
912 (void) snprintf(name, sizeof (name), "%s_%s",
913 zio_type_name[t], zio_taskq_types[q]);
917 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
919 flags |= TASKQ_DC_BATCH;
921 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
922 spa->spa_proc, zio_taskq_basedc, flags);
925 pri_t pri = maxclsyspri;
927 * The write issue taskq can be extremely CPU
928 * intensive. Run it at slightly lower priority
929 * than the other taskqs.
931 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
934 tq = taskq_create_proc(name, value, pri, 50,
935 INT_MAX, spa->spa_proc, flags);
940 tqs->stqs_taskq[i] = tq;
945 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
947 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
949 if (tqs->stqs_taskq == NULL) {
950 ASSERT0(tqs->stqs_count);
954 for (uint_t i = 0; i < tqs->stqs_count; i++) {
955 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
956 taskq_destroy(tqs->stqs_taskq[i]);
959 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
960 tqs->stqs_taskq = NULL;
964 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
965 * Note that a type may have multiple discrete taskqs to avoid lock contention
966 * on the taskq itself. In that case we choose which taskq at random by using
967 * the low bits of gethrtime().
970 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
971 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
973 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
976 ASSERT3P(tqs->stqs_taskq, !=, NULL);
977 ASSERT3U(tqs->stqs_count, !=, 0);
979 if (tqs->stqs_count == 1) {
980 tq = tqs->stqs_taskq[0];
983 tq = tqs->stqs_taskq[cpu_ticks() % tqs->stqs_count];
985 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
989 taskq_dispatch_ent(tq, func, arg, flags, ent);
993 spa_create_zio_taskqs(spa_t *spa)
995 for (int t = 0; t < ZIO_TYPES; t++) {
996 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
997 spa_taskqs_init(spa, t, q);
1005 spa_thread(void *arg)
1007 callb_cpr_t cprinfo;
1010 user_t *pu = PTOU(curproc);
1012 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1015 ASSERT(curproc != &p0);
1016 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1017 "zpool-%s", spa->spa_name);
1018 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1021 /* bind this thread to the requested psrset */
1022 if (zio_taskq_psrset_bind != PS_NONE) {
1024 mutex_enter(&cpu_lock);
1025 mutex_enter(&pidlock);
1026 mutex_enter(&curproc->p_lock);
1028 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1029 0, NULL, NULL) == 0) {
1030 curthread->t_bind_pset = zio_taskq_psrset_bind;
1033 "Couldn't bind process for zfs pool \"%s\" to "
1034 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1037 mutex_exit(&curproc->p_lock);
1038 mutex_exit(&pidlock);
1039 mutex_exit(&cpu_lock);
1045 if (zio_taskq_sysdc) {
1046 sysdc_thread_enter(curthread, 100, 0);
1050 spa->spa_proc = curproc;
1051 spa->spa_did = curthread->t_did;
1053 spa_create_zio_taskqs(spa);
1055 mutex_enter(&spa->spa_proc_lock);
1056 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1058 spa->spa_proc_state = SPA_PROC_ACTIVE;
1059 cv_broadcast(&spa->spa_proc_cv);
1061 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1062 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1063 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1064 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1066 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1067 spa->spa_proc_state = SPA_PROC_GONE;
1068 spa->spa_proc = &p0;
1069 cv_broadcast(&spa->spa_proc_cv);
1070 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1072 mutex_enter(&curproc->p_lock);
1075 #endif /* SPA_PROCESS */
1079 * Activate an uninitialized pool.
1082 spa_activate(spa_t *spa, int mode)
1084 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1086 spa->spa_state = POOL_STATE_ACTIVE;
1087 spa->spa_mode = mode;
1089 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1090 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1092 /* Try to create a covering process */
1093 mutex_enter(&spa->spa_proc_lock);
1094 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1095 ASSERT(spa->spa_proc == &p0);
1099 /* Only create a process if we're going to be around a while. */
1100 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1101 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1103 spa->spa_proc_state = SPA_PROC_CREATED;
1104 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1105 cv_wait(&spa->spa_proc_cv,
1106 &spa->spa_proc_lock);
1108 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1109 ASSERT(spa->spa_proc != &p0);
1110 ASSERT(spa->spa_did != 0);
1114 "Couldn't create process for zfs pool \"%s\"\n",
1119 #endif /* SPA_PROCESS */
1120 mutex_exit(&spa->spa_proc_lock);
1122 /* If we didn't create a process, we need to create our taskqs. */
1123 ASSERT(spa->spa_proc == &p0);
1124 if (spa->spa_proc == &p0) {
1125 spa_create_zio_taskqs(spa);
1129 * Start TRIM thread.
1131 trim_thread_create(spa);
1133 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1134 offsetof(vdev_t, vdev_config_dirty_node));
1135 list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1136 offsetof(objset_t, os_evicting_node));
1137 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1138 offsetof(vdev_t, vdev_state_dirty_node));
1140 txg_list_create(&spa->spa_vdev_txg_list,
1141 offsetof(struct vdev, vdev_txg_node));
1143 avl_create(&spa->spa_errlist_scrub,
1144 spa_error_entry_compare, sizeof (spa_error_entry_t),
1145 offsetof(spa_error_entry_t, se_avl));
1146 avl_create(&spa->spa_errlist_last,
1147 spa_error_entry_compare, sizeof (spa_error_entry_t),
1148 offsetof(spa_error_entry_t, se_avl));
1152 * Opposite of spa_activate().
1155 spa_deactivate(spa_t *spa)
1157 ASSERT(spa->spa_sync_on == B_FALSE);
1158 ASSERT(spa->spa_dsl_pool == NULL);
1159 ASSERT(spa->spa_root_vdev == NULL);
1160 ASSERT(spa->spa_async_zio_root == NULL);
1161 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1164 * Stop TRIM thread in case spa_unload() wasn't called directly
1165 * before spa_deactivate().
1167 trim_thread_destroy(spa);
1169 spa_evicting_os_wait(spa);
1171 txg_list_destroy(&spa->spa_vdev_txg_list);
1173 list_destroy(&spa->spa_config_dirty_list);
1174 list_destroy(&spa->spa_evicting_os_list);
1175 list_destroy(&spa->spa_state_dirty_list);
1177 for (int t = 0; t < ZIO_TYPES; t++) {
1178 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1179 spa_taskqs_fini(spa, t, q);
1183 metaslab_class_destroy(spa->spa_normal_class);
1184 spa->spa_normal_class = NULL;
1186 metaslab_class_destroy(spa->spa_log_class);
1187 spa->spa_log_class = NULL;
1190 * If this was part of an import or the open otherwise failed, we may
1191 * still have errors left in the queues. Empty them just in case.
1193 spa_errlog_drain(spa);
1195 avl_destroy(&spa->spa_errlist_scrub);
1196 avl_destroy(&spa->spa_errlist_last);
1198 spa->spa_state = POOL_STATE_UNINITIALIZED;
1200 mutex_enter(&spa->spa_proc_lock);
1201 if (spa->spa_proc_state != SPA_PROC_NONE) {
1202 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1203 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1204 cv_broadcast(&spa->spa_proc_cv);
1205 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1206 ASSERT(spa->spa_proc != &p0);
1207 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1209 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1210 spa->spa_proc_state = SPA_PROC_NONE;
1212 ASSERT(spa->spa_proc == &p0);
1213 mutex_exit(&spa->spa_proc_lock);
1217 * We want to make sure spa_thread() has actually exited the ZFS
1218 * module, so that the module can't be unloaded out from underneath
1221 if (spa->spa_did != 0) {
1222 thread_join(spa->spa_did);
1225 #endif /* SPA_PROCESS */
1229 * Verify a pool configuration, and construct the vdev tree appropriately. This
1230 * will create all the necessary vdevs in the appropriate layout, with each vdev
1231 * in the CLOSED state. This will prep the pool before open/creation/import.
1232 * All vdev validation is done by the vdev_alloc() routine.
1235 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1236 uint_t id, int atype)
1242 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1245 if ((*vdp)->vdev_ops->vdev_op_leaf)
1248 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1251 if (error == ENOENT)
1257 return (SET_ERROR(EINVAL));
1260 for (int c = 0; c < children; c++) {
1262 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1270 ASSERT(*vdp != NULL);
1276 * Opposite of spa_load().
1279 spa_unload(spa_t *spa)
1283 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1288 trim_thread_destroy(spa);
1293 spa_async_suspend(spa);
1298 if (spa->spa_sync_on) {
1299 txg_sync_stop(spa->spa_dsl_pool);
1300 spa->spa_sync_on = B_FALSE;
1304 * Wait for any outstanding async I/O to complete.
1306 if (spa->spa_async_zio_root != NULL) {
1307 for (int i = 0; i < max_ncpus; i++)
1308 (void) zio_wait(spa->spa_async_zio_root[i]);
1309 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
1310 spa->spa_async_zio_root = NULL;
1313 bpobj_close(&spa->spa_deferred_bpobj);
1315 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1320 if (spa->spa_root_vdev)
1321 vdev_free(spa->spa_root_vdev);
1322 ASSERT(spa->spa_root_vdev == NULL);
1325 * Close the dsl pool.
1327 if (spa->spa_dsl_pool) {
1328 dsl_pool_close(spa->spa_dsl_pool);
1329 spa->spa_dsl_pool = NULL;
1330 spa->spa_meta_objset = NULL;
1337 * Drop and purge level 2 cache
1339 spa_l2cache_drop(spa);
1341 for (i = 0; i < spa->spa_spares.sav_count; i++)
1342 vdev_free(spa->spa_spares.sav_vdevs[i]);
1343 if (spa->spa_spares.sav_vdevs) {
1344 kmem_free(spa->spa_spares.sav_vdevs,
1345 spa->spa_spares.sav_count * sizeof (void *));
1346 spa->spa_spares.sav_vdevs = NULL;
1348 if (spa->spa_spares.sav_config) {
1349 nvlist_free(spa->spa_spares.sav_config);
1350 spa->spa_spares.sav_config = NULL;
1352 spa->spa_spares.sav_count = 0;
1354 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1355 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1356 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1358 if (spa->spa_l2cache.sav_vdevs) {
1359 kmem_free(spa->spa_l2cache.sav_vdevs,
1360 spa->spa_l2cache.sav_count * sizeof (void *));
1361 spa->spa_l2cache.sav_vdevs = NULL;
1363 if (spa->spa_l2cache.sav_config) {
1364 nvlist_free(spa->spa_l2cache.sav_config);
1365 spa->spa_l2cache.sav_config = NULL;
1367 spa->spa_l2cache.sav_count = 0;
1369 spa->spa_async_suspended = 0;
1371 if (spa->spa_comment != NULL) {
1372 spa_strfree(spa->spa_comment);
1373 spa->spa_comment = NULL;
1376 spa_config_exit(spa, SCL_ALL, FTAG);
1380 * Load (or re-load) the current list of vdevs describing the active spares for
1381 * this pool. When this is called, we have some form of basic information in
1382 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1383 * then re-generate a more complete list including status information.
1386 spa_load_spares(spa_t *spa)
1393 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1396 * First, close and free any existing spare vdevs.
1398 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1399 vd = spa->spa_spares.sav_vdevs[i];
1401 /* Undo the call to spa_activate() below */
1402 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1403 B_FALSE)) != NULL && tvd->vdev_isspare)
1404 spa_spare_remove(tvd);
1409 if (spa->spa_spares.sav_vdevs)
1410 kmem_free(spa->spa_spares.sav_vdevs,
1411 spa->spa_spares.sav_count * sizeof (void *));
1413 if (spa->spa_spares.sav_config == NULL)
1416 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1417 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1419 spa->spa_spares.sav_count = (int)nspares;
1420 spa->spa_spares.sav_vdevs = NULL;
1426 * Construct the array of vdevs, opening them to get status in the
1427 * process. For each spare, there is potentially two different vdev_t
1428 * structures associated with it: one in the list of spares (used only
1429 * for basic validation purposes) and one in the active vdev
1430 * configuration (if it's spared in). During this phase we open and
1431 * validate each vdev on the spare list. If the vdev also exists in the
1432 * active configuration, then we also mark this vdev as an active spare.
1434 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1436 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1437 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1438 VDEV_ALLOC_SPARE) == 0);
1441 spa->spa_spares.sav_vdevs[i] = vd;
1443 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1444 B_FALSE)) != NULL) {
1445 if (!tvd->vdev_isspare)
1449 * We only mark the spare active if we were successfully
1450 * able to load the vdev. Otherwise, importing a pool
1451 * with a bad active spare would result in strange
1452 * behavior, because multiple pool would think the spare
1453 * is actively in use.
1455 * There is a vulnerability here to an equally bizarre
1456 * circumstance, where a dead active spare is later
1457 * brought back to life (onlined or otherwise). Given
1458 * the rarity of this scenario, and the extra complexity
1459 * it adds, we ignore the possibility.
1461 if (!vdev_is_dead(tvd))
1462 spa_spare_activate(tvd);
1466 vd->vdev_aux = &spa->spa_spares;
1468 if (vdev_open(vd) != 0)
1471 if (vdev_validate_aux(vd) == 0)
1476 * Recompute the stashed list of spares, with status information
1479 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1480 DATA_TYPE_NVLIST_ARRAY) == 0);
1482 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1484 for (i = 0; i < spa->spa_spares.sav_count; i++)
1485 spares[i] = vdev_config_generate(spa,
1486 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1487 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1488 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1489 for (i = 0; i < spa->spa_spares.sav_count; i++)
1490 nvlist_free(spares[i]);
1491 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1495 * Load (or re-load) the current list of vdevs describing the active l2cache for
1496 * this pool. When this is called, we have some form of basic information in
1497 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1498 * then re-generate a more complete list including status information.
1499 * Devices which are already active have their details maintained, and are
1503 spa_load_l2cache(spa_t *spa)
1507 int i, j, oldnvdevs;
1509 vdev_t *vd, **oldvdevs, **newvdevs;
1510 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1512 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1514 if (sav->sav_config != NULL) {
1515 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1516 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1517 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1523 oldvdevs = sav->sav_vdevs;
1524 oldnvdevs = sav->sav_count;
1525 sav->sav_vdevs = NULL;
1529 * Process new nvlist of vdevs.
1531 for (i = 0; i < nl2cache; i++) {
1532 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1536 for (j = 0; j < oldnvdevs; j++) {
1538 if (vd != NULL && guid == vd->vdev_guid) {
1540 * Retain previous vdev for add/remove ops.
1548 if (newvdevs[i] == NULL) {
1552 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1553 VDEV_ALLOC_L2CACHE) == 0);
1558 * Commit this vdev as an l2cache device,
1559 * even if it fails to open.
1561 spa_l2cache_add(vd);
1566 spa_l2cache_activate(vd);
1568 if (vdev_open(vd) != 0)
1571 (void) vdev_validate_aux(vd);
1573 if (!vdev_is_dead(vd))
1574 l2arc_add_vdev(spa, vd);
1579 * Purge vdevs that were dropped
1581 for (i = 0; i < oldnvdevs; i++) {
1586 ASSERT(vd->vdev_isl2cache);
1588 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1589 pool != 0ULL && l2arc_vdev_present(vd))
1590 l2arc_remove_vdev(vd);
1591 vdev_clear_stats(vd);
1597 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1599 if (sav->sav_config == NULL)
1602 sav->sav_vdevs = newvdevs;
1603 sav->sav_count = (int)nl2cache;
1606 * Recompute the stashed list of l2cache devices, with status
1607 * information this time.
1609 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1610 DATA_TYPE_NVLIST_ARRAY) == 0);
1612 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1613 for (i = 0; i < sav->sav_count; i++)
1614 l2cache[i] = vdev_config_generate(spa,
1615 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1616 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1617 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1619 for (i = 0; i < sav->sav_count; i++)
1620 nvlist_free(l2cache[i]);
1622 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1626 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1629 char *packed = NULL;
1634 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1638 nvsize = *(uint64_t *)db->db_data;
1639 dmu_buf_rele(db, FTAG);
1641 packed = kmem_alloc(nvsize, KM_SLEEP);
1642 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1645 error = nvlist_unpack(packed, nvsize, value, 0);
1646 kmem_free(packed, nvsize);
1652 * Checks to see if the given vdev could not be opened, in which case we post a
1653 * sysevent to notify the autoreplace code that the device has been removed.
1656 spa_check_removed(vdev_t *vd)
1658 for (int c = 0; c < vd->vdev_children; c++)
1659 spa_check_removed(vd->vdev_child[c]);
1661 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1663 zfs_post_autoreplace(vd->vdev_spa, vd);
1664 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1669 spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd)
1671 ASSERT3U(vd->vdev_children, ==, mvd->vdev_children);
1673 vd->vdev_top_zap = mvd->vdev_top_zap;
1674 vd->vdev_leaf_zap = mvd->vdev_leaf_zap;
1676 for (uint64_t i = 0; i < vd->vdev_children; i++) {
1677 spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]);
1682 * Validate the current config against the MOS config
1685 spa_config_valid(spa_t *spa, nvlist_t *config)
1687 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1690 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1692 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1693 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1695 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1698 * If we're doing a normal import, then build up any additional
1699 * diagnostic information about missing devices in this config.
1700 * We'll pass this up to the user for further processing.
1702 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1703 nvlist_t **child, *nv;
1706 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1708 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1710 for (int c = 0; c < rvd->vdev_children; c++) {
1711 vdev_t *tvd = rvd->vdev_child[c];
1712 vdev_t *mtvd = mrvd->vdev_child[c];
1714 if (tvd->vdev_ops == &vdev_missing_ops &&
1715 mtvd->vdev_ops != &vdev_missing_ops &&
1717 child[idx++] = vdev_config_generate(spa, mtvd,
1722 VERIFY(nvlist_add_nvlist_array(nv,
1723 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1724 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1725 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1727 for (int i = 0; i < idx; i++)
1728 nvlist_free(child[i]);
1731 kmem_free(child, rvd->vdev_children * sizeof (char **));
1735 * Compare the root vdev tree with the information we have
1736 * from the MOS config (mrvd). Check each top-level vdev
1737 * with the corresponding MOS config top-level (mtvd).
1739 for (int c = 0; c < rvd->vdev_children; c++) {
1740 vdev_t *tvd = rvd->vdev_child[c];
1741 vdev_t *mtvd = mrvd->vdev_child[c];
1744 * Resolve any "missing" vdevs in the current configuration.
1745 * If we find that the MOS config has more accurate information
1746 * about the top-level vdev then use that vdev instead.
1748 if (tvd->vdev_ops == &vdev_missing_ops &&
1749 mtvd->vdev_ops != &vdev_missing_ops) {
1751 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1755 * Device specific actions.
1757 if (mtvd->vdev_islog) {
1758 spa_set_log_state(spa, SPA_LOG_CLEAR);
1761 * XXX - once we have 'readonly' pool
1762 * support we should be able to handle
1763 * missing data devices by transitioning
1764 * the pool to readonly.
1770 * Swap the missing vdev with the data we were
1771 * able to obtain from the MOS config.
1773 vdev_remove_child(rvd, tvd);
1774 vdev_remove_child(mrvd, mtvd);
1776 vdev_add_child(rvd, mtvd);
1777 vdev_add_child(mrvd, tvd);
1779 spa_config_exit(spa, SCL_ALL, FTAG);
1781 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1785 if (mtvd->vdev_islog) {
1787 * Load the slog device's state from the MOS
1788 * config since it's possible that the label
1789 * does not contain the most up-to-date
1792 vdev_load_log_state(tvd, mtvd);
1797 * Per-vdev ZAP info is stored exclusively in the MOS.
1799 spa_config_valid_zaps(tvd, mtvd);
1804 spa_config_exit(spa, SCL_ALL, FTAG);
1807 * Ensure we were able to validate the config.
1809 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1813 * Check for missing log devices
1816 spa_check_logs(spa_t *spa)
1818 boolean_t rv = B_FALSE;
1819 dsl_pool_t *dp = spa_get_dsl(spa);
1821 switch (spa->spa_log_state) {
1822 case SPA_LOG_MISSING:
1823 /* need to recheck in case slog has been restored */
1824 case SPA_LOG_UNKNOWN:
1825 rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1826 zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
1828 spa_set_log_state(spa, SPA_LOG_MISSING);
1835 spa_passivate_log(spa_t *spa)
1837 vdev_t *rvd = spa->spa_root_vdev;
1838 boolean_t slog_found = B_FALSE;
1840 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1842 if (!spa_has_slogs(spa))
1845 for (int c = 0; c < rvd->vdev_children; c++) {
1846 vdev_t *tvd = rvd->vdev_child[c];
1847 metaslab_group_t *mg = tvd->vdev_mg;
1849 if (tvd->vdev_islog) {
1850 metaslab_group_passivate(mg);
1851 slog_found = B_TRUE;
1855 return (slog_found);
1859 spa_activate_log(spa_t *spa)
1861 vdev_t *rvd = spa->spa_root_vdev;
1863 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1865 for (int c = 0; c < rvd->vdev_children; c++) {
1866 vdev_t *tvd = rvd->vdev_child[c];
1867 metaslab_group_t *mg = tvd->vdev_mg;
1869 if (tvd->vdev_islog)
1870 metaslab_group_activate(mg);
1875 spa_offline_log(spa_t *spa)
1879 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1880 NULL, DS_FIND_CHILDREN);
1883 * We successfully offlined the log device, sync out the
1884 * current txg so that the "stubby" block can be removed
1887 txg_wait_synced(spa->spa_dsl_pool, 0);
1893 spa_aux_check_removed(spa_aux_vdev_t *sav)
1897 for (i = 0; i < sav->sav_count; i++)
1898 spa_check_removed(sav->sav_vdevs[i]);
1902 spa_claim_notify(zio_t *zio)
1904 spa_t *spa = zio->io_spa;
1909 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1910 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1911 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1912 mutex_exit(&spa->spa_props_lock);
1915 typedef struct spa_load_error {
1916 uint64_t sle_meta_count;
1917 uint64_t sle_data_count;
1921 spa_load_verify_done(zio_t *zio)
1923 blkptr_t *bp = zio->io_bp;
1924 spa_load_error_t *sle = zio->io_private;
1925 dmu_object_type_t type = BP_GET_TYPE(bp);
1926 int error = zio->io_error;
1927 spa_t *spa = zio->io_spa;
1930 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1931 type != DMU_OT_INTENT_LOG)
1932 atomic_inc_64(&sle->sle_meta_count);
1934 atomic_inc_64(&sle->sle_data_count);
1936 zio_data_buf_free(zio->io_data, zio->io_size);
1938 mutex_enter(&spa->spa_scrub_lock);
1939 spa->spa_scrub_inflight--;
1940 cv_broadcast(&spa->spa_scrub_io_cv);
1941 mutex_exit(&spa->spa_scrub_lock);
1945 * Maximum number of concurrent scrub i/os to create while verifying
1946 * a pool while importing it.
1948 int spa_load_verify_maxinflight = 10000;
1949 boolean_t spa_load_verify_metadata = B_TRUE;
1950 boolean_t spa_load_verify_data = B_TRUE;
1952 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1953 &spa_load_verify_maxinflight, 0,
1954 "Maximum number of concurrent scrub I/Os to create while verifying a "
1955 "pool while importing it");
1957 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1958 &spa_load_verify_metadata, 0,
1959 "Check metadata on import?");
1961 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1962 &spa_load_verify_data, 0,
1963 "Check user data on import?");
1967 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1968 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1970 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1973 * Note: normally this routine will not be called if
1974 * spa_load_verify_metadata is not set. However, it may be useful
1975 * to manually set the flag after the traversal has begun.
1977 if (!spa_load_verify_metadata)
1979 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1983 size_t size = BP_GET_PSIZE(bp);
1984 void *data = zio_data_buf_alloc(size);
1986 mutex_enter(&spa->spa_scrub_lock);
1987 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1988 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1989 spa->spa_scrub_inflight++;
1990 mutex_exit(&spa->spa_scrub_lock);
1992 zio_nowait(zio_read(rio, spa, bp, data, size,
1993 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1994 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1995 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
2001 verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2003 char namebuf[MAXPATHLEN];
2004 dsl_dataset_name(ds, namebuf);
2005 if (strlen(namebuf) > MAXNAMELEN) {
2006 return (SET_ERROR(ENAMETOOLONG));
2013 spa_load_verify(spa_t *spa)
2016 spa_load_error_t sle = { 0 };
2017 zpool_rewind_policy_t policy;
2018 boolean_t verify_ok = B_FALSE;
2021 zpool_get_rewind_policy(spa->spa_config, &policy);
2023 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
2026 dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
2027 error = dmu_objset_find_dp(spa->spa_dsl_pool,
2028 spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
2030 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
2034 rio = zio_root(spa, NULL, &sle,
2035 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
2037 if (spa_load_verify_metadata) {
2038 error = traverse_pool(spa, spa->spa_verify_min_txg,
2039 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2040 spa_load_verify_cb, rio);
2043 (void) zio_wait(rio);
2045 spa->spa_load_meta_errors = sle.sle_meta_count;
2046 spa->spa_load_data_errors = sle.sle_data_count;
2048 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
2049 sle.sle_data_count <= policy.zrp_maxdata) {
2053 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2054 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2056 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2057 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2058 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2059 VERIFY(nvlist_add_int64(spa->spa_load_info,
2060 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2061 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2062 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2064 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2068 if (error != ENXIO && error != EIO)
2069 error = SET_ERROR(EIO);
2073 return (verify_ok ? 0 : EIO);
2077 * Find a value in the pool props object.
2080 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2082 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2083 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2087 * Find a value in the pool directory object.
2090 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2092 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2093 name, sizeof (uint64_t), 1, val));
2097 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2099 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2104 * Fix up config after a partly-completed split. This is done with the
2105 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2106 * pool have that entry in their config, but only the splitting one contains
2107 * a list of all the guids of the vdevs that are being split off.
2109 * This function determines what to do with that list: either rejoin
2110 * all the disks to the pool, or complete the splitting process. To attempt
2111 * the rejoin, each disk that is offlined is marked online again, and
2112 * we do a reopen() call. If the vdev label for every disk that was
2113 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2114 * then we call vdev_split() on each disk, and complete the split.
2116 * Otherwise we leave the config alone, with all the vdevs in place in
2117 * the original pool.
2120 spa_try_repair(spa_t *spa, nvlist_t *config)
2127 boolean_t attempt_reopen;
2129 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2132 /* check that the config is complete */
2133 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2134 &glist, &gcount) != 0)
2137 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2139 /* attempt to online all the vdevs & validate */
2140 attempt_reopen = B_TRUE;
2141 for (i = 0; i < gcount; i++) {
2142 if (glist[i] == 0) /* vdev is hole */
2145 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2146 if (vd[i] == NULL) {
2148 * Don't bother attempting to reopen the disks;
2149 * just do the split.
2151 attempt_reopen = B_FALSE;
2153 /* attempt to re-online it */
2154 vd[i]->vdev_offline = B_FALSE;
2158 if (attempt_reopen) {
2159 vdev_reopen(spa->spa_root_vdev);
2161 /* check each device to see what state it's in */
2162 for (extracted = 0, i = 0; i < gcount; i++) {
2163 if (vd[i] != NULL &&
2164 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2171 * If every disk has been moved to the new pool, or if we never
2172 * even attempted to look at them, then we split them off for
2175 if (!attempt_reopen || gcount == extracted) {
2176 for (i = 0; i < gcount; i++)
2179 vdev_reopen(spa->spa_root_vdev);
2182 kmem_free(vd, gcount * sizeof (vdev_t *));
2186 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2187 boolean_t mosconfig)
2189 nvlist_t *config = spa->spa_config;
2190 char *ereport = FM_EREPORT_ZFS_POOL;
2196 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2197 return (SET_ERROR(EINVAL));
2199 ASSERT(spa->spa_comment == NULL);
2200 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2201 spa->spa_comment = spa_strdup(comment);
2204 * Versioning wasn't explicitly added to the label until later, so if
2205 * it's not present treat it as the initial version.
2207 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2208 &spa->spa_ubsync.ub_version) != 0)
2209 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2211 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2212 &spa->spa_config_txg);
2214 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2215 spa_guid_exists(pool_guid, 0)) {
2216 error = SET_ERROR(EEXIST);
2218 spa->spa_config_guid = pool_guid;
2220 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2222 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2226 nvlist_free(spa->spa_load_info);
2227 spa->spa_load_info = fnvlist_alloc();
2229 gethrestime(&spa->spa_loaded_ts);
2230 error = spa_load_impl(spa, pool_guid, config, state, type,
2231 mosconfig, &ereport);
2235 * Don't count references from objsets that are already closed
2236 * and are making their way through the eviction process.
2238 spa_evicting_os_wait(spa);
2239 spa->spa_minref = refcount_count(&spa->spa_refcount);
2241 if (error != EEXIST) {
2242 spa->spa_loaded_ts.tv_sec = 0;
2243 spa->spa_loaded_ts.tv_nsec = 0;
2245 if (error != EBADF) {
2246 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2249 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2256 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2257 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2258 * spa's per-vdev ZAP list.
2261 vdev_count_verify_zaps(vdev_t *vd)
2263 spa_t *spa = vd->vdev_spa;
2265 if (vd->vdev_top_zap != 0) {
2267 ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2268 spa->spa_all_vdev_zaps, vd->vdev_top_zap));
2270 if (vd->vdev_leaf_zap != 0) {
2272 ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2273 spa->spa_all_vdev_zaps, vd->vdev_leaf_zap));
2276 for (uint64_t i = 0; i < vd->vdev_children; i++) {
2277 total += vdev_count_verify_zaps(vd->vdev_child[i]);
2284 * Load an existing storage pool, using the pool's builtin spa_config as a
2285 * source of configuration information.
2288 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2289 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2293 nvlist_t *nvroot = NULL;
2296 uberblock_t *ub = &spa->spa_uberblock;
2297 uint64_t children, config_cache_txg = spa->spa_config_txg;
2298 int orig_mode = spa->spa_mode;
2301 boolean_t missing_feat_write = B_FALSE;
2304 * If this is an untrusted config, access the pool in read-only mode.
2305 * This prevents things like resilvering recently removed devices.
2308 spa->spa_mode = FREAD;
2310 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2312 spa->spa_load_state = state;
2314 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2315 return (SET_ERROR(EINVAL));
2317 parse = (type == SPA_IMPORT_EXISTING ?
2318 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2321 * Create "The Godfather" zio to hold all async IOs
2323 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2325 for (int i = 0; i < max_ncpus; i++) {
2326 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2327 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2328 ZIO_FLAG_GODFATHER);
2332 * Parse the configuration into a vdev tree. We explicitly set the
2333 * value that will be returned by spa_version() since parsing the
2334 * configuration requires knowing the version number.
2336 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2337 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2338 spa_config_exit(spa, SCL_ALL, FTAG);
2343 ASSERT(spa->spa_root_vdev == rvd);
2344 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2345 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
2347 if (type != SPA_IMPORT_ASSEMBLE) {
2348 ASSERT(spa_guid(spa) == pool_guid);
2352 * Try to open all vdevs, loading each label in the process.
2354 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2355 error = vdev_open(rvd);
2356 spa_config_exit(spa, SCL_ALL, FTAG);
2361 * We need to validate the vdev labels against the configuration that
2362 * we have in hand, which is dependent on the setting of mosconfig. If
2363 * mosconfig is true then we're validating the vdev labels based on
2364 * that config. Otherwise, we're validating against the cached config
2365 * (zpool.cache) that was read when we loaded the zfs module, and then
2366 * later we will recursively call spa_load() and validate against
2369 * If we're assembling a new pool that's been split off from an
2370 * existing pool, the labels haven't yet been updated so we skip
2371 * validation for now.
2373 if (type != SPA_IMPORT_ASSEMBLE) {
2374 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2375 error = vdev_validate(rvd, mosconfig);
2376 spa_config_exit(spa, SCL_ALL, FTAG);
2381 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2382 return (SET_ERROR(ENXIO));
2386 * Find the best uberblock.
2388 vdev_uberblock_load(rvd, ub, &label);
2391 * If we weren't able to find a single valid uberblock, return failure.
2393 if (ub->ub_txg == 0) {
2395 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2399 * If the pool has an unsupported version we can't open it.
2401 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2403 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2406 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2410 * If we weren't able to find what's necessary for reading the
2411 * MOS in the label, return failure.
2413 if (label == NULL || nvlist_lookup_nvlist(label,
2414 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2416 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2421 * Update our in-core representation with the definitive values
2424 nvlist_free(spa->spa_label_features);
2425 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2431 * Look through entries in the label nvlist's features_for_read. If
2432 * there is a feature listed there which we don't understand then we
2433 * cannot open a pool.
2435 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2436 nvlist_t *unsup_feat;
2438 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2441 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2443 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2444 if (!zfeature_is_supported(nvpair_name(nvp))) {
2445 VERIFY(nvlist_add_string(unsup_feat,
2446 nvpair_name(nvp), "") == 0);
2450 if (!nvlist_empty(unsup_feat)) {
2451 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2452 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2453 nvlist_free(unsup_feat);
2454 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2458 nvlist_free(unsup_feat);
2462 * If the vdev guid sum doesn't match the uberblock, we have an
2463 * incomplete configuration. We first check to see if the pool
2464 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2465 * If it is, defer the vdev_guid_sum check till later so we
2466 * can handle missing vdevs.
2468 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2469 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2470 rvd->vdev_guid_sum != ub->ub_guid_sum)
2471 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2473 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2474 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2475 spa_try_repair(spa, config);
2476 spa_config_exit(spa, SCL_ALL, FTAG);
2477 nvlist_free(spa->spa_config_splitting);
2478 spa->spa_config_splitting = NULL;
2482 * Initialize internal SPA structures.
2484 spa->spa_state = POOL_STATE_ACTIVE;
2485 spa->spa_ubsync = spa->spa_uberblock;
2486 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2487 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2488 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2489 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2490 spa->spa_claim_max_txg = spa->spa_first_txg;
2491 spa->spa_prev_software_version = ub->ub_software_version;
2493 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2495 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2496 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2498 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2499 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2501 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2502 boolean_t missing_feat_read = B_FALSE;
2503 nvlist_t *unsup_feat, *enabled_feat;
2505 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2506 &spa->spa_feat_for_read_obj) != 0) {
2507 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2510 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2511 &spa->spa_feat_for_write_obj) != 0) {
2512 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2515 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2516 &spa->spa_feat_desc_obj) != 0) {
2517 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2520 enabled_feat = fnvlist_alloc();
2521 unsup_feat = fnvlist_alloc();
2523 if (!spa_features_check(spa, B_FALSE,
2524 unsup_feat, enabled_feat))
2525 missing_feat_read = B_TRUE;
2527 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2528 if (!spa_features_check(spa, B_TRUE,
2529 unsup_feat, enabled_feat)) {
2530 missing_feat_write = B_TRUE;
2534 fnvlist_add_nvlist(spa->spa_load_info,
2535 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2537 if (!nvlist_empty(unsup_feat)) {
2538 fnvlist_add_nvlist(spa->spa_load_info,
2539 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2542 fnvlist_free(enabled_feat);
2543 fnvlist_free(unsup_feat);
2545 if (!missing_feat_read) {
2546 fnvlist_add_boolean(spa->spa_load_info,
2547 ZPOOL_CONFIG_CAN_RDONLY);
2551 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2552 * twofold: to determine whether the pool is available for
2553 * import in read-write mode and (if it is not) whether the
2554 * pool is available for import in read-only mode. If the pool
2555 * is available for import in read-write mode, it is displayed
2556 * as available in userland; if it is not available for import
2557 * in read-only mode, it is displayed as unavailable in
2558 * userland. If the pool is available for import in read-only
2559 * mode but not read-write mode, it is displayed as unavailable
2560 * in userland with a special note that the pool is actually
2561 * available for open in read-only mode.
2563 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2564 * missing a feature for write, we must first determine whether
2565 * the pool can be opened read-only before returning to
2566 * userland in order to know whether to display the
2567 * abovementioned note.
2569 if (missing_feat_read || (missing_feat_write &&
2570 spa_writeable(spa))) {
2571 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2576 * Load refcounts for ZFS features from disk into an in-memory
2577 * cache during SPA initialization.
2579 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2582 error = feature_get_refcount_from_disk(spa,
2583 &spa_feature_table[i], &refcount);
2585 spa->spa_feat_refcount_cache[i] = refcount;
2586 } else if (error == ENOTSUP) {
2587 spa->spa_feat_refcount_cache[i] =
2588 SPA_FEATURE_DISABLED;
2590 return (spa_vdev_err(rvd,
2591 VDEV_AUX_CORRUPT_DATA, EIO));
2596 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2597 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2598 &spa->spa_feat_enabled_txg_obj) != 0)
2599 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2602 spa->spa_is_initializing = B_TRUE;
2603 error = dsl_pool_open(spa->spa_dsl_pool);
2604 spa->spa_is_initializing = B_FALSE;
2606 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2610 nvlist_t *policy = NULL, *nvconfig;
2612 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2613 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2615 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2616 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2618 unsigned long myhostid = 0;
2620 VERIFY(nvlist_lookup_string(nvconfig,
2621 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2624 myhostid = zone_get_hostid(NULL);
2627 * We're emulating the system's hostid in userland, so
2628 * we can't use zone_get_hostid().
2630 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2631 #endif /* _KERNEL */
2632 if (check_hostid && hostid != 0 && myhostid != 0 &&
2633 hostid != myhostid) {
2634 nvlist_free(nvconfig);
2635 cmn_err(CE_WARN, "pool '%s' could not be "
2636 "loaded as it was last accessed by "
2637 "another system (host: %s hostid: 0x%lx). "
2638 "See: http://illumos.org/msg/ZFS-8000-EY",
2639 spa_name(spa), hostname,
2640 (unsigned long)hostid);
2641 return (SET_ERROR(EBADF));
2644 if (nvlist_lookup_nvlist(spa->spa_config,
2645 ZPOOL_REWIND_POLICY, &policy) == 0)
2646 VERIFY(nvlist_add_nvlist(nvconfig,
2647 ZPOOL_REWIND_POLICY, policy) == 0);
2649 spa_config_set(spa, nvconfig);
2651 spa_deactivate(spa);
2652 spa_activate(spa, orig_mode);
2654 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2657 /* Grab the secret checksum salt from the MOS. */
2658 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2659 DMU_POOL_CHECKSUM_SALT, 1,
2660 sizeof (spa->spa_cksum_salt.zcs_bytes),
2661 spa->spa_cksum_salt.zcs_bytes);
2662 if (error == ENOENT) {
2663 /* Generate a new salt for subsequent use */
2664 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
2665 sizeof (spa->spa_cksum_salt.zcs_bytes));
2666 } else if (error != 0) {
2667 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2670 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2671 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2672 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2674 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2677 * Load the bit that tells us to use the new accounting function
2678 * (raid-z deflation). If we have an older pool, this will not
2681 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2682 if (error != 0 && error != ENOENT)
2683 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2685 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2686 &spa->spa_creation_version);
2687 if (error != 0 && error != ENOENT)
2688 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2691 * Load the persistent error log. If we have an older pool, this will
2694 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2695 if (error != 0 && error != ENOENT)
2696 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2698 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2699 &spa->spa_errlog_scrub);
2700 if (error != 0 && error != ENOENT)
2701 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2704 * Load the history object. If we have an older pool, this
2705 * will not be present.
2707 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2708 if (error != 0 && error != ENOENT)
2709 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2712 * Load the per-vdev ZAP map. If we have an older pool, this will not
2713 * be present; in this case, defer its creation to a later time to
2714 * avoid dirtying the MOS this early / out of sync context. See
2715 * spa_sync_config_object.
2718 /* The sentinel is only available in the MOS config. */
2719 nvlist_t *mos_config;
2720 if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0)
2721 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2723 error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP,
2724 &spa->spa_all_vdev_zaps);
2726 if (error != ENOENT && error != 0) {
2727 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2728 } else if (error == 0 && !nvlist_exists(mos_config,
2729 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) {
2731 * An older version of ZFS overwrote the sentinel value, so
2732 * we have orphaned per-vdev ZAPs in the MOS. Defer their
2733 * destruction to later; see spa_sync_config_object.
2735 spa->spa_avz_action = AVZ_ACTION_DESTROY;
2737 * We're assuming that no vdevs have had their ZAPs created
2738 * before this. Better be sure of it.
2740 ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
2742 nvlist_free(mos_config);
2745 * If we're assembling the pool from the split-off vdevs of
2746 * an existing pool, we don't want to attach the spares & cache
2751 * Load any hot spares for this pool.
2753 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2754 if (error != 0 && error != ENOENT)
2755 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2756 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2757 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2758 if (load_nvlist(spa, spa->spa_spares.sav_object,
2759 &spa->spa_spares.sav_config) != 0)
2760 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2762 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2763 spa_load_spares(spa);
2764 spa_config_exit(spa, SCL_ALL, FTAG);
2765 } else if (error == 0) {
2766 spa->spa_spares.sav_sync = B_TRUE;
2770 * Load any level 2 ARC devices for this pool.
2772 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2773 &spa->spa_l2cache.sav_object);
2774 if (error != 0 && error != ENOENT)
2775 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2776 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2777 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2778 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2779 &spa->spa_l2cache.sav_config) != 0)
2780 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2782 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2783 spa_load_l2cache(spa);
2784 spa_config_exit(spa, SCL_ALL, FTAG);
2785 } else if (error == 0) {
2786 spa->spa_l2cache.sav_sync = B_TRUE;
2789 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2791 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2792 if (error && error != ENOENT)
2793 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2796 uint64_t autoreplace;
2798 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2799 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2800 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2801 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2802 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2803 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2804 &spa->spa_dedup_ditto);
2806 spa->spa_autoreplace = (autoreplace != 0);
2810 * If the 'autoreplace' property is set, then post a resource notifying
2811 * the ZFS DE that it should not issue any faults for unopenable
2812 * devices. We also iterate over the vdevs, and post a sysevent for any
2813 * unopenable vdevs so that the normal autoreplace handler can take
2816 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2817 spa_check_removed(spa->spa_root_vdev);
2819 * For the import case, this is done in spa_import(), because
2820 * at this point we're using the spare definitions from
2821 * the MOS config, not necessarily from the userland config.
2823 if (state != SPA_LOAD_IMPORT) {
2824 spa_aux_check_removed(&spa->spa_spares);
2825 spa_aux_check_removed(&spa->spa_l2cache);
2830 * Load the vdev state for all toplevel vdevs.
2835 * Propagate the leaf DTLs we just loaded all the way up the tree.
2837 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2838 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2839 spa_config_exit(spa, SCL_ALL, FTAG);
2842 * Load the DDTs (dedup tables).
2844 error = ddt_load(spa);
2846 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2848 spa_update_dspace(spa);
2851 * Validate the config, using the MOS config to fill in any
2852 * information which might be missing. If we fail to validate
2853 * the config then declare the pool unfit for use. If we're
2854 * assembling a pool from a split, the log is not transferred
2857 if (type != SPA_IMPORT_ASSEMBLE) {
2860 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2861 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2863 if (!spa_config_valid(spa, nvconfig)) {
2864 nvlist_free(nvconfig);
2865 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2868 nvlist_free(nvconfig);
2871 * Now that we've validated the config, check the state of the
2872 * root vdev. If it can't be opened, it indicates one or
2873 * more toplevel vdevs are faulted.
2875 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2876 return (SET_ERROR(ENXIO));
2878 if (spa_writeable(spa) && spa_check_logs(spa)) {
2879 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2880 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2884 if (missing_feat_write) {
2885 ASSERT(state == SPA_LOAD_TRYIMPORT);
2888 * At this point, we know that we can open the pool in
2889 * read-only mode but not read-write mode. We now have enough
2890 * information and can return to userland.
2892 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2896 * We've successfully opened the pool, verify that we're ready
2897 * to start pushing transactions.
2899 if (state != SPA_LOAD_TRYIMPORT) {
2900 if (error = spa_load_verify(spa))
2901 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2905 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2906 spa->spa_load_max_txg == UINT64_MAX)) {
2908 int need_update = B_FALSE;
2909 dsl_pool_t *dp = spa_get_dsl(spa);
2911 ASSERT(state != SPA_LOAD_TRYIMPORT);
2914 * Claim log blocks that haven't been committed yet.
2915 * This must all happen in a single txg.
2916 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2917 * invoked from zil_claim_log_block()'s i/o done callback.
2918 * Price of rollback is that we abandon the log.
2920 spa->spa_claiming = B_TRUE;
2922 tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
2923 (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2924 zil_claim, tx, DS_FIND_CHILDREN);
2927 spa->spa_claiming = B_FALSE;
2929 spa_set_log_state(spa, SPA_LOG_GOOD);
2930 spa->spa_sync_on = B_TRUE;
2931 txg_sync_start(spa->spa_dsl_pool);
2934 * Wait for all claims to sync. We sync up to the highest
2935 * claimed log block birth time so that claimed log blocks
2936 * don't appear to be from the future. spa_claim_max_txg
2937 * will have been set for us by either zil_check_log_chain()
2938 * (invoked from spa_check_logs()) or zil_claim() above.
2940 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2943 * If the config cache is stale, or we have uninitialized
2944 * metaslabs (see spa_vdev_add()), then update the config.
2946 * If this is a verbatim import, trust the current
2947 * in-core spa_config and update the disk labels.
2949 if (config_cache_txg != spa->spa_config_txg ||
2950 state == SPA_LOAD_IMPORT ||
2951 state == SPA_LOAD_RECOVER ||
2952 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2953 need_update = B_TRUE;
2955 for (int c = 0; c < rvd->vdev_children; c++)
2956 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2957 need_update = B_TRUE;
2960 * Update the config cache asychronously in case we're the
2961 * root pool, in which case the config cache isn't writable yet.
2964 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2967 * Check all DTLs to see if anything needs resilvering.
2969 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2970 vdev_resilver_needed(rvd, NULL, NULL))
2971 spa_async_request(spa, SPA_ASYNC_RESILVER);
2974 * Log the fact that we booted up (so that we can detect if
2975 * we rebooted in the middle of an operation).
2977 spa_history_log_version(spa, "open");
2980 * Delete any inconsistent datasets.
2982 (void) dmu_objset_find(spa_name(spa),
2983 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2986 * Clean up any stale temporary dataset userrefs.
2988 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2995 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2997 int mode = spa->spa_mode;
3000 spa_deactivate(spa);
3002 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
3004 spa_activate(spa, mode);
3005 spa_async_suspend(spa);
3007 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
3011 * If spa_load() fails this function will try loading prior txg's. If
3012 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3013 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3014 * function will not rewind the pool and will return the same error as
3018 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
3019 uint64_t max_request, int rewind_flags)
3021 nvlist_t *loadinfo = NULL;
3022 nvlist_t *config = NULL;
3023 int load_error, rewind_error;
3024 uint64_t safe_rewind_txg;
3027 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
3028 spa->spa_load_max_txg = spa->spa_load_txg;
3029 spa_set_log_state(spa, SPA_LOG_CLEAR);
3031 spa->spa_load_max_txg = max_request;
3032 if (max_request != UINT64_MAX)
3033 spa->spa_extreme_rewind = B_TRUE;
3036 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
3038 if (load_error == 0)
3041 if (spa->spa_root_vdev != NULL)
3042 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3044 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
3045 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
3047 if (rewind_flags & ZPOOL_NEVER_REWIND) {
3048 nvlist_free(config);
3049 return (load_error);
3052 if (state == SPA_LOAD_RECOVER) {
3053 /* Price of rolling back is discarding txgs, including log */
3054 spa_set_log_state(spa, SPA_LOG_CLEAR);
3057 * If we aren't rolling back save the load info from our first
3058 * import attempt so that we can restore it after attempting
3061 loadinfo = spa->spa_load_info;
3062 spa->spa_load_info = fnvlist_alloc();
3065 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
3066 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
3067 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
3068 TXG_INITIAL : safe_rewind_txg;
3071 * Continue as long as we're finding errors, we're still within
3072 * the acceptable rewind range, and we're still finding uberblocks
3074 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
3075 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
3076 if (spa->spa_load_max_txg < safe_rewind_txg)
3077 spa->spa_extreme_rewind = B_TRUE;
3078 rewind_error = spa_load_retry(spa, state, mosconfig);
3081 spa->spa_extreme_rewind = B_FALSE;
3082 spa->spa_load_max_txg = UINT64_MAX;
3084 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
3085 spa_config_set(spa, config);
3087 if (state == SPA_LOAD_RECOVER) {
3088 ASSERT3P(loadinfo, ==, NULL);
3089 return (rewind_error);
3091 /* Store the rewind info as part of the initial load info */
3092 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
3093 spa->spa_load_info);
3095 /* Restore the initial load info */
3096 fnvlist_free(spa->spa_load_info);
3097 spa->spa_load_info = loadinfo;
3099 return (load_error);
3106 * The import case is identical to an open except that the configuration is sent
3107 * down from userland, instead of grabbed from the configuration cache. For the
3108 * case of an open, the pool configuration will exist in the
3109 * POOL_STATE_UNINITIALIZED state.
3111 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3112 * the same time open the pool, without having to keep around the spa_t in some
3116 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
3120 spa_load_state_t state = SPA_LOAD_OPEN;
3122 int locked = B_FALSE;
3123 int firstopen = B_FALSE;
3128 * As disgusting as this is, we need to support recursive calls to this
3129 * function because dsl_dir_open() is called during spa_load(), and ends
3130 * up calling spa_open() again. The real fix is to figure out how to
3131 * avoid dsl_dir_open() calling this in the first place.
3133 if (mutex_owner(&spa_namespace_lock) != curthread) {
3134 mutex_enter(&spa_namespace_lock);
3138 if ((spa = spa_lookup(pool)) == NULL) {
3140 mutex_exit(&spa_namespace_lock);
3141 return (SET_ERROR(ENOENT));
3144 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
3145 zpool_rewind_policy_t policy;
3149 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
3151 if (policy.zrp_request & ZPOOL_DO_REWIND)
3152 state = SPA_LOAD_RECOVER;
3154 spa_activate(spa, spa_mode_global);
3156 if (state != SPA_LOAD_RECOVER)
3157 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3159 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3160 policy.zrp_request);
3162 if (error == EBADF) {
3164 * If vdev_validate() returns failure (indicated by
3165 * EBADF), it indicates that one of the vdevs indicates
3166 * that the pool has been exported or destroyed. If
3167 * this is the case, the config cache is out of sync and
3168 * we should remove the pool from the namespace.
3171 spa_deactivate(spa);
3172 spa_config_sync(spa, B_TRUE, B_TRUE);
3175 mutex_exit(&spa_namespace_lock);
3176 return (SET_ERROR(ENOENT));
3181 * We can't open the pool, but we still have useful
3182 * information: the state of each vdev after the
3183 * attempted vdev_open(). Return this to the user.
3185 if (config != NULL && spa->spa_config) {
3186 VERIFY(nvlist_dup(spa->spa_config, config,
3188 VERIFY(nvlist_add_nvlist(*config,
3189 ZPOOL_CONFIG_LOAD_INFO,
3190 spa->spa_load_info) == 0);
3193 spa_deactivate(spa);
3194 spa->spa_last_open_failed = error;
3196 mutex_exit(&spa_namespace_lock);
3202 spa_open_ref(spa, tag);
3205 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3208 * If we've recovered the pool, pass back any information we
3209 * gathered while doing the load.
3211 if (state == SPA_LOAD_RECOVER) {
3212 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3213 spa->spa_load_info) == 0);
3217 spa->spa_last_open_failed = 0;
3218 spa->spa_last_ubsync_txg = 0;
3219 spa->spa_load_txg = 0;
3220 mutex_exit(&spa_namespace_lock);
3224 zvol_create_minors(spa->spa_name);
3235 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3238 return (spa_open_common(name, spapp, tag, policy, config));
3242 spa_open(const char *name, spa_t **spapp, void *tag)
3244 return (spa_open_common(name, spapp, tag, NULL, NULL));
3248 * Lookup the given spa_t, incrementing the inject count in the process,
3249 * preventing it from being exported or destroyed.
3252 spa_inject_addref(char *name)
3256 mutex_enter(&spa_namespace_lock);
3257 if ((spa = spa_lookup(name)) == NULL) {
3258 mutex_exit(&spa_namespace_lock);
3261 spa->spa_inject_ref++;
3262 mutex_exit(&spa_namespace_lock);
3268 spa_inject_delref(spa_t *spa)
3270 mutex_enter(&spa_namespace_lock);
3271 spa->spa_inject_ref--;
3272 mutex_exit(&spa_namespace_lock);
3276 * Add spares device information to the nvlist.
3279 spa_add_spares(spa_t *spa, nvlist_t *config)
3289 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3291 if (spa->spa_spares.sav_count == 0)
3294 VERIFY(nvlist_lookup_nvlist(config,
3295 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3296 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3297 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3299 VERIFY(nvlist_add_nvlist_array(nvroot,
3300 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3301 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3302 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3305 * Go through and find any spares which have since been
3306 * repurposed as an active spare. If this is the case, update
3307 * their status appropriately.
3309 for (i = 0; i < nspares; i++) {
3310 VERIFY(nvlist_lookup_uint64(spares[i],
3311 ZPOOL_CONFIG_GUID, &guid) == 0);
3312 if (spa_spare_exists(guid, &pool, NULL) &&
3314 VERIFY(nvlist_lookup_uint64_array(
3315 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3316 (uint64_t **)&vs, &vsc) == 0);
3317 vs->vs_state = VDEV_STATE_CANT_OPEN;
3318 vs->vs_aux = VDEV_AUX_SPARED;
3325 * Add l2cache device information to the nvlist, including vdev stats.
3328 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3331 uint_t i, j, nl2cache;
3338 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3340 if (spa->spa_l2cache.sav_count == 0)
3343 VERIFY(nvlist_lookup_nvlist(config,
3344 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3345 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3346 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3347 if (nl2cache != 0) {
3348 VERIFY(nvlist_add_nvlist_array(nvroot,
3349 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3350 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3351 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3354 * Update level 2 cache device stats.
3357 for (i = 0; i < nl2cache; i++) {
3358 VERIFY(nvlist_lookup_uint64(l2cache[i],
3359 ZPOOL_CONFIG_GUID, &guid) == 0);
3362 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3364 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3365 vd = spa->spa_l2cache.sav_vdevs[j];
3371 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3372 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3374 vdev_get_stats(vd, vs);
3380 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3386 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3387 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3389 /* We may be unable to read features if pool is suspended. */
3390 if (spa_suspended(spa))
3393 if (spa->spa_feat_for_read_obj != 0) {
3394 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3395 spa->spa_feat_for_read_obj);
3396 zap_cursor_retrieve(&zc, &za) == 0;
3397 zap_cursor_advance(&zc)) {
3398 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3399 za.za_num_integers == 1);
3400 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3401 za.za_first_integer));
3403 zap_cursor_fini(&zc);
3406 if (spa->spa_feat_for_write_obj != 0) {
3407 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3408 spa->spa_feat_for_write_obj);
3409 zap_cursor_retrieve(&zc, &za) == 0;
3410 zap_cursor_advance(&zc)) {
3411 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3412 za.za_num_integers == 1);
3413 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3414 za.za_first_integer));
3416 zap_cursor_fini(&zc);
3420 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3422 nvlist_free(features);
3426 spa_get_stats(const char *name, nvlist_t **config,
3427 char *altroot, size_t buflen)
3433 error = spa_open_common(name, &spa, FTAG, NULL, config);
3437 * This still leaves a window of inconsistency where the spares
3438 * or l2cache devices could change and the config would be
3439 * self-inconsistent.
3441 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3443 if (*config != NULL) {
3444 uint64_t loadtimes[2];
3446 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3447 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3448 VERIFY(nvlist_add_uint64_array(*config,
3449 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3451 VERIFY(nvlist_add_uint64(*config,
3452 ZPOOL_CONFIG_ERRCOUNT,
3453 spa_get_errlog_size(spa)) == 0);
3455 if (spa_suspended(spa))
3456 VERIFY(nvlist_add_uint64(*config,
3457 ZPOOL_CONFIG_SUSPENDED,
3458 spa->spa_failmode) == 0);
3460 spa_add_spares(spa, *config);
3461 spa_add_l2cache(spa, *config);
3462 spa_add_feature_stats(spa, *config);
3467 * We want to get the alternate root even for faulted pools, so we cheat
3468 * and call spa_lookup() directly.
3472 mutex_enter(&spa_namespace_lock);
3473 spa = spa_lookup(name);
3475 spa_altroot(spa, altroot, buflen);
3479 mutex_exit(&spa_namespace_lock);
3481 spa_altroot(spa, altroot, buflen);
3486 spa_config_exit(spa, SCL_CONFIG, FTAG);
3487 spa_close(spa, FTAG);
3494 * Validate that the auxiliary device array is well formed. We must have an
3495 * array of nvlists, each which describes a valid leaf vdev. If this is an
3496 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3497 * specified, as long as they are well-formed.
3500 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3501 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3502 vdev_labeltype_t label)
3509 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3512 * It's acceptable to have no devs specified.
3514 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3518 return (SET_ERROR(EINVAL));
3521 * Make sure the pool is formatted with a version that supports this
3524 if (spa_version(spa) < version)
3525 return (SET_ERROR(ENOTSUP));
3528 * Set the pending device list so we correctly handle device in-use
3531 sav->sav_pending = dev;
3532 sav->sav_npending = ndev;
3534 for (i = 0; i < ndev; i++) {
3535 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3539 if (!vd->vdev_ops->vdev_op_leaf) {
3541 error = SET_ERROR(EINVAL);
3546 * The L2ARC currently only supports disk devices in
3547 * kernel context. For user-level testing, we allow it.
3550 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3551 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3552 error = SET_ERROR(ENOTBLK);
3559 if ((error = vdev_open(vd)) == 0 &&
3560 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3561 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3562 vd->vdev_guid) == 0);
3568 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3575 sav->sav_pending = NULL;
3576 sav->sav_npending = 0;
3581 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3585 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3587 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3588 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3589 VDEV_LABEL_SPARE)) != 0) {
3593 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3594 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3595 VDEV_LABEL_L2CACHE));
3599 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3604 if (sav->sav_config != NULL) {
3610 * Generate new dev list by concatentating with the
3613 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3614 &olddevs, &oldndevs) == 0);
3616 newdevs = kmem_alloc(sizeof (void *) *
3617 (ndevs + oldndevs), KM_SLEEP);
3618 for (i = 0; i < oldndevs; i++)
3619 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3621 for (i = 0; i < ndevs; i++)
3622 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3625 VERIFY(nvlist_remove(sav->sav_config, config,
3626 DATA_TYPE_NVLIST_ARRAY) == 0);
3628 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3629 config, newdevs, ndevs + oldndevs) == 0);
3630 for (i = 0; i < oldndevs + ndevs; i++)
3631 nvlist_free(newdevs[i]);
3632 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3635 * Generate a new dev list.
3637 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3639 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3645 * Stop and drop level 2 ARC devices
3648 spa_l2cache_drop(spa_t *spa)
3652 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3654 for (i = 0; i < sav->sav_count; i++) {
3657 vd = sav->sav_vdevs[i];
3660 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3661 pool != 0ULL && l2arc_vdev_present(vd))
3662 l2arc_remove_vdev(vd);
3670 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3674 char *altroot = NULL;
3679 uint64_t txg = TXG_INITIAL;
3680 nvlist_t **spares, **l2cache;
3681 uint_t nspares, nl2cache;
3682 uint64_t version, obj;
3683 boolean_t has_features;
3686 * If this pool already exists, return failure.
3688 mutex_enter(&spa_namespace_lock);
3689 if (spa_lookup(pool) != NULL) {
3690 mutex_exit(&spa_namespace_lock);
3691 return (SET_ERROR(EEXIST));
3695 * Allocate a new spa_t structure.
3697 (void) nvlist_lookup_string(props,
3698 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3699 spa = spa_add(pool, NULL, altroot);
3700 spa_activate(spa, spa_mode_global);
3702 if (props && (error = spa_prop_validate(spa, props))) {
3703 spa_deactivate(spa);
3705 mutex_exit(&spa_namespace_lock);
3709 has_features = B_FALSE;
3710 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3711 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3712 if (zpool_prop_feature(nvpair_name(elem)))
3713 has_features = B_TRUE;
3716 if (has_features || nvlist_lookup_uint64(props,
3717 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3718 version = SPA_VERSION;
3720 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3722 spa->spa_first_txg = txg;
3723 spa->spa_uberblock.ub_txg = txg - 1;
3724 spa->spa_uberblock.ub_version = version;
3725 spa->spa_ubsync = spa->spa_uberblock;
3728 * Create "The Godfather" zio to hold all async IOs
3730 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3732 for (int i = 0; i < max_ncpus; i++) {
3733 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3734 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3735 ZIO_FLAG_GODFATHER);
3739 * Create the root vdev.
3741 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3743 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3745 ASSERT(error != 0 || rvd != NULL);
3746 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3748 if (error == 0 && !zfs_allocatable_devs(nvroot))
3749 error = SET_ERROR(EINVAL);
3752 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3753 (error = spa_validate_aux(spa, nvroot, txg,
3754 VDEV_ALLOC_ADD)) == 0) {
3755 for (int c = 0; c < rvd->vdev_children; c++) {
3756 vdev_ashift_optimize(rvd->vdev_child[c]);
3757 vdev_metaslab_set_size(rvd->vdev_child[c]);
3758 vdev_expand(rvd->vdev_child[c], txg);
3762 spa_config_exit(spa, SCL_ALL, FTAG);
3766 spa_deactivate(spa);
3768 mutex_exit(&spa_namespace_lock);
3773 * Get the list of spares, if specified.
3775 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3776 &spares, &nspares) == 0) {
3777 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3779 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3780 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3781 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3782 spa_load_spares(spa);
3783 spa_config_exit(spa, SCL_ALL, FTAG);
3784 spa->spa_spares.sav_sync = B_TRUE;
3788 * Get the list of level 2 cache devices, if specified.
3790 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3791 &l2cache, &nl2cache) == 0) {
3792 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3793 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3794 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3795 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3796 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3797 spa_load_l2cache(spa);
3798 spa_config_exit(spa, SCL_ALL, FTAG);
3799 spa->spa_l2cache.sav_sync = B_TRUE;
3802 spa->spa_is_initializing = B_TRUE;
3803 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3804 spa->spa_meta_objset = dp->dp_meta_objset;
3805 spa->spa_is_initializing = B_FALSE;
3808 * Create DDTs (dedup tables).
3812 spa_update_dspace(spa);
3814 tx = dmu_tx_create_assigned(dp, txg);
3817 * Create the pool config object.
3819 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3820 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3821 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3823 if (zap_add(spa->spa_meta_objset,
3824 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3825 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3826 cmn_err(CE_PANIC, "failed to add pool config");
3829 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3830 spa_feature_create_zap_objects(spa, tx);
3832 if (zap_add(spa->spa_meta_objset,
3833 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3834 sizeof (uint64_t), 1, &version, tx) != 0) {
3835 cmn_err(CE_PANIC, "failed to add pool version");
3838 /* Newly created pools with the right version are always deflated. */
3839 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3840 spa->spa_deflate = TRUE;
3841 if (zap_add(spa->spa_meta_objset,
3842 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3843 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3844 cmn_err(CE_PANIC, "failed to add deflate");
3849 * Create the deferred-free bpobj. Turn off compression
3850 * because sync-to-convergence takes longer if the blocksize
3853 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3854 dmu_object_set_compress(spa->spa_meta_objset, obj,
3855 ZIO_COMPRESS_OFF, tx);
3856 if (zap_add(spa->spa_meta_objset,
3857 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3858 sizeof (uint64_t), 1, &obj, tx) != 0) {
3859 cmn_err(CE_PANIC, "failed to add bpobj");
3861 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3862 spa->spa_meta_objset, obj));
3865 * Create the pool's history object.
3867 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3868 spa_history_create_obj(spa, tx);
3871 * Generate some random noise for salted checksums to operate on.
3873 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
3874 sizeof (spa->spa_cksum_salt.zcs_bytes));
3877 * Set pool properties.
3879 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3880 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3881 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3882 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3884 if (props != NULL) {
3885 spa_configfile_set(spa, props, B_FALSE);
3886 spa_sync_props(props, tx);
3891 spa->spa_sync_on = B_TRUE;
3892 txg_sync_start(spa->spa_dsl_pool);
3895 * We explicitly wait for the first transaction to complete so that our
3896 * bean counters are appropriately updated.
3898 txg_wait_synced(spa->spa_dsl_pool, txg);
3900 spa_config_sync(spa, B_FALSE, B_TRUE);
3901 spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE);
3903 spa_history_log_version(spa, "create");
3906 * Don't count references from objsets that are already closed
3907 * and are making their way through the eviction process.
3909 spa_evicting_os_wait(spa);
3910 spa->spa_minref = refcount_count(&spa->spa_refcount);
3912 mutex_exit(&spa_namespace_lock);
3920 * Get the root pool information from the root disk, then import the root pool
3921 * during the system boot up time.
3923 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3926 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3929 nvlist_t *nvtop, *nvroot;
3932 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3936 * Add this top-level vdev to the child array.
3938 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3940 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3942 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3945 * Put this pool's top-level vdevs into a root vdev.
3947 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3948 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3949 VDEV_TYPE_ROOT) == 0);
3950 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3951 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3952 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3956 * Replace the existing vdev_tree with the new root vdev in
3957 * this pool's configuration (remove the old, add the new).
3959 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3960 nvlist_free(nvroot);
3965 * Walk the vdev tree and see if we can find a device with "better"
3966 * configuration. A configuration is "better" if the label on that
3967 * device has a more recent txg.
3970 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3972 for (int c = 0; c < vd->vdev_children; c++)
3973 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3975 if (vd->vdev_ops->vdev_op_leaf) {
3979 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3983 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3987 * Do we have a better boot device?
3989 if (label_txg > *txg) {
3998 * Import a root pool.
4000 * For x86. devpath_list will consist of devid and/or physpath name of
4001 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
4002 * The GRUB "findroot" command will return the vdev we should boot.
4004 * For Sparc, devpath_list consists the physpath name of the booting device
4005 * no matter the rootpool is a single device pool or a mirrored pool.
4007 * "/pci@1f,0/ide@d/disk@0,0:a"
4010 spa_import_rootpool(char *devpath, char *devid)
4013 vdev_t *rvd, *bvd, *avd = NULL;
4014 nvlist_t *config, *nvtop;
4020 * Read the label from the boot device and generate a configuration.
4022 config = spa_generate_rootconf(devpath, devid, &guid);
4023 #if defined(_OBP) && defined(_KERNEL)
4024 if (config == NULL) {
4025 if (strstr(devpath, "/iscsi/ssd") != NULL) {
4027 get_iscsi_bootpath_phy(devpath);
4028 config = spa_generate_rootconf(devpath, devid, &guid);
4032 if (config == NULL) {
4033 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
4035 return (SET_ERROR(EIO));
4038 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4040 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
4042 mutex_enter(&spa_namespace_lock);
4043 if ((spa = spa_lookup(pname)) != NULL) {
4045 * Remove the existing root pool from the namespace so that we
4046 * can replace it with the correct config we just read in.
4051 spa = spa_add(pname, config, NULL);
4052 spa->spa_is_root = B_TRUE;
4053 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4056 * Build up a vdev tree based on the boot device's label config.
4058 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4060 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4061 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4062 VDEV_ALLOC_ROOTPOOL);
4063 spa_config_exit(spa, SCL_ALL, FTAG);
4065 mutex_exit(&spa_namespace_lock);
4066 nvlist_free(config);
4067 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4073 * Get the boot vdev.
4075 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
4076 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
4077 (u_longlong_t)guid);
4078 error = SET_ERROR(ENOENT);
4083 * Determine if there is a better boot device.
4086 spa_alt_rootvdev(rvd, &avd, &txg);
4088 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
4089 "try booting from '%s'", avd->vdev_path);
4090 error = SET_ERROR(EINVAL);
4095 * If the boot device is part of a spare vdev then ensure that
4096 * we're booting off the active spare.
4098 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4099 !bvd->vdev_isspare) {
4100 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
4101 "try booting from '%s'",
4103 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
4104 error = SET_ERROR(EINVAL);
4110 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4112 spa_config_exit(spa, SCL_ALL, FTAG);
4113 mutex_exit(&spa_namespace_lock);
4115 nvlist_free(config);
4119 #else /* !illumos */
4121 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
4125 spa_generate_rootconf(const char *name)
4127 nvlist_t **configs, **tops;
4129 nvlist_t *best_cfg, *nvtop, *nvroot;
4138 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
4141 ASSERT3U(count, !=, 0);
4143 for (i = 0; i < count; i++) {
4146 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
4148 if (txg > best_txg) {
4150 best_cfg = configs[i];
4155 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
4157 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
4160 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
4161 for (i = 0; i < nchildren; i++) {
4164 if (configs[i] == NULL)
4166 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
4168 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
4170 for (i = 0; holes != NULL && i < nholes; i++) {
4173 if (tops[holes[i]] != NULL)
4175 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
4176 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
4177 VDEV_TYPE_HOLE) == 0);
4178 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
4180 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4183 for (i = 0; i < nchildren; i++) {
4184 if (tops[i] != NULL)
4186 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4187 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4188 VDEV_TYPE_MISSING) == 0);
4189 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4191 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4196 * Create pool config based on the best vdev config.
4198 nvlist_dup(best_cfg, &config, KM_SLEEP);
4201 * Put this pool's top-level vdevs into a root vdev.
4203 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4205 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4206 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4207 VDEV_TYPE_ROOT) == 0);
4208 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4209 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4210 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4211 tops, nchildren) == 0);
4214 * Replace the existing vdev_tree with the new root vdev in
4215 * this pool's configuration (remove the old, add the new).
4217 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4220 * Drop vdev config elements that should not be present at pool level.
4222 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4223 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4225 for (i = 0; i < count; i++)
4226 nvlist_free(configs[i]);
4227 kmem_free(configs, count * sizeof(void *));
4228 for (i = 0; i < nchildren; i++)
4229 nvlist_free(tops[i]);
4230 kmem_free(tops, nchildren * sizeof(void *));
4231 nvlist_free(nvroot);
4236 spa_import_rootpool(const char *name)
4239 vdev_t *rvd, *bvd, *avd = NULL;
4240 nvlist_t *config, *nvtop;
4246 * Read the label from the boot device and generate a configuration.
4248 config = spa_generate_rootconf(name);
4250 mutex_enter(&spa_namespace_lock);
4251 if (config != NULL) {
4252 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4253 &pname) == 0 && strcmp(name, pname) == 0);
4254 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4257 if ((spa = spa_lookup(pname)) != NULL) {
4259 * Remove the existing root pool from the namespace so
4260 * that we can replace it with the correct config
4265 spa = spa_add(pname, config, NULL);
4268 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4269 * via spa_version().
4271 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4272 &spa->spa_ubsync.ub_version) != 0)
4273 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4274 } else if ((spa = spa_lookup(name)) == NULL) {
4275 mutex_exit(&spa_namespace_lock);
4276 nvlist_free(config);
4277 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4281 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4283 spa->spa_is_root = B_TRUE;
4284 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4287 * Build up a vdev tree based on the boot device's label config.
4289 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4291 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4292 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4293 VDEV_ALLOC_ROOTPOOL);
4294 spa_config_exit(spa, SCL_ALL, FTAG);
4296 mutex_exit(&spa_namespace_lock);
4297 nvlist_free(config);
4298 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4303 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4305 spa_config_exit(spa, SCL_ALL, FTAG);
4306 mutex_exit(&spa_namespace_lock);
4308 nvlist_free(config);
4312 #endif /* illumos */
4313 #endif /* _KERNEL */
4316 * Import a non-root pool into the system.
4319 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4322 char *altroot = NULL;
4323 spa_load_state_t state = SPA_LOAD_IMPORT;
4324 zpool_rewind_policy_t policy;
4325 uint64_t mode = spa_mode_global;
4326 uint64_t readonly = B_FALSE;
4329 nvlist_t **spares, **l2cache;
4330 uint_t nspares, nl2cache;
4333 * If a pool with this name exists, return failure.
4335 mutex_enter(&spa_namespace_lock);
4336 if (spa_lookup(pool) != NULL) {
4337 mutex_exit(&spa_namespace_lock);
4338 return (SET_ERROR(EEXIST));
4342 * Create and initialize the spa structure.
4344 (void) nvlist_lookup_string(props,
4345 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4346 (void) nvlist_lookup_uint64(props,
4347 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4350 spa = spa_add(pool, config, altroot);
4351 spa->spa_import_flags = flags;
4354 * Verbatim import - Take a pool and insert it into the namespace
4355 * as if it had been loaded at boot.
4357 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4359 spa_configfile_set(spa, props, B_FALSE);
4361 spa_config_sync(spa, B_FALSE, B_TRUE);
4362 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4364 mutex_exit(&spa_namespace_lock);
4368 spa_activate(spa, mode);
4371 * Don't start async tasks until we know everything is healthy.
4373 spa_async_suspend(spa);
4375 zpool_get_rewind_policy(config, &policy);
4376 if (policy.zrp_request & ZPOOL_DO_REWIND)
4377 state = SPA_LOAD_RECOVER;
4380 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4381 * because the user-supplied config is actually the one to trust when
4384 if (state != SPA_LOAD_RECOVER)
4385 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4387 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4388 policy.zrp_request);
4391 * Propagate anything learned while loading the pool and pass it
4392 * back to caller (i.e. rewind info, missing devices, etc).
4394 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4395 spa->spa_load_info) == 0);
4397 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4399 * Toss any existing sparelist, as it doesn't have any validity
4400 * anymore, and conflicts with spa_has_spare().
4402 if (spa->spa_spares.sav_config) {
4403 nvlist_free(spa->spa_spares.sav_config);
4404 spa->spa_spares.sav_config = NULL;
4405 spa_load_spares(spa);
4407 if (spa->spa_l2cache.sav_config) {
4408 nvlist_free(spa->spa_l2cache.sav_config);
4409 spa->spa_l2cache.sav_config = NULL;
4410 spa_load_l2cache(spa);
4413 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4416 error = spa_validate_aux(spa, nvroot, -1ULL,
4419 error = spa_validate_aux(spa, nvroot, -1ULL,
4420 VDEV_ALLOC_L2CACHE);
4421 spa_config_exit(spa, SCL_ALL, FTAG);
4424 spa_configfile_set(spa, props, B_FALSE);
4426 if (error != 0 || (props && spa_writeable(spa) &&
4427 (error = spa_prop_set(spa, props)))) {
4429 spa_deactivate(spa);
4431 mutex_exit(&spa_namespace_lock);
4435 spa_async_resume(spa);
4438 * Override any spares and level 2 cache devices as specified by
4439 * the user, as these may have correct device names/devids, etc.
4441 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4442 &spares, &nspares) == 0) {
4443 if (spa->spa_spares.sav_config)
4444 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4445 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4447 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4448 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4449 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4450 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4451 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4452 spa_load_spares(spa);
4453 spa_config_exit(spa, SCL_ALL, FTAG);
4454 spa->spa_spares.sav_sync = B_TRUE;
4456 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4457 &l2cache, &nl2cache) == 0) {
4458 if (spa->spa_l2cache.sav_config)
4459 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4460 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4462 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4463 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4464 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4465 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4466 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4467 spa_load_l2cache(spa);
4468 spa_config_exit(spa, SCL_ALL, FTAG);
4469 spa->spa_l2cache.sav_sync = B_TRUE;
4473 * Check for any removed devices.
4475 if (spa->spa_autoreplace) {
4476 spa_aux_check_removed(&spa->spa_spares);
4477 spa_aux_check_removed(&spa->spa_l2cache);
4480 if (spa_writeable(spa)) {
4482 * Update the config cache to include the newly-imported pool.
4484 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4488 * It's possible that the pool was expanded while it was exported.
4489 * We kick off an async task to handle this for us.
4491 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4493 spa_history_log_version(spa, "import");
4495 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4497 mutex_exit(&spa_namespace_lock);
4501 zvol_create_minors(pool);
4508 spa_tryimport(nvlist_t *tryconfig)
4510 nvlist_t *config = NULL;
4516 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4519 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4523 * Create and initialize the spa structure.
4525 mutex_enter(&spa_namespace_lock);
4526 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4527 spa_activate(spa, FREAD);
4530 * Pass off the heavy lifting to spa_load().
4531 * Pass TRUE for mosconfig because the user-supplied config
4532 * is actually the one to trust when doing an import.
4534 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4537 * If 'tryconfig' was at least parsable, return the current config.
4539 if (spa->spa_root_vdev != NULL) {
4540 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4541 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4543 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4545 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4546 spa->spa_uberblock.ub_timestamp) == 0);
4547 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4548 spa->spa_load_info) == 0);
4551 * If the bootfs property exists on this pool then we
4552 * copy it out so that external consumers can tell which
4553 * pools are bootable.
4555 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4556 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4559 * We have to play games with the name since the
4560 * pool was opened as TRYIMPORT_NAME.
4562 if (dsl_dsobj_to_dsname(spa_name(spa),
4563 spa->spa_bootfs, tmpname) == 0) {
4565 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4567 cp = strchr(tmpname, '/');
4569 (void) strlcpy(dsname, tmpname,
4572 (void) snprintf(dsname, MAXPATHLEN,
4573 "%s/%s", poolname, ++cp);
4575 VERIFY(nvlist_add_string(config,
4576 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4577 kmem_free(dsname, MAXPATHLEN);
4579 kmem_free(tmpname, MAXPATHLEN);
4583 * Add the list of hot spares and level 2 cache devices.
4585 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4586 spa_add_spares(spa, config);
4587 spa_add_l2cache(spa, config);
4588 spa_config_exit(spa, SCL_CONFIG, FTAG);
4592 spa_deactivate(spa);
4594 mutex_exit(&spa_namespace_lock);
4600 * Pool export/destroy
4602 * The act of destroying or exporting a pool is very simple. We make sure there
4603 * is no more pending I/O and any references to the pool are gone. Then, we
4604 * update the pool state and sync all the labels to disk, removing the
4605 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4606 * we don't sync the labels or remove the configuration cache.
4609 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4610 boolean_t force, boolean_t hardforce)
4617 if (!(spa_mode_global & FWRITE))
4618 return (SET_ERROR(EROFS));
4620 mutex_enter(&spa_namespace_lock);
4621 if ((spa = spa_lookup(pool)) == NULL) {
4622 mutex_exit(&spa_namespace_lock);
4623 return (SET_ERROR(ENOENT));
4627 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4628 * reacquire the namespace lock, and see if we can export.
4630 spa_open_ref(spa, FTAG);
4631 mutex_exit(&spa_namespace_lock);
4632 spa_async_suspend(spa);
4633 mutex_enter(&spa_namespace_lock);
4634 spa_close(spa, FTAG);
4637 * The pool will be in core if it's openable,
4638 * in which case we can modify its state.
4640 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4642 * Objsets may be open only because they're dirty, so we
4643 * have to force it to sync before checking spa_refcnt.
4645 txg_wait_synced(spa->spa_dsl_pool, 0);
4646 spa_evicting_os_wait(spa);
4649 * A pool cannot be exported or destroyed if there are active
4650 * references. If we are resetting a pool, allow references by
4651 * fault injection handlers.
4653 if (!spa_refcount_zero(spa) ||
4654 (spa->spa_inject_ref != 0 &&
4655 new_state != POOL_STATE_UNINITIALIZED)) {
4656 spa_async_resume(spa);
4657 mutex_exit(&spa_namespace_lock);
4658 return (SET_ERROR(EBUSY));
4662 * A pool cannot be exported if it has an active shared spare.
4663 * This is to prevent other pools stealing the active spare
4664 * from an exported pool. At user's own will, such pool can
4665 * be forcedly exported.
4667 if (!force && new_state == POOL_STATE_EXPORTED &&
4668 spa_has_active_shared_spare(spa)) {
4669 spa_async_resume(spa);
4670 mutex_exit(&spa_namespace_lock);
4671 return (SET_ERROR(EXDEV));
4675 * We want this to be reflected on every label,
4676 * so mark them all dirty. spa_unload() will do the
4677 * final sync that pushes these changes out.
4679 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4680 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4681 spa->spa_state = new_state;
4682 spa->spa_final_txg = spa_last_synced_txg(spa) +
4684 vdev_config_dirty(spa->spa_root_vdev);
4685 spa_config_exit(spa, SCL_ALL, FTAG);
4689 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4691 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4693 spa_deactivate(spa);
4696 if (oldconfig && spa->spa_config)
4697 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4699 if (new_state != POOL_STATE_UNINITIALIZED) {
4701 spa_config_sync(spa, B_TRUE, B_TRUE);
4704 mutex_exit(&spa_namespace_lock);
4710 * Destroy a storage pool.
4713 spa_destroy(char *pool)
4715 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4720 * Export a storage pool.
4723 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4724 boolean_t hardforce)
4726 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4731 * Similar to spa_export(), this unloads the spa_t without actually removing it
4732 * from the namespace in any way.
4735 spa_reset(char *pool)
4737 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4742 * ==========================================================================
4743 * Device manipulation
4744 * ==========================================================================
4748 * Add a device to a storage pool.
4751 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4755 vdev_t *rvd = spa->spa_root_vdev;
4757 nvlist_t **spares, **l2cache;
4758 uint_t nspares, nl2cache;
4760 ASSERT(spa_writeable(spa));
4762 txg = spa_vdev_enter(spa);
4764 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4765 VDEV_ALLOC_ADD)) != 0)
4766 return (spa_vdev_exit(spa, NULL, txg, error));
4768 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4770 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4774 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4778 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4779 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4781 if (vd->vdev_children != 0 &&
4782 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4783 return (spa_vdev_exit(spa, vd, txg, error));
4786 * We must validate the spares and l2cache devices after checking the
4787 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4789 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4790 return (spa_vdev_exit(spa, vd, txg, error));
4793 * Transfer each new top-level vdev from vd to rvd.
4795 for (int c = 0; c < vd->vdev_children; c++) {
4798 * Set the vdev id to the first hole, if one exists.
4800 for (id = 0; id < rvd->vdev_children; id++) {
4801 if (rvd->vdev_child[id]->vdev_ishole) {
4802 vdev_free(rvd->vdev_child[id]);
4806 tvd = vd->vdev_child[c];
4807 vdev_remove_child(vd, tvd);
4809 vdev_add_child(rvd, tvd);
4810 vdev_config_dirty(tvd);
4814 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4815 ZPOOL_CONFIG_SPARES);
4816 spa_load_spares(spa);
4817 spa->spa_spares.sav_sync = B_TRUE;
4820 if (nl2cache != 0) {
4821 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4822 ZPOOL_CONFIG_L2CACHE);
4823 spa_load_l2cache(spa);
4824 spa->spa_l2cache.sav_sync = B_TRUE;
4828 * We have to be careful when adding new vdevs to an existing pool.
4829 * If other threads start allocating from these vdevs before we
4830 * sync the config cache, and we lose power, then upon reboot we may
4831 * fail to open the pool because there are DVAs that the config cache
4832 * can't translate. Therefore, we first add the vdevs without
4833 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4834 * and then let spa_config_update() initialize the new metaslabs.
4836 * spa_load() checks for added-but-not-initialized vdevs, so that
4837 * if we lose power at any point in this sequence, the remaining
4838 * steps will be completed the next time we load the pool.
4840 (void) spa_vdev_exit(spa, vd, txg, 0);
4842 mutex_enter(&spa_namespace_lock);
4843 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4844 spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD);
4845 mutex_exit(&spa_namespace_lock);
4851 * Attach a device to a mirror. The arguments are the path to any device
4852 * in the mirror, and the nvroot for the new device. If the path specifies
4853 * a device that is not mirrored, we automatically insert the mirror vdev.
4855 * If 'replacing' is specified, the new device is intended to replace the
4856 * existing device; in this case the two devices are made into their own
4857 * mirror using the 'replacing' vdev, which is functionally identical to
4858 * the mirror vdev (it actually reuses all the same ops) but has a few
4859 * extra rules: you can't attach to it after it's been created, and upon
4860 * completion of resilvering, the first disk (the one being replaced)
4861 * is automatically detached.
4864 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4866 uint64_t txg, dtl_max_txg;
4867 vdev_t *rvd = spa->spa_root_vdev;
4868 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4870 char *oldvdpath, *newvdpath;
4874 ASSERT(spa_writeable(spa));
4876 txg = spa_vdev_enter(spa);
4878 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4881 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4883 if (!oldvd->vdev_ops->vdev_op_leaf)
4884 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4886 pvd = oldvd->vdev_parent;
4888 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4889 VDEV_ALLOC_ATTACH)) != 0)
4890 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4892 if (newrootvd->vdev_children != 1)
4893 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4895 newvd = newrootvd->vdev_child[0];
4897 if (!newvd->vdev_ops->vdev_op_leaf)
4898 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4900 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4901 return (spa_vdev_exit(spa, newrootvd, txg, error));
4904 * Spares can't replace logs
4906 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4907 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4911 * For attach, the only allowable parent is a mirror or the root
4914 if (pvd->vdev_ops != &vdev_mirror_ops &&
4915 pvd->vdev_ops != &vdev_root_ops)
4916 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4918 pvops = &vdev_mirror_ops;
4921 * Active hot spares can only be replaced by inactive hot
4924 if (pvd->vdev_ops == &vdev_spare_ops &&
4925 oldvd->vdev_isspare &&
4926 !spa_has_spare(spa, newvd->vdev_guid))
4927 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4930 * If the source is a hot spare, and the parent isn't already a
4931 * spare, then we want to create a new hot spare. Otherwise, we
4932 * want to create a replacing vdev. The user is not allowed to
4933 * attach to a spared vdev child unless the 'isspare' state is
4934 * the same (spare replaces spare, non-spare replaces
4937 if (pvd->vdev_ops == &vdev_replacing_ops &&
4938 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4939 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4940 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4941 newvd->vdev_isspare != oldvd->vdev_isspare) {
4942 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4945 if (newvd->vdev_isspare)
4946 pvops = &vdev_spare_ops;
4948 pvops = &vdev_replacing_ops;
4952 * Make sure the new device is big enough.
4954 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4955 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4958 * The new device cannot have a higher alignment requirement
4959 * than the top-level vdev.
4961 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4962 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4965 * If this is an in-place replacement, update oldvd's path and devid
4966 * to make it distinguishable from newvd, and unopenable from now on.
4968 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4969 spa_strfree(oldvd->vdev_path);
4970 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4972 (void) sprintf(oldvd->vdev_path, "%s/%s",
4973 newvd->vdev_path, "old");
4974 if (oldvd->vdev_devid != NULL) {
4975 spa_strfree(oldvd->vdev_devid);
4976 oldvd->vdev_devid = NULL;
4980 /* mark the device being resilvered */
4981 newvd->vdev_resilver_txg = txg;
4984 * If the parent is not a mirror, or if we're replacing, insert the new
4985 * mirror/replacing/spare vdev above oldvd.
4987 if (pvd->vdev_ops != pvops)
4988 pvd = vdev_add_parent(oldvd, pvops);
4990 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4991 ASSERT(pvd->vdev_ops == pvops);
4992 ASSERT(oldvd->vdev_parent == pvd);
4995 * Extract the new device from its root and add it to pvd.
4997 vdev_remove_child(newrootvd, newvd);
4998 newvd->vdev_id = pvd->vdev_children;
4999 newvd->vdev_crtxg = oldvd->vdev_crtxg;
5000 vdev_add_child(pvd, newvd);
5002 tvd = newvd->vdev_top;
5003 ASSERT(pvd->vdev_top == tvd);
5004 ASSERT(tvd->vdev_parent == rvd);
5006 vdev_config_dirty(tvd);
5009 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
5010 * for any dmu_sync-ed blocks. It will propagate upward when
5011 * spa_vdev_exit() calls vdev_dtl_reassess().
5013 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
5015 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
5016 dtl_max_txg - TXG_INITIAL);
5018 if (newvd->vdev_isspare) {
5019 spa_spare_activate(newvd);
5020 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
5023 oldvdpath = spa_strdup(oldvd->vdev_path);
5024 newvdpath = spa_strdup(newvd->vdev_path);
5025 newvd_isspare = newvd->vdev_isspare;
5028 * Mark newvd's DTL dirty in this txg.
5030 vdev_dirty(tvd, VDD_DTL, newvd, txg);
5033 * Schedule the resilver to restart in the future. We do this to
5034 * ensure that dmu_sync-ed blocks have been stitched into the
5035 * respective datasets.
5037 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
5039 if (spa->spa_bootfs)
5040 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
5042 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH);
5047 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
5049 spa_history_log_internal(spa, "vdev attach", NULL,
5050 "%s vdev=%s %s vdev=%s",
5051 replacing && newvd_isspare ? "spare in" :
5052 replacing ? "replace" : "attach", newvdpath,
5053 replacing ? "for" : "to", oldvdpath);
5055 spa_strfree(oldvdpath);
5056 spa_strfree(newvdpath);
5062 * Detach a device from a mirror or replacing vdev.
5064 * If 'replace_done' is specified, only detach if the parent
5065 * is a replacing vdev.
5068 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
5072 vdev_t *rvd = spa->spa_root_vdev;
5073 vdev_t *vd, *pvd, *cvd, *tvd;
5074 boolean_t unspare = B_FALSE;
5075 uint64_t unspare_guid = 0;
5078 ASSERT(spa_writeable(spa));
5080 txg = spa_vdev_enter(spa);
5082 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5085 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
5087 if (!vd->vdev_ops->vdev_op_leaf)
5088 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5090 pvd = vd->vdev_parent;
5093 * If the parent/child relationship is not as expected, don't do it.
5094 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
5095 * vdev that's replacing B with C. The user's intent in replacing
5096 * is to go from M(A,B) to M(A,C). If the user decides to cancel
5097 * the replace by detaching C, the expected behavior is to end up
5098 * M(A,B). But suppose that right after deciding to detach C,
5099 * the replacement of B completes. We would have M(A,C), and then
5100 * ask to detach C, which would leave us with just A -- not what
5101 * the user wanted. To prevent this, we make sure that the
5102 * parent/child relationship hasn't changed -- in this example,
5103 * that C's parent is still the replacing vdev R.
5105 if (pvd->vdev_guid != pguid && pguid != 0)
5106 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5109 * Only 'replacing' or 'spare' vdevs can be replaced.
5111 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
5112 pvd->vdev_ops != &vdev_spare_ops)
5113 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5115 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
5116 spa_version(spa) >= SPA_VERSION_SPARES);
5119 * Only mirror, replacing, and spare vdevs support detach.
5121 if (pvd->vdev_ops != &vdev_replacing_ops &&
5122 pvd->vdev_ops != &vdev_mirror_ops &&
5123 pvd->vdev_ops != &vdev_spare_ops)
5124 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5127 * If this device has the only valid copy of some data,
5128 * we cannot safely detach it.
5130 if (vdev_dtl_required(vd))
5131 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5133 ASSERT(pvd->vdev_children >= 2);
5136 * If we are detaching the second disk from a replacing vdev, then
5137 * check to see if we changed the original vdev's path to have "/old"
5138 * at the end in spa_vdev_attach(). If so, undo that change now.
5140 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
5141 vd->vdev_path != NULL) {
5142 size_t len = strlen(vd->vdev_path);
5144 for (int c = 0; c < pvd->vdev_children; c++) {
5145 cvd = pvd->vdev_child[c];
5147 if (cvd == vd || cvd->vdev_path == NULL)
5150 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
5151 strcmp(cvd->vdev_path + len, "/old") == 0) {
5152 spa_strfree(cvd->vdev_path);
5153 cvd->vdev_path = spa_strdup(vd->vdev_path);
5160 * If we are detaching the original disk from a spare, then it implies
5161 * that the spare should become a real disk, and be removed from the
5162 * active spare list for the pool.
5164 if (pvd->vdev_ops == &vdev_spare_ops &&
5166 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
5170 * Erase the disk labels so the disk can be used for other things.
5171 * This must be done after all other error cases are handled,
5172 * but before we disembowel vd (so we can still do I/O to it).
5173 * But if we can't do it, don't treat the error as fatal --
5174 * it may be that the unwritability of the disk is the reason
5175 * it's being detached!
5177 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5180 * Remove vd from its parent and compact the parent's children.
5182 vdev_remove_child(pvd, vd);
5183 vdev_compact_children(pvd);
5186 * Remember one of the remaining children so we can get tvd below.
5188 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5191 * If we need to remove the remaining child from the list of hot spares,
5192 * do it now, marking the vdev as no longer a spare in the process.
5193 * We must do this before vdev_remove_parent(), because that can
5194 * change the GUID if it creates a new toplevel GUID. For a similar
5195 * reason, we must remove the spare now, in the same txg as the detach;
5196 * otherwise someone could attach a new sibling, change the GUID, and
5197 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5200 ASSERT(cvd->vdev_isspare);
5201 spa_spare_remove(cvd);
5202 unspare_guid = cvd->vdev_guid;
5203 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5204 cvd->vdev_unspare = B_TRUE;
5208 * If the parent mirror/replacing vdev only has one child,
5209 * the parent is no longer needed. Remove it from the tree.
5211 if (pvd->vdev_children == 1) {
5212 if (pvd->vdev_ops == &vdev_spare_ops)
5213 cvd->vdev_unspare = B_FALSE;
5214 vdev_remove_parent(cvd);
5219 * We don't set tvd until now because the parent we just removed
5220 * may have been the previous top-level vdev.
5222 tvd = cvd->vdev_top;
5223 ASSERT(tvd->vdev_parent == rvd);
5226 * Reevaluate the parent vdev state.
5228 vdev_propagate_state(cvd);
5231 * If the 'autoexpand' property is set on the pool then automatically
5232 * try to expand the size of the pool. For example if the device we
5233 * just detached was smaller than the others, it may be possible to
5234 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5235 * first so that we can obtain the updated sizes of the leaf vdevs.
5237 if (spa->spa_autoexpand) {
5239 vdev_expand(tvd, txg);
5242 vdev_config_dirty(tvd);
5245 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5246 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5247 * But first make sure we're not on any *other* txg's DTL list, to
5248 * prevent vd from being accessed after it's freed.
5250 vdpath = spa_strdup(vd->vdev_path);
5251 for (int t = 0; t < TXG_SIZE; t++)
5252 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5253 vd->vdev_detached = B_TRUE;
5254 vdev_dirty(tvd, VDD_DTL, vd, txg);
5256 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5258 /* hang on to the spa before we release the lock */
5259 spa_open_ref(spa, FTAG);
5261 error = spa_vdev_exit(spa, vd, txg, 0);
5263 spa_history_log_internal(spa, "detach", NULL,
5265 spa_strfree(vdpath);
5268 * If this was the removal of the original device in a hot spare vdev,
5269 * then we want to go through and remove the device from the hot spare
5270 * list of every other pool.
5273 spa_t *altspa = NULL;
5275 mutex_enter(&spa_namespace_lock);
5276 while ((altspa = spa_next(altspa)) != NULL) {
5277 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5281 spa_open_ref(altspa, FTAG);
5282 mutex_exit(&spa_namespace_lock);
5283 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5284 mutex_enter(&spa_namespace_lock);
5285 spa_close(altspa, FTAG);
5287 mutex_exit(&spa_namespace_lock);
5289 /* search the rest of the vdevs for spares to remove */
5290 spa_vdev_resilver_done(spa);
5293 /* all done with the spa; OK to release */
5294 mutex_enter(&spa_namespace_lock);
5295 spa_close(spa, FTAG);
5296 mutex_exit(&spa_namespace_lock);
5302 * Split a set of devices from their mirrors, and create a new pool from them.
5305 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5306 nvlist_t *props, boolean_t exp)
5309 uint64_t txg, *glist;
5311 uint_t c, children, lastlog;
5312 nvlist_t **child, *nvl, *tmp;
5314 char *altroot = NULL;
5315 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5316 boolean_t activate_slog;
5318 ASSERT(spa_writeable(spa));
5320 txg = spa_vdev_enter(spa);
5322 /* clear the log and flush everything up to now */
5323 activate_slog = spa_passivate_log(spa);
5324 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5325 error = spa_offline_log(spa);
5326 txg = spa_vdev_config_enter(spa);
5329 spa_activate_log(spa);
5332 return (spa_vdev_exit(spa, NULL, txg, error));
5334 /* check new spa name before going any further */
5335 if (spa_lookup(newname) != NULL)
5336 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5339 * scan through all the children to ensure they're all mirrors
5341 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5342 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5344 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5346 /* first, check to ensure we've got the right child count */
5347 rvd = spa->spa_root_vdev;
5349 for (c = 0; c < rvd->vdev_children; c++) {
5350 vdev_t *vd = rvd->vdev_child[c];
5352 /* don't count the holes & logs as children */
5353 if (vd->vdev_islog || vd->vdev_ishole) {
5361 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5362 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5364 /* next, ensure no spare or cache devices are part of the split */
5365 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5366 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5367 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5369 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5370 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5372 /* then, loop over each vdev and validate it */
5373 for (c = 0; c < children; c++) {
5374 uint64_t is_hole = 0;
5376 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5380 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5381 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5384 error = SET_ERROR(EINVAL);
5389 /* which disk is going to be split? */
5390 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5392 error = SET_ERROR(EINVAL);
5396 /* look it up in the spa */
5397 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5398 if (vml[c] == NULL) {
5399 error = SET_ERROR(ENODEV);
5403 /* make sure there's nothing stopping the split */
5404 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5405 vml[c]->vdev_islog ||
5406 vml[c]->vdev_ishole ||
5407 vml[c]->vdev_isspare ||
5408 vml[c]->vdev_isl2cache ||
5409 !vdev_writeable(vml[c]) ||
5410 vml[c]->vdev_children != 0 ||
5411 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5412 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5413 error = SET_ERROR(EINVAL);
5417 if (vdev_dtl_required(vml[c])) {
5418 error = SET_ERROR(EBUSY);
5422 /* we need certain info from the top level */
5423 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5424 vml[c]->vdev_top->vdev_ms_array) == 0);
5425 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5426 vml[c]->vdev_top->vdev_ms_shift) == 0);
5427 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5428 vml[c]->vdev_top->vdev_asize) == 0);
5429 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5430 vml[c]->vdev_top->vdev_ashift) == 0);
5432 /* transfer per-vdev ZAPs */
5433 ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0);
5434 VERIFY0(nvlist_add_uint64(child[c],
5435 ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap));
5437 ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0);
5438 VERIFY0(nvlist_add_uint64(child[c],
5439 ZPOOL_CONFIG_VDEV_TOP_ZAP,
5440 vml[c]->vdev_parent->vdev_top_zap));
5444 kmem_free(vml, children * sizeof (vdev_t *));
5445 kmem_free(glist, children * sizeof (uint64_t));
5446 return (spa_vdev_exit(spa, NULL, txg, error));
5449 /* stop writers from using the disks */
5450 for (c = 0; c < children; c++) {
5452 vml[c]->vdev_offline = B_TRUE;
5454 vdev_reopen(spa->spa_root_vdev);
5457 * Temporarily record the splitting vdevs in the spa config. This
5458 * will disappear once the config is regenerated.
5460 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5461 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5462 glist, children) == 0);
5463 kmem_free(glist, children * sizeof (uint64_t));
5465 mutex_enter(&spa->spa_props_lock);
5466 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5468 mutex_exit(&spa->spa_props_lock);
5469 spa->spa_config_splitting = nvl;
5470 vdev_config_dirty(spa->spa_root_vdev);
5472 /* configure and create the new pool */
5473 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5474 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5475 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5476 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5477 spa_version(spa)) == 0);
5478 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5479 spa->spa_config_txg) == 0);
5480 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5481 spa_generate_guid(NULL)) == 0);
5482 VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
5483 (void) nvlist_lookup_string(props,
5484 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5486 /* add the new pool to the namespace */
5487 newspa = spa_add(newname, config, altroot);
5488 newspa->spa_avz_action = AVZ_ACTION_REBUILD;
5489 newspa->spa_config_txg = spa->spa_config_txg;
5490 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5492 /* release the spa config lock, retaining the namespace lock */
5493 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5495 if (zio_injection_enabled)
5496 zio_handle_panic_injection(spa, FTAG, 1);
5498 spa_activate(newspa, spa_mode_global);
5499 spa_async_suspend(newspa);
5502 /* mark that we are creating new spa by splitting */
5503 newspa->spa_splitting_newspa = B_TRUE;
5505 /* create the new pool from the disks of the original pool */
5506 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5508 newspa->spa_splitting_newspa = B_FALSE;
5513 /* if that worked, generate a real config for the new pool */
5514 if (newspa->spa_root_vdev != NULL) {
5515 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5516 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5517 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5518 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5519 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5524 if (props != NULL) {
5525 spa_configfile_set(newspa, props, B_FALSE);
5526 error = spa_prop_set(newspa, props);
5531 /* flush everything */
5532 txg = spa_vdev_config_enter(newspa);
5533 vdev_config_dirty(newspa->spa_root_vdev);
5534 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5536 if (zio_injection_enabled)
5537 zio_handle_panic_injection(spa, FTAG, 2);
5539 spa_async_resume(newspa);
5541 /* finally, update the original pool's config */
5542 txg = spa_vdev_config_enter(spa);
5543 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5544 error = dmu_tx_assign(tx, TXG_WAIT);
5547 for (c = 0; c < children; c++) {
5548 if (vml[c] != NULL) {
5551 spa_history_log_internal(spa, "detach", tx,
5552 "vdev=%s", vml[c]->vdev_path);
5557 spa->spa_avz_action = AVZ_ACTION_REBUILD;
5558 vdev_config_dirty(spa->spa_root_vdev);
5559 spa->spa_config_splitting = NULL;
5563 (void) spa_vdev_exit(spa, NULL, txg, 0);
5565 if (zio_injection_enabled)
5566 zio_handle_panic_injection(spa, FTAG, 3);
5568 /* split is complete; log a history record */
5569 spa_history_log_internal(newspa, "split", NULL,
5570 "from pool %s", spa_name(spa));
5572 kmem_free(vml, children * sizeof (vdev_t *));
5574 /* if we're not going to mount the filesystems in userland, export */
5576 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5583 spa_deactivate(newspa);
5586 txg = spa_vdev_config_enter(spa);
5588 /* re-online all offlined disks */
5589 for (c = 0; c < children; c++) {
5591 vml[c]->vdev_offline = B_FALSE;
5593 vdev_reopen(spa->spa_root_vdev);
5595 nvlist_free(spa->spa_config_splitting);
5596 spa->spa_config_splitting = NULL;
5597 (void) spa_vdev_exit(spa, NULL, txg, error);
5599 kmem_free(vml, children * sizeof (vdev_t *));
5604 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5606 for (int i = 0; i < count; i++) {
5609 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5612 if (guid == target_guid)
5620 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5621 nvlist_t *dev_to_remove)
5623 nvlist_t **newdev = NULL;
5626 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5628 for (int i = 0, j = 0; i < count; i++) {
5629 if (dev[i] == dev_to_remove)
5631 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5634 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5635 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5637 for (int i = 0; i < count - 1; i++)
5638 nvlist_free(newdev[i]);
5641 kmem_free(newdev, (count - 1) * sizeof (void *));
5645 * Evacuate the device.
5648 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5653 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5654 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5655 ASSERT(vd == vd->vdev_top);
5658 * Evacuate the device. We don't hold the config lock as writer
5659 * since we need to do I/O but we do keep the
5660 * spa_namespace_lock held. Once this completes the device
5661 * should no longer have any blocks allocated on it.
5663 if (vd->vdev_islog) {
5664 if (vd->vdev_stat.vs_alloc != 0)
5665 error = spa_offline_log(spa);
5667 error = SET_ERROR(ENOTSUP);
5674 * The evacuation succeeded. Remove any remaining MOS metadata
5675 * associated with this vdev, and wait for these changes to sync.
5677 ASSERT0(vd->vdev_stat.vs_alloc);
5678 txg = spa_vdev_config_enter(spa);
5679 vd->vdev_removing = B_TRUE;
5680 vdev_dirty_leaves(vd, VDD_DTL, txg);
5681 vdev_config_dirty(vd);
5682 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5688 * Complete the removal by cleaning up the namespace.
5691 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5693 vdev_t *rvd = spa->spa_root_vdev;
5694 uint64_t id = vd->vdev_id;
5695 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5697 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5698 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5699 ASSERT(vd == vd->vdev_top);
5702 * Only remove any devices which are empty.
5704 if (vd->vdev_stat.vs_alloc != 0)
5707 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5709 if (list_link_active(&vd->vdev_state_dirty_node))
5710 vdev_state_clean(vd);
5711 if (list_link_active(&vd->vdev_config_dirty_node))
5712 vdev_config_clean(vd);
5717 vdev_compact_children(rvd);
5719 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5720 vdev_add_child(rvd, vd);
5722 vdev_config_dirty(rvd);
5725 * Reassess the health of our root vdev.
5731 * Remove a device from the pool -
5733 * Removing a device from the vdev namespace requires several steps
5734 * and can take a significant amount of time. As a result we use
5735 * the spa_vdev_config_[enter/exit] functions which allow us to
5736 * grab and release the spa_config_lock while still holding the namespace
5737 * lock. During each step the configuration is synced out.
5739 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5743 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5746 metaslab_group_t *mg;
5747 nvlist_t **spares, **l2cache, *nv;
5749 uint_t nspares, nl2cache;
5751 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5753 ASSERT(spa_writeable(spa));
5756 txg = spa_vdev_enter(spa);
5758 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5760 if (spa->spa_spares.sav_vdevs != NULL &&
5761 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5762 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5763 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5765 * Only remove the hot spare if it's not currently in use
5768 if (vd == NULL || unspare) {
5769 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5770 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5771 spa_load_spares(spa);
5772 spa->spa_spares.sav_sync = B_TRUE;
5774 error = SET_ERROR(EBUSY);
5776 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5777 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5778 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5779 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5781 * Cache devices can always be removed.
5783 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5784 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5785 spa_load_l2cache(spa);
5786 spa->spa_l2cache.sav_sync = B_TRUE;
5787 } else if (vd != NULL && vd->vdev_islog) {
5789 ASSERT(vd == vd->vdev_top);
5794 * Stop allocating from this vdev.
5796 metaslab_group_passivate(mg);
5799 * Wait for the youngest allocations and frees to sync,
5800 * and then wait for the deferral of those frees to finish.
5802 spa_vdev_config_exit(spa, NULL,
5803 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5806 * Attempt to evacuate the vdev.
5808 error = spa_vdev_remove_evacuate(spa, vd);
5810 txg = spa_vdev_config_enter(spa);
5813 * If we couldn't evacuate the vdev, unwind.
5816 metaslab_group_activate(mg);
5817 return (spa_vdev_exit(spa, NULL, txg, error));
5821 * Clean up the vdev namespace.
5823 spa_vdev_remove_from_namespace(spa, vd);
5825 } else if (vd != NULL) {
5827 * Normal vdevs cannot be removed (yet).
5829 error = SET_ERROR(ENOTSUP);
5832 * There is no vdev of any kind with the specified guid.
5834 error = SET_ERROR(ENOENT);
5838 return (spa_vdev_exit(spa, NULL, txg, error));
5844 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5845 * currently spared, so we can detach it.
5848 spa_vdev_resilver_done_hunt(vdev_t *vd)
5850 vdev_t *newvd, *oldvd;
5852 for (int c = 0; c < vd->vdev_children; c++) {
5853 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5859 * Check for a completed replacement. We always consider the first
5860 * vdev in the list to be the oldest vdev, and the last one to be
5861 * the newest (see spa_vdev_attach() for how that works). In
5862 * the case where the newest vdev is faulted, we will not automatically
5863 * remove it after a resilver completes. This is OK as it will require
5864 * user intervention to determine which disk the admin wishes to keep.
5866 if (vd->vdev_ops == &vdev_replacing_ops) {
5867 ASSERT(vd->vdev_children > 1);
5869 newvd = vd->vdev_child[vd->vdev_children - 1];
5870 oldvd = vd->vdev_child[0];
5872 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5873 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5874 !vdev_dtl_required(oldvd))
5879 * Check for a completed resilver with the 'unspare' flag set.
5881 if (vd->vdev_ops == &vdev_spare_ops) {
5882 vdev_t *first = vd->vdev_child[0];
5883 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5885 if (last->vdev_unspare) {
5888 } else if (first->vdev_unspare) {
5895 if (oldvd != NULL &&
5896 vdev_dtl_empty(newvd, DTL_MISSING) &&
5897 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5898 !vdev_dtl_required(oldvd))
5902 * If there are more than two spares attached to a disk,
5903 * and those spares are not required, then we want to
5904 * attempt to free them up now so that they can be used
5905 * by other pools. Once we're back down to a single
5906 * disk+spare, we stop removing them.
5908 if (vd->vdev_children > 2) {
5909 newvd = vd->vdev_child[1];
5911 if (newvd->vdev_isspare && last->vdev_isspare &&
5912 vdev_dtl_empty(last, DTL_MISSING) &&
5913 vdev_dtl_empty(last, DTL_OUTAGE) &&
5914 !vdev_dtl_required(newvd))
5923 spa_vdev_resilver_done(spa_t *spa)
5925 vdev_t *vd, *pvd, *ppvd;
5926 uint64_t guid, sguid, pguid, ppguid;
5928 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5930 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5931 pvd = vd->vdev_parent;
5932 ppvd = pvd->vdev_parent;
5933 guid = vd->vdev_guid;
5934 pguid = pvd->vdev_guid;
5935 ppguid = ppvd->vdev_guid;
5938 * If we have just finished replacing a hot spared device, then
5939 * we need to detach the parent's first child (the original hot
5942 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5943 ppvd->vdev_children == 2) {
5944 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5945 sguid = ppvd->vdev_child[1]->vdev_guid;
5947 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5949 spa_config_exit(spa, SCL_ALL, FTAG);
5950 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5952 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5954 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5957 spa_config_exit(spa, SCL_ALL, FTAG);
5961 * Update the stored path or FRU for this vdev.
5964 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5968 boolean_t sync = B_FALSE;
5970 ASSERT(spa_writeable(spa));
5972 spa_vdev_state_enter(spa, SCL_ALL);
5974 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5975 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5977 if (!vd->vdev_ops->vdev_op_leaf)
5978 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5981 if (strcmp(value, vd->vdev_path) != 0) {
5982 spa_strfree(vd->vdev_path);
5983 vd->vdev_path = spa_strdup(value);
5987 if (vd->vdev_fru == NULL) {
5988 vd->vdev_fru = spa_strdup(value);
5990 } else if (strcmp(value, vd->vdev_fru) != 0) {
5991 spa_strfree(vd->vdev_fru);
5992 vd->vdev_fru = spa_strdup(value);
5997 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
6001 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
6003 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
6007 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
6009 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
6013 * ==========================================================================
6015 * ==========================================================================
6019 spa_scan_stop(spa_t *spa)
6021 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
6022 if (dsl_scan_resilvering(spa->spa_dsl_pool))
6023 return (SET_ERROR(EBUSY));
6024 return (dsl_scan_cancel(spa->spa_dsl_pool));
6028 spa_scan(spa_t *spa, pool_scan_func_t func)
6030 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
6032 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
6033 return (SET_ERROR(ENOTSUP));
6036 * If a resilver was requested, but there is no DTL on a
6037 * writeable leaf device, we have nothing to do.
6039 if (func == POOL_SCAN_RESILVER &&
6040 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
6041 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
6045 return (dsl_scan(spa->spa_dsl_pool, func));
6049 * ==========================================================================
6050 * SPA async task processing
6051 * ==========================================================================
6055 spa_async_remove(spa_t *spa, vdev_t *vd)
6057 if (vd->vdev_remove_wanted) {
6058 vd->vdev_remove_wanted = B_FALSE;
6059 vd->vdev_delayed_close = B_FALSE;
6060 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
6063 * We want to clear the stats, but we don't want to do a full
6064 * vdev_clear() as that will cause us to throw away
6065 * degraded/faulted state as well as attempt to reopen the
6066 * device, all of which is a waste.
6068 vd->vdev_stat.vs_read_errors = 0;
6069 vd->vdev_stat.vs_write_errors = 0;
6070 vd->vdev_stat.vs_checksum_errors = 0;
6072 vdev_state_dirty(vd->vdev_top);
6073 /* Tell userspace that the vdev is gone. */
6074 zfs_post_remove(spa, vd);
6077 for (int c = 0; c < vd->vdev_children; c++)
6078 spa_async_remove(spa, vd->vdev_child[c]);
6082 spa_async_probe(spa_t *spa, vdev_t *vd)
6084 if (vd->vdev_probe_wanted) {
6085 vd->vdev_probe_wanted = B_FALSE;
6086 vdev_reopen(vd); /* vdev_open() does the actual probe */
6089 for (int c = 0; c < vd->vdev_children; c++)
6090 spa_async_probe(spa, vd->vdev_child[c]);
6094 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
6100 if (!spa->spa_autoexpand)
6103 for (int c = 0; c < vd->vdev_children; c++) {
6104 vdev_t *cvd = vd->vdev_child[c];
6105 spa_async_autoexpand(spa, cvd);
6108 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
6111 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
6112 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
6114 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6115 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
6117 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
6118 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
6121 kmem_free(physpath, MAXPATHLEN);
6125 spa_async_thread(void *arg)
6130 ASSERT(spa->spa_sync_on);
6132 mutex_enter(&spa->spa_async_lock);
6133 tasks = spa->spa_async_tasks;
6134 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
6135 mutex_exit(&spa->spa_async_lock);
6138 * See if the config needs to be updated.
6140 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
6141 uint64_t old_space, new_space;
6143 mutex_enter(&spa_namespace_lock);
6144 old_space = metaslab_class_get_space(spa_normal_class(spa));
6145 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
6146 new_space = metaslab_class_get_space(spa_normal_class(spa));
6147 mutex_exit(&spa_namespace_lock);
6150 * If the pool grew as a result of the config update,
6151 * then log an internal history event.
6153 if (new_space != old_space) {
6154 spa_history_log_internal(spa, "vdev online", NULL,
6155 "pool '%s' size: %llu(+%llu)",
6156 spa_name(spa), new_space, new_space - old_space);
6160 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
6161 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6162 spa_async_autoexpand(spa, spa->spa_root_vdev);
6163 spa_config_exit(spa, SCL_CONFIG, FTAG);
6167 * See if any devices need to be probed.
6169 if (tasks & SPA_ASYNC_PROBE) {
6170 spa_vdev_state_enter(spa, SCL_NONE);
6171 spa_async_probe(spa, spa->spa_root_vdev);
6172 (void) spa_vdev_state_exit(spa, NULL, 0);
6176 * If any devices are done replacing, detach them.
6178 if (tasks & SPA_ASYNC_RESILVER_DONE)
6179 spa_vdev_resilver_done(spa);
6182 * Kick off a resilver.
6184 if (tasks & SPA_ASYNC_RESILVER)
6185 dsl_resilver_restart(spa->spa_dsl_pool, 0);
6188 * Let the world know that we're done.
6190 mutex_enter(&spa->spa_async_lock);
6191 spa->spa_async_thread = NULL;
6192 cv_broadcast(&spa->spa_async_cv);
6193 mutex_exit(&spa->spa_async_lock);
6198 spa_async_thread_vd(void *arg)
6203 ASSERT(spa->spa_sync_on);
6205 mutex_enter(&spa->spa_async_lock);
6206 tasks = spa->spa_async_tasks;
6208 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6209 mutex_exit(&spa->spa_async_lock);
6212 * See if any devices need to be marked REMOVED.
6214 if (tasks & SPA_ASYNC_REMOVE) {
6215 spa_vdev_state_enter(spa, SCL_NONE);
6216 spa_async_remove(spa, spa->spa_root_vdev);
6217 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6218 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6219 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6220 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6221 (void) spa_vdev_state_exit(spa, NULL, 0);
6225 * Let the world know that we're done.
6227 mutex_enter(&spa->spa_async_lock);
6228 tasks = spa->spa_async_tasks;
6229 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6231 spa->spa_async_thread_vd = NULL;
6232 cv_broadcast(&spa->spa_async_cv);
6233 mutex_exit(&spa->spa_async_lock);
6238 spa_async_suspend(spa_t *spa)
6240 mutex_enter(&spa->spa_async_lock);
6241 spa->spa_async_suspended++;
6242 while (spa->spa_async_thread != NULL &&
6243 spa->spa_async_thread_vd != NULL)
6244 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6245 mutex_exit(&spa->spa_async_lock);
6249 spa_async_resume(spa_t *spa)
6251 mutex_enter(&spa->spa_async_lock);
6252 ASSERT(spa->spa_async_suspended != 0);
6253 spa->spa_async_suspended--;
6254 mutex_exit(&spa->spa_async_lock);
6258 spa_async_tasks_pending(spa_t *spa)
6260 uint_t non_config_tasks;
6262 boolean_t config_task_suspended;
6264 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6266 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6267 if (spa->spa_ccw_fail_time == 0) {
6268 config_task_suspended = B_FALSE;
6270 config_task_suspended =
6271 (gethrtime() - spa->spa_ccw_fail_time) <
6272 (zfs_ccw_retry_interval * NANOSEC);
6275 return (non_config_tasks || (config_task && !config_task_suspended));
6279 spa_async_dispatch(spa_t *spa)
6281 mutex_enter(&spa->spa_async_lock);
6282 if (spa_async_tasks_pending(spa) &&
6283 !spa->spa_async_suspended &&
6284 spa->spa_async_thread == NULL &&
6286 spa->spa_async_thread = thread_create(NULL, 0,
6287 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6288 mutex_exit(&spa->spa_async_lock);
6292 spa_async_dispatch_vd(spa_t *spa)
6294 mutex_enter(&spa->spa_async_lock);
6295 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6296 !spa->spa_async_suspended &&
6297 spa->spa_async_thread_vd == NULL &&
6299 spa->spa_async_thread_vd = thread_create(NULL, 0,
6300 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6301 mutex_exit(&spa->spa_async_lock);
6305 spa_async_request(spa_t *spa, int task)
6307 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6308 mutex_enter(&spa->spa_async_lock);
6309 spa->spa_async_tasks |= task;
6310 mutex_exit(&spa->spa_async_lock);
6311 spa_async_dispatch_vd(spa);
6315 * ==========================================================================
6316 * SPA syncing routines
6317 * ==========================================================================
6321 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6324 bpobj_enqueue(bpo, bp, tx);
6329 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6333 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6334 BP_GET_PSIZE(bp), zio->io_flags));
6339 * Note: this simple function is not inlined to make it easier to dtrace the
6340 * amount of time spent syncing frees.
6343 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6345 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6346 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6347 VERIFY(zio_wait(zio) == 0);
6351 * Note: this simple function is not inlined to make it easier to dtrace the
6352 * amount of time spent syncing deferred frees.
6355 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6357 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6358 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6359 spa_free_sync_cb, zio, tx), ==, 0);
6360 VERIFY0(zio_wait(zio));
6365 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6367 char *packed = NULL;
6372 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6375 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6376 * information. This avoids the dmu_buf_will_dirty() path and
6377 * saves us a pre-read to get data we don't actually care about.
6379 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6380 packed = kmem_alloc(bufsize, KM_SLEEP);
6382 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6384 bzero(packed + nvsize, bufsize - nvsize);
6386 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6388 kmem_free(packed, bufsize);
6390 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6391 dmu_buf_will_dirty(db, tx);
6392 *(uint64_t *)db->db_data = nvsize;
6393 dmu_buf_rele(db, FTAG);
6397 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6398 const char *config, const char *entry)
6408 * Update the MOS nvlist describing the list of available devices.
6409 * spa_validate_aux() will have already made sure this nvlist is
6410 * valid and the vdevs are labeled appropriately.
6412 if (sav->sav_object == 0) {
6413 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6414 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6415 sizeof (uint64_t), tx);
6416 VERIFY(zap_update(spa->spa_meta_objset,
6417 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6418 &sav->sav_object, tx) == 0);
6421 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6422 if (sav->sav_count == 0) {
6423 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6425 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6426 for (i = 0; i < sav->sav_count; i++)
6427 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6428 B_FALSE, VDEV_CONFIG_L2CACHE);
6429 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6430 sav->sav_count) == 0);
6431 for (i = 0; i < sav->sav_count; i++)
6432 nvlist_free(list[i]);
6433 kmem_free(list, sav->sav_count * sizeof (void *));
6436 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6437 nvlist_free(nvroot);
6439 sav->sav_sync = B_FALSE;
6443 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6444 * The all-vdev ZAP must be empty.
6447 spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx)
6449 spa_t *spa = vd->vdev_spa;
6450 if (vd->vdev_top_zap != 0) {
6451 VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
6452 vd->vdev_top_zap, tx));
6454 if (vd->vdev_leaf_zap != 0) {
6455 VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
6456 vd->vdev_leaf_zap, tx));
6458 for (uint64_t i = 0; i < vd->vdev_children; i++) {
6459 spa_avz_build(vd->vdev_child[i], avz, tx);
6464 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6469 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6470 * its config may not be dirty but we still need to build per-vdev ZAPs.
6471 * Similarly, if the pool is being assembled (e.g. after a split), we
6472 * need to rebuild the AVZ although the config may not be dirty.
6474 if (list_is_empty(&spa->spa_config_dirty_list) &&
6475 spa->spa_avz_action == AVZ_ACTION_NONE)
6478 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6480 ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE ||
6481 spa->spa_all_vdev_zaps != 0);
6483 if (spa->spa_avz_action == AVZ_ACTION_REBUILD) {
6484 /* Make and build the new AVZ */
6485 uint64_t new_avz = zap_create(spa->spa_meta_objset,
6486 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
6487 spa_avz_build(spa->spa_root_vdev, new_avz, tx);
6489 /* Diff old AVZ with new one */
6493 for (zap_cursor_init(&zc, spa->spa_meta_objset,
6494 spa->spa_all_vdev_zaps);
6495 zap_cursor_retrieve(&zc, &za) == 0;
6496 zap_cursor_advance(&zc)) {
6497 uint64_t vdzap = za.za_first_integer;
6498 if (zap_lookup_int(spa->spa_meta_objset, new_avz,
6501 * ZAP is listed in old AVZ but not in new one;
6504 VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap,
6509 zap_cursor_fini(&zc);
6511 /* Destroy the old AVZ */
6512 VERIFY0(zap_destroy(spa->spa_meta_objset,
6513 spa->spa_all_vdev_zaps, tx));
6515 /* Replace the old AVZ in the dir obj with the new one */
6516 VERIFY0(zap_update(spa->spa_meta_objset,
6517 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP,
6518 sizeof (new_avz), 1, &new_avz, tx));
6520 spa->spa_all_vdev_zaps = new_avz;
6521 } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) {
6525 /* Walk through the AVZ and destroy all listed ZAPs */
6526 for (zap_cursor_init(&zc, spa->spa_meta_objset,
6527 spa->spa_all_vdev_zaps);
6528 zap_cursor_retrieve(&zc, &za) == 0;
6529 zap_cursor_advance(&zc)) {
6530 uint64_t zap = za.za_first_integer;
6531 VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx));
6534 zap_cursor_fini(&zc);
6536 /* Destroy and unlink the AVZ itself */
6537 VERIFY0(zap_destroy(spa->spa_meta_objset,
6538 spa->spa_all_vdev_zaps, tx));
6539 VERIFY0(zap_remove(spa->spa_meta_objset,
6540 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx));
6541 spa->spa_all_vdev_zaps = 0;
6544 if (spa->spa_all_vdev_zaps == 0) {
6545 spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset,
6546 DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
6547 DMU_POOL_VDEV_ZAP_MAP, tx);
6549 spa->spa_avz_action = AVZ_ACTION_NONE;
6551 /* Create ZAPs for vdevs that don't have them. */
6552 vdev_construct_zaps(spa->spa_root_vdev, tx);
6554 config = spa_config_generate(spa, spa->spa_root_vdev,
6555 dmu_tx_get_txg(tx), B_FALSE);
6558 * If we're upgrading the spa version then make sure that
6559 * the config object gets updated with the correct version.
6561 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6562 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6563 spa->spa_uberblock.ub_version);
6565 spa_config_exit(spa, SCL_STATE, FTAG);
6567 nvlist_free(spa->spa_config_syncing);
6568 spa->spa_config_syncing = config;
6570 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6574 spa_sync_version(void *arg, dmu_tx_t *tx)
6576 uint64_t *versionp = arg;
6577 uint64_t version = *versionp;
6578 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6581 * Setting the version is special cased when first creating the pool.
6583 ASSERT(tx->tx_txg != TXG_INITIAL);
6585 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6586 ASSERT(version >= spa_version(spa));
6588 spa->spa_uberblock.ub_version = version;
6589 vdev_config_dirty(spa->spa_root_vdev);
6590 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6594 * Set zpool properties.
6597 spa_sync_props(void *arg, dmu_tx_t *tx)
6599 nvlist_t *nvp = arg;
6600 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6601 objset_t *mos = spa->spa_meta_objset;
6602 nvpair_t *elem = NULL;
6604 mutex_enter(&spa->spa_props_lock);
6606 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6608 char *strval, *fname;
6610 const char *propname;
6611 zprop_type_t proptype;
6614 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6617 * We checked this earlier in spa_prop_validate().
6619 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6621 fname = strchr(nvpair_name(elem), '@') + 1;
6622 VERIFY0(zfeature_lookup_name(fname, &fid));
6624 spa_feature_enable(spa, fid, tx);
6625 spa_history_log_internal(spa, "set", tx,
6626 "%s=enabled", nvpair_name(elem));
6629 case ZPOOL_PROP_VERSION:
6630 intval = fnvpair_value_uint64(elem);
6632 * The version is synced seperatly before other
6633 * properties and should be correct by now.
6635 ASSERT3U(spa_version(spa), >=, intval);
6638 case ZPOOL_PROP_ALTROOT:
6640 * 'altroot' is a non-persistent property. It should
6641 * have been set temporarily at creation or import time.
6643 ASSERT(spa->spa_root != NULL);
6646 case ZPOOL_PROP_READONLY:
6647 case ZPOOL_PROP_CACHEFILE:
6649 * 'readonly' and 'cachefile' are also non-persisitent
6653 case ZPOOL_PROP_COMMENT:
6654 strval = fnvpair_value_string(elem);
6655 if (spa->spa_comment != NULL)
6656 spa_strfree(spa->spa_comment);
6657 spa->spa_comment = spa_strdup(strval);
6659 * We need to dirty the configuration on all the vdevs
6660 * so that their labels get updated. It's unnecessary
6661 * to do this for pool creation since the vdev's
6662 * configuratoin has already been dirtied.
6664 if (tx->tx_txg != TXG_INITIAL)
6665 vdev_config_dirty(spa->spa_root_vdev);
6666 spa_history_log_internal(spa, "set", tx,
6667 "%s=%s", nvpair_name(elem), strval);
6671 * Set pool property values in the poolprops mos object.
6673 if (spa->spa_pool_props_object == 0) {
6674 spa->spa_pool_props_object =
6675 zap_create_link(mos, DMU_OT_POOL_PROPS,
6676 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6680 /* normalize the property name */
6681 propname = zpool_prop_to_name(prop);
6682 proptype = zpool_prop_get_type(prop);
6684 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6685 ASSERT(proptype == PROP_TYPE_STRING);
6686 strval = fnvpair_value_string(elem);
6687 VERIFY0(zap_update(mos,
6688 spa->spa_pool_props_object, propname,
6689 1, strlen(strval) + 1, strval, tx));
6690 spa_history_log_internal(spa, "set", tx,
6691 "%s=%s", nvpair_name(elem), strval);
6692 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6693 intval = fnvpair_value_uint64(elem);
6695 if (proptype == PROP_TYPE_INDEX) {
6697 VERIFY0(zpool_prop_index_to_string(
6698 prop, intval, &unused));
6700 VERIFY0(zap_update(mos,
6701 spa->spa_pool_props_object, propname,
6702 8, 1, &intval, tx));
6703 spa_history_log_internal(spa, "set", tx,
6704 "%s=%lld", nvpair_name(elem), intval);
6706 ASSERT(0); /* not allowed */
6710 case ZPOOL_PROP_DELEGATION:
6711 spa->spa_delegation = intval;
6713 case ZPOOL_PROP_BOOTFS:
6714 spa->spa_bootfs = intval;
6716 case ZPOOL_PROP_FAILUREMODE:
6717 spa->spa_failmode = intval;
6719 case ZPOOL_PROP_AUTOEXPAND:
6720 spa->spa_autoexpand = intval;
6721 if (tx->tx_txg != TXG_INITIAL)
6722 spa_async_request(spa,
6723 SPA_ASYNC_AUTOEXPAND);
6725 case ZPOOL_PROP_DEDUPDITTO:
6726 spa->spa_dedup_ditto = intval;
6735 mutex_exit(&spa->spa_props_lock);
6739 * Perform one-time upgrade on-disk changes. spa_version() does not
6740 * reflect the new version this txg, so there must be no changes this
6741 * txg to anything that the upgrade code depends on after it executes.
6742 * Therefore this must be called after dsl_pool_sync() does the sync
6746 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6748 dsl_pool_t *dp = spa->spa_dsl_pool;
6750 ASSERT(spa->spa_sync_pass == 1);
6752 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6754 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6755 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6756 dsl_pool_create_origin(dp, tx);
6758 /* Keeping the origin open increases spa_minref */
6759 spa->spa_minref += 3;
6762 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6763 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6764 dsl_pool_upgrade_clones(dp, tx);
6767 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6768 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6769 dsl_pool_upgrade_dir_clones(dp, tx);
6771 /* Keeping the freedir open increases spa_minref */
6772 spa->spa_minref += 3;
6775 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6776 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6777 spa_feature_create_zap_objects(spa, tx);
6781 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6782 * when possibility to use lz4 compression for metadata was added
6783 * Old pools that have this feature enabled must be upgraded to have
6784 * this feature active
6786 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6787 boolean_t lz4_en = spa_feature_is_enabled(spa,
6788 SPA_FEATURE_LZ4_COMPRESS);
6789 boolean_t lz4_ac = spa_feature_is_active(spa,
6790 SPA_FEATURE_LZ4_COMPRESS);
6792 if (lz4_en && !lz4_ac)
6793 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6797 * If we haven't written the salt, do so now. Note that the
6798 * feature may not be activated yet, but that's fine since
6799 * the presence of this ZAP entry is backwards compatible.
6801 if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
6802 DMU_POOL_CHECKSUM_SALT) == ENOENT) {
6803 VERIFY0(zap_add(spa->spa_meta_objset,
6804 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
6805 sizeof (spa->spa_cksum_salt.zcs_bytes),
6806 spa->spa_cksum_salt.zcs_bytes, tx));
6809 rrw_exit(&dp->dp_config_rwlock, FTAG);
6813 * Sync the specified transaction group. New blocks may be dirtied as
6814 * part of the process, so we iterate until it converges.
6817 spa_sync(spa_t *spa, uint64_t txg)
6819 dsl_pool_t *dp = spa->spa_dsl_pool;
6820 objset_t *mos = spa->spa_meta_objset;
6821 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6822 vdev_t *rvd = spa->spa_root_vdev;
6827 VERIFY(spa_writeable(spa));
6830 * Lock out configuration changes.
6832 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6834 spa->spa_syncing_txg = txg;
6835 spa->spa_sync_pass = 0;
6838 * If there are any pending vdev state changes, convert them
6839 * into config changes that go out with this transaction group.
6841 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6842 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6844 * We need the write lock here because, for aux vdevs,
6845 * calling vdev_config_dirty() modifies sav_config.
6846 * This is ugly and will become unnecessary when we
6847 * eliminate the aux vdev wart by integrating all vdevs
6848 * into the root vdev tree.
6850 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6851 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6852 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6853 vdev_state_clean(vd);
6854 vdev_config_dirty(vd);
6856 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6857 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6859 spa_config_exit(spa, SCL_STATE, FTAG);
6861 tx = dmu_tx_create_assigned(dp, txg);
6863 spa->spa_sync_starttime = gethrtime();
6865 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6866 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6867 #else /* !illumos */
6869 callout_reset(&spa->spa_deadman_cycid,
6870 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6872 #endif /* illumos */
6875 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6876 * set spa_deflate if we have no raid-z vdevs.
6878 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6879 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6882 for (i = 0; i < rvd->vdev_children; i++) {
6883 vd = rvd->vdev_child[i];
6884 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6887 if (i == rvd->vdev_children) {
6888 spa->spa_deflate = TRUE;
6889 VERIFY(0 == zap_add(spa->spa_meta_objset,
6890 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6891 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6896 * Iterate to convergence.
6899 int pass = ++spa->spa_sync_pass;
6901 spa_sync_config_object(spa, tx);
6902 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6903 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6904 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6905 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6906 spa_errlog_sync(spa, txg);
6907 dsl_pool_sync(dp, txg);
6909 if (pass < zfs_sync_pass_deferred_free) {
6910 spa_sync_frees(spa, free_bpl, tx);
6913 * We can not defer frees in pass 1, because
6914 * we sync the deferred frees later in pass 1.
6916 ASSERT3U(pass, >, 1);
6917 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6918 &spa->spa_deferred_bpobj, tx);
6922 dsl_scan_sync(dp, tx);
6924 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6928 spa_sync_upgrades(spa, tx);
6930 spa->spa_uberblock.ub_rootbp.blk_birth);
6932 * Note: We need to check if the MOS is dirty
6933 * because we could have marked the MOS dirty
6934 * without updating the uberblock (e.g. if we
6935 * have sync tasks but no dirty user data). We
6936 * need to check the uberblock's rootbp because
6937 * it is updated if we have synced out dirty
6938 * data (though in this case the MOS will most
6939 * likely also be dirty due to second order
6940 * effects, we don't want to rely on that here).
6942 if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6943 !dmu_objset_is_dirty(mos, txg)) {
6945 * Nothing changed on the first pass,
6946 * therefore this TXG is a no-op. Avoid
6947 * syncing deferred frees, so that we
6948 * can keep this TXG as a no-op.
6950 ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6952 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6953 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
6956 spa_sync_deferred_frees(spa, tx);
6959 } while (dmu_objset_is_dirty(mos, txg));
6961 if (!list_is_empty(&spa->spa_config_dirty_list)) {
6963 * Make sure that the number of ZAPs for all the vdevs matches
6964 * the number of ZAPs in the per-vdev ZAP list. This only gets
6965 * called if the config is dirty; otherwise there may be
6966 * outstanding AVZ operations that weren't completed in
6967 * spa_sync_config_object.
6969 uint64_t all_vdev_zap_entry_count;
6970 ASSERT0(zap_count(spa->spa_meta_objset,
6971 spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count));
6972 ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==,
6973 all_vdev_zap_entry_count);
6977 * Rewrite the vdev configuration (which includes the uberblock)
6978 * to commit the transaction group.
6980 * If there are no dirty vdevs, we sync the uberblock to a few
6981 * random top-level vdevs that are known to be visible in the
6982 * config cache (see spa_vdev_add() for a complete description).
6983 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6987 * We hold SCL_STATE to prevent vdev open/close/etc.
6988 * while we're attempting to write the vdev labels.
6990 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6992 if (list_is_empty(&spa->spa_config_dirty_list)) {
6993 vdev_t *svd[SPA_DVAS_PER_BP];
6995 int children = rvd->vdev_children;
6996 int c0 = spa_get_random(children);
6998 for (int c = 0; c < children; c++) {
6999 vd = rvd->vdev_child[(c0 + c) % children];
7000 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
7002 svd[svdcount++] = vd;
7003 if (svdcount == SPA_DVAS_PER_BP)
7006 error = vdev_config_sync(svd, svdcount, txg);
7008 error = vdev_config_sync(rvd->vdev_child,
7009 rvd->vdev_children, txg);
7013 spa->spa_last_synced_guid = rvd->vdev_guid;
7015 spa_config_exit(spa, SCL_STATE, FTAG);
7019 zio_suspend(spa, NULL);
7020 zio_resume_wait(spa);
7025 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
7026 #else /* !illumos */
7028 callout_drain(&spa->spa_deadman_cycid);
7030 #endif /* illumos */
7033 * Clear the dirty config list.
7035 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
7036 vdev_config_clean(vd);
7039 * Now that the new config has synced transactionally,
7040 * let it become visible to the config cache.
7042 if (spa->spa_config_syncing != NULL) {
7043 spa_config_set(spa, spa->spa_config_syncing);
7044 spa->spa_config_txg = txg;
7045 spa->spa_config_syncing = NULL;
7048 spa->spa_ubsync = spa->spa_uberblock;
7050 dsl_pool_sync_done(dp, txg);
7053 * Update usable space statistics.
7055 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
7056 vdev_sync_done(vd, txg);
7058 spa_update_dspace(spa);
7061 * It had better be the case that we didn't dirty anything
7062 * since vdev_config_sync().
7064 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
7065 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
7066 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
7068 spa->spa_sync_pass = 0;
7070 spa_config_exit(spa, SCL_CONFIG, FTAG);
7072 spa_handle_ignored_writes(spa);
7075 * If any async tasks have been requested, kick them off.
7077 spa_async_dispatch(spa);
7078 spa_async_dispatch_vd(spa);
7082 * Sync all pools. We don't want to hold the namespace lock across these
7083 * operations, so we take a reference on the spa_t and drop the lock during the
7087 spa_sync_allpools(void)
7090 mutex_enter(&spa_namespace_lock);
7091 while ((spa = spa_next(spa)) != NULL) {
7092 if (spa_state(spa) != POOL_STATE_ACTIVE ||
7093 !spa_writeable(spa) || spa_suspended(spa))
7095 spa_open_ref(spa, FTAG);
7096 mutex_exit(&spa_namespace_lock);
7097 txg_wait_synced(spa_get_dsl(spa), 0);
7098 mutex_enter(&spa_namespace_lock);
7099 spa_close(spa, FTAG);
7101 mutex_exit(&spa_namespace_lock);
7105 * ==========================================================================
7106 * Miscellaneous routines
7107 * ==========================================================================
7111 * Remove all pools in the system.
7119 * Remove all cached state. All pools should be closed now,
7120 * so every spa in the AVL tree should be unreferenced.
7122 mutex_enter(&spa_namespace_lock);
7123 while ((spa = spa_next(NULL)) != NULL) {
7125 * Stop async tasks. The async thread may need to detach
7126 * a device that's been replaced, which requires grabbing
7127 * spa_namespace_lock, so we must drop it here.
7129 spa_open_ref(spa, FTAG);
7130 mutex_exit(&spa_namespace_lock);
7131 spa_async_suspend(spa);
7132 mutex_enter(&spa_namespace_lock);
7133 spa_close(spa, FTAG);
7135 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
7137 spa_deactivate(spa);
7141 mutex_exit(&spa_namespace_lock);
7145 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
7150 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
7154 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
7155 vd = spa->spa_l2cache.sav_vdevs[i];
7156 if (vd->vdev_guid == guid)
7160 for (i = 0; i < spa->spa_spares.sav_count; i++) {
7161 vd = spa->spa_spares.sav_vdevs[i];
7162 if (vd->vdev_guid == guid)
7171 spa_upgrade(spa_t *spa, uint64_t version)
7173 ASSERT(spa_writeable(spa));
7175 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
7178 * This should only be called for a non-faulted pool, and since a
7179 * future version would result in an unopenable pool, this shouldn't be
7182 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
7183 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
7185 spa->spa_uberblock.ub_version = version;
7186 vdev_config_dirty(spa->spa_root_vdev);
7188 spa_config_exit(spa, SCL_ALL, FTAG);
7190 txg_wait_synced(spa_get_dsl(spa), 0);
7194 spa_has_spare(spa_t *spa, uint64_t guid)
7198 spa_aux_vdev_t *sav = &spa->spa_spares;
7200 for (i = 0; i < sav->sav_count; i++)
7201 if (sav->sav_vdevs[i]->vdev_guid == guid)
7204 for (i = 0; i < sav->sav_npending; i++) {
7205 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
7206 &spareguid) == 0 && spareguid == guid)
7214 * Check if a pool has an active shared spare device.
7215 * Note: reference count of an active spare is 2, as a spare and as a replace
7218 spa_has_active_shared_spare(spa_t *spa)
7222 spa_aux_vdev_t *sav = &spa->spa_spares;
7224 for (i = 0; i < sav->sav_count; i++) {
7225 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
7226 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
7235 * Post a sysevent corresponding to the given event. The 'name' must be one of
7236 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
7237 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7238 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7239 * or zdb as real changes.
7242 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
7246 sysevent_attr_list_t *attr = NULL;
7247 sysevent_value_t value;
7250 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
7253 value.value_type = SE_DATA_TYPE_STRING;
7254 value.value.sv_string = spa_name(spa);
7255 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
7258 value.value_type = SE_DATA_TYPE_UINT64;
7259 value.value.sv_uint64 = spa_guid(spa);
7260 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
7264 value.value_type = SE_DATA_TYPE_UINT64;
7265 value.value.sv_uint64 = vd->vdev_guid;
7266 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
7270 if (vd->vdev_path) {
7271 value.value_type = SE_DATA_TYPE_STRING;
7272 value.value.sv_string = vd->vdev_path;
7273 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
7274 &value, SE_SLEEP) != 0)
7279 if (sysevent_attach_attributes(ev, attr) != 0)
7283 (void) log_sysevent(ev, SE_SLEEP, &eid);
7287 sysevent_free_attr(attr);