4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 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;
87 * The interval, in seconds, at which failed configuration cache file writes
90 static int zfs_ccw_retry_interval = 300;
92 SYSCTL_DECL(_vfs_zfs);
93 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RWTUN, &check_hostid, 0,
94 "Check hostid on import?");
95 TUNABLE_INT("vfs.zfs.ccw_retry_interval", &zfs_ccw_retry_interval);
96 SYSCTL_INT(_vfs_zfs, OID_AUTO, ccw_retry_interval, CTLFLAG_RW,
97 &zfs_ccw_retry_interval, 0,
98 "Configuration cache file write, retry after failure, interval (seconds)");
100 typedef enum zti_modes {
101 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
102 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
103 ZTI_MODE_NULL, /* don't create a taskq */
107 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
108 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
109 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
111 #define ZTI_N(n) ZTI_P(n, 1)
112 #define ZTI_ONE ZTI_N(1)
114 typedef struct zio_taskq_info {
115 zti_modes_t zti_mode;
120 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
121 "issue", "issue_high", "intr", "intr_high"
125 * This table defines the taskq settings for each ZFS I/O type. When
126 * initializing a pool, we use this table to create an appropriately sized
127 * taskq. Some operations are low volume and therefore have a small, static
128 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
129 * macros. Other operations process a large amount of data; the ZTI_BATCH
130 * macro causes us to create a taskq oriented for throughput. Some operations
131 * are so high frequency and short-lived that the taskq itself can become a a
132 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
133 * additional degree of parallelism specified by the number of threads per-
134 * taskq and the number of taskqs; when dispatching an event in this case, the
135 * particular taskq is chosen at random.
137 * The different taskq priorities are to handle the different contexts (issue
138 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
139 * need to be handled with minimum delay.
141 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
142 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
143 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
144 { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */
145 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
146 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
147 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
148 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
151 static void spa_sync_version(void *arg, dmu_tx_t *tx);
152 static void spa_sync_props(void *arg, dmu_tx_t *tx);
153 static boolean_t spa_has_active_shared_spare(spa_t *spa);
154 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
155 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
157 static void spa_vdev_resilver_done(spa_t *spa);
159 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
161 id_t zio_taskq_psrset_bind = PS_NONE;
164 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
166 uint_t zio_taskq_basedc = 80; /* base duty cycle */
168 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
169 extern int zfs_sync_pass_deferred_free;
172 extern void spa_deadman(void *arg);
176 * This (illegal) pool name is used when temporarily importing a spa_t in order
177 * to get the vdev stats associated with the imported devices.
179 #define TRYIMPORT_NAME "$import"
182 * ==========================================================================
183 * SPA properties routines
184 * ==========================================================================
188 * Add a (source=src, propname=propval) list to an nvlist.
191 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
192 uint64_t intval, zprop_source_t src)
194 const char *propname = zpool_prop_to_name(prop);
197 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
198 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
201 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
203 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
205 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
206 nvlist_free(propval);
210 * Get property values from the spa configuration.
213 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
215 vdev_t *rvd = spa->spa_root_vdev;
216 dsl_pool_t *pool = spa->spa_dsl_pool;
217 uint64_t size, alloc, cap, version;
218 zprop_source_t src = ZPROP_SRC_NONE;
219 spa_config_dirent_t *dp;
220 metaslab_class_t *mc = spa_normal_class(spa);
222 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
225 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
226 size = metaslab_class_get_space(spa_normal_class(spa));
227 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
228 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
229 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
230 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
233 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
234 metaslab_class_fragmentation(mc), src);
235 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
236 metaslab_class_expandable_space(mc), src);
237 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
238 (spa_mode(spa) == FREAD), src);
240 cap = (size == 0) ? 0 : (alloc * 100 / size);
241 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
243 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
244 ddt_get_pool_dedup_ratio(spa), src);
246 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
247 rvd->vdev_state, src);
249 version = spa_version(spa);
250 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
251 src = ZPROP_SRC_DEFAULT;
253 src = ZPROP_SRC_LOCAL;
254 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
259 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
260 * when opening pools before this version freedir will be NULL.
262 if (pool->dp_free_dir != NULL) {
263 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
264 pool->dp_free_dir->dd_phys->dd_used_bytes, src);
266 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
270 if (pool->dp_leak_dir != NULL) {
271 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
272 pool->dp_leak_dir->dd_phys->dd_used_bytes, src);
274 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
279 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
281 if (spa->spa_comment != NULL) {
282 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
286 if (spa->spa_root != NULL)
287 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
290 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
291 if (dp->scd_path == NULL) {
292 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
293 "none", 0, ZPROP_SRC_LOCAL);
294 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
295 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
296 dp->scd_path, 0, ZPROP_SRC_LOCAL);
302 * Get zpool property values.
305 spa_prop_get(spa_t *spa, nvlist_t **nvp)
307 objset_t *mos = spa->spa_meta_objset;
312 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
314 mutex_enter(&spa->spa_props_lock);
317 * Get properties from the spa config.
319 spa_prop_get_config(spa, nvp);
321 /* If no pool property object, no more prop to get. */
322 if (mos == NULL || spa->spa_pool_props_object == 0) {
323 mutex_exit(&spa->spa_props_lock);
328 * Get properties from the MOS pool property object.
330 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
331 (err = zap_cursor_retrieve(&zc, &za)) == 0;
332 zap_cursor_advance(&zc)) {
335 zprop_source_t src = ZPROP_SRC_DEFAULT;
338 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
341 switch (za.za_integer_length) {
343 /* integer property */
344 if (za.za_first_integer !=
345 zpool_prop_default_numeric(prop))
346 src = ZPROP_SRC_LOCAL;
348 if (prop == ZPOOL_PROP_BOOTFS) {
350 dsl_dataset_t *ds = NULL;
352 dp = spa_get_dsl(spa);
353 dsl_pool_config_enter(dp, FTAG);
354 if (err = dsl_dataset_hold_obj(dp,
355 za.za_first_integer, FTAG, &ds)) {
356 dsl_pool_config_exit(dp, FTAG);
361 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
363 dsl_dataset_name(ds, strval);
364 dsl_dataset_rele(ds, FTAG);
365 dsl_pool_config_exit(dp, FTAG);
368 intval = za.za_first_integer;
371 spa_prop_add_list(*nvp, prop, strval, intval, src);
375 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
380 /* string property */
381 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
382 err = zap_lookup(mos, spa->spa_pool_props_object,
383 za.za_name, 1, za.za_num_integers, strval);
385 kmem_free(strval, za.za_num_integers);
388 spa_prop_add_list(*nvp, prop, strval, 0, src);
389 kmem_free(strval, za.za_num_integers);
396 zap_cursor_fini(&zc);
397 mutex_exit(&spa->spa_props_lock);
399 if (err && err != ENOENT) {
409 * Validate the given pool properties nvlist and modify the list
410 * for the property values to be set.
413 spa_prop_validate(spa_t *spa, nvlist_t *props)
416 int error = 0, reset_bootfs = 0;
418 boolean_t has_feature = B_FALSE;
421 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
423 char *strval, *slash, *check, *fname;
424 const char *propname = nvpair_name(elem);
425 zpool_prop_t prop = zpool_name_to_prop(propname);
429 if (!zpool_prop_feature(propname)) {
430 error = SET_ERROR(EINVAL);
435 * Sanitize the input.
437 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
438 error = SET_ERROR(EINVAL);
442 if (nvpair_value_uint64(elem, &intval) != 0) {
443 error = SET_ERROR(EINVAL);
448 error = SET_ERROR(EINVAL);
452 fname = strchr(propname, '@') + 1;
453 if (zfeature_lookup_name(fname, NULL) != 0) {
454 error = SET_ERROR(EINVAL);
458 has_feature = B_TRUE;
461 case ZPOOL_PROP_VERSION:
462 error = nvpair_value_uint64(elem, &intval);
464 (intval < spa_version(spa) ||
465 intval > SPA_VERSION_BEFORE_FEATURES ||
467 error = SET_ERROR(EINVAL);
470 case ZPOOL_PROP_DELEGATION:
471 case ZPOOL_PROP_AUTOREPLACE:
472 case ZPOOL_PROP_LISTSNAPS:
473 case ZPOOL_PROP_AUTOEXPAND:
474 error = nvpair_value_uint64(elem, &intval);
475 if (!error && intval > 1)
476 error = SET_ERROR(EINVAL);
479 case ZPOOL_PROP_BOOTFS:
481 * If the pool version is less than SPA_VERSION_BOOTFS,
482 * or the pool is still being created (version == 0),
483 * the bootfs property cannot be set.
485 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
486 error = SET_ERROR(ENOTSUP);
491 * Make sure the vdev config is bootable
493 if (!vdev_is_bootable(spa->spa_root_vdev)) {
494 error = SET_ERROR(ENOTSUP);
500 error = nvpair_value_string(elem, &strval);
506 if (strval == NULL || strval[0] == '\0') {
507 objnum = zpool_prop_default_numeric(
512 if (error = dmu_objset_hold(strval, FTAG, &os))
515 /* Must be ZPL and not gzip compressed. */
517 if (dmu_objset_type(os) != DMU_OST_ZFS) {
518 error = SET_ERROR(ENOTSUP);
520 dsl_prop_get_int_ds(dmu_objset_ds(os),
521 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
523 !BOOTFS_COMPRESS_VALID(compress)) {
524 error = SET_ERROR(ENOTSUP);
526 objnum = dmu_objset_id(os);
528 dmu_objset_rele(os, FTAG);
532 case ZPOOL_PROP_FAILUREMODE:
533 error = nvpair_value_uint64(elem, &intval);
534 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
535 intval > ZIO_FAILURE_MODE_PANIC))
536 error = SET_ERROR(EINVAL);
539 * This is a special case which only occurs when
540 * the pool has completely failed. This allows
541 * the user to change the in-core failmode property
542 * without syncing it out to disk (I/Os might
543 * currently be blocked). We do this by returning
544 * EIO to the caller (spa_prop_set) to trick it
545 * into thinking we encountered a property validation
548 if (!error && spa_suspended(spa)) {
549 spa->spa_failmode = intval;
550 error = SET_ERROR(EIO);
554 case ZPOOL_PROP_CACHEFILE:
555 if ((error = nvpair_value_string(elem, &strval)) != 0)
558 if (strval[0] == '\0')
561 if (strcmp(strval, "none") == 0)
564 if (strval[0] != '/') {
565 error = SET_ERROR(EINVAL);
569 slash = strrchr(strval, '/');
570 ASSERT(slash != NULL);
572 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
573 strcmp(slash, "/..") == 0)
574 error = SET_ERROR(EINVAL);
577 case ZPOOL_PROP_COMMENT:
578 if ((error = nvpair_value_string(elem, &strval)) != 0)
580 for (check = strval; *check != '\0'; check++) {
582 * The kernel doesn't have an easy isprint()
583 * check. For this kernel check, we merely
584 * check ASCII apart from DEL. Fix this if
585 * there is an easy-to-use kernel isprint().
587 if (*check >= 0x7f) {
588 error = SET_ERROR(EINVAL);
593 if (strlen(strval) > ZPROP_MAX_COMMENT)
597 case ZPOOL_PROP_DEDUPDITTO:
598 if (spa_version(spa) < SPA_VERSION_DEDUP)
599 error = SET_ERROR(ENOTSUP);
601 error = nvpair_value_uint64(elem, &intval);
603 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
604 error = SET_ERROR(EINVAL);
612 if (!error && reset_bootfs) {
613 error = nvlist_remove(props,
614 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
617 error = nvlist_add_uint64(props,
618 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
626 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
629 spa_config_dirent_t *dp;
631 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
635 dp = kmem_alloc(sizeof (spa_config_dirent_t),
638 if (cachefile[0] == '\0')
639 dp->scd_path = spa_strdup(spa_config_path);
640 else if (strcmp(cachefile, "none") == 0)
643 dp->scd_path = spa_strdup(cachefile);
645 list_insert_head(&spa->spa_config_list, dp);
647 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
651 spa_prop_set(spa_t *spa, nvlist_t *nvp)
654 nvpair_t *elem = NULL;
655 boolean_t need_sync = B_FALSE;
657 if ((error = spa_prop_validate(spa, nvp)) != 0)
660 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
661 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
663 if (prop == ZPOOL_PROP_CACHEFILE ||
664 prop == ZPOOL_PROP_ALTROOT ||
665 prop == ZPOOL_PROP_READONLY)
668 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
671 if (prop == ZPOOL_PROP_VERSION) {
672 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
674 ASSERT(zpool_prop_feature(nvpair_name(elem)));
675 ver = SPA_VERSION_FEATURES;
679 /* Save time if the version is already set. */
680 if (ver == spa_version(spa))
684 * In addition to the pool directory object, we might
685 * create the pool properties object, the features for
686 * read object, the features for write object, or the
687 * feature descriptions object.
689 error = dsl_sync_task(spa->spa_name, NULL,
690 spa_sync_version, &ver,
691 6, ZFS_SPACE_CHECK_RESERVED);
702 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
703 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
710 * If the bootfs property value is dsobj, clear it.
713 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
715 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
716 VERIFY(zap_remove(spa->spa_meta_objset,
717 spa->spa_pool_props_object,
718 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
725 spa_change_guid_check(void *arg, dmu_tx_t *tx)
727 uint64_t *newguid = arg;
728 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
729 vdev_t *rvd = spa->spa_root_vdev;
732 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
733 vdev_state = rvd->vdev_state;
734 spa_config_exit(spa, SCL_STATE, FTAG);
736 if (vdev_state != VDEV_STATE_HEALTHY)
737 return (SET_ERROR(ENXIO));
739 ASSERT3U(spa_guid(spa), !=, *newguid);
745 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
747 uint64_t *newguid = arg;
748 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
750 vdev_t *rvd = spa->spa_root_vdev;
752 oldguid = spa_guid(spa);
754 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
755 rvd->vdev_guid = *newguid;
756 rvd->vdev_guid_sum += (*newguid - oldguid);
757 vdev_config_dirty(rvd);
758 spa_config_exit(spa, SCL_STATE, FTAG);
760 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
765 * Change the GUID for the pool. This is done so that we can later
766 * re-import a pool built from a clone of our own vdevs. We will modify
767 * the root vdev's guid, our own pool guid, and then mark all of our
768 * vdevs dirty. Note that we must make sure that all our vdevs are
769 * online when we do this, or else any vdevs that weren't present
770 * would be orphaned from our pool. We are also going to issue a
771 * sysevent to update any watchers.
774 spa_change_guid(spa_t *spa)
779 mutex_enter(&spa->spa_vdev_top_lock);
780 mutex_enter(&spa_namespace_lock);
781 guid = spa_generate_guid(NULL);
783 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
784 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
787 spa_config_sync(spa, B_FALSE, B_TRUE);
788 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
791 mutex_exit(&spa_namespace_lock);
792 mutex_exit(&spa->spa_vdev_top_lock);
798 * ==========================================================================
799 * SPA state manipulation (open/create/destroy/import/export)
800 * ==========================================================================
804 spa_error_entry_compare(const void *a, const void *b)
806 spa_error_entry_t *sa = (spa_error_entry_t *)a;
807 spa_error_entry_t *sb = (spa_error_entry_t *)b;
810 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
811 sizeof (zbookmark_phys_t));
822 * Utility function which retrieves copies of the current logs and
823 * re-initializes them in the process.
826 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
828 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
830 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
831 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
833 avl_create(&spa->spa_errlist_scrub,
834 spa_error_entry_compare, sizeof (spa_error_entry_t),
835 offsetof(spa_error_entry_t, se_avl));
836 avl_create(&spa->spa_errlist_last,
837 spa_error_entry_compare, sizeof (spa_error_entry_t),
838 offsetof(spa_error_entry_t, se_avl));
842 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
844 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
845 enum zti_modes mode = ztip->zti_mode;
846 uint_t value = ztip->zti_value;
847 uint_t count = ztip->zti_count;
848 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
851 boolean_t batch = B_FALSE;
853 if (mode == ZTI_MODE_NULL) {
855 tqs->stqs_taskq = NULL;
859 ASSERT3U(count, >, 0);
861 tqs->stqs_count = count;
862 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
866 ASSERT3U(value, >=, 1);
867 value = MAX(value, 1);
872 flags |= TASKQ_THREADS_CPU_PCT;
873 value = zio_taskq_batch_pct;
877 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
879 zio_type_name[t], zio_taskq_types[q], mode, value);
883 for (uint_t i = 0; i < count; i++) {
887 (void) snprintf(name, sizeof (name), "%s_%s_%u",
888 zio_type_name[t], zio_taskq_types[q], i);
890 (void) snprintf(name, sizeof (name), "%s_%s",
891 zio_type_name[t], zio_taskq_types[q]);
895 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
897 flags |= TASKQ_DC_BATCH;
899 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
900 spa->spa_proc, zio_taskq_basedc, flags);
903 pri_t pri = maxclsyspri;
905 * The write issue taskq can be extremely CPU
906 * intensive. Run it at slightly lower priority
907 * than the other taskqs.
909 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
912 tq = taskq_create_proc(name, value, pri, 50,
913 INT_MAX, spa->spa_proc, flags);
918 tqs->stqs_taskq[i] = tq;
923 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
925 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
927 if (tqs->stqs_taskq == NULL) {
928 ASSERT0(tqs->stqs_count);
932 for (uint_t i = 0; i < tqs->stqs_count; i++) {
933 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
934 taskq_destroy(tqs->stqs_taskq[i]);
937 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
938 tqs->stqs_taskq = NULL;
942 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
943 * Note that a type may have multiple discrete taskqs to avoid lock contention
944 * on the taskq itself. In that case we choose which taskq at random by using
945 * the low bits of gethrtime().
948 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
949 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
951 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
954 ASSERT3P(tqs->stqs_taskq, !=, NULL);
955 ASSERT3U(tqs->stqs_count, !=, 0);
957 if (tqs->stqs_count == 1) {
958 tq = tqs->stqs_taskq[0];
961 tq = tqs->stqs_taskq[cpu_ticks() % tqs->stqs_count];
963 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
967 taskq_dispatch_ent(tq, func, arg, flags, ent);
971 spa_create_zio_taskqs(spa_t *spa)
973 for (int t = 0; t < ZIO_TYPES; t++) {
974 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
975 spa_taskqs_init(spa, t, q);
983 spa_thread(void *arg)
988 user_t *pu = PTOU(curproc);
990 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
993 ASSERT(curproc != &p0);
994 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
995 "zpool-%s", spa->spa_name);
996 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
999 /* bind this thread to the requested psrset */
1000 if (zio_taskq_psrset_bind != PS_NONE) {
1002 mutex_enter(&cpu_lock);
1003 mutex_enter(&pidlock);
1004 mutex_enter(&curproc->p_lock);
1006 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1007 0, NULL, NULL) == 0) {
1008 curthread->t_bind_pset = zio_taskq_psrset_bind;
1011 "Couldn't bind process for zfs pool \"%s\" to "
1012 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1015 mutex_exit(&curproc->p_lock);
1016 mutex_exit(&pidlock);
1017 mutex_exit(&cpu_lock);
1023 if (zio_taskq_sysdc) {
1024 sysdc_thread_enter(curthread, 100, 0);
1028 spa->spa_proc = curproc;
1029 spa->spa_did = curthread->t_did;
1031 spa_create_zio_taskqs(spa);
1033 mutex_enter(&spa->spa_proc_lock);
1034 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1036 spa->spa_proc_state = SPA_PROC_ACTIVE;
1037 cv_broadcast(&spa->spa_proc_cv);
1039 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1040 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1041 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1042 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1044 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1045 spa->spa_proc_state = SPA_PROC_GONE;
1046 spa->spa_proc = &p0;
1047 cv_broadcast(&spa->spa_proc_cv);
1048 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1050 mutex_enter(&curproc->p_lock);
1053 #endif /* SPA_PROCESS */
1057 * Activate an uninitialized pool.
1060 spa_activate(spa_t *spa, int mode)
1062 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1064 spa->spa_state = POOL_STATE_ACTIVE;
1065 spa->spa_mode = mode;
1067 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1068 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1070 /* Try to create a covering process */
1071 mutex_enter(&spa->spa_proc_lock);
1072 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1073 ASSERT(spa->spa_proc == &p0);
1077 /* Only create a process if we're going to be around a while. */
1078 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1079 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1081 spa->spa_proc_state = SPA_PROC_CREATED;
1082 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1083 cv_wait(&spa->spa_proc_cv,
1084 &spa->spa_proc_lock);
1086 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1087 ASSERT(spa->spa_proc != &p0);
1088 ASSERT(spa->spa_did != 0);
1092 "Couldn't create process for zfs pool \"%s\"\n",
1097 #endif /* SPA_PROCESS */
1098 mutex_exit(&spa->spa_proc_lock);
1100 /* If we didn't create a process, we need to create our taskqs. */
1101 ASSERT(spa->spa_proc == &p0);
1102 if (spa->spa_proc == &p0) {
1103 spa_create_zio_taskqs(spa);
1107 * Start TRIM thread.
1109 trim_thread_create(spa);
1111 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1112 offsetof(vdev_t, vdev_config_dirty_node));
1113 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1114 offsetof(vdev_t, vdev_state_dirty_node));
1116 txg_list_create(&spa->spa_vdev_txg_list,
1117 offsetof(struct vdev, vdev_txg_node));
1119 avl_create(&spa->spa_errlist_scrub,
1120 spa_error_entry_compare, sizeof (spa_error_entry_t),
1121 offsetof(spa_error_entry_t, se_avl));
1122 avl_create(&spa->spa_errlist_last,
1123 spa_error_entry_compare, sizeof (spa_error_entry_t),
1124 offsetof(spa_error_entry_t, se_avl));
1128 * Opposite of spa_activate().
1131 spa_deactivate(spa_t *spa)
1133 ASSERT(spa->spa_sync_on == B_FALSE);
1134 ASSERT(spa->spa_dsl_pool == NULL);
1135 ASSERT(spa->spa_root_vdev == NULL);
1136 ASSERT(spa->spa_async_zio_root == NULL);
1137 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1140 * Stop TRIM thread in case spa_unload() wasn't called directly
1141 * before spa_deactivate().
1143 trim_thread_destroy(spa);
1145 txg_list_destroy(&spa->spa_vdev_txg_list);
1147 list_destroy(&spa->spa_config_dirty_list);
1148 list_destroy(&spa->spa_state_dirty_list);
1150 for (int t = 0; t < ZIO_TYPES; t++) {
1151 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1152 spa_taskqs_fini(spa, t, q);
1156 metaslab_class_destroy(spa->spa_normal_class);
1157 spa->spa_normal_class = NULL;
1159 metaslab_class_destroy(spa->spa_log_class);
1160 spa->spa_log_class = NULL;
1163 * If this was part of an import or the open otherwise failed, we may
1164 * still have errors left in the queues. Empty them just in case.
1166 spa_errlog_drain(spa);
1168 avl_destroy(&spa->spa_errlist_scrub);
1169 avl_destroy(&spa->spa_errlist_last);
1171 spa->spa_state = POOL_STATE_UNINITIALIZED;
1173 mutex_enter(&spa->spa_proc_lock);
1174 if (spa->spa_proc_state != SPA_PROC_NONE) {
1175 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1176 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1177 cv_broadcast(&spa->spa_proc_cv);
1178 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1179 ASSERT(spa->spa_proc != &p0);
1180 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1182 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1183 spa->spa_proc_state = SPA_PROC_NONE;
1185 ASSERT(spa->spa_proc == &p0);
1186 mutex_exit(&spa->spa_proc_lock);
1190 * We want to make sure spa_thread() has actually exited the ZFS
1191 * module, so that the module can't be unloaded out from underneath
1194 if (spa->spa_did != 0) {
1195 thread_join(spa->spa_did);
1198 #endif /* SPA_PROCESS */
1202 * Verify a pool configuration, and construct the vdev tree appropriately. This
1203 * will create all the necessary vdevs in the appropriate layout, with each vdev
1204 * in the CLOSED state. This will prep the pool before open/creation/import.
1205 * All vdev validation is done by the vdev_alloc() routine.
1208 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1209 uint_t id, int atype)
1215 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1218 if ((*vdp)->vdev_ops->vdev_op_leaf)
1221 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1224 if (error == ENOENT)
1230 return (SET_ERROR(EINVAL));
1233 for (int c = 0; c < children; c++) {
1235 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1243 ASSERT(*vdp != NULL);
1249 * Opposite of spa_load().
1252 spa_unload(spa_t *spa)
1256 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1261 trim_thread_destroy(spa);
1266 spa_async_suspend(spa);
1271 if (spa->spa_sync_on) {
1272 txg_sync_stop(spa->spa_dsl_pool);
1273 spa->spa_sync_on = B_FALSE;
1277 * Wait for any outstanding async I/O to complete.
1279 if (spa->spa_async_zio_root != NULL) {
1280 for (int i = 0; i < max_ncpus; i++)
1281 (void) zio_wait(spa->spa_async_zio_root[i]);
1282 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
1283 spa->spa_async_zio_root = NULL;
1286 bpobj_close(&spa->spa_deferred_bpobj);
1288 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1293 if (spa->spa_root_vdev)
1294 vdev_free(spa->spa_root_vdev);
1295 ASSERT(spa->spa_root_vdev == NULL);
1298 * Close the dsl pool.
1300 if (spa->spa_dsl_pool) {
1301 dsl_pool_close(spa->spa_dsl_pool);
1302 spa->spa_dsl_pool = NULL;
1303 spa->spa_meta_objset = NULL;
1310 * Drop and purge level 2 cache
1312 spa_l2cache_drop(spa);
1314 for (i = 0; i < spa->spa_spares.sav_count; i++)
1315 vdev_free(spa->spa_spares.sav_vdevs[i]);
1316 if (spa->spa_spares.sav_vdevs) {
1317 kmem_free(spa->spa_spares.sav_vdevs,
1318 spa->spa_spares.sav_count * sizeof (void *));
1319 spa->spa_spares.sav_vdevs = NULL;
1321 if (spa->spa_spares.sav_config) {
1322 nvlist_free(spa->spa_spares.sav_config);
1323 spa->spa_spares.sav_config = NULL;
1325 spa->spa_spares.sav_count = 0;
1327 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1328 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1329 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1331 if (spa->spa_l2cache.sav_vdevs) {
1332 kmem_free(spa->spa_l2cache.sav_vdevs,
1333 spa->spa_l2cache.sav_count * sizeof (void *));
1334 spa->spa_l2cache.sav_vdevs = NULL;
1336 if (spa->spa_l2cache.sav_config) {
1337 nvlist_free(spa->spa_l2cache.sav_config);
1338 spa->spa_l2cache.sav_config = NULL;
1340 spa->spa_l2cache.sav_count = 0;
1342 spa->spa_async_suspended = 0;
1344 if (spa->spa_comment != NULL) {
1345 spa_strfree(spa->spa_comment);
1346 spa->spa_comment = NULL;
1349 spa_config_exit(spa, SCL_ALL, FTAG);
1353 * Load (or re-load) the current list of vdevs describing the active spares for
1354 * this pool. When this is called, we have some form of basic information in
1355 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1356 * then re-generate a more complete list including status information.
1359 spa_load_spares(spa_t *spa)
1366 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1369 * First, close and free any existing spare vdevs.
1371 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1372 vd = spa->spa_spares.sav_vdevs[i];
1374 /* Undo the call to spa_activate() below */
1375 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1376 B_FALSE)) != NULL && tvd->vdev_isspare)
1377 spa_spare_remove(tvd);
1382 if (spa->spa_spares.sav_vdevs)
1383 kmem_free(spa->spa_spares.sav_vdevs,
1384 spa->spa_spares.sav_count * sizeof (void *));
1386 if (spa->spa_spares.sav_config == NULL)
1389 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1390 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1392 spa->spa_spares.sav_count = (int)nspares;
1393 spa->spa_spares.sav_vdevs = NULL;
1399 * Construct the array of vdevs, opening them to get status in the
1400 * process. For each spare, there is potentially two different vdev_t
1401 * structures associated with it: one in the list of spares (used only
1402 * for basic validation purposes) and one in the active vdev
1403 * configuration (if it's spared in). During this phase we open and
1404 * validate each vdev on the spare list. If the vdev also exists in the
1405 * active configuration, then we also mark this vdev as an active spare.
1407 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1409 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1410 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1411 VDEV_ALLOC_SPARE) == 0);
1414 spa->spa_spares.sav_vdevs[i] = vd;
1416 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1417 B_FALSE)) != NULL) {
1418 if (!tvd->vdev_isspare)
1422 * We only mark the spare active if we were successfully
1423 * able to load the vdev. Otherwise, importing a pool
1424 * with a bad active spare would result in strange
1425 * behavior, because multiple pool would think the spare
1426 * is actively in use.
1428 * There is a vulnerability here to an equally bizarre
1429 * circumstance, where a dead active spare is later
1430 * brought back to life (onlined or otherwise). Given
1431 * the rarity of this scenario, and the extra complexity
1432 * it adds, we ignore the possibility.
1434 if (!vdev_is_dead(tvd))
1435 spa_spare_activate(tvd);
1439 vd->vdev_aux = &spa->spa_spares;
1441 if (vdev_open(vd) != 0)
1444 if (vdev_validate_aux(vd) == 0)
1449 * Recompute the stashed list of spares, with status information
1452 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1453 DATA_TYPE_NVLIST_ARRAY) == 0);
1455 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1457 for (i = 0; i < spa->spa_spares.sav_count; i++)
1458 spares[i] = vdev_config_generate(spa,
1459 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1460 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1461 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1462 for (i = 0; i < spa->spa_spares.sav_count; i++)
1463 nvlist_free(spares[i]);
1464 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1468 * Load (or re-load) the current list of vdevs describing the active l2cache for
1469 * this pool. When this is called, we have some form of basic information in
1470 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1471 * then re-generate a more complete list including status information.
1472 * Devices which are already active have their details maintained, and are
1476 spa_load_l2cache(spa_t *spa)
1480 int i, j, oldnvdevs;
1482 vdev_t *vd, **oldvdevs, **newvdevs;
1483 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1485 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1487 if (sav->sav_config != NULL) {
1488 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1489 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1490 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1496 oldvdevs = sav->sav_vdevs;
1497 oldnvdevs = sav->sav_count;
1498 sav->sav_vdevs = NULL;
1502 * Process new nvlist of vdevs.
1504 for (i = 0; i < nl2cache; i++) {
1505 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1509 for (j = 0; j < oldnvdevs; j++) {
1511 if (vd != NULL && guid == vd->vdev_guid) {
1513 * Retain previous vdev for add/remove ops.
1521 if (newvdevs[i] == NULL) {
1525 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1526 VDEV_ALLOC_L2CACHE) == 0);
1531 * Commit this vdev as an l2cache device,
1532 * even if it fails to open.
1534 spa_l2cache_add(vd);
1539 spa_l2cache_activate(vd);
1541 if (vdev_open(vd) != 0)
1544 (void) vdev_validate_aux(vd);
1546 if (!vdev_is_dead(vd))
1547 l2arc_add_vdev(spa, vd);
1552 * Purge vdevs that were dropped
1554 for (i = 0; i < oldnvdevs; i++) {
1559 ASSERT(vd->vdev_isl2cache);
1561 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1562 pool != 0ULL && l2arc_vdev_present(vd))
1563 l2arc_remove_vdev(vd);
1564 vdev_clear_stats(vd);
1570 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1572 if (sav->sav_config == NULL)
1575 sav->sav_vdevs = newvdevs;
1576 sav->sav_count = (int)nl2cache;
1579 * Recompute the stashed list of l2cache devices, with status
1580 * information this time.
1582 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1583 DATA_TYPE_NVLIST_ARRAY) == 0);
1585 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1586 for (i = 0; i < sav->sav_count; i++)
1587 l2cache[i] = vdev_config_generate(spa,
1588 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1589 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1590 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1592 for (i = 0; i < sav->sav_count; i++)
1593 nvlist_free(l2cache[i]);
1595 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1599 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1602 char *packed = NULL;
1607 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1610 nvsize = *(uint64_t *)db->db_data;
1611 dmu_buf_rele(db, FTAG);
1613 packed = kmem_alloc(nvsize, KM_SLEEP);
1614 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1617 error = nvlist_unpack(packed, nvsize, value, 0);
1618 kmem_free(packed, nvsize);
1624 * Checks to see if the given vdev could not be opened, in which case we post a
1625 * sysevent to notify the autoreplace code that the device has been removed.
1628 spa_check_removed(vdev_t *vd)
1630 for (int c = 0; c < vd->vdev_children; c++)
1631 spa_check_removed(vd->vdev_child[c]);
1633 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1635 zfs_post_autoreplace(vd->vdev_spa, vd);
1636 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1641 * Validate the current config against the MOS config
1644 spa_config_valid(spa_t *spa, nvlist_t *config)
1646 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1649 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1651 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1652 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1654 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1657 * If we're doing a normal import, then build up any additional
1658 * diagnostic information about missing devices in this config.
1659 * We'll pass this up to the user for further processing.
1661 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1662 nvlist_t **child, *nv;
1665 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1667 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1669 for (int c = 0; c < rvd->vdev_children; c++) {
1670 vdev_t *tvd = rvd->vdev_child[c];
1671 vdev_t *mtvd = mrvd->vdev_child[c];
1673 if (tvd->vdev_ops == &vdev_missing_ops &&
1674 mtvd->vdev_ops != &vdev_missing_ops &&
1676 child[idx++] = vdev_config_generate(spa, mtvd,
1681 VERIFY(nvlist_add_nvlist_array(nv,
1682 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1683 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1684 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1686 for (int i = 0; i < idx; i++)
1687 nvlist_free(child[i]);
1690 kmem_free(child, rvd->vdev_children * sizeof (char **));
1694 * Compare the root vdev tree with the information we have
1695 * from the MOS config (mrvd). Check each top-level vdev
1696 * with the corresponding MOS config top-level (mtvd).
1698 for (int c = 0; c < rvd->vdev_children; c++) {
1699 vdev_t *tvd = rvd->vdev_child[c];
1700 vdev_t *mtvd = mrvd->vdev_child[c];
1703 * Resolve any "missing" vdevs in the current configuration.
1704 * If we find that the MOS config has more accurate information
1705 * about the top-level vdev then use that vdev instead.
1707 if (tvd->vdev_ops == &vdev_missing_ops &&
1708 mtvd->vdev_ops != &vdev_missing_ops) {
1710 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1714 * Device specific actions.
1716 if (mtvd->vdev_islog) {
1717 spa_set_log_state(spa, SPA_LOG_CLEAR);
1720 * XXX - once we have 'readonly' pool
1721 * support we should be able to handle
1722 * missing data devices by transitioning
1723 * the pool to readonly.
1729 * Swap the missing vdev with the data we were
1730 * able to obtain from the MOS config.
1732 vdev_remove_child(rvd, tvd);
1733 vdev_remove_child(mrvd, mtvd);
1735 vdev_add_child(rvd, mtvd);
1736 vdev_add_child(mrvd, tvd);
1738 spa_config_exit(spa, SCL_ALL, FTAG);
1740 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1743 } else if (mtvd->vdev_islog) {
1745 * Load the slog device's state from the MOS config
1746 * since it's possible that the label does not
1747 * contain the most up-to-date information.
1749 vdev_load_log_state(tvd, mtvd);
1754 spa_config_exit(spa, SCL_ALL, FTAG);
1757 * Ensure we were able to validate the config.
1759 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1763 * Check for missing log devices
1766 spa_check_logs(spa_t *spa)
1768 boolean_t rv = B_FALSE;
1770 switch (spa->spa_log_state) {
1771 case SPA_LOG_MISSING:
1772 /* need to recheck in case slog has been restored */
1773 case SPA_LOG_UNKNOWN:
1774 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1775 NULL, DS_FIND_CHILDREN) != 0);
1777 spa_set_log_state(spa, SPA_LOG_MISSING);
1784 spa_passivate_log(spa_t *spa)
1786 vdev_t *rvd = spa->spa_root_vdev;
1787 boolean_t slog_found = B_FALSE;
1789 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1791 if (!spa_has_slogs(spa))
1794 for (int c = 0; c < rvd->vdev_children; c++) {
1795 vdev_t *tvd = rvd->vdev_child[c];
1796 metaslab_group_t *mg = tvd->vdev_mg;
1798 if (tvd->vdev_islog) {
1799 metaslab_group_passivate(mg);
1800 slog_found = B_TRUE;
1804 return (slog_found);
1808 spa_activate_log(spa_t *spa)
1810 vdev_t *rvd = spa->spa_root_vdev;
1812 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1814 for (int c = 0; c < rvd->vdev_children; c++) {
1815 vdev_t *tvd = rvd->vdev_child[c];
1816 metaslab_group_t *mg = tvd->vdev_mg;
1818 if (tvd->vdev_islog)
1819 metaslab_group_activate(mg);
1824 spa_offline_log(spa_t *spa)
1828 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1829 NULL, DS_FIND_CHILDREN);
1832 * We successfully offlined the log device, sync out the
1833 * current txg so that the "stubby" block can be removed
1836 txg_wait_synced(spa->spa_dsl_pool, 0);
1842 spa_aux_check_removed(spa_aux_vdev_t *sav)
1846 for (i = 0; i < sav->sav_count; i++)
1847 spa_check_removed(sav->sav_vdevs[i]);
1851 spa_claim_notify(zio_t *zio)
1853 spa_t *spa = zio->io_spa;
1858 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1859 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1860 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1861 mutex_exit(&spa->spa_props_lock);
1864 typedef struct spa_load_error {
1865 uint64_t sle_meta_count;
1866 uint64_t sle_data_count;
1870 spa_load_verify_done(zio_t *zio)
1872 blkptr_t *bp = zio->io_bp;
1873 spa_load_error_t *sle = zio->io_private;
1874 dmu_object_type_t type = BP_GET_TYPE(bp);
1875 int error = zio->io_error;
1876 spa_t *spa = zio->io_spa;
1879 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1880 type != DMU_OT_INTENT_LOG)
1881 atomic_inc_64(&sle->sle_meta_count);
1883 atomic_inc_64(&sle->sle_data_count);
1885 zio_data_buf_free(zio->io_data, zio->io_size);
1887 mutex_enter(&spa->spa_scrub_lock);
1888 spa->spa_scrub_inflight--;
1889 cv_broadcast(&spa->spa_scrub_io_cv);
1890 mutex_exit(&spa->spa_scrub_lock);
1894 * Maximum number of concurrent scrub i/os to create while verifying
1895 * a pool while importing it.
1897 int spa_load_verify_maxinflight = 10000;
1898 boolean_t spa_load_verify_metadata = B_TRUE;
1899 boolean_t spa_load_verify_data = B_TRUE;
1901 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1902 &spa_load_verify_maxinflight, 0,
1903 "Maximum number of concurrent scrub I/Os to create while verifying a "
1904 "pool while importing it");
1906 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1907 &spa_load_verify_metadata, 0,
1908 "Check metadata on import?");
1910 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1911 &spa_load_verify_data, 0,
1912 "Check user data on import?");
1916 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1917 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1919 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1922 * Note: normally this routine will not be called if
1923 * spa_load_verify_metadata is not set. However, it may be useful
1924 * to manually set the flag after the traversal has begun.
1926 if (!spa_load_verify_metadata)
1928 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1932 size_t size = BP_GET_PSIZE(bp);
1933 void *data = zio_data_buf_alloc(size);
1935 mutex_enter(&spa->spa_scrub_lock);
1936 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1937 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1938 spa->spa_scrub_inflight++;
1939 mutex_exit(&spa->spa_scrub_lock);
1941 zio_nowait(zio_read(rio, spa, bp, data, size,
1942 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1943 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1944 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1949 spa_load_verify(spa_t *spa)
1952 spa_load_error_t sle = { 0 };
1953 zpool_rewind_policy_t policy;
1954 boolean_t verify_ok = B_FALSE;
1957 zpool_get_rewind_policy(spa->spa_config, &policy);
1959 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1962 rio = zio_root(spa, NULL, &sle,
1963 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1965 if (spa_load_verify_metadata) {
1966 error = traverse_pool(spa, spa->spa_verify_min_txg,
1967 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
1968 spa_load_verify_cb, rio);
1971 (void) zio_wait(rio);
1973 spa->spa_load_meta_errors = sle.sle_meta_count;
1974 spa->spa_load_data_errors = sle.sle_data_count;
1976 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1977 sle.sle_data_count <= policy.zrp_maxdata) {
1981 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1982 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1984 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1985 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1986 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1987 VERIFY(nvlist_add_int64(spa->spa_load_info,
1988 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1989 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1990 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1992 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1996 if (error != ENXIO && error != EIO)
1997 error = SET_ERROR(EIO);
2001 return (verify_ok ? 0 : EIO);
2005 * Find a value in the pool props object.
2008 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2010 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2011 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2015 * Find a value in the pool directory object.
2018 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2020 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2021 name, sizeof (uint64_t), 1, val));
2025 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2027 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2032 * Fix up config after a partly-completed split. This is done with the
2033 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2034 * pool have that entry in their config, but only the splitting one contains
2035 * a list of all the guids of the vdevs that are being split off.
2037 * This function determines what to do with that list: either rejoin
2038 * all the disks to the pool, or complete the splitting process. To attempt
2039 * the rejoin, each disk that is offlined is marked online again, and
2040 * we do a reopen() call. If the vdev label for every disk that was
2041 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2042 * then we call vdev_split() on each disk, and complete the split.
2044 * Otherwise we leave the config alone, with all the vdevs in place in
2045 * the original pool.
2048 spa_try_repair(spa_t *spa, nvlist_t *config)
2055 boolean_t attempt_reopen;
2057 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2060 /* check that the config is complete */
2061 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2062 &glist, &gcount) != 0)
2065 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2067 /* attempt to online all the vdevs & validate */
2068 attempt_reopen = B_TRUE;
2069 for (i = 0; i < gcount; i++) {
2070 if (glist[i] == 0) /* vdev is hole */
2073 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2074 if (vd[i] == NULL) {
2076 * Don't bother attempting to reopen the disks;
2077 * just do the split.
2079 attempt_reopen = B_FALSE;
2081 /* attempt to re-online it */
2082 vd[i]->vdev_offline = B_FALSE;
2086 if (attempt_reopen) {
2087 vdev_reopen(spa->spa_root_vdev);
2089 /* check each device to see what state it's in */
2090 for (extracted = 0, i = 0; i < gcount; i++) {
2091 if (vd[i] != NULL &&
2092 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2099 * If every disk has been moved to the new pool, or if we never
2100 * even attempted to look at them, then we split them off for
2103 if (!attempt_reopen || gcount == extracted) {
2104 for (i = 0; i < gcount; i++)
2107 vdev_reopen(spa->spa_root_vdev);
2110 kmem_free(vd, gcount * sizeof (vdev_t *));
2114 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2115 boolean_t mosconfig)
2117 nvlist_t *config = spa->spa_config;
2118 char *ereport = FM_EREPORT_ZFS_POOL;
2124 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2125 return (SET_ERROR(EINVAL));
2127 ASSERT(spa->spa_comment == NULL);
2128 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2129 spa->spa_comment = spa_strdup(comment);
2132 * Versioning wasn't explicitly added to the label until later, so if
2133 * it's not present treat it as the initial version.
2135 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2136 &spa->spa_ubsync.ub_version) != 0)
2137 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2139 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2140 &spa->spa_config_txg);
2142 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2143 spa_guid_exists(pool_guid, 0)) {
2144 error = SET_ERROR(EEXIST);
2146 spa->spa_config_guid = pool_guid;
2148 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2150 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2154 nvlist_free(spa->spa_load_info);
2155 spa->spa_load_info = fnvlist_alloc();
2157 gethrestime(&spa->spa_loaded_ts);
2158 error = spa_load_impl(spa, pool_guid, config, state, type,
2159 mosconfig, &ereport);
2162 spa->spa_minref = refcount_count(&spa->spa_refcount);
2164 if (error != EEXIST) {
2165 spa->spa_loaded_ts.tv_sec = 0;
2166 spa->spa_loaded_ts.tv_nsec = 0;
2168 if (error != EBADF) {
2169 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2172 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2179 * Load an existing storage pool, using the pool's builtin spa_config as a
2180 * source of configuration information.
2183 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2184 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2188 nvlist_t *nvroot = NULL;
2191 uberblock_t *ub = &spa->spa_uberblock;
2192 uint64_t children, config_cache_txg = spa->spa_config_txg;
2193 int orig_mode = spa->spa_mode;
2196 boolean_t missing_feat_write = B_FALSE;
2199 * If this is an untrusted config, access the pool in read-only mode.
2200 * This prevents things like resilvering recently removed devices.
2203 spa->spa_mode = FREAD;
2205 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2207 spa->spa_load_state = state;
2209 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2210 return (SET_ERROR(EINVAL));
2212 parse = (type == SPA_IMPORT_EXISTING ?
2213 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2216 * Create "The Godfather" zio to hold all async IOs
2218 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2220 for (int i = 0; i < max_ncpus; i++) {
2221 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2222 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2223 ZIO_FLAG_GODFATHER);
2227 * Parse the configuration into a vdev tree. We explicitly set the
2228 * value that will be returned by spa_version() since parsing the
2229 * configuration requires knowing the version number.
2231 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2232 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2233 spa_config_exit(spa, SCL_ALL, FTAG);
2238 ASSERT(spa->spa_root_vdev == rvd);
2240 if (type != SPA_IMPORT_ASSEMBLE) {
2241 ASSERT(spa_guid(spa) == pool_guid);
2245 * Try to open all vdevs, loading each label in the process.
2247 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2248 error = vdev_open(rvd);
2249 spa_config_exit(spa, SCL_ALL, FTAG);
2254 * We need to validate the vdev labels against the configuration that
2255 * we have in hand, which is dependent on the setting of mosconfig. If
2256 * mosconfig is true then we're validating the vdev labels based on
2257 * that config. Otherwise, we're validating against the cached config
2258 * (zpool.cache) that was read when we loaded the zfs module, and then
2259 * later we will recursively call spa_load() and validate against
2262 * If we're assembling a new pool that's been split off from an
2263 * existing pool, the labels haven't yet been updated so we skip
2264 * validation for now.
2266 if (type != SPA_IMPORT_ASSEMBLE) {
2267 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2268 error = vdev_validate(rvd, mosconfig);
2269 spa_config_exit(spa, SCL_ALL, FTAG);
2274 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2275 return (SET_ERROR(ENXIO));
2279 * Find the best uberblock.
2281 vdev_uberblock_load(rvd, ub, &label);
2284 * If we weren't able to find a single valid uberblock, return failure.
2286 if (ub->ub_txg == 0) {
2288 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2292 * If the pool has an unsupported version we can't open it.
2294 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2296 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2299 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2303 * If we weren't able to find what's necessary for reading the
2304 * MOS in the label, return failure.
2306 if (label == NULL || nvlist_lookup_nvlist(label,
2307 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2309 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2314 * Update our in-core representation with the definitive values
2317 nvlist_free(spa->spa_label_features);
2318 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2324 * Look through entries in the label nvlist's features_for_read. If
2325 * there is a feature listed there which we don't understand then we
2326 * cannot open a pool.
2328 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2329 nvlist_t *unsup_feat;
2331 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2334 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2336 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2337 if (!zfeature_is_supported(nvpair_name(nvp))) {
2338 VERIFY(nvlist_add_string(unsup_feat,
2339 nvpair_name(nvp), "") == 0);
2343 if (!nvlist_empty(unsup_feat)) {
2344 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2345 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2346 nvlist_free(unsup_feat);
2347 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2351 nvlist_free(unsup_feat);
2355 * If the vdev guid sum doesn't match the uberblock, we have an
2356 * incomplete configuration. We first check to see if the pool
2357 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2358 * If it is, defer the vdev_guid_sum check till later so we
2359 * can handle missing vdevs.
2361 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2362 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2363 rvd->vdev_guid_sum != ub->ub_guid_sum)
2364 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2366 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2367 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2368 spa_try_repair(spa, config);
2369 spa_config_exit(spa, SCL_ALL, FTAG);
2370 nvlist_free(spa->spa_config_splitting);
2371 spa->spa_config_splitting = NULL;
2375 * Initialize internal SPA structures.
2377 spa->spa_state = POOL_STATE_ACTIVE;
2378 spa->spa_ubsync = spa->spa_uberblock;
2379 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2380 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2381 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2382 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2383 spa->spa_claim_max_txg = spa->spa_first_txg;
2384 spa->spa_prev_software_version = ub->ub_software_version;
2386 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2388 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2389 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2391 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2392 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2394 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2395 boolean_t missing_feat_read = B_FALSE;
2396 nvlist_t *unsup_feat, *enabled_feat;
2398 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2399 &spa->spa_feat_for_read_obj) != 0) {
2400 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2403 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2404 &spa->spa_feat_for_write_obj) != 0) {
2405 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2408 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2409 &spa->spa_feat_desc_obj) != 0) {
2410 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2413 enabled_feat = fnvlist_alloc();
2414 unsup_feat = fnvlist_alloc();
2416 if (!spa_features_check(spa, B_FALSE,
2417 unsup_feat, enabled_feat))
2418 missing_feat_read = B_TRUE;
2420 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2421 if (!spa_features_check(spa, B_TRUE,
2422 unsup_feat, enabled_feat)) {
2423 missing_feat_write = B_TRUE;
2427 fnvlist_add_nvlist(spa->spa_load_info,
2428 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2430 if (!nvlist_empty(unsup_feat)) {
2431 fnvlist_add_nvlist(spa->spa_load_info,
2432 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2435 fnvlist_free(enabled_feat);
2436 fnvlist_free(unsup_feat);
2438 if (!missing_feat_read) {
2439 fnvlist_add_boolean(spa->spa_load_info,
2440 ZPOOL_CONFIG_CAN_RDONLY);
2444 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2445 * twofold: to determine whether the pool is available for
2446 * import in read-write mode and (if it is not) whether the
2447 * pool is available for import in read-only mode. If the pool
2448 * is available for import in read-write mode, it is displayed
2449 * as available in userland; if it is not available for import
2450 * in read-only mode, it is displayed as unavailable in
2451 * userland. If the pool is available for import in read-only
2452 * mode but not read-write mode, it is displayed as unavailable
2453 * in userland with a special note that the pool is actually
2454 * available for open in read-only mode.
2456 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2457 * missing a feature for write, we must first determine whether
2458 * the pool can be opened read-only before returning to
2459 * userland in order to know whether to display the
2460 * abovementioned note.
2462 if (missing_feat_read || (missing_feat_write &&
2463 spa_writeable(spa))) {
2464 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2469 * Load refcounts for ZFS features from disk into an in-memory
2470 * cache during SPA initialization.
2472 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2475 error = feature_get_refcount_from_disk(spa,
2476 &spa_feature_table[i], &refcount);
2478 spa->spa_feat_refcount_cache[i] = refcount;
2479 } else if (error == ENOTSUP) {
2480 spa->spa_feat_refcount_cache[i] =
2481 SPA_FEATURE_DISABLED;
2483 return (spa_vdev_err(rvd,
2484 VDEV_AUX_CORRUPT_DATA, EIO));
2489 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2490 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2491 &spa->spa_feat_enabled_txg_obj) != 0)
2492 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2495 spa->spa_is_initializing = B_TRUE;
2496 error = dsl_pool_open(spa->spa_dsl_pool);
2497 spa->spa_is_initializing = B_FALSE;
2499 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2503 nvlist_t *policy = NULL, *nvconfig;
2505 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2506 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2508 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2509 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2511 unsigned long myhostid = 0;
2513 VERIFY(nvlist_lookup_string(nvconfig,
2514 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2517 myhostid = zone_get_hostid(NULL);
2520 * We're emulating the system's hostid in userland, so
2521 * we can't use zone_get_hostid().
2523 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2524 #endif /* _KERNEL */
2525 if (check_hostid && hostid != 0 && myhostid != 0 &&
2526 hostid != myhostid) {
2527 nvlist_free(nvconfig);
2528 cmn_err(CE_WARN, "pool '%s' could not be "
2529 "loaded as it was last accessed by "
2530 "another system (host: %s hostid: 0x%lx). "
2531 "See: http://illumos.org/msg/ZFS-8000-EY",
2532 spa_name(spa), hostname,
2533 (unsigned long)hostid);
2534 return (SET_ERROR(EBADF));
2537 if (nvlist_lookup_nvlist(spa->spa_config,
2538 ZPOOL_REWIND_POLICY, &policy) == 0)
2539 VERIFY(nvlist_add_nvlist(nvconfig,
2540 ZPOOL_REWIND_POLICY, policy) == 0);
2542 spa_config_set(spa, nvconfig);
2544 spa_deactivate(spa);
2545 spa_activate(spa, orig_mode);
2547 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2550 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2551 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2552 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2554 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2557 * Load the bit that tells us to use the new accounting function
2558 * (raid-z deflation). If we have an older pool, this will not
2561 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2562 if (error != 0 && error != ENOENT)
2563 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2565 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2566 &spa->spa_creation_version);
2567 if (error != 0 && error != ENOENT)
2568 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2571 * Load the persistent error log. If we have an older pool, this will
2574 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2575 if (error != 0 && error != ENOENT)
2576 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2578 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2579 &spa->spa_errlog_scrub);
2580 if (error != 0 && error != ENOENT)
2581 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2584 * Load the history object. If we have an older pool, this
2585 * will not be present.
2587 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2588 if (error != 0 && error != ENOENT)
2589 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2592 * If we're assembling the pool from the split-off vdevs of
2593 * an existing pool, we don't want to attach the spares & cache
2598 * Load any hot spares for this pool.
2600 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2601 if (error != 0 && error != ENOENT)
2602 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2603 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2604 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2605 if (load_nvlist(spa, spa->spa_spares.sav_object,
2606 &spa->spa_spares.sav_config) != 0)
2607 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2609 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2610 spa_load_spares(spa);
2611 spa_config_exit(spa, SCL_ALL, FTAG);
2612 } else if (error == 0) {
2613 spa->spa_spares.sav_sync = B_TRUE;
2617 * Load any level 2 ARC devices for this pool.
2619 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2620 &spa->spa_l2cache.sav_object);
2621 if (error != 0 && error != ENOENT)
2622 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2623 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2624 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2625 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2626 &spa->spa_l2cache.sav_config) != 0)
2627 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2629 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2630 spa_load_l2cache(spa);
2631 spa_config_exit(spa, SCL_ALL, FTAG);
2632 } else if (error == 0) {
2633 spa->spa_l2cache.sav_sync = B_TRUE;
2636 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2638 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2639 if (error && error != ENOENT)
2640 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2643 uint64_t autoreplace;
2645 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2646 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2647 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2648 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2649 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2650 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2651 &spa->spa_dedup_ditto);
2653 spa->spa_autoreplace = (autoreplace != 0);
2657 * If the 'autoreplace' property is set, then post a resource notifying
2658 * the ZFS DE that it should not issue any faults for unopenable
2659 * devices. We also iterate over the vdevs, and post a sysevent for any
2660 * unopenable vdevs so that the normal autoreplace handler can take
2663 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2664 spa_check_removed(spa->spa_root_vdev);
2666 * For the import case, this is done in spa_import(), because
2667 * at this point we're using the spare definitions from
2668 * the MOS config, not necessarily from the userland config.
2670 if (state != SPA_LOAD_IMPORT) {
2671 spa_aux_check_removed(&spa->spa_spares);
2672 spa_aux_check_removed(&spa->spa_l2cache);
2677 * Load the vdev state for all toplevel vdevs.
2682 * Propagate the leaf DTLs we just loaded all the way up the tree.
2684 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2685 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2686 spa_config_exit(spa, SCL_ALL, FTAG);
2689 * Load the DDTs (dedup tables).
2691 error = ddt_load(spa);
2693 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2695 spa_update_dspace(spa);
2698 * Validate the config, using the MOS config to fill in any
2699 * information which might be missing. If we fail to validate
2700 * the config then declare the pool unfit for use. If we're
2701 * assembling a pool from a split, the log is not transferred
2704 if (type != SPA_IMPORT_ASSEMBLE) {
2707 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2708 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2710 if (!spa_config_valid(spa, nvconfig)) {
2711 nvlist_free(nvconfig);
2712 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2715 nvlist_free(nvconfig);
2718 * Now that we've validated the config, check the state of the
2719 * root vdev. If it can't be opened, it indicates one or
2720 * more toplevel vdevs are faulted.
2722 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2723 return (SET_ERROR(ENXIO));
2725 if (spa_check_logs(spa)) {
2726 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2727 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2731 if (missing_feat_write) {
2732 ASSERT(state == SPA_LOAD_TRYIMPORT);
2735 * At this point, we know that we can open the pool in
2736 * read-only mode but not read-write mode. We now have enough
2737 * information and can return to userland.
2739 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2743 * We've successfully opened the pool, verify that we're ready
2744 * to start pushing transactions.
2746 if (state != SPA_LOAD_TRYIMPORT) {
2747 if (error = spa_load_verify(spa))
2748 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2752 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2753 spa->spa_load_max_txg == UINT64_MAX)) {
2755 int need_update = B_FALSE;
2757 ASSERT(state != SPA_LOAD_TRYIMPORT);
2760 * Claim log blocks that haven't been committed yet.
2761 * This must all happen in a single txg.
2762 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2763 * invoked from zil_claim_log_block()'s i/o done callback.
2764 * Price of rollback is that we abandon the log.
2766 spa->spa_claiming = B_TRUE;
2768 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2769 spa_first_txg(spa));
2770 (void) dmu_objset_find(spa_name(spa),
2771 zil_claim, tx, DS_FIND_CHILDREN);
2774 spa->spa_claiming = B_FALSE;
2776 spa_set_log_state(spa, SPA_LOG_GOOD);
2777 spa->spa_sync_on = B_TRUE;
2778 txg_sync_start(spa->spa_dsl_pool);
2781 * Wait for all claims to sync. We sync up to the highest
2782 * claimed log block birth time so that claimed log blocks
2783 * don't appear to be from the future. spa_claim_max_txg
2784 * will have been set for us by either zil_check_log_chain()
2785 * (invoked from spa_check_logs()) or zil_claim() above.
2787 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2790 * If the config cache is stale, or we have uninitialized
2791 * metaslabs (see spa_vdev_add()), then update the config.
2793 * If this is a verbatim import, trust the current
2794 * in-core spa_config and update the disk labels.
2796 if (config_cache_txg != spa->spa_config_txg ||
2797 state == SPA_LOAD_IMPORT ||
2798 state == SPA_LOAD_RECOVER ||
2799 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2800 need_update = B_TRUE;
2802 for (int c = 0; c < rvd->vdev_children; c++)
2803 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2804 need_update = B_TRUE;
2807 * Update the config cache asychronously in case we're the
2808 * root pool, in which case the config cache isn't writable yet.
2811 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2814 * Check all DTLs to see if anything needs resilvering.
2816 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2817 vdev_resilver_needed(rvd, NULL, NULL))
2818 spa_async_request(spa, SPA_ASYNC_RESILVER);
2821 * Log the fact that we booted up (so that we can detect if
2822 * we rebooted in the middle of an operation).
2824 spa_history_log_version(spa, "open");
2827 * Delete any inconsistent datasets.
2829 (void) dmu_objset_find(spa_name(spa),
2830 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2833 * Clean up any stale temporary dataset userrefs.
2835 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2842 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2844 int mode = spa->spa_mode;
2847 spa_deactivate(spa);
2849 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2851 spa_activate(spa, mode);
2852 spa_async_suspend(spa);
2854 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2858 * If spa_load() fails this function will try loading prior txg's. If
2859 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2860 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2861 * function will not rewind the pool and will return the same error as
2865 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2866 uint64_t max_request, int rewind_flags)
2868 nvlist_t *loadinfo = NULL;
2869 nvlist_t *config = NULL;
2870 int load_error, rewind_error;
2871 uint64_t safe_rewind_txg;
2874 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2875 spa->spa_load_max_txg = spa->spa_load_txg;
2876 spa_set_log_state(spa, SPA_LOG_CLEAR);
2878 spa->spa_load_max_txg = max_request;
2879 if (max_request != UINT64_MAX)
2880 spa->spa_extreme_rewind = B_TRUE;
2883 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2885 if (load_error == 0)
2888 if (spa->spa_root_vdev != NULL)
2889 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2891 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2892 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2894 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2895 nvlist_free(config);
2896 return (load_error);
2899 if (state == SPA_LOAD_RECOVER) {
2900 /* Price of rolling back is discarding txgs, including log */
2901 spa_set_log_state(spa, SPA_LOG_CLEAR);
2904 * If we aren't rolling back save the load info from our first
2905 * import attempt so that we can restore it after attempting
2908 loadinfo = spa->spa_load_info;
2909 spa->spa_load_info = fnvlist_alloc();
2912 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2913 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2914 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2915 TXG_INITIAL : safe_rewind_txg;
2918 * Continue as long as we're finding errors, we're still within
2919 * the acceptable rewind range, and we're still finding uberblocks
2921 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2922 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2923 if (spa->spa_load_max_txg < safe_rewind_txg)
2924 spa->spa_extreme_rewind = B_TRUE;
2925 rewind_error = spa_load_retry(spa, state, mosconfig);
2928 spa->spa_extreme_rewind = B_FALSE;
2929 spa->spa_load_max_txg = UINT64_MAX;
2931 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2932 spa_config_set(spa, config);
2934 if (state == SPA_LOAD_RECOVER) {
2935 ASSERT3P(loadinfo, ==, NULL);
2936 return (rewind_error);
2938 /* Store the rewind info as part of the initial load info */
2939 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2940 spa->spa_load_info);
2942 /* Restore the initial load info */
2943 fnvlist_free(spa->spa_load_info);
2944 spa->spa_load_info = loadinfo;
2946 return (load_error);
2953 * The import case is identical to an open except that the configuration is sent
2954 * down from userland, instead of grabbed from the configuration cache. For the
2955 * case of an open, the pool configuration will exist in the
2956 * POOL_STATE_UNINITIALIZED state.
2958 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2959 * the same time open the pool, without having to keep around the spa_t in some
2963 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2967 spa_load_state_t state = SPA_LOAD_OPEN;
2969 int locked = B_FALSE;
2970 int firstopen = B_FALSE;
2975 * As disgusting as this is, we need to support recursive calls to this
2976 * function because dsl_dir_open() is called during spa_load(), and ends
2977 * up calling spa_open() again. The real fix is to figure out how to
2978 * avoid dsl_dir_open() calling this in the first place.
2980 if (mutex_owner(&spa_namespace_lock) != curthread) {
2981 mutex_enter(&spa_namespace_lock);
2985 if ((spa = spa_lookup(pool)) == NULL) {
2987 mutex_exit(&spa_namespace_lock);
2988 return (SET_ERROR(ENOENT));
2991 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2992 zpool_rewind_policy_t policy;
2996 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2998 if (policy.zrp_request & ZPOOL_DO_REWIND)
2999 state = SPA_LOAD_RECOVER;
3001 spa_activate(spa, spa_mode_global);
3003 if (state != SPA_LOAD_RECOVER)
3004 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3006 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3007 policy.zrp_request);
3009 if (error == EBADF) {
3011 * If vdev_validate() returns failure (indicated by
3012 * EBADF), it indicates that one of the vdevs indicates
3013 * that the pool has been exported or destroyed. If
3014 * this is the case, the config cache is out of sync and
3015 * we should remove the pool from the namespace.
3018 spa_deactivate(spa);
3019 spa_config_sync(spa, B_TRUE, B_TRUE);
3022 mutex_exit(&spa_namespace_lock);
3023 return (SET_ERROR(ENOENT));
3028 * We can't open the pool, but we still have useful
3029 * information: the state of each vdev after the
3030 * attempted vdev_open(). Return this to the user.
3032 if (config != NULL && spa->spa_config) {
3033 VERIFY(nvlist_dup(spa->spa_config, config,
3035 VERIFY(nvlist_add_nvlist(*config,
3036 ZPOOL_CONFIG_LOAD_INFO,
3037 spa->spa_load_info) == 0);
3040 spa_deactivate(spa);
3041 spa->spa_last_open_failed = error;
3043 mutex_exit(&spa_namespace_lock);
3049 spa_open_ref(spa, tag);
3052 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3055 * If we've recovered the pool, pass back any information we
3056 * gathered while doing the load.
3058 if (state == SPA_LOAD_RECOVER) {
3059 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3060 spa->spa_load_info) == 0);
3064 spa->spa_last_open_failed = 0;
3065 spa->spa_last_ubsync_txg = 0;
3066 spa->spa_load_txg = 0;
3067 mutex_exit(&spa_namespace_lock);
3071 zvol_create_minors(spa->spa_name);
3082 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3085 return (spa_open_common(name, spapp, tag, policy, config));
3089 spa_open(const char *name, spa_t **spapp, void *tag)
3091 return (spa_open_common(name, spapp, tag, NULL, NULL));
3095 * Lookup the given spa_t, incrementing the inject count in the process,
3096 * preventing it from being exported or destroyed.
3099 spa_inject_addref(char *name)
3103 mutex_enter(&spa_namespace_lock);
3104 if ((spa = spa_lookup(name)) == NULL) {
3105 mutex_exit(&spa_namespace_lock);
3108 spa->spa_inject_ref++;
3109 mutex_exit(&spa_namespace_lock);
3115 spa_inject_delref(spa_t *spa)
3117 mutex_enter(&spa_namespace_lock);
3118 spa->spa_inject_ref--;
3119 mutex_exit(&spa_namespace_lock);
3123 * Add spares device information to the nvlist.
3126 spa_add_spares(spa_t *spa, nvlist_t *config)
3136 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3138 if (spa->spa_spares.sav_count == 0)
3141 VERIFY(nvlist_lookup_nvlist(config,
3142 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3143 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3144 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3146 VERIFY(nvlist_add_nvlist_array(nvroot,
3147 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3148 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3149 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3152 * Go through and find any spares which have since been
3153 * repurposed as an active spare. If this is the case, update
3154 * their status appropriately.
3156 for (i = 0; i < nspares; i++) {
3157 VERIFY(nvlist_lookup_uint64(spares[i],
3158 ZPOOL_CONFIG_GUID, &guid) == 0);
3159 if (spa_spare_exists(guid, &pool, NULL) &&
3161 VERIFY(nvlist_lookup_uint64_array(
3162 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3163 (uint64_t **)&vs, &vsc) == 0);
3164 vs->vs_state = VDEV_STATE_CANT_OPEN;
3165 vs->vs_aux = VDEV_AUX_SPARED;
3172 * Add l2cache device information to the nvlist, including vdev stats.
3175 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3178 uint_t i, j, nl2cache;
3185 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3187 if (spa->spa_l2cache.sav_count == 0)
3190 VERIFY(nvlist_lookup_nvlist(config,
3191 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3192 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3193 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3194 if (nl2cache != 0) {
3195 VERIFY(nvlist_add_nvlist_array(nvroot,
3196 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3197 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3198 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3201 * Update level 2 cache device stats.
3204 for (i = 0; i < nl2cache; i++) {
3205 VERIFY(nvlist_lookup_uint64(l2cache[i],
3206 ZPOOL_CONFIG_GUID, &guid) == 0);
3209 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3211 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3212 vd = spa->spa_l2cache.sav_vdevs[j];
3218 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3219 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3221 vdev_get_stats(vd, vs);
3227 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3233 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3234 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3236 /* We may be unable to read features if pool is suspended. */
3237 if (spa_suspended(spa))
3240 if (spa->spa_feat_for_read_obj != 0) {
3241 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3242 spa->spa_feat_for_read_obj);
3243 zap_cursor_retrieve(&zc, &za) == 0;
3244 zap_cursor_advance(&zc)) {
3245 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3246 za.za_num_integers == 1);
3247 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3248 za.za_first_integer));
3250 zap_cursor_fini(&zc);
3253 if (spa->spa_feat_for_write_obj != 0) {
3254 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3255 spa->spa_feat_for_write_obj);
3256 zap_cursor_retrieve(&zc, &za) == 0;
3257 zap_cursor_advance(&zc)) {
3258 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3259 za.za_num_integers == 1);
3260 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3261 za.za_first_integer));
3263 zap_cursor_fini(&zc);
3267 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3269 nvlist_free(features);
3273 spa_get_stats(const char *name, nvlist_t **config,
3274 char *altroot, size_t buflen)
3280 error = spa_open_common(name, &spa, FTAG, NULL, config);
3284 * This still leaves a window of inconsistency where the spares
3285 * or l2cache devices could change and the config would be
3286 * self-inconsistent.
3288 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3290 if (*config != NULL) {
3291 uint64_t loadtimes[2];
3293 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3294 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3295 VERIFY(nvlist_add_uint64_array(*config,
3296 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3298 VERIFY(nvlist_add_uint64(*config,
3299 ZPOOL_CONFIG_ERRCOUNT,
3300 spa_get_errlog_size(spa)) == 0);
3302 if (spa_suspended(spa))
3303 VERIFY(nvlist_add_uint64(*config,
3304 ZPOOL_CONFIG_SUSPENDED,
3305 spa->spa_failmode) == 0);
3307 spa_add_spares(spa, *config);
3308 spa_add_l2cache(spa, *config);
3309 spa_add_feature_stats(spa, *config);
3314 * We want to get the alternate root even for faulted pools, so we cheat
3315 * and call spa_lookup() directly.
3319 mutex_enter(&spa_namespace_lock);
3320 spa = spa_lookup(name);
3322 spa_altroot(spa, altroot, buflen);
3326 mutex_exit(&spa_namespace_lock);
3328 spa_altroot(spa, altroot, buflen);
3333 spa_config_exit(spa, SCL_CONFIG, FTAG);
3334 spa_close(spa, FTAG);
3341 * Validate that the auxiliary device array is well formed. We must have an
3342 * array of nvlists, each which describes a valid leaf vdev. If this is an
3343 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3344 * specified, as long as they are well-formed.
3347 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3348 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3349 vdev_labeltype_t label)
3356 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3359 * It's acceptable to have no devs specified.
3361 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3365 return (SET_ERROR(EINVAL));
3368 * Make sure the pool is formatted with a version that supports this
3371 if (spa_version(spa) < version)
3372 return (SET_ERROR(ENOTSUP));
3375 * Set the pending device list so we correctly handle device in-use
3378 sav->sav_pending = dev;
3379 sav->sav_npending = ndev;
3381 for (i = 0; i < ndev; i++) {
3382 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3386 if (!vd->vdev_ops->vdev_op_leaf) {
3388 error = SET_ERROR(EINVAL);
3393 * The L2ARC currently only supports disk devices in
3394 * kernel context. For user-level testing, we allow it.
3397 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3398 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3399 error = SET_ERROR(ENOTBLK);
3406 if ((error = vdev_open(vd)) == 0 &&
3407 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3408 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3409 vd->vdev_guid) == 0);
3415 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3422 sav->sav_pending = NULL;
3423 sav->sav_npending = 0;
3428 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3432 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3434 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3435 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3436 VDEV_LABEL_SPARE)) != 0) {
3440 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3441 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3442 VDEV_LABEL_L2CACHE));
3446 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3451 if (sav->sav_config != NULL) {
3457 * Generate new dev list by concatentating with the
3460 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3461 &olddevs, &oldndevs) == 0);
3463 newdevs = kmem_alloc(sizeof (void *) *
3464 (ndevs + oldndevs), KM_SLEEP);
3465 for (i = 0; i < oldndevs; i++)
3466 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3468 for (i = 0; i < ndevs; i++)
3469 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3472 VERIFY(nvlist_remove(sav->sav_config, config,
3473 DATA_TYPE_NVLIST_ARRAY) == 0);
3475 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3476 config, newdevs, ndevs + oldndevs) == 0);
3477 for (i = 0; i < oldndevs + ndevs; i++)
3478 nvlist_free(newdevs[i]);
3479 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3482 * Generate a new dev list.
3484 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3486 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3492 * Stop and drop level 2 ARC devices
3495 spa_l2cache_drop(spa_t *spa)
3499 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3501 for (i = 0; i < sav->sav_count; i++) {
3504 vd = sav->sav_vdevs[i];
3507 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3508 pool != 0ULL && l2arc_vdev_present(vd))
3509 l2arc_remove_vdev(vd);
3517 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3521 char *altroot = NULL;
3526 uint64_t txg = TXG_INITIAL;
3527 nvlist_t **spares, **l2cache;
3528 uint_t nspares, nl2cache;
3529 uint64_t version, obj;
3530 boolean_t has_features;
3533 * If this pool already exists, return failure.
3535 mutex_enter(&spa_namespace_lock);
3536 if (spa_lookup(pool) != NULL) {
3537 mutex_exit(&spa_namespace_lock);
3538 return (SET_ERROR(EEXIST));
3542 * Allocate a new spa_t structure.
3544 (void) nvlist_lookup_string(props,
3545 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3546 spa = spa_add(pool, NULL, altroot);
3547 spa_activate(spa, spa_mode_global);
3549 if (props && (error = spa_prop_validate(spa, props))) {
3550 spa_deactivate(spa);
3552 mutex_exit(&spa_namespace_lock);
3556 has_features = B_FALSE;
3557 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3558 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3559 if (zpool_prop_feature(nvpair_name(elem)))
3560 has_features = B_TRUE;
3563 if (has_features || nvlist_lookup_uint64(props,
3564 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3565 version = SPA_VERSION;
3567 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3569 spa->spa_first_txg = txg;
3570 spa->spa_uberblock.ub_txg = txg - 1;
3571 spa->spa_uberblock.ub_version = version;
3572 spa->spa_ubsync = spa->spa_uberblock;
3575 * Create "The Godfather" zio to hold all async IOs
3577 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3579 for (int i = 0; i < max_ncpus; i++) {
3580 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3581 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3582 ZIO_FLAG_GODFATHER);
3586 * Create the root vdev.
3588 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3590 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3592 ASSERT(error != 0 || rvd != NULL);
3593 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3595 if (error == 0 && !zfs_allocatable_devs(nvroot))
3596 error = SET_ERROR(EINVAL);
3599 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3600 (error = spa_validate_aux(spa, nvroot, txg,
3601 VDEV_ALLOC_ADD)) == 0) {
3602 for (int c = 0; c < rvd->vdev_children; c++) {
3603 vdev_ashift_optimize(rvd->vdev_child[c]);
3604 vdev_metaslab_set_size(rvd->vdev_child[c]);
3605 vdev_expand(rvd->vdev_child[c], txg);
3609 spa_config_exit(spa, SCL_ALL, FTAG);
3613 spa_deactivate(spa);
3615 mutex_exit(&spa_namespace_lock);
3620 * Get the list of spares, if specified.
3622 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3623 &spares, &nspares) == 0) {
3624 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3626 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3627 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3628 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3629 spa_load_spares(spa);
3630 spa_config_exit(spa, SCL_ALL, FTAG);
3631 spa->spa_spares.sav_sync = B_TRUE;
3635 * Get the list of level 2 cache devices, if specified.
3637 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3638 &l2cache, &nl2cache) == 0) {
3639 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3640 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3641 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3642 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3643 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3644 spa_load_l2cache(spa);
3645 spa_config_exit(spa, SCL_ALL, FTAG);
3646 spa->spa_l2cache.sav_sync = B_TRUE;
3649 spa->spa_is_initializing = B_TRUE;
3650 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3651 spa->spa_meta_objset = dp->dp_meta_objset;
3652 spa->spa_is_initializing = B_FALSE;
3655 * Create DDTs (dedup tables).
3659 spa_update_dspace(spa);
3661 tx = dmu_tx_create_assigned(dp, txg);
3664 * Create the pool config object.
3666 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3667 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3668 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3670 if (zap_add(spa->spa_meta_objset,
3671 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3672 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3673 cmn_err(CE_PANIC, "failed to add pool config");
3676 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3677 spa_feature_create_zap_objects(spa, tx);
3679 if (zap_add(spa->spa_meta_objset,
3680 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3681 sizeof (uint64_t), 1, &version, tx) != 0) {
3682 cmn_err(CE_PANIC, "failed to add pool version");
3685 /* Newly created pools with the right version are always deflated. */
3686 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3687 spa->spa_deflate = TRUE;
3688 if (zap_add(spa->spa_meta_objset,
3689 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3690 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3691 cmn_err(CE_PANIC, "failed to add deflate");
3696 * Create the deferred-free bpobj. Turn off compression
3697 * because sync-to-convergence takes longer if the blocksize
3700 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3701 dmu_object_set_compress(spa->spa_meta_objset, obj,
3702 ZIO_COMPRESS_OFF, tx);
3703 if (zap_add(spa->spa_meta_objset,
3704 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3705 sizeof (uint64_t), 1, &obj, tx) != 0) {
3706 cmn_err(CE_PANIC, "failed to add bpobj");
3708 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3709 spa->spa_meta_objset, obj));
3712 * Create the pool's history object.
3714 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3715 spa_history_create_obj(spa, tx);
3718 * Set pool properties.
3720 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3721 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3722 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3723 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3725 if (props != NULL) {
3726 spa_configfile_set(spa, props, B_FALSE);
3727 spa_sync_props(props, tx);
3732 spa->spa_sync_on = B_TRUE;
3733 txg_sync_start(spa->spa_dsl_pool);
3736 * We explicitly wait for the first transaction to complete so that our
3737 * bean counters are appropriately updated.
3739 txg_wait_synced(spa->spa_dsl_pool, txg);
3741 spa_config_sync(spa, B_FALSE, B_TRUE);
3743 spa_history_log_version(spa, "create");
3745 spa->spa_minref = refcount_count(&spa->spa_refcount);
3747 mutex_exit(&spa_namespace_lock);
3755 * Get the root pool information from the root disk, then import the root pool
3756 * during the system boot up time.
3758 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3761 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3764 nvlist_t *nvtop, *nvroot;
3767 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3771 * Add this top-level vdev to the child array.
3773 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3775 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3777 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3780 * Put this pool's top-level vdevs into a root vdev.
3782 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3783 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3784 VDEV_TYPE_ROOT) == 0);
3785 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3786 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3787 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3791 * Replace the existing vdev_tree with the new root vdev in
3792 * this pool's configuration (remove the old, add the new).
3794 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3795 nvlist_free(nvroot);
3800 * Walk the vdev tree and see if we can find a device with "better"
3801 * configuration. A configuration is "better" if the label on that
3802 * device has a more recent txg.
3805 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3807 for (int c = 0; c < vd->vdev_children; c++)
3808 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3810 if (vd->vdev_ops->vdev_op_leaf) {
3814 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3818 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3822 * Do we have a better boot device?
3824 if (label_txg > *txg) {
3833 * Import a root pool.
3835 * For x86. devpath_list will consist of devid and/or physpath name of
3836 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3837 * The GRUB "findroot" command will return the vdev we should boot.
3839 * For Sparc, devpath_list consists the physpath name of the booting device
3840 * no matter the rootpool is a single device pool or a mirrored pool.
3842 * "/pci@1f,0/ide@d/disk@0,0:a"
3845 spa_import_rootpool(char *devpath, char *devid)
3848 vdev_t *rvd, *bvd, *avd = NULL;
3849 nvlist_t *config, *nvtop;
3855 * Read the label from the boot device and generate a configuration.
3857 config = spa_generate_rootconf(devpath, devid, &guid);
3858 #if defined(_OBP) && defined(_KERNEL)
3859 if (config == NULL) {
3860 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3862 get_iscsi_bootpath_phy(devpath);
3863 config = spa_generate_rootconf(devpath, devid, &guid);
3867 if (config == NULL) {
3868 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3870 return (SET_ERROR(EIO));
3873 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3875 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3877 mutex_enter(&spa_namespace_lock);
3878 if ((spa = spa_lookup(pname)) != NULL) {
3880 * Remove the existing root pool from the namespace so that we
3881 * can replace it with the correct config we just read in.
3886 spa = spa_add(pname, config, NULL);
3887 spa->spa_is_root = B_TRUE;
3888 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3891 * Build up a vdev tree based on the boot device's label config.
3893 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3895 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3896 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3897 VDEV_ALLOC_ROOTPOOL);
3898 spa_config_exit(spa, SCL_ALL, FTAG);
3900 mutex_exit(&spa_namespace_lock);
3901 nvlist_free(config);
3902 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3908 * Get the boot vdev.
3910 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3911 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3912 (u_longlong_t)guid);
3913 error = SET_ERROR(ENOENT);
3918 * Determine if there is a better boot device.
3921 spa_alt_rootvdev(rvd, &avd, &txg);
3923 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3924 "try booting from '%s'", avd->vdev_path);
3925 error = SET_ERROR(EINVAL);
3930 * If the boot device is part of a spare vdev then ensure that
3931 * we're booting off the active spare.
3933 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3934 !bvd->vdev_isspare) {
3935 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3936 "try booting from '%s'",
3938 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3939 error = SET_ERROR(EINVAL);
3945 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3947 spa_config_exit(spa, SCL_ALL, FTAG);
3948 mutex_exit(&spa_namespace_lock);
3950 nvlist_free(config);
3956 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3960 spa_generate_rootconf(const char *name)
3962 nvlist_t **configs, **tops;
3964 nvlist_t *best_cfg, *nvtop, *nvroot;
3973 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3976 ASSERT3U(count, !=, 0);
3978 for (i = 0; i < count; i++) {
3981 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
3983 if (txg > best_txg) {
3985 best_cfg = configs[i];
3990 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3992 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
3995 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
3996 for (i = 0; i < nchildren; i++) {
3999 if (configs[i] == NULL)
4001 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
4003 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
4005 for (i = 0; holes != NULL && i < nholes; i++) {
4008 if (tops[holes[i]] != NULL)
4010 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
4011 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
4012 VDEV_TYPE_HOLE) == 0);
4013 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
4015 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4018 for (i = 0; i < nchildren; i++) {
4019 if (tops[i] != NULL)
4021 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4022 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4023 VDEV_TYPE_MISSING) == 0);
4024 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4026 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4031 * Create pool config based on the best vdev config.
4033 nvlist_dup(best_cfg, &config, KM_SLEEP);
4036 * Put this pool's top-level vdevs into a root vdev.
4038 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4040 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4041 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4042 VDEV_TYPE_ROOT) == 0);
4043 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4044 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4045 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4046 tops, nchildren) == 0);
4049 * Replace the existing vdev_tree with the new root vdev in
4050 * this pool's configuration (remove the old, add the new).
4052 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4055 * Drop vdev config elements that should not be present at pool level.
4057 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4058 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4060 for (i = 0; i < count; i++)
4061 nvlist_free(configs[i]);
4062 kmem_free(configs, count * sizeof(void *));
4063 for (i = 0; i < nchildren; i++)
4064 nvlist_free(tops[i]);
4065 kmem_free(tops, nchildren * sizeof(void *));
4066 nvlist_free(nvroot);
4071 spa_import_rootpool(const char *name)
4074 vdev_t *rvd, *bvd, *avd = NULL;
4075 nvlist_t *config, *nvtop;
4081 * Read the label from the boot device and generate a configuration.
4083 config = spa_generate_rootconf(name);
4085 mutex_enter(&spa_namespace_lock);
4086 if (config != NULL) {
4087 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4088 &pname) == 0 && strcmp(name, pname) == 0);
4089 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4092 if ((spa = spa_lookup(pname)) != NULL) {
4094 * Remove the existing root pool from the namespace so
4095 * that we can replace it with the correct config
4100 spa = spa_add(pname, config, NULL);
4103 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4104 * via spa_version().
4106 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4107 &spa->spa_ubsync.ub_version) != 0)
4108 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4109 } else if ((spa = spa_lookup(name)) == NULL) {
4110 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4114 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4116 spa->spa_is_root = B_TRUE;
4117 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4120 * Build up a vdev tree based on the boot device's label config.
4122 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4124 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4125 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4126 VDEV_ALLOC_ROOTPOOL);
4127 spa_config_exit(spa, SCL_ALL, FTAG);
4129 mutex_exit(&spa_namespace_lock);
4130 nvlist_free(config);
4131 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4136 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4138 spa_config_exit(spa, SCL_ALL, FTAG);
4139 mutex_exit(&spa_namespace_lock);
4141 nvlist_free(config);
4149 * Import a non-root pool into the system.
4152 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4155 char *altroot = NULL;
4156 spa_load_state_t state = SPA_LOAD_IMPORT;
4157 zpool_rewind_policy_t policy;
4158 uint64_t mode = spa_mode_global;
4159 uint64_t readonly = B_FALSE;
4162 nvlist_t **spares, **l2cache;
4163 uint_t nspares, nl2cache;
4166 * If a pool with this name exists, return failure.
4168 mutex_enter(&spa_namespace_lock);
4169 if (spa_lookup(pool) != NULL) {
4170 mutex_exit(&spa_namespace_lock);
4171 return (SET_ERROR(EEXIST));
4175 * Create and initialize the spa structure.
4177 (void) nvlist_lookup_string(props,
4178 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4179 (void) nvlist_lookup_uint64(props,
4180 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4183 spa = spa_add(pool, config, altroot);
4184 spa->spa_import_flags = flags;
4187 * Verbatim import - Take a pool and insert it into the namespace
4188 * as if it had been loaded at boot.
4190 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4192 spa_configfile_set(spa, props, B_FALSE);
4194 spa_config_sync(spa, B_FALSE, B_TRUE);
4196 mutex_exit(&spa_namespace_lock);
4200 spa_activate(spa, mode);
4203 * Don't start async tasks until we know everything is healthy.
4205 spa_async_suspend(spa);
4207 zpool_get_rewind_policy(config, &policy);
4208 if (policy.zrp_request & ZPOOL_DO_REWIND)
4209 state = SPA_LOAD_RECOVER;
4212 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4213 * because the user-supplied config is actually the one to trust when
4216 if (state != SPA_LOAD_RECOVER)
4217 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4219 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4220 policy.zrp_request);
4223 * Propagate anything learned while loading the pool and pass it
4224 * back to caller (i.e. rewind info, missing devices, etc).
4226 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4227 spa->spa_load_info) == 0);
4229 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4231 * Toss any existing sparelist, as it doesn't have any validity
4232 * anymore, and conflicts with spa_has_spare().
4234 if (spa->spa_spares.sav_config) {
4235 nvlist_free(spa->spa_spares.sav_config);
4236 spa->spa_spares.sav_config = NULL;
4237 spa_load_spares(spa);
4239 if (spa->spa_l2cache.sav_config) {
4240 nvlist_free(spa->spa_l2cache.sav_config);
4241 spa->spa_l2cache.sav_config = NULL;
4242 spa_load_l2cache(spa);
4245 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4248 error = spa_validate_aux(spa, nvroot, -1ULL,
4251 error = spa_validate_aux(spa, nvroot, -1ULL,
4252 VDEV_ALLOC_L2CACHE);
4253 spa_config_exit(spa, SCL_ALL, FTAG);
4256 spa_configfile_set(spa, props, B_FALSE);
4258 if (error != 0 || (props && spa_writeable(spa) &&
4259 (error = spa_prop_set(spa, props)))) {
4261 spa_deactivate(spa);
4263 mutex_exit(&spa_namespace_lock);
4267 spa_async_resume(spa);
4270 * Override any spares and level 2 cache devices as specified by
4271 * the user, as these may have correct device names/devids, etc.
4273 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4274 &spares, &nspares) == 0) {
4275 if (spa->spa_spares.sav_config)
4276 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4277 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4279 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4280 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4281 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4282 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4283 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4284 spa_load_spares(spa);
4285 spa_config_exit(spa, SCL_ALL, FTAG);
4286 spa->spa_spares.sav_sync = B_TRUE;
4288 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4289 &l2cache, &nl2cache) == 0) {
4290 if (spa->spa_l2cache.sav_config)
4291 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4292 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4294 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4295 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4296 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4297 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4298 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4299 spa_load_l2cache(spa);
4300 spa_config_exit(spa, SCL_ALL, FTAG);
4301 spa->spa_l2cache.sav_sync = B_TRUE;
4305 * Check for any removed devices.
4307 if (spa->spa_autoreplace) {
4308 spa_aux_check_removed(&spa->spa_spares);
4309 spa_aux_check_removed(&spa->spa_l2cache);
4312 if (spa_writeable(spa)) {
4314 * Update the config cache to include the newly-imported pool.
4316 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4320 * It's possible that the pool was expanded while it was exported.
4321 * We kick off an async task to handle this for us.
4323 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4325 mutex_exit(&spa_namespace_lock);
4326 spa_history_log_version(spa, "import");
4330 zvol_create_minors(pool);
4337 spa_tryimport(nvlist_t *tryconfig)
4339 nvlist_t *config = NULL;
4345 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4348 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4352 * Create and initialize the spa structure.
4354 mutex_enter(&spa_namespace_lock);
4355 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4356 spa_activate(spa, FREAD);
4359 * Pass off the heavy lifting to spa_load().
4360 * Pass TRUE for mosconfig because the user-supplied config
4361 * is actually the one to trust when doing an import.
4363 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4366 * If 'tryconfig' was at least parsable, return the current config.
4368 if (spa->spa_root_vdev != NULL) {
4369 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4370 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4372 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4374 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4375 spa->spa_uberblock.ub_timestamp) == 0);
4376 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4377 spa->spa_load_info) == 0);
4380 * If the bootfs property exists on this pool then we
4381 * copy it out so that external consumers can tell which
4382 * pools are bootable.
4384 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4385 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4388 * We have to play games with the name since the
4389 * pool was opened as TRYIMPORT_NAME.
4391 if (dsl_dsobj_to_dsname(spa_name(spa),
4392 spa->spa_bootfs, tmpname) == 0) {
4394 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4396 cp = strchr(tmpname, '/');
4398 (void) strlcpy(dsname, tmpname,
4401 (void) snprintf(dsname, MAXPATHLEN,
4402 "%s/%s", poolname, ++cp);
4404 VERIFY(nvlist_add_string(config,
4405 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4406 kmem_free(dsname, MAXPATHLEN);
4408 kmem_free(tmpname, MAXPATHLEN);
4412 * Add the list of hot spares and level 2 cache devices.
4414 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4415 spa_add_spares(spa, config);
4416 spa_add_l2cache(spa, config);
4417 spa_config_exit(spa, SCL_CONFIG, FTAG);
4421 spa_deactivate(spa);
4423 mutex_exit(&spa_namespace_lock);
4429 * Pool export/destroy
4431 * The act of destroying or exporting a pool is very simple. We make sure there
4432 * is no more pending I/O and any references to the pool are gone. Then, we
4433 * update the pool state and sync all the labels to disk, removing the
4434 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4435 * we don't sync the labels or remove the configuration cache.
4438 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4439 boolean_t force, boolean_t hardforce)
4446 if (!(spa_mode_global & FWRITE))
4447 return (SET_ERROR(EROFS));
4449 mutex_enter(&spa_namespace_lock);
4450 if ((spa = spa_lookup(pool)) == NULL) {
4451 mutex_exit(&spa_namespace_lock);
4452 return (SET_ERROR(ENOENT));
4456 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4457 * reacquire the namespace lock, and see if we can export.
4459 spa_open_ref(spa, FTAG);
4460 mutex_exit(&spa_namespace_lock);
4461 spa_async_suspend(spa);
4462 mutex_enter(&spa_namespace_lock);
4463 spa_close(spa, FTAG);
4466 * The pool will be in core if it's openable,
4467 * in which case we can modify its state.
4469 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4471 * Objsets may be open only because they're dirty, so we
4472 * have to force it to sync before checking spa_refcnt.
4474 txg_wait_synced(spa->spa_dsl_pool, 0);
4477 * A pool cannot be exported or destroyed if there are active
4478 * references. If we are resetting a pool, allow references by
4479 * fault injection handlers.
4481 if (!spa_refcount_zero(spa) ||
4482 (spa->spa_inject_ref != 0 &&
4483 new_state != POOL_STATE_UNINITIALIZED)) {
4484 spa_async_resume(spa);
4485 mutex_exit(&spa_namespace_lock);
4486 return (SET_ERROR(EBUSY));
4490 * A pool cannot be exported if it has an active shared spare.
4491 * This is to prevent other pools stealing the active spare
4492 * from an exported pool. At user's own will, such pool can
4493 * be forcedly exported.
4495 if (!force && new_state == POOL_STATE_EXPORTED &&
4496 spa_has_active_shared_spare(spa)) {
4497 spa_async_resume(spa);
4498 mutex_exit(&spa_namespace_lock);
4499 return (SET_ERROR(EXDEV));
4503 * We want this to be reflected on every label,
4504 * so mark them all dirty. spa_unload() will do the
4505 * final sync that pushes these changes out.
4507 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4508 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4509 spa->spa_state = new_state;
4510 spa->spa_final_txg = spa_last_synced_txg(spa) +
4512 vdev_config_dirty(spa->spa_root_vdev);
4513 spa_config_exit(spa, SCL_ALL, FTAG);
4517 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4519 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4521 spa_deactivate(spa);
4524 if (oldconfig && spa->spa_config)
4525 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4527 if (new_state != POOL_STATE_UNINITIALIZED) {
4529 spa_config_sync(spa, B_TRUE, B_TRUE);
4532 mutex_exit(&spa_namespace_lock);
4538 * Destroy a storage pool.
4541 spa_destroy(char *pool)
4543 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4548 * Export a storage pool.
4551 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4552 boolean_t hardforce)
4554 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4559 * Similar to spa_export(), this unloads the spa_t without actually removing it
4560 * from the namespace in any way.
4563 spa_reset(char *pool)
4565 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4570 * ==========================================================================
4571 * Device manipulation
4572 * ==========================================================================
4576 * Add a device to a storage pool.
4579 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4583 vdev_t *rvd = spa->spa_root_vdev;
4585 nvlist_t **spares, **l2cache;
4586 uint_t nspares, nl2cache;
4588 ASSERT(spa_writeable(spa));
4590 txg = spa_vdev_enter(spa);
4592 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4593 VDEV_ALLOC_ADD)) != 0)
4594 return (spa_vdev_exit(spa, NULL, txg, error));
4596 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4598 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4602 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4606 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4607 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4609 if (vd->vdev_children != 0 &&
4610 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4611 return (spa_vdev_exit(spa, vd, txg, error));
4614 * We must validate the spares and l2cache devices after checking the
4615 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4617 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4618 return (spa_vdev_exit(spa, vd, txg, error));
4621 * Transfer each new top-level vdev from vd to rvd.
4623 for (int c = 0; c < vd->vdev_children; c++) {
4626 * Set the vdev id to the first hole, if one exists.
4628 for (id = 0; id < rvd->vdev_children; id++) {
4629 if (rvd->vdev_child[id]->vdev_ishole) {
4630 vdev_free(rvd->vdev_child[id]);
4634 tvd = vd->vdev_child[c];
4635 vdev_remove_child(vd, tvd);
4637 vdev_add_child(rvd, tvd);
4638 vdev_config_dirty(tvd);
4642 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4643 ZPOOL_CONFIG_SPARES);
4644 spa_load_spares(spa);
4645 spa->spa_spares.sav_sync = B_TRUE;
4648 if (nl2cache != 0) {
4649 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4650 ZPOOL_CONFIG_L2CACHE);
4651 spa_load_l2cache(spa);
4652 spa->spa_l2cache.sav_sync = B_TRUE;
4656 * We have to be careful when adding new vdevs to an existing pool.
4657 * If other threads start allocating from these vdevs before we
4658 * sync the config cache, and we lose power, then upon reboot we may
4659 * fail to open the pool because there are DVAs that the config cache
4660 * can't translate. Therefore, we first add the vdevs without
4661 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4662 * and then let spa_config_update() initialize the new metaslabs.
4664 * spa_load() checks for added-but-not-initialized vdevs, so that
4665 * if we lose power at any point in this sequence, the remaining
4666 * steps will be completed the next time we load the pool.
4668 (void) spa_vdev_exit(spa, vd, txg, 0);
4670 mutex_enter(&spa_namespace_lock);
4671 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4672 mutex_exit(&spa_namespace_lock);
4678 * Attach a device to a mirror. The arguments are the path to any device
4679 * in the mirror, and the nvroot for the new device. If the path specifies
4680 * a device that is not mirrored, we automatically insert the mirror vdev.
4682 * If 'replacing' is specified, the new device is intended to replace the
4683 * existing device; in this case the two devices are made into their own
4684 * mirror using the 'replacing' vdev, which is functionally identical to
4685 * the mirror vdev (it actually reuses all the same ops) but has a few
4686 * extra rules: you can't attach to it after it's been created, and upon
4687 * completion of resilvering, the first disk (the one being replaced)
4688 * is automatically detached.
4691 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4693 uint64_t txg, dtl_max_txg;
4694 vdev_t *rvd = spa->spa_root_vdev;
4695 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4697 char *oldvdpath, *newvdpath;
4701 ASSERT(spa_writeable(spa));
4703 txg = spa_vdev_enter(spa);
4705 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4708 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4710 if (!oldvd->vdev_ops->vdev_op_leaf)
4711 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4713 pvd = oldvd->vdev_parent;
4715 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4716 VDEV_ALLOC_ATTACH)) != 0)
4717 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4719 if (newrootvd->vdev_children != 1)
4720 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4722 newvd = newrootvd->vdev_child[0];
4724 if (!newvd->vdev_ops->vdev_op_leaf)
4725 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4727 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4728 return (spa_vdev_exit(spa, newrootvd, txg, error));
4731 * Spares can't replace logs
4733 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4734 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4738 * For attach, the only allowable parent is a mirror or the root
4741 if (pvd->vdev_ops != &vdev_mirror_ops &&
4742 pvd->vdev_ops != &vdev_root_ops)
4743 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4745 pvops = &vdev_mirror_ops;
4748 * Active hot spares can only be replaced by inactive hot
4751 if (pvd->vdev_ops == &vdev_spare_ops &&
4752 oldvd->vdev_isspare &&
4753 !spa_has_spare(spa, newvd->vdev_guid))
4754 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4757 * If the source is a hot spare, and the parent isn't already a
4758 * spare, then we want to create a new hot spare. Otherwise, we
4759 * want to create a replacing vdev. The user is not allowed to
4760 * attach to a spared vdev child unless the 'isspare' state is
4761 * the same (spare replaces spare, non-spare replaces
4764 if (pvd->vdev_ops == &vdev_replacing_ops &&
4765 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4766 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4767 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4768 newvd->vdev_isspare != oldvd->vdev_isspare) {
4769 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4772 if (newvd->vdev_isspare)
4773 pvops = &vdev_spare_ops;
4775 pvops = &vdev_replacing_ops;
4779 * Make sure the new device is big enough.
4781 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4782 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4785 * The new device cannot have a higher alignment requirement
4786 * than the top-level vdev.
4788 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4789 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4792 * If this is an in-place replacement, update oldvd's path and devid
4793 * to make it distinguishable from newvd, and unopenable from now on.
4795 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4796 spa_strfree(oldvd->vdev_path);
4797 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4799 (void) sprintf(oldvd->vdev_path, "%s/%s",
4800 newvd->vdev_path, "old");
4801 if (oldvd->vdev_devid != NULL) {
4802 spa_strfree(oldvd->vdev_devid);
4803 oldvd->vdev_devid = NULL;
4807 /* mark the device being resilvered */
4808 newvd->vdev_resilver_txg = txg;
4811 * If the parent is not a mirror, or if we're replacing, insert the new
4812 * mirror/replacing/spare vdev above oldvd.
4814 if (pvd->vdev_ops != pvops)
4815 pvd = vdev_add_parent(oldvd, pvops);
4817 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4818 ASSERT(pvd->vdev_ops == pvops);
4819 ASSERT(oldvd->vdev_parent == pvd);
4822 * Extract the new device from its root and add it to pvd.
4824 vdev_remove_child(newrootvd, newvd);
4825 newvd->vdev_id = pvd->vdev_children;
4826 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4827 vdev_add_child(pvd, newvd);
4829 tvd = newvd->vdev_top;
4830 ASSERT(pvd->vdev_top == tvd);
4831 ASSERT(tvd->vdev_parent == rvd);
4833 vdev_config_dirty(tvd);
4836 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4837 * for any dmu_sync-ed blocks. It will propagate upward when
4838 * spa_vdev_exit() calls vdev_dtl_reassess().
4840 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4842 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4843 dtl_max_txg - TXG_INITIAL);
4845 if (newvd->vdev_isspare) {
4846 spa_spare_activate(newvd);
4847 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4850 oldvdpath = spa_strdup(oldvd->vdev_path);
4851 newvdpath = spa_strdup(newvd->vdev_path);
4852 newvd_isspare = newvd->vdev_isspare;
4855 * Mark newvd's DTL dirty in this txg.
4857 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4860 * Schedule the resilver to restart in the future. We do this to
4861 * ensure that dmu_sync-ed blocks have been stitched into the
4862 * respective datasets.
4864 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4869 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4871 spa_history_log_internal(spa, "vdev attach", NULL,
4872 "%s vdev=%s %s vdev=%s",
4873 replacing && newvd_isspare ? "spare in" :
4874 replacing ? "replace" : "attach", newvdpath,
4875 replacing ? "for" : "to", oldvdpath);
4877 spa_strfree(oldvdpath);
4878 spa_strfree(newvdpath);
4880 if (spa->spa_bootfs)
4881 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4887 * Detach a device from a mirror or replacing vdev.
4889 * If 'replace_done' is specified, only detach if the parent
4890 * is a replacing vdev.
4893 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4897 vdev_t *rvd = spa->spa_root_vdev;
4898 vdev_t *vd, *pvd, *cvd, *tvd;
4899 boolean_t unspare = B_FALSE;
4900 uint64_t unspare_guid = 0;
4903 ASSERT(spa_writeable(spa));
4905 txg = spa_vdev_enter(spa);
4907 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4910 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4912 if (!vd->vdev_ops->vdev_op_leaf)
4913 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4915 pvd = vd->vdev_parent;
4918 * If the parent/child relationship is not as expected, don't do it.
4919 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4920 * vdev that's replacing B with C. The user's intent in replacing
4921 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4922 * the replace by detaching C, the expected behavior is to end up
4923 * M(A,B). But suppose that right after deciding to detach C,
4924 * the replacement of B completes. We would have M(A,C), and then
4925 * ask to detach C, which would leave us with just A -- not what
4926 * the user wanted. To prevent this, we make sure that the
4927 * parent/child relationship hasn't changed -- in this example,
4928 * that C's parent is still the replacing vdev R.
4930 if (pvd->vdev_guid != pguid && pguid != 0)
4931 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4934 * Only 'replacing' or 'spare' vdevs can be replaced.
4936 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4937 pvd->vdev_ops != &vdev_spare_ops)
4938 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4940 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4941 spa_version(spa) >= SPA_VERSION_SPARES);
4944 * Only mirror, replacing, and spare vdevs support detach.
4946 if (pvd->vdev_ops != &vdev_replacing_ops &&
4947 pvd->vdev_ops != &vdev_mirror_ops &&
4948 pvd->vdev_ops != &vdev_spare_ops)
4949 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4952 * If this device has the only valid copy of some data,
4953 * we cannot safely detach it.
4955 if (vdev_dtl_required(vd))
4956 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4958 ASSERT(pvd->vdev_children >= 2);
4961 * If we are detaching the second disk from a replacing vdev, then
4962 * check to see if we changed the original vdev's path to have "/old"
4963 * at the end in spa_vdev_attach(). If so, undo that change now.
4965 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4966 vd->vdev_path != NULL) {
4967 size_t len = strlen(vd->vdev_path);
4969 for (int c = 0; c < pvd->vdev_children; c++) {
4970 cvd = pvd->vdev_child[c];
4972 if (cvd == vd || cvd->vdev_path == NULL)
4975 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4976 strcmp(cvd->vdev_path + len, "/old") == 0) {
4977 spa_strfree(cvd->vdev_path);
4978 cvd->vdev_path = spa_strdup(vd->vdev_path);
4985 * If we are detaching the original disk from a spare, then it implies
4986 * that the spare should become a real disk, and be removed from the
4987 * active spare list for the pool.
4989 if (pvd->vdev_ops == &vdev_spare_ops &&
4991 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4995 * Erase the disk labels so the disk can be used for other things.
4996 * This must be done after all other error cases are handled,
4997 * but before we disembowel vd (so we can still do I/O to it).
4998 * But if we can't do it, don't treat the error as fatal --
4999 * it may be that the unwritability of the disk is the reason
5000 * it's being detached!
5002 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5005 * Remove vd from its parent and compact the parent's children.
5007 vdev_remove_child(pvd, vd);
5008 vdev_compact_children(pvd);
5011 * Remember one of the remaining children so we can get tvd below.
5013 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5016 * If we need to remove the remaining child from the list of hot spares,
5017 * do it now, marking the vdev as no longer a spare in the process.
5018 * We must do this before vdev_remove_parent(), because that can
5019 * change the GUID if it creates a new toplevel GUID. For a similar
5020 * reason, we must remove the spare now, in the same txg as the detach;
5021 * otherwise someone could attach a new sibling, change the GUID, and
5022 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5025 ASSERT(cvd->vdev_isspare);
5026 spa_spare_remove(cvd);
5027 unspare_guid = cvd->vdev_guid;
5028 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5029 cvd->vdev_unspare = B_TRUE;
5033 * If the parent mirror/replacing vdev only has one child,
5034 * the parent is no longer needed. Remove it from the tree.
5036 if (pvd->vdev_children == 1) {
5037 if (pvd->vdev_ops == &vdev_spare_ops)
5038 cvd->vdev_unspare = B_FALSE;
5039 vdev_remove_parent(cvd);
5044 * We don't set tvd until now because the parent we just removed
5045 * may have been the previous top-level vdev.
5047 tvd = cvd->vdev_top;
5048 ASSERT(tvd->vdev_parent == rvd);
5051 * Reevaluate the parent vdev state.
5053 vdev_propagate_state(cvd);
5056 * If the 'autoexpand' property is set on the pool then automatically
5057 * try to expand the size of the pool. For example if the device we
5058 * just detached was smaller than the others, it may be possible to
5059 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5060 * first so that we can obtain the updated sizes of the leaf vdevs.
5062 if (spa->spa_autoexpand) {
5064 vdev_expand(tvd, txg);
5067 vdev_config_dirty(tvd);
5070 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5071 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5072 * But first make sure we're not on any *other* txg's DTL list, to
5073 * prevent vd from being accessed after it's freed.
5075 vdpath = spa_strdup(vd->vdev_path);
5076 for (int t = 0; t < TXG_SIZE; t++)
5077 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5078 vd->vdev_detached = B_TRUE;
5079 vdev_dirty(tvd, VDD_DTL, vd, txg);
5081 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5083 /* hang on to the spa before we release the lock */
5084 spa_open_ref(spa, FTAG);
5086 error = spa_vdev_exit(spa, vd, txg, 0);
5088 spa_history_log_internal(spa, "detach", NULL,
5090 spa_strfree(vdpath);
5093 * If this was the removal of the original device in a hot spare vdev,
5094 * then we want to go through and remove the device from the hot spare
5095 * list of every other pool.
5098 spa_t *altspa = NULL;
5100 mutex_enter(&spa_namespace_lock);
5101 while ((altspa = spa_next(altspa)) != NULL) {
5102 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5106 spa_open_ref(altspa, FTAG);
5107 mutex_exit(&spa_namespace_lock);
5108 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5109 mutex_enter(&spa_namespace_lock);
5110 spa_close(altspa, FTAG);
5112 mutex_exit(&spa_namespace_lock);
5114 /* search the rest of the vdevs for spares to remove */
5115 spa_vdev_resilver_done(spa);
5118 /* all done with the spa; OK to release */
5119 mutex_enter(&spa_namespace_lock);
5120 spa_close(spa, FTAG);
5121 mutex_exit(&spa_namespace_lock);
5127 * Split a set of devices from their mirrors, and create a new pool from them.
5130 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5131 nvlist_t *props, boolean_t exp)
5134 uint64_t txg, *glist;
5136 uint_t c, children, lastlog;
5137 nvlist_t **child, *nvl, *tmp;
5139 char *altroot = NULL;
5140 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5141 boolean_t activate_slog;
5143 ASSERT(spa_writeable(spa));
5145 txg = spa_vdev_enter(spa);
5147 /* clear the log and flush everything up to now */
5148 activate_slog = spa_passivate_log(spa);
5149 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5150 error = spa_offline_log(spa);
5151 txg = spa_vdev_config_enter(spa);
5154 spa_activate_log(spa);
5157 return (spa_vdev_exit(spa, NULL, txg, error));
5159 /* check new spa name before going any further */
5160 if (spa_lookup(newname) != NULL)
5161 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5164 * scan through all the children to ensure they're all mirrors
5166 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5167 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5169 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5171 /* first, check to ensure we've got the right child count */
5172 rvd = spa->spa_root_vdev;
5174 for (c = 0; c < rvd->vdev_children; c++) {
5175 vdev_t *vd = rvd->vdev_child[c];
5177 /* don't count the holes & logs as children */
5178 if (vd->vdev_islog || vd->vdev_ishole) {
5186 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5187 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5189 /* next, ensure no spare or cache devices are part of the split */
5190 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5191 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5192 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5194 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5195 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5197 /* then, loop over each vdev and validate it */
5198 for (c = 0; c < children; c++) {
5199 uint64_t is_hole = 0;
5201 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5205 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5206 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5209 error = SET_ERROR(EINVAL);
5214 /* which disk is going to be split? */
5215 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5217 error = SET_ERROR(EINVAL);
5221 /* look it up in the spa */
5222 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5223 if (vml[c] == NULL) {
5224 error = SET_ERROR(ENODEV);
5228 /* make sure there's nothing stopping the split */
5229 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5230 vml[c]->vdev_islog ||
5231 vml[c]->vdev_ishole ||
5232 vml[c]->vdev_isspare ||
5233 vml[c]->vdev_isl2cache ||
5234 !vdev_writeable(vml[c]) ||
5235 vml[c]->vdev_children != 0 ||
5236 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5237 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5238 error = SET_ERROR(EINVAL);
5242 if (vdev_dtl_required(vml[c])) {
5243 error = SET_ERROR(EBUSY);
5247 /* we need certain info from the top level */
5248 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5249 vml[c]->vdev_top->vdev_ms_array) == 0);
5250 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5251 vml[c]->vdev_top->vdev_ms_shift) == 0);
5252 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5253 vml[c]->vdev_top->vdev_asize) == 0);
5254 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5255 vml[c]->vdev_top->vdev_ashift) == 0);
5259 kmem_free(vml, children * sizeof (vdev_t *));
5260 kmem_free(glist, children * sizeof (uint64_t));
5261 return (spa_vdev_exit(spa, NULL, txg, error));
5264 /* stop writers from using the disks */
5265 for (c = 0; c < children; c++) {
5267 vml[c]->vdev_offline = B_TRUE;
5269 vdev_reopen(spa->spa_root_vdev);
5272 * Temporarily record the splitting vdevs in the spa config. This
5273 * will disappear once the config is regenerated.
5275 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5276 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5277 glist, children) == 0);
5278 kmem_free(glist, children * sizeof (uint64_t));
5280 mutex_enter(&spa->spa_props_lock);
5281 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5283 mutex_exit(&spa->spa_props_lock);
5284 spa->spa_config_splitting = nvl;
5285 vdev_config_dirty(spa->spa_root_vdev);
5287 /* configure and create the new pool */
5288 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5289 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5290 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5291 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5292 spa_version(spa)) == 0);
5293 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5294 spa->spa_config_txg) == 0);
5295 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5296 spa_generate_guid(NULL)) == 0);
5297 (void) nvlist_lookup_string(props,
5298 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5300 /* add the new pool to the namespace */
5301 newspa = spa_add(newname, config, altroot);
5302 newspa->spa_config_txg = spa->spa_config_txg;
5303 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5305 /* release the spa config lock, retaining the namespace lock */
5306 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5308 if (zio_injection_enabled)
5309 zio_handle_panic_injection(spa, FTAG, 1);
5311 spa_activate(newspa, spa_mode_global);
5312 spa_async_suspend(newspa);
5315 /* mark that we are creating new spa by splitting */
5316 newspa->spa_splitting_newspa = B_TRUE;
5318 /* create the new pool from the disks of the original pool */
5319 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5321 newspa->spa_splitting_newspa = B_FALSE;
5326 /* if that worked, generate a real config for the new pool */
5327 if (newspa->spa_root_vdev != NULL) {
5328 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5329 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5330 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5331 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5332 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5337 if (props != NULL) {
5338 spa_configfile_set(newspa, props, B_FALSE);
5339 error = spa_prop_set(newspa, props);
5344 /* flush everything */
5345 txg = spa_vdev_config_enter(newspa);
5346 vdev_config_dirty(newspa->spa_root_vdev);
5347 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5349 if (zio_injection_enabled)
5350 zio_handle_panic_injection(spa, FTAG, 2);
5352 spa_async_resume(newspa);
5354 /* finally, update the original pool's config */
5355 txg = spa_vdev_config_enter(spa);
5356 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5357 error = dmu_tx_assign(tx, TXG_WAIT);
5360 for (c = 0; c < children; c++) {
5361 if (vml[c] != NULL) {
5364 spa_history_log_internal(spa, "detach", tx,
5365 "vdev=%s", vml[c]->vdev_path);
5369 vdev_config_dirty(spa->spa_root_vdev);
5370 spa->spa_config_splitting = NULL;
5374 (void) spa_vdev_exit(spa, NULL, txg, 0);
5376 if (zio_injection_enabled)
5377 zio_handle_panic_injection(spa, FTAG, 3);
5379 /* split is complete; log a history record */
5380 spa_history_log_internal(newspa, "split", NULL,
5381 "from pool %s", spa_name(spa));
5383 kmem_free(vml, children * sizeof (vdev_t *));
5385 /* if we're not going to mount the filesystems in userland, export */
5387 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5394 spa_deactivate(newspa);
5397 txg = spa_vdev_config_enter(spa);
5399 /* re-online all offlined disks */
5400 for (c = 0; c < children; c++) {
5402 vml[c]->vdev_offline = B_FALSE;
5404 vdev_reopen(spa->spa_root_vdev);
5406 nvlist_free(spa->spa_config_splitting);
5407 spa->spa_config_splitting = NULL;
5408 (void) spa_vdev_exit(spa, NULL, txg, error);
5410 kmem_free(vml, children * sizeof (vdev_t *));
5415 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5417 for (int i = 0; i < count; i++) {
5420 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5423 if (guid == target_guid)
5431 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5432 nvlist_t *dev_to_remove)
5434 nvlist_t **newdev = NULL;
5437 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5439 for (int i = 0, j = 0; i < count; i++) {
5440 if (dev[i] == dev_to_remove)
5442 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5445 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5446 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5448 for (int i = 0; i < count - 1; i++)
5449 nvlist_free(newdev[i]);
5452 kmem_free(newdev, (count - 1) * sizeof (void *));
5456 * Evacuate the device.
5459 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5464 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5465 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5466 ASSERT(vd == vd->vdev_top);
5469 * Evacuate the device. We don't hold the config lock as writer
5470 * since we need to do I/O but we do keep the
5471 * spa_namespace_lock held. Once this completes the device
5472 * should no longer have any blocks allocated on it.
5474 if (vd->vdev_islog) {
5475 if (vd->vdev_stat.vs_alloc != 0)
5476 error = spa_offline_log(spa);
5478 error = SET_ERROR(ENOTSUP);
5485 * The evacuation succeeded. Remove any remaining MOS metadata
5486 * associated with this vdev, and wait for these changes to sync.
5488 ASSERT0(vd->vdev_stat.vs_alloc);
5489 txg = spa_vdev_config_enter(spa);
5490 vd->vdev_removing = B_TRUE;
5491 vdev_dirty_leaves(vd, VDD_DTL, txg);
5492 vdev_config_dirty(vd);
5493 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5499 * Complete the removal by cleaning up the namespace.
5502 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5504 vdev_t *rvd = spa->spa_root_vdev;
5505 uint64_t id = vd->vdev_id;
5506 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5508 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5509 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5510 ASSERT(vd == vd->vdev_top);
5513 * Only remove any devices which are empty.
5515 if (vd->vdev_stat.vs_alloc != 0)
5518 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5520 if (list_link_active(&vd->vdev_state_dirty_node))
5521 vdev_state_clean(vd);
5522 if (list_link_active(&vd->vdev_config_dirty_node))
5523 vdev_config_clean(vd);
5528 vdev_compact_children(rvd);
5530 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5531 vdev_add_child(rvd, vd);
5533 vdev_config_dirty(rvd);
5536 * Reassess the health of our root vdev.
5542 * Remove a device from the pool -
5544 * Removing a device from the vdev namespace requires several steps
5545 * and can take a significant amount of time. As a result we use
5546 * the spa_vdev_config_[enter/exit] functions which allow us to
5547 * grab and release the spa_config_lock while still holding the namespace
5548 * lock. During each step the configuration is synced out.
5550 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5554 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5557 metaslab_group_t *mg;
5558 nvlist_t **spares, **l2cache, *nv;
5560 uint_t nspares, nl2cache;
5562 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5564 ASSERT(spa_writeable(spa));
5567 txg = spa_vdev_enter(spa);
5569 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5571 if (spa->spa_spares.sav_vdevs != NULL &&
5572 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5573 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5574 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5576 * Only remove the hot spare if it's not currently in use
5579 if (vd == NULL || unspare) {
5580 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5581 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5582 spa_load_spares(spa);
5583 spa->spa_spares.sav_sync = B_TRUE;
5585 error = SET_ERROR(EBUSY);
5587 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5588 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5589 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5590 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5592 * Cache devices can always be removed.
5594 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5595 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5596 spa_load_l2cache(spa);
5597 spa->spa_l2cache.sav_sync = B_TRUE;
5598 } else if (vd != NULL && vd->vdev_islog) {
5600 ASSERT(vd == vd->vdev_top);
5605 * Stop allocating from this vdev.
5607 metaslab_group_passivate(mg);
5610 * Wait for the youngest allocations and frees to sync,
5611 * and then wait for the deferral of those frees to finish.
5613 spa_vdev_config_exit(spa, NULL,
5614 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5617 * Attempt to evacuate the vdev.
5619 error = spa_vdev_remove_evacuate(spa, vd);
5621 txg = spa_vdev_config_enter(spa);
5624 * If we couldn't evacuate the vdev, unwind.
5627 metaslab_group_activate(mg);
5628 return (spa_vdev_exit(spa, NULL, txg, error));
5632 * Clean up the vdev namespace.
5634 spa_vdev_remove_from_namespace(spa, vd);
5636 } else if (vd != NULL) {
5638 * Normal vdevs cannot be removed (yet).
5640 error = SET_ERROR(ENOTSUP);
5643 * There is no vdev of any kind with the specified guid.
5645 error = SET_ERROR(ENOENT);
5649 return (spa_vdev_exit(spa, NULL, txg, error));
5655 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5656 * currently spared, so we can detach it.
5659 spa_vdev_resilver_done_hunt(vdev_t *vd)
5661 vdev_t *newvd, *oldvd;
5663 for (int c = 0; c < vd->vdev_children; c++) {
5664 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5670 * Check for a completed replacement. We always consider the first
5671 * vdev in the list to be the oldest vdev, and the last one to be
5672 * the newest (see spa_vdev_attach() for how that works). In
5673 * the case where the newest vdev is faulted, we will not automatically
5674 * remove it after a resilver completes. This is OK as it will require
5675 * user intervention to determine which disk the admin wishes to keep.
5677 if (vd->vdev_ops == &vdev_replacing_ops) {
5678 ASSERT(vd->vdev_children > 1);
5680 newvd = vd->vdev_child[vd->vdev_children - 1];
5681 oldvd = vd->vdev_child[0];
5683 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5684 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5685 !vdev_dtl_required(oldvd))
5690 * Check for a completed resilver with the 'unspare' flag set.
5692 if (vd->vdev_ops == &vdev_spare_ops) {
5693 vdev_t *first = vd->vdev_child[0];
5694 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5696 if (last->vdev_unspare) {
5699 } else if (first->vdev_unspare) {
5706 if (oldvd != NULL &&
5707 vdev_dtl_empty(newvd, DTL_MISSING) &&
5708 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5709 !vdev_dtl_required(oldvd))
5713 * If there are more than two spares attached to a disk,
5714 * and those spares are not required, then we want to
5715 * attempt to free them up now so that they can be used
5716 * by other pools. Once we're back down to a single
5717 * disk+spare, we stop removing them.
5719 if (vd->vdev_children > 2) {
5720 newvd = vd->vdev_child[1];
5722 if (newvd->vdev_isspare && last->vdev_isspare &&
5723 vdev_dtl_empty(last, DTL_MISSING) &&
5724 vdev_dtl_empty(last, DTL_OUTAGE) &&
5725 !vdev_dtl_required(newvd))
5734 spa_vdev_resilver_done(spa_t *spa)
5736 vdev_t *vd, *pvd, *ppvd;
5737 uint64_t guid, sguid, pguid, ppguid;
5739 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5741 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5742 pvd = vd->vdev_parent;
5743 ppvd = pvd->vdev_parent;
5744 guid = vd->vdev_guid;
5745 pguid = pvd->vdev_guid;
5746 ppguid = ppvd->vdev_guid;
5749 * If we have just finished replacing a hot spared device, then
5750 * we need to detach the parent's first child (the original hot
5753 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5754 ppvd->vdev_children == 2) {
5755 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5756 sguid = ppvd->vdev_child[1]->vdev_guid;
5758 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5760 spa_config_exit(spa, SCL_ALL, FTAG);
5761 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5763 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5765 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5768 spa_config_exit(spa, SCL_ALL, FTAG);
5772 * Update the stored path or FRU for this vdev.
5775 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5779 boolean_t sync = B_FALSE;
5781 ASSERT(spa_writeable(spa));
5783 spa_vdev_state_enter(spa, SCL_ALL);
5785 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5786 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5788 if (!vd->vdev_ops->vdev_op_leaf)
5789 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5792 if (strcmp(value, vd->vdev_path) != 0) {
5793 spa_strfree(vd->vdev_path);
5794 vd->vdev_path = spa_strdup(value);
5798 if (vd->vdev_fru == NULL) {
5799 vd->vdev_fru = spa_strdup(value);
5801 } else if (strcmp(value, vd->vdev_fru) != 0) {
5802 spa_strfree(vd->vdev_fru);
5803 vd->vdev_fru = spa_strdup(value);
5808 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5812 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5814 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5818 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5820 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5824 * ==========================================================================
5826 * ==========================================================================
5830 spa_scan_stop(spa_t *spa)
5832 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5833 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5834 return (SET_ERROR(EBUSY));
5835 return (dsl_scan_cancel(spa->spa_dsl_pool));
5839 spa_scan(spa_t *spa, pool_scan_func_t func)
5841 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5843 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5844 return (SET_ERROR(ENOTSUP));
5847 * If a resilver was requested, but there is no DTL on a
5848 * writeable leaf device, we have nothing to do.
5850 if (func == POOL_SCAN_RESILVER &&
5851 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5852 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5856 return (dsl_scan(spa->spa_dsl_pool, func));
5860 * ==========================================================================
5861 * SPA async task processing
5862 * ==========================================================================
5866 spa_async_remove(spa_t *spa, vdev_t *vd)
5868 if (vd->vdev_remove_wanted) {
5869 vd->vdev_remove_wanted = B_FALSE;
5870 vd->vdev_delayed_close = B_FALSE;
5871 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5874 * We want to clear the stats, but we don't want to do a full
5875 * vdev_clear() as that will cause us to throw away
5876 * degraded/faulted state as well as attempt to reopen the
5877 * device, all of which is a waste.
5879 vd->vdev_stat.vs_read_errors = 0;
5880 vd->vdev_stat.vs_write_errors = 0;
5881 vd->vdev_stat.vs_checksum_errors = 0;
5883 vdev_state_dirty(vd->vdev_top);
5886 for (int c = 0; c < vd->vdev_children; c++)
5887 spa_async_remove(spa, vd->vdev_child[c]);
5891 spa_async_probe(spa_t *spa, vdev_t *vd)
5893 if (vd->vdev_probe_wanted) {
5894 vd->vdev_probe_wanted = B_FALSE;
5895 vdev_reopen(vd); /* vdev_open() does the actual probe */
5898 for (int c = 0; c < vd->vdev_children; c++)
5899 spa_async_probe(spa, vd->vdev_child[c]);
5903 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5909 if (!spa->spa_autoexpand)
5912 for (int c = 0; c < vd->vdev_children; c++) {
5913 vdev_t *cvd = vd->vdev_child[c];
5914 spa_async_autoexpand(spa, cvd);
5917 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5920 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5921 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5923 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5924 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5926 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5927 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5930 kmem_free(physpath, MAXPATHLEN);
5934 spa_async_thread(void *arg)
5939 ASSERT(spa->spa_sync_on);
5941 mutex_enter(&spa->spa_async_lock);
5942 tasks = spa->spa_async_tasks;
5943 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
5944 mutex_exit(&spa->spa_async_lock);
5947 * See if the config needs to be updated.
5949 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5950 uint64_t old_space, new_space;
5952 mutex_enter(&spa_namespace_lock);
5953 old_space = metaslab_class_get_space(spa_normal_class(spa));
5954 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5955 new_space = metaslab_class_get_space(spa_normal_class(spa));
5956 mutex_exit(&spa_namespace_lock);
5959 * If the pool grew as a result of the config update,
5960 * then log an internal history event.
5962 if (new_space != old_space) {
5963 spa_history_log_internal(spa, "vdev online", NULL,
5964 "pool '%s' size: %llu(+%llu)",
5965 spa_name(spa), new_space, new_space - old_space);
5969 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5970 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5971 spa_async_autoexpand(spa, spa->spa_root_vdev);
5972 spa_config_exit(spa, SCL_CONFIG, FTAG);
5976 * See if any devices need to be probed.
5978 if (tasks & SPA_ASYNC_PROBE) {
5979 spa_vdev_state_enter(spa, SCL_NONE);
5980 spa_async_probe(spa, spa->spa_root_vdev);
5981 (void) spa_vdev_state_exit(spa, NULL, 0);
5985 * If any devices are done replacing, detach them.
5987 if (tasks & SPA_ASYNC_RESILVER_DONE)
5988 spa_vdev_resilver_done(spa);
5991 * Kick off a resilver.
5993 if (tasks & SPA_ASYNC_RESILVER)
5994 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5997 * Let the world know that we're done.
5999 mutex_enter(&spa->spa_async_lock);
6000 spa->spa_async_thread = NULL;
6001 cv_broadcast(&spa->spa_async_cv);
6002 mutex_exit(&spa->spa_async_lock);
6007 spa_async_thread_vd(void *arg)
6012 ASSERT(spa->spa_sync_on);
6014 mutex_enter(&spa->spa_async_lock);
6015 tasks = spa->spa_async_tasks;
6017 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6018 mutex_exit(&spa->spa_async_lock);
6021 * See if any devices need to be marked REMOVED.
6023 if (tasks & SPA_ASYNC_REMOVE) {
6024 spa_vdev_state_enter(spa, SCL_NONE);
6025 spa_async_remove(spa, spa->spa_root_vdev);
6026 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6027 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6028 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6029 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6030 (void) spa_vdev_state_exit(spa, NULL, 0);
6034 * Let the world know that we're done.
6036 mutex_enter(&spa->spa_async_lock);
6037 tasks = spa->spa_async_tasks;
6038 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6040 spa->spa_async_thread_vd = NULL;
6041 cv_broadcast(&spa->spa_async_cv);
6042 mutex_exit(&spa->spa_async_lock);
6047 spa_async_suspend(spa_t *spa)
6049 mutex_enter(&spa->spa_async_lock);
6050 spa->spa_async_suspended++;
6051 while (spa->spa_async_thread != NULL &&
6052 spa->spa_async_thread_vd != NULL)
6053 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6054 mutex_exit(&spa->spa_async_lock);
6058 spa_async_resume(spa_t *spa)
6060 mutex_enter(&spa->spa_async_lock);
6061 ASSERT(spa->spa_async_suspended != 0);
6062 spa->spa_async_suspended--;
6063 mutex_exit(&spa->spa_async_lock);
6067 spa_async_tasks_pending(spa_t *spa)
6069 uint_t non_config_tasks;
6071 boolean_t config_task_suspended;
6073 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6075 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6076 if (spa->spa_ccw_fail_time == 0) {
6077 config_task_suspended = B_FALSE;
6079 config_task_suspended =
6080 (gethrtime() - spa->spa_ccw_fail_time) <
6081 (zfs_ccw_retry_interval * NANOSEC);
6084 return (non_config_tasks || (config_task && !config_task_suspended));
6088 spa_async_dispatch(spa_t *spa)
6090 mutex_enter(&spa->spa_async_lock);
6091 if (spa_async_tasks_pending(spa) &&
6092 !spa->spa_async_suspended &&
6093 spa->spa_async_thread == NULL &&
6095 spa->spa_async_thread = thread_create(NULL, 0,
6096 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6097 mutex_exit(&spa->spa_async_lock);
6101 spa_async_dispatch_vd(spa_t *spa)
6103 mutex_enter(&spa->spa_async_lock);
6104 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6105 !spa->spa_async_suspended &&
6106 spa->spa_async_thread_vd == NULL &&
6108 spa->spa_async_thread_vd = thread_create(NULL, 0,
6109 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6110 mutex_exit(&spa->spa_async_lock);
6114 spa_async_request(spa_t *spa, int task)
6116 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6117 mutex_enter(&spa->spa_async_lock);
6118 spa->spa_async_tasks |= task;
6119 mutex_exit(&spa->spa_async_lock);
6120 spa_async_dispatch_vd(spa);
6124 * ==========================================================================
6125 * SPA syncing routines
6126 * ==========================================================================
6130 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6133 bpobj_enqueue(bpo, bp, tx);
6138 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6142 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6143 BP_GET_PSIZE(bp), zio->io_flags));
6148 * Note: this simple function is not inlined to make it easier to dtrace the
6149 * amount of time spent syncing frees.
6152 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6154 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6155 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6156 VERIFY(zio_wait(zio) == 0);
6160 * Note: this simple function is not inlined to make it easier to dtrace the
6161 * amount of time spent syncing deferred frees.
6164 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6166 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6167 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6168 spa_free_sync_cb, zio, tx), ==, 0);
6169 VERIFY0(zio_wait(zio));
6174 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6176 char *packed = NULL;
6181 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6184 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6185 * information. This avoids the dmu_buf_will_dirty() path and
6186 * saves us a pre-read to get data we don't actually care about.
6188 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6189 packed = kmem_alloc(bufsize, KM_SLEEP);
6191 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6193 bzero(packed + nvsize, bufsize - nvsize);
6195 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6197 kmem_free(packed, bufsize);
6199 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6200 dmu_buf_will_dirty(db, tx);
6201 *(uint64_t *)db->db_data = nvsize;
6202 dmu_buf_rele(db, FTAG);
6206 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6207 const char *config, const char *entry)
6217 * Update the MOS nvlist describing the list of available devices.
6218 * spa_validate_aux() will have already made sure this nvlist is
6219 * valid and the vdevs are labeled appropriately.
6221 if (sav->sav_object == 0) {
6222 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6223 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6224 sizeof (uint64_t), tx);
6225 VERIFY(zap_update(spa->spa_meta_objset,
6226 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6227 &sav->sav_object, tx) == 0);
6230 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6231 if (sav->sav_count == 0) {
6232 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6234 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6235 for (i = 0; i < sav->sav_count; i++)
6236 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6237 B_FALSE, VDEV_CONFIG_L2CACHE);
6238 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6239 sav->sav_count) == 0);
6240 for (i = 0; i < sav->sav_count; i++)
6241 nvlist_free(list[i]);
6242 kmem_free(list, sav->sav_count * sizeof (void *));
6245 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6246 nvlist_free(nvroot);
6248 sav->sav_sync = B_FALSE;
6252 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6256 if (list_is_empty(&spa->spa_config_dirty_list))
6259 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6261 config = spa_config_generate(spa, spa->spa_root_vdev,
6262 dmu_tx_get_txg(tx), B_FALSE);
6265 * If we're upgrading the spa version then make sure that
6266 * the config object gets updated with the correct version.
6268 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6269 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6270 spa->spa_uberblock.ub_version);
6272 spa_config_exit(spa, SCL_STATE, FTAG);
6274 if (spa->spa_config_syncing)
6275 nvlist_free(spa->spa_config_syncing);
6276 spa->spa_config_syncing = config;
6278 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6282 spa_sync_version(void *arg, dmu_tx_t *tx)
6284 uint64_t *versionp = arg;
6285 uint64_t version = *versionp;
6286 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6289 * Setting the version is special cased when first creating the pool.
6291 ASSERT(tx->tx_txg != TXG_INITIAL);
6293 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6294 ASSERT(version >= spa_version(spa));
6296 spa->spa_uberblock.ub_version = version;
6297 vdev_config_dirty(spa->spa_root_vdev);
6298 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6302 * Set zpool properties.
6305 spa_sync_props(void *arg, dmu_tx_t *tx)
6307 nvlist_t *nvp = arg;
6308 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6309 objset_t *mos = spa->spa_meta_objset;
6310 nvpair_t *elem = NULL;
6312 mutex_enter(&spa->spa_props_lock);
6314 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6316 char *strval, *fname;
6318 const char *propname;
6319 zprop_type_t proptype;
6322 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6325 * We checked this earlier in spa_prop_validate().
6327 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6329 fname = strchr(nvpair_name(elem), '@') + 1;
6330 VERIFY0(zfeature_lookup_name(fname, &fid));
6332 spa_feature_enable(spa, fid, tx);
6333 spa_history_log_internal(spa, "set", tx,
6334 "%s=enabled", nvpair_name(elem));
6337 case ZPOOL_PROP_VERSION:
6338 intval = fnvpair_value_uint64(elem);
6340 * The version is synced seperatly before other
6341 * properties and should be correct by now.
6343 ASSERT3U(spa_version(spa), >=, intval);
6346 case ZPOOL_PROP_ALTROOT:
6348 * 'altroot' is a non-persistent property. It should
6349 * have been set temporarily at creation or import time.
6351 ASSERT(spa->spa_root != NULL);
6354 case ZPOOL_PROP_READONLY:
6355 case ZPOOL_PROP_CACHEFILE:
6357 * 'readonly' and 'cachefile' are also non-persisitent
6361 case ZPOOL_PROP_COMMENT:
6362 strval = fnvpair_value_string(elem);
6363 if (spa->spa_comment != NULL)
6364 spa_strfree(spa->spa_comment);
6365 spa->spa_comment = spa_strdup(strval);
6367 * We need to dirty the configuration on all the vdevs
6368 * so that their labels get updated. It's unnecessary
6369 * to do this for pool creation since the vdev's
6370 * configuratoin has already been dirtied.
6372 if (tx->tx_txg != TXG_INITIAL)
6373 vdev_config_dirty(spa->spa_root_vdev);
6374 spa_history_log_internal(spa, "set", tx,
6375 "%s=%s", nvpair_name(elem), strval);
6379 * Set pool property values in the poolprops mos object.
6381 if (spa->spa_pool_props_object == 0) {
6382 spa->spa_pool_props_object =
6383 zap_create_link(mos, DMU_OT_POOL_PROPS,
6384 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6388 /* normalize the property name */
6389 propname = zpool_prop_to_name(prop);
6390 proptype = zpool_prop_get_type(prop);
6392 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6393 ASSERT(proptype == PROP_TYPE_STRING);
6394 strval = fnvpair_value_string(elem);
6395 VERIFY0(zap_update(mos,
6396 spa->spa_pool_props_object, propname,
6397 1, strlen(strval) + 1, strval, tx));
6398 spa_history_log_internal(spa, "set", tx,
6399 "%s=%s", nvpair_name(elem), strval);
6400 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6401 intval = fnvpair_value_uint64(elem);
6403 if (proptype == PROP_TYPE_INDEX) {
6405 VERIFY0(zpool_prop_index_to_string(
6406 prop, intval, &unused));
6408 VERIFY0(zap_update(mos,
6409 spa->spa_pool_props_object, propname,
6410 8, 1, &intval, tx));
6411 spa_history_log_internal(spa, "set", tx,
6412 "%s=%lld", nvpair_name(elem), intval);
6414 ASSERT(0); /* not allowed */
6418 case ZPOOL_PROP_DELEGATION:
6419 spa->spa_delegation = intval;
6421 case ZPOOL_PROP_BOOTFS:
6422 spa->spa_bootfs = intval;
6424 case ZPOOL_PROP_FAILUREMODE:
6425 spa->spa_failmode = intval;
6427 case ZPOOL_PROP_AUTOEXPAND:
6428 spa->spa_autoexpand = intval;
6429 if (tx->tx_txg != TXG_INITIAL)
6430 spa_async_request(spa,
6431 SPA_ASYNC_AUTOEXPAND);
6433 case ZPOOL_PROP_DEDUPDITTO:
6434 spa->spa_dedup_ditto = intval;
6443 mutex_exit(&spa->spa_props_lock);
6447 * Perform one-time upgrade on-disk changes. spa_version() does not
6448 * reflect the new version this txg, so there must be no changes this
6449 * txg to anything that the upgrade code depends on after it executes.
6450 * Therefore this must be called after dsl_pool_sync() does the sync
6454 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6456 dsl_pool_t *dp = spa->spa_dsl_pool;
6458 ASSERT(spa->spa_sync_pass == 1);
6460 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6462 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6463 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6464 dsl_pool_create_origin(dp, tx);
6466 /* Keeping the origin open increases spa_minref */
6467 spa->spa_minref += 3;
6470 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6471 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6472 dsl_pool_upgrade_clones(dp, tx);
6475 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6476 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6477 dsl_pool_upgrade_dir_clones(dp, tx);
6479 /* Keeping the freedir open increases spa_minref */
6480 spa->spa_minref += 3;
6483 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6484 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6485 spa_feature_create_zap_objects(spa, tx);
6489 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6490 * when possibility to use lz4 compression for metadata was added
6491 * Old pools that have this feature enabled must be upgraded to have
6492 * this feature active
6494 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6495 boolean_t lz4_en = spa_feature_is_enabled(spa,
6496 SPA_FEATURE_LZ4_COMPRESS);
6497 boolean_t lz4_ac = spa_feature_is_active(spa,
6498 SPA_FEATURE_LZ4_COMPRESS);
6500 if (lz4_en && !lz4_ac)
6501 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6503 rrw_exit(&dp->dp_config_rwlock, FTAG);
6507 * Sync the specified transaction group. New blocks may be dirtied as
6508 * part of the process, so we iterate until it converges.
6511 spa_sync(spa_t *spa, uint64_t txg)
6513 dsl_pool_t *dp = spa->spa_dsl_pool;
6514 objset_t *mos = spa->spa_meta_objset;
6515 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6516 vdev_t *rvd = spa->spa_root_vdev;
6521 VERIFY(spa_writeable(spa));
6524 * Lock out configuration changes.
6526 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6528 spa->spa_syncing_txg = txg;
6529 spa->spa_sync_pass = 0;
6532 * If there are any pending vdev state changes, convert them
6533 * into config changes that go out with this transaction group.
6535 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6536 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6538 * We need the write lock here because, for aux vdevs,
6539 * calling vdev_config_dirty() modifies sav_config.
6540 * This is ugly and will become unnecessary when we
6541 * eliminate the aux vdev wart by integrating all vdevs
6542 * into the root vdev tree.
6544 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6545 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6546 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6547 vdev_state_clean(vd);
6548 vdev_config_dirty(vd);
6550 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6551 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6553 spa_config_exit(spa, SCL_STATE, FTAG);
6555 tx = dmu_tx_create_assigned(dp, txg);
6557 spa->spa_sync_starttime = gethrtime();
6559 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6560 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6563 callout_reset(&spa->spa_deadman_cycid,
6564 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6569 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6570 * set spa_deflate if we have no raid-z vdevs.
6572 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6573 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6576 for (i = 0; i < rvd->vdev_children; i++) {
6577 vd = rvd->vdev_child[i];
6578 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6581 if (i == rvd->vdev_children) {
6582 spa->spa_deflate = TRUE;
6583 VERIFY(0 == zap_add(spa->spa_meta_objset,
6584 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6585 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6590 * If anything has changed in this txg, or if someone is waiting
6591 * for this txg to sync (eg, spa_vdev_remove()), push the
6592 * deferred frees from the previous txg. If not, leave them
6593 * alone so that we don't generate work on an otherwise idle
6596 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6597 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6598 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6599 ((dsl_scan_active(dp->dp_scan) ||
6600 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6601 spa_sync_deferred_frees(spa, tx);
6605 * Iterate to convergence.
6608 int pass = ++spa->spa_sync_pass;
6610 spa_sync_config_object(spa, tx);
6611 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6612 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6613 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6614 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6615 spa_errlog_sync(spa, txg);
6616 dsl_pool_sync(dp, txg);
6618 if (pass < zfs_sync_pass_deferred_free) {
6619 spa_sync_frees(spa, free_bpl, tx);
6621 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6622 &spa->spa_deferred_bpobj, tx);
6626 dsl_scan_sync(dp, tx);
6628 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6632 spa_sync_upgrades(spa, tx);
6634 } while (dmu_objset_is_dirty(mos, txg));
6637 * Rewrite the vdev configuration (which includes the uberblock)
6638 * to commit the transaction group.
6640 * If there are no dirty vdevs, we sync the uberblock to a few
6641 * random top-level vdevs that are known to be visible in the
6642 * config cache (see spa_vdev_add() for a complete description).
6643 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6647 * We hold SCL_STATE to prevent vdev open/close/etc.
6648 * while we're attempting to write the vdev labels.
6650 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6652 if (list_is_empty(&spa->spa_config_dirty_list)) {
6653 vdev_t *svd[SPA_DVAS_PER_BP];
6655 int children = rvd->vdev_children;
6656 int c0 = spa_get_random(children);
6658 for (int c = 0; c < children; c++) {
6659 vd = rvd->vdev_child[(c0 + c) % children];
6660 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6662 svd[svdcount++] = vd;
6663 if (svdcount == SPA_DVAS_PER_BP)
6666 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6668 error = vdev_config_sync(svd, svdcount, txg,
6671 error = vdev_config_sync(rvd->vdev_child,
6672 rvd->vdev_children, txg, B_FALSE);
6674 error = vdev_config_sync(rvd->vdev_child,
6675 rvd->vdev_children, txg, B_TRUE);
6679 spa->spa_last_synced_guid = rvd->vdev_guid;
6681 spa_config_exit(spa, SCL_STATE, FTAG);
6685 zio_suspend(spa, NULL);
6686 zio_resume_wait(spa);
6691 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6694 callout_drain(&spa->spa_deadman_cycid);
6699 * Clear the dirty config list.
6701 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6702 vdev_config_clean(vd);
6705 * Now that the new config has synced transactionally,
6706 * let it become visible to the config cache.
6708 if (spa->spa_config_syncing != NULL) {
6709 spa_config_set(spa, spa->spa_config_syncing);
6710 spa->spa_config_txg = txg;
6711 spa->spa_config_syncing = NULL;
6714 spa->spa_ubsync = spa->spa_uberblock;
6716 dsl_pool_sync_done(dp, txg);
6719 * Update usable space statistics.
6721 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6722 vdev_sync_done(vd, txg);
6724 spa_update_dspace(spa);
6727 * It had better be the case that we didn't dirty anything
6728 * since vdev_config_sync().
6730 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6731 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6732 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6734 spa->spa_sync_pass = 0;
6736 spa_config_exit(spa, SCL_CONFIG, FTAG);
6738 spa_handle_ignored_writes(spa);
6741 * If any async tasks have been requested, kick them off.
6743 spa_async_dispatch(spa);
6744 spa_async_dispatch_vd(spa);
6748 * Sync all pools. We don't want to hold the namespace lock across these
6749 * operations, so we take a reference on the spa_t and drop the lock during the
6753 spa_sync_allpools(void)
6756 mutex_enter(&spa_namespace_lock);
6757 while ((spa = spa_next(spa)) != NULL) {
6758 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6759 !spa_writeable(spa) || spa_suspended(spa))
6761 spa_open_ref(spa, FTAG);
6762 mutex_exit(&spa_namespace_lock);
6763 txg_wait_synced(spa_get_dsl(spa), 0);
6764 mutex_enter(&spa_namespace_lock);
6765 spa_close(spa, FTAG);
6767 mutex_exit(&spa_namespace_lock);
6771 * ==========================================================================
6772 * Miscellaneous routines
6773 * ==========================================================================
6777 * Remove all pools in the system.
6785 * Remove all cached state. All pools should be closed now,
6786 * so every spa in the AVL tree should be unreferenced.
6788 mutex_enter(&spa_namespace_lock);
6789 while ((spa = spa_next(NULL)) != NULL) {
6791 * Stop async tasks. The async thread may need to detach
6792 * a device that's been replaced, which requires grabbing
6793 * spa_namespace_lock, so we must drop it here.
6795 spa_open_ref(spa, FTAG);
6796 mutex_exit(&spa_namespace_lock);
6797 spa_async_suspend(spa);
6798 mutex_enter(&spa_namespace_lock);
6799 spa_close(spa, FTAG);
6801 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6803 spa_deactivate(spa);
6807 mutex_exit(&spa_namespace_lock);
6811 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6816 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6820 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6821 vd = spa->spa_l2cache.sav_vdevs[i];
6822 if (vd->vdev_guid == guid)
6826 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6827 vd = spa->spa_spares.sav_vdevs[i];
6828 if (vd->vdev_guid == guid)
6837 spa_upgrade(spa_t *spa, uint64_t version)
6839 ASSERT(spa_writeable(spa));
6841 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6844 * This should only be called for a non-faulted pool, and since a
6845 * future version would result in an unopenable pool, this shouldn't be
6848 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6849 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6851 spa->spa_uberblock.ub_version = version;
6852 vdev_config_dirty(spa->spa_root_vdev);
6854 spa_config_exit(spa, SCL_ALL, FTAG);
6856 txg_wait_synced(spa_get_dsl(spa), 0);
6860 spa_has_spare(spa_t *spa, uint64_t guid)
6864 spa_aux_vdev_t *sav = &spa->spa_spares;
6866 for (i = 0; i < sav->sav_count; i++)
6867 if (sav->sav_vdevs[i]->vdev_guid == guid)
6870 for (i = 0; i < sav->sav_npending; i++) {
6871 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6872 &spareguid) == 0 && spareguid == guid)
6880 * Check if a pool has an active shared spare device.
6881 * Note: reference count of an active spare is 2, as a spare and as a replace
6884 spa_has_active_shared_spare(spa_t *spa)
6888 spa_aux_vdev_t *sav = &spa->spa_spares;
6890 for (i = 0; i < sav->sav_count; i++) {
6891 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6892 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6901 * Post a sysevent corresponding to the given event. The 'name' must be one of
6902 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6903 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6904 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6905 * or zdb as real changes.
6908 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6912 sysevent_attr_list_t *attr = NULL;
6913 sysevent_value_t value;
6916 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6919 value.value_type = SE_DATA_TYPE_STRING;
6920 value.value.sv_string = spa_name(spa);
6921 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6924 value.value_type = SE_DATA_TYPE_UINT64;
6925 value.value.sv_uint64 = spa_guid(spa);
6926 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6930 value.value_type = SE_DATA_TYPE_UINT64;
6931 value.value.sv_uint64 = vd->vdev_guid;
6932 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6936 if (vd->vdev_path) {
6937 value.value_type = SE_DATA_TYPE_STRING;
6938 value.value.sv_string = vd->vdev_path;
6939 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6940 &value, SE_SLEEP) != 0)
6945 if (sysevent_attach_attributes(ev, attr) != 0)
6949 (void) log_sysevent(ev, SE_SLEEP, &eid);
6953 sysevent_free_attr(attr);