4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * SPA: Storage Pool Allocator
32 * This file contains all the routines used when modifying on-disk SPA state.
33 * This includes opening, importing, destroying, exporting a pool, and syncing a
37 #include <sys/zfs_context.h>
38 #include <sys/fm/fs/zfs.h>
39 #include <sys/spa_impl.h>
41 #include <sys/zio_checksum.h>
43 #include <sys/dmu_tx.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/metaslab.h>
49 #include <sys/metaslab_impl.h>
50 #include <sys/uberblock_impl.h>
53 #include <sys/dmu_traverse.h>
54 #include <sys/dmu_objset.h>
55 #include <sys/unique.h>
56 #include <sys/dsl_pool.h>
57 #include <sys/dsl_dataset.h>
58 #include <sys/dsl_dir.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/dsl_synctask.h>
61 #include <sys/fs/zfs.h>
63 #include <sys/callb.h>
64 #include <sys/spa_boot.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/dsl_scan.h>
67 #include <sys/dmu_send.h>
68 #include <sys/dsl_destroy.h>
69 #include <sys/dsl_userhold.h>
70 #include <sys/zfeature.h>
72 #include <sys/trim_map.h>
75 #include <sys/callb.h>
76 #include <sys/cpupart.h>
81 #include "zfs_comutil.h"
83 /* Check hostid on import? */
84 static int check_hostid = 1;
86 SYSCTL_DECL(_vfs_zfs);
87 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
88 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
89 "Check hostid on import?");
92 * The interval, in seconds, at which failed configuration cache file writes
95 static int zfs_ccw_retry_interval = 300;
97 typedef enum zti_modes {
98 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
99 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
100 ZTI_MODE_NULL, /* don't create a taskq */
104 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
105 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
106 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
108 #define ZTI_N(n) ZTI_P(n, 1)
109 #define ZTI_ONE ZTI_N(1)
111 typedef struct zio_taskq_info {
112 zti_modes_t zti_mode;
117 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
118 "issue", "issue_high", "intr", "intr_high"
122 * This table defines the taskq settings for each ZFS I/O type. When
123 * initializing a pool, we use this table to create an appropriately sized
124 * taskq. Some operations are low volume and therefore have a small, static
125 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
126 * macros. Other operations process a large amount of data; the ZTI_BATCH
127 * macro causes us to create a taskq oriented for throughput. Some operations
128 * are so high frequency and short-lived that the taskq itself can become a a
129 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
130 * additional degree of parallelism specified by the number of threads per-
131 * taskq and the number of taskqs; when dispatching an event in this case, the
132 * particular taskq is chosen at random.
134 * The different taskq priorities are to handle the different contexts (issue
135 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
136 * need to be handled with minimum delay.
138 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
139 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
140 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
141 { ZTI_N(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, /* READ */
142 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
143 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
144 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
145 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
148 static void spa_sync_version(void *arg, dmu_tx_t *tx);
149 static void spa_sync_props(void *arg, dmu_tx_t *tx);
150 static boolean_t spa_has_active_shared_spare(spa_t *spa);
151 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
152 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
154 static void spa_vdev_resilver_done(spa_t *spa);
156 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
158 id_t zio_taskq_psrset_bind = PS_NONE;
161 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
163 uint_t zio_taskq_basedc = 80; /* base duty cycle */
165 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
166 extern int zfs_sync_pass_deferred_free;
169 extern void spa_deadman(void *arg);
173 * This (illegal) pool name is used when temporarily importing a spa_t in order
174 * to get the vdev stats associated with the imported devices.
176 #define TRYIMPORT_NAME "$import"
179 * ==========================================================================
180 * SPA properties routines
181 * ==========================================================================
185 * Add a (source=src, propname=propval) list to an nvlist.
188 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
189 uint64_t intval, zprop_source_t src)
191 const char *propname = zpool_prop_to_name(prop);
194 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
195 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
198 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
200 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
202 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
203 nvlist_free(propval);
207 * Get property values from the spa configuration.
210 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
212 vdev_t *rvd = spa->spa_root_vdev;
213 dsl_pool_t *pool = spa->spa_dsl_pool;
214 uint64_t size, alloc, cap, version;
215 zprop_source_t src = ZPROP_SRC_NONE;
216 spa_config_dirent_t *dp;
217 metaslab_class_t *mc = spa_normal_class(spa);
219 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
222 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
223 size = metaslab_class_get_space(spa_normal_class(spa));
224 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
225 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
226 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
227 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
230 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
231 metaslab_class_fragmentation(mc), src);
232 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
233 metaslab_class_expandable_space(mc), src);
234 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
235 (spa_mode(spa) == FREAD), src);
237 cap = (size == 0) ? 0 : (alloc * 100 / size);
238 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
240 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
241 ddt_get_pool_dedup_ratio(spa), src);
243 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
244 rvd->vdev_state, src);
246 version = spa_version(spa);
247 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
248 src = ZPROP_SRC_DEFAULT;
250 src = ZPROP_SRC_LOCAL;
251 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
256 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
257 * when opening pools before this version freedir will be NULL.
259 if (pool->dp_free_dir != NULL) {
260 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
261 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
264 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
268 if (pool->dp_leak_dir != NULL) {
269 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
270 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
273 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
278 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
280 if (spa->spa_comment != NULL) {
281 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
285 if (spa->spa_root != NULL)
286 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
289 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
290 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
291 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
293 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
294 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
297 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
298 if (dp->scd_path == NULL) {
299 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
300 "none", 0, ZPROP_SRC_LOCAL);
301 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
302 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
303 dp->scd_path, 0, ZPROP_SRC_LOCAL);
309 * Get zpool property values.
312 spa_prop_get(spa_t *spa, nvlist_t **nvp)
314 objset_t *mos = spa->spa_meta_objset;
319 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
321 mutex_enter(&spa->spa_props_lock);
324 * Get properties from the spa config.
326 spa_prop_get_config(spa, nvp);
328 /* If no pool property object, no more prop to get. */
329 if (mos == NULL || spa->spa_pool_props_object == 0) {
330 mutex_exit(&spa->spa_props_lock);
335 * Get properties from the MOS pool property object.
337 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
338 (err = zap_cursor_retrieve(&zc, &za)) == 0;
339 zap_cursor_advance(&zc)) {
342 zprop_source_t src = ZPROP_SRC_DEFAULT;
345 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
348 switch (za.za_integer_length) {
350 /* integer property */
351 if (za.za_first_integer !=
352 zpool_prop_default_numeric(prop))
353 src = ZPROP_SRC_LOCAL;
355 if (prop == ZPOOL_PROP_BOOTFS) {
357 dsl_dataset_t *ds = NULL;
359 dp = spa_get_dsl(spa);
360 dsl_pool_config_enter(dp, FTAG);
361 if (err = dsl_dataset_hold_obj(dp,
362 za.za_first_integer, FTAG, &ds)) {
363 dsl_pool_config_exit(dp, FTAG);
368 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
370 dsl_dataset_name(ds, strval);
371 dsl_dataset_rele(ds, FTAG);
372 dsl_pool_config_exit(dp, FTAG);
375 intval = za.za_first_integer;
378 spa_prop_add_list(*nvp, prop, strval, intval, src);
382 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
387 /* string property */
388 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
389 err = zap_lookup(mos, spa->spa_pool_props_object,
390 za.za_name, 1, za.za_num_integers, strval);
392 kmem_free(strval, za.za_num_integers);
395 spa_prop_add_list(*nvp, prop, strval, 0, src);
396 kmem_free(strval, za.za_num_integers);
403 zap_cursor_fini(&zc);
404 mutex_exit(&spa->spa_props_lock);
406 if (err && err != ENOENT) {
416 * Validate the given pool properties nvlist and modify the list
417 * for the property values to be set.
420 spa_prop_validate(spa_t *spa, nvlist_t *props)
423 int error = 0, reset_bootfs = 0;
425 boolean_t has_feature = B_FALSE;
428 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
430 char *strval, *slash, *check, *fname;
431 const char *propname = nvpair_name(elem);
432 zpool_prop_t prop = zpool_name_to_prop(propname);
436 if (!zpool_prop_feature(propname)) {
437 error = SET_ERROR(EINVAL);
442 * Sanitize the input.
444 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
445 error = SET_ERROR(EINVAL);
449 if (nvpair_value_uint64(elem, &intval) != 0) {
450 error = SET_ERROR(EINVAL);
455 error = SET_ERROR(EINVAL);
459 fname = strchr(propname, '@') + 1;
460 if (zfeature_lookup_name(fname, NULL) != 0) {
461 error = SET_ERROR(EINVAL);
465 has_feature = B_TRUE;
468 case ZPOOL_PROP_VERSION:
469 error = nvpair_value_uint64(elem, &intval);
471 (intval < spa_version(spa) ||
472 intval > SPA_VERSION_BEFORE_FEATURES ||
474 error = SET_ERROR(EINVAL);
477 case ZPOOL_PROP_DELEGATION:
478 case ZPOOL_PROP_AUTOREPLACE:
479 case ZPOOL_PROP_LISTSNAPS:
480 case ZPOOL_PROP_AUTOEXPAND:
481 error = nvpair_value_uint64(elem, &intval);
482 if (!error && intval > 1)
483 error = SET_ERROR(EINVAL);
486 case ZPOOL_PROP_BOOTFS:
488 * If the pool version is less than SPA_VERSION_BOOTFS,
489 * or the pool is still being created (version == 0),
490 * the bootfs property cannot be set.
492 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
493 error = SET_ERROR(ENOTSUP);
498 * Make sure the vdev config is bootable
500 if (!vdev_is_bootable(spa->spa_root_vdev)) {
501 error = SET_ERROR(ENOTSUP);
507 error = nvpair_value_string(elem, &strval);
513 if (strval == NULL || strval[0] == '\0') {
514 objnum = zpool_prop_default_numeric(
519 if (error = dmu_objset_hold(strval, FTAG, &os))
523 * Must be ZPL, and its property settings
524 * must be supported by GRUB (compression
525 * is not gzip, and large blocks are not used).
528 if (dmu_objset_type(os) != DMU_OST_ZFS) {
529 error = SET_ERROR(ENOTSUP);
531 dsl_prop_get_int_ds(dmu_objset_ds(os),
532 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
534 !BOOTFS_COMPRESS_VALID(propval)) {
535 error = SET_ERROR(ENOTSUP);
537 dsl_prop_get_int_ds(dmu_objset_ds(os),
538 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
540 propval > SPA_OLD_MAXBLOCKSIZE) {
541 error = SET_ERROR(ENOTSUP);
543 objnum = dmu_objset_id(os);
545 dmu_objset_rele(os, FTAG);
549 case ZPOOL_PROP_FAILUREMODE:
550 error = nvpair_value_uint64(elem, &intval);
551 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
552 intval > ZIO_FAILURE_MODE_PANIC))
553 error = SET_ERROR(EINVAL);
556 * This is a special case which only occurs when
557 * the pool has completely failed. This allows
558 * the user to change the in-core failmode property
559 * without syncing it out to disk (I/Os might
560 * currently be blocked). We do this by returning
561 * EIO to the caller (spa_prop_set) to trick it
562 * into thinking we encountered a property validation
565 if (!error && spa_suspended(spa)) {
566 spa->spa_failmode = intval;
567 error = SET_ERROR(EIO);
571 case ZPOOL_PROP_CACHEFILE:
572 if ((error = nvpair_value_string(elem, &strval)) != 0)
575 if (strval[0] == '\0')
578 if (strcmp(strval, "none") == 0)
581 if (strval[0] != '/') {
582 error = SET_ERROR(EINVAL);
586 slash = strrchr(strval, '/');
587 ASSERT(slash != NULL);
589 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
590 strcmp(slash, "/..") == 0)
591 error = SET_ERROR(EINVAL);
594 case ZPOOL_PROP_COMMENT:
595 if ((error = nvpair_value_string(elem, &strval)) != 0)
597 for (check = strval; *check != '\0'; check++) {
599 * The kernel doesn't have an easy isprint()
600 * check. For this kernel check, we merely
601 * check ASCII apart from DEL. Fix this if
602 * there is an easy-to-use kernel isprint().
604 if (*check >= 0x7f) {
605 error = SET_ERROR(EINVAL);
610 if (strlen(strval) > ZPROP_MAX_COMMENT)
614 case ZPOOL_PROP_DEDUPDITTO:
615 if (spa_version(spa) < SPA_VERSION_DEDUP)
616 error = SET_ERROR(ENOTSUP);
618 error = nvpair_value_uint64(elem, &intval);
620 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
621 error = SET_ERROR(EINVAL);
629 if (!error && reset_bootfs) {
630 error = nvlist_remove(props,
631 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
634 error = nvlist_add_uint64(props,
635 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
643 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
646 spa_config_dirent_t *dp;
648 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
652 dp = kmem_alloc(sizeof (spa_config_dirent_t),
655 if (cachefile[0] == '\0')
656 dp->scd_path = spa_strdup(spa_config_path);
657 else if (strcmp(cachefile, "none") == 0)
660 dp->scd_path = spa_strdup(cachefile);
662 list_insert_head(&spa->spa_config_list, dp);
664 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
668 spa_prop_set(spa_t *spa, nvlist_t *nvp)
671 nvpair_t *elem = NULL;
672 boolean_t need_sync = B_FALSE;
674 if ((error = spa_prop_validate(spa, nvp)) != 0)
677 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
678 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
680 if (prop == ZPOOL_PROP_CACHEFILE ||
681 prop == ZPOOL_PROP_ALTROOT ||
682 prop == ZPOOL_PROP_READONLY)
685 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
688 if (prop == ZPOOL_PROP_VERSION) {
689 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
691 ASSERT(zpool_prop_feature(nvpair_name(elem)));
692 ver = SPA_VERSION_FEATURES;
696 /* Save time if the version is already set. */
697 if (ver == spa_version(spa))
701 * In addition to the pool directory object, we might
702 * create the pool properties object, the features for
703 * read object, the features for write object, or the
704 * feature descriptions object.
706 error = dsl_sync_task(spa->spa_name, NULL,
707 spa_sync_version, &ver,
708 6, ZFS_SPACE_CHECK_RESERVED);
719 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
720 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
727 * If the bootfs property value is dsobj, clear it.
730 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
732 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
733 VERIFY(zap_remove(spa->spa_meta_objset,
734 spa->spa_pool_props_object,
735 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
742 spa_change_guid_check(void *arg, dmu_tx_t *tx)
744 uint64_t *newguid = arg;
745 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
746 vdev_t *rvd = spa->spa_root_vdev;
749 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
750 vdev_state = rvd->vdev_state;
751 spa_config_exit(spa, SCL_STATE, FTAG);
753 if (vdev_state != VDEV_STATE_HEALTHY)
754 return (SET_ERROR(ENXIO));
756 ASSERT3U(spa_guid(spa), !=, *newguid);
762 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
764 uint64_t *newguid = arg;
765 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
767 vdev_t *rvd = spa->spa_root_vdev;
769 oldguid = spa_guid(spa);
771 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
772 rvd->vdev_guid = *newguid;
773 rvd->vdev_guid_sum += (*newguid - oldguid);
774 vdev_config_dirty(rvd);
775 spa_config_exit(spa, SCL_STATE, FTAG);
777 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
782 * Change the GUID for the pool. This is done so that we can later
783 * re-import a pool built from a clone of our own vdevs. We will modify
784 * the root vdev's guid, our own pool guid, and then mark all of our
785 * vdevs dirty. Note that we must make sure that all our vdevs are
786 * online when we do this, or else any vdevs that weren't present
787 * would be orphaned from our pool. We are also going to issue a
788 * sysevent to update any watchers.
791 spa_change_guid(spa_t *spa)
796 mutex_enter(&spa->spa_vdev_top_lock);
797 mutex_enter(&spa_namespace_lock);
798 guid = spa_generate_guid(NULL);
800 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
801 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
804 spa_config_sync(spa, B_FALSE, B_TRUE);
805 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
808 mutex_exit(&spa_namespace_lock);
809 mutex_exit(&spa->spa_vdev_top_lock);
815 * ==========================================================================
816 * SPA state manipulation (open/create/destroy/import/export)
817 * ==========================================================================
821 spa_error_entry_compare(const void *a, const void *b)
823 spa_error_entry_t *sa = (spa_error_entry_t *)a;
824 spa_error_entry_t *sb = (spa_error_entry_t *)b;
827 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
828 sizeof (zbookmark_phys_t));
839 * Utility function which retrieves copies of the current logs and
840 * re-initializes them in the process.
843 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
845 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
847 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
848 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
850 avl_create(&spa->spa_errlist_scrub,
851 spa_error_entry_compare, sizeof (spa_error_entry_t),
852 offsetof(spa_error_entry_t, se_avl));
853 avl_create(&spa->spa_errlist_last,
854 spa_error_entry_compare, sizeof (spa_error_entry_t),
855 offsetof(spa_error_entry_t, se_avl));
859 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
861 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
862 enum zti_modes mode = ztip->zti_mode;
863 uint_t value = ztip->zti_value;
864 uint_t count = ztip->zti_count;
865 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
868 boolean_t batch = B_FALSE;
870 if (mode == ZTI_MODE_NULL) {
872 tqs->stqs_taskq = NULL;
876 ASSERT3U(count, >, 0);
878 tqs->stqs_count = count;
879 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
883 ASSERT3U(value, >=, 1);
884 value = MAX(value, 1);
889 flags |= TASKQ_THREADS_CPU_PCT;
890 value = zio_taskq_batch_pct;
894 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
896 zio_type_name[t], zio_taskq_types[q], mode, value);
900 for (uint_t i = 0; i < count; i++) {
904 (void) snprintf(name, sizeof (name), "%s_%s_%u",
905 zio_type_name[t], zio_taskq_types[q], i);
907 (void) snprintf(name, sizeof (name), "%s_%s",
908 zio_type_name[t], zio_taskq_types[q]);
912 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
914 flags |= TASKQ_DC_BATCH;
916 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
917 spa->spa_proc, zio_taskq_basedc, flags);
920 pri_t pri = maxclsyspri;
922 * The write issue taskq can be extremely CPU
923 * intensive. Run it at slightly lower priority
924 * than the other taskqs.
926 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
929 tq = taskq_create_proc(name, value, pri, 50,
930 INT_MAX, spa->spa_proc, flags);
935 tqs->stqs_taskq[i] = tq;
940 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
942 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
944 if (tqs->stqs_taskq == NULL) {
945 ASSERT0(tqs->stqs_count);
949 for (uint_t i = 0; i < tqs->stqs_count; i++) {
950 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
951 taskq_destroy(tqs->stqs_taskq[i]);
954 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
955 tqs->stqs_taskq = NULL;
959 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
960 * Note that a type may have multiple discrete taskqs to avoid lock contention
961 * on the taskq itself. In that case we choose which taskq at random by using
962 * the low bits of gethrtime().
965 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
966 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
968 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
971 ASSERT3P(tqs->stqs_taskq, !=, NULL);
972 ASSERT3U(tqs->stqs_count, !=, 0);
974 if (tqs->stqs_count == 1) {
975 tq = tqs->stqs_taskq[0];
978 tq = tqs->stqs_taskq[cpu_ticks() % tqs->stqs_count];
980 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
984 taskq_dispatch_ent(tq, func, arg, flags, ent);
988 spa_create_zio_taskqs(spa_t *spa)
990 for (int t = 0; t < ZIO_TYPES; t++) {
991 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
992 spa_taskqs_init(spa, t, q);
1000 spa_thread(void *arg)
1002 callb_cpr_t cprinfo;
1005 user_t *pu = PTOU(curproc);
1007 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1010 ASSERT(curproc != &p0);
1011 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1012 "zpool-%s", spa->spa_name);
1013 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1016 /* bind this thread to the requested psrset */
1017 if (zio_taskq_psrset_bind != PS_NONE) {
1019 mutex_enter(&cpu_lock);
1020 mutex_enter(&pidlock);
1021 mutex_enter(&curproc->p_lock);
1023 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1024 0, NULL, NULL) == 0) {
1025 curthread->t_bind_pset = zio_taskq_psrset_bind;
1028 "Couldn't bind process for zfs pool \"%s\" to "
1029 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1032 mutex_exit(&curproc->p_lock);
1033 mutex_exit(&pidlock);
1034 mutex_exit(&cpu_lock);
1040 if (zio_taskq_sysdc) {
1041 sysdc_thread_enter(curthread, 100, 0);
1045 spa->spa_proc = curproc;
1046 spa->spa_did = curthread->t_did;
1048 spa_create_zio_taskqs(spa);
1050 mutex_enter(&spa->spa_proc_lock);
1051 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1053 spa->spa_proc_state = SPA_PROC_ACTIVE;
1054 cv_broadcast(&spa->spa_proc_cv);
1056 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1057 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1058 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1059 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1061 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1062 spa->spa_proc_state = SPA_PROC_GONE;
1063 spa->spa_proc = &p0;
1064 cv_broadcast(&spa->spa_proc_cv);
1065 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1067 mutex_enter(&curproc->p_lock);
1070 #endif /* SPA_PROCESS */
1074 * Activate an uninitialized pool.
1077 spa_activate(spa_t *spa, int mode)
1079 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1081 spa->spa_state = POOL_STATE_ACTIVE;
1082 spa->spa_mode = mode;
1084 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1085 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1087 /* Try to create a covering process */
1088 mutex_enter(&spa->spa_proc_lock);
1089 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1090 ASSERT(spa->spa_proc == &p0);
1094 /* Only create a process if we're going to be around a while. */
1095 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1096 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1098 spa->spa_proc_state = SPA_PROC_CREATED;
1099 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1100 cv_wait(&spa->spa_proc_cv,
1101 &spa->spa_proc_lock);
1103 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1104 ASSERT(spa->spa_proc != &p0);
1105 ASSERT(spa->spa_did != 0);
1109 "Couldn't create process for zfs pool \"%s\"\n",
1114 #endif /* SPA_PROCESS */
1115 mutex_exit(&spa->spa_proc_lock);
1117 /* If we didn't create a process, we need to create our taskqs. */
1118 ASSERT(spa->spa_proc == &p0);
1119 if (spa->spa_proc == &p0) {
1120 spa_create_zio_taskqs(spa);
1124 * Start TRIM thread.
1126 trim_thread_create(spa);
1128 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1129 offsetof(vdev_t, vdev_config_dirty_node));
1130 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1131 offsetof(vdev_t, vdev_state_dirty_node));
1133 txg_list_create(&spa->spa_vdev_txg_list,
1134 offsetof(struct vdev, vdev_txg_node));
1136 avl_create(&spa->spa_errlist_scrub,
1137 spa_error_entry_compare, sizeof (spa_error_entry_t),
1138 offsetof(spa_error_entry_t, se_avl));
1139 avl_create(&spa->spa_errlist_last,
1140 spa_error_entry_compare, sizeof (spa_error_entry_t),
1141 offsetof(spa_error_entry_t, se_avl));
1145 * Opposite of spa_activate().
1148 spa_deactivate(spa_t *spa)
1150 ASSERT(spa->spa_sync_on == B_FALSE);
1151 ASSERT(spa->spa_dsl_pool == NULL);
1152 ASSERT(spa->spa_root_vdev == NULL);
1153 ASSERT(spa->spa_async_zio_root == NULL);
1154 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1157 * Stop TRIM thread in case spa_unload() wasn't called directly
1158 * before spa_deactivate().
1160 trim_thread_destroy(spa);
1162 txg_list_destroy(&spa->spa_vdev_txg_list);
1164 list_destroy(&spa->spa_config_dirty_list);
1165 list_destroy(&spa->spa_state_dirty_list);
1167 for (int t = 0; t < ZIO_TYPES; t++) {
1168 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1169 spa_taskqs_fini(spa, t, q);
1173 metaslab_class_destroy(spa->spa_normal_class);
1174 spa->spa_normal_class = NULL;
1176 metaslab_class_destroy(spa->spa_log_class);
1177 spa->spa_log_class = NULL;
1180 * If this was part of an import or the open otherwise failed, we may
1181 * still have errors left in the queues. Empty them just in case.
1183 spa_errlog_drain(spa);
1185 avl_destroy(&spa->spa_errlist_scrub);
1186 avl_destroy(&spa->spa_errlist_last);
1188 spa->spa_state = POOL_STATE_UNINITIALIZED;
1190 mutex_enter(&spa->spa_proc_lock);
1191 if (spa->spa_proc_state != SPA_PROC_NONE) {
1192 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1193 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1194 cv_broadcast(&spa->spa_proc_cv);
1195 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1196 ASSERT(spa->spa_proc != &p0);
1197 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1199 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1200 spa->spa_proc_state = SPA_PROC_NONE;
1202 ASSERT(spa->spa_proc == &p0);
1203 mutex_exit(&spa->spa_proc_lock);
1207 * We want to make sure spa_thread() has actually exited the ZFS
1208 * module, so that the module can't be unloaded out from underneath
1211 if (spa->spa_did != 0) {
1212 thread_join(spa->spa_did);
1215 #endif /* SPA_PROCESS */
1219 * Verify a pool configuration, and construct the vdev tree appropriately. This
1220 * will create all the necessary vdevs in the appropriate layout, with each vdev
1221 * in the CLOSED state. This will prep the pool before open/creation/import.
1222 * All vdev validation is done by the vdev_alloc() routine.
1225 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1226 uint_t id, int atype)
1232 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1235 if ((*vdp)->vdev_ops->vdev_op_leaf)
1238 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1241 if (error == ENOENT)
1247 return (SET_ERROR(EINVAL));
1250 for (int c = 0; c < children; c++) {
1252 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1260 ASSERT(*vdp != NULL);
1266 * Opposite of spa_load().
1269 spa_unload(spa_t *spa)
1273 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1278 trim_thread_destroy(spa);
1283 spa_async_suspend(spa);
1288 if (spa->spa_sync_on) {
1289 txg_sync_stop(spa->spa_dsl_pool);
1290 spa->spa_sync_on = B_FALSE;
1294 * Wait for any outstanding async I/O to complete.
1296 if (spa->spa_async_zio_root != NULL) {
1297 for (int i = 0; i < max_ncpus; i++)
1298 (void) zio_wait(spa->spa_async_zio_root[i]);
1299 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
1300 spa->spa_async_zio_root = NULL;
1303 bpobj_close(&spa->spa_deferred_bpobj);
1305 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1310 if (spa->spa_root_vdev)
1311 vdev_free(spa->spa_root_vdev);
1312 ASSERT(spa->spa_root_vdev == NULL);
1315 * Close the dsl pool.
1317 if (spa->spa_dsl_pool) {
1318 dsl_pool_close(spa->spa_dsl_pool);
1319 spa->spa_dsl_pool = NULL;
1320 spa->spa_meta_objset = NULL;
1327 * Drop and purge level 2 cache
1329 spa_l2cache_drop(spa);
1331 for (i = 0; i < spa->spa_spares.sav_count; i++)
1332 vdev_free(spa->spa_spares.sav_vdevs[i]);
1333 if (spa->spa_spares.sav_vdevs) {
1334 kmem_free(spa->spa_spares.sav_vdevs,
1335 spa->spa_spares.sav_count * sizeof (void *));
1336 spa->spa_spares.sav_vdevs = NULL;
1338 if (spa->spa_spares.sav_config) {
1339 nvlist_free(spa->spa_spares.sav_config);
1340 spa->spa_spares.sav_config = NULL;
1342 spa->spa_spares.sav_count = 0;
1344 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1345 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1346 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1348 if (spa->spa_l2cache.sav_vdevs) {
1349 kmem_free(spa->spa_l2cache.sav_vdevs,
1350 spa->spa_l2cache.sav_count * sizeof (void *));
1351 spa->spa_l2cache.sav_vdevs = NULL;
1353 if (spa->spa_l2cache.sav_config) {
1354 nvlist_free(spa->spa_l2cache.sav_config);
1355 spa->spa_l2cache.sav_config = NULL;
1357 spa->spa_l2cache.sav_count = 0;
1359 spa->spa_async_suspended = 0;
1361 if (spa->spa_comment != NULL) {
1362 spa_strfree(spa->spa_comment);
1363 spa->spa_comment = NULL;
1366 spa_config_exit(spa, SCL_ALL, FTAG);
1370 * Load (or re-load) the current list of vdevs describing the active spares for
1371 * this pool. When this is called, we have some form of basic information in
1372 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1373 * then re-generate a more complete list including status information.
1376 spa_load_spares(spa_t *spa)
1383 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1386 * First, close and free any existing spare vdevs.
1388 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1389 vd = spa->spa_spares.sav_vdevs[i];
1391 /* Undo the call to spa_activate() below */
1392 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1393 B_FALSE)) != NULL && tvd->vdev_isspare)
1394 spa_spare_remove(tvd);
1399 if (spa->spa_spares.sav_vdevs)
1400 kmem_free(spa->spa_spares.sav_vdevs,
1401 spa->spa_spares.sav_count * sizeof (void *));
1403 if (spa->spa_spares.sav_config == NULL)
1406 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1407 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1409 spa->spa_spares.sav_count = (int)nspares;
1410 spa->spa_spares.sav_vdevs = NULL;
1416 * Construct the array of vdevs, opening them to get status in the
1417 * process. For each spare, there is potentially two different vdev_t
1418 * structures associated with it: one in the list of spares (used only
1419 * for basic validation purposes) and one in the active vdev
1420 * configuration (if it's spared in). During this phase we open and
1421 * validate each vdev on the spare list. If the vdev also exists in the
1422 * active configuration, then we also mark this vdev as an active spare.
1424 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1426 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1427 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1428 VDEV_ALLOC_SPARE) == 0);
1431 spa->spa_spares.sav_vdevs[i] = vd;
1433 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1434 B_FALSE)) != NULL) {
1435 if (!tvd->vdev_isspare)
1439 * We only mark the spare active if we were successfully
1440 * able to load the vdev. Otherwise, importing a pool
1441 * with a bad active spare would result in strange
1442 * behavior, because multiple pool would think the spare
1443 * is actively in use.
1445 * There is a vulnerability here to an equally bizarre
1446 * circumstance, where a dead active spare is later
1447 * brought back to life (onlined or otherwise). Given
1448 * the rarity of this scenario, and the extra complexity
1449 * it adds, we ignore the possibility.
1451 if (!vdev_is_dead(tvd))
1452 spa_spare_activate(tvd);
1456 vd->vdev_aux = &spa->spa_spares;
1458 if (vdev_open(vd) != 0)
1461 if (vdev_validate_aux(vd) == 0)
1466 * Recompute the stashed list of spares, with status information
1469 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1470 DATA_TYPE_NVLIST_ARRAY) == 0);
1472 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1474 for (i = 0; i < spa->spa_spares.sav_count; i++)
1475 spares[i] = vdev_config_generate(spa,
1476 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1477 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1478 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1479 for (i = 0; i < spa->spa_spares.sav_count; i++)
1480 nvlist_free(spares[i]);
1481 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1485 * Load (or re-load) the current list of vdevs describing the active l2cache for
1486 * this pool. When this is called, we have some form of basic information in
1487 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1488 * then re-generate a more complete list including status information.
1489 * Devices which are already active have their details maintained, and are
1493 spa_load_l2cache(spa_t *spa)
1497 int i, j, oldnvdevs;
1499 vdev_t *vd, **oldvdevs, **newvdevs;
1500 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1502 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1504 if (sav->sav_config != NULL) {
1505 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1506 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1507 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1513 oldvdevs = sav->sav_vdevs;
1514 oldnvdevs = sav->sav_count;
1515 sav->sav_vdevs = NULL;
1519 * Process new nvlist of vdevs.
1521 for (i = 0; i < nl2cache; i++) {
1522 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1526 for (j = 0; j < oldnvdevs; j++) {
1528 if (vd != NULL && guid == vd->vdev_guid) {
1530 * Retain previous vdev for add/remove ops.
1538 if (newvdevs[i] == NULL) {
1542 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1543 VDEV_ALLOC_L2CACHE) == 0);
1548 * Commit this vdev as an l2cache device,
1549 * even if it fails to open.
1551 spa_l2cache_add(vd);
1556 spa_l2cache_activate(vd);
1558 if (vdev_open(vd) != 0)
1561 (void) vdev_validate_aux(vd);
1563 if (!vdev_is_dead(vd))
1564 l2arc_add_vdev(spa, vd);
1569 * Purge vdevs that were dropped
1571 for (i = 0; i < oldnvdevs; i++) {
1576 ASSERT(vd->vdev_isl2cache);
1578 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1579 pool != 0ULL && l2arc_vdev_present(vd))
1580 l2arc_remove_vdev(vd);
1581 vdev_clear_stats(vd);
1587 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1589 if (sav->sav_config == NULL)
1592 sav->sav_vdevs = newvdevs;
1593 sav->sav_count = (int)nl2cache;
1596 * Recompute the stashed list of l2cache devices, with status
1597 * information this time.
1599 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1600 DATA_TYPE_NVLIST_ARRAY) == 0);
1602 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1603 for (i = 0; i < sav->sav_count; i++)
1604 l2cache[i] = vdev_config_generate(spa,
1605 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1606 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1607 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1609 for (i = 0; i < sav->sav_count; i++)
1610 nvlist_free(l2cache[i]);
1612 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1616 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1619 char *packed = NULL;
1624 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1627 nvsize = *(uint64_t *)db->db_data;
1628 dmu_buf_rele(db, FTAG);
1630 packed = kmem_alloc(nvsize, KM_SLEEP);
1631 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1634 error = nvlist_unpack(packed, nvsize, value, 0);
1635 kmem_free(packed, nvsize);
1641 * Checks to see if the given vdev could not be opened, in which case we post a
1642 * sysevent to notify the autoreplace code that the device has been removed.
1645 spa_check_removed(vdev_t *vd)
1647 for (int c = 0; c < vd->vdev_children; c++)
1648 spa_check_removed(vd->vdev_child[c]);
1650 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1652 zfs_post_autoreplace(vd->vdev_spa, vd);
1653 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1658 * Validate the current config against the MOS config
1661 spa_config_valid(spa_t *spa, nvlist_t *config)
1663 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1666 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1668 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1669 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1671 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1674 * If we're doing a normal import, then build up any additional
1675 * diagnostic information about missing devices in this config.
1676 * We'll pass this up to the user for further processing.
1678 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1679 nvlist_t **child, *nv;
1682 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1684 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1686 for (int c = 0; c < rvd->vdev_children; c++) {
1687 vdev_t *tvd = rvd->vdev_child[c];
1688 vdev_t *mtvd = mrvd->vdev_child[c];
1690 if (tvd->vdev_ops == &vdev_missing_ops &&
1691 mtvd->vdev_ops != &vdev_missing_ops &&
1693 child[idx++] = vdev_config_generate(spa, mtvd,
1698 VERIFY(nvlist_add_nvlist_array(nv,
1699 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1700 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1701 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1703 for (int i = 0; i < idx; i++)
1704 nvlist_free(child[i]);
1707 kmem_free(child, rvd->vdev_children * sizeof (char **));
1711 * Compare the root vdev tree with the information we have
1712 * from the MOS config (mrvd). Check each top-level vdev
1713 * with the corresponding MOS config top-level (mtvd).
1715 for (int c = 0; c < rvd->vdev_children; c++) {
1716 vdev_t *tvd = rvd->vdev_child[c];
1717 vdev_t *mtvd = mrvd->vdev_child[c];
1720 * Resolve any "missing" vdevs in the current configuration.
1721 * If we find that the MOS config has more accurate information
1722 * about the top-level vdev then use that vdev instead.
1724 if (tvd->vdev_ops == &vdev_missing_ops &&
1725 mtvd->vdev_ops != &vdev_missing_ops) {
1727 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1731 * Device specific actions.
1733 if (mtvd->vdev_islog) {
1734 spa_set_log_state(spa, SPA_LOG_CLEAR);
1737 * XXX - once we have 'readonly' pool
1738 * support we should be able to handle
1739 * missing data devices by transitioning
1740 * the pool to readonly.
1746 * Swap the missing vdev with the data we were
1747 * able to obtain from the MOS config.
1749 vdev_remove_child(rvd, tvd);
1750 vdev_remove_child(mrvd, mtvd);
1752 vdev_add_child(rvd, mtvd);
1753 vdev_add_child(mrvd, tvd);
1755 spa_config_exit(spa, SCL_ALL, FTAG);
1757 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1760 } else if (mtvd->vdev_islog) {
1762 * Load the slog device's state from the MOS config
1763 * since it's possible that the label does not
1764 * contain the most up-to-date information.
1766 vdev_load_log_state(tvd, mtvd);
1771 spa_config_exit(spa, SCL_ALL, FTAG);
1774 * Ensure we were able to validate the config.
1776 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1780 * Check for missing log devices
1783 spa_check_logs(spa_t *spa)
1785 boolean_t rv = B_FALSE;
1787 switch (spa->spa_log_state) {
1788 case SPA_LOG_MISSING:
1789 /* need to recheck in case slog has been restored */
1790 case SPA_LOG_UNKNOWN:
1791 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1792 NULL, DS_FIND_CHILDREN) != 0);
1794 spa_set_log_state(spa, SPA_LOG_MISSING);
1801 spa_passivate_log(spa_t *spa)
1803 vdev_t *rvd = spa->spa_root_vdev;
1804 boolean_t slog_found = B_FALSE;
1806 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1808 if (!spa_has_slogs(spa))
1811 for (int c = 0; c < rvd->vdev_children; c++) {
1812 vdev_t *tvd = rvd->vdev_child[c];
1813 metaslab_group_t *mg = tvd->vdev_mg;
1815 if (tvd->vdev_islog) {
1816 metaslab_group_passivate(mg);
1817 slog_found = B_TRUE;
1821 return (slog_found);
1825 spa_activate_log(spa_t *spa)
1827 vdev_t *rvd = spa->spa_root_vdev;
1829 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1831 for (int c = 0; c < rvd->vdev_children; c++) {
1832 vdev_t *tvd = rvd->vdev_child[c];
1833 metaslab_group_t *mg = tvd->vdev_mg;
1835 if (tvd->vdev_islog)
1836 metaslab_group_activate(mg);
1841 spa_offline_log(spa_t *spa)
1845 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1846 NULL, DS_FIND_CHILDREN);
1849 * We successfully offlined the log device, sync out the
1850 * current txg so that the "stubby" block can be removed
1853 txg_wait_synced(spa->spa_dsl_pool, 0);
1859 spa_aux_check_removed(spa_aux_vdev_t *sav)
1863 for (i = 0; i < sav->sav_count; i++)
1864 spa_check_removed(sav->sav_vdevs[i]);
1868 spa_claim_notify(zio_t *zio)
1870 spa_t *spa = zio->io_spa;
1875 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1876 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1877 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1878 mutex_exit(&spa->spa_props_lock);
1881 typedef struct spa_load_error {
1882 uint64_t sle_meta_count;
1883 uint64_t sle_data_count;
1887 spa_load_verify_done(zio_t *zio)
1889 blkptr_t *bp = zio->io_bp;
1890 spa_load_error_t *sle = zio->io_private;
1891 dmu_object_type_t type = BP_GET_TYPE(bp);
1892 int error = zio->io_error;
1893 spa_t *spa = zio->io_spa;
1896 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1897 type != DMU_OT_INTENT_LOG)
1898 atomic_inc_64(&sle->sle_meta_count);
1900 atomic_inc_64(&sle->sle_data_count);
1902 zio_data_buf_free(zio->io_data, zio->io_size);
1904 mutex_enter(&spa->spa_scrub_lock);
1905 spa->spa_scrub_inflight--;
1906 cv_broadcast(&spa->spa_scrub_io_cv);
1907 mutex_exit(&spa->spa_scrub_lock);
1911 * Maximum number of concurrent scrub i/os to create while verifying
1912 * a pool while importing it.
1914 int spa_load_verify_maxinflight = 10000;
1915 boolean_t spa_load_verify_metadata = B_TRUE;
1916 boolean_t spa_load_verify_data = B_TRUE;
1918 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1919 &spa_load_verify_maxinflight, 0,
1920 "Maximum number of concurrent scrub I/Os to create while verifying a "
1921 "pool while importing it");
1923 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1924 &spa_load_verify_metadata, 0,
1925 "Check metadata on import?");
1927 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1928 &spa_load_verify_data, 0,
1929 "Check user data on import?");
1933 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1934 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1936 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1939 * Note: normally this routine will not be called if
1940 * spa_load_verify_metadata is not set. However, it may be useful
1941 * to manually set the flag after the traversal has begun.
1943 if (!spa_load_verify_metadata)
1945 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1949 size_t size = BP_GET_PSIZE(bp);
1950 void *data = zio_data_buf_alloc(size);
1952 mutex_enter(&spa->spa_scrub_lock);
1953 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1954 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1955 spa->spa_scrub_inflight++;
1956 mutex_exit(&spa->spa_scrub_lock);
1958 zio_nowait(zio_read(rio, spa, bp, data, size,
1959 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1960 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1961 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1966 spa_load_verify(spa_t *spa)
1969 spa_load_error_t sle = { 0 };
1970 zpool_rewind_policy_t policy;
1971 boolean_t verify_ok = B_FALSE;
1974 zpool_get_rewind_policy(spa->spa_config, &policy);
1976 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1979 rio = zio_root(spa, NULL, &sle,
1980 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1982 if (spa_load_verify_metadata) {
1983 error = traverse_pool(spa, spa->spa_verify_min_txg,
1984 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
1985 spa_load_verify_cb, rio);
1988 (void) zio_wait(rio);
1990 spa->spa_load_meta_errors = sle.sle_meta_count;
1991 spa->spa_load_data_errors = sle.sle_data_count;
1993 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1994 sle.sle_data_count <= policy.zrp_maxdata) {
1998 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1999 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2001 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2002 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2003 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2004 VERIFY(nvlist_add_int64(spa->spa_load_info,
2005 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2006 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2007 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2009 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2013 if (error != ENXIO && error != EIO)
2014 error = SET_ERROR(EIO);
2018 return (verify_ok ? 0 : EIO);
2022 * Find a value in the pool props object.
2025 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2027 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2028 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2032 * Find a value in the pool directory object.
2035 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2037 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2038 name, sizeof (uint64_t), 1, val));
2042 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2044 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2049 * Fix up config after a partly-completed split. This is done with the
2050 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2051 * pool have that entry in their config, but only the splitting one contains
2052 * a list of all the guids of the vdevs that are being split off.
2054 * This function determines what to do with that list: either rejoin
2055 * all the disks to the pool, or complete the splitting process. To attempt
2056 * the rejoin, each disk that is offlined is marked online again, and
2057 * we do a reopen() call. If the vdev label for every disk that was
2058 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2059 * then we call vdev_split() on each disk, and complete the split.
2061 * Otherwise we leave the config alone, with all the vdevs in place in
2062 * the original pool.
2065 spa_try_repair(spa_t *spa, nvlist_t *config)
2072 boolean_t attempt_reopen;
2074 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2077 /* check that the config is complete */
2078 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2079 &glist, &gcount) != 0)
2082 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2084 /* attempt to online all the vdevs & validate */
2085 attempt_reopen = B_TRUE;
2086 for (i = 0; i < gcount; i++) {
2087 if (glist[i] == 0) /* vdev is hole */
2090 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2091 if (vd[i] == NULL) {
2093 * Don't bother attempting to reopen the disks;
2094 * just do the split.
2096 attempt_reopen = B_FALSE;
2098 /* attempt to re-online it */
2099 vd[i]->vdev_offline = B_FALSE;
2103 if (attempt_reopen) {
2104 vdev_reopen(spa->spa_root_vdev);
2106 /* check each device to see what state it's in */
2107 for (extracted = 0, i = 0; i < gcount; i++) {
2108 if (vd[i] != NULL &&
2109 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2116 * If every disk has been moved to the new pool, or if we never
2117 * even attempted to look at them, then we split them off for
2120 if (!attempt_reopen || gcount == extracted) {
2121 for (i = 0; i < gcount; i++)
2124 vdev_reopen(spa->spa_root_vdev);
2127 kmem_free(vd, gcount * sizeof (vdev_t *));
2131 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2132 boolean_t mosconfig)
2134 nvlist_t *config = spa->spa_config;
2135 char *ereport = FM_EREPORT_ZFS_POOL;
2141 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2142 return (SET_ERROR(EINVAL));
2144 ASSERT(spa->spa_comment == NULL);
2145 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2146 spa->spa_comment = spa_strdup(comment);
2149 * Versioning wasn't explicitly added to the label until later, so if
2150 * it's not present treat it as the initial version.
2152 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2153 &spa->spa_ubsync.ub_version) != 0)
2154 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2156 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2157 &spa->spa_config_txg);
2159 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2160 spa_guid_exists(pool_guid, 0)) {
2161 error = SET_ERROR(EEXIST);
2163 spa->spa_config_guid = pool_guid;
2165 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2167 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2171 nvlist_free(spa->spa_load_info);
2172 spa->spa_load_info = fnvlist_alloc();
2174 gethrestime(&spa->spa_loaded_ts);
2175 error = spa_load_impl(spa, pool_guid, config, state, type,
2176 mosconfig, &ereport);
2179 spa->spa_minref = refcount_count(&spa->spa_refcount);
2181 if (error != EEXIST) {
2182 spa->spa_loaded_ts.tv_sec = 0;
2183 spa->spa_loaded_ts.tv_nsec = 0;
2185 if (error != EBADF) {
2186 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2189 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2196 * Load an existing storage pool, using the pool's builtin spa_config as a
2197 * source of configuration information.
2200 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2201 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2205 nvlist_t *nvroot = NULL;
2208 uberblock_t *ub = &spa->spa_uberblock;
2209 uint64_t children, config_cache_txg = spa->spa_config_txg;
2210 int orig_mode = spa->spa_mode;
2213 boolean_t missing_feat_write = B_FALSE;
2216 * If this is an untrusted config, access the pool in read-only mode.
2217 * This prevents things like resilvering recently removed devices.
2220 spa->spa_mode = FREAD;
2222 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2224 spa->spa_load_state = state;
2226 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2227 return (SET_ERROR(EINVAL));
2229 parse = (type == SPA_IMPORT_EXISTING ?
2230 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2233 * Create "The Godfather" zio to hold all async IOs
2235 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2237 for (int i = 0; i < max_ncpus; i++) {
2238 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2239 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2240 ZIO_FLAG_GODFATHER);
2244 * Parse the configuration into a vdev tree. We explicitly set the
2245 * value that will be returned by spa_version() since parsing the
2246 * configuration requires knowing the version number.
2248 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2249 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2250 spa_config_exit(spa, SCL_ALL, FTAG);
2255 ASSERT(spa->spa_root_vdev == rvd);
2256 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2257 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
2259 if (type != SPA_IMPORT_ASSEMBLE) {
2260 ASSERT(spa_guid(spa) == pool_guid);
2264 * Try to open all vdevs, loading each label in the process.
2266 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2267 error = vdev_open(rvd);
2268 spa_config_exit(spa, SCL_ALL, FTAG);
2273 * We need to validate the vdev labels against the configuration that
2274 * we have in hand, which is dependent on the setting of mosconfig. If
2275 * mosconfig is true then we're validating the vdev labels based on
2276 * that config. Otherwise, we're validating against the cached config
2277 * (zpool.cache) that was read when we loaded the zfs module, and then
2278 * later we will recursively call spa_load() and validate against
2281 * If we're assembling a new pool that's been split off from an
2282 * existing pool, the labels haven't yet been updated so we skip
2283 * validation for now.
2285 if (type != SPA_IMPORT_ASSEMBLE) {
2286 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2287 error = vdev_validate(rvd, mosconfig);
2288 spa_config_exit(spa, SCL_ALL, FTAG);
2293 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2294 return (SET_ERROR(ENXIO));
2298 * Find the best uberblock.
2300 vdev_uberblock_load(rvd, ub, &label);
2303 * If we weren't able to find a single valid uberblock, return failure.
2305 if (ub->ub_txg == 0) {
2307 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2311 * If the pool has an unsupported version we can't open it.
2313 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2315 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2318 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2322 * If we weren't able to find what's necessary for reading the
2323 * MOS in the label, return failure.
2325 if (label == NULL || nvlist_lookup_nvlist(label,
2326 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2328 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2333 * Update our in-core representation with the definitive values
2336 nvlist_free(spa->spa_label_features);
2337 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2343 * Look through entries in the label nvlist's features_for_read. If
2344 * there is a feature listed there which we don't understand then we
2345 * cannot open a pool.
2347 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2348 nvlist_t *unsup_feat;
2350 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2353 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2355 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2356 if (!zfeature_is_supported(nvpair_name(nvp))) {
2357 VERIFY(nvlist_add_string(unsup_feat,
2358 nvpair_name(nvp), "") == 0);
2362 if (!nvlist_empty(unsup_feat)) {
2363 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2364 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2365 nvlist_free(unsup_feat);
2366 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2370 nvlist_free(unsup_feat);
2374 * If the vdev guid sum doesn't match the uberblock, we have an
2375 * incomplete configuration. We first check to see if the pool
2376 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2377 * If it is, defer the vdev_guid_sum check till later so we
2378 * can handle missing vdevs.
2380 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2381 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2382 rvd->vdev_guid_sum != ub->ub_guid_sum)
2383 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2385 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2386 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2387 spa_try_repair(spa, config);
2388 spa_config_exit(spa, SCL_ALL, FTAG);
2389 nvlist_free(spa->spa_config_splitting);
2390 spa->spa_config_splitting = NULL;
2394 * Initialize internal SPA structures.
2396 spa->spa_state = POOL_STATE_ACTIVE;
2397 spa->spa_ubsync = spa->spa_uberblock;
2398 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2399 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2400 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2401 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2402 spa->spa_claim_max_txg = spa->spa_first_txg;
2403 spa->spa_prev_software_version = ub->ub_software_version;
2405 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2407 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2408 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2410 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2411 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2413 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2414 boolean_t missing_feat_read = B_FALSE;
2415 nvlist_t *unsup_feat, *enabled_feat;
2417 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2418 &spa->spa_feat_for_read_obj) != 0) {
2419 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2422 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2423 &spa->spa_feat_for_write_obj) != 0) {
2424 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2427 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2428 &spa->spa_feat_desc_obj) != 0) {
2429 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2432 enabled_feat = fnvlist_alloc();
2433 unsup_feat = fnvlist_alloc();
2435 if (!spa_features_check(spa, B_FALSE,
2436 unsup_feat, enabled_feat))
2437 missing_feat_read = B_TRUE;
2439 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2440 if (!spa_features_check(spa, B_TRUE,
2441 unsup_feat, enabled_feat)) {
2442 missing_feat_write = B_TRUE;
2446 fnvlist_add_nvlist(spa->spa_load_info,
2447 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2449 if (!nvlist_empty(unsup_feat)) {
2450 fnvlist_add_nvlist(spa->spa_load_info,
2451 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2454 fnvlist_free(enabled_feat);
2455 fnvlist_free(unsup_feat);
2457 if (!missing_feat_read) {
2458 fnvlist_add_boolean(spa->spa_load_info,
2459 ZPOOL_CONFIG_CAN_RDONLY);
2463 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2464 * twofold: to determine whether the pool is available for
2465 * import in read-write mode and (if it is not) whether the
2466 * pool is available for import in read-only mode. If the pool
2467 * is available for import in read-write mode, it is displayed
2468 * as available in userland; if it is not available for import
2469 * in read-only mode, it is displayed as unavailable in
2470 * userland. If the pool is available for import in read-only
2471 * mode but not read-write mode, it is displayed as unavailable
2472 * in userland with a special note that the pool is actually
2473 * available for open in read-only mode.
2475 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2476 * missing a feature for write, we must first determine whether
2477 * the pool can be opened read-only before returning to
2478 * userland in order to know whether to display the
2479 * abovementioned note.
2481 if (missing_feat_read || (missing_feat_write &&
2482 spa_writeable(spa))) {
2483 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2488 * Load refcounts for ZFS features from disk into an in-memory
2489 * cache during SPA initialization.
2491 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2494 error = feature_get_refcount_from_disk(spa,
2495 &spa_feature_table[i], &refcount);
2497 spa->spa_feat_refcount_cache[i] = refcount;
2498 } else if (error == ENOTSUP) {
2499 spa->spa_feat_refcount_cache[i] =
2500 SPA_FEATURE_DISABLED;
2502 return (spa_vdev_err(rvd,
2503 VDEV_AUX_CORRUPT_DATA, EIO));
2508 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2509 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2510 &spa->spa_feat_enabled_txg_obj) != 0)
2511 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2514 spa->spa_is_initializing = B_TRUE;
2515 error = dsl_pool_open(spa->spa_dsl_pool);
2516 spa->spa_is_initializing = B_FALSE;
2518 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2522 nvlist_t *policy = NULL, *nvconfig;
2524 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2525 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2527 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2528 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2530 unsigned long myhostid = 0;
2532 VERIFY(nvlist_lookup_string(nvconfig,
2533 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2536 myhostid = zone_get_hostid(NULL);
2539 * We're emulating the system's hostid in userland, so
2540 * we can't use zone_get_hostid().
2542 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2543 #endif /* _KERNEL */
2544 if (check_hostid && hostid != 0 && myhostid != 0 &&
2545 hostid != myhostid) {
2546 nvlist_free(nvconfig);
2547 cmn_err(CE_WARN, "pool '%s' could not be "
2548 "loaded as it was last accessed by "
2549 "another system (host: %s hostid: 0x%lx). "
2550 "See: http://illumos.org/msg/ZFS-8000-EY",
2551 spa_name(spa), hostname,
2552 (unsigned long)hostid);
2553 return (SET_ERROR(EBADF));
2556 if (nvlist_lookup_nvlist(spa->spa_config,
2557 ZPOOL_REWIND_POLICY, &policy) == 0)
2558 VERIFY(nvlist_add_nvlist(nvconfig,
2559 ZPOOL_REWIND_POLICY, policy) == 0);
2561 spa_config_set(spa, nvconfig);
2563 spa_deactivate(spa);
2564 spa_activate(spa, orig_mode);
2566 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2569 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2570 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2571 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2573 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2576 * Load the bit that tells us to use the new accounting function
2577 * (raid-z deflation). If we have an older pool, this will not
2580 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2581 if (error != 0 && error != ENOENT)
2582 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2584 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2585 &spa->spa_creation_version);
2586 if (error != 0 && error != ENOENT)
2587 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2590 * Load the persistent error log. If we have an older pool, this will
2593 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2594 if (error != 0 && error != ENOENT)
2595 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2597 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2598 &spa->spa_errlog_scrub);
2599 if (error != 0 && error != ENOENT)
2600 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2603 * Load the history object. If we have an older pool, this
2604 * will not be present.
2606 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2607 if (error != 0 && error != ENOENT)
2608 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2611 * If we're assembling the pool from the split-off vdevs of
2612 * an existing pool, we don't want to attach the spares & cache
2617 * Load any hot spares for this pool.
2619 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2620 if (error != 0 && error != ENOENT)
2621 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2622 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2623 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2624 if (load_nvlist(spa, spa->spa_spares.sav_object,
2625 &spa->spa_spares.sav_config) != 0)
2626 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2628 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2629 spa_load_spares(spa);
2630 spa_config_exit(spa, SCL_ALL, FTAG);
2631 } else if (error == 0) {
2632 spa->spa_spares.sav_sync = B_TRUE;
2636 * Load any level 2 ARC devices for this pool.
2638 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2639 &spa->spa_l2cache.sav_object);
2640 if (error != 0 && error != ENOENT)
2641 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2642 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2643 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2644 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2645 &spa->spa_l2cache.sav_config) != 0)
2646 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2648 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2649 spa_load_l2cache(spa);
2650 spa_config_exit(spa, SCL_ALL, FTAG);
2651 } else if (error == 0) {
2652 spa->spa_l2cache.sav_sync = B_TRUE;
2655 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2657 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2658 if (error && error != ENOENT)
2659 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2662 uint64_t autoreplace;
2664 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2665 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2666 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2667 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2668 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2669 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2670 &spa->spa_dedup_ditto);
2672 spa->spa_autoreplace = (autoreplace != 0);
2676 * If the 'autoreplace' property is set, then post a resource notifying
2677 * the ZFS DE that it should not issue any faults for unopenable
2678 * devices. We also iterate over the vdevs, and post a sysevent for any
2679 * unopenable vdevs so that the normal autoreplace handler can take
2682 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2683 spa_check_removed(spa->spa_root_vdev);
2685 * For the import case, this is done in spa_import(), because
2686 * at this point we're using the spare definitions from
2687 * the MOS config, not necessarily from the userland config.
2689 if (state != SPA_LOAD_IMPORT) {
2690 spa_aux_check_removed(&spa->spa_spares);
2691 spa_aux_check_removed(&spa->spa_l2cache);
2696 * Load the vdev state for all toplevel vdevs.
2701 * Propagate the leaf DTLs we just loaded all the way up the tree.
2703 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2704 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2705 spa_config_exit(spa, SCL_ALL, FTAG);
2708 * Load the DDTs (dedup tables).
2710 error = ddt_load(spa);
2712 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2714 spa_update_dspace(spa);
2717 * Validate the config, using the MOS config to fill in any
2718 * information which might be missing. If we fail to validate
2719 * the config then declare the pool unfit for use. If we're
2720 * assembling a pool from a split, the log is not transferred
2723 if (type != SPA_IMPORT_ASSEMBLE) {
2726 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2727 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2729 if (!spa_config_valid(spa, nvconfig)) {
2730 nvlist_free(nvconfig);
2731 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2734 nvlist_free(nvconfig);
2737 * Now that we've validated the config, check the state of the
2738 * root vdev. If it can't be opened, it indicates one or
2739 * more toplevel vdevs are faulted.
2741 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2742 return (SET_ERROR(ENXIO));
2744 if (spa_check_logs(spa)) {
2745 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2746 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2750 if (missing_feat_write) {
2751 ASSERT(state == SPA_LOAD_TRYIMPORT);
2754 * At this point, we know that we can open the pool in
2755 * read-only mode but not read-write mode. We now have enough
2756 * information and can return to userland.
2758 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2762 * We've successfully opened the pool, verify that we're ready
2763 * to start pushing transactions.
2765 if (state != SPA_LOAD_TRYIMPORT) {
2766 if (error = spa_load_verify(spa))
2767 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2771 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2772 spa->spa_load_max_txg == UINT64_MAX)) {
2774 int need_update = B_FALSE;
2776 ASSERT(state != SPA_LOAD_TRYIMPORT);
2779 * Claim log blocks that haven't been committed yet.
2780 * This must all happen in a single txg.
2781 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2782 * invoked from zil_claim_log_block()'s i/o done callback.
2783 * Price of rollback is that we abandon the log.
2785 spa->spa_claiming = B_TRUE;
2787 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2788 spa_first_txg(spa));
2789 (void) dmu_objset_find(spa_name(spa),
2790 zil_claim, tx, DS_FIND_CHILDREN);
2793 spa->spa_claiming = B_FALSE;
2795 spa_set_log_state(spa, SPA_LOG_GOOD);
2796 spa->spa_sync_on = B_TRUE;
2797 txg_sync_start(spa->spa_dsl_pool);
2800 * Wait for all claims to sync. We sync up to the highest
2801 * claimed log block birth time so that claimed log blocks
2802 * don't appear to be from the future. spa_claim_max_txg
2803 * will have been set for us by either zil_check_log_chain()
2804 * (invoked from spa_check_logs()) or zil_claim() above.
2806 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2809 * If the config cache is stale, or we have uninitialized
2810 * metaslabs (see spa_vdev_add()), then update the config.
2812 * If this is a verbatim import, trust the current
2813 * in-core spa_config and update the disk labels.
2815 if (config_cache_txg != spa->spa_config_txg ||
2816 state == SPA_LOAD_IMPORT ||
2817 state == SPA_LOAD_RECOVER ||
2818 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2819 need_update = B_TRUE;
2821 for (int c = 0; c < rvd->vdev_children; c++)
2822 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2823 need_update = B_TRUE;
2826 * Update the config cache asychronously in case we're the
2827 * root pool, in which case the config cache isn't writable yet.
2830 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2833 * Check all DTLs to see if anything needs resilvering.
2835 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2836 vdev_resilver_needed(rvd, NULL, NULL))
2837 spa_async_request(spa, SPA_ASYNC_RESILVER);
2840 * Log the fact that we booted up (so that we can detect if
2841 * we rebooted in the middle of an operation).
2843 spa_history_log_version(spa, "open");
2846 * Delete any inconsistent datasets.
2848 (void) dmu_objset_find(spa_name(spa),
2849 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2852 * Clean up any stale temporary dataset userrefs.
2854 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2861 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2863 int mode = spa->spa_mode;
2866 spa_deactivate(spa);
2868 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2870 spa_activate(spa, mode);
2871 spa_async_suspend(spa);
2873 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2877 * If spa_load() fails this function will try loading prior txg's. If
2878 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2879 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2880 * function will not rewind the pool and will return the same error as
2884 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2885 uint64_t max_request, int rewind_flags)
2887 nvlist_t *loadinfo = NULL;
2888 nvlist_t *config = NULL;
2889 int load_error, rewind_error;
2890 uint64_t safe_rewind_txg;
2893 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2894 spa->spa_load_max_txg = spa->spa_load_txg;
2895 spa_set_log_state(spa, SPA_LOG_CLEAR);
2897 spa->spa_load_max_txg = max_request;
2898 if (max_request != UINT64_MAX)
2899 spa->spa_extreme_rewind = B_TRUE;
2902 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2904 if (load_error == 0)
2907 if (spa->spa_root_vdev != NULL)
2908 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2910 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2911 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2913 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2914 nvlist_free(config);
2915 return (load_error);
2918 if (state == SPA_LOAD_RECOVER) {
2919 /* Price of rolling back is discarding txgs, including log */
2920 spa_set_log_state(spa, SPA_LOG_CLEAR);
2923 * If we aren't rolling back save the load info from our first
2924 * import attempt so that we can restore it after attempting
2927 loadinfo = spa->spa_load_info;
2928 spa->spa_load_info = fnvlist_alloc();
2931 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2932 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2933 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2934 TXG_INITIAL : safe_rewind_txg;
2937 * Continue as long as we're finding errors, we're still within
2938 * the acceptable rewind range, and we're still finding uberblocks
2940 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2941 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2942 if (spa->spa_load_max_txg < safe_rewind_txg)
2943 spa->spa_extreme_rewind = B_TRUE;
2944 rewind_error = spa_load_retry(spa, state, mosconfig);
2947 spa->spa_extreme_rewind = B_FALSE;
2948 spa->spa_load_max_txg = UINT64_MAX;
2950 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2951 spa_config_set(spa, config);
2953 if (state == SPA_LOAD_RECOVER) {
2954 ASSERT3P(loadinfo, ==, NULL);
2955 return (rewind_error);
2957 /* Store the rewind info as part of the initial load info */
2958 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2959 spa->spa_load_info);
2961 /* Restore the initial load info */
2962 fnvlist_free(spa->spa_load_info);
2963 spa->spa_load_info = loadinfo;
2965 return (load_error);
2972 * The import case is identical to an open except that the configuration is sent
2973 * down from userland, instead of grabbed from the configuration cache. For the
2974 * case of an open, the pool configuration will exist in the
2975 * POOL_STATE_UNINITIALIZED state.
2977 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2978 * the same time open the pool, without having to keep around the spa_t in some
2982 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2986 spa_load_state_t state = SPA_LOAD_OPEN;
2988 int locked = B_FALSE;
2989 int firstopen = B_FALSE;
2994 * As disgusting as this is, we need to support recursive calls to this
2995 * function because dsl_dir_open() is called during spa_load(), and ends
2996 * up calling spa_open() again. The real fix is to figure out how to
2997 * avoid dsl_dir_open() calling this in the first place.
2999 if (mutex_owner(&spa_namespace_lock) != curthread) {
3000 mutex_enter(&spa_namespace_lock);
3004 if ((spa = spa_lookup(pool)) == NULL) {
3006 mutex_exit(&spa_namespace_lock);
3007 return (SET_ERROR(ENOENT));
3010 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
3011 zpool_rewind_policy_t policy;
3015 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
3017 if (policy.zrp_request & ZPOOL_DO_REWIND)
3018 state = SPA_LOAD_RECOVER;
3020 spa_activate(spa, spa_mode_global);
3022 if (state != SPA_LOAD_RECOVER)
3023 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3025 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3026 policy.zrp_request);
3028 if (error == EBADF) {
3030 * If vdev_validate() returns failure (indicated by
3031 * EBADF), it indicates that one of the vdevs indicates
3032 * that the pool has been exported or destroyed. If
3033 * this is the case, the config cache is out of sync and
3034 * we should remove the pool from the namespace.
3037 spa_deactivate(spa);
3038 spa_config_sync(spa, B_TRUE, B_TRUE);
3041 mutex_exit(&spa_namespace_lock);
3042 return (SET_ERROR(ENOENT));
3047 * We can't open the pool, but we still have useful
3048 * information: the state of each vdev after the
3049 * attempted vdev_open(). Return this to the user.
3051 if (config != NULL && spa->spa_config) {
3052 VERIFY(nvlist_dup(spa->spa_config, config,
3054 VERIFY(nvlist_add_nvlist(*config,
3055 ZPOOL_CONFIG_LOAD_INFO,
3056 spa->spa_load_info) == 0);
3059 spa_deactivate(spa);
3060 spa->spa_last_open_failed = error;
3062 mutex_exit(&spa_namespace_lock);
3068 spa_open_ref(spa, tag);
3071 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3074 * If we've recovered the pool, pass back any information we
3075 * gathered while doing the load.
3077 if (state == SPA_LOAD_RECOVER) {
3078 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3079 spa->spa_load_info) == 0);
3083 spa->spa_last_open_failed = 0;
3084 spa->spa_last_ubsync_txg = 0;
3085 spa->spa_load_txg = 0;
3086 mutex_exit(&spa_namespace_lock);
3090 zvol_create_minors(spa->spa_name);
3101 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3104 return (spa_open_common(name, spapp, tag, policy, config));
3108 spa_open(const char *name, spa_t **spapp, void *tag)
3110 return (spa_open_common(name, spapp, tag, NULL, NULL));
3114 * Lookup the given spa_t, incrementing the inject count in the process,
3115 * preventing it from being exported or destroyed.
3118 spa_inject_addref(char *name)
3122 mutex_enter(&spa_namespace_lock);
3123 if ((spa = spa_lookup(name)) == NULL) {
3124 mutex_exit(&spa_namespace_lock);
3127 spa->spa_inject_ref++;
3128 mutex_exit(&spa_namespace_lock);
3134 spa_inject_delref(spa_t *spa)
3136 mutex_enter(&spa_namespace_lock);
3137 spa->spa_inject_ref--;
3138 mutex_exit(&spa_namespace_lock);
3142 * Add spares device information to the nvlist.
3145 spa_add_spares(spa_t *spa, nvlist_t *config)
3155 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3157 if (spa->spa_spares.sav_count == 0)
3160 VERIFY(nvlist_lookup_nvlist(config,
3161 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3162 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3163 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3165 VERIFY(nvlist_add_nvlist_array(nvroot,
3166 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3167 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3168 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3171 * Go through and find any spares which have since been
3172 * repurposed as an active spare. If this is the case, update
3173 * their status appropriately.
3175 for (i = 0; i < nspares; i++) {
3176 VERIFY(nvlist_lookup_uint64(spares[i],
3177 ZPOOL_CONFIG_GUID, &guid) == 0);
3178 if (spa_spare_exists(guid, &pool, NULL) &&
3180 VERIFY(nvlist_lookup_uint64_array(
3181 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3182 (uint64_t **)&vs, &vsc) == 0);
3183 vs->vs_state = VDEV_STATE_CANT_OPEN;
3184 vs->vs_aux = VDEV_AUX_SPARED;
3191 * Add l2cache device information to the nvlist, including vdev stats.
3194 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3197 uint_t i, j, nl2cache;
3204 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3206 if (spa->spa_l2cache.sav_count == 0)
3209 VERIFY(nvlist_lookup_nvlist(config,
3210 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3211 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3212 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3213 if (nl2cache != 0) {
3214 VERIFY(nvlist_add_nvlist_array(nvroot,
3215 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3216 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3217 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3220 * Update level 2 cache device stats.
3223 for (i = 0; i < nl2cache; i++) {
3224 VERIFY(nvlist_lookup_uint64(l2cache[i],
3225 ZPOOL_CONFIG_GUID, &guid) == 0);
3228 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3230 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3231 vd = spa->spa_l2cache.sav_vdevs[j];
3237 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3238 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3240 vdev_get_stats(vd, vs);
3246 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3252 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3253 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3255 /* We may be unable to read features if pool is suspended. */
3256 if (spa_suspended(spa))
3259 if (spa->spa_feat_for_read_obj != 0) {
3260 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3261 spa->spa_feat_for_read_obj);
3262 zap_cursor_retrieve(&zc, &za) == 0;
3263 zap_cursor_advance(&zc)) {
3264 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3265 za.za_num_integers == 1);
3266 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3267 za.za_first_integer));
3269 zap_cursor_fini(&zc);
3272 if (spa->spa_feat_for_write_obj != 0) {
3273 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3274 spa->spa_feat_for_write_obj);
3275 zap_cursor_retrieve(&zc, &za) == 0;
3276 zap_cursor_advance(&zc)) {
3277 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3278 za.za_num_integers == 1);
3279 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3280 za.za_first_integer));
3282 zap_cursor_fini(&zc);
3286 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3288 nvlist_free(features);
3292 spa_get_stats(const char *name, nvlist_t **config,
3293 char *altroot, size_t buflen)
3299 error = spa_open_common(name, &spa, FTAG, NULL, config);
3303 * This still leaves a window of inconsistency where the spares
3304 * or l2cache devices could change and the config would be
3305 * self-inconsistent.
3307 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3309 if (*config != NULL) {
3310 uint64_t loadtimes[2];
3312 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3313 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3314 VERIFY(nvlist_add_uint64_array(*config,
3315 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3317 VERIFY(nvlist_add_uint64(*config,
3318 ZPOOL_CONFIG_ERRCOUNT,
3319 spa_get_errlog_size(spa)) == 0);
3321 if (spa_suspended(spa))
3322 VERIFY(nvlist_add_uint64(*config,
3323 ZPOOL_CONFIG_SUSPENDED,
3324 spa->spa_failmode) == 0);
3326 spa_add_spares(spa, *config);
3327 spa_add_l2cache(spa, *config);
3328 spa_add_feature_stats(spa, *config);
3333 * We want to get the alternate root even for faulted pools, so we cheat
3334 * and call spa_lookup() directly.
3338 mutex_enter(&spa_namespace_lock);
3339 spa = spa_lookup(name);
3341 spa_altroot(spa, altroot, buflen);
3345 mutex_exit(&spa_namespace_lock);
3347 spa_altroot(spa, altroot, buflen);
3352 spa_config_exit(spa, SCL_CONFIG, FTAG);
3353 spa_close(spa, FTAG);
3360 * Validate that the auxiliary device array is well formed. We must have an
3361 * array of nvlists, each which describes a valid leaf vdev. If this is an
3362 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3363 * specified, as long as they are well-formed.
3366 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3367 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3368 vdev_labeltype_t label)
3375 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3378 * It's acceptable to have no devs specified.
3380 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3384 return (SET_ERROR(EINVAL));
3387 * Make sure the pool is formatted with a version that supports this
3390 if (spa_version(spa) < version)
3391 return (SET_ERROR(ENOTSUP));
3394 * Set the pending device list so we correctly handle device in-use
3397 sav->sav_pending = dev;
3398 sav->sav_npending = ndev;
3400 for (i = 0; i < ndev; i++) {
3401 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3405 if (!vd->vdev_ops->vdev_op_leaf) {
3407 error = SET_ERROR(EINVAL);
3412 * The L2ARC currently only supports disk devices in
3413 * kernel context. For user-level testing, we allow it.
3416 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3417 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3418 error = SET_ERROR(ENOTBLK);
3425 if ((error = vdev_open(vd)) == 0 &&
3426 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3427 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3428 vd->vdev_guid) == 0);
3434 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3441 sav->sav_pending = NULL;
3442 sav->sav_npending = 0;
3447 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3451 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3453 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3454 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3455 VDEV_LABEL_SPARE)) != 0) {
3459 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3460 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3461 VDEV_LABEL_L2CACHE));
3465 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3470 if (sav->sav_config != NULL) {
3476 * Generate new dev list by concatentating with the
3479 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3480 &olddevs, &oldndevs) == 0);
3482 newdevs = kmem_alloc(sizeof (void *) *
3483 (ndevs + oldndevs), KM_SLEEP);
3484 for (i = 0; i < oldndevs; i++)
3485 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3487 for (i = 0; i < ndevs; i++)
3488 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3491 VERIFY(nvlist_remove(sav->sav_config, config,
3492 DATA_TYPE_NVLIST_ARRAY) == 0);
3494 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3495 config, newdevs, ndevs + oldndevs) == 0);
3496 for (i = 0; i < oldndevs + ndevs; i++)
3497 nvlist_free(newdevs[i]);
3498 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3501 * Generate a new dev list.
3503 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3505 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3511 * Stop and drop level 2 ARC devices
3514 spa_l2cache_drop(spa_t *spa)
3518 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3520 for (i = 0; i < sav->sav_count; i++) {
3523 vd = sav->sav_vdevs[i];
3526 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3527 pool != 0ULL && l2arc_vdev_present(vd))
3528 l2arc_remove_vdev(vd);
3536 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3540 char *altroot = NULL;
3545 uint64_t txg = TXG_INITIAL;
3546 nvlist_t **spares, **l2cache;
3547 uint_t nspares, nl2cache;
3548 uint64_t version, obj;
3549 boolean_t has_features;
3552 * If this pool already exists, return failure.
3554 mutex_enter(&spa_namespace_lock);
3555 if (spa_lookup(pool) != NULL) {
3556 mutex_exit(&spa_namespace_lock);
3557 return (SET_ERROR(EEXIST));
3561 * Allocate a new spa_t structure.
3563 (void) nvlist_lookup_string(props,
3564 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3565 spa = spa_add(pool, NULL, altroot);
3566 spa_activate(spa, spa_mode_global);
3568 if (props && (error = spa_prop_validate(spa, props))) {
3569 spa_deactivate(spa);
3571 mutex_exit(&spa_namespace_lock);
3575 has_features = B_FALSE;
3576 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3577 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3578 if (zpool_prop_feature(nvpair_name(elem)))
3579 has_features = B_TRUE;
3582 if (has_features || nvlist_lookup_uint64(props,
3583 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3584 version = SPA_VERSION;
3586 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3588 spa->spa_first_txg = txg;
3589 spa->spa_uberblock.ub_txg = txg - 1;
3590 spa->spa_uberblock.ub_version = version;
3591 spa->spa_ubsync = spa->spa_uberblock;
3594 * Create "The Godfather" zio to hold all async IOs
3596 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3598 for (int i = 0; i < max_ncpus; i++) {
3599 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3600 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3601 ZIO_FLAG_GODFATHER);
3605 * Create the root vdev.
3607 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3609 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3611 ASSERT(error != 0 || rvd != NULL);
3612 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3614 if (error == 0 && !zfs_allocatable_devs(nvroot))
3615 error = SET_ERROR(EINVAL);
3618 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3619 (error = spa_validate_aux(spa, nvroot, txg,
3620 VDEV_ALLOC_ADD)) == 0) {
3621 for (int c = 0; c < rvd->vdev_children; c++) {
3622 vdev_ashift_optimize(rvd->vdev_child[c]);
3623 vdev_metaslab_set_size(rvd->vdev_child[c]);
3624 vdev_expand(rvd->vdev_child[c], txg);
3628 spa_config_exit(spa, SCL_ALL, FTAG);
3632 spa_deactivate(spa);
3634 mutex_exit(&spa_namespace_lock);
3639 * Get the list of spares, if specified.
3641 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3642 &spares, &nspares) == 0) {
3643 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3645 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3646 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3647 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3648 spa_load_spares(spa);
3649 spa_config_exit(spa, SCL_ALL, FTAG);
3650 spa->spa_spares.sav_sync = B_TRUE;
3654 * Get the list of level 2 cache devices, if specified.
3656 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3657 &l2cache, &nl2cache) == 0) {
3658 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3659 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3660 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3661 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3662 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3663 spa_load_l2cache(spa);
3664 spa_config_exit(spa, SCL_ALL, FTAG);
3665 spa->spa_l2cache.sav_sync = B_TRUE;
3668 spa->spa_is_initializing = B_TRUE;
3669 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3670 spa->spa_meta_objset = dp->dp_meta_objset;
3671 spa->spa_is_initializing = B_FALSE;
3674 * Create DDTs (dedup tables).
3678 spa_update_dspace(spa);
3680 tx = dmu_tx_create_assigned(dp, txg);
3683 * Create the pool config object.
3685 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3686 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3687 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3689 if (zap_add(spa->spa_meta_objset,
3690 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3691 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3692 cmn_err(CE_PANIC, "failed to add pool config");
3695 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3696 spa_feature_create_zap_objects(spa, tx);
3698 if (zap_add(spa->spa_meta_objset,
3699 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3700 sizeof (uint64_t), 1, &version, tx) != 0) {
3701 cmn_err(CE_PANIC, "failed to add pool version");
3704 /* Newly created pools with the right version are always deflated. */
3705 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3706 spa->spa_deflate = TRUE;
3707 if (zap_add(spa->spa_meta_objset,
3708 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3709 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3710 cmn_err(CE_PANIC, "failed to add deflate");
3715 * Create the deferred-free bpobj. Turn off compression
3716 * because sync-to-convergence takes longer if the blocksize
3719 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3720 dmu_object_set_compress(spa->spa_meta_objset, obj,
3721 ZIO_COMPRESS_OFF, tx);
3722 if (zap_add(spa->spa_meta_objset,
3723 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3724 sizeof (uint64_t), 1, &obj, tx) != 0) {
3725 cmn_err(CE_PANIC, "failed to add bpobj");
3727 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3728 spa->spa_meta_objset, obj));
3731 * Create the pool's history object.
3733 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3734 spa_history_create_obj(spa, tx);
3737 * Set pool properties.
3739 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3740 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3741 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3742 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3744 if (props != NULL) {
3745 spa_configfile_set(spa, props, B_FALSE);
3746 spa_sync_props(props, tx);
3751 spa->spa_sync_on = B_TRUE;
3752 txg_sync_start(spa->spa_dsl_pool);
3755 * We explicitly wait for the first transaction to complete so that our
3756 * bean counters are appropriately updated.
3758 txg_wait_synced(spa->spa_dsl_pool, txg);
3760 spa_config_sync(spa, B_FALSE, B_TRUE);
3762 spa_history_log_version(spa, "create");
3764 spa->spa_minref = refcount_count(&spa->spa_refcount);
3766 mutex_exit(&spa_namespace_lock);
3774 * Get the root pool information from the root disk, then import the root pool
3775 * during the system boot up time.
3777 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3780 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3783 nvlist_t *nvtop, *nvroot;
3786 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3790 * Add this top-level vdev to the child array.
3792 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3794 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3796 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3799 * Put this pool's top-level vdevs into a root vdev.
3801 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3802 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3803 VDEV_TYPE_ROOT) == 0);
3804 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3805 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3806 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3810 * Replace the existing vdev_tree with the new root vdev in
3811 * this pool's configuration (remove the old, add the new).
3813 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3814 nvlist_free(nvroot);
3819 * Walk the vdev tree and see if we can find a device with "better"
3820 * configuration. A configuration is "better" if the label on that
3821 * device has a more recent txg.
3824 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3826 for (int c = 0; c < vd->vdev_children; c++)
3827 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3829 if (vd->vdev_ops->vdev_op_leaf) {
3833 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3837 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3841 * Do we have a better boot device?
3843 if (label_txg > *txg) {
3852 * Import a root pool.
3854 * For x86. devpath_list will consist of devid and/or physpath name of
3855 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3856 * The GRUB "findroot" command will return the vdev we should boot.
3858 * For Sparc, devpath_list consists the physpath name of the booting device
3859 * no matter the rootpool is a single device pool or a mirrored pool.
3861 * "/pci@1f,0/ide@d/disk@0,0:a"
3864 spa_import_rootpool(char *devpath, char *devid)
3867 vdev_t *rvd, *bvd, *avd = NULL;
3868 nvlist_t *config, *nvtop;
3874 * Read the label from the boot device and generate a configuration.
3876 config = spa_generate_rootconf(devpath, devid, &guid);
3877 #if defined(_OBP) && defined(_KERNEL)
3878 if (config == NULL) {
3879 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3881 get_iscsi_bootpath_phy(devpath);
3882 config = spa_generate_rootconf(devpath, devid, &guid);
3886 if (config == NULL) {
3887 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3889 return (SET_ERROR(EIO));
3892 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3894 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3896 mutex_enter(&spa_namespace_lock);
3897 if ((spa = spa_lookup(pname)) != NULL) {
3899 * Remove the existing root pool from the namespace so that we
3900 * can replace it with the correct config we just read in.
3905 spa = spa_add(pname, config, NULL);
3906 spa->spa_is_root = B_TRUE;
3907 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3910 * Build up a vdev tree based on the boot device's label config.
3912 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3914 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3915 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3916 VDEV_ALLOC_ROOTPOOL);
3917 spa_config_exit(spa, SCL_ALL, FTAG);
3919 mutex_exit(&spa_namespace_lock);
3920 nvlist_free(config);
3921 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3927 * Get the boot vdev.
3929 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3930 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3931 (u_longlong_t)guid);
3932 error = SET_ERROR(ENOENT);
3937 * Determine if there is a better boot device.
3940 spa_alt_rootvdev(rvd, &avd, &txg);
3942 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3943 "try booting from '%s'", avd->vdev_path);
3944 error = SET_ERROR(EINVAL);
3949 * If the boot device is part of a spare vdev then ensure that
3950 * we're booting off the active spare.
3952 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3953 !bvd->vdev_isspare) {
3954 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3955 "try booting from '%s'",
3957 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3958 error = SET_ERROR(EINVAL);
3964 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3966 spa_config_exit(spa, SCL_ALL, FTAG);
3967 mutex_exit(&spa_namespace_lock);
3969 nvlist_free(config);
3975 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3979 spa_generate_rootconf(const char *name)
3981 nvlist_t **configs, **tops;
3983 nvlist_t *best_cfg, *nvtop, *nvroot;
3992 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3995 ASSERT3U(count, !=, 0);
3997 for (i = 0; i < count; i++) {
4000 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
4002 if (txg > best_txg) {
4004 best_cfg = configs[i];
4009 * Multi-vdev root pool configuration discovery is not supported yet.
4012 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
4014 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
4017 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
4018 for (i = 0; i < nchildren; i++) {
4021 if (configs[i] == NULL)
4023 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
4025 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
4027 for (i = 0; holes != NULL && i < nholes; i++) {
4030 if (tops[holes[i]] != NULL)
4032 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
4033 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
4034 VDEV_TYPE_HOLE) == 0);
4035 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
4037 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4040 for (i = 0; i < nchildren; i++) {
4041 if (tops[i] != NULL)
4043 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4044 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4045 VDEV_TYPE_MISSING) == 0);
4046 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4048 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4053 * Create pool config based on the best vdev config.
4055 nvlist_dup(best_cfg, &config, KM_SLEEP);
4058 * Put this pool's top-level vdevs into a root vdev.
4060 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4062 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4063 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4064 VDEV_TYPE_ROOT) == 0);
4065 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4066 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4067 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4068 tops, nchildren) == 0);
4071 * Replace the existing vdev_tree with the new root vdev in
4072 * this pool's configuration (remove the old, add the new).
4074 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4077 * Drop vdev config elements that should not be present at pool level.
4079 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4080 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4082 for (i = 0; i < count; i++)
4083 nvlist_free(configs[i]);
4084 kmem_free(configs, count * sizeof(void *));
4085 for (i = 0; i < nchildren; i++)
4086 nvlist_free(tops[i]);
4087 kmem_free(tops, nchildren * sizeof(void *));
4088 nvlist_free(nvroot);
4093 spa_import_rootpool(const char *name)
4096 vdev_t *rvd, *bvd, *avd = NULL;
4097 nvlist_t *config, *nvtop;
4103 * Read the label from the boot device and generate a configuration.
4105 config = spa_generate_rootconf(name);
4107 mutex_enter(&spa_namespace_lock);
4108 if (config != NULL) {
4109 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4110 &pname) == 0 && strcmp(name, pname) == 0);
4111 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4114 if ((spa = spa_lookup(pname)) != NULL) {
4116 * Remove the existing root pool from the namespace so
4117 * that we can replace it with the correct config
4122 spa = spa_add(pname, config, NULL);
4125 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4126 * via spa_version().
4128 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4129 &spa->spa_ubsync.ub_version) != 0)
4130 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4131 } else if ((spa = spa_lookup(name)) == NULL) {
4132 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4136 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4138 spa->spa_is_root = B_TRUE;
4139 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4142 * Build up a vdev tree based on the boot device's label config.
4144 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4146 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4147 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4148 VDEV_ALLOC_ROOTPOOL);
4149 spa_config_exit(spa, SCL_ALL, FTAG);
4151 mutex_exit(&spa_namespace_lock);
4152 nvlist_free(config);
4153 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4158 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4160 spa_config_exit(spa, SCL_ALL, FTAG);
4161 mutex_exit(&spa_namespace_lock);
4163 nvlist_free(config);
4171 * Import a non-root pool into the system.
4174 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4177 char *altroot = NULL;
4178 spa_load_state_t state = SPA_LOAD_IMPORT;
4179 zpool_rewind_policy_t policy;
4180 uint64_t mode = spa_mode_global;
4181 uint64_t readonly = B_FALSE;
4184 nvlist_t **spares, **l2cache;
4185 uint_t nspares, nl2cache;
4188 * If a pool with this name exists, return failure.
4190 mutex_enter(&spa_namespace_lock);
4191 if (spa_lookup(pool) != NULL) {
4192 mutex_exit(&spa_namespace_lock);
4193 return (SET_ERROR(EEXIST));
4197 * Create and initialize the spa structure.
4199 (void) nvlist_lookup_string(props,
4200 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4201 (void) nvlist_lookup_uint64(props,
4202 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4205 spa = spa_add(pool, config, altroot);
4206 spa->spa_import_flags = flags;
4209 * Verbatim import - Take a pool and insert it into the namespace
4210 * as if it had been loaded at boot.
4212 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4214 spa_configfile_set(spa, props, B_FALSE);
4216 spa_config_sync(spa, B_FALSE, B_TRUE);
4218 mutex_exit(&spa_namespace_lock);
4222 spa_activate(spa, mode);
4225 * Don't start async tasks until we know everything is healthy.
4227 spa_async_suspend(spa);
4229 zpool_get_rewind_policy(config, &policy);
4230 if (policy.zrp_request & ZPOOL_DO_REWIND)
4231 state = SPA_LOAD_RECOVER;
4234 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4235 * because the user-supplied config is actually the one to trust when
4238 if (state != SPA_LOAD_RECOVER)
4239 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4241 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4242 policy.zrp_request);
4245 * Propagate anything learned while loading the pool and pass it
4246 * back to caller (i.e. rewind info, missing devices, etc).
4248 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4249 spa->spa_load_info) == 0);
4251 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4253 * Toss any existing sparelist, as it doesn't have any validity
4254 * anymore, and conflicts with spa_has_spare().
4256 if (spa->spa_spares.sav_config) {
4257 nvlist_free(spa->spa_spares.sav_config);
4258 spa->spa_spares.sav_config = NULL;
4259 spa_load_spares(spa);
4261 if (spa->spa_l2cache.sav_config) {
4262 nvlist_free(spa->spa_l2cache.sav_config);
4263 spa->spa_l2cache.sav_config = NULL;
4264 spa_load_l2cache(spa);
4267 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4270 error = spa_validate_aux(spa, nvroot, -1ULL,
4273 error = spa_validate_aux(spa, nvroot, -1ULL,
4274 VDEV_ALLOC_L2CACHE);
4275 spa_config_exit(spa, SCL_ALL, FTAG);
4278 spa_configfile_set(spa, props, B_FALSE);
4280 if (error != 0 || (props && spa_writeable(spa) &&
4281 (error = spa_prop_set(spa, props)))) {
4283 spa_deactivate(spa);
4285 mutex_exit(&spa_namespace_lock);
4289 spa_async_resume(spa);
4292 * Override any spares and level 2 cache devices as specified by
4293 * the user, as these may have correct device names/devids, etc.
4295 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4296 &spares, &nspares) == 0) {
4297 if (spa->spa_spares.sav_config)
4298 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4299 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4301 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4302 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4303 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4304 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4305 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4306 spa_load_spares(spa);
4307 spa_config_exit(spa, SCL_ALL, FTAG);
4308 spa->spa_spares.sav_sync = B_TRUE;
4310 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4311 &l2cache, &nl2cache) == 0) {
4312 if (spa->spa_l2cache.sav_config)
4313 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4314 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4316 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4317 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4318 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4319 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4320 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4321 spa_load_l2cache(spa);
4322 spa_config_exit(spa, SCL_ALL, FTAG);
4323 spa->spa_l2cache.sav_sync = B_TRUE;
4327 * Check for any removed devices.
4329 if (spa->spa_autoreplace) {
4330 spa_aux_check_removed(&spa->spa_spares);
4331 spa_aux_check_removed(&spa->spa_l2cache);
4334 if (spa_writeable(spa)) {
4336 * Update the config cache to include the newly-imported pool.
4338 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4342 * It's possible that the pool was expanded while it was exported.
4343 * We kick off an async task to handle this for us.
4345 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4347 mutex_exit(&spa_namespace_lock);
4348 spa_history_log_version(spa, "import");
4352 zvol_create_minors(pool);
4359 spa_tryimport(nvlist_t *tryconfig)
4361 nvlist_t *config = NULL;
4367 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4370 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4374 * Create and initialize the spa structure.
4376 mutex_enter(&spa_namespace_lock);
4377 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4378 spa_activate(spa, FREAD);
4381 * Pass off the heavy lifting to spa_load().
4382 * Pass TRUE for mosconfig because the user-supplied config
4383 * is actually the one to trust when doing an import.
4385 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4388 * If 'tryconfig' was at least parsable, return the current config.
4390 if (spa->spa_root_vdev != NULL) {
4391 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4392 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4394 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4396 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4397 spa->spa_uberblock.ub_timestamp) == 0);
4398 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4399 spa->spa_load_info) == 0);
4402 * If the bootfs property exists on this pool then we
4403 * copy it out so that external consumers can tell which
4404 * pools are bootable.
4406 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4407 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4410 * We have to play games with the name since the
4411 * pool was opened as TRYIMPORT_NAME.
4413 if (dsl_dsobj_to_dsname(spa_name(spa),
4414 spa->spa_bootfs, tmpname) == 0) {
4416 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4418 cp = strchr(tmpname, '/');
4420 (void) strlcpy(dsname, tmpname,
4423 (void) snprintf(dsname, MAXPATHLEN,
4424 "%s/%s", poolname, ++cp);
4426 VERIFY(nvlist_add_string(config,
4427 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4428 kmem_free(dsname, MAXPATHLEN);
4430 kmem_free(tmpname, MAXPATHLEN);
4434 * Add the list of hot spares and level 2 cache devices.
4436 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4437 spa_add_spares(spa, config);
4438 spa_add_l2cache(spa, config);
4439 spa_config_exit(spa, SCL_CONFIG, FTAG);
4443 spa_deactivate(spa);
4445 mutex_exit(&spa_namespace_lock);
4451 * Pool export/destroy
4453 * The act of destroying or exporting a pool is very simple. We make sure there
4454 * is no more pending I/O and any references to the pool are gone. Then, we
4455 * update the pool state and sync all the labels to disk, removing the
4456 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4457 * we don't sync the labels or remove the configuration cache.
4460 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4461 boolean_t force, boolean_t hardforce)
4468 if (!(spa_mode_global & FWRITE))
4469 return (SET_ERROR(EROFS));
4471 mutex_enter(&spa_namespace_lock);
4472 if ((spa = spa_lookup(pool)) == NULL) {
4473 mutex_exit(&spa_namespace_lock);
4474 return (SET_ERROR(ENOENT));
4478 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4479 * reacquire the namespace lock, and see if we can export.
4481 spa_open_ref(spa, FTAG);
4482 mutex_exit(&spa_namespace_lock);
4483 spa_async_suspend(spa);
4484 mutex_enter(&spa_namespace_lock);
4485 spa_close(spa, FTAG);
4488 * The pool will be in core if it's openable,
4489 * in which case we can modify its state.
4491 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4493 * Objsets may be open only because they're dirty, so we
4494 * have to force it to sync before checking spa_refcnt.
4496 txg_wait_synced(spa->spa_dsl_pool, 0);
4499 * A pool cannot be exported or destroyed if there are active
4500 * references. If we are resetting a pool, allow references by
4501 * fault injection handlers.
4503 if (!spa_refcount_zero(spa) ||
4504 (spa->spa_inject_ref != 0 &&
4505 new_state != POOL_STATE_UNINITIALIZED)) {
4506 spa_async_resume(spa);
4507 mutex_exit(&spa_namespace_lock);
4508 return (SET_ERROR(EBUSY));
4512 * A pool cannot be exported if it has an active shared spare.
4513 * This is to prevent other pools stealing the active spare
4514 * from an exported pool. At user's own will, such pool can
4515 * be forcedly exported.
4517 if (!force && new_state == POOL_STATE_EXPORTED &&
4518 spa_has_active_shared_spare(spa)) {
4519 spa_async_resume(spa);
4520 mutex_exit(&spa_namespace_lock);
4521 return (SET_ERROR(EXDEV));
4525 * We want this to be reflected on every label,
4526 * so mark them all dirty. spa_unload() will do the
4527 * final sync that pushes these changes out.
4529 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4530 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4531 spa->spa_state = new_state;
4532 spa->spa_final_txg = spa_last_synced_txg(spa) +
4534 vdev_config_dirty(spa->spa_root_vdev);
4535 spa_config_exit(spa, SCL_ALL, FTAG);
4539 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4541 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4543 spa_deactivate(spa);
4546 if (oldconfig && spa->spa_config)
4547 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4549 if (new_state != POOL_STATE_UNINITIALIZED) {
4551 spa_config_sync(spa, B_TRUE, B_TRUE);
4554 mutex_exit(&spa_namespace_lock);
4560 * Destroy a storage pool.
4563 spa_destroy(char *pool)
4565 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4570 * Export a storage pool.
4573 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4574 boolean_t hardforce)
4576 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4581 * Similar to spa_export(), this unloads the spa_t without actually removing it
4582 * from the namespace in any way.
4585 spa_reset(char *pool)
4587 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4592 * ==========================================================================
4593 * Device manipulation
4594 * ==========================================================================
4598 * Add a device to a storage pool.
4601 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4605 vdev_t *rvd = spa->spa_root_vdev;
4607 nvlist_t **spares, **l2cache;
4608 uint_t nspares, nl2cache;
4610 ASSERT(spa_writeable(spa));
4612 txg = spa_vdev_enter(spa);
4614 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4615 VDEV_ALLOC_ADD)) != 0)
4616 return (spa_vdev_exit(spa, NULL, txg, error));
4618 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4620 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4624 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4628 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4629 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4631 if (vd->vdev_children != 0 &&
4632 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4633 return (spa_vdev_exit(spa, vd, txg, error));
4636 * We must validate the spares and l2cache devices after checking the
4637 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4639 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4640 return (spa_vdev_exit(spa, vd, txg, error));
4643 * Transfer each new top-level vdev from vd to rvd.
4645 for (int c = 0; c < vd->vdev_children; c++) {
4648 * Set the vdev id to the first hole, if one exists.
4650 for (id = 0; id < rvd->vdev_children; id++) {
4651 if (rvd->vdev_child[id]->vdev_ishole) {
4652 vdev_free(rvd->vdev_child[id]);
4656 tvd = vd->vdev_child[c];
4657 vdev_remove_child(vd, tvd);
4659 vdev_add_child(rvd, tvd);
4660 vdev_config_dirty(tvd);
4664 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4665 ZPOOL_CONFIG_SPARES);
4666 spa_load_spares(spa);
4667 spa->spa_spares.sav_sync = B_TRUE;
4670 if (nl2cache != 0) {
4671 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4672 ZPOOL_CONFIG_L2CACHE);
4673 spa_load_l2cache(spa);
4674 spa->spa_l2cache.sav_sync = B_TRUE;
4678 * We have to be careful when adding new vdevs to an existing pool.
4679 * If other threads start allocating from these vdevs before we
4680 * sync the config cache, and we lose power, then upon reboot we may
4681 * fail to open the pool because there are DVAs that the config cache
4682 * can't translate. Therefore, we first add the vdevs without
4683 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4684 * and then let spa_config_update() initialize the new metaslabs.
4686 * spa_load() checks for added-but-not-initialized vdevs, so that
4687 * if we lose power at any point in this sequence, the remaining
4688 * steps will be completed the next time we load the pool.
4690 (void) spa_vdev_exit(spa, vd, txg, 0);
4692 mutex_enter(&spa_namespace_lock);
4693 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4694 mutex_exit(&spa_namespace_lock);
4700 * Attach a device to a mirror. The arguments are the path to any device
4701 * in the mirror, and the nvroot for the new device. If the path specifies
4702 * a device that is not mirrored, we automatically insert the mirror vdev.
4704 * If 'replacing' is specified, the new device is intended to replace the
4705 * existing device; in this case the two devices are made into their own
4706 * mirror using the 'replacing' vdev, which is functionally identical to
4707 * the mirror vdev (it actually reuses all the same ops) but has a few
4708 * extra rules: you can't attach to it after it's been created, and upon
4709 * completion of resilvering, the first disk (the one being replaced)
4710 * is automatically detached.
4713 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4715 uint64_t txg, dtl_max_txg;
4716 vdev_t *rvd = spa->spa_root_vdev;
4717 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4719 char *oldvdpath, *newvdpath;
4723 ASSERT(spa_writeable(spa));
4725 txg = spa_vdev_enter(spa);
4727 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4730 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4732 if (!oldvd->vdev_ops->vdev_op_leaf)
4733 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4735 pvd = oldvd->vdev_parent;
4737 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4738 VDEV_ALLOC_ATTACH)) != 0)
4739 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4741 if (newrootvd->vdev_children != 1)
4742 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4744 newvd = newrootvd->vdev_child[0];
4746 if (!newvd->vdev_ops->vdev_op_leaf)
4747 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4749 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4750 return (spa_vdev_exit(spa, newrootvd, txg, error));
4753 * Spares can't replace logs
4755 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4756 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4760 * For attach, the only allowable parent is a mirror or the root
4763 if (pvd->vdev_ops != &vdev_mirror_ops &&
4764 pvd->vdev_ops != &vdev_root_ops)
4765 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4767 pvops = &vdev_mirror_ops;
4770 * Active hot spares can only be replaced by inactive hot
4773 if (pvd->vdev_ops == &vdev_spare_ops &&
4774 oldvd->vdev_isspare &&
4775 !spa_has_spare(spa, newvd->vdev_guid))
4776 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4779 * If the source is a hot spare, and the parent isn't already a
4780 * spare, then we want to create a new hot spare. Otherwise, we
4781 * want to create a replacing vdev. The user is not allowed to
4782 * attach to a spared vdev child unless the 'isspare' state is
4783 * the same (spare replaces spare, non-spare replaces
4786 if (pvd->vdev_ops == &vdev_replacing_ops &&
4787 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4788 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4789 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4790 newvd->vdev_isspare != oldvd->vdev_isspare) {
4791 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4794 if (newvd->vdev_isspare)
4795 pvops = &vdev_spare_ops;
4797 pvops = &vdev_replacing_ops;
4801 * Make sure the new device is big enough.
4803 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4804 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4807 * The new device cannot have a higher alignment requirement
4808 * than the top-level vdev.
4810 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4811 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4814 * If this is an in-place replacement, update oldvd's path and devid
4815 * to make it distinguishable from newvd, and unopenable from now on.
4817 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4818 spa_strfree(oldvd->vdev_path);
4819 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4821 (void) sprintf(oldvd->vdev_path, "%s/%s",
4822 newvd->vdev_path, "old");
4823 if (oldvd->vdev_devid != NULL) {
4824 spa_strfree(oldvd->vdev_devid);
4825 oldvd->vdev_devid = NULL;
4829 /* mark the device being resilvered */
4830 newvd->vdev_resilver_txg = txg;
4833 * If the parent is not a mirror, or if we're replacing, insert the new
4834 * mirror/replacing/spare vdev above oldvd.
4836 if (pvd->vdev_ops != pvops)
4837 pvd = vdev_add_parent(oldvd, pvops);
4839 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4840 ASSERT(pvd->vdev_ops == pvops);
4841 ASSERT(oldvd->vdev_parent == pvd);
4844 * Extract the new device from its root and add it to pvd.
4846 vdev_remove_child(newrootvd, newvd);
4847 newvd->vdev_id = pvd->vdev_children;
4848 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4849 vdev_add_child(pvd, newvd);
4851 tvd = newvd->vdev_top;
4852 ASSERT(pvd->vdev_top == tvd);
4853 ASSERT(tvd->vdev_parent == rvd);
4855 vdev_config_dirty(tvd);
4858 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4859 * for any dmu_sync-ed blocks. It will propagate upward when
4860 * spa_vdev_exit() calls vdev_dtl_reassess().
4862 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4864 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4865 dtl_max_txg - TXG_INITIAL);
4867 if (newvd->vdev_isspare) {
4868 spa_spare_activate(newvd);
4869 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4872 oldvdpath = spa_strdup(oldvd->vdev_path);
4873 newvdpath = spa_strdup(newvd->vdev_path);
4874 newvd_isspare = newvd->vdev_isspare;
4877 * Mark newvd's DTL dirty in this txg.
4879 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4882 * Schedule the resilver to restart in the future. We do this to
4883 * ensure that dmu_sync-ed blocks have been stitched into the
4884 * respective datasets.
4886 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4891 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4893 spa_history_log_internal(spa, "vdev attach", NULL,
4894 "%s vdev=%s %s vdev=%s",
4895 replacing && newvd_isspare ? "spare in" :
4896 replacing ? "replace" : "attach", newvdpath,
4897 replacing ? "for" : "to", oldvdpath);
4899 spa_strfree(oldvdpath);
4900 spa_strfree(newvdpath);
4902 if (spa->spa_bootfs)
4903 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4909 * Detach a device from a mirror or replacing vdev.
4911 * If 'replace_done' is specified, only detach if the parent
4912 * is a replacing vdev.
4915 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4919 vdev_t *rvd = spa->spa_root_vdev;
4920 vdev_t *vd, *pvd, *cvd, *tvd;
4921 boolean_t unspare = B_FALSE;
4922 uint64_t unspare_guid = 0;
4925 ASSERT(spa_writeable(spa));
4927 txg = spa_vdev_enter(spa);
4929 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4932 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4934 if (!vd->vdev_ops->vdev_op_leaf)
4935 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4937 pvd = vd->vdev_parent;
4940 * If the parent/child relationship is not as expected, don't do it.
4941 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4942 * vdev that's replacing B with C. The user's intent in replacing
4943 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4944 * the replace by detaching C, the expected behavior is to end up
4945 * M(A,B). But suppose that right after deciding to detach C,
4946 * the replacement of B completes. We would have M(A,C), and then
4947 * ask to detach C, which would leave us with just A -- not what
4948 * the user wanted. To prevent this, we make sure that the
4949 * parent/child relationship hasn't changed -- in this example,
4950 * that C's parent is still the replacing vdev R.
4952 if (pvd->vdev_guid != pguid && pguid != 0)
4953 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4956 * Only 'replacing' or 'spare' vdevs can be replaced.
4958 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4959 pvd->vdev_ops != &vdev_spare_ops)
4960 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4962 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4963 spa_version(spa) >= SPA_VERSION_SPARES);
4966 * Only mirror, replacing, and spare vdevs support detach.
4968 if (pvd->vdev_ops != &vdev_replacing_ops &&
4969 pvd->vdev_ops != &vdev_mirror_ops &&
4970 pvd->vdev_ops != &vdev_spare_ops)
4971 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4974 * If this device has the only valid copy of some data,
4975 * we cannot safely detach it.
4977 if (vdev_dtl_required(vd))
4978 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4980 ASSERT(pvd->vdev_children >= 2);
4983 * If we are detaching the second disk from a replacing vdev, then
4984 * check to see if we changed the original vdev's path to have "/old"
4985 * at the end in spa_vdev_attach(). If so, undo that change now.
4987 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4988 vd->vdev_path != NULL) {
4989 size_t len = strlen(vd->vdev_path);
4991 for (int c = 0; c < pvd->vdev_children; c++) {
4992 cvd = pvd->vdev_child[c];
4994 if (cvd == vd || cvd->vdev_path == NULL)
4997 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4998 strcmp(cvd->vdev_path + len, "/old") == 0) {
4999 spa_strfree(cvd->vdev_path);
5000 cvd->vdev_path = spa_strdup(vd->vdev_path);
5007 * If we are detaching the original disk from a spare, then it implies
5008 * that the spare should become a real disk, and be removed from the
5009 * active spare list for the pool.
5011 if (pvd->vdev_ops == &vdev_spare_ops &&
5013 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
5017 * Erase the disk labels so the disk can be used for other things.
5018 * This must be done after all other error cases are handled,
5019 * but before we disembowel vd (so we can still do I/O to it).
5020 * But if we can't do it, don't treat the error as fatal --
5021 * it may be that the unwritability of the disk is the reason
5022 * it's being detached!
5024 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5027 * Remove vd from its parent and compact the parent's children.
5029 vdev_remove_child(pvd, vd);
5030 vdev_compact_children(pvd);
5033 * Remember one of the remaining children so we can get tvd below.
5035 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5038 * If we need to remove the remaining child from the list of hot spares,
5039 * do it now, marking the vdev as no longer a spare in the process.
5040 * We must do this before vdev_remove_parent(), because that can
5041 * change the GUID if it creates a new toplevel GUID. For a similar
5042 * reason, we must remove the spare now, in the same txg as the detach;
5043 * otherwise someone could attach a new sibling, change the GUID, and
5044 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5047 ASSERT(cvd->vdev_isspare);
5048 spa_spare_remove(cvd);
5049 unspare_guid = cvd->vdev_guid;
5050 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5051 cvd->vdev_unspare = B_TRUE;
5055 * If the parent mirror/replacing vdev only has one child,
5056 * the parent is no longer needed. Remove it from the tree.
5058 if (pvd->vdev_children == 1) {
5059 if (pvd->vdev_ops == &vdev_spare_ops)
5060 cvd->vdev_unspare = B_FALSE;
5061 vdev_remove_parent(cvd);
5066 * We don't set tvd until now because the parent we just removed
5067 * may have been the previous top-level vdev.
5069 tvd = cvd->vdev_top;
5070 ASSERT(tvd->vdev_parent == rvd);
5073 * Reevaluate the parent vdev state.
5075 vdev_propagate_state(cvd);
5078 * If the 'autoexpand' property is set on the pool then automatically
5079 * try to expand the size of the pool. For example if the device we
5080 * just detached was smaller than the others, it may be possible to
5081 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5082 * first so that we can obtain the updated sizes of the leaf vdevs.
5084 if (spa->spa_autoexpand) {
5086 vdev_expand(tvd, txg);
5089 vdev_config_dirty(tvd);
5092 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5093 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5094 * But first make sure we're not on any *other* txg's DTL list, to
5095 * prevent vd from being accessed after it's freed.
5097 vdpath = spa_strdup(vd->vdev_path);
5098 for (int t = 0; t < TXG_SIZE; t++)
5099 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5100 vd->vdev_detached = B_TRUE;
5101 vdev_dirty(tvd, VDD_DTL, vd, txg);
5103 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5105 /* hang on to the spa before we release the lock */
5106 spa_open_ref(spa, FTAG);
5108 error = spa_vdev_exit(spa, vd, txg, 0);
5110 spa_history_log_internal(spa, "detach", NULL,
5112 spa_strfree(vdpath);
5115 * If this was the removal of the original device in a hot spare vdev,
5116 * then we want to go through and remove the device from the hot spare
5117 * list of every other pool.
5120 spa_t *altspa = NULL;
5122 mutex_enter(&spa_namespace_lock);
5123 while ((altspa = spa_next(altspa)) != NULL) {
5124 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5128 spa_open_ref(altspa, FTAG);
5129 mutex_exit(&spa_namespace_lock);
5130 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5131 mutex_enter(&spa_namespace_lock);
5132 spa_close(altspa, FTAG);
5134 mutex_exit(&spa_namespace_lock);
5136 /* search the rest of the vdevs for spares to remove */
5137 spa_vdev_resilver_done(spa);
5140 /* all done with the spa; OK to release */
5141 mutex_enter(&spa_namespace_lock);
5142 spa_close(spa, FTAG);
5143 mutex_exit(&spa_namespace_lock);
5149 * Split a set of devices from their mirrors, and create a new pool from them.
5152 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5153 nvlist_t *props, boolean_t exp)
5156 uint64_t txg, *glist;
5158 uint_t c, children, lastlog;
5159 nvlist_t **child, *nvl, *tmp;
5161 char *altroot = NULL;
5162 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5163 boolean_t activate_slog;
5165 ASSERT(spa_writeable(spa));
5167 txg = spa_vdev_enter(spa);
5169 /* clear the log and flush everything up to now */
5170 activate_slog = spa_passivate_log(spa);
5171 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5172 error = spa_offline_log(spa);
5173 txg = spa_vdev_config_enter(spa);
5176 spa_activate_log(spa);
5179 return (spa_vdev_exit(spa, NULL, txg, error));
5181 /* check new spa name before going any further */
5182 if (spa_lookup(newname) != NULL)
5183 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5186 * scan through all the children to ensure they're all mirrors
5188 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5189 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5191 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5193 /* first, check to ensure we've got the right child count */
5194 rvd = spa->spa_root_vdev;
5196 for (c = 0; c < rvd->vdev_children; c++) {
5197 vdev_t *vd = rvd->vdev_child[c];
5199 /* don't count the holes & logs as children */
5200 if (vd->vdev_islog || vd->vdev_ishole) {
5208 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5209 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5211 /* next, ensure no spare or cache devices are part of the split */
5212 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5213 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5214 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5216 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5217 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5219 /* then, loop over each vdev and validate it */
5220 for (c = 0; c < children; c++) {
5221 uint64_t is_hole = 0;
5223 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5227 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5228 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5231 error = SET_ERROR(EINVAL);
5236 /* which disk is going to be split? */
5237 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5239 error = SET_ERROR(EINVAL);
5243 /* look it up in the spa */
5244 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5245 if (vml[c] == NULL) {
5246 error = SET_ERROR(ENODEV);
5250 /* make sure there's nothing stopping the split */
5251 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5252 vml[c]->vdev_islog ||
5253 vml[c]->vdev_ishole ||
5254 vml[c]->vdev_isspare ||
5255 vml[c]->vdev_isl2cache ||
5256 !vdev_writeable(vml[c]) ||
5257 vml[c]->vdev_children != 0 ||
5258 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5259 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5260 error = SET_ERROR(EINVAL);
5264 if (vdev_dtl_required(vml[c])) {
5265 error = SET_ERROR(EBUSY);
5269 /* we need certain info from the top level */
5270 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5271 vml[c]->vdev_top->vdev_ms_array) == 0);
5272 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5273 vml[c]->vdev_top->vdev_ms_shift) == 0);
5274 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5275 vml[c]->vdev_top->vdev_asize) == 0);
5276 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5277 vml[c]->vdev_top->vdev_ashift) == 0);
5281 kmem_free(vml, children * sizeof (vdev_t *));
5282 kmem_free(glist, children * sizeof (uint64_t));
5283 return (spa_vdev_exit(spa, NULL, txg, error));
5286 /* stop writers from using the disks */
5287 for (c = 0; c < children; c++) {
5289 vml[c]->vdev_offline = B_TRUE;
5291 vdev_reopen(spa->spa_root_vdev);
5294 * Temporarily record the splitting vdevs in the spa config. This
5295 * will disappear once the config is regenerated.
5297 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5298 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5299 glist, children) == 0);
5300 kmem_free(glist, children * sizeof (uint64_t));
5302 mutex_enter(&spa->spa_props_lock);
5303 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5305 mutex_exit(&spa->spa_props_lock);
5306 spa->spa_config_splitting = nvl;
5307 vdev_config_dirty(spa->spa_root_vdev);
5309 /* configure and create the new pool */
5310 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5311 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5312 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5313 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5314 spa_version(spa)) == 0);
5315 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5316 spa->spa_config_txg) == 0);
5317 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5318 spa_generate_guid(NULL)) == 0);
5319 (void) nvlist_lookup_string(props,
5320 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5322 /* add the new pool to the namespace */
5323 newspa = spa_add(newname, config, altroot);
5324 newspa->spa_config_txg = spa->spa_config_txg;
5325 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5327 /* release the spa config lock, retaining the namespace lock */
5328 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5330 if (zio_injection_enabled)
5331 zio_handle_panic_injection(spa, FTAG, 1);
5333 spa_activate(newspa, spa_mode_global);
5334 spa_async_suspend(newspa);
5337 /* mark that we are creating new spa by splitting */
5338 newspa->spa_splitting_newspa = B_TRUE;
5340 /* create the new pool from the disks of the original pool */
5341 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5343 newspa->spa_splitting_newspa = B_FALSE;
5348 /* if that worked, generate a real config for the new pool */
5349 if (newspa->spa_root_vdev != NULL) {
5350 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5351 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5352 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5353 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5354 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5359 if (props != NULL) {
5360 spa_configfile_set(newspa, props, B_FALSE);
5361 error = spa_prop_set(newspa, props);
5366 /* flush everything */
5367 txg = spa_vdev_config_enter(newspa);
5368 vdev_config_dirty(newspa->spa_root_vdev);
5369 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5371 if (zio_injection_enabled)
5372 zio_handle_panic_injection(spa, FTAG, 2);
5374 spa_async_resume(newspa);
5376 /* finally, update the original pool's config */
5377 txg = spa_vdev_config_enter(spa);
5378 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5379 error = dmu_tx_assign(tx, TXG_WAIT);
5382 for (c = 0; c < children; c++) {
5383 if (vml[c] != NULL) {
5386 spa_history_log_internal(spa, "detach", tx,
5387 "vdev=%s", vml[c]->vdev_path);
5391 vdev_config_dirty(spa->spa_root_vdev);
5392 spa->spa_config_splitting = NULL;
5396 (void) spa_vdev_exit(spa, NULL, txg, 0);
5398 if (zio_injection_enabled)
5399 zio_handle_panic_injection(spa, FTAG, 3);
5401 /* split is complete; log a history record */
5402 spa_history_log_internal(newspa, "split", NULL,
5403 "from pool %s", spa_name(spa));
5405 kmem_free(vml, children * sizeof (vdev_t *));
5407 /* if we're not going to mount the filesystems in userland, export */
5409 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5416 spa_deactivate(newspa);
5419 txg = spa_vdev_config_enter(spa);
5421 /* re-online all offlined disks */
5422 for (c = 0; c < children; c++) {
5424 vml[c]->vdev_offline = B_FALSE;
5426 vdev_reopen(spa->spa_root_vdev);
5428 nvlist_free(spa->spa_config_splitting);
5429 spa->spa_config_splitting = NULL;
5430 (void) spa_vdev_exit(spa, NULL, txg, error);
5432 kmem_free(vml, children * sizeof (vdev_t *));
5437 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5439 for (int i = 0; i < count; i++) {
5442 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5445 if (guid == target_guid)
5453 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5454 nvlist_t *dev_to_remove)
5456 nvlist_t **newdev = NULL;
5459 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5461 for (int i = 0, j = 0; i < count; i++) {
5462 if (dev[i] == dev_to_remove)
5464 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5467 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5468 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5470 for (int i = 0; i < count - 1; i++)
5471 nvlist_free(newdev[i]);
5474 kmem_free(newdev, (count - 1) * sizeof (void *));
5478 * Evacuate the device.
5481 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5486 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5487 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5488 ASSERT(vd == vd->vdev_top);
5491 * Evacuate the device. We don't hold the config lock as writer
5492 * since we need to do I/O but we do keep the
5493 * spa_namespace_lock held. Once this completes the device
5494 * should no longer have any blocks allocated on it.
5496 if (vd->vdev_islog) {
5497 if (vd->vdev_stat.vs_alloc != 0)
5498 error = spa_offline_log(spa);
5500 error = SET_ERROR(ENOTSUP);
5507 * The evacuation succeeded. Remove any remaining MOS metadata
5508 * associated with this vdev, and wait for these changes to sync.
5510 ASSERT0(vd->vdev_stat.vs_alloc);
5511 txg = spa_vdev_config_enter(spa);
5512 vd->vdev_removing = B_TRUE;
5513 vdev_dirty_leaves(vd, VDD_DTL, txg);
5514 vdev_config_dirty(vd);
5515 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5521 * Complete the removal by cleaning up the namespace.
5524 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5526 vdev_t *rvd = spa->spa_root_vdev;
5527 uint64_t id = vd->vdev_id;
5528 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5530 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5531 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5532 ASSERT(vd == vd->vdev_top);
5535 * Only remove any devices which are empty.
5537 if (vd->vdev_stat.vs_alloc != 0)
5540 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5542 if (list_link_active(&vd->vdev_state_dirty_node))
5543 vdev_state_clean(vd);
5544 if (list_link_active(&vd->vdev_config_dirty_node))
5545 vdev_config_clean(vd);
5550 vdev_compact_children(rvd);
5552 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5553 vdev_add_child(rvd, vd);
5555 vdev_config_dirty(rvd);
5558 * Reassess the health of our root vdev.
5564 * Remove a device from the pool -
5566 * Removing a device from the vdev namespace requires several steps
5567 * and can take a significant amount of time. As a result we use
5568 * the spa_vdev_config_[enter/exit] functions which allow us to
5569 * grab and release the spa_config_lock while still holding the namespace
5570 * lock. During each step the configuration is synced out.
5572 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5576 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5579 metaslab_group_t *mg;
5580 nvlist_t **spares, **l2cache, *nv;
5582 uint_t nspares, nl2cache;
5584 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5586 ASSERT(spa_writeable(spa));
5589 txg = spa_vdev_enter(spa);
5591 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5593 if (spa->spa_spares.sav_vdevs != NULL &&
5594 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5595 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5596 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5598 * Only remove the hot spare if it's not currently in use
5601 if (vd == NULL || unspare) {
5602 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5603 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5604 spa_load_spares(spa);
5605 spa->spa_spares.sav_sync = B_TRUE;
5607 error = SET_ERROR(EBUSY);
5609 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5610 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5611 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5612 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5614 * Cache devices can always be removed.
5616 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5617 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5618 spa_load_l2cache(spa);
5619 spa->spa_l2cache.sav_sync = B_TRUE;
5620 } else if (vd != NULL && vd->vdev_islog) {
5622 ASSERT(vd == vd->vdev_top);
5627 * Stop allocating from this vdev.
5629 metaslab_group_passivate(mg);
5632 * Wait for the youngest allocations and frees to sync,
5633 * and then wait for the deferral of those frees to finish.
5635 spa_vdev_config_exit(spa, NULL,
5636 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5639 * Attempt to evacuate the vdev.
5641 error = spa_vdev_remove_evacuate(spa, vd);
5643 txg = spa_vdev_config_enter(spa);
5646 * If we couldn't evacuate the vdev, unwind.
5649 metaslab_group_activate(mg);
5650 return (spa_vdev_exit(spa, NULL, txg, error));
5654 * Clean up the vdev namespace.
5656 spa_vdev_remove_from_namespace(spa, vd);
5658 } else if (vd != NULL) {
5660 * Normal vdevs cannot be removed (yet).
5662 error = SET_ERROR(ENOTSUP);
5665 * There is no vdev of any kind with the specified guid.
5667 error = SET_ERROR(ENOENT);
5671 return (spa_vdev_exit(spa, NULL, txg, error));
5677 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5678 * currently spared, so we can detach it.
5681 spa_vdev_resilver_done_hunt(vdev_t *vd)
5683 vdev_t *newvd, *oldvd;
5685 for (int c = 0; c < vd->vdev_children; c++) {
5686 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5692 * Check for a completed replacement. We always consider the first
5693 * vdev in the list to be the oldest vdev, and the last one to be
5694 * the newest (see spa_vdev_attach() for how that works). In
5695 * the case where the newest vdev is faulted, we will not automatically
5696 * remove it after a resilver completes. This is OK as it will require
5697 * user intervention to determine which disk the admin wishes to keep.
5699 if (vd->vdev_ops == &vdev_replacing_ops) {
5700 ASSERT(vd->vdev_children > 1);
5702 newvd = vd->vdev_child[vd->vdev_children - 1];
5703 oldvd = vd->vdev_child[0];
5705 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5706 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5707 !vdev_dtl_required(oldvd))
5712 * Check for a completed resilver with the 'unspare' flag set.
5714 if (vd->vdev_ops == &vdev_spare_ops) {
5715 vdev_t *first = vd->vdev_child[0];
5716 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5718 if (last->vdev_unspare) {
5721 } else if (first->vdev_unspare) {
5728 if (oldvd != NULL &&
5729 vdev_dtl_empty(newvd, DTL_MISSING) &&
5730 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5731 !vdev_dtl_required(oldvd))
5735 * If there are more than two spares attached to a disk,
5736 * and those spares are not required, then we want to
5737 * attempt to free them up now so that they can be used
5738 * by other pools. Once we're back down to a single
5739 * disk+spare, we stop removing them.
5741 if (vd->vdev_children > 2) {
5742 newvd = vd->vdev_child[1];
5744 if (newvd->vdev_isspare && last->vdev_isspare &&
5745 vdev_dtl_empty(last, DTL_MISSING) &&
5746 vdev_dtl_empty(last, DTL_OUTAGE) &&
5747 !vdev_dtl_required(newvd))
5756 spa_vdev_resilver_done(spa_t *spa)
5758 vdev_t *vd, *pvd, *ppvd;
5759 uint64_t guid, sguid, pguid, ppguid;
5761 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5763 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5764 pvd = vd->vdev_parent;
5765 ppvd = pvd->vdev_parent;
5766 guid = vd->vdev_guid;
5767 pguid = pvd->vdev_guid;
5768 ppguid = ppvd->vdev_guid;
5771 * If we have just finished replacing a hot spared device, then
5772 * we need to detach the parent's first child (the original hot
5775 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5776 ppvd->vdev_children == 2) {
5777 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5778 sguid = ppvd->vdev_child[1]->vdev_guid;
5780 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5782 spa_config_exit(spa, SCL_ALL, FTAG);
5783 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5785 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5787 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5790 spa_config_exit(spa, SCL_ALL, FTAG);
5794 * Update the stored path or FRU for this vdev.
5797 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5801 boolean_t sync = B_FALSE;
5803 ASSERT(spa_writeable(spa));
5805 spa_vdev_state_enter(spa, SCL_ALL);
5807 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5808 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5810 if (!vd->vdev_ops->vdev_op_leaf)
5811 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5814 if (strcmp(value, vd->vdev_path) != 0) {
5815 spa_strfree(vd->vdev_path);
5816 vd->vdev_path = spa_strdup(value);
5820 if (vd->vdev_fru == NULL) {
5821 vd->vdev_fru = spa_strdup(value);
5823 } else if (strcmp(value, vd->vdev_fru) != 0) {
5824 spa_strfree(vd->vdev_fru);
5825 vd->vdev_fru = spa_strdup(value);
5830 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5834 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5836 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5840 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5842 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5846 * ==========================================================================
5848 * ==========================================================================
5852 spa_scan_stop(spa_t *spa)
5854 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5855 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5856 return (SET_ERROR(EBUSY));
5857 return (dsl_scan_cancel(spa->spa_dsl_pool));
5861 spa_scan(spa_t *spa, pool_scan_func_t func)
5863 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5865 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5866 return (SET_ERROR(ENOTSUP));
5869 * If a resilver was requested, but there is no DTL on a
5870 * writeable leaf device, we have nothing to do.
5872 if (func == POOL_SCAN_RESILVER &&
5873 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5874 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5878 return (dsl_scan(spa->spa_dsl_pool, func));
5882 * ==========================================================================
5883 * SPA async task processing
5884 * ==========================================================================
5888 spa_async_remove(spa_t *spa, vdev_t *vd)
5890 if (vd->vdev_remove_wanted) {
5891 vd->vdev_remove_wanted = B_FALSE;
5892 vd->vdev_delayed_close = B_FALSE;
5893 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5896 * We want to clear the stats, but we don't want to do a full
5897 * vdev_clear() as that will cause us to throw away
5898 * degraded/faulted state as well as attempt to reopen the
5899 * device, all of which is a waste.
5901 vd->vdev_stat.vs_read_errors = 0;
5902 vd->vdev_stat.vs_write_errors = 0;
5903 vd->vdev_stat.vs_checksum_errors = 0;
5905 vdev_state_dirty(vd->vdev_top);
5908 for (int c = 0; c < vd->vdev_children; c++)
5909 spa_async_remove(spa, vd->vdev_child[c]);
5913 spa_async_probe(spa_t *spa, vdev_t *vd)
5915 if (vd->vdev_probe_wanted) {
5916 vd->vdev_probe_wanted = B_FALSE;
5917 vdev_reopen(vd); /* vdev_open() does the actual probe */
5920 for (int c = 0; c < vd->vdev_children; c++)
5921 spa_async_probe(spa, vd->vdev_child[c]);
5925 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5931 if (!spa->spa_autoexpand)
5934 for (int c = 0; c < vd->vdev_children; c++) {
5935 vdev_t *cvd = vd->vdev_child[c];
5936 spa_async_autoexpand(spa, cvd);
5939 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5942 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5943 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5945 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5946 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5948 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5949 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5952 kmem_free(physpath, MAXPATHLEN);
5956 spa_async_thread(void *arg)
5961 ASSERT(spa->spa_sync_on);
5963 mutex_enter(&spa->spa_async_lock);
5964 tasks = spa->spa_async_tasks;
5965 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
5966 mutex_exit(&spa->spa_async_lock);
5969 * See if the config needs to be updated.
5971 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5972 uint64_t old_space, new_space;
5974 mutex_enter(&spa_namespace_lock);
5975 old_space = metaslab_class_get_space(spa_normal_class(spa));
5976 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5977 new_space = metaslab_class_get_space(spa_normal_class(spa));
5978 mutex_exit(&spa_namespace_lock);
5981 * If the pool grew as a result of the config update,
5982 * then log an internal history event.
5984 if (new_space != old_space) {
5985 spa_history_log_internal(spa, "vdev online", NULL,
5986 "pool '%s' size: %llu(+%llu)",
5987 spa_name(spa), new_space, new_space - old_space);
5991 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5992 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5993 spa_async_autoexpand(spa, spa->spa_root_vdev);
5994 spa_config_exit(spa, SCL_CONFIG, FTAG);
5998 * See if any devices need to be probed.
6000 if (tasks & SPA_ASYNC_PROBE) {
6001 spa_vdev_state_enter(spa, SCL_NONE);
6002 spa_async_probe(spa, spa->spa_root_vdev);
6003 (void) spa_vdev_state_exit(spa, NULL, 0);
6007 * If any devices are done replacing, detach them.
6009 if (tasks & SPA_ASYNC_RESILVER_DONE)
6010 spa_vdev_resilver_done(spa);
6013 * Kick off a resilver.
6015 if (tasks & SPA_ASYNC_RESILVER)
6016 dsl_resilver_restart(spa->spa_dsl_pool, 0);
6019 * Let the world know that we're done.
6021 mutex_enter(&spa->spa_async_lock);
6022 spa->spa_async_thread = NULL;
6023 cv_broadcast(&spa->spa_async_cv);
6024 mutex_exit(&spa->spa_async_lock);
6029 spa_async_thread_vd(void *arg)
6034 ASSERT(spa->spa_sync_on);
6036 mutex_enter(&spa->spa_async_lock);
6037 tasks = spa->spa_async_tasks;
6039 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6040 mutex_exit(&spa->spa_async_lock);
6043 * See if any devices need to be marked REMOVED.
6045 if (tasks & SPA_ASYNC_REMOVE) {
6046 spa_vdev_state_enter(spa, SCL_NONE);
6047 spa_async_remove(spa, spa->spa_root_vdev);
6048 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6049 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6050 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6051 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6052 (void) spa_vdev_state_exit(spa, NULL, 0);
6056 * Let the world know that we're done.
6058 mutex_enter(&spa->spa_async_lock);
6059 tasks = spa->spa_async_tasks;
6060 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6062 spa->spa_async_thread_vd = NULL;
6063 cv_broadcast(&spa->spa_async_cv);
6064 mutex_exit(&spa->spa_async_lock);
6069 spa_async_suspend(spa_t *spa)
6071 mutex_enter(&spa->spa_async_lock);
6072 spa->spa_async_suspended++;
6073 while (spa->spa_async_thread != NULL &&
6074 spa->spa_async_thread_vd != NULL)
6075 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6076 mutex_exit(&spa->spa_async_lock);
6080 spa_async_resume(spa_t *spa)
6082 mutex_enter(&spa->spa_async_lock);
6083 ASSERT(spa->spa_async_suspended != 0);
6084 spa->spa_async_suspended--;
6085 mutex_exit(&spa->spa_async_lock);
6089 spa_async_tasks_pending(spa_t *spa)
6091 uint_t non_config_tasks;
6093 boolean_t config_task_suspended;
6095 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6097 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6098 if (spa->spa_ccw_fail_time == 0) {
6099 config_task_suspended = B_FALSE;
6101 config_task_suspended =
6102 (gethrtime() - spa->spa_ccw_fail_time) <
6103 (zfs_ccw_retry_interval * NANOSEC);
6106 return (non_config_tasks || (config_task && !config_task_suspended));
6110 spa_async_dispatch(spa_t *spa)
6112 mutex_enter(&spa->spa_async_lock);
6113 if (spa_async_tasks_pending(spa) &&
6114 !spa->spa_async_suspended &&
6115 spa->spa_async_thread == NULL &&
6117 spa->spa_async_thread = thread_create(NULL, 0,
6118 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6119 mutex_exit(&spa->spa_async_lock);
6123 spa_async_dispatch_vd(spa_t *spa)
6125 mutex_enter(&spa->spa_async_lock);
6126 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6127 !spa->spa_async_suspended &&
6128 spa->spa_async_thread_vd == NULL &&
6130 spa->spa_async_thread_vd = thread_create(NULL, 0,
6131 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6132 mutex_exit(&spa->spa_async_lock);
6136 spa_async_request(spa_t *spa, int task)
6138 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6139 mutex_enter(&spa->spa_async_lock);
6140 spa->spa_async_tasks |= task;
6141 mutex_exit(&spa->spa_async_lock);
6142 spa_async_dispatch_vd(spa);
6146 * ==========================================================================
6147 * SPA syncing routines
6148 * ==========================================================================
6152 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6155 bpobj_enqueue(bpo, bp, tx);
6160 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6164 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6165 BP_GET_PSIZE(bp), zio->io_flags));
6170 * Note: this simple function is not inlined to make it easier to dtrace the
6171 * amount of time spent syncing frees.
6174 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6176 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6177 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6178 VERIFY(zio_wait(zio) == 0);
6182 * Note: this simple function is not inlined to make it easier to dtrace the
6183 * amount of time spent syncing deferred frees.
6186 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6188 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6189 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6190 spa_free_sync_cb, zio, tx), ==, 0);
6191 VERIFY0(zio_wait(zio));
6196 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6198 char *packed = NULL;
6203 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6206 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6207 * information. This avoids the dmu_buf_will_dirty() path and
6208 * saves us a pre-read to get data we don't actually care about.
6210 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6211 packed = kmem_alloc(bufsize, KM_SLEEP);
6213 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6215 bzero(packed + nvsize, bufsize - nvsize);
6217 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6219 kmem_free(packed, bufsize);
6221 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6222 dmu_buf_will_dirty(db, tx);
6223 *(uint64_t *)db->db_data = nvsize;
6224 dmu_buf_rele(db, FTAG);
6228 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6229 const char *config, const char *entry)
6239 * Update the MOS nvlist describing the list of available devices.
6240 * spa_validate_aux() will have already made sure this nvlist is
6241 * valid and the vdevs are labeled appropriately.
6243 if (sav->sav_object == 0) {
6244 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6245 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6246 sizeof (uint64_t), tx);
6247 VERIFY(zap_update(spa->spa_meta_objset,
6248 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6249 &sav->sav_object, tx) == 0);
6252 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6253 if (sav->sav_count == 0) {
6254 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6256 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6257 for (i = 0; i < sav->sav_count; i++)
6258 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6259 B_FALSE, VDEV_CONFIG_L2CACHE);
6260 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6261 sav->sav_count) == 0);
6262 for (i = 0; i < sav->sav_count; i++)
6263 nvlist_free(list[i]);
6264 kmem_free(list, sav->sav_count * sizeof (void *));
6267 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6268 nvlist_free(nvroot);
6270 sav->sav_sync = B_FALSE;
6274 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6278 if (list_is_empty(&spa->spa_config_dirty_list))
6281 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6283 config = spa_config_generate(spa, spa->spa_root_vdev,
6284 dmu_tx_get_txg(tx), B_FALSE);
6287 * If we're upgrading the spa version then make sure that
6288 * the config object gets updated with the correct version.
6290 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6291 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6292 spa->spa_uberblock.ub_version);
6294 spa_config_exit(spa, SCL_STATE, FTAG);
6296 if (spa->spa_config_syncing)
6297 nvlist_free(spa->spa_config_syncing);
6298 spa->spa_config_syncing = config;
6300 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6304 spa_sync_version(void *arg, dmu_tx_t *tx)
6306 uint64_t *versionp = arg;
6307 uint64_t version = *versionp;
6308 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6311 * Setting the version is special cased when first creating the pool.
6313 ASSERT(tx->tx_txg != TXG_INITIAL);
6315 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6316 ASSERT(version >= spa_version(spa));
6318 spa->spa_uberblock.ub_version = version;
6319 vdev_config_dirty(spa->spa_root_vdev);
6320 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6324 * Set zpool properties.
6327 spa_sync_props(void *arg, dmu_tx_t *tx)
6329 nvlist_t *nvp = arg;
6330 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6331 objset_t *mos = spa->spa_meta_objset;
6332 nvpair_t *elem = NULL;
6334 mutex_enter(&spa->spa_props_lock);
6336 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6338 char *strval, *fname;
6340 const char *propname;
6341 zprop_type_t proptype;
6344 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6347 * We checked this earlier in spa_prop_validate().
6349 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6351 fname = strchr(nvpair_name(elem), '@') + 1;
6352 VERIFY0(zfeature_lookup_name(fname, &fid));
6354 spa_feature_enable(spa, fid, tx);
6355 spa_history_log_internal(spa, "set", tx,
6356 "%s=enabled", nvpair_name(elem));
6359 case ZPOOL_PROP_VERSION:
6360 intval = fnvpair_value_uint64(elem);
6362 * The version is synced seperatly before other
6363 * properties and should be correct by now.
6365 ASSERT3U(spa_version(spa), >=, intval);
6368 case ZPOOL_PROP_ALTROOT:
6370 * 'altroot' is a non-persistent property. It should
6371 * have been set temporarily at creation or import time.
6373 ASSERT(spa->spa_root != NULL);
6376 case ZPOOL_PROP_READONLY:
6377 case ZPOOL_PROP_CACHEFILE:
6379 * 'readonly' and 'cachefile' are also non-persisitent
6383 case ZPOOL_PROP_COMMENT:
6384 strval = fnvpair_value_string(elem);
6385 if (spa->spa_comment != NULL)
6386 spa_strfree(spa->spa_comment);
6387 spa->spa_comment = spa_strdup(strval);
6389 * We need to dirty the configuration on all the vdevs
6390 * so that their labels get updated. It's unnecessary
6391 * to do this for pool creation since the vdev's
6392 * configuratoin has already been dirtied.
6394 if (tx->tx_txg != TXG_INITIAL)
6395 vdev_config_dirty(spa->spa_root_vdev);
6396 spa_history_log_internal(spa, "set", tx,
6397 "%s=%s", nvpair_name(elem), strval);
6401 * Set pool property values in the poolprops mos object.
6403 if (spa->spa_pool_props_object == 0) {
6404 spa->spa_pool_props_object =
6405 zap_create_link(mos, DMU_OT_POOL_PROPS,
6406 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6410 /* normalize the property name */
6411 propname = zpool_prop_to_name(prop);
6412 proptype = zpool_prop_get_type(prop);
6414 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6415 ASSERT(proptype == PROP_TYPE_STRING);
6416 strval = fnvpair_value_string(elem);
6417 VERIFY0(zap_update(mos,
6418 spa->spa_pool_props_object, propname,
6419 1, strlen(strval) + 1, strval, tx));
6420 spa_history_log_internal(spa, "set", tx,
6421 "%s=%s", nvpair_name(elem), strval);
6422 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6423 intval = fnvpair_value_uint64(elem);
6425 if (proptype == PROP_TYPE_INDEX) {
6427 VERIFY0(zpool_prop_index_to_string(
6428 prop, intval, &unused));
6430 VERIFY0(zap_update(mos,
6431 spa->spa_pool_props_object, propname,
6432 8, 1, &intval, tx));
6433 spa_history_log_internal(spa, "set", tx,
6434 "%s=%lld", nvpair_name(elem), intval);
6436 ASSERT(0); /* not allowed */
6440 case ZPOOL_PROP_DELEGATION:
6441 spa->spa_delegation = intval;
6443 case ZPOOL_PROP_BOOTFS:
6444 spa->spa_bootfs = intval;
6446 case ZPOOL_PROP_FAILUREMODE:
6447 spa->spa_failmode = intval;
6449 case ZPOOL_PROP_AUTOEXPAND:
6450 spa->spa_autoexpand = intval;
6451 if (tx->tx_txg != TXG_INITIAL)
6452 spa_async_request(spa,
6453 SPA_ASYNC_AUTOEXPAND);
6455 case ZPOOL_PROP_DEDUPDITTO:
6456 spa->spa_dedup_ditto = intval;
6465 mutex_exit(&spa->spa_props_lock);
6469 * Perform one-time upgrade on-disk changes. spa_version() does not
6470 * reflect the new version this txg, so there must be no changes this
6471 * txg to anything that the upgrade code depends on after it executes.
6472 * Therefore this must be called after dsl_pool_sync() does the sync
6476 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6478 dsl_pool_t *dp = spa->spa_dsl_pool;
6480 ASSERT(spa->spa_sync_pass == 1);
6482 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6484 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6485 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6486 dsl_pool_create_origin(dp, tx);
6488 /* Keeping the origin open increases spa_minref */
6489 spa->spa_minref += 3;
6492 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6493 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6494 dsl_pool_upgrade_clones(dp, tx);
6497 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6498 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6499 dsl_pool_upgrade_dir_clones(dp, tx);
6501 /* Keeping the freedir open increases spa_minref */
6502 spa->spa_minref += 3;
6505 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6506 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6507 spa_feature_create_zap_objects(spa, tx);
6511 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6512 * when possibility to use lz4 compression for metadata was added
6513 * Old pools that have this feature enabled must be upgraded to have
6514 * this feature active
6516 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6517 boolean_t lz4_en = spa_feature_is_enabled(spa,
6518 SPA_FEATURE_LZ4_COMPRESS);
6519 boolean_t lz4_ac = spa_feature_is_active(spa,
6520 SPA_FEATURE_LZ4_COMPRESS);
6522 if (lz4_en && !lz4_ac)
6523 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6525 rrw_exit(&dp->dp_config_rwlock, FTAG);
6529 * Sync the specified transaction group. New blocks may be dirtied as
6530 * part of the process, so we iterate until it converges.
6533 spa_sync(spa_t *spa, uint64_t txg)
6535 dsl_pool_t *dp = spa->spa_dsl_pool;
6536 objset_t *mos = spa->spa_meta_objset;
6537 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6538 vdev_t *rvd = spa->spa_root_vdev;
6543 VERIFY(spa_writeable(spa));
6546 * Lock out configuration changes.
6548 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6550 spa->spa_syncing_txg = txg;
6551 spa->spa_sync_pass = 0;
6554 * If there are any pending vdev state changes, convert them
6555 * into config changes that go out with this transaction group.
6557 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6558 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6560 * We need the write lock here because, for aux vdevs,
6561 * calling vdev_config_dirty() modifies sav_config.
6562 * This is ugly and will become unnecessary when we
6563 * eliminate the aux vdev wart by integrating all vdevs
6564 * into the root vdev tree.
6566 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6567 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6568 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6569 vdev_state_clean(vd);
6570 vdev_config_dirty(vd);
6572 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6573 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6575 spa_config_exit(spa, SCL_STATE, FTAG);
6577 tx = dmu_tx_create_assigned(dp, txg);
6579 spa->spa_sync_starttime = gethrtime();
6581 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6582 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6585 callout_reset(&spa->spa_deadman_cycid,
6586 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6591 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6592 * set spa_deflate if we have no raid-z vdevs.
6594 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6595 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6598 for (i = 0; i < rvd->vdev_children; i++) {
6599 vd = rvd->vdev_child[i];
6600 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6603 if (i == rvd->vdev_children) {
6604 spa->spa_deflate = TRUE;
6605 VERIFY(0 == zap_add(spa->spa_meta_objset,
6606 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6607 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6612 * Iterate to convergence.
6615 int pass = ++spa->spa_sync_pass;
6617 spa_sync_config_object(spa, tx);
6618 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6619 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6620 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6621 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6622 spa_errlog_sync(spa, txg);
6623 dsl_pool_sync(dp, txg);
6625 if (pass < zfs_sync_pass_deferred_free) {
6626 spa_sync_frees(spa, free_bpl, tx);
6629 * We can not defer frees in pass 1, because
6630 * we sync the deferred frees later in pass 1.
6632 ASSERT3U(pass, >, 1);
6633 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6634 &spa->spa_deferred_bpobj, tx);
6638 dsl_scan_sync(dp, tx);
6640 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6644 spa_sync_upgrades(spa, tx);
6646 spa->spa_uberblock.ub_rootbp.blk_birth);
6648 * Note: We need to check if the MOS is dirty
6649 * because we could have marked the MOS dirty
6650 * without updating the uberblock (e.g. if we
6651 * have sync tasks but no dirty user data). We
6652 * need to check the uberblock's rootbp because
6653 * it is updated if we have synced out dirty
6654 * data (though in this case the MOS will most
6655 * likely also be dirty due to second order
6656 * effects, we don't want to rely on that here).
6658 if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6659 !dmu_objset_is_dirty(mos, txg)) {
6661 * Nothing changed on the first pass,
6662 * therefore this TXG is a no-op. Avoid
6663 * syncing deferred frees, so that we
6664 * can keep this TXG as a no-op.
6666 ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6668 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6669 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
6672 spa_sync_deferred_frees(spa, tx);
6675 } while (dmu_objset_is_dirty(mos, txg));
6678 * Rewrite the vdev configuration (which includes the uberblock)
6679 * to commit the transaction group.
6681 * If there are no dirty vdevs, we sync the uberblock to a few
6682 * random top-level vdevs that are known to be visible in the
6683 * config cache (see spa_vdev_add() for a complete description).
6684 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6688 * We hold SCL_STATE to prevent vdev open/close/etc.
6689 * while we're attempting to write the vdev labels.
6691 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6693 if (list_is_empty(&spa->spa_config_dirty_list)) {
6694 vdev_t *svd[SPA_DVAS_PER_BP];
6696 int children = rvd->vdev_children;
6697 int c0 = spa_get_random(children);
6699 for (int c = 0; c < children; c++) {
6700 vd = rvd->vdev_child[(c0 + c) % children];
6701 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6703 svd[svdcount++] = vd;
6704 if (svdcount == SPA_DVAS_PER_BP)
6707 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6709 error = vdev_config_sync(svd, svdcount, txg,
6712 error = vdev_config_sync(rvd->vdev_child,
6713 rvd->vdev_children, txg, B_FALSE);
6715 error = vdev_config_sync(rvd->vdev_child,
6716 rvd->vdev_children, txg, B_TRUE);
6720 spa->spa_last_synced_guid = rvd->vdev_guid;
6722 spa_config_exit(spa, SCL_STATE, FTAG);
6726 zio_suspend(spa, NULL);
6727 zio_resume_wait(spa);
6732 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6735 callout_drain(&spa->spa_deadman_cycid);
6740 * Clear the dirty config list.
6742 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6743 vdev_config_clean(vd);
6746 * Now that the new config has synced transactionally,
6747 * let it become visible to the config cache.
6749 if (spa->spa_config_syncing != NULL) {
6750 spa_config_set(spa, spa->spa_config_syncing);
6751 spa->spa_config_txg = txg;
6752 spa->spa_config_syncing = NULL;
6755 spa->spa_ubsync = spa->spa_uberblock;
6757 dsl_pool_sync_done(dp, txg);
6760 * Update usable space statistics.
6762 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6763 vdev_sync_done(vd, txg);
6765 spa_update_dspace(spa);
6768 * It had better be the case that we didn't dirty anything
6769 * since vdev_config_sync().
6771 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6772 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6773 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6775 spa->spa_sync_pass = 0;
6777 spa_config_exit(spa, SCL_CONFIG, FTAG);
6779 spa_handle_ignored_writes(spa);
6782 * If any async tasks have been requested, kick them off.
6784 spa_async_dispatch(spa);
6785 spa_async_dispatch_vd(spa);
6789 * Sync all pools. We don't want to hold the namespace lock across these
6790 * operations, so we take a reference on the spa_t and drop the lock during the
6794 spa_sync_allpools(void)
6797 mutex_enter(&spa_namespace_lock);
6798 while ((spa = spa_next(spa)) != NULL) {
6799 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6800 !spa_writeable(spa) || spa_suspended(spa))
6802 spa_open_ref(spa, FTAG);
6803 mutex_exit(&spa_namespace_lock);
6804 txg_wait_synced(spa_get_dsl(spa), 0);
6805 mutex_enter(&spa_namespace_lock);
6806 spa_close(spa, FTAG);
6808 mutex_exit(&spa_namespace_lock);
6812 * ==========================================================================
6813 * Miscellaneous routines
6814 * ==========================================================================
6818 * Remove all pools in the system.
6826 * Remove all cached state. All pools should be closed now,
6827 * so every spa in the AVL tree should be unreferenced.
6829 mutex_enter(&spa_namespace_lock);
6830 while ((spa = spa_next(NULL)) != NULL) {
6832 * Stop async tasks. The async thread may need to detach
6833 * a device that's been replaced, which requires grabbing
6834 * spa_namespace_lock, so we must drop it here.
6836 spa_open_ref(spa, FTAG);
6837 mutex_exit(&spa_namespace_lock);
6838 spa_async_suspend(spa);
6839 mutex_enter(&spa_namespace_lock);
6840 spa_close(spa, FTAG);
6842 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6844 spa_deactivate(spa);
6848 mutex_exit(&spa_namespace_lock);
6852 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6857 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6861 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6862 vd = spa->spa_l2cache.sav_vdevs[i];
6863 if (vd->vdev_guid == guid)
6867 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6868 vd = spa->spa_spares.sav_vdevs[i];
6869 if (vd->vdev_guid == guid)
6878 spa_upgrade(spa_t *spa, uint64_t version)
6880 ASSERT(spa_writeable(spa));
6882 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6885 * This should only be called for a non-faulted pool, and since a
6886 * future version would result in an unopenable pool, this shouldn't be
6889 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6890 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6892 spa->spa_uberblock.ub_version = version;
6893 vdev_config_dirty(spa->spa_root_vdev);
6895 spa_config_exit(spa, SCL_ALL, FTAG);
6897 txg_wait_synced(spa_get_dsl(spa), 0);
6901 spa_has_spare(spa_t *spa, uint64_t guid)
6905 spa_aux_vdev_t *sav = &spa->spa_spares;
6907 for (i = 0; i < sav->sav_count; i++)
6908 if (sav->sav_vdevs[i]->vdev_guid == guid)
6911 for (i = 0; i < sav->sav_npending; i++) {
6912 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6913 &spareguid) == 0 && spareguid == guid)
6921 * Check if a pool has an active shared spare device.
6922 * Note: reference count of an active spare is 2, as a spare and as a replace
6925 spa_has_active_shared_spare(spa_t *spa)
6929 spa_aux_vdev_t *sav = &spa->spa_spares;
6931 for (i = 0; i < sav->sav_count; i++) {
6932 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6933 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6942 * Post a sysevent corresponding to the given event. The 'name' must be one of
6943 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6944 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6945 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6946 * or zdb as real changes.
6949 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6953 sysevent_attr_list_t *attr = NULL;
6954 sysevent_value_t value;
6957 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6960 value.value_type = SE_DATA_TYPE_STRING;
6961 value.value.sv_string = spa_name(spa);
6962 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6965 value.value_type = SE_DATA_TYPE_UINT64;
6966 value.value.sv_uint64 = spa_guid(spa);
6967 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6971 value.value_type = SE_DATA_TYPE_UINT64;
6972 value.value.sv_uint64 = vd->vdev_guid;
6973 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6977 if (vd->vdev_path) {
6978 value.value_type = SE_DATA_TYPE_STRING;
6979 value.value.sv_string = vd->vdev_path;
6980 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6981 &value, SE_SLEEP) != 0)
6986 if (sysevent_attach_attributes(ev, attr) != 0)
6990 (void) log_sysevent(ev, SE_SLEEP, &eid);
6994 sysevent_free_attr(attr);