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.
32 * SPA: Storage Pool Allocator
34 * This file contains all the routines used when modifying on-disk SPA state.
35 * This includes opening, importing, destroying, exporting a pool, and syncing a
39 #include <sys/zfs_context.h>
40 #include <sys/fm/fs/zfs.h>
41 #include <sys/spa_impl.h>
43 #include <sys/zio_checksum.h>
45 #include <sys/dmu_tx.h>
49 #include <sys/vdev_impl.h>
50 #include <sys/metaslab.h>
51 #include <sys/metaslab_impl.h>
52 #include <sys/uberblock_impl.h>
55 #include <sys/dmu_traverse.h>
56 #include <sys/dmu_objset.h>
57 #include <sys/unique.h>
58 #include <sys/dsl_pool.h>
59 #include <sys/dsl_dataset.h>
60 #include <sys/dsl_dir.h>
61 #include <sys/dsl_prop.h>
62 #include <sys/dsl_synctask.h>
63 #include <sys/fs/zfs.h>
65 #include <sys/callb.h>
66 #include <sys/spa_boot.h>
67 #include <sys/zfs_ioctl.h>
68 #include <sys/dsl_scan.h>
69 #include <sys/dmu_send.h>
70 #include <sys/dsl_destroy.h>
71 #include <sys/dsl_userhold.h>
72 #include <sys/zfeature.h>
74 #include <sys/trim_map.h>
77 #include <sys/callb.h>
78 #include <sys/cpupart.h>
83 #include "zfs_comutil.h"
85 /* Check hostid on import? */
86 static int check_hostid = 1;
89 * The interval, in seconds, at which failed configuration cache file writes
92 static int zfs_ccw_retry_interval = 300;
94 SYSCTL_DECL(_vfs_zfs);
95 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
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 * Validate the current config against the MOS config
1672 spa_config_valid(spa_t *spa, nvlist_t *config)
1674 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1677 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1679 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1680 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1682 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1685 * If we're doing a normal import, then build up any additional
1686 * diagnostic information about missing devices in this config.
1687 * We'll pass this up to the user for further processing.
1689 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1690 nvlist_t **child, *nv;
1693 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1695 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1697 for (int c = 0; c < rvd->vdev_children; c++) {
1698 vdev_t *tvd = rvd->vdev_child[c];
1699 vdev_t *mtvd = mrvd->vdev_child[c];
1701 if (tvd->vdev_ops == &vdev_missing_ops &&
1702 mtvd->vdev_ops != &vdev_missing_ops &&
1704 child[idx++] = vdev_config_generate(spa, mtvd,
1709 VERIFY(nvlist_add_nvlist_array(nv,
1710 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1711 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1712 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1714 for (int i = 0; i < idx; i++)
1715 nvlist_free(child[i]);
1718 kmem_free(child, rvd->vdev_children * sizeof (char **));
1722 * Compare the root vdev tree with the information we have
1723 * from the MOS config (mrvd). Check each top-level vdev
1724 * with the corresponding MOS config top-level (mtvd).
1726 for (int c = 0; c < rvd->vdev_children; c++) {
1727 vdev_t *tvd = rvd->vdev_child[c];
1728 vdev_t *mtvd = mrvd->vdev_child[c];
1731 * Resolve any "missing" vdevs in the current configuration.
1732 * If we find that the MOS config has more accurate information
1733 * about the top-level vdev then use that vdev instead.
1735 if (tvd->vdev_ops == &vdev_missing_ops &&
1736 mtvd->vdev_ops != &vdev_missing_ops) {
1738 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1742 * Device specific actions.
1744 if (mtvd->vdev_islog) {
1745 spa_set_log_state(spa, SPA_LOG_CLEAR);
1748 * XXX - once we have 'readonly' pool
1749 * support we should be able to handle
1750 * missing data devices by transitioning
1751 * the pool to readonly.
1757 * Swap the missing vdev with the data we were
1758 * able to obtain from the MOS config.
1760 vdev_remove_child(rvd, tvd);
1761 vdev_remove_child(mrvd, mtvd);
1763 vdev_add_child(rvd, mtvd);
1764 vdev_add_child(mrvd, tvd);
1766 spa_config_exit(spa, SCL_ALL, FTAG);
1768 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1771 } else if (mtvd->vdev_islog) {
1773 * Load the slog device's state from the MOS config
1774 * since it's possible that the label does not
1775 * contain the most up-to-date information.
1777 vdev_load_log_state(tvd, mtvd);
1782 spa_config_exit(spa, SCL_ALL, FTAG);
1785 * Ensure we were able to validate the config.
1787 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1791 * Check for missing log devices
1794 spa_check_logs(spa_t *spa)
1796 boolean_t rv = B_FALSE;
1797 dsl_pool_t *dp = spa_get_dsl(spa);
1799 switch (spa->spa_log_state) {
1800 case SPA_LOG_MISSING:
1801 /* need to recheck in case slog has been restored */
1802 case SPA_LOG_UNKNOWN:
1803 rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1804 zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
1806 spa_set_log_state(spa, SPA_LOG_MISSING);
1813 spa_passivate_log(spa_t *spa)
1815 vdev_t *rvd = spa->spa_root_vdev;
1816 boolean_t slog_found = B_FALSE;
1818 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1820 if (!spa_has_slogs(spa))
1823 for (int c = 0; c < rvd->vdev_children; c++) {
1824 vdev_t *tvd = rvd->vdev_child[c];
1825 metaslab_group_t *mg = tvd->vdev_mg;
1827 if (tvd->vdev_islog) {
1828 metaslab_group_passivate(mg);
1829 slog_found = B_TRUE;
1833 return (slog_found);
1837 spa_activate_log(spa_t *spa)
1839 vdev_t *rvd = spa->spa_root_vdev;
1841 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1843 for (int c = 0; c < rvd->vdev_children; c++) {
1844 vdev_t *tvd = rvd->vdev_child[c];
1845 metaslab_group_t *mg = tvd->vdev_mg;
1847 if (tvd->vdev_islog)
1848 metaslab_group_activate(mg);
1853 spa_offline_log(spa_t *spa)
1857 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1858 NULL, DS_FIND_CHILDREN);
1861 * We successfully offlined the log device, sync out the
1862 * current txg so that the "stubby" block can be removed
1865 txg_wait_synced(spa->spa_dsl_pool, 0);
1871 spa_aux_check_removed(spa_aux_vdev_t *sav)
1875 for (i = 0; i < sav->sav_count; i++)
1876 spa_check_removed(sav->sav_vdevs[i]);
1880 spa_claim_notify(zio_t *zio)
1882 spa_t *spa = zio->io_spa;
1887 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1888 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1889 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1890 mutex_exit(&spa->spa_props_lock);
1893 typedef struct spa_load_error {
1894 uint64_t sle_meta_count;
1895 uint64_t sle_data_count;
1899 spa_load_verify_done(zio_t *zio)
1901 blkptr_t *bp = zio->io_bp;
1902 spa_load_error_t *sle = zio->io_private;
1903 dmu_object_type_t type = BP_GET_TYPE(bp);
1904 int error = zio->io_error;
1905 spa_t *spa = zio->io_spa;
1908 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1909 type != DMU_OT_INTENT_LOG)
1910 atomic_inc_64(&sle->sle_meta_count);
1912 atomic_inc_64(&sle->sle_data_count);
1914 zio_data_buf_free(zio->io_data, zio->io_size);
1916 mutex_enter(&spa->spa_scrub_lock);
1917 spa->spa_scrub_inflight--;
1918 cv_broadcast(&spa->spa_scrub_io_cv);
1919 mutex_exit(&spa->spa_scrub_lock);
1923 * Maximum number of concurrent scrub i/os to create while verifying
1924 * a pool while importing it.
1926 int spa_load_verify_maxinflight = 10000;
1927 boolean_t spa_load_verify_metadata = B_TRUE;
1928 boolean_t spa_load_verify_data = B_TRUE;
1930 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1931 &spa_load_verify_maxinflight, 0,
1932 "Maximum number of concurrent scrub I/Os to create while verifying a "
1933 "pool while importing it");
1935 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1936 &spa_load_verify_metadata, 0,
1937 "Check metadata on import?");
1939 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1940 &spa_load_verify_data, 0,
1941 "Check user data on import?");
1945 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1946 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1948 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1951 * Note: normally this routine will not be called if
1952 * spa_load_verify_metadata is not set. However, it may be useful
1953 * to manually set the flag after the traversal has begun.
1955 if (!spa_load_verify_metadata)
1957 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1961 size_t size = BP_GET_PSIZE(bp);
1962 void *data = zio_data_buf_alloc(size);
1964 mutex_enter(&spa->spa_scrub_lock);
1965 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1966 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1967 spa->spa_scrub_inflight++;
1968 mutex_exit(&spa->spa_scrub_lock);
1970 zio_nowait(zio_read(rio, spa, bp, data, size,
1971 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1972 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1973 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1978 spa_load_verify(spa_t *spa)
1981 spa_load_error_t sle = { 0 };
1982 zpool_rewind_policy_t policy;
1983 boolean_t verify_ok = B_FALSE;
1986 zpool_get_rewind_policy(spa->spa_config, &policy);
1988 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1991 rio = zio_root(spa, NULL, &sle,
1992 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1994 if (spa_load_verify_metadata) {
1995 error = traverse_pool(spa, spa->spa_verify_min_txg,
1996 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
1997 spa_load_verify_cb, rio);
2000 (void) zio_wait(rio);
2002 spa->spa_load_meta_errors = sle.sle_meta_count;
2003 spa->spa_load_data_errors = sle.sle_data_count;
2005 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
2006 sle.sle_data_count <= policy.zrp_maxdata) {
2010 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2011 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2013 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2014 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2015 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2016 VERIFY(nvlist_add_int64(spa->spa_load_info,
2017 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2018 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2019 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2021 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2025 if (error != ENXIO && error != EIO)
2026 error = SET_ERROR(EIO);
2030 return (verify_ok ? 0 : EIO);
2034 * Find a value in the pool props object.
2037 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2039 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2040 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2044 * Find a value in the pool directory object.
2047 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2049 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2050 name, sizeof (uint64_t), 1, val));
2054 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2056 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2061 * Fix up config after a partly-completed split. This is done with the
2062 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2063 * pool have that entry in their config, but only the splitting one contains
2064 * a list of all the guids of the vdevs that are being split off.
2066 * This function determines what to do with that list: either rejoin
2067 * all the disks to the pool, or complete the splitting process. To attempt
2068 * the rejoin, each disk that is offlined is marked online again, and
2069 * we do a reopen() call. If the vdev label for every disk that was
2070 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2071 * then we call vdev_split() on each disk, and complete the split.
2073 * Otherwise we leave the config alone, with all the vdevs in place in
2074 * the original pool.
2077 spa_try_repair(spa_t *spa, nvlist_t *config)
2084 boolean_t attempt_reopen;
2086 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2089 /* check that the config is complete */
2090 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2091 &glist, &gcount) != 0)
2094 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2096 /* attempt to online all the vdevs & validate */
2097 attempt_reopen = B_TRUE;
2098 for (i = 0; i < gcount; i++) {
2099 if (glist[i] == 0) /* vdev is hole */
2102 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2103 if (vd[i] == NULL) {
2105 * Don't bother attempting to reopen the disks;
2106 * just do the split.
2108 attempt_reopen = B_FALSE;
2110 /* attempt to re-online it */
2111 vd[i]->vdev_offline = B_FALSE;
2115 if (attempt_reopen) {
2116 vdev_reopen(spa->spa_root_vdev);
2118 /* check each device to see what state it's in */
2119 for (extracted = 0, i = 0; i < gcount; i++) {
2120 if (vd[i] != NULL &&
2121 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2128 * If every disk has been moved to the new pool, or if we never
2129 * even attempted to look at them, then we split them off for
2132 if (!attempt_reopen || gcount == extracted) {
2133 for (i = 0; i < gcount; i++)
2136 vdev_reopen(spa->spa_root_vdev);
2139 kmem_free(vd, gcount * sizeof (vdev_t *));
2143 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2144 boolean_t mosconfig)
2146 nvlist_t *config = spa->spa_config;
2147 char *ereport = FM_EREPORT_ZFS_POOL;
2153 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2154 return (SET_ERROR(EINVAL));
2156 ASSERT(spa->spa_comment == NULL);
2157 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2158 spa->spa_comment = spa_strdup(comment);
2161 * Versioning wasn't explicitly added to the label until later, so if
2162 * it's not present treat it as the initial version.
2164 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2165 &spa->spa_ubsync.ub_version) != 0)
2166 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2168 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2169 &spa->spa_config_txg);
2171 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2172 spa_guid_exists(pool_guid, 0)) {
2173 error = SET_ERROR(EEXIST);
2175 spa->spa_config_guid = pool_guid;
2177 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2179 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2183 nvlist_free(spa->spa_load_info);
2184 spa->spa_load_info = fnvlist_alloc();
2186 gethrestime(&spa->spa_loaded_ts);
2187 error = spa_load_impl(spa, pool_guid, config, state, type,
2188 mosconfig, &ereport);
2192 * Don't count references from objsets that are already closed
2193 * and are making their way through the eviction process.
2195 spa_evicting_os_wait(spa);
2196 spa->spa_minref = refcount_count(&spa->spa_refcount);
2198 if (error != EEXIST) {
2199 spa->spa_loaded_ts.tv_sec = 0;
2200 spa->spa_loaded_ts.tv_nsec = 0;
2202 if (error != EBADF) {
2203 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2206 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2213 * Load an existing storage pool, using the pool's builtin spa_config as a
2214 * source of configuration information.
2217 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2218 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2222 nvlist_t *nvroot = NULL;
2225 uberblock_t *ub = &spa->spa_uberblock;
2226 uint64_t children, config_cache_txg = spa->spa_config_txg;
2227 int orig_mode = spa->spa_mode;
2230 boolean_t missing_feat_write = B_FALSE;
2233 * If this is an untrusted config, access the pool in read-only mode.
2234 * This prevents things like resilvering recently removed devices.
2237 spa->spa_mode = FREAD;
2239 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2241 spa->spa_load_state = state;
2243 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2244 return (SET_ERROR(EINVAL));
2246 parse = (type == SPA_IMPORT_EXISTING ?
2247 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2250 * Create "The Godfather" zio to hold all async IOs
2252 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2254 for (int i = 0; i < max_ncpus; i++) {
2255 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2256 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2257 ZIO_FLAG_GODFATHER);
2261 * Parse the configuration into a vdev tree. We explicitly set the
2262 * value that will be returned by spa_version() since parsing the
2263 * configuration requires knowing the version number.
2265 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2266 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2267 spa_config_exit(spa, SCL_ALL, FTAG);
2272 ASSERT(spa->spa_root_vdev == rvd);
2273 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2274 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
2276 if (type != SPA_IMPORT_ASSEMBLE) {
2277 ASSERT(spa_guid(spa) == pool_guid);
2281 * Try to open all vdevs, loading each label in the process.
2283 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2284 error = vdev_open(rvd);
2285 spa_config_exit(spa, SCL_ALL, FTAG);
2290 * We need to validate the vdev labels against the configuration that
2291 * we have in hand, which is dependent on the setting of mosconfig. If
2292 * mosconfig is true then we're validating the vdev labels based on
2293 * that config. Otherwise, we're validating against the cached config
2294 * (zpool.cache) that was read when we loaded the zfs module, and then
2295 * later we will recursively call spa_load() and validate against
2298 * If we're assembling a new pool that's been split off from an
2299 * existing pool, the labels haven't yet been updated so we skip
2300 * validation for now.
2302 if (type != SPA_IMPORT_ASSEMBLE) {
2303 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2304 error = vdev_validate(rvd, mosconfig);
2305 spa_config_exit(spa, SCL_ALL, FTAG);
2310 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2311 return (SET_ERROR(ENXIO));
2315 * Find the best uberblock.
2317 vdev_uberblock_load(rvd, ub, &label);
2320 * If we weren't able to find a single valid uberblock, return failure.
2322 if (ub->ub_txg == 0) {
2324 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2328 * If the pool has an unsupported version we can't open it.
2330 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2332 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2335 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2339 * If we weren't able to find what's necessary for reading the
2340 * MOS in the label, return failure.
2342 if (label == NULL || nvlist_lookup_nvlist(label,
2343 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2345 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2350 * Update our in-core representation with the definitive values
2353 nvlist_free(spa->spa_label_features);
2354 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2360 * Look through entries in the label nvlist's features_for_read. If
2361 * there is a feature listed there which we don't understand then we
2362 * cannot open a pool.
2364 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2365 nvlist_t *unsup_feat;
2367 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2370 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2372 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2373 if (!zfeature_is_supported(nvpair_name(nvp))) {
2374 VERIFY(nvlist_add_string(unsup_feat,
2375 nvpair_name(nvp), "") == 0);
2379 if (!nvlist_empty(unsup_feat)) {
2380 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2381 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2382 nvlist_free(unsup_feat);
2383 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2387 nvlist_free(unsup_feat);
2391 * If the vdev guid sum doesn't match the uberblock, we have an
2392 * incomplete configuration. We first check to see if the pool
2393 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2394 * If it is, defer the vdev_guid_sum check till later so we
2395 * can handle missing vdevs.
2397 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2398 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2399 rvd->vdev_guid_sum != ub->ub_guid_sum)
2400 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2402 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2403 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2404 spa_try_repair(spa, config);
2405 spa_config_exit(spa, SCL_ALL, FTAG);
2406 nvlist_free(spa->spa_config_splitting);
2407 spa->spa_config_splitting = NULL;
2411 * Initialize internal SPA structures.
2413 spa->spa_state = POOL_STATE_ACTIVE;
2414 spa->spa_ubsync = spa->spa_uberblock;
2415 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2416 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2417 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2418 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2419 spa->spa_claim_max_txg = spa->spa_first_txg;
2420 spa->spa_prev_software_version = ub->ub_software_version;
2422 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2424 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2425 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2427 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2428 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2430 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2431 boolean_t missing_feat_read = B_FALSE;
2432 nvlist_t *unsup_feat, *enabled_feat;
2434 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2435 &spa->spa_feat_for_read_obj) != 0) {
2436 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2439 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2440 &spa->spa_feat_for_write_obj) != 0) {
2441 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2444 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2445 &spa->spa_feat_desc_obj) != 0) {
2446 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2449 enabled_feat = fnvlist_alloc();
2450 unsup_feat = fnvlist_alloc();
2452 if (!spa_features_check(spa, B_FALSE,
2453 unsup_feat, enabled_feat))
2454 missing_feat_read = B_TRUE;
2456 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2457 if (!spa_features_check(spa, B_TRUE,
2458 unsup_feat, enabled_feat)) {
2459 missing_feat_write = B_TRUE;
2463 fnvlist_add_nvlist(spa->spa_load_info,
2464 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2466 if (!nvlist_empty(unsup_feat)) {
2467 fnvlist_add_nvlist(spa->spa_load_info,
2468 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2471 fnvlist_free(enabled_feat);
2472 fnvlist_free(unsup_feat);
2474 if (!missing_feat_read) {
2475 fnvlist_add_boolean(spa->spa_load_info,
2476 ZPOOL_CONFIG_CAN_RDONLY);
2480 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2481 * twofold: to determine whether the pool is available for
2482 * import in read-write mode and (if it is not) whether the
2483 * pool is available for import in read-only mode. If the pool
2484 * is available for import in read-write mode, it is displayed
2485 * as available in userland; if it is not available for import
2486 * in read-only mode, it is displayed as unavailable in
2487 * userland. If the pool is available for import in read-only
2488 * mode but not read-write mode, it is displayed as unavailable
2489 * in userland with a special note that the pool is actually
2490 * available for open in read-only mode.
2492 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2493 * missing a feature for write, we must first determine whether
2494 * the pool can be opened read-only before returning to
2495 * userland in order to know whether to display the
2496 * abovementioned note.
2498 if (missing_feat_read || (missing_feat_write &&
2499 spa_writeable(spa))) {
2500 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2505 * Load refcounts for ZFS features from disk into an in-memory
2506 * cache during SPA initialization.
2508 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2511 error = feature_get_refcount_from_disk(spa,
2512 &spa_feature_table[i], &refcount);
2514 spa->spa_feat_refcount_cache[i] = refcount;
2515 } else if (error == ENOTSUP) {
2516 spa->spa_feat_refcount_cache[i] =
2517 SPA_FEATURE_DISABLED;
2519 return (spa_vdev_err(rvd,
2520 VDEV_AUX_CORRUPT_DATA, EIO));
2525 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2526 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2527 &spa->spa_feat_enabled_txg_obj) != 0)
2528 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2531 spa->spa_is_initializing = B_TRUE;
2532 error = dsl_pool_open(spa->spa_dsl_pool);
2533 spa->spa_is_initializing = B_FALSE;
2535 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2539 nvlist_t *policy = NULL, *nvconfig;
2541 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2542 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2544 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2545 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2547 unsigned long myhostid = 0;
2549 VERIFY(nvlist_lookup_string(nvconfig,
2550 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2553 myhostid = zone_get_hostid(NULL);
2556 * We're emulating the system's hostid in userland, so
2557 * we can't use zone_get_hostid().
2559 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2560 #endif /* _KERNEL */
2561 if (check_hostid && hostid != 0 && myhostid != 0 &&
2562 hostid != myhostid) {
2563 nvlist_free(nvconfig);
2564 cmn_err(CE_WARN, "pool '%s' could not be "
2565 "loaded as it was last accessed by "
2566 "another system (host: %s hostid: 0x%lx). "
2567 "See: http://illumos.org/msg/ZFS-8000-EY",
2568 spa_name(spa), hostname,
2569 (unsigned long)hostid);
2570 return (SET_ERROR(EBADF));
2573 if (nvlist_lookup_nvlist(spa->spa_config,
2574 ZPOOL_REWIND_POLICY, &policy) == 0)
2575 VERIFY(nvlist_add_nvlist(nvconfig,
2576 ZPOOL_REWIND_POLICY, policy) == 0);
2578 spa_config_set(spa, nvconfig);
2580 spa_deactivate(spa);
2581 spa_activate(spa, orig_mode);
2583 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2586 /* Grab the secret checksum salt from the MOS. */
2587 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2588 DMU_POOL_CHECKSUM_SALT, 1,
2589 sizeof (spa->spa_cksum_salt.zcs_bytes),
2590 spa->spa_cksum_salt.zcs_bytes);
2591 if (error == ENOENT) {
2592 /* Generate a new salt for subsequent use */
2593 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
2594 sizeof (spa->spa_cksum_salt.zcs_bytes));
2595 } else if (error != 0) {
2596 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2599 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2600 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2601 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2603 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2606 * Load the bit that tells us to use the new accounting function
2607 * (raid-z deflation). If we have an older pool, this will not
2610 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2611 if (error != 0 && error != ENOENT)
2612 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2614 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2615 &spa->spa_creation_version);
2616 if (error != 0 && error != ENOENT)
2617 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2620 * Load the persistent error log. If we have an older pool, this will
2623 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2624 if (error != 0 && error != ENOENT)
2625 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2627 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2628 &spa->spa_errlog_scrub);
2629 if (error != 0 && error != ENOENT)
2630 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2633 * Load the history object. If we have an older pool, this
2634 * will not be present.
2636 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2637 if (error != 0 && error != ENOENT)
2638 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2641 * If we're assembling the pool from the split-off vdevs of
2642 * an existing pool, we don't want to attach the spares & cache
2647 * Load any hot spares for this pool.
2649 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2650 if (error != 0 && error != ENOENT)
2651 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2652 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2653 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2654 if (load_nvlist(spa, spa->spa_spares.sav_object,
2655 &spa->spa_spares.sav_config) != 0)
2656 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2658 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2659 spa_load_spares(spa);
2660 spa_config_exit(spa, SCL_ALL, FTAG);
2661 } else if (error == 0) {
2662 spa->spa_spares.sav_sync = B_TRUE;
2666 * Load any level 2 ARC devices for this pool.
2668 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2669 &spa->spa_l2cache.sav_object);
2670 if (error != 0 && error != ENOENT)
2671 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2672 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2673 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2674 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2675 &spa->spa_l2cache.sav_config) != 0)
2676 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2678 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2679 spa_load_l2cache(spa);
2680 spa_config_exit(spa, SCL_ALL, FTAG);
2681 } else if (error == 0) {
2682 spa->spa_l2cache.sav_sync = B_TRUE;
2685 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2687 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2688 if (error && error != ENOENT)
2689 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2692 uint64_t autoreplace;
2694 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2695 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2696 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2697 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2698 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2699 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2700 &spa->spa_dedup_ditto);
2702 spa->spa_autoreplace = (autoreplace != 0);
2706 * If the 'autoreplace' property is set, then post a resource notifying
2707 * the ZFS DE that it should not issue any faults for unopenable
2708 * devices. We also iterate over the vdevs, and post a sysevent for any
2709 * unopenable vdevs so that the normal autoreplace handler can take
2712 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2713 spa_check_removed(spa->spa_root_vdev);
2715 * For the import case, this is done in spa_import(), because
2716 * at this point we're using the spare definitions from
2717 * the MOS config, not necessarily from the userland config.
2719 if (state != SPA_LOAD_IMPORT) {
2720 spa_aux_check_removed(&spa->spa_spares);
2721 spa_aux_check_removed(&spa->spa_l2cache);
2726 * Load the vdev state for all toplevel vdevs.
2731 * Propagate the leaf DTLs we just loaded all the way up the tree.
2733 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2734 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2735 spa_config_exit(spa, SCL_ALL, FTAG);
2738 * Load the DDTs (dedup tables).
2740 error = ddt_load(spa);
2742 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2744 spa_update_dspace(spa);
2747 * Validate the config, using the MOS config to fill in any
2748 * information which might be missing. If we fail to validate
2749 * the config then declare the pool unfit for use. If we're
2750 * assembling a pool from a split, the log is not transferred
2753 if (type != SPA_IMPORT_ASSEMBLE) {
2756 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2757 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2759 if (!spa_config_valid(spa, nvconfig)) {
2760 nvlist_free(nvconfig);
2761 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2764 nvlist_free(nvconfig);
2767 * Now that we've validated the config, check the state of the
2768 * root vdev. If it can't be opened, it indicates one or
2769 * more toplevel vdevs are faulted.
2771 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2772 return (SET_ERROR(ENXIO));
2774 if (spa_writeable(spa) && spa_check_logs(spa)) {
2775 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2776 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2780 if (missing_feat_write) {
2781 ASSERT(state == SPA_LOAD_TRYIMPORT);
2784 * At this point, we know that we can open the pool in
2785 * read-only mode but not read-write mode. We now have enough
2786 * information and can return to userland.
2788 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2792 * We've successfully opened the pool, verify that we're ready
2793 * to start pushing transactions.
2795 if (state != SPA_LOAD_TRYIMPORT) {
2796 if (error = spa_load_verify(spa))
2797 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2801 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2802 spa->spa_load_max_txg == UINT64_MAX)) {
2804 int need_update = B_FALSE;
2805 dsl_pool_t *dp = spa_get_dsl(spa);
2807 ASSERT(state != SPA_LOAD_TRYIMPORT);
2810 * Claim log blocks that haven't been committed yet.
2811 * This must all happen in a single txg.
2812 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2813 * invoked from zil_claim_log_block()'s i/o done callback.
2814 * Price of rollback is that we abandon the log.
2816 spa->spa_claiming = B_TRUE;
2818 tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
2819 (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2820 zil_claim, tx, DS_FIND_CHILDREN);
2823 spa->spa_claiming = B_FALSE;
2825 spa_set_log_state(spa, SPA_LOG_GOOD);
2826 spa->spa_sync_on = B_TRUE;
2827 txg_sync_start(spa->spa_dsl_pool);
2830 * Wait for all claims to sync. We sync up to the highest
2831 * claimed log block birth time so that claimed log blocks
2832 * don't appear to be from the future. spa_claim_max_txg
2833 * will have been set for us by either zil_check_log_chain()
2834 * (invoked from spa_check_logs()) or zil_claim() above.
2836 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2839 * If the config cache is stale, or we have uninitialized
2840 * metaslabs (see spa_vdev_add()), then update the config.
2842 * If this is a verbatim import, trust the current
2843 * in-core spa_config and update the disk labels.
2845 if (config_cache_txg != spa->spa_config_txg ||
2846 state == SPA_LOAD_IMPORT ||
2847 state == SPA_LOAD_RECOVER ||
2848 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2849 need_update = B_TRUE;
2851 for (int c = 0; c < rvd->vdev_children; c++)
2852 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2853 need_update = B_TRUE;
2856 * Update the config cache asychronously in case we're the
2857 * root pool, in which case the config cache isn't writable yet.
2860 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2863 * Check all DTLs to see if anything needs resilvering.
2865 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2866 vdev_resilver_needed(rvd, NULL, NULL))
2867 spa_async_request(spa, SPA_ASYNC_RESILVER);
2870 * Log the fact that we booted up (so that we can detect if
2871 * we rebooted in the middle of an operation).
2873 spa_history_log_version(spa, "open");
2876 * Delete any inconsistent datasets.
2878 (void) dmu_objset_find(spa_name(spa),
2879 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2882 * Clean up any stale temporary dataset userrefs.
2884 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2891 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2893 int mode = spa->spa_mode;
2896 spa_deactivate(spa);
2898 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2900 spa_activate(spa, mode);
2901 spa_async_suspend(spa);
2903 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2907 * If spa_load() fails this function will try loading prior txg's. If
2908 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2909 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2910 * function will not rewind the pool and will return the same error as
2914 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2915 uint64_t max_request, int rewind_flags)
2917 nvlist_t *loadinfo = NULL;
2918 nvlist_t *config = NULL;
2919 int load_error, rewind_error;
2920 uint64_t safe_rewind_txg;
2923 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2924 spa->spa_load_max_txg = spa->spa_load_txg;
2925 spa_set_log_state(spa, SPA_LOG_CLEAR);
2927 spa->spa_load_max_txg = max_request;
2928 if (max_request != UINT64_MAX)
2929 spa->spa_extreme_rewind = B_TRUE;
2932 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2934 if (load_error == 0)
2937 if (spa->spa_root_vdev != NULL)
2938 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2940 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2941 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2943 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2944 nvlist_free(config);
2945 return (load_error);
2948 if (state == SPA_LOAD_RECOVER) {
2949 /* Price of rolling back is discarding txgs, including log */
2950 spa_set_log_state(spa, SPA_LOG_CLEAR);
2953 * If we aren't rolling back save the load info from our first
2954 * import attempt so that we can restore it after attempting
2957 loadinfo = spa->spa_load_info;
2958 spa->spa_load_info = fnvlist_alloc();
2961 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2962 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2963 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2964 TXG_INITIAL : safe_rewind_txg;
2967 * Continue as long as we're finding errors, we're still within
2968 * the acceptable rewind range, and we're still finding uberblocks
2970 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2971 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2972 if (spa->spa_load_max_txg < safe_rewind_txg)
2973 spa->spa_extreme_rewind = B_TRUE;
2974 rewind_error = spa_load_retry(spa, state, mosconfig);
2977 spa->spa_extreme_rewind = B_FALSE;
2978 spa->spa_load_max_txg = UINT64_MAX;
2980 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2981 spa_config_set(spa, config);
2983 if (state == SPA_LOAD_RECOVER) {
2984 ASSERT3P(loadinfo, ==, NULL);
2985 return (rewind_error);
2987 /* Store the rewind info as part of the initial load info */
2988 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2989 spa->spa_load_info);
2991 /* Restore the initial load info */
2992 fnvlist_free(spa->spa_load_info);
2993 spa->spa_load_info = loadinfo;
2995 return (load_error);
3002 * The import case is identical to an open except that the configuration is sent
3003 * down from userland, instead of grabbed from the configuration cache. For the
3004 * case of an open, the pool configuration will exist in the
3005 * POOL_STATE_UNINITIALIZED state.
3007 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3008 * the same time open the pool, without having to keep around the spa_t in some
3012 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
3016 spa_load_state_t state = SPA_LOAD_OPEN;
3018 int locked = B_FALSE;
3019 int firstopen = B_FALSE;
3024 * As disgusting as this is, we need to support recursive calls to this
3025 * function because dsl_dir_open() is called during spa_load(), and ends
3026 * up calling spa_open() again. The real fix is to figure out how to
3027 * avoid dsl_dir_open() calling this in the first place.
3029 if (mutex_owner(&spa_namespace_lock) != curthread) {
3030 mutex_enter(&spa_namespace_lock);
3034 if ((spa = spa_lookup(pool)) == NULL) {
3036 mutex_exit(&spa_namespace_lock);
3037 return (SET_ERROR(ENOENT));
3040 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
3041 zpool_rewind_policy_t policy;
3045 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
3047 if (policy.zrp_request & ZPOOL_DO_REWIND)
3048 state = SPA_LOAD_RECOVER;
3050 spa_activate(spa, spa_mode_global);
3052 if (state != SPA_LOAD_RECOVER)
3053 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3055 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3056 policy.zrp_request);
3058 if (error == EBADF) {
3060 * If vdev_validate() returns failure (indicated by
3061 * EBADF), it indicates that one of the vdevs indicates
3062 * that the pool has been exported or destroyed. If
3063 * this is the case, the config cache is out of sync and
3064 * we should remove the pool from the namespace.
3067 spa_deactivate(spa);
3068 spa_config_sync(spa, B_TRUE, B_TRUE);
3071 mutex_exit(&spa_namespace_lock);
3072 return (SET_ERROR(ENOENT));
3077 * We can't open the pool, but we still have useful
3078 * information: the state of each vdev after the
3079 * attempted vdev_open(). Return this to the user.
3081 if (config != NULL && spa->spa_config) {
3082 VERIFY(nvlist_dup(spa->spa_config, config,
3084 VERIFY(nvlist_add_nvlist(*config,
3085 ZPOOL_CONFIG_LOAD_INFO,
3086 spa->spa_load_info) == 0);
3089 spa_deactivate(spa);
3090 spa->spa_last_open_failed = error;
3092 mutex_exit(&spa_namespace_lock);
3098 spa_open_ref(spa, tag);
3101 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3104 * If we've recovered the pool, pass back any information we
3105 * gathered while doing the load.
3107 if (state == SPA_LOAD_RECOVER) {
3108 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3109 spa->spa_load_info) == 0);
3113 spa->spa_last_open_failed = 0;
3114 spa->spa_last_ubsync_txg = 0;
3115 spa->spa_load_txg = 0;
3116 mutex_exit(&spa_namespace_lock);
3120 zvol_create_minors(spa->spa_name);
3131 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3134 return (spa_open_common(name, spapp, tag, policy, config));
3138 spa_open(const char *name, spa_t **spapp, void *tag)
3140 return (spa_open_common(name, spapp, tag, NULL, NULL));
3144 * Lookup the given spa_t, incrementing the inject count in the process,
3145 * preventing it from being exported or destroyed.
3148 spa_inject_addref(char *name)
3152 mutex_enter(&spa_namespace_lock);
3153 if ((spa = spa_lookup(name)) == NULL) {
3154 mutex_exit(&spa_namespace_lock);
3157 spa->spa_inject_ref++;
3158 mutex_exit(&spa_namespace_lock);
3164 spa_inject_delref(spa_t *spa)
3166 mutex_enter(&spa_namespace_lock);
3167 spa->spa_inject_ref--;
3168 mutex_exit(&spa_namespace_lock);
3172 * Add spares device information to the nvlist.
3175 spa_add_spares(spa_t *spa, nvlist_t *config)
3185 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3187 if (spa->spa_spares.sav_count == 0)
3190 VERIFY(nvlist_lookup_nvlist(config,
3191 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3192 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3193 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3195 VERIFY(nvlist_add_nvlist_array(nvroot,
3196 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3197 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3198 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3201 * Go through and find any spares which have since been
3202 * repurposed as an active spare. If this is the case, update
3203 * their status appropriately.
3205 for (i = 0; i < nspares; i++) {
3206 VERIFY(nvlist_lookup_uint64(spares[i],
3207 ZPOOL_CONFIG_GUID, &guid) == 0);
3208 if (spa_spare_exists(guid, &pool, NULL) &&
3210 VERIFY(nvlist_lookup_uint64_array(
3211 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3212 (uint64_t **)&vs, &vsc) == 0);
3213 vs->vs_state = VDEV_STATE_CANT_OPEN;
3214 vs->vs_aux = VDEV_AUX_SPARED;
3221 * Add l2cache device information to the nvlist, including vdev stats.
3224 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3227 uint_t i, j, nl2cache;
3234 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3236 if (spa->spa_l2cache.sav_count == 0)
3239 VERIFY(nvlist_lookup_nvlist(config,
3240 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3241 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3242 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3243 if (nl2cache != 0) {
3244 VERIFY(nvlist_add_nvlist_array(nvroot,
3245 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3246 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3247 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3250 * Update level 2 cache device stats.
3253 for (i = 0; i < nl2cache; i++) {
3254 VERIFY(nvlist_lookup_uint64(l2cache[i],
3255 ZPOOL_CONFIG_GUID, &guid) == 0);
3258 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3260 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3261 vd = spa->spa_l2cache.sav_vdevs[j];
3267 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3268 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3270 vdev_get_stats(vd, vs);
3276 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3282 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3283 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3285 /* We may be unable to read features if pool is suspended. */
3286 if (spa_suspended(spa))
3289 if (spa->spa_feat_for_read_obj != 0) {
3290 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3291 spa->spa_feat_for_read_obj);
3292 zap_cursor_retrieve(&zc, &za) == 0;
3293 zap_cursor_advance(&zc)) {
3294 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3295 za.za_num_integers == 1);
3296 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3297 za.za_first_integer));
3299 zap_cursor_fini(&zc);
3302 if (spa->spa_feat_for_write_obj != 0) {
3303 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3304 spa->spa_feat_for_write_obj);
3305 zap_cursor_retrieve(&zc, &za) == 0;
3306 zap_cursor_advance(&zc)) {
3307 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3308 za.za_num_integers == 1);
3309 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3310 za.za_first_integer));
3312 zap_cursor_fini(&zc);
3316 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3318 nvlist_free(features);
3322 spa_get_stats(const char *name, nvlist_t **config,
3323 char *altroot, size_t buflen)
3329 error = spa_open_common(name, &spa, FTAG, NULL, config);
3333 * This still leaves a window of inconsistency where the spares
3334 * or l2cache devices could change and the config would be
3335 * self-inconsistent.
3337 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3339 if (*config != NULL) {
3340 uint64_t loadtimes[2];
3342 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3343 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3344 VERIFY(nvlist_add_uint64_array(*config,
3345 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3347 VERIFY(nvlist_add_uint64(*config,
3348 ZPOOL_CONFIG_ERRCOUNT,
3349 spa_get_errlog_size(spa)) == 0);
3351 if (spa_suspended(spa))
3352 VERIFY(nvlist_add_uint64(*config,
3353 ZPOOL_CONFIG_SUSPENDED,
3354 spa->spa_failmode) == 0);
3356 spa_add_spares(spa, *config);
3357 spa_add_l2cache(spa, *config);
3358 spa_add_feature_stats(spa, *config);
3363 * We want to get the alternate root even for faulted pools, so we cheat
3364 * and call spa_lookup() directly.
3368 mutex_enter(&spa_namespace_lock);
3369 spa = spa_lookup(name);
3371 spa_altroot(spa, altroot, buflen);
3375 mutex_exit(&spa_namespace_lock);
3377 spa_altroot(spa, altroot, buflen);
3382 spa_config_exit(spa, SCL_CONFIG, FTAG);
3383 spa_close(spa, FTAG);
3390 * Validate that the auxiliary device array is well formed. We must have an
3391 * array of nvlists, each which describes a valid leaf vdev. If this is an
3392 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3393 * specified, as long as they are well-formed.
3396 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3397 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3398 vdev_labeltype_t label)
3405 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3408 * It's acceptable to have no devs specified.
3410 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3414 return (SET_ERROR(EINVAL));
3417 * Make sure the pool is formatted with a version that supports this
3420 if (spa_version(spa) < version)
3421 return (SET_ERROR(ENOTSUP));
3424 * Set the pending device list so we correctly handle device in-use
3427 sav->sav_pending = dev;
3428 sav->sav_npending = ndev;
3430 for (i = 0; i < ndev; i++) {
3431 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3435 if (!vd->vdev_ops->vdev_op_leaf) {
3437 error = SET_ERROR(EINVAL);
3442 * The L2ARC currently only supports disk devices in
3443 * kernel context. For user-level testing, we allow it.
3446 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3447 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3448 error = SET_ERROR(ENOTBLK);
3455 if ((error = vdev_open(vd)) == 0 &&
3456 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3457 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3458 vd->vdev_guid) == 0);
3464 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3471 sav->sav_pending = NULL;
3472 sav->sav_npending = 0;
3477 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3481 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3483 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3484 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3485 VDEV_LABEL_SPARE)) != 0) {
3489 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3490 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3491 VDEV_LABEL_L2CACHE));
3495 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3500 if (sav->sav_config != NULL) {
3506 * Generate new dev list by concatentating with the
3509 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3510 &olddevs, &oldndevs) == 0);
3512 newdevs = kmem_alloc(sizeof (void *) *
3513 (ndevs + oldndevs), KM_SLEEP);
3514 for (i = 0; i < oldndevs; i++)
3515 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3517 for (i = 0; i < ndevs; i++)
3518 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3521 VERIFY(nvlist_remove(sav->sav_config, config,
3522 DATA_TYPE_NVLIST_ARRAY) == 0);
3524 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3525 config, newdevs, ndevs + oldndevs) == 0);
3526 for (i = 0; i < oldndevs + ndevs; i++)
3527 nvlist_free(newdevs[i]);
3528 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3531 * Generate a new dev list.
3533 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3535 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3541 * Stop and drop level 2 ARC devices
3544 spa_l2cache_drop(spa_t *spa)
3548 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3550 for (i = 0; i < sav->sav_count; i++) {
3553 vd = sav->sav_vdevs[i];
3556 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3557 pool != 0ULL && l2arc_vdev_present(vd))
3558 l2arc_remove_vdev(vd);
3566 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3570 char *altroot = NULL;
3575 uint64_t txg = TXG_INITIAL;
3576 nvlist_t **spares, **l2cache;
3577 uint_t nspares, nl2cache;
3578 uint64_t version, obj;
3579 boolean_t has_features;
3582 * If this pool already exists, return failure.
3584 mutex_enter(&spa_namespace_lock);
3585 if (spa_lookup(pool) != NULL) {
3586 mutex_exit(&spa_namespace_lock);
3587 return (SET_ERROR(EEXIST));
3591 * Allocate a new spa_t structure.
3593 (void) nvlist_lookup_string(props,
3594 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3595 spa = spa_add(pool, NULL, altroot);
3596 spa_activate(spa, spa_mode_global);
3598 if (props && (error = spa_prop_validate(spa, props))) {
3599 spa_deactivate(spa);
3601 mutex_exit(&spa_namespace_lock);
3605 has_features = B_FALSE;
3606 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3607 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3608 if (zpool_prop_feature(nvpair_name(elem)))
3609 has_features = B_TRUE;
3612 if (has_features || nvlist_lookup_uint64(props,
3613 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3614 version = SPA_VERSION;
3616 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3618 spa->spa_first_txg = txg;
3619 spa->spa_uberblock.ub_txg = txg - 1;
3620 spa->spa_uberblock.ub_version = version;
3621 spa->spa_ubsync = spa->spa_uberblock;
3624 * Create "The Godfather" zio to hold all async IOs
3626 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3628 for (int i = 0; i < max_ncpus; i++) {
3629 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3630 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3631 ZIO_FLAG_GODFATHER);
3635 * Create the root vdev.
3637 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3639 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3641 ASSERT(error != 0 || rvd != NULL);
3642 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3644 if (error == 0 && !zfs_allocatable_devs(nvroot))
3645 error = SET_ERROR(EINVAL);
3648 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3649 (error = spa_validate_aux(spa, nvroot, txg,
3650 VDEV_ALLOC_ADD)) == 0) {
3651 for (int c = 0; c < rvd->vdev_children; c++) {
3652 vdev_ashift_optimize(rvd->vdev_child[c]);
3653 vdev_metaslab_set_size(rvd->vdev_child[c]);
3654 vdev_expand(rvd->vdev_child[c], txg);
3658 spa_config_exit(spa, SCL_ALL, FTAG);
3662 spa_deactivate(spa);
3664 mutex_exit(&spa_namespace_lock);
3669 * Get the list of spares, if specified.
3671 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3672 &spares, &nspares) == 0) {
3673 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3675 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3676 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3677 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3678 spa_load_spares(spa);
3679 spa_config_exit(spa, SCL_ALL, FTAG);
3680 spa->spa_spares.sav_sync = B_TRUE;
3684 * Get the list of level 2 cache devices, if specified.
3686 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3687 &l2cache, &nl2cache) == 0) {
3688 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3689 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3690 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3691 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3692 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3693 spa_load_l2cache(spa);
3694 spa_config_exit(spa, SCL_ALL, FTAG);
3695 spa->spa_l2cache.sav_sync = B_TRUE;
3698 spa->spa_is_initializing = B_TRUE;
3699 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3700 spa->spa_meta_objset = dp->dp_meta_objset;
3701 spa->spa_is_initializing = B_FALSE;
3704 * Create DDTs (dedup tables).
3708 spa_update_dspace(spa);
3710 tx = dmu_tx_create_assigned(dp, txg);
3713 * Create the pool config object.
3715 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3716 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3717 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3719 if (zap_add(spa->spa_meta_objset,
3720 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3721 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3722 cmn_err(CE_PANIC, "failed to add pool config");
3725 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3726 spa_feature_create_zap_objects(spa, tx);
3728 if (zap_add(spa->spa_meta_objset,
3729 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3730 sizeof (uint64_t), 1, &version, tx) != 0) {
3731 cmn_err(CE_PANIC, "failed to add pool version");
3734 /* Newly created pools with the right version are always deflated. */
3735 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3736 spa->spa_deflate = TRUE;
3737 if (zap_add(spa->spa_meta_objset,
3738 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3739 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3740 cmn_err(CE_PANIC, "failed to add deflate");
3745 * Create the deferred-free bpobj. Turn off compression
3746 * because sync-to-convergence takes longer if the blocksize
3749 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3750 dmu_object_set_compress(spa->spa_meta_objset, obj,
3751 ZIO_COMPRESS_OFF, tx);
3752 if (zap_add(spa->spa_meta_objset,
3753 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3754 sizeof (uint64_t), 1, &obj, tx) != 0) {
3755 cmn_err(CE_PANIC, "failed to add bpobj");
3757 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3758 spa->spa_meta_objset, obj));
3761 * Create the pool's history object.
3763 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3764 spa_history_create_obj(spa, tx);
3767 * Generate some random noise for salted checksums to operate on.
3769 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
3770 sizeof (spa->spa_cksum_salt.zcs_bytes));
3773 * Set pool properties.
3775 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3776 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3777 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3778 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3780 if (props != NULL) {
3781 spa_configfile_set(spa, props, B_FALSE);
3782 spa_sync_props(props, tx);
3787 spa->spa_sync_on = B_TRUE;
3788 txg_sync_start(spa->spa_dsl_pool);
3791 * We explicitly wait for the first transaction to complete so that our
3792 * bean counters are appropriately updated.
3794 txg_wait_synced(spa->spa_dsl_pool, txg);
3796 spa_config_sync(spa, B_FALSE, B_TRUE);
3797 spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE);
3799 spa_history_log_version(spa, "create");
3802 * Don't count references from objsets that are already closed
3803 * and are making their way through the eviction process.
3805 spa_evicting_os_wait(spa);
3806 spa->spa_minref = refcount_count(&spa->spa_refcount);
3808 mutex_exit(&spa_namespace_lock);
3816 * Get the root pool information from the root disk, then import the root pool
3817 * during the system boot up time.
3819 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3822 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3825 nvlist_t *nvtop, *nvroot;
3828 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3832 * Add this top-level vdev to the child array.
3834 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3836 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3838 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3841 * Put this pool's top-level vdevs into a root vdev.
3843 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3844 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3845 VDEV_TYPE_ROOT) == 0);
3846 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3847 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3848 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3852 * Replace the existing vdev_tree with the new root vdev in
3853 * this pool's configuration (remove the old, add the new).
3855 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3856 nvlist_free(nvroot);
3861 * Walk the vdev tree and see if we can find a device with "better"
3862 * configuration. A configuration is "better" if the label on that
3863 * device has a more recent txg.
3866 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3868 for (int c = 0; c < vd->vdev_children; c++)
3869 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3871 if (vd->vdev_ops->vdev_op_leaf) {
3875 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3879 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3883 * Do we have a better boot device?
3885 if (label_txg > *txg) {
3894 * Import a root pool.
3896 * For x86. devpath_list will consist of devid and/or physpath name of
3897 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3898 * The GRUB "findroot" command will return the vdev we should boot.
3900 * For Sparc, devpath_list consists the physpath name of the booting device
3901 * no matter the rootpool is a single device pool or a mirrored pool.
3903 * "/pci@1f,0/ide@d/disk@0,0:a"
3906 spa_import_rootpool(char *devpath, char *devid)
3909 vdev_t *rvd, *bvd, *avd = NULL;
3910 nvlist_t *config, *nvtop;
3916 * Read the label from the boot device and generate a configuration.
3918 config = spa_generate_rootconf(devpath, devid, &guid);
3919 #if defined(_OBP) && defined(_KERNEL)
3920 if (config == NULL) {
3921 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3923 get_iscsi_bootpath_phy(devpath);
3924 config = spa_generate_rootconf(devpath, devid, &guid);
3928 if (config == NULL) {
3929 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3931 return (SET_ERROR(EIO));
3934 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3936 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3938 mutex_enter(&spa_namespace_lock);
3939 if ((spa = spa_lookup(pname)) != NULL) {
3941 * Remove the existing root pool from the namespace so that we
3942 * can replace it with the correct config we just read in.
3947 spa = spa_add(pname, config, NULL);
3948 spa->spa_is_root = B_TRUE;
3949 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3952 * Build up a vdev tree based on the boot device's label config.
3954 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3956 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3957 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3958 VDEV_ALLOC_ROOTPOOL);
3959 spa_config_exit(spa, SCL_ALL, FTAG);
3961 mutex_exit(&spa_namespace_lock);
3962 nvlist_free(config);
3963 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3969 * Get the boot vdev.
3971 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3972 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3973 (u_longlong_t)guid);
3974 error = SET_ERROR(ENOENT);
3979 * Determine if there is a better boot device.
3982 spa_alt_rootvdev(rvd, &avd, &txg);
3984 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3985 "try booting from '%s'", avd->vdev_path);
3986 error = SET_ERROR(EINVAL);
3991 * If the boot device is part of a spare vdev then ensure that
3992 * we're booting off the active spare.
3994 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3995 !bvd->vdev_isspare) {
3996 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3997 "try booting from '%s'",
3999 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
4000 error = SET_ERROR(EINVAL);
4006 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4008 spa_config_exit(spa, SCL_ALL, FTAG);
4009 mutex_exit(&spa_namespace_lock);
4011 nvlist_free(config);
4015 #else /* !illumos */
4017 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
4021 spa_generate_rootconf(const char *name)
4023 nvlist_t **configs, **tops;
4025 nvlist_t *best_cfg, *nvtop, *nvroot;
4034 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
4037 ASSERT3U(count, !=, 0);
4039 for (i = 0; i < count; i++) {
4042 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
4044 if (txg > best_txg) {
4046 best_cfg = configs[i];
4051 * Multi-vdev root pool configuration discovery is not supported yet.
4054 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
4056 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
4059 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
4060 for (i = 0; i < nchildren; i++) {
4063 if (configs[i] == NULL)
4065 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
4067 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
4069 for (i = 0; holes != NULL && i < nholes; i++) {
4072 if (tops[holes[i]] != NULL)
4074 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
4075 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
4076 VDEV_TYPE_HOLE) == 0);
4077 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
4079 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4082 for (i = 0; i < nchildren; i++) {
4083 if (tops[i] != NULL)
4085 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4086 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4087 VDEV_TYPE_MISSING) == 0);
4088 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4090 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4095 * Create pool config based on the best vdev config.
4097 nvlist_dup(best_cfg, &config, KM_SLEEP);
4100 * Put this pool's top-level vdevs into a root vdev.
4102 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4104 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4105 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4106 VDEV_TYPE_ROOT) == 0);
4107 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4108 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4109 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4110 tops, nchildren) == 0);
4113 * Replace the existing vdev_tree with the new root vdev in
4114 * this pool's configuration (remove the old, add the new).
4116 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4119 * Drop vdev config elements that should not be present at pool level.
4121 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4122 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4124 for (i = 0; i < count; i++)
4125 nvlist_free(configs[i]);
4126 kmem_free(configs, count * sizeof(void *));
4127 for (i = 0; i < nchildren; i++)
4128 nvlist_free(tops[i]);
4129 kmem_free(tops, nchildren * sizeof(void *));
4130 nvlist_free(nvroot);
4135 spa_import_rootpool(const char *name)
4138 vdev_t *rvd, *bvd, *avd = NULL;
4139 nvlist_t *config, *nvtop;
4145 * Read the label from the boot device and generate a configuration.
4147 config = spa_generate_rootconf(name);
4149 mutex_enter(&spa_namespace_lock);
4150 if (config != NULL) {
4151 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4152 &pname) == 0 && strcmp(name, pname) == 0);
4153 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4156 if ((spa = spa_lookup(pname)) != NULL) {
4158 * Remove the existing root pool from the namespace so
4159 * that we can replace it with the correct config
4164 spa = spa_add(pname, config, NULL);
4167 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4168 * via spa_version().
4170 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4171 &spa->spa_ubsync.ub_version) != 0)
4172 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4173 } else if ((spa = spa_lookup(name)) == NULL) {
4174 mutex_exit(&spa_namespace_lock);
4175 nvlist_free(config);
4176 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4180 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4182 spa->spa_is_root = B_TRUE;
4183 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4186 * Build up a vdev tree based on the boot device's label config.
4188 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4190 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4191 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4192 VDEV_ALLOC_ROOTPOOL);
4193 spa_config_exit(spa, SCL_ALL, FTAG);
4195 mutex_exit(&spa_namespace_lock);
4196 nvlist_free(config);
4197 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4202 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4204 spa_config_exit(spa, SCL_ALL, FTAG);
4205 mutex_exit(&spa_namespace_lock);
4207 nvlist_free(config);
4211 #endif /* illumos */
4212 #endif /* _KERNEL */
4215 * Import a non-root pool into the system.
4218 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4221 char *altroot = NULL;
4222 spa_load_state_t state = SPA_LOAD_IMPORT;
4223 zpool_rewind_policy_t policy;
4224 uint64_t mode = spa_mode_global;
4225 uint64_t readonly = B_FALSE;
4228 nvlist_t **spares, **l2cache;
4229 uint_t nspares, nl2cache;
4232 * If a pool with this name exists, return failure.
4234 mutex_enter(&spa_namespace_lock);
4235 if (spa_lookup(pool) != NULL) {
4236 mutex_exit(&spa_namespace_lock);
4237 return (SET_ERROR(EEXIST));
4241 * Create and initialize the spa structure.
4243 (void) nvlist_lookup_string(props,
4244 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4245 (void) nvlist_lookup_uint64(props,
4246 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4249 spa = spa_add(pool, config, altroot);
4250 spa->spa_import_flags = flags;
4253 * Verbatim import - Take a pool and insert it into the namespace
4254 * as if it had been loaded at boot.
4256 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4258 spa_configfile_set(spa, props, B_FALSE);
4260 spa_config_sync(spa, B_FALSE, B_TRUE);
4261 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4263 mutex_exit(&spa_namespace_lock);
4267 spa_activate(spa, mode);
4270 * Don't start async tasks until we know everything is healthy.
4272 spa_async_suspend(spa);
4274 zpool_get_rewind_policy(config, &policy);
4275 if (policy.zrp_request & ZPOOL_DO_REWIND)
4276 state = SPA_LOAD_RECOVER;
4279 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4280 * because the user-supplied config is actually the one to trust when
4283 if (state != SPA_LOAD_RECOVER)
4284 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4286 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4287 policy.zrp_request);
4290 * Propagate anything learned while loading the pool and pass it
4291 * back to caller (i.e. rewind info, missing devices, etc).
4293 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4294 spa->spa_load_info) == 0);
4296 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4298 * Toss any existing sparelist, as it doesn't have any validity
4299 * anymore, and conflicts with spa_has_spare().
4301 if (spa->spa_spares.sav_config) {
4302 nvlist_free(spa->spa_spares.sav_config);
4303 spa->spa_spares.sav_config = NULL;
4304 spa_load_spares(spa);
4306 if (spa->spa_l2cache.sav_config) {
4307 nvlist_free(spa->spa_l2cache.sav_config);
4308 spa->spa_l2cache.sav_config = NULL;
4309 spa_load_l2cache(spa);
4312 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4315 error = spa_validate_aux(spa, nvroot, -1ULL,
4318 error = spa_validate_aux(spa, nvroot, -1ULL,
4319 VDEV_ALLOC_L2CACHE);
4320 spa_config_exit(spa, SCL_ALL, FTAG);
4323 spa_configfile_set(spa, props, B_FALSE);
4325 if (error != 0 || (props && spa_writeable(spa) &&
4326 (error = spa_prop_set(spa, props)))) {
4328 spa_deactivate(spa);
4330 mutex_exit(&spa_namespace_lock);
4334 spa_async_resume(spa);
4337 * Override any spares and level 2 cache devices as specified by
4338 * the user, as these may have correct device names/devids, etc.
4340 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4341 &spares, &nspares) == 0) {
4342 if (spa->spa_spares.sav_config)
4343 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4344 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4346 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4347 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4348 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4349 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4350 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4351 spa_load_spares(spa);
4352 spa_config_exit(spa, SCL_ALL, FTAG);
4353 spa->spa_spares.sav_sync = B_TRUE;
4355 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4356 &l2cache, &nl2cache) == 0) {
4357 if (spa->spa_l2cache.sav_config)
4358 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4359 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4361 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4362 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4363 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4364 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4365 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4366 spa_load_l2cache(spa);
4367 spa_config_exit(spa, SCL_ALL, FTAG);
4368 spa->spa_l2cache.sav_sync = B_TRUE;
4372 * Check for any removed devices.
4374 if (spa->spa_autoreplace) {
4375 spa_aux_check_removed(&spa->spa_spares);
4376 spa_aux_check_removed(&spa->spa_l2cache);
4379 if (spa_writeable(spa)) {
4381 * Update the config cache to include the newly-imported pool.
4383 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4387 * It's possible that the pool was expanded while it was exported.
4388 * We kick off an async task to handle this for us.
4390 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4392 spa_history_log_version(spa, "import");
4394 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4396 mutex_exit(&spa_namespace_lock);
4400 zvol_create_minors(pool);
4407 spa_tryimport(nvlist_t *tryconfig)
4409 nvlist_t *config = NULL;
4415 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4418 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4422 * Create and initialize the spa structure.
4424 mutex_enter(&spa_namespace_lock);
4425 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4426 spa_activate(spa, FREAD);
4429 * Pass off the heavy lifting to spa_load().
4430 * Pass TRUE for mosconfig because the user-supplied config
4431 * is actually the one to trust when doing an import.
4433 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4436 * If 'tryconfig' was at least parsable, return the current config.
4438 if (spa->spa_root_vdev != NULL) {
4439 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4440 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4442 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4444 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4445 spa->spa_uberblock.ub_timestamp) == 0);
4446 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4447 spa->spa_load_info) == 0);
4450 * If the bootfs property exists on this pool then we
4451 * copy it out so that external consumers can tell which
4452 * pools are bootable.
4454 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4455 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4458 * We have to play games with the name since the
4459 * pool was opened as TRYIMPORT_NAME.
4461 if (dsl_dsobj_to_dsname(spa_name(spa),
4462 spa->spa_bootfs, tmpname) == 0) {
4464 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4466 cp = strchr(tmpname, '/');
4468 (void) strlcpy(dsname, tmpname,
4471 (void) snprintf(dsname, MAXPATHLEN,
4472 "%s/%s", poolname, ++cp);
4474 VERIFY(nvlist_add_string(config,
4475 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4476 kmem_free(dsname, MAXPATHLEN);
4478 kmem_free(tmpname, MAXPATHLEN);
4482 * Add the list of hot spares and level 2 cache devices.
4484 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4485 spa_add_spares(spa, config);
4486 spa_add_l2cache(spa, config);
4487 spa_config_exit(spa, SCL_CONFIG, FTAG);
4491 spa_deactivate(spa);
4493 mutex_exit(&spa_namespace_lock);
4499 * Pool export/destroy
4501 * The act of destroying or exporting a pool is very simple. We make sure there
4502 * is no more pending I/O and any references to the pool are gone. Then, we
4503 * update the pool state and sync all the labels to disk, removing the
4504 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4505 * we don't sync the labels or remove the configuration cache.
4508 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4509 boolean_t force, boolean_t hardforce)
4516 if (!(spa_mode_global & FWRITE))
4517 return (SET_ERROR(EROFS));
4519 mutex_enter(&spa_namespace_lock);
4520 if ((spa = spa_lookup(pool)) == NULL) {
4521 mutex_exit(&spa_namespace_lock);
4522 return (SET_ERROR(ENOENT));
4526 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4527 * reacquire the namespace lock, and see if we can export.
4529 spa_open_ref(spa, FTAG);
4530 mutex_exit(&spa_namespace_lock);
4531 spa_async_suspend(spa);
4532 mutex_enter(&spa_namespace_lock);
4533 spa_close(spa, FTAG);
4536 * The pool will be in core if it's openable,
4537 * in which case we can modify its state.
4539 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4541 * Objsets may be open only because they're dirty, so we
4542 * have to force it to sync before checking spa_refcnt.
4544 txg_wait_synced(spa->spa_dsl_pool, 0);
4545 spa_evicting_os_wait(spa);
4548 * A pool cannot be exported or destroyed if there are active
4549 * references. If we are resetting a pool, allow references by
4550 * fault injection handlers.
4552 if (!spa_refcount_zero(spa) ||
4553 (spa->spa_inject_ref != 0 &&
4554 new_state != POOL_STATE_UNINITIALIZED)) {
4555 spa_async_resume(spa);
4556 mutex_exit(&spa_namespace_lock);
4557 return (SET_ERROR(EBUSY));
4561 * A pool cannot be exported if it has an active shared spare.
4562 * This is to prevent other pools stealing the active spare
4563 * from an exported pool. At user's own will, such pool can
4564 * be forcedly exported.
4566 if (!force && new_state == POOL_STATE_EXPORTED &&
4567 spa_has_active_shared_spare(spa)) {
4568 spa_async_resume(spa);
4569 mutex_exit(&spa_namespace_lock);
4570 return (SET_ERROR(EXDEV));
4574 * We want this to be reflected on every label,
4575 * so mark them all dirty. spa_unload() will do the
4576 * final sync that pushes these changes out.
4578 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4579 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4580 spa->spa_state = new_state;
4581 spa->spa_final_txg = spa_last_synced_txg(spa) +
4583 vdev_config_dirty(spa->spa_root_vdev);
4584 spa_config_exit(spa, SCL_ALL, FTAG);
4588 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4590 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4592 spa_deactivate(spa);
4595 if (oldconfig && spa->spa_config)
4596 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4598 if (new_state != POOL_STATE_UNINITIALIZED) {
4600 spa_config_sync(spa, B_TRUE, B_TRUE);
4603 mutex_exit(&spa_namespace_lock);
4609 * Destroy a storage pool.
4612 spa_destroy(char *pool)
4614 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4619 * Export a storage pool.
4622 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4623 boolean_t hardforce)
4625 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4630 * Similar to spa_export(), this unloads the spa_t without actually removing it
4631 * from the namespace in any way.
4634 spa_reset(char *pool)
4636 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4641 * ==========================================================================
4642 * Device manipulation
4643 * ==========================================================================
4647 * Add a device to a storage pool.
4650 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4654 vdev_t *rvd = spa->spa_root_vdev;
4656 nvlist_t **spares, **l2cache;
4657 uint_t nspares, nl2cache;
4659 ASSERT(spa_writeable(spa));
4661 txg = spa_vdev_enter(spa);
4663 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4664 VDEV_ALLOC_ADD)) != 0)
4665 return (spa_vdev_exit(spa, NULL, txg, error));
4667 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4669 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4673 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4677 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4678 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4680 if (vd->vdev_children != 0 &&
4681 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4682 return (spa_vdev_exit(spa, vd, txg, error));
4685 * We must validate the spares and l2cache devices after checking the
4686 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4688 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4689 return (spa_vdev_exit(spa, vd, txg, error));
4692 * Transfer each new top-level vdev from vd to rvd.
4694 for (int c = 0; c < vd->vdev_children; c++) {
4697 * Set the vdev id to the first hole, if one exists.
4699 for (id = 0; id < rvd->vdev_children; id++) {
4700 if (rvd->vdev_child[id]->vdev_ishole) {
4701 vdev_free(rvd->vdev_child[id]);
4705 tvd = vd->vdev_child[c];
4706 vdev_remove_child(vd, tvd);
4708 vdev_add_child(rvd, tvd);
4709 vdev_config_dirty(tvd);
4713 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4714 ZPOOL_CONFIG_SPARES);
4715 spa_load_spares(spa);
4716 spa->spa_spares.sav_sync = B_TRUE;
4719 if (nl2cache != 0) {
4720 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4721 ZPOOL_CONFIG_L2CACHE);
4722 spa_load_l2cache(spa);
4723 spa->spa_l2cache.sav_sync = B_TRUE;
4727 * We have to be careful when adding new vdevs to an existing pool.
4728 * If other threads start allocating from these vdevs before we
4729 * sync the config cache, and we lose power, then upon reboot we may
4730 * fail to open the pool because there are DVAs that the config cache
4731 * can't translate. Therefore, we first add the vdevs without
4732 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4733 * and then let spa_config_update() initialize the new metaslabs.
4735 * spa_load() checks for added-but-not-initialized vdevs, so that
4736 * if we lose power at any point in this sequence, the remaining
4737 * steps will be completed the next time we load the pool.
4739 (void) spa_vdev_exit(spa, vd, txg, 0);
4741 mutex_enter(&spa_namespace_lock);
4742 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4743 spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD);
4744 mutex_exit(&spa_namespace_lock);
4750 * Attach a device to a mirror. The arguments are the path to any device
4751 * in the mirror, and the nvroot for the new device. If the path specifies
4752 * a device that is not mirrored, we automatically insert the mirror vdev.
4754 * If 'replacing' is specified, the new device is intended to replace the
4755 * existing device; in this case the two devices are made into their own
4756 * mirror using the 'replacing' vdev, which is functionally identical to
4757 * the mirror vdev (it actually reuses all the same ops) but has a few
4758 * extra rules: you can't attach to it after it's been created, and upon
4759 * completion of resilvering, the first disk (the one being replaced)
4760 * is automatically detached.
4763 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4765 uint64_t txg, dtl_max_txg;
4766 vdev_t *rvd = spa->spa_root_vdev;
4767 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4769 char *oldvdpath, *newvdpath;
4773 ASSERT(spa_writeable(spa));
4775 txg = spa_vdev_enter(spa);
4777 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4780 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4782 if (!oldvd->vdev_ops->vdev_op_leaf)
4783 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4785 pvd = oldvd->vdev_parent;
4787 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4788 VDEV_ALLOC_ATTACH)) != 0)
4789 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4791 if (newrootvd->vdev_children != 1)
4792 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4794 newvd = newrootvd->vdev_child[0];
4796 if (!newvd->vdev_ops->vdev_op_leaf)
4797 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4799 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4800 return (spa_vdev_exit(spa, newrootvd, txg, error));
4803 * Spares can't replace logs
4805 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4806 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4810 * For attach, the only allowable parent is a mirror or the root
4813 if (pvd->vdev_ops != &vdev_mirror_ops &&
4814 pvd->vdev_ops != &vdev_root_ops)
4815 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4817 pvops = &vdev_mirror_ops;
4820 * Active hot spares can only be replaced by inactive hot
4823 if (pvd->vdev_ops == &vdev_spare_ops &&
4824 oldvd->vdev_isspare &&
4825 !spa_has_spare(spa, newvd->vdev_guid))
4826 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4829 * If the source is a hot spare, and the parent isn't already a
4830 * spare, then we want to create a new hot spare. Otherwise, we
4831 * want to create a replacing vdev. The user is not allowed to
4832 * attach to a spared vdev child unless the 'isspare' state is
4833 * the same (spare replaces spare, non-spare replaces
4836 if (pvd->vdev_ops == &vdev_replacing_ops &&
4837 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4838 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4839 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4840 newvd->vdev_isspare != oldvd->vdev_isspare) {
4841 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4844 if (newvd->vdev_isspare)
4845 pvops = &vdev_spare_ops;
4847 pvops = &vdev_replacing_ops;
4851 * Make sure the new device is big enough.
4853 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4854 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4857 * The new device cannot have a higher alignment requirement
4858 * than the top-level vdev.
4860 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4861 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4864 * If this is an in-place replacement, update oldvd's path and devid
4865 * to make it distinguishable from newvd, and unopenable from now on.
4867 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4868 spa_strfree(oldvd->vdev_path);
4869 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4871 (void) sprintf(oldvd->vdev_path, "%s/%s",
4872 newvd->vdev_path, "old");
4873 if (oldvd->vdev_devid != NULL) {
4874 spa_strfree(oldvd->vdev_devid);
4875 oldvd->vdev_devid = NULL;
4879 /* mark the device being resilvered */
4880 newvd->vdev_resilver_txg = txg;
4883 * If the parent is not a mirror, or if we're replacing, insert the new
4884 * mirror/replacing/spare vdev above oldvd.
4886 if (pvd->vdev_ops != pvops)
4887 pvd = vdev_add_parent(oldvd, pvops);
4889 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4890 ASSERT(pvd->vdev_ops == pvops);
4891 ASSERT(oldvd->vdev_parent == pvd);
4894 * Extract the new device from its root and add it to pvd.
4896 vdev_remove_child(newrootvd, newvd);
4897 newvd->vdev_id = pvd->vdev_children;
4898 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4899 vdev_add_child(pvd, newvd);
4901 tvd = newvd->vdev_top;
4902 ASSERT(pvd->vdev_top == tvd);
4903 ASSERT(tvd->vdev_parent == rvd);
4905 vdev_config_dirty(tvd);
4908 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4909 * for any dmu_sync-ed blocks. It will propagate upward when
4910 * spa_vdev_exit() calls vdev_dtl_reassess().
4912 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4914 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4915 dtl_max_txg - TXG_INITIAL);
4917 if (newvd->vdev_isspare) {
4918 spa_spare_activate(newvd);
4919 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4922 oldvdpath = spa_strdup(oldvd->vdev_path);
4923 newvdpath = spa_strdup(newvd->vdev_path);
4924 newvd_isspare = newvd->vdev_isspare;
4927 * Mark newvd's DTL dirty in this txg.
4929 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4932 * Schedule the resilver to restart in the future. We do this to
4933 * ensure that dmu_sync-ed blocks have been stitched into the
4934 * respective datasets.
4936 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4938 if (spa->spa_bootfs)
4939 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4941 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH);
4946 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4948 spa_history_log_internal(spa, "vdev attach", NULL,
4949 "%s vdev=%s %s vdev=%s",
4950 replacing && newvd_isspare ? "spare in" :
4951 replacing ? "replace" : "attach", newvdpath,
4952 replacing ? "for" : "to", oldvdpath);
4954 spa_strfree(oldvdpath);
4955 spa_strfree(newvdpath);
4961 * Detach a device from a mirror or replacing vdev.
4963 * If 'replace_done' is specified, only detach if the parent
4964 * is a replacing vdev.
4967 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4971 vdev_t *rvd = spa->spa_root_vdev;
4972 vdev_t *vd, *pvd, *cvd, *tvd;
4973 boolean_t unspare = B_FALSE;
4974 uint64_t unspare_guid = 0;
4977 ASSERT(spa_writeable(spa));
4979 txg = spa_vdev_enter(spa);
4981 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4984 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4986 if (!vd->vdev_ops->vdev_op_leaf)
4987 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4989 pvd = vd->vdev_parent;
4992 * If the parent/child relationship is not as expected, don't do it.
4993 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4994 * vdev that's replacing B with C. The user's intent in replacing
4995 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4996 * the replace by detaching C, the expected behavior is to end up
4997 * M(A,B). But suppose that right after deciding to detach C,
4998 * the replacement of B completes. We would have M(A,C), and then
4999 * ask to detach C, which would leave us with just A -- not what
5000 * the user wanted. To prevent this, we make sure that the
5001 * parent/child relationship hasn't changed -- in this example,
5002 * that C's parent is still the replacing vdev R.
5004 if (pvd->vdev_guid != pguid && pguid != 0)
5005 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5008 * Only 'replacing' or 'spare' vdevs can be replaced.
5010 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
5011 pvd->vdev_ops != &vdev_spare_ops)
5012 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5014 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
5015 spa_version(spa) >= SPA_VERSION_SPARES);
5018 * Only mirror, replacing, and spare vdevs support detach.
5020 if (pvd->vdev_ops != &vdev_replacing_ops &&
5021 pvd->vdev_ops != &vdev_mirror_ops &&
5022 pvd->vdev_ops != &vdev_spare_ops)
5023 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5026 * If this device has the only valid copy of some data,
5027 * we cannot safely detach it.
5029 if (vdev_dtl_required(vd))
5030 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5032 ASSERT(pvd->vdev_children >= 2);
5035 * If we are detaching the second disk from a replacing vdev, then
5036 * check to see if we changed the original vdev's path to have "/old"
5037 * at the end in spa_vdev_attach(). If so, undo that change now.
5039 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
5040 vd->vdev_path != NULL) {
5041 size_t len = strlen(vd->vdev_path);
5043 for (int c = 0; c < pvd->vdev_children; c++) {
5044 cvd = pvd->vdev_child[c];
5046 if (cvd == vd || cvd->vdev_path == NULL)
5049 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
5050 strcmp(cvd->vdev_path + len, "/old") == 0) {
5051 spa_strfree(cvd->vdev_path);
5052 cvd->vdev_path = spa_strdup(vd->vdev_path);
5059 * If we are detaching the original disk from a spare, then it implies
5060 * that the spare should become a real disk, and be removed from the
5061 * active spare list for the pool.
5063 if (pvd->vdev_ops == &vdev_spare_ops &&
5065 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
5069 * Erase the disk labels so the disk can be used for other things.
5070 * This must be done after all other error cases are handled,
5071 * but before we disembowel vd (so we can still do I/O to it).
5072 * But if we can't do it, don't treat the error as fatal --
5073 * it may be that the unwritability of the disk is the reason
5074 * it's being detached!
5076 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5079 * Remove vd from its parent and compact the parent's children.
5081 vdev_remove_child(pvd, vd);
5082 vdev_compact_children(pvd);
5085 * Remember one of the remaining children so we can get tvd below.
5087 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5090 * If we need to remove the remaining child from the list of hot spares,
5091 * do it now, marking the vdev as no longer a spare in the process.
5092 * We must do this before vdev_remove_parent(), because that can
5093 * change the GUID if it creates a new toplevel GUID. For a similar
5094 * reason, we must remove the spare now, in the same txg as the detach;
5095 * otherwise someone could attach a new sibling, change the GUID, and
5096 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5099 ASSERT(cvd->vdev_isspare);
5100 spa_spare_remove(cvd);
5101 unspare_guid = cvd->vdev_guid;
5102 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5103 cvd->vdev_unspare = B_TRUE;
5107 * If the parent mirror/replacing vdev only has one child,
5108 * the parent is no longer needed. Remove it from the tree.
5110 if (pvd->vdev_children == 1) {
5111 if (pvd->vdev_ops == &vdev_spare_ops)
5112 cvd->vdev_unspare = B_FALSE;
5113 vdev_remove_parent(cvd);
5118 * We don't set tvd until now because the parent we just removed
5119 * may have been the previous top-level vdev.
5121 tvd = cvd->vdev_top;
5122 ASSERT(tvd->vdev_parent == rvd);
5125 * Reevaluate the parent vdev state.
5127 vdev_propagate_state(cvd);
5130 * If the 'autoexpand' property is set on the pool then automatically
5131 * try to expand the size of the pool. For example if the device we
5132 * just detached was smaller than the others, it may be possible to
5133 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5134 * first so that we can obtain the updated sizes of the leaf vdevs.
5136 if (spa->spa_autoexpand) {
5138 vdev_expand(tvd, txg);
5141 vdev_config_dirty(tvd);
5144 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5145 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5146 * But first make sure we're not on any *other* txg's DTL list, to
5147 * prevent vd from being accessed after it's freed.
5149 vdpath = spa_strdup(vd->vdev_path);
5150 for (int t = 0; t < TXG_SIZE; t++)
5151 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5152 vd->vdev_detached = B_TRUE;
5153 vdev_dirty(tvd, VDD_DTL, vd, txg);
5155 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5157 /* hang on to the spa before we release the lock */
5158 spa_open_ref(spa, FTAG);
5160 error = spa_vdev_exit(spa, vd, txg, 0);
5162 spa_history_log_internal(spa, "detach", NULL,
5164 spa_strfree(vdpath);
5167 * If this was the removal of the original device in a hot spare vdev,
5168 * then we want to go through and remove the device from the hot spare
5169 * list of every other pool.
5172 spa_t *altspa = NULL;
5174 mutex_enter(&spa_namespace_lock);
5175 while ((altspa = spa_next(altspa)) != NULL) {
5176 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5180 spa_open_ref(altspa, FTAG);
5181 mutex_exit(&spa_namespace_lock);
5182 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5183 mutex_enter(&spa_namespace_lock);
5184 spa_close(altspa, FTAG);
5186 mutex_exit(&spa_namespace_lock);
5188 /* search the rest of the vdevs for spares to remove */
5189 spa_vdev_resilver_done(spa);
5192 /* all done with the spa; OK to release */
5193 mutex_enter(&spa_namespace_lock);
5194 spa_close(spa, FTAG);
5195 mutex_exit(&spa_namespace_lock);
5201 * Split a set of devices from their mirrors, and create a new pool from them.
5204 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5205 nvlist_t *props, boolean_t exp)
5208 uint64_t txg, *glist;
5210 uint_t c, children, lastlog;
5211 nvlist_t **child, *nvl, *tmp;
5213 char *altroot = NULL;
5214 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5215 boolean_t activate_slog;
5217 ASSERT(spa_writeable(spa));
5219 txg = spa_vdev_enter(spa);
5221 /* clear the log and flush everything up to now */
5222 activate_slog = spa_passivate_log(spa);
5223 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5224 error = spa_offline_log(spa);
5225 txg = spa_vdev_config_enter(spa);
5228 spa_activate_log(spa);
5231 return (spa_vdev_exit(spa, NULL, txg, error));
5233 /* check new spa name before going any further */
5234 if (spa_lookup(newname) != NULL)
5235 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5238 * scan through all the children to ensure they're all mirrors
5240 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5241 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5243 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5245 /* first, check to ensure we've got the right child count */
5246 rvd = spa->spa_root_vdev;
5248 for (c = 0; c < rvd->vdev_children; c++) {
5249 vdev_t *vd = rvd->vdev_child[c];
5251 /* don't count the holes & logs as children */
5252 if (vd->vdev_islog || vd->vdev_ishole) {
5260 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5261 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5263 /* next, ensure no spare or cache devices are part of the split */
5264 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5265 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5266 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5268 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5269 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5271 /* then, loop over each vdev and validate it */
5272 for (c = 0; c < children; c++) {
5273 uint64_t is_hole = 0;
5275 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5279 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5280 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5283 error = SET_ERROR(EINVAL);
5288 /* which disk is going to be split? */
5289 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5291 error = SET_ERROR(EINVAL);
5295 /* look it up in the spa */
5296 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5297 if (vml[c] == NULL) {
5298 error = SET_ERROR(ENODEV);
5302 /* make sure there's nothing stopping the split */
5303 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5304 vml[c]->vdev_islog ||
5305 vml[c]->vdev_ishole ||
5306 vml[c]->vdev_isspare ||
5307 vml[c]->vdev_isl2cache ||
5308 !vdev_writeable(vml[c]) ||
5309 vml[c]->vdev_children != 0 ||
5310 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5311 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5312 error = SET_ERROR(EINVAL);
5316 if (vdev_dtl_required(vml[c])) {
5317 error = SET_ERROR(EBUSY);
5321 /* we need certain info from the top level */
5322 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5323 vml[c]->vdev_top->vdev_ms_array) == 0);
5324 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5325 vml[c]->vdev_top->vdev_ms_shift) == 0);
5326 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5327 vml[c]->vdev_top->vdev_asize) == 0);
5328 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5329 vml[c]->vdev_top->vdev_ashift) == 0);
5333 kmem_free(vml, children * sizeof (vdev_t *));
5334 kmem_free(glist, children * sizeof (uint64_t));
5335 return (spa_vdev_exit(spa, NULL, txg, error));
5338 /* stop writers from using the disks */
5339 for (c = 0; c < children; c++) {
5341 vml[c]->vdev_offline = B_TRUE;
5343 vdev_reopen(spa->spa_root_vdev);
5346 * Temporarily record the splitting vdevs in the spa config. This
5347 * will disappear once the config is regenerated.
5349 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5350 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5351 glist, children) == 0);
5352 kmem_free(glist, children * sizeof (uint64_t));
5354 mutex_enter(&spa->spa_props_lock);
5355 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5357 mutex_exit(&spa->spa_props_lock);
5358 spa->spa_config_splitting = nvl;
5359 vdev_config_dirty(spa->spa_root_vdev);
5361 /* configure and create the new pool */
5362 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5363 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5364 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5365 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5366 spa_version(spa)) == 0);
5367 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5368 spa->spa_config_txg) == 0);
5369 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5370 spa_generate_guid(NULL)) == 0);
5371 (void) nvlist_lookup_string(props,
5372 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5374 /* add the new pool to the namespace */
5375 newspa = spa_add(newname, config, altroot);
5376 newspa->spa_config_txg = spa->spa_config_txg;
5377 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5379 /* release the spa config lock, retaining the namespace lock */
5380 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5382 if (zio_injection_enabled)
5383 zio_handle_panic_injection(spa, FTAG, 1);
5385 spa_activate(newspa, spa_mode_global);
5386 spa_async_suspend(newspa);
5389 /* mark that we are creating new spa by splitting */
5390 newspa->spa_splitting_newspa = B_TRUE;
5392 /* create the new pool from the disks of the original pool */
5393 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5395 newspa->spa_splitting_newspa = B_FALSE;
5400 /* if that worked, generate a real config for the new pool */
5401 if (newspa->spa_root_vdev != NULL) {
5402 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5403 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5404 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5405 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5406 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5411 if (props != NULL) {
5412 spa_configfile_set(newspa, props, B_FALSE);
5413 error = spa_prop_set(newspa, props);
5418 /* flush everything */
5419 txg = spa_vdev_config_enter(newspa);
5420 vdev_config_dirty(newspa->spa_root_vdev);
5421 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5423 if (zio_injection_enabled)
5424 zio_handle_panic_injection(spa, FTAG, 2);
5426 spa_async_resume(newspa);
5428 /* finally, update the original pool's config */
5429 txg = spa_vdev_config_enter(spa);
5430 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5431 error = dmu_tx_assign(tx, TXG_WAIT);
5434 for (c = 0; c < children; c++) {
5435 if (vml[c] != NULL) {
5438 spa_history_log_internal(spa, "detach", tx,
5439 "vdev=%s", vml[c]->vdev_path);
5443 vdev_config_dirty(spa->spa_root_vdev);
5444 spa->spa_config_splitting = NULL;
5448 (void) spa_vdev_exit(spa, NULL, txg, 0);
5450 if (zio_injection_enabled)
5451 zio_handle_panic_injection(spa, FTAG, 3);
5453 /* split is complete; log a history record */
5454 spa_history_log_internal(newspa, "split", NULL,
5455 "from pool %s", spa_name(spa));
5457 kmem_free(vml, children * sizeof (vdev_t *));
5459 /* if we're not going to mount the filesystems in userland, export */
5461 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5468 spa_deactivate(newspa);
5471 txg = spa_vdev_config_enter(spa);
5473 /* re-online all offlined disks */
5474 for (c = 0; c < children; c++) {
5476 vml[c]->vdev_offline = B_FALSE;
5478 vdev_reopen(spa->spa_root_vdev);
5480 nvlist_free(spa->spa_config_splitting);
5481 spa->spa_config_splitting = NULL;
5482 (void) spa_vdev_exit(spa, NULL, txg, error);
5484 kmem_free(vml, children * sizeof (vdev_t *));
5489 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5491 for (int i = 0; i < count; i++) {
5494 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5497 if (guid == target_guid)
5505 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5506 nvlist_t *dev_to_remove)
5508 nvlist_t **newdev = NULL;
5511 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5513 for (int i = 0, j = 0; i < count; i++) {
5514 if (dev[i] == dev_to_remove)
5516 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5519 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5520 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5522 for (int i = 0; i < count - 1; i++)
5523 nvlist_free(newdev[i]);
5526 kmem_free(newdev, (count - 1) * sizeof (void *));
5530 * Evacuate the device.
5533 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5538 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5539 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5540 ASSERT(vd == vd->vdev_top);
5543 * Evacuate the device. We don't hold the config lock as writer
5544 * since we need to do I/O but we do keep the
5545 * spa_namespace_lock held. Once this completes the device
5546 * should no longer have any blocks allocated on it.
5548 if (vd->vdev_islog) {
5549 if (vd->vdev_stat.vs_alloc != 0)
5550 error = spa_offline_log(spa);
5552 error = SET_ERROR(ENOTSUP);
5559 * The evacuation succeeded. Remove any remaining MOS metadata
5560 * associated with this vdev, and wait for these changes to sync.
5562 ASSERT0(vd->vdev_stat.vs_alloc);
5563 txg = spa_vdev_config_enter(spa);
5564 vd->vdev_removing = B_TRUE;
5565 vdev_dirty_leaves(vd, VDD_DTL, txg);
5566 vdev_config_dirty(vd);
5567 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5573 * Complete the removal by cleaning up the namespace.
5576 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5578 vdev_t *rvd = spa->spa_root_vdev;
5579 uint64_t id = vd->vdev_id;
5580 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5582 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5583 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5584 ASSERT(vd == vd->vdev_top);
5587 * Only remove any devices which are empty.
5589 if (vd->vdev_stat.vs_alloc != 0)
5592 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5594 if (list_link_active(&vd->vdev_state_dirty_node))
5595 vdev_state_clean(vd);
5596 if (list_link_active(&vd->vdev_config_dirty_node))
5597 vdev_config_clean(vd);
5602 vdev_compact_children(rvd);
5604 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5605 vdev_add_child(rvd, vd);
5607 vdev_config_dirty(rvd);
5610 * Reassess the health of our root vdev.
5616 * Remove a device from the pool -
5618 * Removing a device from the vdev namespace requires several steps
5619 * and can take a significant amount of time. As a result we use
5620 * the spa_vdev_config_[enter/exit] functions which allow us to
5621 * grab and release the spa_config_lock while still holding the namespace
5622 * lock. During each step the configuration is synced out.
5624 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5628 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5631 metaslab_group_t *mg;
5632 nvlist_t **spares, **l2cache, *nv;
5634 uint_t nspares, nl2cache;
5636 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5638 ASSERT(spa_writeable(spa));
5641 txg = spa_vdev_enter(spa);
5643 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5645 if (spa->spa_spares.sav_vdevs != NULL &&
5646 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5647 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5648 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5650 * Only remove the hot spare if it's not currently in use
5653 if (vd == NULL || unspare) {
5654 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5655 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5656 spa_load_spares(spa);
5657 spa->spa_spares.sav_sync = B_TRUE;
5659 error = SET_ERROR(EBUSY);
5661 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5662 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5663 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5664 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5666 * Cache devices can always be removed.
5668 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5669 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5670 spa_load_l2cache(spa);
5671 spa->spa_l2cache.sav_sync = B_TRUE;
5672 } else if (vd != NULL && vd->vdev_islog) {
5674 ASSERT(vd == vd->vdev_top);
5679 * Stop allocating from this vdev.
5681 metaslab_group_passivate(mg);
5684 * Wait for the youngest allocations and frees to sync,
5685 * and then wait for the deferral of those frees to finish.
5687 spa_vdev_config_exit(spa, NULL,
5688 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5691 * Attempt to evacuate the vdev.
5693 error = spa_vdev_remove_evacuate(spa, vd);
5695 txg = spa_vdev_config_enter(spa);
5698 * If we couldn't evacuate the vdev, unwind.
5701 metaslab_group_activate(mg);
5702 return (spa_vdev_exit(spa, NULL, txg, error));
5706 * Clean up the vdev namespace.
5708 spa_vdev_remove_from_namespace(spa, vd);
5710 } else if (vd != NULL) {
5712 * Normal vdevs cannot be removed (yet).
5714 error = SET_ERROR(ENOTSUP);
5717 * There is no vdev of any kind with the specified guid.
5719 error = SET_ERROR(ENOENT);
5723 return (spa_vdev_exit(spa, NULL, txg, error));
5729 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5730 * currently spared, so we can detach it.
5733 spa_vdev_resilver_done_hunt(vdev_t *vd)
5735 vdev_t *newvd, *oldvd;
5737 for (int c = 0; c < vd->vdev_children; c++) {
5738 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5744 * Check for a completed replacement. We always consider the first
5745 * vdev in the list to be the oldest vdev, and the last one to be
5746 * the newest (see spa_vdev_attach() for how that works). In
5747 * the case where the newest vdev is faulted, we will not automatically
5748 * remove it after a resilver completes. This is OK as it will require
5749 * user intervention to determine which disk the admin wishes to keep.
5751 if (vd->vdev_ops == &vdev_replacing_ops) {
5752 ASSERT(vd->vdev_children > 1);
5754 newvd = vd->vdev_child[vd->vdev_children - 1];
5755 oldvd = vd->vdev_child[0];
5757 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5758 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5759 !vdev_dtl_required(oldvd))
5764 * Check for a completed resilver with the 'unspare' flag set.
5766 if (vd->vdev_ops == &vdev_spare_ops) {
5767 vdev_t *first = vd->vdev_child[0];
5768 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5770 if (last->vdev_unspare) {
5773 } else if (first->vdev_unspare) {
5780 if (oldvd != NULL &&
5781 vdev_dtl_empty(newvd, DTL_MISSING) &&
5782 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5783 !vdev_dtl_required(oldvd))
5787 * If there are more than two spares attached to a disk,
5788 * and those spares are not required, then we want to
5789 * attempt to free them up now so that they can be used
5790 * by other pools. Once we're back down to a single
5791 * disk+spare, we stop removing them.
5793 if (vd->vdev_children > 2) {
5794 newvd = vd->vdev_child[1];
5796 if (newvd->vdev_isspare && last->vdev_isspare &&
5797 vdev_dtl_empty(last, DTL_MISSING) &&
5798 vdev_dtl_empty(last, DTL_OUTAGE) &&
5799 !vdev_dtl_required(newvd))
5808 spa_vdev_resilver_done(spa_t *spa)
5810 vdev_t *vd, *pvd, *ppvd;
5811 uint64_t guid, sguid, pguid, ppguid;
5813 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5815 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5816 pvd = vd->vdev_parent;
5817 ppvd = pvd->vdev_parent;
5818 guid = vd->vdev_guid;
5819 pguid = pvd->vdev_guid;
5820 ppguid = ppvd->vdev_guid;
5823 * If we have just finished replacing a hot spared device, then
5824 * we need to detach the parent's first child (the original hot
5827 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5828 ppvd->vdev_children == 2) {
5829 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5830 sguid = ppvd->vdev_child[1]->vdev_guid;
5832 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5834 spa_config_exit(spa, SCL_ALL, FTAG);
5835 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5837 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5839 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5842 spa_config_exit(spa, SCL_ALL, FTAG);
5846 * Update the stored path or FRU for this vdev.
5849 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5853 boolean_t sync = B_FALSE;
5855 ASSERT(spa_writeable(spa));
5857 spa_vdev_state_enter(spa, SCL_ALL);
5859 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5860 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5862 if (!vd->vdev_ops->vdev_op_leaf)
5863 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5866 if (strcmp(value, vd->vdev_path) != 0) {
5867 spa_strfree(vd->vdev_path);
5868 vd->vdev_path = spa_strdup(value);
5872 if (vd->vdev_fru == NULL) {
5873 vd->vdev_fru = spa_strdup(value);
5875 } else if (strcmp(value, vd->vdev_fru) != 0) {
5876 spa_strfree(vd->vdev_fru);
5877 vd->vdev_fru = spa_strdup(value);
5882 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5886 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5888 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5892 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5894 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5898 * ==========================================================================
5900 * ==========================================================================
5904 spa_scan_stop(spa_t *spa)
5906 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5907 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5908 return (SET_ERROR(EBUSY));
5909 return (dsl_scan_cancel(spa->spa_dsl_pool));
5913 spa_scan(spa_t *spa, pool_scan_func_t func)
5915 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5917 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5918 return (SET_ERROR(ENOTSUP));
5921 * If a resilver was requested, but there is no DTL on a
5922 * writeable leaf device, we have nothing to do.
5924 if (func == POOL_SCAN_RESILVER &&
5925 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5926 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5930 return (dsl_scan(spa->spa_dsl_pool, func));
5934 * ==========================================================================
5935 * SPA async task processing
5936 * ==========================================================================
5940 spa_async_remove(spa_t *spa, vdev_t *vd)
5942 if (vd->vdev_remove_wanted) {
5943 vd->vdev_remove_wanted = B_FALSE;
5944 vd->vdev_delayed_close = B_FALSE;
5945 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5948 * We want to clear the stats, but we don't want to do a full
5949 * vdev_clear() as that will cause us to throw away
5950 * degraded/faulted state as well as attempt to reopen the
5951 * device, all of which is a waste.
5953 vd->vdev_stat.vs_read_errors = 0;
5954 vd->vdev_stat.vs_write_errors = 0;
5955 vd->vdev_stat.vs_checksum_errors = 0;
5957 vdev_state_dirty(vd->vdev_top);
5958 /* Tell userspace that the vdev is gone. */
5959 zfs_post_remove(spa, vd);
5962 for (int c = 0; c < vd->vdev_children; c++)
5963 spa_async_remove(spa, vd->vdev_child[c]);
5967 spa_async_probe(spa_t *spa, vdev_t *vd)
5969 if (vd->vdev_probe_wanted) {
5970 vd->vdev_probe_wanted = B_FALSE;
5971 vdev_reopen(vd); /* vdev_open() does the actual probe */
5974 for (int c = 0; c < vd->vdev_children; c++)
5975 spa_async_probe(spa, vd->vdev_child[c]);
5979 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5985 if (!spa->spa_autoexpand)
5988 for (int c = 0; c < vd->vdev_children; c++) {
5989 vdev_t *cvd = vd->vdev_child[c];
5990 spa_async_autoexpand(spa, cvd);
5993 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5996 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5997 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5999 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6000 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
6002 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
6003 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
6006 kmem_free(physpath, MAXPATHLEN);
6010 spa_async_thread(void *arg)
6015 ASSERT(spa->spa_sync_on);
6017 mutex_enter(&spa->spa_async_lock);
6018 tasks = spa->spa_async_tasks;
6019 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
6020 mutex_exit(&spa->spa_async_lock);
6023 * See if the config needs to be updated.
6025 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
6026 uint64_t old_space, new_space;
6028 mutex_enter(&spa_namespace_lock);
6029 old_space = metaslab_class_get_space(spa_normal_class(spa));
6030 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
6031 new_space = metaslab_class_get_space(spa_normal_class(spa));
6032 mutex_exit(&spa_namespace_lock);
6035 * If the pool grew as a result of the config update,
6036 * then log an internal history event.
6038 if (new_space != old_space) {
6039 spa_history_log_internal(spa, "vdev online", NULL,
6040 "pool '%s' size: %llu(+%llu)",
6041 spa_name(spa), new_space, new_space - old_space);
6045 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
6046 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6047 spa_async_autoexpand(spa, spa->spa_root_vdev);
6048 spa_config_exit(spa, SCL_CONFIG, FTAG);
6052 * See if any devices need to be probed.
6054 if (tasks & SPA_ASYNC_PROBE) {
6055 spa_vdev_state_enter(spa, SCL_NONE);
6056 spa_async_probe(spa, spa->spa_root_vdev);
6057 (void) spa_vdev_state_exit(spa, NULL, 0);
6061 * If any devices are done replacing, detach them.
6063 if (tasks & SPA_ASYNC_RESILVER_DONE)
6064 spa_vdev_resilver_done(spa);
6067 * Kick off a resilver.
6069 if (tasks & SPA_ASYNC_RESILVER)
6070 dsl_resilver_restart(spa->spa_dsl_pool, 0);
6073 * Let the world know that we're done.
6075 mutex_enter(&spa->spa_async_lock);
6076 spa->spa_async_thread = NULL;
6077 cv_broadcast(&spa->spa_async_cv);
6078 mutex_exit(&spa->spa_async_lock);
6083 spa_async_thread_vd(void *arg)
6088 ASSERT(spa->spa_sync_on);
6090 mutex_enter(&spa->spa_async_lock);
6091 tasks = spa->spa_async_tasks;
6093 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6094 mutex_exit(&spa->spa_async_lock);
6097 * See if any devices need to be marked REMOVED.
6099 if (tasks & SPA_ASYNC_REMOVE) {
6100 spa_vdev_state_enter(spa, SCL_NONE);
6101 spa_async_remove(spa, spa->spa_root_vdev);
6102 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6103 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6104 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6105 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6106 (void) spa_vdev_state_exit(spa, NULL, 0);
6110 * Let the world know that we're done.
6112 mutex_enter(&spa->spa_async_lock);
6113 tasks = spa->spa_async_tasks;
6114 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6116 spa->spa_async_thread_vd = NULL;
6117 cv_broadcast(&spa->spa_async_cv);
6118 mutex_exit(&spa->spa_async_lock);
6123 spa_async_suspend(spa_t *spa)
6125 mutex_enter(&spa->spa_async_lock);
6126 spa->spa_async_suspended++;
6127 while (spa->spa_async_thread != NULL &&
6128 spa->spa_async_thread_vd != NULL)
6129 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6130 mutex_exit(&spa->spa_async_lock);
6134 spa_async_resume(spa_t *spa)
6136 mutex_enter(&spa->spa_async_lock);
6137 ASSERT(spa->spa_async_suspended != 0);
6138 spa->spa_async_suspended--;
6139 mutex_exit(&spa->spa_async_lock);
6143 spa_async_tasks_pending(spa_t *spa)
6145 uint_t non_config_tasks;
6147 boolean_t config_task_suspended;
6149 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6151 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6152 if (spa->spa_ccw_fail_time == 0) {
6153 config_task_suspended = B_FALSE;
6155 config_task_suspended =
6156 (gethrtime() - spa->spa_ccw_fail_time) <
6157 (zfs_ccw_retry_interval * NANOSEC);
6160 return (non_config_tasks || (config_task && !config_task_suspended));
6164 spa_async_dispatch(spa_t *spa)
6166 mutex_enter(&spa->spa_async_lock);
6167 if (spa_async_tasks_pending(spa) &&
6168 !spa->spa_async_suspended &&
6169 spa->spa_async_thread == NULL &&
6171 spa->spa_async_thread = thread_create(NULL, 0,
6172 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6173 mutex_exit(&spa->spa_async_lock);
6177 spa_async_dispatch_vd(spa_t *spa)
6179 mutex_enter(&spa->spa_async_lock);
6180 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6181 !spa->spa_async_suspended &&
6182 spa->spa_async_thread_vd == NULL &&
6184 spa->spa_async_thread_vd = thread_create(NULL, 0,
6185 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6186 mutex_exit(&spa->spa_async_lock);
6190 spa_async_request(spa_t *spa, int task)
6192 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6193 mutex_enter(&spa->spa_async_lock);
6194 spa->spa_async_tasks |= task;
6195 mutex_exit(&spa->spa_async_lock);
6196 spa_async_dispatch_vd(spa);
6200 * ==========================================================================
6201 * SPA syncing routines
6202 * ==========================================================================
6206 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6209 bpobj_enqueue(bpo, bp, tx);
6214 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6218 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6219 BP_GET_PSIZE(bp), zio->io_flags));
6224 * Note: this simple function is not inlined to make it easier to dtrace the
6225 * amount of time spent syncing frees.
6228 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6230 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6231 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6232 VERIFY(zio_wait(zio) == 0);
6236 * Note: this simple function is not inlined to make it easier to dtrace the
6237 * amount of time spent syncing deferred frees.
6240 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6242 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6243 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6244 spa_free_sync_cb, zio, tx), ==, 0);
6245 VERIFY0(zio_wait(zio));
6250 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6252 char *packed = NULL;
6257 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6260 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6261 * information. This avoids the dmu_buf_will_dirty() path and
6262 * saves us a pre-read to get data we don't actually care about.
6264 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6265 packed = kmem_alloc(bufsize, KM_SLEEP);
6267 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6269 bzero(packed + nvsize, bufsize - nvsize);
6271 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6273 kmem_free(packed, bufsize);
6275 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6276 dmu_buf_will_dirty(db, tx);
6277 *(uint64_t *)db->db_data = nvsize;
6278 dmu_buf_rele(db, FTAG);
6282 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6283 const char *config, const char *entry)
6293 * Update the MOS nvlist describing the list of available devices.
6294 * spa_validate_aux() will have already made sure this nvlist is
6295 * valid and the vdevs are labeled appropriately.
6297 if (sav->sav_object == 0) {
6298 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6299 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6300 sizeof (uint64_t), tx);
6301 VERIFY(zap_update(spa->spa_meta_objset,
6302 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6303 &sav->sav_object, tx) == 0);
6306 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6307 if (sav->sav_count == 0) {
6308 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6310 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6311 for (i = 0; i < sav->sav_count; i++)
6312 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6313 B_FALSE, VDEV_CONFIG_L2CACHE);
6314 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6315 sav->sav_count) == 0);
6316 for (i = 0; i < sav->sav_count; i++)
6317 nvlist_free(list[i]);
6318 kmem_free(list, sav->sav_count * sizeof (void *));
6321 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6322 nvlist_free(nvroot);
6324 sav->sav_sync = B_FALSE;
6328 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6332 if (list_is_empty(&spa->spa_config_dirty_list))
6335 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6337 config = spa_config_generate(spa, spa->spa_root_vdev,
6338 dmu_tx_get_txg(tx), B_FALSE);
6341 * If we're upgrading the spa version then make sure that
6342 * the config object gets updated with the correct version.
6344 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6345 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6346 spa->spa_uberblock.ub_version);
6348 spa_config_exit(spa, SCL_STATE, FTAG);
6350 if (spa->spa_config_syncing)
6351 nvlist_free(spa->spa_config_syncing);
6352 spa->spa_config_syncing = config;
6354 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6358 spa_sync_version(void *arg, dmu_tx_t *tx)
6360 uint64_t *versionp = arg;
6361 uint64_t version = *versionp;
6362 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6365 * Setting the version is special cased when first creating the pool.
6367 ASSERT(tx->tx_txg != TXG_INITIAL);
6369 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6370 ASSERT(version >= spa_version(spa));
6372 spa->spa_uberblock.ub_version = version;
6373 vdev_config_dirty(spa->spa_root_vdev);
6374 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6378 * Set zpool properties.
6381 spa_sync_props(void *arg, dmu_tx_t *tx)
6383 nvlist_t *nvp = arg;
6384 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6385 objset_t *mos = spa->spa_meta_objset;
6386 nvpair_t *elem = NULL;
6388 mutex_enter(&spa->spa_props_lock);
6390 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6392 char *strval, *fname;
6394 const char *propname;
6395 zprop_type_t proptype;
6398 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6401 * We checked this earlier in spa_prop_validate().
6403 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6405 fname = strchr(nvpair_name(elem), '@') + 1;
6406 VERIFY0(zfeature_lookup_name(fname, &fid));
6408 spa_feature_enable(spa, fid, tx);
6409 spa_history_log_internal(spa, "set", tx,
6410 "%s=enabled", nvpair_name(elem));
6413 case ZPOOL_PROP_VERSION:
6414 intval = fnvpair_value_uint64(elem);
6416 * The version is synced seperatly before other
6417 * properties and should be correct by now.
6419 ASSERT3U(spa_version(spa), >=, intval);
6422 case ZPOOL_PROP_ALTROOT:
6424 * 'altroot' is a non-persistent property. It should
6425 * have been set temporarily at creation or import time.
6427 ASSERT(spa->spa_root != NULL);
6430 case ZPOOL_PROP_READONLY:
6431 case ZPOOL_PROP_CACHEFILE:
6433 * 'readonly' and 'cachefile' are also non-persisitent
6437 case ZPOOL_PROP_COMMENT:
6438 strval = fnvpair_value_string(elem);
6439 if (spa->spa_comment != NULL)
6440 spa_strfree(spa->spa_comment);
6441 spa->spa_comment = spa_strdup(strval);
6443 * We need to dirty the configuration on all the vdevs
6444 * so that their labels get updated. It's unnecessary
6445 * to do this for pool creation since the vdev's
6446 * configuratoin has already been dirtied.
6448 if (tx->tx_txg != TXG_INITIAL)
6449 vdev_config_dirty(spa->spa_root_vdev);
6450 spa_history_log_internal(spa, "set", tx,
6451 "%s=%s", nvpair_name(elem), strval);
6455 * Set pool property values in the poolprops mos object.
6457 if (spa->spa_pool_props_object == 0) {
6458 spa->spa_pool_props_object =
6459 zap_create_link(mos, DMU_OT_POOL_PROPS,
6460 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6464 /* normalize the property name */
6465 propname = zpool_prop_to_name(prop);
6466 proptype = zpool_prop_get_type(prop);
6468 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6469 ASSERT(proptype == PROP_TYPE_STRING);
6470 strval = fnvpair_value_string(elem);
6471 VERIFY0(zap_update(mos,
6472 spa->spa_pool_props_object, propname,
6473 1, strlen(strval) + 1, strval, tx));
6474 spa_history_log_internal(spa, "set", tx,
6475 "%s=%s", nvpair_name(elem), strval);
6476 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6477 intval = fnvpair_value_uint64(elem);
6479 if (proptype == PROP_TYPE_INDEX) {
6481 VERIFY0(zpool_prop_index_to_string(
6482 prop, intval, &unused));
6484 VERIFY0(zap_update(mos,
6485 spa->spa_pool_props_object, propname,
6486 8, 1, &intval, tx));
6487 spa_history_log_internal(spa, "set", tx,
6488 "%s=%lld", nvpair_name(elem), intval);
6490 ASSERT(0); /* not allowed */
6494 case ZPOOL_PROP_DELEGATION:
6495 spa->spa_delegation = intval;
6497 case ZPOOL_PROP_BOOTFS:
6498 spa->spa_bootfs = intval;
6500 case ZPOOL_PROP_FAILUREMODE:
6501 spa->spa_failmode = intval;
6503 case ZPOOL_PROP_AUTOEXPAND:
6504 spa->spa_autoexpand = intval;
6505 if (tx->tx_txg != TXG_INITIAL)
6506 spa_async_request(spa,
6507 SPA_ASYNC_AUTOEXPAND);
6509 case ZPOOL_PROP_DEDUPDITTO:
6510 spa->spa_dedup_ditto = intval;
6519 mutex_exit(&spa->spa_props_lock);
6523 * Perform one-time upgrade on-disk changes. spa_version() does not
6524 * reflect the new version this txg, so there must be no changes this
6525 * txg to anything that the upgrade code depends on after it executes.
6526 * Therefore this must be called after dsl_pool_sync() does the sync
6530 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6532 dsl_pool_t *dp = spa->spa_dsl_pool;
6534 ASSERT(spa->spa_sync_pass == 1);
6536 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6538 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6539 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6540 dsl_pool_create_origin(dp, tx);
6542 /* Keeping the origin open increases spa_minref */
6543 spa->spa_minref += 3;
6546 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6547 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6548 dsl_pool_upgrade_clones(dp, tx);
6551 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6552 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6553 dsl_pool_upgrade_dir_clones(dp, tx);
6555 /* Keeping the freedir open increases spa_minref */
6556 spa->spa_minref += 3;
6559 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6560 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6561 spa_feature_create_zap_objects(spa, tx);
6565 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6566 * when possibility to use lz4 compression for metadata was added
6567 * Old pools that have this feature enabled must be upgraded to have
6568 * this feature active
6570 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6571 boolean_t lz4_en = spa_feature_is_enabled(spa,
6572 SPA_FEATURE_LZ4_COMPRESS);
6573 boolean_t lz4_ac = spa_feature_is_active(spa,
6574 SPA_FEATURE_LZ4_COMPRESS);
6576 if (lz4_en && !lz4_ac)
6577 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6581 * If we haven't written the salt, do so now. Note that the
6582 * feature may not be activated yet, but that's fine since
6583 * the presence of this ZAP entry is backwards compatible.
6585 if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
6586 DMU_POOL_CHECKSUM_SALT) == ENOENT) {
6587 VERIFY0(zap_add(spa->spa_meta_objset,
6588 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
6589 sizeof (spa->spa_cksum_salt.zcs_bytes),
6590 spa->spa_cksum_salt.zcs_bytes, tx));
6593 rrw_exit(&dp->dp_config_rwlock, FTAG);
6597 * Sync the specified transaction group. New blocks may be dirtied as
6598 * part of the process, so we iterate until it converges.
6601 spa_sync(spa_t *spa, uint64_t txg)
6603 dsl_pool_t *dp = spa->spa_dsl_pool;
6604 objset_t *mos = spa->spa_meta_objset;
6605 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6606 vdev_t *rvd = spa->spa_root_vdev;
6611 VERIFY(spa_writeable(spa));
6614 * Lock out configuration changes.
6616 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6618 spa->spa_syncing_txg = txg;
6619 spa->spa_sync_pass = 0;
6622 * If there are any pending vdev state changes, convert them
6623 * into config changes that go out with this transaction group.
6625 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6626 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6628 * We need the write lock here because, for aux vdevs,
6629 * calling vdev_config_dirty() modifies sav_config.
6630 * This is ugly and will become unnecessary when we
6631 * eliminate the aux vdev wart by integrating all vdevs
6632 * into the root vdev tree.
6634 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6635 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6636 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6637 vdev_state_clean(vd);
6638 vdev_config_dirty(vd);
6640 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6641 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6643 spa_config_exit(spa, SCL_STATE, FTAG);
6645 tx = dmu_tx_create_assigned(dp, txg);
6647 spa->spa_sync_starttime = gethrtime();
6649 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6650 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6651 #else /* !illumos */
6653 callout_reset(&spa->spa_deadman_cycid,
6654 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6656 #endif /* illumos */
6659 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6660 * set spa_deflate if we have no raid-z vdevs.
6662 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6663 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6666 for (i = 0; i < rvd->vdev_children; i++) {
6667 vd = rvd->vdev_child[i];
6668 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6671 if (i == rvd->vdev_children) {
6672 spa->spa_deflate = TRUE;
6673 VERIFY(0 == zap_add(spa->spa_meta_objset,
6674 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6675 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6680 * Iterate to convergence.
6683 int pass = ++spa->spa_sync_pass;
6685 spa_sync_config_object(spa, tx);
6686 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6687 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6688 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6689 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6690 spa_errlog_sync(spa, txg);
6691 dsl_pool_sync(dp, txg);
6693 if (pass < zfs_sync_pass_deferred_free) {
6694 spa_sync_frees(spa, free_bpl, tx);
6697 * We can not defer frees in pass 1, because
6698 * we sync the deferred frees later in pass 1.
6700 ASSERT3U(pass, >, 1);
6701 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6702 &spa->spa_deferred_bpobj, tx);
6706 dsl_scan_sync(dp, tx);
6708 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6712 spa_sync_upgrades(spa, tx);
6714 spa->spa_uberblock.ub_rootbp.blk_birth);
6716 * Note: We need to check if the MOS is dirty
6717 * because we could have marked the MOS dirty
6718 * without updating the uberblock (e.g. if we
6719 * have sync tasks but no dirty user data). We
6720 * need to check the uberblock's rootbp because
6721 * it is updated if we have synced out dirty
6722 * data (though in this case the MOS will most
6723 * likely also be dirty due to second order
6724 * effects, we don't want to rely on that here).
6726 if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6727 !dmu_objset_is_dirty(mos, txg)) {
6729 * Nothing changed on the first pass,
6730 * therefore this TXG is a no-op. Avoid
6731 * syncing deferred frees, so that we
6732 * can keep this TXG as a no-op.
6734 ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6736 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6737 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
6740 spa_sync_deferred_frees(spa, tx);
6743 } while (dmu_objset_is_dirty(mos, txg));
6746 * Rewrite the vdev configuration (which includes the uberblock)
6747 * to commit the transaction group.
6749 * If there are no dirty vdevs, we sync the uberblock to a few
6750 * random top-level vdevs that are known to be visible in the
6751 * config cache (see spa_vdev_add() for a complete description).
6752 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6756 * We hold SCL_STATE to prevent vdev open/close/etc.
6757 * while we're attempting to write the vdev labels.
6759 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6761 if (list_is_empty(&spa->spa_config_dirty_list)) {
6762 vdev_t *svd[SPA_DVAS_PER_BP];
6764 int children = rvd->vdev_children;
6765 int c0 = spa_get_random(children);
6767 for (int c = 0; c < children; c++) {
6768 vd = rvd->vdev_child[(c0 + c) % children];
6769 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6771 svd[svdcount++] = vd;
6772 if (svdcount == SPA_DVAS_PER_BP)
6775 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6777 error = vdev_config_sync(svd, svdcount, txg,
6780 error = vdev_config_sync(rvd->vdev_child,
6781 rvd->vdev_children, txg, B_FALSE);
6783 error = vdev_config_sync(rvd->vdev_child,
6784 rvd->vdev_children, txg, B_TRUE);
6788 spa->spa_last_synced_guid = rvd->vdev_guid;
6790 spa_config_exit(spa, SCL_STATE, FTAG);
6794 zio_suspend(spa, NULL);
6795 zio_resume_wait(spa);
6800 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6801 #else /* !illumos */
6803 callout_drain(&spa->spa_deadman_cycid);
6805 #endif /* illumos */
6808 * Clear the dirty config list.
6810 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6811 vdev_config_clean(vd);
6814 * Now that the new config has synced transactionally,
6815 * let it become visible to the config cache.
6817 if (spa->spa_config_syncing != NULL) {
6818 spa_config_set(spa, spa->spa_config_syncing);
6819 spa->spa_config_txg = txg;
6820 spa->spa_config_syncing = NULL;
6823 spa->spa_ubsync = spa->spa_uberblock;
6825 dsl_pool_sync_done(dp, txg);
6828 * Update usable space statistics.
6830 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6831 vdev_sync_done(vd, txg);
6833 spa_update_dspace(spa);
6836 * It had better be the case that we didn't dirty anything
6837 * since vdev_config_sync().
6839 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6840 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6841 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6843 spa->spa_sync_pass = 0;
6845 spa_config_exit(spa, SCL_CONFIG, FTAG);
6847 spa_handle_ignored_writes(spa);
6850 * If any async tasks have been requested, kick them off.
6852 spa_async_dispatch(spa);
6853 spa_async_dispatch_vd(spa);
6857 * Sync all pools. We don't want to hold the namespace lock across these
6858 * operations, so we take a reference on the spa_t and drop the lock during the
6862 spa_sync_allpools(void)
6865 mutex_enter(&spa_namespace_lock);
6866 while ((spa = spa_next(spa)) != NULL) {
6867 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6868 !spa_writeable(spa) || spa_suspended(spa))
6870 spa_open_ref(spa, FTAG);
6871 mutex_exit(&spa_namespace_lock);
6872 txg_wait_synced(spa_get_dsl(spa), 0);
6873 mutex_enter(&spa_namespace_lock);
6874 spa_close(spa, FTAG);
6876 mutex_exit(&spa_namespace_lock);
6880 * ==========================================================================
6881 * Miscellaneous routines
6882 * ==========================================================================
6886 * Remove all pools in the system.
6894 * Remove all cached state. All pools should be closed now,
6895 * so every spa in the AVL tree should be unreferenced.
6897 mutex_enter(&spa_namespace_lock);
6898 while ((spa = spa_next(NULL)) != NULL) {
6900 * Stop async tasks. The async thread may need to detach
6901 * a device that's been replaced, which requires grabbing
6902 * spa_namespace_lock, so we must drop it here.
6904 spa_open_ref(spa, FTAG);
6905 mutex_exit(&spa_namespace_lock);
6906 spa_async_suspend(spa);
6907 mutex_enter(&spa_namespace_lock);
6908 spa_close(spa, FTAG);
6910 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6912 spa_deactivate(spa);
6916 mutex_exit(&spa_namespace_lock);
6920 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6925 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6929 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6930 vd = spa->spa_l2cache.sav_vdevs[i];
6931 if (vd->vdev_guid == guid)
6935 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6936 vd = spa->spa_spares.sav_vdevs[i];
6937 if (vd->vdev_guid == guid)
6946 spa_upgrade(spa_t *spa, uint64_t version)
6948 ASSERT(spa_writeable(spa));
6950 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6953 * This should only be called for a non-faulted pool, and since a
6954 * future version would result in an unopenable pool, this shouldn't be
6957 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6958 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6960 spa->spa_uberblock.ub_version = version;
6961 vdev_config_dirty(spa->spa_root_vdev);
6963 spa_config_exit(spa, SCL_ALL, FTAG);
6965 txg_wait_synced(spa_get_dsl(spa), 0);
6969 spa_has_spare(spa_t *spa, uint64_t guid)
6973 spa_aux_vdev_t *sav = &spa->spa_spares;
6975 for (i = 0; i < sav->sav_count; i++)
6976 if (sav->sav_vdevs[i]->vdev_guid == guid)
6979 for (i = 0; i < sav->sav_npending; i++) {
6980 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6981 &spareguid) == 0 && spareguid == guid)
6989 * Check if a pool has an active shared spare device.
6990 * Note: reference count of an active spare is 2, as a spare and as a replace
6993 spa_has_active_shared_spare(spa_t *spa)
6997 spa_aux_vdev_t *sav = &spa->spa_spares;
6999 for (i = 0; i < sav->sav_count; i++) {
7000 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
7001 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
7010 * Post a sysevent corresponding to the given event. The 'name' must be one of
7011 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
7012 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7013 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7014 * or zdb as real changes.
7017 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
7021 sysevent_attr_list_t *attr = NULL;
7022 sysevent_value_t value;
7025 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
7028 value.value_type = SE_DATA_TYPE_STRING;
7029 value.value.sv_string = spa_name(spa);
7030 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
7033 value.value_type = SE_DATA_TYPE_UINT64;
7034 value.value.sv_uint64 = spa_guid(spa);
7035 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
7039 value.value_type = SE_DATA_TYPE_UINT64;
7040 value.value.sv_uint64 = vd->vdev_guid;
7041 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
7045 if (vd->vdev_path) {
7046 value.value_type = SE_DATA_TYPE_STRING;
7047 value.value.sv_string = vd->vdev_path;
7048 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
7049 &value, SE_SLEEP) != 0)
7054 if (sysevent_attach_attributes(ev, attr) != 0)
7058 (void) log_sysevent(ev, SE_SLEEP, &eid);
7062 sysevent_free_attr(attr);