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;
86 SYSCTL_DECL(_vfs_zfs);
87 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RWTUN, &check_hostid, 0,
88 "Check hostid on import?");
91 * The interval, in seconds, at which failed configuration cache file writes
94 static int zfs_ccw_retry_interval = 300;
96 typedef enum zti_modes {
97 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
98 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
99 ZTI_MODE_NULL, /* don't create a taskq */
103 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
104 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
105 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
107 #define ZTI_N(n) ZTI_P(n, 1)
108 #define ZTI_ONE ZTI_N(1)
110 typedef struct zio_taskq_info {
111 zti_modes_t zti_mode;
116 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
117 "issue", "issue_high", "intr", "intr_high"
121 * This table defines the taskq settings for each ZFS I/O type. When
122 * initializing a pool, we use this table to create an appropriately sized
123 * taskq. Some operations are low volume and therefore have a small, static
124 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
125 * macros. Other operations process a large amount of data; the ZTI_BATCH
126 * macro causes us to create a taskq oriented for throughput. Some operations
127 * are so high frequency and short-lived that the taskq itself can become a a
128 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
129 * additional degree of parallelism specified by the number of threads per-
130 * taskq and the number of taskqs; when dispatching an event in this case, the
131 * particular taskq is chosen at random.
133 * The different taskq priorities are to handle the different contexts (issue
134 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
135 * need to be handled with minimum delay.
137 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
138 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
139 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
140 { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */
141 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
142 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
143 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
144 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
147 static void spa_sync_version(void *arg, dmu_tx_t *tx);
148 static void spa_sync_props(void *arg, dmu_tx_t *tx);
149 static boolean_t spa_has_active_shared_spare(spa_t *spa);
150 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
151 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
153 static void spa_vdev_resilver_done(spa_t *spa);
155 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
157 id_t zio_taskq_psrset_bind = PS_NONE;
160 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
162 uint_t zio_taskq_basedc = 80; /* base duty cycle */
164 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
165 extern int zfs_sync_pass_deferred_free;
168 extern void spa_deadman(void *arg);
172 * This (illegal) pool name is used when temporarily importing a spa_t in order
173 * to get the vdev stats associated with the imported devices.
175 #define TRYIMPORT_NAME "$import"
178 * ==========================================================================
179 * SPA properties routines
180 * ==========================================================================
184 * Add a (source=src, propname=propval) list to an nvlist.
187 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
188 uint64_t intval, zprop_source_t src)
190 const char *propname = zpool_prop_to_name(prop);
193 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
194 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
197 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
199 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
201 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
202 nvlist_free(propval);
206 * Get property values from the spa configuration.
209 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
211 vdev_t *rvd = spa->spa_root_vdev;
212 dsl_pool_t *pool = spa->spa_dsl_pool;
213 uint64_t size, alloc, cap, version;
214 zprop_source_t src = ZPROP_SRC_NONE;
215 spa_config_dirent_t *dp;
216 metaslab_class_t *mc = spa_normal_class(spa);
218 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
221 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
222 size = metaslab_class_get_space(spa_normal_class(spa));
223 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
224 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
225 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
226 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
229 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
230 metaslab_class_fragmentation(mc), src);
231 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
232 metaslab_class_expandable_space(mc), src);
233 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
234 (spa_mode(spa) == FREAD), src);
236 cap = (size == 0) ? 0 : (alloc * 100 / size);
237 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
239 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
240 ddt_get_pool_dedup_ratio(spa), src);
242 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
243 rvd->vdev_state, src);
245 version = spa_version(spa);
246 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
247 src = ZPROP_SRC_DEFAULT;
249 src = ZPROP_SRC_LOCAL;
250 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
255 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
256 * when opening pools before this version freedir will be NULL.
258 if (pool->dp_free_dir != NULL) {
259 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
260 pool->dp_free_dir->dd_phys->dd_used_bytes, src);
262 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
266 if (pool->dp_leak_dir != NULL) {
267 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
268 pool->dp_leak_dir->dd_phys->dd_used_bytes, src);
270 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
275 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
277 if (spa->spa_comment != NULL) {
278 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
282 if (spa->spa_root != NULL)
283 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
286 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
287 if (dp->scd_path == NULL) {
288 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
289 "none", 0, ZPROP_SRC_LOCAL);
290 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
291 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
292 dp->scd_path, 0, ZPROP_SRC_LOCAL);
298 * Get zpool property values.
301 spa_prop_get(spa_t *spa, nvlist_t **nvp)
303 objset_t *mos = spa->spa_meta_objset;
308 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
310 mutex_enter(&spa->spa_props_lock);
313 * Get properties from the spa config.
315 spa_prop_get_config(spa, nvp);
317 /* If no pool property object, no more prop to get. */
318 if (mos == NULL || spa->spa_pool_props_object == 0) {
319 mutex_exit(&spa->spa_props_lock);
324 * Get properties from the MOS pool property object.
326 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
327 (err = zap_cursor_retrieve(&zc, &za)) == 0;
328 zap_cursor_advance(&zc)) {
331 zprop_source_t src = ZPROP_SRC_DEFAULT;
334 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
337 switch (za.za_integer_length) {
339 /* integer property */
340 if (za.za_first_integer !=
341 zpool_prop_default_numeric(prop))
342 src = ZPROP_SRC_LOCAL;
344 if (prop == ZPOOL_PROP_BOOTFS) {
346 dsl_dataset_t *ds = NULL;
348 dp = spa_get_dsl(spa);
349 dsl_pool_config_enter(dp, FTAG);
350 if (err = dsl_dataset_hold_obj(dp,
351 za.za_first_integer, FTAG, &ds)) {
352 dsl_pool_config_exit(dp, FTAG);
357 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
359 dsl_dataset_name(ds, strval);
360 dsl_dataset_rele(ds, FTAG);
361 dsl_pool_config_exit(dp, FTAG);
364 intval = za.za_first_integer;
367 spa_prop_add_list(*nvp, prop, strval, intval, src);
371 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
376 /* string property */
377 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
378 err = zap_lookup(mos, spa->spa_pool_props_object,
379 za.za_name, 1, za.za_num_integers, strval);
381 kmem_free(strval, za.za_num_integers);
384 spa_prop_add_list(*nvp, prop, strval, 0, src);
385 kmem_free(strval, za.za_num_integers);
392 zap_cursor_fini(&zc);
393 mutex_exit(&spa->spa_props_lock);
395 if (err && err != ENOENT) {
405 * Validate the given pool properties nvlist and modify the list
406 * for the property values to be set.
409 spa_prop_validate(spa_t *spa, nvlist_t *props)
412 int error = 0, reset_bootfs = 0;
414 boolean_t has_feature = B_FALSE;
417 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
419 char *strval, *slash, *check, *fname;
420 const char *propname = nvpair_name(elem);
421 zpool_prop_t prop = zpool_name_to_prop(propname);
425 if (!zpool_prop_feature(propname)) {
426 error = SET_ERROR(EINVAL);
431 * Sanitize the input.
433 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
434 error = SET_ERROR(EINVAL);
438 if (nvpair_value_uint64(elem, &intval) != 0) {
439 error = SET_ERROR(EINVAL);
444 error = SET_ERROR(EINVAL);
448 fname = strchr(propname, '@') + 1;
449 if (zfeature_lookup_name(fname, NULL) != 0) {
450 error = SET_ERROR(EINVAL);
454 has_feature = B_TRUE;
457 case ZPOOL_PROP_VERSION:
458 error = nvpair_value_uint64(elem, &intval);
460 (intval < spa_version(spa) ||
461 intval > SPA_VERSION_BEFORE_FEATURES ||
463 error = SET_ERROR(EINVAL);
466 case ZPOOL_PROP_DELEGATION:
467 case ZPOOL_PROP_AUTOREPLACE:
468 case ZPOOL_PROP_LISTSNAPS:
469 case ZPOOL_PROP_AUTOEXPAND:
470 error = nvpair_value_uint64(elem, &intval);
471 if (!error && intval > 1)
472 error = SET_ERROR(EINVAL);
475 case ZPOOL_PROP_BOOTFS:
477 * If the pool version is less than SPA_VERSION_BOOTFS,
478 * or the pool is still being created (version == 0),
479 * the bootfs property cannot be set.
481 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
482 error = SET_ERROR(ENOTSUP);
487 * Make sure the vdev config is bootable
489 if (!vdev_is_bootable(spa->spa_root_vdev)) {
490 error = SET_ERROR(ENOTSUP);
496 error = nvpair_value_string(elem, &strval);
502 if (strval == NULL || strval[0] == '\0') {
503 objnum = zpool_prop_default_numeric(
508 if (error = dmu_objset_hold(strval, FTAG, &os))
511 /* Must be ZPL and not gzip compressed. */
513 if (dmu_objset_type(os) != DMU_OST_ZFS) {
514 error = SET_ERROR(ENOTSUP);
516 dsl_prop_get_int_ds(dmu_objset_ds(os),
517 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
519 !BOOTFS_COMPRESS_VALID(compress)) {
520 error = SET_ERROR(ENOTSUP);
522 objnum = dmu_objset_id(os);
524 dmu_objset_rele(os, FTAG);
528 case ZPOOL_PROP_FAILUREMODE:
529 error = nvpair_value_uint64(elem, &intval);
530 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
531 intval > ZIO_FAILURE_MODE_PANIC))
532 error = SET_ERROR(EINVAL);
535 * This is a special case which only occurs when
536 * the pool has completely failed. This allows
537 * the user to change the in-core failmode property
538 * without syncing it out to disk (I/Os might
539 * currently be blocked). We do this by returning
540 * EIO to the caller (spa_prop_set) to trick it
541 * into thinking we encountered a property validation
544 if (!error && spa_suspended(spa)) {
545 spa->spa_failmode = intval;
546 error = SET_ERROR(EIO);
550 case ZPOOL_PROP_CACHEFILE:
551 if ((error = nvpair_value_string(elem, &strval)) != 0)
554 if (strval[0] == '\0')
557 if (strcmp(strval, "none") == 0)
560 if (strval[0] != '/') {
561 error = SET_ERROR(EINVAL);
565 slash = strrchr(strval, '/');
566 ASSERT(slash != NULL);
568 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
569 strcmp(slash, "/..") == 0)
570 error = SET_ERROR(EINVAL);
573 case ZPOOL_PROP_COMMENT:
574 if ((error = nvpair_value_string(elem, &strval)) != 0)
576 for (check = strval; *check != '\0'; check++) {
578 * The kernel doesn't have an easy isprint()
579 * check. For this kernel check, we merely
580 * check ASCII apart from DEL. Fix this if
581 * there is an easy-to-use kernel isprint().
583 if (*check >= 0x7f) {
584 error = SET_ERROR(EINVAL);
589 if (strlen(strval) > ZPROP_MAX_COMMENT)
593 case ZPOOL_PROP_DEDUPDITTO:
594 if (spa_version(spa) < SPA_VERSION_DEDUP)
595 error = SET_ERROR(ENOTSUP);
597 error = nvpair_value_uint64(elem, &intval);
599 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
600 error = SET_ERROR(EINVAL);
608 if (!error && reset_bootfs) {
609 error = nvlist_remove(props,
610 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
613 error = nvlist_add_uint64(props,
614 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
622 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
625 spa_config_dirent_t *dp;
627 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
631 dp = kmem_alloc(sizeof (spa_config_dirent_t),
634 if (cachefile[0] == '\0')
635 dp->scd_path = spa_strdup(spa_config_path);
636 else if (strcmp(cachefile, "none") == 0)
639 dp->scd_path = spa_strdup(cachefile);
641 list_insert_head(&spa->spa_config_list, dp);
643 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
647 spa_prop_set(spa_t *spa, nvlist_t *nvp)
650 nvpair_t *elem = NULL;
651 boolean_t need_sync = B_FALSE;
653 if ((error = spa_prop_validate(spa, nvp)) != 0)
656 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
657 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
659 if (prop == ZPOOL_PROP_CACHEFILE ||
660 prop == ZPOOL_PROP_ALTROOT ||
661 prop == ZPOOL_PROP_READONLY)
664 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
667 if (prop == ZPOOL_PROP_VERSION) {
668 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
670 ASSERT(zpool_prop_feature(nvpair_name(elem)));
671 ver = SPA_VERSION_FEATURES;
675 /* Save time if the version is already set. */
676 if (ver == spa_version(spa))
680 * In addition to the pool directory object, we might
681 * create the pool properties object, the features for
682 * read object, the features for write object, or the
683 * feature descriptions object.
685 error = dsl_sync_task(spa->spa_name, NULL,
686 spa_sync_version, &ver,
687 6, ZFS_SPACE_CHECK_RESERVED);
698 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
699 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
706 * If the bootfs property value is dsobj, clear it.
709 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
711 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
712 VERIFY(zap_remove(spa->spa_meta_objset,
713 spa->spa_pool_props_object,
714 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
721 spa_change_guid_check(void *arg, dmu_tx_t *tx)
723 uint64_t *newguid = arg;
724 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
725 vdev_t *rvd = spa->spa_root_vdev;
728 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
729 vdev_state = rvd->vdev_state;
730 spa_config_exit(spa, SCL_STATE, FTAG);
732 if (vdev_state != VDEV_STATE_HEALTHY)
733 return (SET_ERROR(ENXIO));
735 ASSERT3U(spa_guid(spa), !=, *newguid);
741 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
743 uint64_t *newguid = arg;
744 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
746 vdev_t *rvd = spa->spa_root_vdev;
748 oldguid = spa_guid(spa);
750 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
751 rvd->vdev_guid = *newguid;
752 rvd->vdev_guid_sum += (*newguid - oldguid);
753 vdev_config_dirty(rvd);
754 spa_config_exit(spa, SCL_STATE, FTAG);
756 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
761 * Change the GUID for the pool. This is done so that we can later
762 * re-import a pool built from a clone of our own vdevs. We will modify
763 * the root vdev's guid, our own pool guid, and then mark all of our
764 * vdevs dirty. Note that we must make sure that all our vdevs are
765 * online when we do this, or else any vdevs that weren't present
766 * would be orphaned from our pool. We are also going to issue a
767 * sysevent to update any watchers.
770 spa_change_guid(spa_t *spa)
775 mutex_enter(&spa->spa_vdev_top_lock);
776 mutex_enter(&spa_namespace_lock);
777 guid = spa_generate_guid(NULL);
779 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
780 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
783 spa_config_sync(spa, B_FALSE, B_TRUE);
784 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
787 mutex_exit(&spa_namespace_lock);
788 mutex_exit(&spa->spa_vdev_top_lock);
794 * ==========================================================================
795 * SPA state manipulation (open/create/destroy/import/export)
796 * ==========================================================================
800 spa_error_entry_compare(const void *a, const void *b)
802 spa_error_entry_t *sa = (spa_error_entry_t *)a;
803 spa_error_entry_t *sb = (spa_error_entry_t *)b;
806 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
807 sizeof (zbookmark_phys_t));
818 * Utility function which retrieves copies of the current logs and
819 * re-initializes them in the process.
822 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
824 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
826 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
827 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
829 avl_create(&spa->spa_errlist_scrub,
830 spa_error_entry_compare, sizeof (spa_error_entry_t),
831 offsetof(spa_error_entry_t, se_avl));
832 avl_create(&spa->spa_errlist_last,
833 spa_error_entry_compare, sizeof (spa_error_entry_t),
834 offsetof(spa_error_entry_t, se_avl));
838 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
840 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
841 enum zti_modes mode = ztip->zti_mode;
842 uint_t value = ztip->zti_value;
843 uint_t count = ztip->zti_count;
844 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
847 boolean_t batch = B_FALSE;
849 if (mode == ZTI_MODE_NULL) {
851 tqs->stqs_taskq = NULL;
855 ASSERT3U(count, >, 0);
857 tqs->stqs_count = count;
858 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
862 ASSERT3U(value, >=, 1);
863 value = MAX(value, 1);
868 flags |= TASKQ_THREADS_CPU_PCT;
869 value = zio_taskq_batch_pct;
873 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
875 zio_type_name[t], zio_taskq_types[q], mode, value);
879 for (uint_t i = 0; i < count; i++) {
883 (void) snprintf(name, sizeof (name), "%s_%s_%u",
884 zio_type_name[t], zio_taskq_types[q], i);
886 (void) snprintf(name, sizeof (name), "%s_%s",
887 zio_type_name[t], zio_taskq_types[q]);
891 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
893 flags |= TASKQ_DC_BATCH;
895 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
896 spa->spa_proc, zio_taskq_basedc, flags);
899 pri_t pri = maxclsyspri;
901 * The write issue taskq can be extremely CPU
902 * intensive. Run it at slightly lower priority
903 * than the other taskqs.
905 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
908 tq = taskq_create_proc(name, value, pri, 50,
909 INT_MAX, spa->spa_proc, flags);
914 tqs->stqs_taskq[i] = tq;
919 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
921 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
923 if (tqs->stqs_taskq == NULL) {
924 ASSERT0(tqs->stqs_count);
928 for (uint_t i = 0; i < tqs->stqs_count; i++) {
929 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
930 taskq_destroy(tqs->stqs_taskq[i]);
933 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
934 tqs->stqs_taskq = NULL;
938 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
939 * Note that a type may have multiple discrete taskqs to avoid lock contention
940 * on the taskq itself. In that case we choose which taskq at random by using
941 * the low bits of gethrtime().
944 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
945 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
947 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
950 ASSERT3P(tqs->stqs_taskq, !=, NULL);
951 ASSERT3U(tqs->stqs_count, !=, 0);
953 if (tqs->stqs_count == 1) {
954 tq = tqs->stqs_taskq[0];
957 tq = tqs->stqs_taskq[cpu_ticks() % tqs->stqs_count];
959 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
963 taskq_dispatch_ent(tq, func, arg, flags, ent);
967 spa_create_zio_taskqs(spa_t *spa)
969 for (int t = 0; t < ZIO_TYPES; t++) {
970 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
971 spa_taskqs_init(spa, t, q);
979 spa_thread(void *arg)
984 user_t *pu = PTOU(curproc);
986 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
989 ASSERT(curproc != &p0);
990 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
991 "zpool-%s", spa->spa_name);
992 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
995 /* bind this thread to the requested psrset */
996 if (zio_taskq_psrset_bind != PS_NONE) {
998 mutex_enter(&cpu_lock);
999 mutex_enter(&pidlock);
1000 mutex_enter(&curproc->p_lock);
1002 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1003 0, NULL, NULL) == 0) {
1004 curthread->t_bind_pset = zio_taskq_psrset_bind;
1007 "Couldn't bind process for zfs pool \"%s\" to "
1008 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1011 mutex_exit(&curproc->p_lock);
1012 mutex_exit(&pidlock);
1013 mutex_exit(&cpu_lock);
1019 if (zio_taskq_sysdc) {
1020 sysdc_thread_enter(curthread, 100, 0);
1024 spa->spa_proc = curproc;
1025 spa->spa_did = curthread->t_did;
1027 spa_create_zio_taskqs(spa);
1029 mutex_enter(&spa->spa_proc_lock);
1030 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1032 spa->spa_proc_state = SPA_PROC_ACTIVE;
1033 cv_broadcast(&spa->spa_proc_cv);
1035 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1036 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1037 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1038 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1040 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1041 spa->spa_proc_state = SPA_PROC_GONE;
1042 spa->spa_proc = &p0;
1043 cv_broadcast(&spa->spa_proc_cv);
1044 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1046 mutex_enter(&curproc->p_lock);
1049 #endif /* SPA_PROCESS */
1053 * Activate an uninitialized pool.
1056 spa_activate(spa_t *spa, int mode)
1058 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1060 spa->spa_state = POOL_STATE_ACTIVE;
1061 spa->spa_mode = mode;
1063 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1064 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1066 /* Try to create a covering process */
1067 mutex_enter(&spa->spa_proc_lock);
1068 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1069 ASSERT(spa->spa_proc == &p0);
1073 /* Only create a process if we're going to be around a while. */
1074 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1075 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1077 spa->spa_proc_state = SPA_PROC_CREATED;
1078 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1079 cv_wait(&spa->spa_proc_cv,
1080 &spa->spa_proc_lock);
1082 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1083 ASSERT(spa->spa_proc != &p0);
1084 ASSERT(spa->spa_did != 0);
1088 "Couldn't create process for zfs pool \"%s\"\n",
1093 #endif /* SPA_PROCESS */
1094 mutex_exit(&spa->spa_proc_lock);
1096 /* If we didn't create a process, we need to create our taskqs. */
1097 ASSERT(spa->spa_proc == &p0);
1098 if (spa->spa_proc == &p0) {
1099 spa_create_zio_taskqs(spa);
1103 * Start TRIM thread.
1105 trim_thread_create(spa);
1107 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1108 offsetof(vdev_t, vdev_config_dirty_node));
1109 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1110 offsetof(vdev_t, vdev_state_dirty_node));
1112 txg_list_create(&spa->spa_vdev_txg_list,
1113 offsetof(struct vdev, vdev_txg_node));
1115 avl_create(&spa->spa_errlist_scrub,
1116 spa_error_entry_compare, sizeof (spa_error_entry_t),
1117 offsetof(spa_error_entry_t, se_avl));
1118 avl_create(&spa->spa_errlist_last,
1119 spa_error_entry_compare, sizeof (spa_error_entry_t),
1120 offsetof(spa_error_entry_t, se_avl));
1124 * Opposite of spa_activate().
1127 spa_deactivate(spa_t *spa)
1129 ASSERT(spa->spa_sync_on == B_FALSE);
1130 ASSERT(spa->spa_dsl_pool == NULL);
1131 ASSERT(spa->spa_root_vdev == NULL);
1132 ASSERT(spa->spa_async_zio_root == NULL);
1133 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1136 * Stop TRIM thread in case spa_unload() wasn't called directly
1137 * before spa_deactivate().
1139 trim_thread_destroy(spa);
1141 txg_list_destroy(&spa->spa_vdev_txg_list);
1143 list_destroy(&spa->spa_config_dirty_list);
1144 list_destroy(&spa->spa_state_dirty_list);
1146 for (int t = 0; t < ZIO_TYPES; t++) {
1147 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1148 spa_taskqs_fini(spa, t, q);
1152 metaslab_class_destroy(spa->spa_normal_class);
1153 spa->spa_normal_class = NULL;
1155 metaslab_class_destroy(spa->spa_log_class);
1156 spa->spa_log_class = NULL;
1159 * If this was part of an import or the open otherwise failed, we may
1160 * still have errors left in the queues. Empty them just in case.
1162 spa_errlog_drain(spa);
1164 avl_destroy(&spa->spa_errlist_scrub);
1165 avl_destroy(&spa->spa_errlist_last);
1167 spa->spa_state = POOL_STATE_UNINITIALIZED;
1169 mutex_enter(&spa->spa_proc_lock);
1170 if (spa->spa_proc_state != SPA_PROC_NONE) {
1171 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1172 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1173 cv_broadcast(&spa->spa_proc_cv);
1174 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1175 ASSERT(spa->spa_proc != &p0);
1176 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1178 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1179 spa->spa_proc_state = SPA_PROC_NONE;
1181 ASSERT(spa->spa_proc == &p0);
1182 mutex_exit(&spa->spa_proc_lock);
1186 * We want to make sure spa_thread() has actually exited the ZFS
1187 * module, so that the module can't be unloaded out from underneath
1190 if (spa->spa_did != 0) {
1191 thread_join(spa->spa_did);
1194 #endif /* SPA_PROCESS */
1198 * Verify a pool configuration, and construct the vdev tree appropriately. This
1199 * will create all the necessary vdevs in the appropriate layout, with each vdev
1200 * in the CLOSED state. This will prep the pool before open/creation/import.
1201 * All vdev validation is done by the vdev_alloc() routine.
1204 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1205 uint_t id, int atype)
1211 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1214 if ((*vdp)->vdev_ops->vdev_op_leaf)
1217 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1220 if (error == ENOENT)
1226 return (SET_ERROR(EINVAL));
1229 for (int c = 0; c < children; c++) {
1231 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1239 ASSERT(*vdp != NULL);
1245 * Opposite of spa_load().
1248 spa_unload(spa_t *spa)
1252 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1257 trim_thread_destroy(spa);
1262 spa_async_suspend(spa);
1267 if (spa->spa_sync_on) {
1268 txg_sync_stop(spa->spa_dsl_pool);
1269 spa->spa_sync_on = B_FALSE;
1273 * Wait for any outstanding async I/O to complete.
1275 if (spa->spa_async_zio_root != NULL) {
1276 (void) zio_wait(spa->spa_async_zio_root);
1277 spa->spa_async_zio_root = NULL;
1280 bpobj_close(&spa->spa_deferred_bpobj);
1282 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1287 if (spa->spa_root_vdev)
1288 vdev_free(spa->spa_root_vdev);
1289 ASSERT(spa->spa_root_vdev == NULL);
1292 * Close the dsl pool.
1294 if (spa->spa_dsl_pool) {
1295 dsl_pool_close(spa->spa_dsl_pool);
1296 spa->spa_dsl_pool = NULL;
1297 spa->spa_meta_objset = NULL;
1304 * Drop and purge level 2 cache
1306 spa_l2cache_drop(spa);
1308 for (i = 0; i < spa->spa_spares.sav_count; i++)
1309 vdev_free(spa->spa_spares.sav_vdevs[i]);
1310 if (spa->spa_spares.sav_vdevs) {
1311 kmem_free(spa->spa_spares.sav_vdevs,
1312 spa->spa_spares.sav_count * sizeof (void *));
1313 spa->spa_spares.sav_vdevs = NULL;
1315 if (spa->spa_spares.sav_config) {
1316 nvlist_free(spa->spa_spares.sav_config);
1317 spa->spa_spares.sav_config = NULL;
1319 spa->spa_spares.sav_count = 0;
1321 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1322 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1323 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1325 if (spa->spa_l2cache.sav_vdevs) {
1326 kmem_free(spa->spa_l2cache.sav_vdevs,
1327 spa->spa_l2cache.sav_count * sizeof (void *));
1328 spa->spa_l2cache.sav_vdevs = NULL;
1330 if (spa->spa_l2cache.sav_config) {
1331 nvlist_free(spa->spa_l2cache.sav_config);
1332 spa->spa_l2cache.sav_config = NULL;
1334 spa->spa_l2cache.sav_count = 0;
1336 spa->spa_async_suspended = 0;
1338 if (spa->spa_comment != NULL) {
1339 spa_strfree(spa->spa_comment);
1340 spa->spa_comment = NULL;
1343 spa_config_exit(spa, SCL_ALL, FTAG);
1347 * Load (or re-load) the current list of vdevs describing the active spares for
1348 * this pool. When this is called, we have some form of basic information in
1349 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1350 * then re-generate a more complete list including status information.
1353 spa_load_spares(spa_t *spa)
1360 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1363 * First, close and free any existing spare vdevs.
1365 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1366 vd = spa->spa_spares.sav_vdevs[i];
1368 /* Undo the call to spa_activate() below */
1369 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1370 B_FALSE)) != NULL && tvd->vdev_isspare)
1371 spa_spare_remove(tvd);
1376 if (spa->spa_spares.sav_vdevs)
1377 kmem_free(spa->spa_spares.sav_vdevs,
1378 spa->spa_spares.sav_count * sizeof (void *));
1380 if (spa->spa_spares.sav_config == NULL)
1383 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1384 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1386 spa->spa_spares.sav_count = (int)nspares;
1387 spa->spa_spares.sav_vdevs = NULL;
1393 * Construct the array of vdevs, opening them to get status in the
1394 * process. For each spare, there is potentially two different vdev_t
1395 * structures associated with it: one in the list of spares (used only
1396 * for basic validation purposes) and one in the active vdev
1397 * configuration (if it's spared in). During this phase we open and
1398 * validate each vdev on the spare list. If the vdev also exists in the
1399 * active configuration, then we also mark this vdev as an active spare.
1401 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1403 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1404 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1405 VDEV_ALLOC_SPARE) == 0);
1408 spa->spa_spares.sav_vdevs[i] = vd;
1410 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1411 B_FALSE)) != NULL) {
1412 if (!tvd->vdev_isspare)
1416 * We only mark the spare active if we were successfully
1417 * able to load the vdev. Otherwise, importing a pool
1418 * with a bad active spare would result in strange
1419 * behavior, because multiple pool would think the spare
1420 * is actively in use.
1422 * There is a vulnerability here to an equally bizarre
1423 * circumstance, where a dead active spare is later
1424 * brought back to life (onlined or otherwise). Given
1425 * the rarity of this scenario, and the extra complexity
1426 * it adds, we ignore the possibility.
1428 if (!vdev_is_dead(tvd))
1429 spa_spare_activate(tvd);
1433 vd->vdev_aux = &spa->spa_spares;
1435 if (vdev_open(vd) != 0)
1438 if (vdev_validate_aux(vd) == 0)
1443 * Recompute the stashed list of spares, with status information
1446 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1447 DATA_TYPE_NVLIST_ARRAY) == 0);
1449 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1451 for (i = 0; i < spa->spa_spares.sav_count; i++)
1452 spares[i] = vdev_config_generate(spa,
1453 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1454 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1455 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1456 for (i = 0; i < spa->spa_spares.sav_count; i++)
1457 nvlist_free(spares[i]);
1458 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1462 * Load (or re-load) the current list of vdevs describing the active l2cache for
1463 * this pool. When this is called, we have some form of basic information in
1464 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1465 * then re-generate a more complete list including status information.
1466 * Devices which are already active have their details maintained, and are
1470 spa_load_l2cache(spa_t *spa)
1474 int i, j, oldnvdevs;
1476 vdev_t *vd, **oldvdevs, **newvdevs;
1477 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1479 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1481 if (sav->sav_config != NULL) {
1482 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1483 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1484 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1490 oldvdevs = sav->sav_vdevs;
1491 oldnvdevs = sav->sav_count;
1492 sav->sav_vdevs = NULL;
1496 * Process new nvlist of vdevs.
1498 for (i = 0; i < nl2cache; i++) {
1499 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1503 for (j = 0; j < oldnvdevs; j++) {
1505 if (vd != NULL && guid == vd->vdev_guid) {
1507 * Retain previous vdev for add/remove ops.
1515 if (newvdevs[i] == NULL) {
1519 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1520 VDEV_ALLOC_L2CACHE) == 0);
1525 * Commit this vdev as an l2cache device,
1526 * even if it fails to open.
1528 spa_l2cache_add(vd);
1533 spa_l2cache_activate(vd);
1535 if (vdev_open(vd) != 0)
1538 (void) vdev_validate_aux(vd);
1540 if (!vdev_is_dead(vd))
1541 l2arc_add_vdev(spa, vd);
1546 * Purge vdevs that were dropped
1548 for (i = 0; i < oldnvdevs; i++) {
1553 ASSERT(vd->vdev_isl2cache);
1555 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1556 pool != 0ULL && l2arc_vdev_present(vd))
1557 l2arc_remove_vdev(vd);
1558 vdev_clear_stats(vd);
1564 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1566 if (sav->sav_config == NULL)
1569 sav->sav_vdevs = newvdevs;
1570 sav->sav_count = (int)nl2cache;
1573 * Recompute the stashed list of l2cache devices, with status
1574 * information this time.
1576 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1577 DATA_TYPE_NVLIST_ARRAY) == 0);
1579 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1580 for (i = 0; i < sav->sav_count; i++)
1581 l2cache[i] = vdev_config_generate(spa,
1582 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1583 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1584 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1586 for (i = 0; i < sav->sav_count; i++)
1587 nvlist_free(l2cache[i]);
1589 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1593 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1596 char *packed = NULL;
1601 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1604 nvsize = *(uint64_t *)db->db_data;
1605 dmu_buf_rele(db, FTAG);
1607 packed = kmem_alloc(nvsize, KM_SLEEP);
1608 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1611 error = nvlist_unpack(packed, nvsize, value, 0);
1612 kmem_free(packed, nvsize);
1618 * Checks to see if the given vdev could not be opened, in which case we post a
1619 * sysevent to notify the autoreplace code that the device has been removed.
1622 spa_check_removed(vdev_t *vd)
1624 for (int c = 0; c < vd->vdev_children; c++)
1625 spa_check_removed(vd->vdev_child[c]);
1627 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1629 zfs_post_autoreplace(vd->vdev_spa, vd);
1630 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1635 * Validate the current config against the MOS config
1638 spa_config_valid(spa_t *spa, nvlist_t *config)
1640 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1643 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1645 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1646 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1648 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1651 * If we're doing a normal import, then build up any additional
1652 * diagnostic information about missing devices in this config.
1653 * We'll pass this up to the user for further processing.
1655 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1656 nvlist_t **child, *nv;
1659 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1661 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1663 for (int c = 0; c < rvd->vdev_children; c++) {
1664 vdev_t *tvd = rvd->vdev_child[c];
1665 vdev_t *mtvd = mrvd->vdev_child[c];
1667 if (tvd->vdev_ops == &vdev_missing_ops &&
1668 mtvd->vdev_ops != &vdev_missing_ops &&
1670 child[idx++] = vdev_config_generate(spa, mtvd,
1675 VERIFY(nvlist_add_nvlist_array(nv,
1676 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1677 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1678 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1680 for (int i = 0; i < idx; i++)
1681 nvlist_free(child[i]);
1684 kmem_free(child, rvd->vdev_children * sizeof (char **));
1688 * Compare the root vdev tree with the information we have
1689 * from the MOS config (mrvd). Check each top-level vdev
1690 * with the corresponding MOS config top-level (mtvd).
1692 for (int c = 0; c < rvd->vdev_children; c++) {
1693 vdev_t *tvd = rvd->vdev_child[c];
1694 vdev_t *mtvd = mrvd->vdev_child[c];
1697 * Resolve any "missing" vdevs in the current configuration.
1698 * If we find that the MOS config has more accurate information
1699 * about the top-level vdev then use that vdev instead.
1701 if (tvd->vdev_ops == &vdev_missing_ops &&
1702 mtvd->vdev_ops != &vdev_missing_ops) {
1704 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1708 * Device specific actions.
1710 if (mtvd->vdev_islog) {
1711 spa_set_log_state(spa, SPA_LOG_CLEAR);
1714 * XXX - once we have 'readonly' pool
1715 * support we should be able to handle
1716 * missing data devices by transitioning
1717 * the pool to readonly.
1723 * Swap the missing vdev with the data we were
1724 * able to obtain from the MOS config.
1726 vdev_remove_child(rvd, tvd);
1727 vdev_remove_child(mrvd, mtvd);
1729 vdev_add_child(rvd, mtvd);
1730 vdev_add_child(mrvd, tvd);
1732 spa_config_exit(spa, SCL_ALL, FTAG);
1734 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1737 } else if (mtvd->vdev_islog) {
1739 * Load the slog device's state from the MOS config
1740 * since it's possible that the label does not
1741 * contain the most up-to-date information.
1743 vdev_load_log_state(tvd, mtvd);
1748 spa_config_exit(spa, SCL_ALL, FTAG);
1751 * Ensure we were able to validate the config.
1753 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1757 * Check for missing log devices
1760 spa_check_logs(spa_t *spa)
1762 boolean_t rv = B_FALSE;
1764 switch (spa->spa_log_state) {
1765 case SPA_LOG_MISSING:
1766 /* need to recheck in case slog has been restored */
1767 case SPA_LOG_UNKNOWN:
1768 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1769 NULL, DS_FIND_CHILDREN) != 0);
1771 spa_set_log_state(spa, SPA_LOG_MISSING);
1778 spa_passivate_log(spa_t *spa)
1780 vdev_t *rvd = spa->spa_root_vdev;
1781 boolean_t slog_found = B_FALSE;
1783 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1785 if (!spa_has_slogs(spa))
1788 for (int c = 0; c < rvd->vdev_children; c++) {
1789 vdev_t *tvd = rvd->vdev_child[c];
1790 metaslab_group_t *mg = tvd->vdev_mg;
1792 if (tvd->vdev_islog) {
1793 metaslab_group_passivate(mg);
1794 slog_found = B_TRUE;
1798 return (slog_found);
1802 spa_activate_log(spa_t *spa)
1804 vdev_t *rvd = spa->spa_root_vdev;
1806 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1808 for (int c = 0; c < rvd->vdev_children; c++) {
1809 vdev_t *tvd = rvd->vdev_child[c];
1810 metaslab_group_t *mg = tvd->vdev_mg;
1812 if (tvd->vdev_islog)
1813 metaslab_group_activate(mg);
1818 spa_offline_log(spa_t *spa)
1822 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1823 NULL, DS_FIND_CHILDREN);
1826 * We successfully offlined the log device, sync out the
1827 * current txg so that the "stubby" block can be removed
1830 txg_wait_synced(spa->spa_dsl_pool, 0);
1836 spa_aux_check_removed(spa_aux_vdev_t *sav)
1840 for (i = 0; i < sav->sav_count; i++)
1841 spa_check_removed(sav->sav_vdevs[i]);
1845 spa_claim_notify(zio_t *zio)
1847 spa_t *spa = zio->io_spa;
1852 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1853 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1854 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1855 mutex_exit(&spa->spa_props_lock);
1858 typedef struct spa_load_error {
1859 uint64_t sle_meta_count;
1860 uint64_t sle_data_count;
1864 spa_load_verify_done(zio_t *zio)
1866 blkptr_t *bp = zio->io_bp;
1867 spa_load_error_t *sle = zio->io_private;
1868 dmu_object_type_t type = BP_GET_TYPE(bp);
1869 int error = zio->io_error;
1870 spa_t *spa = zio->io_spa;
1873 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1874 type != DMU_OT_INTENT_LOG)
1875 atomic_add_64(&sle->sle_meta_count, 1);
1877 atomic_add_64(&sle->sle_data_count, 1);
1879 zio_data_buf_free(zio->io_data, zio->io_size);
1881 mutex_enter(&spa->spa_scrub_lock);
1882 spa->spa_scrub_inflight--;
1883 cv_broadcast(&spa->spa_scrub_io_cv);
1884 mutex_exit(&spa->spa_scrub_lock);
1888 * Maximum number of concurrent scrub i/os to create while verifying
1889 * a pool while importing it.
1891 int spa_load_verify_maxinflight = 10000;
1892 boolean_t spa_load_verify_metadata = B_TRUE;
1893 boolean_t spa_load_verify_data = B_TRUE;
1895 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1896 &spa_load_verify_maxinflight, 0,
1897 "Maximum number of concurrent scrub I/Os to create while verifying a "
1898 "pool while importing it");
1900 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1901 &spa_load_verify_metadata, 0,
1902 "Check metadata on import?");
1904 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1905 &spa_load_verify_data, 0,
1906 "Check user data on import?");
1910 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1911 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1913 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1916 * Note: normally this routine will not be called if
1917 * spa_load_verify_metadata is not set. However, it may be useful
1918 * to manually set the flag after the traversal has begun.
1920 if (!spa_load_verify_metadata)
1922 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1926 size_t size = BP_GET_PSIZE(bp);
1927 void *data = zio_data_buf_alloc(size);
1929 mutex_enter(&spa->spa_scrub_lock);
1930 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1931 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1932 spa->spa_scrub_inflight++;
1933 mutex_exit(&spa->spa_scrub_lock);
1935 zio_nowait(zio_read(rio, spa, bp, data, size,
1936 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1937 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1938 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1943 spa_load_verify(spa_t *spa)
1946 spa_load_error_t sle = { 0 };
1947 zpool_rewind_policy_t policy;
1948 boolean_t verify_ok = B_FALSE;
1951 zpool_get_rewind_policy(spa->spa_config, &policy);
1953 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1956 rio = zio_root(spa, NULL, &sle,
1957 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1959 if (spa_load_verify_metadata) {
1960 error = traverse_pool(spa, spa->spa_verify_min_txg,
1961 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
1962 spa_load_verify_cb, rio);
1965 (void) zio_wait(rio);
1967 spa->spa_load_meta_errors = sle.sle_meta_count;
1968 spa->spa_load_data_errors = sle.sle_data_count;
1970 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1971 sle.sle_data_count <= policy.zrp_maxdata) {
1975 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1976 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1978 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1979 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1980 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1981 VERIFY(nvlist_add_int64(spa->spa_load_info,
1982 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1983 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1984 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1986 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1990 if (error != ENXIO && error != EIO)
1991 error = SET_ERROR(EIO);
1995 return (verify_ok ? 0 : EIO);
1999 * Find a value in the pool props object.
2002 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2004 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2005 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2009 * Find a value in the pool directory object.
2012 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2014 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2015 name, sizeof (uint64_t), 1, val));
2019 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2021 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2026 * Fix up config after a partly-completed split. This is done with the
2027 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2028 * pool have that entry in their config, but only the splitting one contains
2029 * a list of all the guids of the vdevs that are being split off.
2031 * This function determines what to do with that list: either rejoin
2032 * all the disks to the pool, or complete the splitting process. To attempt
2033 * the rejoin, each disk that is offlined is marked online again, and
2034 * we do a reopen() call. If the vdev label for every disk that was
2035 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2036 * then we call vdev_split() on each disk, and complete the split.
2038 * Otherwise we leave the config alone, with all the vdevs in place in
2039 * the original pool.
2042 spa_try_repair(spa_t *spa, nvlist_t *config)
2049 boolean_t attempt_reopen;
2051 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2054 /* check that the config is complete */
2055 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2056 &glist, &gcount) != 0)
2059 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2061 /* attempt to online all the vdevs & validate */
2062 attempt_reopen = B_TRUE;
2063 for (i = 0; i < gcount; i++) {
2064 if (glist[i] == 0) /* vdev is hole */
2067 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2068 if (vd[i] == NULL) {
2070 * Don't bother attempting to reopen the disks;
2071 * just do the split.
2073 attempt_reopen = B_FALSE;
2075 /* attempt to re-online it */
2076 vd[i]->vdev_offline = B_FALSE;
2080 if (attempt_reopen) {
2081 vdev_reopen(spa->spa_root_vdev);
2083 /* check each device to see what state it's in */
2084 for (extracted = 0, i = 0; i < gcount; i++) {
2085 if (vd[i] != NULL &&
2086 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2093 * If every disk has been moved to the new pool, or if we never
2094 * even attempted to look at them, then we split them off for
2097 if (!attempt_reopen || gcount == extracted) {
2098 for (i = 0; i < gcount; i++)
2101 vdev_reopen(spa->spa_root_vdev);
2104 kmem_free(vd, gcount * sizeof (vdev_t *));
2108 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2109 boolean_t mosconfig)
2111 nvlist_t *config = spa->spa_config;
2112 char *ereport = FM_EREPORT_ZFS_POOL;
2118 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2119 return (SET_ERROR(EINVAL));
2121 ASSERT(spa->spa_comment == NULL);
2122 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2123 spa->spa_comment = spa_strdup(comment);
2126 * Versioning wasn't explicitly added to the label until later, so if
2127 * it's not present treat it as the initial version.
2129 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2130 &spa->spa_ubsync.ub_version) != 0)
2131 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2133 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2134 &spa->spa_config_txg);
2136 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2137 spa_guid_exists(pool_guid, 0)) {
2138 error = SET_ERROR(EEXIST);
2140 spa->spa_config_guid = pool_guid;
2142 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2144 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2148 nvlist_free(spa->spa_load_info);
2149 spa->spa_load_info = fnvlist_alloc();
2151 gethrestime(&spa->spa_loaded_ts);
2152 error = spa_load_impl(spa, pool_guid, config, state, type,
2153 mosconfig, &ereport);
2156 spa->spa_minref = refcount_count(&spa->spa_refcount);
2158 if (error != EEXIST) {
2159 spa->spa_loaded_ts.tv_sec = 0;
2160 spa->spa_loaded_ts.tv_nsec = 0;
2162 if (error != EBADF) {
2163 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2166 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2173 * Load an existing storage pool, using the pool's builtin spa_config as a
2174 * source of configuration information.
2177 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2178 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2182 nvlist_t *nvroot = NULL;
2185 uberblock_t *ub = &spa->spa_uberblock;
2186 uint64_t children, config_cache_txg = spa->spa_config_txg;
2187 int orig_mode = spa->spa_mode;
2190 boolean_t missing_feat_write = B_FALSE;
2193 * If this is an untrusted config, access the pool in read-only mode.
2194 * This prevents things like resilvering recently removed devices.
2197 spa->spa_mode = FREAD;
2199 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2201 spa->spa_load_state = state;
2203 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2204 return (SET_ERROR(EINVAL));
2206 parse = (type == SPA_IMPORT_EXISTING ?
2207 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2210 * Create "The Godfather" zio to hold all async IOs
2212 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2213 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2216 * Parse the configuration into a vdev tree. We explicitly set the
2217 * value that will be returned by spa_version() since parsing the
2218 * configuration requires knowing the version number.
2220 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2221 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2222 spa_config_exit(spa, SCL_ALL, FTAG);
2227 ASSERT(spa->spa_root_vdev == rvd);
2229 if (type != SPA_IMPORT_ASSEMBLE) {
2230 ASSERT(spa_guid(spa) == pool_guid);
2234 * Try to open all vdevs, loading each label in the process.
2236 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2237 error = vdev_open(rvd);
2238 spa_config_exit(spa, SCL_ALL, FTAG);
2243 * We need to validate the vdev labels against the configuration that
2244 * we have in hand, which is dependent on the setting of mosconfig. If
2245 * mosconfig is true then we're validating the vdev labels based on
2246 * that config. Otherwise, we're validating against the cached config
2247 * (zpool.cache) that was read when we loaded the zfs module, and then
2248 * later we will recursively call spa_load() and validate against
2251 * If we're assembling a new pool that's been split off from an
2252 * existing pool, the labels haven't yet been updated so we skip
2253 * validation for now.
2255 if (type != SPA_IMPORT_ASSEMBLE) {
2256 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2257 error = vdev_validate(rvd, mosconfig);
2258 spa_config_exit(spa, SCL_ALL, FTAG);
2263 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2264 return (SET_ERROR(ENXIO));
2268 * Find the best uberblock.
2270 vdev_uberblock_load(rvd, ub, &label);
2273 * If we weren't able to find a single valid uberblock, return failure.
2275 if (ub->ub_txg == 0) {
2277 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2281 * If the pool has an unsupported version we can't open it.
2283 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2285 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2288 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2292 * If we weren't able to find what's necessary for reading the
2293 * MOS in the label, return failure.
2295 if (label == NULL || nvlist_lookup_nvlist(label,
2296 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2298 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2303 * Update our in-core representation with the definitive values
2306 nvlist_free(spa->spa_label_features);
2307 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2313 * Look through entries in the label nvlist's features_for_read. If
2314 * there is a feature listed there which we don't understand then we
2315 * cannot open a pool.
2317 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2318 nvlist_t *unsup_feat;
2320 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2323 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2325 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2326 if (!zfeature_is_supported(nvpair_name(nvp))) {
2327 VERIFY(nvlist_add_string(unsup_feat,
2328 nvpair_name(nvp), "") == 0);
2332 if (!nvlist_empty(unsup_feat)) {
2333 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2334 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2335 nvlist_free(unsup_feat);
2336 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2340 nvlist_free(unsup_feat);
2344 * If the vdev guid sum doesn't match the uberblock, we have an
2345 * incomplete configuration. We first check to see if the pool
2346 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2347 * If it is, defer the vdev_guid_sum check till later so we
2348 * can handle missing vdevs.
2350 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2351 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2352 rvd->vdev_guid_sum != ub->ub_guid_sum)
2353 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2355 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2356 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2357 spa_try_repair(spa, config);
2358 spa_config_exit(spa, SCL_ALL, FTAG);
2359 nvlist_free(spa->spa_config_splitting);
2360 spa->spa_config_splitting = NULL;
2364 * Initialize internal SPA structures.
2366 spa->spa_state = POOL_STATE_ACTIVE;
2367 spa->spa_ubsync = spa->spa_uberblock;
2368 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2369 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2370 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2371 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2372 spa->spa_claim_max_txg = spa->spa_first_txg;
2373 spa->spa_prev_software_version = ub->ub_software_version;
2375 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2377 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2378 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2380 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2381 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2383 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2384 boolean_t missing_feat_read = B_FALSE;
2385 nvlist_t *unsup_feat, *enabled_feat;
2387 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2388 &spa->spa_feat_for_read_obj) != 0) {
2389 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2392 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2393 &spa->spa_feat_for_write_obj) != 0) {
2394 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2397 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2398 &spa->spa_feat_desc_obj) != 0) {
2399 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2402 enabled_feat = fnvlist_alloc();
2403 unsup_feat = fnvlist_alloc();
2405 if (!spa_features_check(spa, B_FALSE,
2406 unsup_feat, enabled_feat))
2407 missing_feat_read = B_TRUE;
2409 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2410 if (!spa_features_check(spa, B_TRUE,
2411 unsup_feat, enabled_feat)) {
2412 missing_feat_write = B_TRUE;
2416 fnvlist_add_nvlist(spa->spa_load_info,
2417 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2419 if (!nvlist_empty(unsup_feat)) {
2420 fnvlist_add_nvlist(spa->spa_load_info,
2421 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2424 fnvlist_free(enabled_feat);
2425 fnvlist_free(unsup_feat);
2427 if (!missing_feat_read) {
2428 fnvlist_add_boolean(spa->spa_load_info,
2429 ZPOOL_CONFIG_CAN_RDONLY);
2433 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2434 * twofold: to determine whether the pool is available for
2435 * import in read-write mode and (if it is not) whether the
2436 * pool is available for import in read-only mode. If the pool
2437 * is available for import in read-write mode, it is displayed
2438 * as available in userland; if it is not available for import
2439 * in read-only mode, it is displayed as unavailable in
2440 * userland. If the pool is available for import in read-only
2441 * mode but not read-write mode, it is displayed as unavailable
2442 * in userland with a special note that the pool is actually
2443 * available for open in read-only mode.
2445 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2446 * missing a feature for write, we must first determine whether
2447 * the pool can be opened read-only before returning to
2448 * userland in order to know whether to display the
2449 * abovementioned note.
2451 if (missing_feat_read || (missing_feat_write &&
2452 spa_writeable(spa))) {
2453 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2458 * Load refcounts for ZFS features from disk into an in-memory
2459 * cache during SPA initialization.
2461 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2464 error = feature_get_refcount_from_disk(spa,
2465 &spa_feature_table[i], &refcount);
2467 spa->spa_feat_refcount_cache[i] = refcount;
2468 } else if (error == ENOTSUP) {
2469 spa->spa_feat_refcount_cache[i] =
2470 SPA_FEATURE_DISABLED;
2472 return (spa_vdev_err(rvd,
2473 VDEV_AUX_CORRUPT_DATA, EIO));
2478 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2479 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2480 &spa->spa_feat_enabled_txg_obj) != 0)
2481 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2484 spa->spa_is_initializing = B_TRUE;
2485 error = dsl_pool_open(spa->spa_dsl_pool);
2486 spa->spa_is_initializing = B_FALSE;
2488 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2492 nvlist_t *policy = NULL, *nvconfig;
2494 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2495 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2497 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2498 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2500 unsigned long myhostid = 0;
2502 VERIFY(nvlist_lookup_string(nvconfig,
2503 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2506 myhostid = zone_get_hostid(NULL);
2509 * We're emulating the system's hostid in userland, so
2510 * we can't use zone_get_hostid().
2512 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2513 #endif /* _KERNEL */
2514 if (check_hostid && hostid != 0 && myhostid != 0 &&
2515 hostid != myhostid) {
2516 nvlist_free(nvconfig);
2517 cmn_err(CE_WARN, "pool '%s' could not be "
2518 "loaded as it was last accessed by "
2519 "another system (host: %s hostid: 0x%lx). "
2520 "See: http://illumos.org/msg/ZFS-8000-EY",
2521 spa_name(spa), hostname,
2522 (unsigned long)hostid);
2523 return (SET_ERROR(EBADF));
2526 if (nvlist_lookup_nvlist(spa->spa_config,
2527 ZPOOL_REWIND_POLICY, &policy) == 0)
2528 VERIFY(nvlist_add_nvlist(nvconfig,
2529 ZPOOL_REWIND_POLICY, policy) == 0);
2531 spa_config_set(spa, nvconfig);
2533 spa_deactivate(spa);
2534 spa_activate(spa, orig_mode);
2536 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2539 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2540 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2541 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2543 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2546 * Load the bit that tells us to use the new accounting function
2547 * (raid-z deflation). If we have an older pool, this will not
2550 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2551 if (error != 0 && error != ENOENT)
2552 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2554 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2555 &spa->spa_creation_version);
2556 if (error != 0 && error != ENOENT)
2557 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2560 * Load the persistent error log. If we have an older pool, this will
2563 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2564 if (error != 0 && error != ENOENT)
2565 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2567 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2568 &spa->spa_errlog_scrub);
2569 if (error != 0 && error != ENOENT)
2570 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2573 * Load the history object. If we have an older pool, this
2574 * will not be present.
2576 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2577 if (error != 0 && error != ENOENT)
2578 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2581 * If we're assembling the pool from the split-off vdevs of
2582 * an existing pool, we don't want to attach the spares & cache
2587 * Load any hot spares for this pool.
2589 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2590 if (error != 0 && error != ENOENT)
2591 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2592 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2593 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2594 if (load_nvlist(spa, spa->spa_spares.sav_object,
2595 &spa->spa_spares.sav_config) != 0)
2596 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2598 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2599 spa_load_spares(spa);
2600 spa_config_exit(spa, SCL_ALL, FTAG);
2601 } else if (error == 0) {
2602 spa->spa_spares.sav_sync = B_TRUE;
2606 * Load any level 2 ARC devices for this pool.
2608 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2609 &spa->spa_l2cache.sav_object);
2610 if (error != 0 && error != ENOENT)
2611 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2612 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2613 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2614 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2615 &spa->spa_l2cache.sav_config) != 0)
2616 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2618 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2619 spa_load_l2cache(spa);
2620 spa_config_exit(spa, SCL_ALL, FTAG);
2621 } else if (error == 0) {
2622 spa->spa_l2cache.sav_sync = B_TRUE;
2625 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2627 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2628 if (error && error != ENOENT)
2629 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2632 uint64_t autoreplace;
2634 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2635 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2636 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2637 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2638 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2639 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2640 &spa->spa_dedup_ditto);
2642 spa->spa_autoreplace = (autoreplace != 0);
2646 * If the 'autoreplace' property is set, then post a resource notifying
2647 * the ZFS DE that it should not issue any faults for unopenable
2648 * devices. We also iterate over the vdevs, and post a sysevent for any
2649 * unopenable vdevs so that the normal autoreplace handler can take
2652 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2653 spa_check_removed(spa->spa_root_vdev);
2655 * For the import case, this is done in spa_import(), because
2656 * at this point we're using the spare definitions from
2657 * the MOS config, not necessarily from the userland config.
2659 if (state != SPA_LOAD_IMPORT) {
2660 spa_aux_check_removed(&spa->spa_spares);
2661 spa_aux_check_removed(&spa->spa_l2cache);
2666 * Load the vdev state for all toplevel vdevs.
2671 * Propagate the leaf DTLs we just loaded all the way up the tree.
2673 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2674 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2675 spa_config_exit(spa, SCL_ALL, FTAG);
2678 * Load the DDTs (dedup tables).
2680 error = ddt_load(spa);
2682 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2684 spa_update_dspace(spa);
2687 * Validate the config, using the MOS config to fill in any
2688 * information which might be missing. If we fail to validate
2689 * the config then declare the pool unfit for use. If we're
2690 * assembling a pool from a split, the log is not transferred
2693 if (type != SPA_IMPORT_ASSEMBLE) {
2696 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2697 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2699 if (!spa_config_valid(spa, nvconfig)) {
2700 nvlist_free(nvconfig);
2701 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2704 nvlist_free(nvconfig);
2707 * Now that we've validated the config, check the state of the
2708 * root vdev. If it can't be opened, it indicates one or
2709 * more toplevel vdevs are faulted.
2711 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2712 return (SET_ERROR(ENXIO));
2714 if (spa_check_logs(spa)) {
2715 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2716 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2720 if (missing_feat_write) {
2721 ASSERT(state == SPA_LOAD_TRYIMPORT);
2724 * At this point, we know that we can open the pool in
2725 * read-only mode but not read-write mode. We now have enough
2726 * information and can return to userland.
2728 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2732 * We've successfully opened the pool, verify that we're ready
2733 * to start pushing transactions.
2735 if (state != SPA_LOAD_TRYIMPORT) {
2736 if (error = spa_load_verify(spa))
2737 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2741 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2742 spa->spa_load_max_txg == UINT64_MAX)) {
2744 int need_update = B_FALSE;
2746 ASSERT(state != SPA_LOAD_TRYIMPORT);
2749 * Claim log blocks that haven't been committed yet.
2750 * This must all happen in a single txg.
2751 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2752 * invoked from zil_claim_log_block()'s i/o done callback.
2753 * Price of rollback is that we abandon the log.
2755 spa->spa_claiming = B_TRUE;
2757 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2758 spa_first_txg(spa));
2759 (void) dmu_objset_find(spa_name(spa),
2760 zil_claim, tx, DS_FIND_CHILDREN);
2763 spa->spa_claiming = B_FALSE;
2765 spa_set_log_state(spa, SPA_LOG_GOOD);
2766 spa->spa_sync_on = B_TRUE;
2767 txg_sync_start(spa->spa_dsl_pool);
2770 * Wait for all claims to sync. We sync up to the highest
2771 * claimed log block birth time so that claimed log blocks
2772 * don't appear to be from the future. spa_claim_max_txg
2773 * will have been set for us by either zil_check_log_chain()
2774 * (invoked from spa_check_logs()) or zil_claim() above.
2776 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2779 * If the config cache is stale, or we have uninitialized
2780 * metaslabs (see spa_vdev_add()), then update the config.
2782 * If this is a verbatim import, trust the current
2783 * in-core spa_config and update the disk labels.
2785 if (config_cache_txg != spa->spa_config_txg ||
2786 state == SPA_LOAD_IMPORT ||
2787 state == SPA_LOAD_RECOVER ||
2788 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2789 need_update = B_TRUE;
2791 for (int c = 0; c < rvd->vdev_children; c++)
2792 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2793 need_update = B_TRUE;
2796 * Update the config cache asychronously in case we're the
2797 * root pool, in which case the config cache isn't writable yet.
2800 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2803 * Check all DTLs to see if anything needs resilvering.
2805 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2806 vdev_resilver_needed(rvd, NULL, NULL))
2807 spa_async_request(spa, SPA_ASYNC_RESILVER);
2810 * Log the fact that we booted up (so that we can detect if
2811 * we rebooted in the middle of an operation).
2813 spa_history_log_version(spa, "open");
2816 * Delete any inconsistent datasets.
2818 (void) dmu_objset_find(spa_name(spa),
2819 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2822 * Clean up any stale temporary dataset userrefs.
2824 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2831 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2833 int mode = spa->spa_mode;
2836 spa_deactivate(spa);
2838 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2840 spa_activate(spa, mode);
2841 spa_async_suspend(spa);
2843 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2847 * If spa_load() fails this function will try loading prior txg's. If
2848 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2849 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2850 * function will not rewind the pool and will return the same error as
2854 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2855 uint64_t max_request, int rewind_flags)
2857 nvlist_t *loadinfo = NULL;
2858 nvlist_t *config = NULL;
2859 int load_error, rewind_error;
2860 uint64_t safe_rewind_txg;
2863 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2864 spa->spa_load_max_txg = spa->spa_load_txg;
2865 spa_set_log_state(spa, SPA_LOG_CLEAR);
2867 spa->spa_load_max_txg = max_request;
2868 if (max_request != UINT64_MAX)
2869 spa->spa_extreme_rewind = B_TRUE;
2872 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2874 if (load_error == 0)
2877 if (spa->spa_root_vdev != NULL)
2878 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2880 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2881 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2883 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2884 nvlist_free(config);
2885 return (load_error);
2888 if (state == SPA_LOAD_RECOVER) {
2889 /* Price of rolling back is discarding txgs, including log */
2890 spa_set_log_state(spa, SPA_LOG_CLEAR);
2893 * If we aren't rolling back save the load info from our first
2894 * import attempt so that we can restore it after attempting
2897 loadinfo = spa->spa_load_info;
2898 spa->spa_load_info = fnvlist_alloc();
2901 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2902 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2903 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2904 TXG_INITIAL : safe_rewind_txg;
2907 * Continue as long as we're finding errors, we're still within
2908 * the acceptable rewind range, and we're still finding uberblocks
2910 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2911 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2912 if (spa->spa_load_max_txg < safe_rewind_txg)
2913 spa->spa_extreme_rewind = B_TRUE;
2914 rewind_error = spa_load_retry(spa, state, mosconfig);
2917 spa->spa_extreme_rewind = B_FALSE;
2918 spa->spa_load_max_txg = UINT64_MAX;
2920 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2921 spa_config_set(spa, config);
2923 if (state == SPA_LOAD_RECOVER) {
2924 ASSERT3P(loadinfo, ==, NULL);
2925 return (rewind_error);
2927 /* Store the rewind info as part of the initial load info */
2928 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2929 spa->spa_load_info);
2931 /* Restore the initial load info */
2932 fnvlist_free(spa->spa_load_info);
2933 spa->spa_load_info = loadinfo;
2935 return (load_error);
2942 * The import case is identical to an open except that the configuration is sent
2943 * down from userland, instead of grabbed from the configuration cache. For the
2944 * case of an open, the pool configuration will exist in the
2945 * POOL_STATE_UNINITIALIZED state.
2947 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2948 * the same time open the pool, without having to keep around the spa_t in some
2952 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2956 spa_load_state_t state = SPA_LOAD_OPEN;
2958 int locked = B_FALSE;
2959 int firstopen = B_FALSE;
2964 * As disgusting as this is, we need to support recursive calls to this
2965 * function because dsl_dir_open() is called during spa_load(), and ends
2966 * up calling spa_open() again. The real fix is to figure out how to
2967 * avoid dsl_dir_open() calling this in the first place.
2969 if (mutex_owner(&spa_namespace_lock) != curthread) {
2970 mutex_enter(&spa_namespace_lock);
2974 if ((spa = spa_lookup(pool)) == NULL) {
2976 mutex_exit(&spa_namespace_lock);
2977 return (SET_ERROR(ENOENT));
2980 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2981 zpool_rewind_policy_t policy;
2985 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2987 if (policy.zrp_request & ZPOOL_DO_REWIND)
2988 state = SPA_LOAD_RECOVER;
2990 spa_activate(spa, spa_mode_global);
2992 if (state != SPA_LOAD_RECOVER)
2993 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2995 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2996 policy.zrp_request);
2998 if (error == EBADF) {
3000 * If vdev_validate() returns failure (indicated by
3001 * EBADF), it indicates that one of the vdevs indicates
3002 * that the pool has been exported or destroyed. If
3003 * this is the case, the config cache is out of sync and
3004 * we should remove the pool from the namespace.
3007 spa_deactivate(spa);
3008 spa_config_sync(spa, B_TRUE, B_TRUE);
3011 mutex_exit(&spa_namespace_lock);
3012 return (SET_ERROR(ENOENT));
3017 * We can't open the pool, but we still have useful
3018 * information: the state of each vdev after the
3019 * attempted vdev_open(). Return this to the user.
3021 if (config != NULL && spa->spa_config) {
3022 VERIFY(nvlist_dup(spa->spa_config, config,
3024 VERIFY(nvlist_add_nvlist(*config,
3025 ZPOOL_CONFIG_LOAD_INFO,
3026 spa->spa_load_info) == 0);
3029 spa_deactivate(spa);
3030 spa->spa_last_open_failed = error;
3032 mutex_exit(&spa_namespace_lock);
3038 spa_open_ref(spa, tag);
3041 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3044 * If we've recovered the pool, pass back any information we
3045 * gathered while doing the load.
3047 if (state == SPA_LOAD_RECOVER) {
3048 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3049 spa->spa_load_info) == 0);
3053 spa->spa_last_open_failed = 0;
3054 spa->spa_last_ubsync_txg = 0;
3055 spa->spa_load_txg = 0;
3056 mutex_exit(&spa_namespace_lock);
3060 zvol_create_minors(spa->spa_name);
3071 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3074 return (spa_open_common(name, spapp, tag, policy, config));
3078 spa_open(const char *name, spa_t **spapp, void *tag)
3080 return (spa_open_common(name, spapp, tag, NULL, NULL));
3084 * Lookup the given spa_t, incrementing the inject count in the process,
3085 * preventing it from being exported or destroyed.
3088 spa_inject_addref(char *name)
3092 mutex_enter(&spa_namespace_lock);
3093 if ((spa = spa_lookup(name)) == NULL) {
3094 mutex_exit(&spa_namespace_lock);
3097 spa->spa_inject_ref++;
3098 mutex_exit(&spa_namespace_lock);
3104 spa_inject_delref(spa_t *spa)
3106 mutex_enter(&spa_namespace_lock);
3107 spa->spa_inject_ref--;
3108 mutex_exit(&spa_namespace_lock);
3112 * Add spares device information to the nvlist.
3115 spa_add_spares(spa_t *spa, nvlist_t *config)
3125 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3127 if (spa->spa_spares.sav_count == 0)
3130 VERIFY(nvlist_lookup_nvlist(config,
3131 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3132 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3133 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3135 VERIFY(nvlist_add_nvlist_array(nvroot,
3136 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3137 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3138 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3141 * Go through and find any spares which have since been
3142 * repurposed as an active spare. If this is the case, update
3143 * their status appropriately.
3145 for (i = 0; i < nspares; i++) {
3146 VERIFY(nvlist_lookup_uint64(spares[i],
3147 ZPOOL_CONFIG_GUID, &guid) == 0);
3148 if (spa_spare_exists(guid, &pool, NULL) &&
3150 VERIFY(nvlist_lookup_uint64_array(
3151 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3152 (uint64_t **)&vs, &vsc) == 0);
3153 vs->vs_state = VDEV_STATE_CANT_OPEN;
3154 vs->vs_aux = VDEV_AUX_SPARED;
3161 * Add l2cache device information to the nvlist, including vdev stats.
3164 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3167 uint_t i, j, nl2cache;
3174 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3176 if (spa->spa_l2cache.sav_count == 0)
3179 VERIFY(nvlist_lookup_nvlist(config,
3180 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3181 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3182 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3183 if (nl2cache != 0) {
3184 VERIFY(nvlist_add_nvlist_array(nvroot,
3185 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3186 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3187 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3190 * Update level 2 cache device stats.
3193 for (i = 0; i < nl2cache; i++) {
3194 VERIFY(nvlist_lookup_uint64(l2cache[i],
3195 ZPOOL_CONFIG_GUID, &guid) == 0);
3198 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3200 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3201 vd = spa->spa_l2cache.sav_vdevs[j];
3207 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3208 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3210 vdev_get_stats(vd, vs);
3216 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3222 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3223 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3225 /* We may be unable to read features if pool is suspended. */
3226 if (spa_suspended(spa))
3229 if (spa->spa_feat_for_read_obj != 0) {
3230 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3231 spa->spa_feat_for_read_obj);
3232 zap_cursor_retrieve(&zc, &za) == 0;
3233 zap_cursor_advance(&zc)) {
3234 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3235 za.za_num_integers == 1);
3236 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3237 za.za_first_integer));
3239 zap_cursor_fini(&zc);
3242 if (spa->spa_feat_for_write_obj != 0) {
3243 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3244 spa->spa_feat_for_write_obj);
3245 zap_cursor_retrieve(&zc, &za) == 0;
3246 zap_cursor_advance(&zc)) {
3247 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3248 za.za_num_integers == 1);
3249 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3250 za.za_first_integer));
3252 zap_cursor_fini(&zc);
3256 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3258 nvlist_free(features);
3262 spa_get_stats(const char *name, nvlist_t **config,
3263 char *altroot, size_t buflen)
3269 error = spa_open_common(name, &spa, FTAG, NULL, config);
3273 * This still leaves a window of inconsistency where the spares
3274 * or l2cache devices could change and the config would be
3275 * self-inconsistent.
3277 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3279 if (*config != NULL) {
3280 uint64_t loadtimes[2];
3282 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3283 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3284 VERIFY(nvlist_add_uint64_array(*config,
3285 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3287 VERIFY(nvlist_add_uint64(*config,
3288 ZPOOL_CONFIG_ERRCOUNT,
3289 spa_get_errlog_size(spa)) == 0);
3291 if (spa_suspended(spa))
3292 VERIFY(nvlist_add_uint64(*config,
3293 ZPOOL_CONFIG_SUSPENDED,
3294 spa->spa_failmode) == 0);
3296 spa_add_spares(spa, *config);
3297 spa_add_l2cache(spa, *config);
3298 spa_add_feature_stats(spa, *config);
3303 * We want to get the alternate root even for faulted pools, so we cheat
3304 * and call spa_lookup() directly.
3308 mutex_enter(&spa_namespace_lock);
3309 spa = spa_lookup(name);
3311 spa_altroot(spa, altroot, buflen);
3315 mutex_exit(&spa_namespace_lock);
3317 spa_altroot(spa, altroot, buflen);
3322 spa_config_exit(spa, SCL_CONFIG, FTAG);
3323 spa_close(spa, FTAG);
3330 * Validate that the auxiliary device array is well formed. We must have an
3331 * array of nvlists, each which describes a valid leaf vdev. If this is an
3332 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3333 * specified, as long as they are well-formed.
3336 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3337 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3338 vdev_labeltype_t label)
3345 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3348 * It's acceptable to have no devs specified.
3350 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3354 return (SET_ERROR(EINVAL));
3357 * Make sure the pool is formatted with a version that supports this
3360 if (spa_version(spa) < version)
3361 return (SET_ERROR(ENOTSUP));
3364 * Set the pending device list so we correctly handle device in-use
3367 sav->sav_pending = dev;
3368 sav->sav_npending = ndev;
3370 for (i = 0; i < ndev; i++) {
3371 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3375 if (!vd->vdev_ops->vdev_op_leaf) {
3377 error = SET_ERROR(EINVAL);
3382 * The L2ARC currently only supports disk devices in
3383 * kernel context. For user-level testing, we allow it.
3386 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3387 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3388 error = SET_ERROR(ENOTBLK);
3395 if ((error = vdev_open(vd)) == 0 &&
3396 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3397 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3398 vd->vdev_guid) == 0);
3404 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3411 sav->sav_pending = NULL;
3412 sav->sav_npending = 0;
3417 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3421 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3423 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3424 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3425 VDEV_LABEL_SPARE)) != 0) {
3429 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3430 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3431 VDEV_LABEL_L2CACHE));
3435 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3440 if (sav->sav_config != NULL) {
3446 * Generate new dev list by concatentating with the
3449 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3450 &olddevs, &oldndevs) == 0);
3452 newdevs = kmem_alloc(sizeof (void *) *
3453 (ndevs + oldndevs), KM_SLEEP);
3454 for (i = 0; i < oldndevs; i++)
3455 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3457 for (i = 0; i < ndevs; i++)
3458 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3461 VERIFY(nvlist_remove(sav->sav_config, config,
3462 DATA_TYPE_NVLIST_ARRAY) == 0);
3464 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3465 config, newdevs, ndevs + oldndevs) == 0);
3466 for (i = 0; i < oldndevs + ndevs; i++)
3467 nvlist_free(newdevs[i]);
3468 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3471 * Generate a new dev list.
3473 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3475 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3481 * Stop and drop level 2 ARC devices
3484 spa_l2cache_drop(spa_t *spa)
3488 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3490 for (i = 0; i < sav->sav_count; i++) {
3493 vd = sav->sav_vdevs[i];
3496 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3497 pool != 0ULL && l2arc_vdev_present(vd))
3498 l2arc_remove_vdev(vd);
3506 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3510 char *altroot = NULL;
3515 uint64_t txg = TXG_INITIAL;
3516 nvlist_t **spares, **l2cache;
3517 uint_t nspares, nl2cache;
3518 uint64_t version, obj;
3519 boolean_t has_features;
3522 * If this pool already exists, return failure.
3524 mutex_enter(&spa_namespace_lock);
3525 if (spa_lookup(pool) != NULL) {
3526 mutex_exit(&spa_namespace_lock);
3527 return (SET_ERROR(EEXIST));
3531 * Allocate a new spa_t structure.
3533 (void) nvlist_lookup_string(props,
3534 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3535 spa = spa_add(pool, NULL, altroot);
3536 spa_activate(spa, spa_mode_global);
3538 if (props && (error = spa_prop_validate(spa, props))) {
3539 spa_deactivate(spa);
3541 mutex_exit(&spa_namespace_lock);
3545 has_features = B_FALSE;
3546 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3547 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3548 if (zpool_prop_feature(nvpair_name(elem)))
3549 has_features = B_TRUE;
3552 if (has_features || nvlist_lookup_uint64(props,
3553 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3554 version = SPA_VERSION;
3556 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3558 spa->spa_first_txg = txg;
3559 spa->spa_uberblock.ub_txg = txg - 1;
3560 spa->spa_uberblock.ub_version = version;
3561 spa->spa_ubsync = spa->spa_uberblock;
3564 * Create "The Godfather" zio to hold all async IOs
3566 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3567 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3570 * Create the root vdev.
3572 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3574 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3576 ASSERT(error != 0 || rvd != NULL);
3577 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3579 if (error == 0 && !zfs_allocatable_devs(nvroot))
3580 error = SET_ERROR(EINVAL);
3583 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3584 (error = spa_validate_aux(spa, nvroot, txg,
3585 VDEV_ALLOC_ADD)) == 0) {
3586 for (int c = 0; c < rvd->vdev_children; c++) {
3587 vdev_ashift_optimize(rvd->vdev_child[c]);
3588 vdev_metaslab_set_size(rvd->vdev_child[c]);
3589 vdev_expand(rvd->vdev_child[c], txg);
3593 spa_config_exit(spa, SCL_ALL, FTAG);
3597 spa_deactivate(spa);
3599 mutex_exit(&spa_namespace_lock);
3604 * Get the list of spares, if specified.
3606 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3607 &spares, &nspares) == 0) {
3608 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3610 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3611 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3612 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3613 spa_load_spares(spa);
3614 spa_config_exit(spa, SCL_ALL, FTAG);
3615 spa->spa_spares.sav_sync = B_TRUE;
3619 * Get the list of level 2 cache devices, if specified.
3621 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3622 &l2cache, &nl2cache) == 0) {
3623 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3624 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3625 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3626 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3627 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3628 spa_load_l2cache(spa);
3629 spa_config_exit(spa, SCL_ALL, FTAG);
3630 spa->spa_l2cache.sav_sync = B_TRUE;
3633 spa->spa_is_initializing = B_TRUE;
3634 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3635 spa->spa_meta_objset = dp->dp_meta_objset;
3636 spa->spa_is_initializing = B_FALSE;
3639 * Create DDTs (dedup tables).
3643 spa_update_dspace(spa);
3645 tx = dmu_tx_create_assigned(dp, txg);
3648 * Create the pool config object.
3650 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3651 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3652 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3654 if (zap_add(spa->spa_meta_objset,
3655 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3656 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3657 cmn_err(CE_PANIC, "failed to add pool config");
3660 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3661 spa_feature_create_zap_objects(spa, tx);
3663 if (zap_add(spa->spa_meta_objset,
3664 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3665 sizeof (uint64_t), 1, &version, tx) != 0) {
3666 cmn_err(CE_PANIC, "failed to add pool version");
3669 /* Newly created pools with the right version are always deflated. */
3670 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3671 spa->spa_deflate = TRUE;
3672 if (zap_add(spa->spa_meta_objset,
3673 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3674 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3675 cmn_err(CE_PANIC, "failed to add deflate");
3680 * Create the deferred-free bpobj. Turn off compression
3681 * because sync-to-convergence takes longer if the blocksize
3684 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3685 dmu_object_set_compress(spa->spa_meta_objset, obj,
3686 ZIO_COMPRESS_OFF, tx);
3687 if (zap_add(spa->spa_meta_objset,
3688 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3689 sizeof (uint64_t), 1, &obj, tx) != 0) {
3690 cmn_err(CE_PANIC, "failed to add bpobj");
3692 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3693 spa->spa_meta_objset, obj));
3696 * Create the pool's history object.
3698 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3699 spa_history_create_obj(spa, tx);
3702 * Set pool properties.
3704 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3705 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3706 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3707 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3709 if (props != NULL) {
3710 spa_configfile_set(spa, props, B_FALSE);
3711 spa_sync_props(props, tx);
3716 spa->spa_sync_on = B_TRUE;
3717 txg_sync_start(spa->spa_dsl_pool);
3720 * We explicitly wait for the first transaction to complete so that our
3721 * bean counters are appropriately updated.
3723 txg_wait_synced(spa->spa_dsl_pool, txg);
3725 spa_config_sync(spa, B_FALSE, B_TRUE);
3727 spa_history_log_version(spa, "create");
3729 spa->spa_minref = refcount_count(&spa->spa_refcount);
3731 mutex_exit(&spa_namespace_lock);
3739 * Get the root pool information from the root disk, then import the root pool
3740 * during the system boot up time.
3742 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3745 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3748 nvlist_t *nvtop, *nvroot;
3751 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3755 * Add this top-level vdev to the child array.
3757 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3759 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3761 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3764 * Put this pool's top-level vdevs into a root vdev.
3766 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3767 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3768 VDEV_TYPE_ROOT) == 0);
3769 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3770 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3771 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3775 * Replace the existing vdev_tree with the new root vdev in
3776 * this pool's configuration (remove the old, add the new).
3778 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3779 nvlist_free(nvroot);
3784 * Walk the vdev tree and see if we can find a device with "better"
3785 * configuration. A configuration is "better" if the label on that
3786 * device has a more recent txg.
3789 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3791 for (int c = 0; c < vd->vdev_children; c++)
3792 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3794 if (vd->vdev_ops->vdev_op_leaf) {
3798 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3802 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3806 * Do we have a better boot device?
3808 if (label_txg > *txg) {
3817 * Import a root pool.
3819 * For x86. devpath_list will consist of devid and/or physpath name of
3820 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3821 * The GRUB "findroot" command will return the vdev we should boot.
3823 * For Sparc, devpath_list consists the physpath name of the booting device
3824 * no matter the rootpool is a single device pool or a mirrored pool.
3826 * "/pci@1f,0/ide@d/disk@0,0:a"
3829 spa_import_rootpool(char *devpath, char *devid)
3832 vdev_t *rvd, *bvd, *avd = NULL;
3833 nvlist_t *config, *nvtop;
3839 * Read the label from the boot device and generate a configuration.
3841 config = spa_generate_rootconf(devpath, devid, &guid);
3842 #if defined(_OBP) && defined(_KERNEL)
3843 if (config == NULL) {
3844 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3846 get_iscsi_bootpath_phy(devpath);
3847 config = spa_generate_rootconf(devpath, devid, &guid);
3851 if (config == NULL) {
3852 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3854 return (SET_ERROR(EIO));
3857 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3859 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3861 mutex_enter(&spa_namespace_lock);
3862 if ((spa = spa_lookup(pname)) != NULL) {
3864 * Remove the existing root pool from the namespace so that we
3865 * can replace it with the correct config we just read in.
3870 spa = spa_add(pname, config, NULL);
3871 spa->spa_is_root = B_TRUE;
3872 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3875 * Build up a vdev tree based on the boot device's label config.
3877 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3879 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3880 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3881 VDEV_ALLOC_ROOTPOOL);
3882 spa_config_exit(spa, SCL_ALL, FTAG);
3884 mutex_exit(&spa_namespace_lock);
3885 nvlist_free(config);
3886 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3892 * Get the boot vdev.
3894 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3895 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3896 (u_longlong_t)guid);
3897 error = SET_ERROR(ENOENT);
3902 * Determine if there is a better boot device.
3905 spa_alt_rootvdev(rvd, &avd, &txg);
3907 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3908 "try booting from '%s'", avd->vdev_path);
3909 error = SET_ERROR(EINVAL);
3914 * If the boot device is part of a spare vdev then ensure that
3915 * we're booting off the active spare.
3917 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3918 !bvd->vdev_isspare) {
3919 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3920 "try booting from '%s'",
3922 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3923 error = SET_ERROR(EINVAL);
3929 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3931 spa_config_exit(spa, SCL_ALL, FTAG);
3932 mutex_exit(&spa_namespace_lock);
3934 nvlist_free(config);
3940 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3944 spa_generate_rootconf(const char *name)
3946 nvlist_t **configs, **tops;
3948 nvlist_t *best_cfg, *nvtop, *nvroot;
3957 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3960 ASSERT3U(count, !=, 0);
3962 for (i = 0; i < count; i++) {
3965 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
3967 if (txg > best_txg) {
3969 best_cfg = configs[i];
3974 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3976 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
3979 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
3980 for (i = 0; i < nchildren; i++) {
3983 if (configs[i] == NULL)
3985 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
3987 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
3989 for (i = 0; holes != NULL && i < nholes; i++) {
3992 if (tops[holes[i]] != NULL)
3994 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
3995 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
3996 VDEV_TYPE_HOLE) == 0);
3997 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
3999 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4002 for (i = 0; i < nchildren; i++) {
4003 if (tops[i] != NULL)
4005 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4006 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4007 VDEV_TYPE_MISSING) == 0);
4008 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4010 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4015 * Create pool config based on the best vdev config.
4017 nvlist_dup(best_cfg, &config, KM_SLEEP);
4020 * Put this pool's top-level vdevs into a root vdev.
4022 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4024 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4025 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4026 VDEV_TYPE_ROOT) == 0);
4027 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4028 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4029 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4030 tops, nchildren) == 0);
4033 * Replace the existing vdev_tree with the new root vdev in
4034 * this pool's configuration (remove the old, add the new).
4036 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4039 * Drop vdev config elements that should not be present at pool level.
4041 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4042 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4044 for (i = 0; i < count; i++)
4045 nvlist_free(configs[i]);
4046 kmem_free(configs, count * sizeof(void *));
4047 for (i = 0; i < nchildren; i++)
4048 nvlist_free(tops[i]);
4049 kmem_free(tops, nchildren * sizeof(void *));
4050 nvlist_free(nvroot);
4055 spa_import_rootpool(const char *name)
4058 vdev_t *rvd, *bvd, *avd = NULL;
4059 nvlist_t *config, *nvtop;
4065 * Read the label from the boot device and generate a configuration.
4067 config = spa_generate_rootconf(name);
4069 mutex_enter(&spa_namespace_lock);
4070 if (config != NULL) {
4071 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4072 &pname) == 0 && strcmp(name, pname) == 0);
4073 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4076 if ((spa = spa_lookup(pname)) != NULL) {
4078 * Remove the existing root pool from the namespace so
4079 * that we can replace it with the correct config
4084 spa = spa_add(pname, config, NULL);
4087 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4088 * via spa_version().
4090 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4091 &spa->spa_ubsync.ub_version) != 0)
4092 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4093 } else if ((spa = spa_lookup(name)) == NULL) {
4094 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4098 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4100 spa->spa_is_root = B_TRUE;
4101 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4104 * Build up a vdev tree based on the boot device's label config.
4106 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4108 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4109 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4110 VDEV_ALLOC_ROOTPOOL);
4111 spa_config_exit(spa, SCL_ALL, FTAG);
4113 mutex_exit(&spa_namespace_lock);
4114 nvlist_free(config);
4115 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4120 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4122 spa_config_exit(spa, SCL_ALL, FTAG);
4123 mutex_exit(&spa_namespace_lock);
4125 nvlist_free(config);
4133 * Import a non-root pool into the system.
4136 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4139 char *altroot = NULL;
4140 spa_load_state_t state = SPA_LOAD_IMPORT;
4141 zpool_rewind_policy_t policy;
4142 uint64_t mode = spa_mode_global;
4143 uint64_t readonly = B_FALSE;
4146 nvlist_t **spares, **l2cache;
4147 uint_t nspares, nl2cache;
4150 * If a pool with this name exists, return failure.
4152 mutex_enter(&spa_namespace_lock);
4153 if (spa_lookup(pool) != NULL) {
4154 mutex_exit(&spa_namespace_lock);
4155 return (SET_ERROR(EEXIST));
4159 * Create and initialize the spa structure.
4161 (void) nvlist_lookup_string(props,
4162 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4163 (void) nvlist_lookup_uint64(props,
4164 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4167 spa = spa_add(pool, config, altroot);
4168 spa->spa_import_flags = flags;
4171 * Verbatim import - Take a pool and insert it into the namespace
4172 * as if it had been loaded at boot.
4174 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4176 spa_configfile_set(spa, props, B_FALSE);
4178 spa_config_sync(spa, B_FALSE, B_TRUE);
4180 mutex_exit(&spa_namespace_lock);
4184 spa_activate(spa, mode);
4187 * Don't start async tasks until we know everything is healthy.
4189 spa_async_suspend(spa);
4191 zpool_get_rewind_policy(config, &policy);
4192 if (policy.zrp_request & ZPOOL_DO_REWIND)
4193 state = SPA_LOAD_RECOVER;
4196 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4197 * because the user-supplied config is actually the one to trust when
4200 if (state != SPA_LOAD_RECOVER)
4201 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4203 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4204 policy.zrp_request);
4207 * Propagate anything learned while loading the pool and pass it
4208 * back to caller (i.e. rewind info, missing devices, etc).
4210 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4211 spa->spa_load_info) == 0);
4213 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4215 * Toss any existing sparelist, as it doesn't have any validity
4216 * anymore, and conflicts with spa_has_spare().
4218 if (spa->spa_spares.sav_config) {
4219 nvlist_free(spa->spa_spares.sav_config);
4220 spa->spa_spares.sav_config = NULL;
4221 spa_load_spares(spa);
4223 if (spa->spa_l2cache.sav_config) {
4224 nvlist_free(spa->spa_l2cache.sav_config);
4225 spa->spa_l2cache.sav_config = NULL;
4226 spa_load_l2cache(spa);
4229 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4232 error = spa_validate_aux(spa, nvroot, -1ULL,
4235 error = spa_validate_aux(spa, nvroot, -1ULL,
4236 VDEV_ALLOC_L2CACHE);
4237 spa_config_exit(spa, SCL_ALL, FTAG);
4240 spa_configfile_set(spa, props, B_FALSE);
4242 if (error != 0 || (props && spa_writeable(spa) &&
4243 (error = spa_prop_set(spa, props)))) {
4245 spa_deactivate(spa);
4247 mutex_exit(&spa_namespace_lock);
4251 spa_async_resume(spa);
4254 * Override any spares and level 2 cache devices as specified by
4255 * the user, as these may have correct device names/devids, etc.
4257 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4258 &spares, &nspares) == 0) {
4259 if (spa->spa_spares.sav_config)
4260 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4261 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4263 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4264 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4265 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4266 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4267 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4268 spa_load_spares(spa);
4269 spa_config_exit(spa, SCL_ALL, FTAG);
4270 spa->spa_spares.sav_sync = B_TRUE;
4272 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4273 &l2cache, &nl2cache) == 0) {
4274 if (spa->spa_l2cache.sav_config)
4275 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4276 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4278 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4279 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4280 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4281 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4282 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4283 spa_load_l2cache(spa);
4284 spa_config_exit(spa, SCL_ALL, FTAG);
4285 spa->spa_l2cache.sav_sync = B_TRUE;
4289 * Check for any removed devices.
4291 if (spa->spa_autoreplace) {
4292 spa_aux_check_removed(&spa->spa_spares);
4293 spa_aux_check_removed(&spa->spa_l2cache);
4296 if (spa_writeable(spa)) {
4298 * Update the config cache to include the newly-imported pool.
4300 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4304 * It's possible that the pool was expanded while it was exported.
4305 * We kick off an async task to handle this for us.
4307 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4309 mutex_exit(&spa_namespace_lock);
4310 spa_history_log_version(spa, "import");
4314 zvol_create_minors(pool);
4321 spa_tryimport(nvlist_t *tryconfig)
4323 nvlist_t *config = NULL;
4329 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4332 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4336 * Create and initialize the spa structure.
4338 mutex_enter(&spa_namespace_lock);
4339 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4340 spa_activate(spa, FREAD);
4343 * Pass off the heavy lifting to spa_load().
4344 * Pass TRUE for mosconfig because the user-supplied config
4345 * is actually the one to trust when doing an import.
4347 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4350 * If 'tryconfig' was at least parsable, return the current config.
4352 if (spa->spa_root_vdev != NULL) {
4353 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4354 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4356 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4358 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4359 spa->spa_uberblock.ub_timestamp) == 0);
4360 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4361 spa->spa_load_info) == 0);
4364 * If the bootfs property exists on this pool then we
4365 * copy it out so that external consumers can tell which
4366 * pools are bootable.
4368 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4369 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4372 * We have to play games with the name since the
4373 * pool was opened as TRYIMPORT_NAME.
4375 if (dsl_dsobj_to_dsname(spa_name(spa),
4376 spa->spa_bootfs, tmpname) == 0) {
4378 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4380 cp = strchr(tmpname, '/');
4382 (void) strlcpy(dsname, tmpname,
4385 (void) snprintf(dsname, MAXPATHLEN,
4386 "%s/%s", poolname, ++cp);
4388 VERIFY(nvlist_add_string(config,
4389 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4390 kmem_free(dsname, MAXPATHLEN);
4392 kmem_free(tmpname, MAXPATHLEN);
4396 * Add the list of hot spares and level 2 cache devices.
4398 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4399 spa_add_spares(spa, config);
4400 spa_add_l2cache(spa, config);
4401 spa_config_exit(spa, SCL_CONFIG, FTAG);
4405 spa_deactivate(spa);
4407 mutex_exit(&spa_namespace_lock);
4413 * Pool export/destroy
4415 * The act of destroying or exporting a pool is very simple. We make sure there
4416 * is no more pending I/O and any references to the pool are gone. Then, we
4417 * update the pool state and sync all the labels to disk, removing the
4418 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4419 * we don't sync the labels or remove the configuration cache.
4422 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4423 boolean_t force, boolean_t hardforce)
4430 if (!(spa_mode_global & FWRITE))
4431 return (SET_ERROR(EROFS));
4433 mutex_enter(&spa_namespace_lock);
4434 if ((spa = spa_lookup(pool)) == NULL) {
4435 mutex_exit(&spa_namespace_lock);
4436 return (SET_ERROR(ENOENT));
4440 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4441 * reacquire the namespace lock, and see if we can export.
4443 spa_open_ref(spa, FTAG);
4444 mutex_exit(&spa_namespace_lock);
4445 spa_async_suspend(spa);
4446 mutex_enter(&spa_namespace_lock);
4447 spa_close(spa, FTAG);
4450 * The pool will be in core if it's openable,
4451 * in which case we can modify its state.
4453 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4455 * Objsets may be open only because they're dirty, so we
4456 * have to force it to sync before checking spa_refcnt.
4458 txg_wait_synced(spa->spa_dsl_pool, 0);
4461 * A pool cannot be exported or destroyed if there are active
4462 * references. If we are resetting a pool, allow references by
4463 * fault injection handlers.
4465 if (!spa_refcount_zero(spa) ||
4466 (spa->spa_inject_ref != 0 &&
4467 new_state != POOL_STATE_UNINITIALIZED)) {
4468 spa_async_resume(spa);
4469 mutex_exit(&spa_namespace_lock);
4470 return (SET_ERROR(EBUSY));
4474 * A pool cannot be exported if it has an active shared spare.
4475 * This is to prevent other pools stealing the active spare
4476 * from an exported pool. At user's own will, such pool can
4477 * be forcedly exported.
4479 if (!force && new_state == POOL_STATE_EXPORTED &&
4480 spa_has_active_shared_spare(spa)) {
4481 spa_async_resume(spa);
4482 mutex_exit(&spa_namespace_lock);
4483 return (SET_ERROR(EXDEV));
4487 * We want this to be reflected on every label,
4488 * so mark them all dirty. spa_unload() will do the
4489 * final sync that pushes these changes out.
4491 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4492 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4493 spa->spa_state = new_state;
4494 spa->spa_final_txg = spa_last_synced_txg(spa) +
4496 vdev_config_dirty(spa->spa_root_vdev);
4497 spa_config_exit(spa, SCL_ALL, FTAG);
4501 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4503 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4505 spa_deactivate(spa);
4508 if (oldconfig && spa->spa_config)
4509 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4511 if (new_state != POOL_STATE_UNINITIALIZED) {
4513 spa_config_sync(spa, B_TRUE, B_TRUE);
4516 mutex_exit(&spa_namespace_lock);
4522 * Destroy a storage pool.
4525 spa_destroy(char *pool)
4527 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4532 * Export a storage pool.
4535 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4536 boolean_t hardforce)
4538 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4543 * Similar to spa_export(), this unloads the spa_t without actually removing it
4544 * from the namespace in any way.
4547 spa_reset(char *pool)
4549 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4554 * ==========================================================================
4555 * Device manipulation
4556 * ==========================================================================
4560 * Add a device to a storage pool.
4563 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4567 vdev_t *rvd = spa->spa_root_vdev;
4569 nvlist_t **spares, **l2cache;
4570 uint_t nspares, nl2cache;
4572 ASSERT(spa_writeable(spa));
4574 txg = spa_vdev_enter(spa);
4576 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4577 VDEV_ALLOC_ADD)) != 0)
4578 return (spa_vdev_exit(spa, NULL, txg, error));
4580 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4582 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4586 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4590 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4591 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4593 if (vd->vdev_children != 0 &&
4594 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4595 return (spa_vdev_exit(spa, vd, txg, error));
4598 * We must validate the spares and l2cache devices after checking the
4599 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4601 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4602 return (spa_vdev_exit(spa, vd, txg, error));
4605 * Transfer each new top-level vdev from vd to rvd.
4607 for (int c = 0; c < vd->vdev_children; c++) {
4610 * Set the vdev id to the first hole, if one exists.
4612 for (id = 0; id < rvd->vdev_children; id++) {
4613 if (rvd->vdev_child[id]->vdev_ishole) {
4614 vdev_free(rvd->vdev_child[id]);
4618 tvd = vd->vdev_child[c];
4619 vdev_remove_child(vd, tvd);
4621 vdev_add_child(rvd, tvd);
4622 vdev_config_dirty(tvd);
4626 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4627 ZPOOL_CONFIG_SPARES);
4628 spa_load_spares(spa);
4629 spa->spa_spares.sav_sync = B_TRUE;
4632 if (nl2cache != 0) {
4633 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4634 ZPOOL_CONFIG_L2CACHE);
4635 spa_load_l2cache(spa);
4636 spa->spa_l2cache.sav_sync = B_TRUE;
4640 * We have to be careful when adding new vdevs to an existing pool.
4641 * If other threads start allocating from these vdevs before we
4642 * sync the config cache, and we lose power, then upon reboot we may
4643 * fail to open the pool because there are DVAs that the config cache
4644 * can't translate. Therefore, we first add the vdevs without
4645 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4646 * and then let spa_config_update() initialize the new metaslabs.
4648 * spa_load() checks for added-but-not-initialized vdevs, so that
4649 * if we lose power at any point in this sequence, the remaining
4650 * steps will be completed the next time we load the pool.
4652 (void) spa_vdev_exit(spa, vd, txg, 0);
4654 mutex_enter(&spa_namespace_lock);
4655 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4656 mutex_exit(&spa_namespace_lock);
4662 * Attach a device to a mirror. The arguments are the path to any device
4663 * in the mirror, and the nvroot for the new device. If the path specifies
4664 * a device that is not mirrored, we automatically insert the mirror vdev.
4666 * If 'replacing' is specified, the new device is intended to replace the
4667 * existing device; in this case the two devices are made into their own
4668 * mirror using the 'replacing' vdev, which is functionally identical to
4669 * the mirror vdev (it actually reuses all the same ops) but has a few
4670 * extra rules: you can't attach to it after it's been created, and upon
4671 * completion of resilvering, the first disk (the one being replaced)
4672 * is automatically detached.
4675 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4677 uint64_t txg, dtl_max_txg;
4678 vdev_t *rvd = spa->spa_root_vdev;
4679 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4681 char *oldvdpath, *newvdpath;
4685 ASSERT(spa_writeable(spa));
4687 txg = spa_vdev_enter(spa);
4689 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4692 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4694 if (!oldvd->vdev_ops->vdev_op_leaf)
4695 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4697 pvd = oldvd->vdev_parent;
4699 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4700 VDEV_ALLOC_ATTACH)) != 0)
4701 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4703 if (newrootvd->vdev_children != 1)
4704 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4706 newvd = newrootvd->vdev_child[0];
4708 if (!newvd->vdev_ops->vdev_op_leaf)
4709 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4711 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4712 return (spa_vdev_exit(spa, newrootvd, txg, error));
4715 * Spares can't replace logs
4717 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4718 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4722 * For attach, the only allowable parent is a mirror or the root
4725 if (pvd->vdev_ops != &vdev_mirror_ops &&
4726 pvd->vdev_ops != &vdev_root_ops)
4727 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4729 pvops = &vdev_mirror_ops;
4732 * Active hot spares can only be replaced by inactive hot
4735 if (pvd->vdev_ops == &vdev_spare_ops &&
4736 oldvd->vdev_isspare &&
4737 !spa_has_spare(spa, newvd->vdev_guid))
4738 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4741 * If the source is a hot spare, and the parent isn't already a
4742 * spare, then we want to create a new hot spare. Otherwise, we
4743 * want to create a replacing vdev. The user is not allowed to
4744 * attach to a spared vdev child unless the 'isspare' state is
4745 * the same (spare replaces spare, non-spare replaces
4748 if (pvd->vdev_ops == &vdev_replacing_ops &&
4749 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4750 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4751 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4752 newvd->vdev_isspare != oldvd->vdev_isspare) {
4753 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4756 if (newvd->vdev_isspare)
4757 pvops = &vdev_spare_ops;
4759 pvops = &vdev_replacing_ops;
4763 * Make sure the new device is big enough.
4765 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4766 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4769 * The new device cannot have a higher alignment requirement
4770 * than the top-level vdev.
4772 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4773 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4776 * If this is an in-place replacement, update oldvd's path and devid
4777 * to make it distinguishable from newvd, and unopenable from now on.
4779 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4780 spa_strfree(oldvd->vdev_path);
4781 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4783 (void) sprintf(oldvd->vdev_path, "%s/%s",
4784 newvd->vdev_path, "old");
4785 if (oldvd->vdev_devid != NULL) {
4786 spa_strfree(oldvd->vdev_devid);
4787 oldvd->vdev_devid = NULL;
4791 /* mark the device being resilvered */
4792 newvd->vdev_resilver_txg = txg;
4795 * If the parent is not a mirror, or if we're replacing, insert the new
4796 * mirror/replacing/spare vdev above oldvd.
4798 if (pvd->vdev_ops != pvops)
4799 pvd = vdev_add_parent(oldvd, pvops);
4801 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4802 ASSERT(pvd->vdev_ops == pvops);
4803 ASSERT(oldvd->vdev_parent == pvd);
4806 * Extract the new device from its root and add it to pvd.
4808 vdev_remove_child(newrootvd, newvd);
4809 newvd->vdev_id = pvd->vdev_children;
4810 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4811 vdev_add_child(pvd, newvd);
4813 tvd = newvd->vdev_top;
4814 ASSERT(pvd->vdev_top == tvd);
4815 ASSERT(tvd->vdev_parent == rvd);
4817 vdev_config_dirty(tvd);
4820 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4821 * for any dmu_sync-ed blocks. It will propagate upward when
4822 * spa_vdev_exit() calls vdev_dtl_reassess().
4824 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4826 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4827 dtl_max_txg - TXG_INITIAL);
4829 if (newvd->vdev_isspare) {
4830 spa_spare_activate(newvd);
4831 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4834 oldvdpath = spa_strdup(oldvd->vdev_path);
4835 newvdpath = spa_strdup(newvd->vdev_path);
4836 newvd_isspare = newvd->vdev_isspare;
4839 * Mark newvd's DTL dirty in this txg.
4841 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4844 * Schedule the resilver to restart in the future. We do this to
4845 * ensure that dmu_sync-ed blocks have been stitched into the
4846 * respective datasets.
4848 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4853 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4855 spa_history_log_internal(spa, "vdev attach", NULL,
4856 "%s vdev=%s %s vdev=%s",
4857 replacing && newvd_isspare ? "spare in" :
4858 replacing ? "replace" : "attach", newvdpath,
4859 replacing ? "for" : "to", oldvdpath);
4861 spa_strfree(oldvdpath);
4862 spa_strfree(newvdpath);
4864 if (spa->spa_bootfs)
4865 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4871 * Detach a device from a mirror or replacing vdev.
4873 * If 'replace_done' is specified, only detach if the parent
4874 * is a replacing vdev.
4877 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4881 vdev_t *rvd = spa->spa_root_vdev;
4882 vdev_t *vd, *pvd, *cvd, *tvd;
4883 boolean_t unspare = B_FALSE;
4884 uint64_t unspare_guid = 0;
4887 ASSERT(spa_writeable(spa));
4889 txg = spa_vdev_enter(spa);
4891 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4894 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4896 if (!vd->vdev_ops->vdev_op_leaf)
4897 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4899 pvd = vd->vdev_parent;
4902 * If the parent/child relationship is not as expected, don't do it.
4903 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4904 * vdev that's replacing B with C. The user's intent in replacing
4905 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4906 * the replace by detaching C, the expected behavior is to end up
4907 * M(A,B). But suppose that right after deciding to detach C,
4908 * the replacement of B completes. We would have M(A,C), and then
4909 * ask to detach C, which would leave us with just A -- not what
4910 * the user wanted. To prevent this, we make sure that the
4911 * parent/child relationship hasn't changed -- in this example,
4912 * that C's parent is still the replacing vdev R.
4914 if (pvd->vdev_guid != pguid && pguid != 0)
4915 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4918 * Only 'replacing' or 'spare' vdevs can be replaced.
4920 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4921 pvd->vdev_ops != &vdev_spare_ops)
4922 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4924 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4925 spa_version(spa) >= SPA_VERSION_SPARES);
4928 * Only mirror, replacing, and spare vdevs support detach.
4930 if (pvd->vdev_ops != &vdev_replacing_ops &&
4931 pvd->vdev_ops != &vdev_mirror_ops &&
4932 pvd->vdev_ops != &vdev_spare_ops)
4933 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4936 * If this device has the only valid copy of some data,
4937 * we cannot safely detach it.
4939 if (vdev_dtl_required(vd))
4940 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4942 ASSERT(pvd->vdev_children >= 2);
4945 * If we are detaching the second disk from a replacing vdev, then
4946 * check to see if we changed the original vdev's path to have "/old"
4947 * at the end in spa_vdev_attach(). If so, undo that change now.
4949 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4950 vd->vdev_path != NULL) {
4951 size_t len = strlen(vd->vdev_path);
4953 for (int c = 0; c < pvd->vdev_children; c++) {
4954 cvd = pvd->vdev_child[c];
4956 if (cvd == vd || cvd->vdev_path == NULL)
4959 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4960 strcmp(cvd->vdev_path + len, "/old") == 0) {
4961 spa_strfree(cvd->vdev_path);
4962 cvd->vdev_path = spa_strdup(vd->vdev_path);
4969 * If we are detaching the original disk from a spare, then it implies
4970 * that the spare should become a real disk, and be removed from the
4971 * active spare list for the pool.
4973 if (pvd->vdev_ops == &vdev_spare_ops &&
4975 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4979 * Erase the disk labels so the disk can be used for other things.
4980 * This must be done after all other error cases are handled,
4981 * but before we disembowel vd (so we can still do I/O to it).
4982 * But if we can't do it, don't treat the error as fatal --
4983 * it may be that the unwritability of the disk is the reason
4984 * it's being detached!
4986 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4989 * Remove vd from its parent and compact the parent's children.
4991 vdev_remove_child(pvd, vd);
4992 vdev_compact_children(pvd);
4995 * Remember one of the remaining children so we can get tvd below.
4997 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5000 * If we need to remove the remaining child from the list of hot spares,
5001 * do it now, marking the vdev as no longer a spare in the process.
5002 * We must do this before vdev_remove_parent(), because that can
5003 * change the GUID if it creates a new toplevel GUID. For a similar
5004 * reason, we must remove the spare now, in the same txg as the detach;
5005 * otherwise someone could attach a new sibling, change the GUID, and
5006 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5009 ASSERT(cvd->vdev_isspare);
5010 spa_spare_remove(cvd);
5011 unspare_guid = cvd->vdev_guid;
5012 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5013 cvd->vdev_unspare = B_TRUE;
5017 * If the parent mirror/replacing vdev only has one child,
5018 * the parent is no longer needed. Remove it from the tree.
5020 if (pvd->vdev_children == 1) {
5021 if (pvd->vdev_ops == &vdev_spare_ops)
5022 cvd->vdev_unspare = B_FALSE;
5023 vdev_remove_parent(cvd);
5028 * We don't set tvd until now because the parent we just removed
5029 * may have been the previous top-level vdev.
5031 tvd = cvd->vdev_top;
5032 ASSERT(tvd->vdev_parent == rvd);
5035 * Reevaluate the parent vdev state.
5037 vdev_propagate_state(cvd);
5040 * If the 'autoexpand' property is set on the pool then automatically
5041 * try to expand the size of the pool. For example if the device we
5042 * just detached was smaller than the others, it may be possible to
5043 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5044 * first so that we can obtain the updated sizes of the leaf vdevs.
5046 if (spa->spa_autoexpand) {
5048 vdev_expand(tvd, txg);
5051 vdev_config_dirty(tvd);
5054 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5055 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5056 * But first make sure we're not on any *other* txg's DTL list, to
5057 * prevent vd from being accessed after it's freed.
5059 vdpath = spa_strdup(vd->vdev_path);
5060 for (int t = 0; t < TXG_SIZE; t++)
5061 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5062 vd->vdev_detached = B_TRUE;
5063 vdev_dirty(tvd, VDD_DTL, vd, txg);
5065 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5067 /* hang on to the spa before we release the lock */
5068 spa_open_ref(spa, FTAG);
5070 error = spa_vdev_exit(spa, vd, txg, 0);
5072 spa_history_log_internal(spa, "detach", NULL,
5074 spa_strfree(vdpath);
5077 * If this was the removal of the original device in a hot spare vdev,
5078 * then we want to go through and remove the device from the hot spare
5079 * list of every other pool.
5082 spa_t *altspa = NULL;
5084 mutex_enter(&spa_namespace_lock);
5085 while ((altspa = spa_next(altspa)) != NULL) {
5086 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5090 spa_open_ref(altspa, FTAG);
5091 mutex_exit(&spa_namespace_lock);
5092 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5093 mutex_enter(&spa_namespace_lock);
5094 spa_close(altspa, FTAG);
5096 mutex_exit(&spa_namespace_lock);
5098 /* search the rest of the vdevs for spares to remove */
5099 spa_vdev_resilver_done(spa);
5102 /* all done with the spa; OK to release */
5103 mutex_enter(&spa_namespace_lock);
5104 spa_close(spa, FTAG);
5105 mutex_exit(&spa_namespace_lock);
5111 * Split a set of devices from their mirrors, and create a new pool from them.
5114 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5115 nvlist_t *props, boolean_t exp)
5118 uint64_t txg, *glist;
5120 uint_t c, children, lastlog;
5121 nvlist_t **child, *nvl, *tmp;
5123 char *altroot = NULL;
5124 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5125 boolean_t activate_slog;
5127 ASSERT(spa_writeable(spa));
5129 txg = spa_vdev_enter(spa);
5131 /* clear the log and flush everything up to now */
5132 activate_slog = spa_passivate_log(spa);
5133 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5134 error = spa_offline_log(spa);
5135 txg = spa_vdev_config_enter(spa);
5138 spa_activate_log(spa);
5141 return (spa_vdev_exit(spa, NULL, txg, error));
5143 /* check new spa name before going any further */
5144 if (spa_lookup(newname) != NULL)
5145 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5148 * scan through all the children to ensure they're all mirrors
5150 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5151 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5153 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5155 /* first, check to ensure we've got the right child count */
5156 rvd = spa->spa_root_vdev;
5158 for (c = 0; c < rvd->vdev_children; c++) {
5159 vdev_t *vd = rvd->vdev_child[c];
5161 /* don't count the holes & logs as children */
5162 if (vd->vdev_islog || vd->vdev_ishole) {
5170 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5171 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5173 /* next, ensure no spare or cache devices are part of the split */
5174 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5175 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5176 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5178 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5179 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5181 /* then, loop over each vdev and validate it */
5182 for (c = 0; c < children; c++) {
5183 uint64_t is_hole = 0;
5185 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5189 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5190 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5193 error = SET_ERROR(EINVAL);
5198 /* which disk is going to be split? */
5199 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5201 error = SET_ERROR(EINVAL);
5205 /* look it up in the spa */
5206 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5207 if (vml[c] == NULL) {
5208 error = SET_ERROR(ENODEV);
5212 /* make sure there's nothing stopping the split */
5213 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5214 vml[c]->vdev_islog ||
5215 vml[c]->vdev_ishole ||
5216 vml[c]->vdev_isspare ||
5217 vml[c]->vdev_isl2cache ||
5218 !vdev_writeable(vml[c]) ||
5219 vml[c]->vdev_children != 0 ||
5220 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5221 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5222 error = SET_ERROR(EINVAL);
5226 if (vdev_dtl_required(vml[c])) {
5227 error = SET_ERROR(EBUSY);
5231 /* we need certain info from the top level */
5232 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5233 vml[c]->vdev_top->vdev_ms_array) == 0);
5234 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5235 vml[c]->vdev_top->vdev_ms_shift) == 0);
5236 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5237 vml[c]->vdev_top->vdev_asize) == 0);
5238 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5239 vml[c]->vdev_top->vdev_ashift) == 0);
5243 kmem_free(vml, children * sizeof (vdev_t *));
5244 kmem_free(glist, children * sizeof (uint64_t));
5245 return (spa_vdev_exit(spa, NULL, txg, error));
5248 /* stop writers from using the disks */
5249 for (c = 0; c < children; c++) {
5251 vml[c]->vdev_offline = B_TRUE;
5253 vdev_reopen(spa->spa_root_vdev);
5256 * Temporarily record the splitting vdevs in the spa config. This
5257 * will disappear once the config is regenerated.
5259 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5260 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5261 glist, children) == 0);
5262 kmem_free(glist, children * sizeof (uint64_t));
5264 mutex_enter(&spa->spa_props_lock);
5265 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5267 mutex_exit(&spa->spa_props_lock);
5268 spa->spa_config_splitting = nvl;
5269 vdev_config_dirty(spa->spa_root_vdev);
5271 /* configure and create the new pool */
5272 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5273 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5274 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5275 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5276 spa_version(spa)) == 0);
5277 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5278 spa->spa_config_txg) == 0);
5279 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5280 spa_generate_guid(NULL)) == 0);
5281 (void) nvlist_lookup_string(props,
5282 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5284 /* add the new pool to the namespace */
5285 newspa = spa_add(newname, config, altroot);
5286 newspa->spa_config_txg = spa->spa_config_txg;
5287 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5289 /* release the spa config lock, retaining the namespace lock */
5290 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5292 if (zio_injection_enabled)
5293 zio_handle_panic_injection(spa, FTAG, 1);
5295 spa_activate(newspa, spa_mode_global);
5296 spa_async_suspend(newspa);
5299 /* mark that we are creating new spa by splitting */
5300 newspa->spa_splitting_newspa = B_TRUE;
5302 /* create the new pool from the disks of the original pool */
5303 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5305 newspa->spa_splitting_newspa = B_FALSE;
5310 /* if that worked, generate a real config for the new pool */
5311 if (newspa->spa_root_vdev != NULL) {
5312 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5313 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5314 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5315 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5316 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5321 if (props != NULL) {
5322 spa_configfile_set(newspa, props, B_FALSE);
5323 error = spa_prop_set(newspa, props);
5328 /* flush everything */
5329 txg = spa_vdev_config_enter(newspa);
5330 vdev_config_dirty(newspa->spa_root_vdev);
5331 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5333 if (zio_injection_enabled)
5334 zio_handle_panic_injection(spa, FTAG, 2);
5336 spa_async_resume(newspa);
5338 /* finally, update the original pool's config */
5339 txg = spa_vdev_config_enter(spa);
5340 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5341 error = dmu_tx_assign(tx, TXG_WAIT);
5344 for (c = 0; c < children; c++) {
5345 if (vml[c] != NULL) {
5348 spa_history_log_internal(spa, "detach", tx,
5349 "vdev=%s", vml[c]->vdev_path);
5353 vdev_config_dirty(spa->spa_root_vdev);
5354 spa->spa_config_splitting = NULL;
5358 (void) spa_vdev_exit(spa, NULL, txg, 0);
5360 if (zio_injection_enabled)
5361 zio_handle_panic_injection(spa, FTAG, 3);
5363 /* split is complete; log a history record */
5364 spa_history_log_internal(newspa, "split", NULL,
5365 "from pool %s", spa_name(spa));
5367 kmem_free(vml, children * sizeof (vdev_t *));
5369 /* if we're not going to mount the filesystems in userland, export */
5371 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5378 spa_deactivate(newspa);
5381 txg = spa_vdev_config_enter(spa);
5383 /* re-online all offlined disks */
5384 for (c = 0; c < children; c++) {
5386 vml[c]->vdev_offline = B_FALSE;
5388 vdev_reopen(spa->spa_root_vdev);
5390 nvlist_free(spa->spa_config_splitting);
5391 spa->spa_config_splitting = NULL;
5392 (void) spa_vdev_exit(spa, NULL, txg, error);
5394 kmem_free(vml, children * sizeof (vdev_t *));
5399 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5401 for (int i = 0; i < count; i++) {
5404 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5407 if (guid == target_guid)
5415 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5416 nvlist_t *dev_to_remove)
5418 nvlist_t **newdev = NULL;
5421 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5423 for (int i = 0, j = 0; i < count; i++) {
5424 if (dev[i] == dev_to_remove)
5426 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5429 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5430 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5432 for (int i = 0; i < count - 1; i++)
5433 nvlist_free(newdev[i]);
5436 kmem_free(newdev, (count - 1) * sizeof (void *));
5440 * Evacuate the device.
5443 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5448 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5449 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5450 ASSERT(vd == vd->vdev_top);
5453 * Evacuate the device. We don't hold the config lock as writer
5454 * since we need to do I/O but we do keep the
5455 * spa_namespace_lock held. Once this completes the device
5456 * should no longer have any blocks allocated on it.
5458 if (vd->vdev_islog) {
5459 if (vd->vdev_stat.vs_alloc != 0)
5460 error = spa_offline_log(spa);
5462 error = SET_ERROR(ENOTSUP);
5469 * The evacuation succeeded. Remove any remaining MOS metadata
5470 * associated with this vdev, and wait for these changes to sync.
5472 ASSERT0(vd->vdev_stat.vs_alloc);
5473 txg = spa_vdev_config_enter(spa);
5474 vd->vdev_removing = B_TRUE;
5475 vdev_dirty_leaves(vd, VDD_DTL, txg);
5476 vdev_config_dirty(vd);
5477 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5483 * Complete the removal by cleaning up the namespace.
5486 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5488 vdev_t *rvd = spa->spa_root_vdev;
5489 uint64_t id = vd->vdev_id;
5490 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5492 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5493 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5494 ASSERT(vd == vd->vdev_top);
5497 * Only remove any devices which are empty.
5499 if (vd->vdev_stat.vs_alloc != 0)
5502 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5504 if (list_link_active(&vd->vdev_state_dirty_node))
5505 vdev_state_clean(vd);
5506 if (list_link_active(&vd->vdev_config_dirty_node))
5507 vdev_config_clean(vd);
5512 vdev_compact_children(rvd);
5514 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5515 vdev_add_child(rvd, vd);
5517 vdev_config_dirty(rvd);
5520 * Reassess the health of our root vdev.
5526 * Remove a device from the pool -
5528 * Removing a device from the vdev namespace requires several steps
5529 * and can take a significant amount of time. As a result we use
5530 * the spa_vdev_config_[enter/exit] functions which allow us to
5531 * grab and release the spa_config_lock while still holding the namespace
5532 * lock. During each step the configuration is synced out.
5534 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5538 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5541 metaslab_group_t *mg;
5542 nvlist_t **spares, **l2cache, *nv;
5544 uint_t nspares, nl2cache;
5546 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5548 ASSERT(spa_writeable(spa));
5551 txg = spa_vdev_enter(spa);
5553 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5555 if (spa->spa_spares.sav_vdevs != NULL &&
5556 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5557 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5558 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5560 * Only remove the hot spare if it's not currently in use
5563 if (vd == NULL || unspare) {
5564 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5565 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5566 spa_load_spares(spa);
5567 spa->spa_spares.sav_sync = B_TRUE;
5569 error = SET_ERROR(EBUSY);
5571 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5572 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5573 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5574 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5576 * Cache devices can always be removed.
5578 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5579 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5580 spa_load_l2cache(spa);
5581 spa->spa_l2cache.sav_sync = B_TRUE;
5582 } else if (vd != NULL && vd->vdev_islog) {
5584 ASSERT(vd == vd->vdev_top);
5589 * Stop allocating from this vdev.
5591 metaslab_group_passivate(mg);
5594 * Wait for the youngest allocations and frees to sync,
5595 * and then wait for the deferral of those frees to finish.
5597 spa_vdev_config_exit(spa, NULL,
5598 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5601 * Attempt to evacuate the vdev.
5603 error = spa_vdev_remove_evacuate(spa, vd);
5605 txg = spa_vdev_config_enter(spa);
5608 * If we couldn't evacuate the vdev, unwind.
5611 metaslab_group_activate(mg);
5612 return (spa_vdev_exit(spa, NULL, txg, error));
5616 * Clean up the vdev namespace.
5618 spa_vdev_remove_from_namespace(spa, vd);
5620 } else if (vd != NULL) {
5622 * Normal vdevs cannot be removed (yet).
5624 error = SET_ERROR(ENOTSUP);
5627 * There is no vdev of any kind with the specified guid.
5629 error = SET_ERROR(ENOENT);
5633 return (spa_vdev_exit(spa, NULL, txg, error));
5639 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5640 * currently spared, so we can detach it.
5643 spa_vdev_resilver_done_hunt(vdev_t *vd)
5645 vdev_t *newvd, *oldvd;
5647 for (int c = 0; c < vd->vdev_children; c++) {
5648 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5654 * Check for a completed replacement. We always consider the first
5655 * vdev in the list to be the oldest vdev, and the last one to be
5656 * the newest (see spa_vdev_attach() for how that works). In
5657 * the case where the newest vdev is faulted, we will not automatically
5658 * remove it after a resilver completes. This is OK as it will require
5659 * user intervention to determine which disk the admin wishes to keep.
5661 if (vd->vdev_ops == &vdev_replacing_ops) {
5662 ASSERT(vd->vdev_children > 1);
5664 newvd = vd->vdev_child[vd->vdev_children - 1];
5665 oldvd = vd->vdev_child[0];
5667 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5668 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5669 !vdev_dtl_required(oldvd))
5674 * Check for a completed resilver with the 'unspare' flag set.
5676 if (vd->vdev_ops == &vdev_spare_ops) {
5677 vdev_t *first = vd->vdev_child[0];
5678 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5680 if (last->vdev_unspare) {
5683 } else if (first->vdev_unspare) {
5690 if (oldvd != NULL &&
5691 vdev_dtl_empty(newvd, DTL_MISSING) &&
5692 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5693 !vdev_dtl_required(oldvd))
5697 * If there are more than two spares attached to a disk,
5698 * and those spares are not required, then we want to
5699 * attempt to free them up now so that they can be used
5700 * by other pools. Once we're back down to a single
5701 * disk+spare, we stop removing them.
5703 if (vd->vdev_children > 2) {
5704 newvd = vd->vdev_child[1];
5706 if (newvd->vdev_isspare && last->vdev_isspare &&
5707 vdev_dtl_empty(last, DTL_MISSING) &&
5708 vdev_dtl_empty(last, DTL_OUTAGE) &&
5709 !vdev_dtl_required(newvd))
5718 spa_vdev_resilver_done(spa_t *spa)
5720 vdev_t *vd, *pvd, *ppvd;
5721 uint64_t guid, sguid, pguid, ppguid;
5723 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5725 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5726 pvd = vd->vdev_parent;
5727 ppvd = pvd->vdev_parent;
5728 guid = vd->vdev_guid;
5729 pguid = pvd->vdev_guid;
5730 ppguid = ppvd->vdev_guid;
5733 * If we have just finished replacing a hot spared device, then
5734 * we need to detach the parent's first child (the original hot
5737 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5738 ppvd->vdev_children == 2) {
5739 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5740 sguid = ppvd->vdev_child[1]->vdev_guid;
5742 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5744 spa_config_exit(spa, SCL_ALL, FTAG);
5745 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5747 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5749 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5752 spa_config_exit(spa, SCL_ALL, FTAG);
5756 * Update the stored path or FRU for this vdev.
5759 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5763 boolean_t sync = B_FALSE;
5765 ASSERT(spa_writeable(spa));
5767 spa_vdev_state_enter(spa, SCL_ALL);
5769 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5770 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5772 if (!vd->vdev_ops->vdev_op_leaf)
5773 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5776 if (strcmp(value, vd->vdev_path) != 0) {
5777 spa_strfree(vd->vdev_path);
5778 vd->vdev_path = spa_strdup(value);
5782 if (vd->vdev_fru == NULL) {
5783 vd->vdev_fru = spa_strdup(value);
5785 } else if (strcmp(value, vd->vdev_fru) != 0) {
5786 spa_strfree(vd->vdev_fru);
5787 vd->vdev_fru = spa_strdup(value);
5792 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5796 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5798 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5802 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5804 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5808 * ==========================================================================
5810 * ==========================================================================
5814 spa_scan_stop(spa_t *spa)
5816 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5817 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5818 return (SET_ERROR(EBUSY));
5819 return (dsl_scan_cancel(spa->spa_dsl_pool));
5823 spa_scan(spa_t *spa, pool_scan_func_t func)
5825 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5827 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5828 return (SET_ERROR(ENOTSUP));
5831 * If a resilver was requested, but there is no DTL on a
5832 * writeable leaf device, we have nothing to do.
5834 if (func == POOL_SCAN_RESILVER &&
5835 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5836 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5840 return (dsl_scan(spa->spa_dsl_pool, func));
5844 * ==========================================================================
5845 * SPA async task processing
5846 * ==========================================================================
5850 spa_async_remove(spa_t *spa, vdev_t *vd)
5852 if (vd->vdev_remove_wanted) {
5853 vd->vdev_remove_wanted = B_FALSE;
5854 vd->vdev_delayed_close = B_FALSE;
5855 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5858 * We want to clear the stats, but we don't want to do a full
5859 * vdev_clear() as that will cause us to throw away
5860 * degraded/faulted state as well as attempt to reopen the
5861 * device, all of which is a waste.
5863 vd->vdev_stat.vs_read_errors = 0;
5864 vd->vdev_stat.vs_write_errors = 0;
5865 vd->vdev_stat.vs_checksum_errors = 0;
5867 vdev_state_dirty(vd->vdev_top);
5870 for (int c = 0; c < vd->vdev_children; c++)
5871 spa_async_remove(spa, vd->vdev_child[c]);
5875 spa_async_probe(spa_t *spa, vdev_t *vd)
5877 if (vd->vdev_probe_wanted) {
5878 vd->vdev_probe_wanted = B_FALSE;
5879 vdev_reopen(vd); /* vdev_open() does the actual probe */
5882 for (int c = 0; c < vd->vdev_children; c++)
5883 spa_async_probe(spa, vd->vdev_child[c]);
5887 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5893 if (!spa->spa_autoexpand)
5896 for (int c = 0; c < vd->vdev_children; c++) {
5897 vdev_t *cvd = vd->vdev_child[c];
5898 spa_async_autoexpand(spa, cvd);
5901 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5904 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5905 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5907 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5908 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5910 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5911 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5914 kmem_free(physpath, MAXPATHLEN);
5918 spa_async_thread(void *arg)
5923 ASSERT(spa->spa_sync_on);
5925 mutex_enter(&spa->spa_async_lock);
5926 tasks = spa->spa_async_tasks;
5927 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
5928 mutex_exit(&spa->spa_async_lock);
5931 * See if the config needs to be updated.
5933 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5934 uint64_t old_space, new_space;
5936 mutex_enter(&spa_namespace_lock);
5937 old_space = metaslab_class_get_space(spa_normal_class(spa));
5938 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5939 new_space = metaslab_class_get_space(spa_normal_class(spa));
5940 mutex_exit(&spa_namespace_lock);
5943 * If the pool grew as a result of the config update,
5944 * then log an internal history event.
5946 if (new_space != old_space) {
5947 spa_history_log_internal(spa, "vdev online", NULL,
5948 "pool '%s' size: %llu(+%llu)",
5949 spa_name(spa), new_space, new_space - old_space);
5953 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5954 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5955 spa_async_autoexpand(spa, spa->spa_root_vdev);
5956 spa_config_exit(spa, SCL_CONFIG, FTAG);
5960 * See if any devices need to be probed.
5962 if (tasks & SPA_ASYNC_PROBE) {
5963 spa_vdev_state_enter(spa, SCL_NONE);
5964 spa_async_probe(spa, spa->spa_root_vdev);
5965 (void) spa_vdev_state_exit(spa, NULL, 0);
5969 * If any devices are done replacing, detach them.
5971 if (tasks & SPA_ASYNC_RESILVER_DONE)
5972 spa_vdev_resilver_done(spa);
5975 * Kick off a resilver.
5977 if (tasks & SPA_ASYNC_RESILVER)
5978 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5981 * Let the world know that we're done.
5983 mutex_enter(&spa->spa_async_lock);
5984 spa->spa_async_thread = NULL;
5985 cv_broadcast(&spa->spa_async_cv);
5986 mutex_exit(&spa->spa_async_lock);
5991 spa_async_thread_vd(void *arg)
5996 ASSERT(spa->spa_sync_on);
5998 mutex_enter(&spa->spa_async_lock);
5999 tasks = spa->spa_async_tasks;
6001 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6002 mutex_exit(&spa->spa_async_lock);
6005 * See if any devices need to be marked REMOVED.
6007 if (tasks & SPA_ASYNC_REMOVE) {
6008 spa_vdev_state_enter(spa, SCL_NONE);
6009 spa_async_remove(spa, spa->spa_root_vdev);
6010 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6011 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6012 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6013 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6014 (void) spa_vdev_state_exit(spa, NULL, 0);
6018 * Let the world know that we're done.
6020 mutex_enter(&spa->spa_async_lock);
6021 tasks = spa->spa_async_tasks;
6022 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6024 spa->spa_async_thread_vd = NULL;
6025 cv_broadcast(&spa->spa_async_cv);
6026 mutex_exit(&spa->spa_async_lock);
6031 spa_async_suspend(spa_t *spa)
6033 mutex_enter(&spa->spa_async_lock);
6034 spa->spa_async_suspended++;
6035 while (spa->spa_async_thread != NULL &&
6036 spa->spa_async_thread_vd != NULL)
6037 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6038 mutex_exit(&spa->spa_async_lock);
6042 spa_async_resume(spa_t *spa)
6044 mutex_enter(&spa->spa_async_lock);
6045 ASSERT(spa->spa_async_suspended != 0);
6046 spa->spa_async_suspended--;
6047 mutex_exit(&spa->spa_async_lock);
6051 spa_async_tasks_pending(spa_t *spa)
6053 uint_t non_config_tasks;
6055 boolean_t config_task_suspended;
6057 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6059 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6060 if (spa->spa_ccw_fail_time == 0) {
6061 config_task_suspended = B_FALSE;
6063 config_task_suspended =
6064 (gethrtime() - spa->spa_ccw_fail_time) <
6065 (zfs_ccw_retry_interval * NANOSEC);
6068 return (non_config_tasks || (config_task && !config_task_suspended));
6072 spa_async_dispatch(spa_t *spa)
6074 mutex_enter(&spa->spa_async_lock);
6075 if (spa_async_tasks_pending(spa) &&
6076 !spa->spa_async_suspended &&
6077 spa->spa_async_thread == NULL &&
6079 spa->spa_async_thread = thread_create(NULL, 0,
6080 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6081 mutex_exit(&spa->spa_async_lock);
6085 spa_async_dispatch_vd(spa_t *spa)
6087 mutex_enter(&spa->spa_async_lock);
6088 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6089 !spa->spa_async_suspended &&
6090 spa->spa_async_thread_vd == NULL &&
6092 spa->spa_async_thread_vd = thread_create(NULL, 0,
6093 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6094 mutex_exit(&spa->spa_async_lock);
6098 spa_async_request(spa_t *spa, int task)
6100 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6101 mutex_enter(&spa->spa_async_lock);
6102 spa->spa_async_tasks |= task;
6103 mutex_exit(&spa->spa_async_lock);
6104 spa_async_dispatch_vd(spa);
6108 * ==========================================================================
6109 * SPA syncing routines
6110 * ==========================================================================
6114 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6117 bpobj_enqueue(bpo, bp, tx);
6122 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6126 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6127 BP_GET_PSIZE(bp), zio->io_flags));
6132 * Note: this simple function is not inlined to make it easier to dtrace the
6133 * amount of time spent syncing frees.
6136 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6138 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6139 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6140 VERIFY(zio_wait(zio) == 0);
6144 * Note: this simple function is not inlined to make it easier to dtrace the
6145 * amount of time spent syncing deferred frees.
6148 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6150 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6151 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6152 spa_free_sync_cb, zio, tx), ==, 0);
6153 VERIFY0(zio_wait(zio));
6158 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6160 char *packed = NULL;
6165 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6168 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6169 * information. This avoids the dmu_buf_will_dirty() path and
6170 * saves us a pre-read to get data we don't actually care about.
6172 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6173 packed = kmem_alloc(bufsize, KM_SLEEP);
6175 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6177 bzero(packed + nvsize, bufsize - nvsize);
6179 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6181 kmem_free(packed, bufsize);
6183 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6184 dmu_buf_will_dirty(db, tx);
6185 *(uint64_t *)db->db_data = nvsize;
6186 dmu_buf_rele(db, FTAG);
6190 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6191 const char *config, const char *entry)
6201 * Update the MOS nvlist describing the list of available devices.
6202 * spa_validate_aux() will have already made sure this nvlist is
6203 * valid and the vdevs are labeled appropriately.
6205 if (sav->sav_object == 0) {
6206 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6207 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6208 sizeof (uint64_t), tx);
6209 VERIFY(zap_update(spa->spa_meta_objset,
6210 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6211 &sav->sav_object, tx) == 0);
6214 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6215 if (sav->sav_count == 0) {
6216 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6218 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6219 for (i = 0; i < sav->sav_count; i++)
6220 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6221 B_FALSE, VDEV_CONFIG_L2CACHE);
6222 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6223 sav->sav_count) == 0);
6224 for (i = 0; i < sav->sav_count; i++)
6225 nvlist_free(list[i]);
6226 kmem_free(list, sav->sav_count * sizeof (void *));
6229 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6230 nvlist_free(nvroot);
6232 sav->sav_sync = B_FALSE;
6236 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6240 if (list_is_empty(&spa->spa_config_dirty_list))
6243 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6245 config = spa_config_generate(spa, spa->spa_root_vdev,
6246 dmu_tx_get_txg(tx), B_FALSE);
6249 * If we're upgrading the spa version then make sure that
6250 * the config object gets updated with the correct version.
6252 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6253 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6254 spa->spa_uberblock.ub_version);
6256 spa_config_exit(spa, SCL_STATE, FTAG);
6258 if (spa->spa_config_syncing)
6259 nvlist_free(spa->spa_config_syncing);
6260 spa->spa_config_syncing = config;
6262 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6266 spa_sync_version(void *arg, dmu_tx_t *tx)
6268 uint64_t *versionp = arg;
6269 uint64_t version = *versionp;
6270 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6273 * Setting the version is special cased when first creating the pool.
6275 ASSERT(tx->tx_txg != TXG_INITIAL);
6277 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6278 ASSERT(version >= spa_version(spa));
6280 spa->spa_uberblock.ub_version = version;
6281 vdev_config_dirty(spa->spa_root_vdev);
6282 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6286 * Set zpool properties.
6289 spa_sync_props(void *arg, dmu_tx_t *tx)
6291 nvlist_t *nvp = arg;
6292 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6293 objset_t *mos = spa->spa_meta_objset;
6294 nvpair_t *elem = NULL;
6296 mutex_enter(&spa->spa_props_lock);
6298 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6300 char *strval, *fname;
6302 const char *propname;
6303 zprop_type_t proptype;
6306 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6309 * We checked this earlier in spa_prop_validate().
6311 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6313 fname = strchr(nvpair_name(elem), '@') + 1;
6314 VERIFY0(zfeature_lookup_name(fname, &fid));
6316 spa_feature_enable(spa, fid, tx);
6317 spa_history_log_internal(spa, "set", tx,
6318 "%s=enabled", nvpair_name(elem));
6321 case ZPOOL_PROP_VERSION:
6322 intval = fnvpair_value_uint64(elem);
6324 * The version is synced seperatly before other
6325 * properties and should be correct by now.
6327 ASSERT3U(spa_version(spa), >=, intval);
6330 case ZPOOL_PROP_ALTROOT:
6332 * 'altroot' is a non-persistent property. It should
6333 * have been set temporarily at creation or import time.
6335 ASSERT(spa->spa_root != NULL);
6338 case ZPOOL_PROP_READONLY:
6339 case ZPOOL_PROP_CACHEFILE:
6341 * 'readonly' and 'cachefile' are also non-persisitent
6345 case ZPOOL_PROP_COMMENT:
6346 strval = fnvpair_value_string(elem);
6347 if (spa->spa_comment != NULL)
6348 spa_strfree(spa->spa_comment);
6349 spa->spa_comment = spa_strdup(strval);
6351 * We need to dirty the configuration on all the vdevs
6352 * so that their labels get updated. It's unnecessary
6353 * to do this for pool creation since the vdev's
6354 * configuratoin has already been dirtied.
6356 if (tx->tx_txg != TXG_INITIAL)
6357 vdev_config_dirty(spa->spa_root_vdev);
6358 spa_history_log_internal(spa, "set", tx,
6359 "%s=%s", nvpair_name(elem), strval);
6363 * Set pool property values in the poolprops mos object.
6365 if (spa->spa_pool_props_object == 0) {
6366 spa->spa_pool_props_object =
6367 zap_create_link(mos, DMU_OT_POOL_PROPS,
6368 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6372 /* normalize the property name */
6373 propname = zpool_prop_to_name(prop);
6374 proptype = zpool_prop_get_type(prop);
6376 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6377 ASSERT(proptype == PROP_TYPE_STRING);
6378 strval = fnvpair_value_string(elem);
6379 VERIFY0(zap_update(mos,
6380 spa->spa_pool_props_object, propname,
6381 1, strlen(strval) + 1, strval, tx));
6382 spa_history_log_internal(spa, "set", tx,
6383 "%s=%s", nvpair_name(elem), strval);
6384 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6385 intval = fnvpair_value_uint64(elem);
6387 if (proptype == PROP_TYPE_INDEX) {
6389 VERIFY0(zpool_prop_index_to_string(
6390 prop, intval, &unused));
6392 VERIFY0(zap_update(mos,
6393 spa->spa_pool_props_object, propname,
6394 8, 1, &intval, tx));
6395 spa_history_log_internal(spa, "set", tx,
6396 "%s=%lld", nvpair_name(elem), intval);
6398 ASSERT(0); /* not allowed */
6402 case ZPOOL_PROP_DELEGATION:
6403 spa->spa_delegation = intval;
6405 case ZPOOL_PROP_BOOTFS:
6406 spa->spa_bootfs = intval;
6408 case ZPOOL_PROP_FAILUREMODE:
6409 spa->spa_failmode = intval;
6411 case ZPOOL_PROP_AUTOEXPAND:
6412 spa->spa_autoexpand = intval;
6413 if (tx->tx_txg != TXG_INITIAL)
6414 spa_async_request(spa,
6415 SPA_ASYNC_AUTOEXPAND);
6417 case ZPOOL_PROP_DEDUPDITTO:
6418 spa->spa_dedup_ditto = intval;
6427 mutex_exit(&spa->spa_props_lock);
6431 * Perform one-time upgrade on-disk changes. spa_version() does not
6432 * reflect the new version this txg, so there must be no changes this
6433 * txg to anything that the upgrade code depends on after it executes.
6434 * Therefore this must be called after dsl_pool_sync() does the sync
6438 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6440 dsl_pool_t *dp = spa->spa_dsl_pool;
6442 ASSERT(spa->spa_sync_pass == 1);
6444 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6446 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6447 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6448 dsl_pool_create_origin(dp, tx);
6450 /* Keeping the origin open increases spa_minref */
6451 spa->spa_minref += 3;
6454 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6455 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6456 dsl_pool_upgrade_clones(dp, tx);
6459 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6460 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6461 dsl_pool_upgrade_dir_clones(dp, tx);
6463 /* Keeping the freedir open increases spa_minref */
6464 spa->spa_minref += 3;
6467 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6468 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6469 spa_feature_create_zap_objects(spa, tx);
6473 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6474 * when possibility to use lz4 compression for metadata was added
6475 * Old pools that have this feature enabled must be upgraded to have
6476 * this feature active
6478 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6479 boolean_t lz4_en = spa_feature_is_enabled(spa,
6480 SPA_FEATURE_LZ4_COMPRESS);
6481 boolean_t lz4_ac = spa_feature_is_active(spa,
6482 SPA_FEATURE_LZ4_COMPRESS);
6484 if (lz4_en && !lz4_ac)
6485 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6487 rrw_exit(&dp->dp_config_rwlock, FTAG);
6491 * Sync the specified transaction group. New blocks may be dirtied as
6492 * part of the process, so we iterate until it converges.
6495 spa_sync(spa_t *spa, uint64_t txg)
6497 dsl_pool_t *dp = spa->spa_dsl_pool;
6498 objset_t *mos = spa->spa_meta_objset;
6499 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6500 vdev_t *rvd = spa->spa_root_vdev;
6505 VERIFY(spa_writeable(spa));
6508 * Lock out configuration changes.
6510 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6512 spa->spa_syncing_txg = txg;
6513 spa->spa_sync_pass = 0;
6516 * If there are any pending vdev state changes, convert them
6517 * into config changes that go out with this transaction group.
6519 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6520 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6522 * We need the write lock here because, for aux vdevs,
6523 * calling vdev_config_dirty() modifies sav_config.
6524 * This is ugly and will become unnecessary when we
6525 * eliminate the aux vdev wart by integrating all vdevs
6526 * into the root vdev tree.
6528 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6529 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6530 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6531 vdev_state_clean(vd);
6532 vdev_config_dirty(vd);
6534 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6535 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6537 spa_config_exit(spa, SCL_STATE, FTAG);
6539 tx = dmu_tx_create_assigned(dp, txg);
6541 spa->spa_sync_starttime = gethrtime();
6543 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6544 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6547 callout_reset(&spa->spa_deadman_cycid,
6548 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6553 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6554 * set spa_deflate if we have no raid-z vdevs.
6556 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6557 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6560 for (i = 0; i < rvd->vdev_children; i++) {
6561 vd = rvd->vdev_child[i];
6562 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6565 if (i == rvd->vdev_children) {
6566 spa->spa_deflate = TRUE;
6567 VERIFY(0 == zap_add(spa->spa_meta_objset,
6568 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6569 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6574 * If anything has changed in this txg, or if someone is waiting
6575 * for this txg to sync (eg, spa_vdev_remove()), push the
6576 * deferred frees from the previous txg. If not, leave them
6577 * alone so that we don't generate work on an otherwise idle
6580 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6581 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6582 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6583 ((dsl_scan_active(dp->dp_scan) ||
6584 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6585 spa_sync_deferred_frees(spa, tx);
6589 * Iterate to convergence.
6592 int pass = ++spa->spa_sync_pass;
6594 spa_sync_config_object(spa, tx);
6595 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6596 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6597 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6598 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6599 spa_errlog_sync(spa, txg);
6600 dsl_pool_sync(dp, txg);
6602 if (pass < zfs_sync_pass_deferred_free) {
6603 spa_sync_frees(spa, free_bpl, tx);
6605 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6606 &spa->spa_deferred_bpobj, tx);
6610 dsl_scan_sync(dp, tx);
6612 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6616 spa_sync_upgrades(spa, tx);
6618 } while (dmu_objset_is_dirty(mos, txg));
6621 * Rewrite the vdev configuration (which includes the uberblock)
6622 * to commit the transaction group.
6624 * If there are no dirty vdevs, we sync the uberblock to a few
6625 * random top-level vdevs that are known to be visible in the
6626 * config cache (see spa_vdev_add() for a complete description).
6627 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6631 * We hold SCL_STATE to prevent vdev open/close/etc.
6632 * while we're attempting to write the vdev labels.
6634 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6636 if (list_is_empty(&spa->spa_config_dirty_list)) {
6637 vdev_t *svd[SPA_DVAS_PER_BP];
6639 int children = rvd->vdev_children;
6640 int c0 = spa_get_random(children);
6642 for (int c = 0; c < children; c++) {
6643 vd = rvd->vdev_child[(c0 + c) % children];
6644 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6646 svd[svdcount++] = vd;
6647 if (svdcount == SPA_DVAS_PER_BP)
6650 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6652 error = vdev_config_sync(svd, svdcount, txg,
6655 error = vdev_config_sync(rvd->vdev_child,
6656 rvd->vdev_children, txg, B_FALSE);
6658 error = vdev_config_sync(rvd->vdev_child,
6659 rvd->vdev_children, txg, B_TRUE);
6663 spa->spa_last_synced_guid = rvd->vdev_guid;
6665 spa_config_exit(spa, SCL_STATE, FTAG);
6669 zio_suspend(spa, NULL);
6670 zio_resume_wait(spa);
6675 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6678 callout_drain(&spa->spa_deadman_cycid);
6683 * Clear the dirty config list.
6685 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6686 vdev_config_clean(vd);
6689 * Now that the new config has synced transactionally,
6690 * let it become visible to the config cache.
6692 if (spa->spa_config_syncing != NULL) {
6693 spa_config_set(spa, spa->spa_config_syncing);
6694 spa->spa_config_txg = txg;
6695 spa->spa_config_syncing = NULL;
6698 spa->spa_ubsync = spa->spa_uberblock;
6700 dsl_pool_sync_done(dp, txg);
6703 * Update usable space statistics.
6705 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6706 vdev_sync_done(vd, txg);
6708 spa_update_dspace(spa);
6711 * It had better be the case that we didn't dirty anything
6712 * since vdev_config_sync().
6714 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6715 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6716 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6718 spa->spa_sync_pass = 0;
6720 spa_config_exit(spa, SCL_CONFIG, FTAG);
6722 spa_handle_ignored_writes(spa);
6725 * If any async tasks have been requested, kick them off.
6727 spa_async_dispatch(spa);
6728 spa_async_dispatch_vd(spa);
6732 * Sync all pools. We don't want to hold the namespace lock across these
6733 * operations, so we take a reference on the spa_t and drop the lock during the
6737 spa_sync_allpools(void)
6740 mutex_enter(&spa_namespace_lock);
6741 while ((spa = spa_next(spa)) != NULL) {
6742 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6743 !spa_writeable(spa) || spa_suspended(spa))
6745 spa_open_ref(spa, FTAG);
6746 mutex_exit(&spa_namespace_lock);
6747 txg_wait_synced(spa_get_dsl(spa), 0);
6748 mutex_enter(&spa_namespace_lock);
6749 spa_close(spa, FTAG);
6751 mutex_exit(&spa_namespace_lock);
6755 * ==========================================================================
6756 * Miscellaneous routines
6757 * ==========================================================================
6761 * Remove all pools in the system.
6769 * Remove all cached state. All pools should be closed now,
6770 * so every spa in the AVL tree should be unreferenced.
6772 mutex_enter(&spa_namespace_lock);
6773 while ((spa = spa_next(NULL)) != NULL) {
6775 * Stop async tasks. The async thread may need to detach
6776 * a device that's been replaced, which requires grabbing
6777 * spa_namespace_lock, so we must drop it here.
6779 spa_open_ref(spa, FTAG);
6780 mutex_exit(&spa_namespace_lock);
6781 spa_async_suspend(spa);
6782 mutex_enter(&spa_namespace_lock);
6783 spa_close(spa, FTAG);
6785 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6787 spa_deactivate(spa);
6791 mutex_exit(&spa_namespace_lock);
6795 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6800 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6804 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6805 vd = spa->spa_l2cache.sav_vdevs[i];
6806 if (vd->vdev_guid == guid)
6810 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6811 vd = spa->spa_spares.sav_vdevs[i];
6812 if (vd->vdev_guid == guid)
6821 spa_upgrade(spa_t *spa, uint64_t version)
6823 ASSERT(spa_writeable(spa));
6825 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6828 * This should only be called for a non-faulted pool, and since a
6829 * future version would result in an unopenable pool, this shouldn't be
6832 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6833 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6835 spa->spa_uberblock.ub_version = version;
6836 vdev_config_dirty(spa->spa_root_vdev);
6838 spa_config_exit(spa, SCL_ALL, FTAG);
6840 txg_wait_synced(spa_get_dsl(spa), 0);
6844 spa_has_spare(spa_t *spa, uint64_t guid)
6848 spa_aux_vdev_t *sav = &spa->spa_spares;
6850 for (i = 0; i < sav->sav_count; i++)
6851 if (sav->sav_vdevs[i]->vdev_guid == guid)
6854 for (i = 0; i < sav->sav_npending; i++) {
6855 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6856 &spareguid) == 0 && spareguid == guid)
6864 * Check if a pool has an active shared spare device.
6865 * Note: reference count of an active spare is 2, as a spare and as a replace
6868 spa_has_active_shared_spare(spa_t *spa)
6872 spa_aux_vdev_t *sav = &spa->spa_spares;
6874 for (i = 0; i < sav->sav_count; i++) {
6875 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6876 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6885 * Post a sysevent corresponding to the given event. The 'name' must be one of
6886 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6887 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6888 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6889 * or zdb as real changes.
6892 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6896 sysevent_attr_list_t *attr = NULL;
6897 sysevent_value_t value;
6900 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6903 value.value_type = SE_DATA_TYPE_STRING;
6904 value.value.sv_string = spa_name(spa);
6905 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6908 value.value_type = SE_DATA_TYPE_UINT64;
6909 value.value.sv_uint64 = spa_guid(spa);
6910 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6914 value.value_type = SE_DATA_TYPE_UINT64;
6915 value.value.sv_uint64 = vd->vdev_guid;
6916 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6920 if (vd->vdev_path) {
6921 value.value_type = SE_DATA_TYPE_STRING;
6922 value.value.sv_string = vd->vdev_path;
6923 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6924 &value, SE_SLEEP) != 0)
6929 if (sysevent_attach_attributes(ev, attr) != 0)
6933 (void) log_sysevent(ev, SE_SLEEP, &eid);
6937 sysevent_free_attr(attr);