4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * SPA: Storage Pool Allocator
32 * This file contains all the routines used when modifying on-disk SPA state.
33 * This includes opening, importing, destroying, exporting a pool, and syncing a
37 #include <sys/zfs_context.h>
38 #include <sys/fm/fs/zfs.h>
39 #include <sys/spa_impl.h>
41 #include <sys/zio_checksum.h>
43 #include <sys/dmu_tx.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/metaslab.h>
49 #include <sys/metaslab_impl.h>
50 #include <sys/uberblock_impl.h>
53 #include <sys/dmu_traverse.h>
54 #include <sys/dmu_objset.h>
55 #include <sys/unique.h>
56 #include <sys/dsl_pool.h>
57 #include <sys/dsl_dataset.h>
58 #include <sys/dsl_dir.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/dsl_synctask.h>
61 #include <sys/fs/zfs.h>
63 #include <sys/callb.h>
64 #include <sys/spa_boot.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/dsl_scan.h>
67 #include <sys/dmu_send.h>
68 #include <sys/dsl_destroy.h>
69 #include <sys/dsl_userhold.h>
70 #include <sys/zfeature.h>
72 #include <sys/trim_map.h>
75 #include <sys/callb.h>
76 #include <sys/cpupart.h>
81 #include "zfs_comutil.h"
83 /* Check hostid on import? */
84 static int check_hostid = 1;
86 SYSCTL_DECL(_vfs_zfs);
87 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
88 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RW, &check_hostid, 0,
89 "Check hostid on import?");
92 * The interval, in seconds, at which failed configuration cache file writes
95 static int zfs_ccw_retry_interval = 300;
97 typedef enum zti_modes {
98 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
99 ZTI_MODE_ONLINE_PERCENT, /* value is % of online CPUs */
100 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
101 ZTI_MODE_NULL, /* don't create a taskq */
105 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
106 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
107 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
108 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
110 #define ZTI_N(n) ZTI_P(n, 1)
111 #define ZTI_ONE ZTI_N(1)
113 typedef struct zio_taskq_info {
114 zti_modes_t zti_mode;
119 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
120 "issue", "issue_high", "intr", "intr_high"
124 * This table defines the taskq settings for each ZFS I/O type. When
125 * initializing a pool, we use this table to create an appropriately sized
126 * taskq. Some operations are low volume and therefore have a small, static
127 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
128 * macros. Other operations process a large amount of data; the ZTI_BATCH
129 * macro causes us to create a taskq oriented for throughput. Some operations
130 * are so high frequency and short-lived that the taskq itself can become a a
131 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
132 * additional degree of parallelism specified by the number of threads per-
133 * taskq and the number of taskqs; when dispatching an event in this case, the
134 * particular taskq is chosen at random.
136 * The different taskq priorities are to handle the different contexts (issue
137 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
138 * need to be handled with minimum delay.
140 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
141 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
142 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
143 { ZTI_N(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, /* READ */
144 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
145 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
146 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
147 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
150 static void spa_sync_version(void *arg, dmu_tx_t *tx);
151 static void spa_sync_props(void *arg, dmu_tx_t *tx);
152 static boolean_t spa_has_active_shared_spare(spa_t *spa);
153 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
154 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
156 static void spa_vdev_resilver_done(spa_t *spa);
158 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
160 id_t zio_taskq_psrset_bind = PS_NONE;
163 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
165 uint_t zio_taskq_basedc = 80; /* base duty cycle */
167 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
168 extern int zfs_sync_pass_deferred_free;
171 extern void spa_deadman(void *arg);
175 * This (illegal) pool name is used when temporarily importing a spa_t in order
176 * to get the vdev stats associated with the imported devices.
178 #define TRYIMPORT_NAME "$import"
181 * ==========================================================================
182 * SPA properties routines
183 * ==========================================================================
187 * Add a (source=src, propname=propval) list to an nvlist.
190 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
191 uint64_t intval, zprop_source_t src)
193 const char *propname = zpool_prop_to_name(prop);
196 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
197 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
200 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
202 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
204 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
205 nvlist_free(propval);
209 * Get property values from the spa configuration.
212 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
214 vdev_t *rvd = spa->spa_root_vdev;
215 dsl_pool_t *pool = spa->spa_dsl_pool;
219 uint64_t cap, version;
220 zprop_source_t src = ZPROP_SRC_NONE;
221 spa_config_dirent_t *dp;
223 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
226 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
227 size = metaslab_class_get_space(spa_normal_class(spa));
228 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
229 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
230 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
231 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
235 for (int c = 0; c < rvd->vdev_children; c++) {
236 vdev_t *tvd = rvd->vdev_child[c];
237 space += tvd->vdev_max_asize - tvd->vdev_asize;
239 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
242 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
243 (spa_mode(spa) == FREAD), src);
245 cap = (size == 0) ? 0 : (alloc * 100 / size);
246 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
248 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
249 ddt_get_pool_dedup_ratio(spa), src);
251 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
252 rvd->vdev_state, src);
254 version = spa_version(spa);
255 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
256 src = ZPROP_SRC_DEFAULT;
258 src = ZPROP_SRC_LOCAL;
259 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
263 dsl_dir_t *freedir = pool->dp_free_dir;
266 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
267 * when opening pools before this version freedir will be NULL.
269 if (freedir != NULL) {
270 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
271 freedir->dd_phys->dd_used_bytes, src);
273 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
278 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
280 if (spa->spa_comment != NULL) {
281 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
285 if (spa->spa_root != NULL)
286 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
289 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
290 if (dp->scd_path == NULL) {
291 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
292 "none", 0, ZPROP_SRC_LOCAL);
293 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
294 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
295 dp->scd_path, 0, ZPROP_SRC_LOCAL);
301 * Get zpool property values.
304 spa_prop_get(spa_t *spa, nvlist_t **nvp)
306 objset_t *mos = spa->spa_meta_objset;
311 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
313 mutex_enter(&spa->spa_props_lock);
316 * Get properties from the spa config.
318 spa_prop_get_config(spa, nvp);
320 /* If no pool property object, no more prop to get. */
321 if (mos == NULL || spa->spa_pool_props_object == 0) {
322 mutex_exit(&spa->spa_props_lock);
327 * Get properties from the MOS pool property object.
329 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
330 (err = zap_cursor_retrieve(&zc, &za)) == 0;
331 zap_cursor_advance(&zc)) {
334 zprop_source_t src = ZPROP_SRC_DEFAULT;
337 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
340 switch (za.za_integer_length) {
342 /* integer property */
343 if (za.za_first_integer !=
344 zpool_prop_default_numeric(prop))
345 src = ZPROP_SRC_LOCAL;
347 if (prop == ZPOOL_PROP_BOOTFS) {
349 dsl_dataset_t *ds = NULL;
351 dp = spa_get_dsl(spa);
352 dsl_pool_config_enter(dp, FTAG);
353 if (err = dsl_dataset_hold_obj(dp,
354 za.za_first_integer, FTAG, &ds)) {
355 dsl_pool_config_exit(dp, FTAG);
360 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
362 dsl_dataset_name(ds, strval);
363 dsl_dataset_rele(ds, FTAG);
364 dsl_pool_config_exit(dp, FTAG);
367 intval = za.za_first_integer;
370 spa_prop_add_list(*nvp, prop, strval, intval, src);
374 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
379 /* string property */
380 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
381 err = zap_lookup(mos, spa->spa_pool_props_object,
382 za.za_name, 1, za.za_num_integers, strval);
384 kmem_free(strval, za.za_num_integers);
387 spa_prop_add_list(*nvp, prop, strval, 0, src);
388 kmem_free(strval, za.za_num_integers);
395 zap_cursor_fini(&zc);
396 mutex_exit(&spa->spa_props_lock);
398 if (err && err != ENOENT) {
408 * Validate the given pool properties nvlist and modify the list
409 * for the property values to be set.
412 spa_prop_validate(spa_t *spa, nvlist_t *props)
415 int error = 0, reset_bootfs = 0;
417 boolean_t has_feature = B_FALSE;
420 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
422 char *strval, *slash, *check, *fname;
423 const char *propname = nvpair_name(elem);
424 zpool_prop_t prop = zpool_name_to_prop(propname);
428 if (!zpool_prop_feature(propname)) {
429 error = SET_ERROR(EINVAL);
434 * Sanitize the input.
436 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
437 error = SET_ERROR(EINVAL);
441 if (nvpair_value_uint64(elem, &intval) != 0) {
442 error = SET_ERROR(EINVAL);
447 error = SET_ERROR(EINVAL);
451 fname = strchr(propname, '@') + 1;
452 if (zfeature_lookup_name(fname, NULL) != 0) {
453 error = SET_ERROR(EINVAL);
457 has_feature = B_TRUE;
460 case ZPOOL_PROP_VERSION:
461 error = nvpair_value_uint64(elem, &intval);
463 (intval < spa_version(spa) ||
464 intval > SPA_VERSION_BEFORE_FEATURES ||
466 error = SET_ERROR(EINVAL);
469 case ZPOOL_PROP_DELEGATION:
470 case ZPOOL_PROP_AUTOREPLACE:
471 case ZPOOL_PROP_LISTSNAPS:
472 case ZPOOL_PROP_AUTOEXPAND:
473 error = nvpair_value_uint64(elem, &intval);
474 if (!error && intval > 1)
475 error = SET_ERROR(EINVAL);
478 case ZPOOL_PROP_BOOTFS:
480 * If the pool version is less than SPA_VERSION_BOOTFS,
481 * or the pool is still being created (version == 0),
482 * the bootfs property cannot be set.
484 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
485 error = SET_ERROR(ENOTSUP);
490 * Make sure the vdev config is bootable
492 if (!vdev_is_bootable(spa->spa_root_vdev)) {
493 error = SET_ERROR(ENOTSUP);
499 error = nvpair_value_string(elem, &strval);
505 if (strval == NULL || strval[0] == '\0') {
506 objnum = zpool_prop_default_numeric(
511 if (error = dmu_objset_hold(strval, FTAG, &os))
514 /* Must be ZPL and not gzip compressed. */
516 if (dmu_objset_type(os) != DMU_OST_ZFS) {
517 error = SET_ERROR(ENOTSUP);
519 dsl_prop_get_int_ds(dmu_objset_ds(os),
520 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
522 !BOOTFS_COMPRESS_VALID(compress)) {
523 error = SET_ERROR(ENOTSUP);
525 objnum = dmu_objset_id(os);
527 dmu_objset_rele(os, FTAG);
531 case ZPOOL_PROP_FAILUREMODE:
532 error = nvpair_value_uint64(elem, &intval);
533 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
534 intval > ZIO_FAILURE_MODE_PANIC))
535 error = SET_ERROR(EINVAL);
538 * This is a special case which only occurs when
539 * the pool has completely failed. This allows
540 * the user to change the in-core failmode property
541 * without syncing it out to disk (I/Os might
542 * currently be blocked). We do this by returning
543 * EIO to the caller (spa_prop_set) to trick it
544 * into thinking we encountered a property validation
547 if (!error && spa_suspended(spa)) {
548 spa->spa_failmode = intval;
549 error = SET_ERROR(EIO);
553 case ZPOOL_PROP_CACHEFILE:
554 if ((error = nvpair_value_string(elem, &strval)) != 0)
557 if (strval[0] == '\0')
560 if (strcmp(strval, "none") == 0)
563 if (strval[0] != '/') {
564 error = SET_ERROR(EINVAL);
568 slash = strrchr(strval, '/');
569 ASSERT(slash != NULL);
571 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
572 strcmp(slash, "/..") == 0)
573 error = SET_ERROR(EINVAL);
576 case ZPOOL_PROP_COMMENT:
577 if ((error = nvpair_value_string(elem, &strval)) != 0)
579 for (check = strval; *check != '\0'; check++) {
581 * The kernel doesn't have an easy isprint()
582 * check. For this kernel check, we merely
583 * check ASCII apart from DEL. Fix this if
584 * there is an easy-to-use kernel isprint().
586 if (*check >= 0x7f) {
587 error = SET_ERROR(EINVAL);
592 if (strlen(strval) > ZPROP_MAX_COMMENT)
596 case ZPOOL_PROP_DEDUPDITTO:
597 if (spa_version(spa) < SPA_VERSION_DEDUP)
598 error = SET_ERROR(ENOTSUP);
600 error = nvpair_value_uint64(elem, &intval);
602 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
603 error = SET_ERROR(EINVAL);
611 if (!error && reset_bootfs) {
612 error = nvlist_remove(props,
613 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
616 error = nvlist_add_uint64(props,
617 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
625 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
628 spa_config_dirent_t *dp;
630 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
634 dp = kmem_alloc(sizeof (spa_config_dirent_t),
637 if (cachefile[0] == '\0')
638 dp->scd_path = spa_strdup(spa_config_path);
639 else if (strcmp(cachefile, "none") == 0)
642 dp->scd_path = spa_strdup(cachefile);
644 list_insert_head(&spa->spa_config_list, dp);
646 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
650 spa_prop_set(spa_t *spa, nvlist_t *nvp)
653 nvpair_t *elem = NULL;
654 boolean_t need_sync = B_FALSE;
656 if ((error = spa_prop_validate(spa, nvp)) != 0)
659 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
660 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
662 if (prop == ZPOOL_PROP_CACHEFILE ||
663 prop == ZPOOL_PROP_ALTROOT ||
664 prop == ZPOOL_PROP_READONLY)
667 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
670 if (prop == ZPOOL_PROP_VERSION) {
671 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
673 ASSERT(zpool_prop_feature(nvpair_name(elem)));
674 ver = SPA_VERSION_FEATURES;
678 /* Save time if the version is already set. */
679 if (ver == spa_version(spa))
683 * In addition to the pool directory object, we might
684 * create the pool properties object, the features for
685 * read object, the features for write object, or the
686 * feature descriptions object.
688 error = dsl_sync_task(spa->spa_name, NULL,
689 spa_sync_version, &ver, 6);
700 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
708 * If the bootfs property value is dsobj, clear it.
711 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
713 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
714 VERIFY(zap_remove(spa->spa_meta_objset,
715 spa->spa_pool_props_object,
716 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
723 spa_change_guid_check(void *arg, dmu_tx_t *tx)
725 uint64_t *newguid = arg;
726 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
727 vdev_t *rvd = spa->spa_root_vdev;
730 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
731 vdev_state = rvd->vdev_state;
732 spa_config_exit(spa, SCL_STATE, FTAG);
734 if (vdev_state != VDEV_STATE_HEALTHY)
735 return (SET_ERROR(ENXIO));
737 ASSERT3U(spa_guid(spa), !=, *newguid);
743 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
745 uint64_t *newguid = arg;
746 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
748 vdev_t *rvd = spa->spa_root_vdev;
750 oldguid = spa_guid(spa);
752 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
753 rvd->vdev_guid = *newguid;
754 rvd->vdev_guid_sum += (*newguid - oldguid);
755 vdev_config_dirty(rvd);
756 spa_config_exit(spa, SCL_STATE, FTAG);
758 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
763 * Change the GUID for the pool. This is done so that we can later
764 * re-import a pool built from a clone of our own vdevs. We will modify
765 * the root vdev's guid, our own pool guid, and then mark all of our
766 * vdevs dirty. Note that we must make sure that all our vdevs are
767 * online when we do this, or else any vdevs that weren't present
768 * would be orphaned from our pool. We are also going to issue a
769 * sysevent to update any watchers.
772 spa_change_guid(spa_t *spa)
777 mutex_enter(&spa->spa_vdev_top_lock);
778 mutex_enter(&spa_namespace_lock);
779 guid = spa_generate_guid(NULL);
781 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
782 spa_change_guid_sync, &guid, 5);
785 spa_config_sync(spa, B_FALSE, B_TRUE);
786 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
789 mutex_exit(&spa_namespace_lock);
790 mutex_exit(&spa->spa_vdev_top_lock);
796 * ==========================================================================
797 * SPA state manipulation (open/create/destroy/import/export)
798 * ==========================================================================
802 spa_error_entry_compare(const void *a, const void *b)
804 spa_error_entry_t *sa = (spa_error_entry_t *)a;
805 spa_error_entry_t *sb = (spa_error_entry_t *)b;
808 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
809 sizeof (zbookmark_t));
820 * Utility function which retrieves copies of the current logs and
821 * re-initializes them in the process.
824 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
826 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
828 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
829 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
831 avl_create(&spa->spa_errlist_scrub,
832 spa_error_entry_compare, sizeof (spa_error_entry_t),
833 offsetof(spa_error_entry_t, se_avl));
834 avl_create(&spa->spa_errlist_last,
835 spa_error_entry_compare, sizeof (spa_error_entry_t),
836 offsetof(spa_error_entry_t, se_avl));
840 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
842 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
843 enum zti_modes mode = ztip->zti_mode;
844 uint_t value = ztip->zti_value;
845 uint_t count = ztip->zti_count;
846 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
849 boolean_t batch = B_FALSE;
851 if (mode == ZTI_MODE_NULL) {
853 tqs->stqs_taskq = NULL;
857 ASSERT3U(count, >, 0);
859 tqs->stqs_count = count;
860 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
862 for (uint_t i = 0; i < count; i++) {
867 ASSERT3U(value, >=, 1);
868 value = MAX(value, 1);
873 flags |= TASKQ_THREADS_CPU_PCT;
874 value = zio_taskq_batch_pct;
877 case ZTI_MODE_ONLINE_PERCENT:
878 flags |= TASKQ_THREADS_CPU_PCT;
882 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
884 zio_type_name[t], zio_taskq_types[q], mode, value);
889 (void) snprintf(name, sizeof (name), "%s_%s_%u",
890 zio_type_name[t], zio_taskq_types[q], i);
892 (void) snprintf(name, sizeof (name), "%s_%s",
893 zio_type_name[t], zio_taskq_types[q]);
897 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
899 flags |= TASKQ_DC_BATCH;
901 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
902 spa->spa_proc, zio_taskq_basedc, flags);
905 tq = taskq_create_proc(name, value, maxclsyspri, 50,
906 INT_MAX, spa->spa_proc, flags);
911 tqs->stqs_taskq[i] = tq;
916 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
918 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
920 if (tqs->stqs_taskq == NULL) {
921 ASSERT0(tqs->stqs_count);
925 for (uint_t i = 0; i < tqs->stqs_count; i++) {
926 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
927 taskq_destroy(tqs->stqs_taskq[i]);
930 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
931 tqs->stqs_taskq = NULL;
935 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
936 * Note that a type may have multiple discrete taskqs to avoid lock contention
937 * on the taskq itself. In that case we choose which taskq at random by using
938 * the low bits of gethrtime().
941 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
942 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
944 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
947 ASSERT3P(tqs->stqs_taskq, !=, NULL);
948 ASSERT3U(tqs->stqs_count, !=, 0);
950 if (tqs->stqs_count == 1) {
951 tq = tqs->stqs_taskq[0];
953 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
956 taskq_dispatch_ent(tq, func, arg, flags, ent);
960 spa_create_zio_taskqs(spa_t *spa)
962 for (int t = 0; t < ZIO_TYPES; t++) {
963 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
964 spa_taskqs_init(spa, t, q);
972 spa_thread(void *arg)
977 user_t *pu = PTOU(curproc);
979 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
982 ASSERT(curproc != &p0);
983 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
984 "zpool-%s", spa->spa_name);
985 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
988 /* bind this thread to the requested psrset */
989 if (zio_taskq_psrset_bind != PS_NONE) {
991 mutex_enter(&cpu_lock);
992 mutex_enter(&pidlock);
993 mutex_enter(&curproc->p_lock);
995 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
996 0, NULL, NULL) == 0) {
997 curthread->t_bind_pset = zio_taskq_psrset_bind;
1000 "Couldn't bind process for zfs pool \"%s\" to "
1001 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1004 mutex_exit(&curproc->p_lock);
1005 mutex_exit(&pidlock);
1006 mutex_exit(&cpu_lock);
1012 if (zio_taskq_sysdc) {
1013 sysdc_thread_enter(curthread, 100, 0);
1017 spa->spa_proc = curproc;
1018 spa->spa_did = curthread->t_did;
1020 spa_create_zio_taskqs(spa);
1022 mutex_enter(&spa->spa_proc_lock);
1023 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1025 spa->spa_proc_state = SPA_PROC_ACTIVE;
1026 cv_broadcast(&spa->spa_proc_cv);
1028 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1029 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1030 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1031 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1033 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1034 spa->spa_proc_state = SPA_PROC_GONE;
1035 spa->spa_proc = &p0;
1036 cv_broadcast(&spa->spa_proc_cv);
1037 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1039 mutex_enter(&curproc->p_lock);
1042 #endif /* SPA_PROCESS */
1046 * Activate an uninitialized pool.
1049 spa_activate(spa_t *spa, int mode)
1051 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1053 spa->spa_state = POOL_STATE_ACTIVE;
1054 spa->spa_mode = mode;
1056 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1057 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1059 /* Try to create a covering process */
1060 mutex_enter(&spa->spa_proc_lock);
1061 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1062 ASSERT(spa->spa_proc == &p0);
1066 /* Only create a process if we're going to be around a while. */
1067 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1068 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1070 spa->spa_proc_state = SPA_PROC_CREATED;
1071 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1072 cv_wait(&spa->spa_proc_cv,
1073 &spa->spa_proc_lock);
1075 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1076 ASSERT(spa->spa_proc != &p0);
1077 ASSERT(spa->spa_did != 0);
1081 "Couldn't create process for zfs pool \"%s\"\n",
1086 #endif /* SPA_PROCESS */
1087 mutex_exit(&spa->spa_proc_lock);
1089 /* If we didn't create a process, we need to create our taskqs. */
1090 ASSERT(spa->spa_proc == &p0);
1091 if (spa->spa_proc == &p0) {
1092 spa_create_zio_taskqs(spa);
1096 * Start TRIM thread.
1098 trim_thread_create(spa);
1100 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1101 offsetof(vdev_t, vdev_config_dirty_node));
1102 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1103 offsetof(vdev_t, vdev_state_dirty_node));
1105 txg_list_create(&spa->spa_vdev_txg_list,
1106 offsetof(struct vdev, vdev_txg_node));
1108 avl_create(&spa->spa_errlist_scrub,
1109 spa_error_entry_compare, sizeof (spa_error_entry_t),
1110 offsetof(spa_error_entry_t, se_avl));
1111 avl_create(&spa->spa_errlist_last,
1112 spa_error_entry_compare, sizeof (spa_error_entry_t),
1113 offsetof(spa_error_entry_t, se_avl));
1117 * Opposite of spa_activate().
1120 spa_deactivate(spa_t *spa)
1122 ASSERT(spa->spa_sync_on == B_FALSE);
1123 ASSERT(spa->spa_dsl_pool == NULL);
1124 ASSERT(spa->spa_root_vdev == NULL);
1125 ASSERT(spa->spa_async_zio_root == NULL);
1126 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1129 * Stop TRIM thread in case spa_unload() wasn't called directly
1130 * before spa_deactivate().
1132 trim_thread_destroy(spa);
1134 txg_list_destroy(&spa->spa_vdev_txg_list);
1136 list_destroy(&spa->spa_config_dirty_list);
1137 list_destroy(&spa->spa_state_dirty_list);
1139 for (int t = 0; t < ZIO_TYPES; t++) {
1140 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1141 spa_taskqs_fini(spa, t, q);
1145 metaslab_class_destroy(spa->spa_normal_class);
1146 spa->spa_normal_class = NULL;
1148 metaslab_class_destroy(spa->spa_log_class);
1149 spa->spa_log_class = NULL;
1152 * If this was part of an import or the open otherwise failed, we may
1153 * still have errors left in the queues. Empty them just in case.
1155 spa_errlog_drain(spa);
1157 avl_destroy(&spa->spa_errlist_scrub);
1158 avl_destroy(&spa->spa_errlist_last);
1160 spa->spa_state = POOL_STATE_UNINITIALIZED;
1162 mutex_enter(&spa->spa_proc_lock);
1163 if (spa->spa_proc_state != SPA_PROC_NONE) {
1164 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1165 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1166 cv_broadcast(&spa->spa_proc_cv);
1167 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1168 ASSERT(spa->spa_proc != &p0);
1169 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1171 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1172 spa->spa_proc_state = SPA_PROC_NONE;
1174 ASSERT(spa->spa_proc == &p0);
1175 mutex_exit(&spa->spa_proc_lock);
1179 * We want to make sure spa_thread() has actually exited the ZFS
1180 * module, so that the module can't be unloaded out from underneath
1183 if (spa->spa_did != 0) {
1184 thread_join(spa->spa_did);
1187 #endif /* SPA_PROCESS */
1191 * Verify a pool configuration, and construct the vdev tree appropriately. This
1192 * will create all the necessary vdevs in the appropriate layout, with each vdev
1193 * in the CLOSED state. This will prep the pool before open/creation/import.
1194 * All vdev validation is done by the vdev_alloc() routine.
1197 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1198 uint_t id, int atype)
1204 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1207 if ((*vdp)->vdev_ops->vdev_op_leaf)
1210 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1213 if (error == ENOENT)
1219 return (SET_ERROR(EINVAL));
1222 for (int c = 0; c < children; c++) {
1224 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1232 ASSERT(*vdp != NULL);
1238 * Opposite of spa_load().
1241 spa_unload(spa_t *spa)
1245 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1250 trim_thread_destroy(spa);
1255 spa_async_suspend(spa);
1260 if (spa->spa_sync_on) {
1261 txg_sync_stop(spa->spa_dsl_pool);
1262 spa->spa_sync_on = B_FALSE;
1266 * Wait for any outstanding async I/O to complete.
1268 if (spa->spa_async_zio_root != NULL) {
1269 (void) zio_wait(spa->spa_async_zio_root);
1270 spa->spa_async_zio_root = NULL;
1273 bpobj_close(&spa->spa_deferred_bpobj);
1276 * Close the dsl pool.
1278 if (spa->spa_dsl_pool) {
1279 dsl_pool_close(spa->spa_dsl_pool);
1280 spa->spa_dsl_pool = NULL;
1281 spa->spa_meta_objset = NULL;
1286 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1289 * Drop and purge level 2 cache
1291 spa_l2cache_drop(spa);
1296 if (spa->spa_root_vdev)
1297 vdev_free(spa->spa_root_vdev);
1298 ASSERT(spa->spa_root_vdev == NULL);
1300 for (i = 0; i < spa->spa_spares.sav_count; i++)
1301 vdev_free(spa->spa_spares.sav_vdevs[i]);
1302 if (spa->spa_spares.sav_vdevs) {
1303 kmem_free(spa->spa_spares.sav_vdevs,
1304 spa->spa_spares.sav_count * sizeof (void *));
1305 spa->spa_spares.sav_vdevs = NULL;
1307 if (spa->spa_spares.sav_config) {
1308 nvlist_free(spa->spa_spares.sav_config);
1309 spa->spa_spares.sav_config = NULL;
1311 spa->spa_spares.sav_count = 0;
1313 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1314 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1315 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1317 if (spa->spa_l2cache.sav_vdevs) {
1318 kmem_free(spa->spa_l2cache.sav_vdevs,
1319 spa->spa_l2cache.sav_count * sizeof (void *));
1320 spa->spa_l2cache.sav_vdevs = NULL;
1322 if (spa->spa_l2cache.sav_config) {
1323 nvlist_free(spa->spa_l2cache.sav_config);
1324 spa->spa_l2cache.sav_config = NULL;
1326 spa->spa_l2cache.sav_count = 0;
1328 spa->spa_async_suspended = 0;
1330 if (spa->spa_comment != NULL) {
1331 spa_strfree(spa->spa_comment);
1332 spa->spa_comment = NULL;
1335 spa_config_exit(spa, SCL_ALL, FTAG);
1339 * Load (or re-load) the current list of vdevs describing the active spares for
1340 * this pool. When this is called, we have some form of basic information in
1341 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1342 * then re-generate a more complete list including status information.
1345 spa_load_spares(spa_t *spa)
1352 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1355 * First, close and free any existing spare vdevs.
1357 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1358 vd = spa->spa_spares.sav_vdevs[i];
1360 /* Undo the call to spa_activate() below */
1361 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1362 B_FALSE)) != NULL && tvd->vdev_isspare)
1363 spa_spare_remove(tvd);
1368 if (spa->spa_spares.sav_vdevs)
1369 kmem_free(spa->spa_spares.sav_vdevs,
1370 spa->spa_spares.sav_count * sizeof (void *));
1372 if (spa->spa_spares.sav_config == NULL)
1375 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1376 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1378 spa->spa_spares.sav_count = (int)nspares;
1379 spa->spa_spares.sav_vdevs = NULL;
1385 * Construct the array of vdevs, opening them to get status in the
1386 * process. For each spare, there is potentially two different vdev_t
1387 * structures associated with it: one in the list of spares (used only
1388 * for basic validation purposes) and one in the active vdev
1389 * configuration (if it's spared in). During this phase we open and
1390 * validate each vdev on the spare list. If the vdev also exists in the
1391 * active configuration, then we also mark this vdev as an active spare.
1393 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1395 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1396 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1397 VDEV_ALLOC_SPARE) == 0);
1400 spa->spa_spares.sav_vdevs[i] = vd;
1402 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1403 B_FALSE)) != NULL) {
1404 if (!tvd->vdev_isspare)
1408 * We only mark the spare active if we were successfully
1409 * able to load the vdev. Otherwise, importing a pool
1410 * with a bad active spare would result in strange
1411 * behavior, because multiple pool would think the spare
1412 * is actively in use.
1414 * There is a vulnerability here to an equally bizarre
1415 * circumstance, where a dead active spare is later
1416 * brought back to life (onlined or otherwise). Given
1417 * the rarity of this scenario, and the extra complexity
1418 * it adds, we ignore the possibility.
1420 if (!vdev_is_dead(tvd))
1421 spa_spare_activate(tvd);
1425 vd->vdev_aux = &spa->spa_spares;
1427 if (vdev_open(vd) != 0)
1430 if (vdev_validate_aux(vd) == 0)
1435 * Recompute the stashed list of spares, with status information
1438 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1439 DATA_TYPE_NVLIST_ARRAY) == 0);
1441 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1443 for (i = 0; i < spa->spa_spares.sav_count; i++)
1444 spares[i] = vdev_config_generate(spa,
1445 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1446 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1447 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1448 for (i = 0; i < spa->spa_spares.sav_count; i++)
1449 nvlist_free(spares[i]);
1450 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1454 * Load (or re-load) the current list of vdevs describing the active l2cache for
1455 * this pool. When this is called, we have some form of basic information in
1456 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1457 * then re-generate a more complete list including status information.
1458 * Devices which are already active have their details maintained, and are
1462 spa_load_l2cache(spa_t *spa)
1466 int i, j, oldnvdevs;
1468 vdev_t *vd, **oldvdevs, **newvdevs;
1469 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1471 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1473 if (sav->sav_config != NULL) {
1474 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1475 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1476 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1482 oldvdevs = sav->sav_vdevs;
1483 oldnvdevs = sav->sav_count;
1484 sav->sav_vdevs = NULL;
1488 * Process new nvlist of vdevs.
1490 for (i = 0; i < nl2cache; i++) {
1491 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1495 for (j = 0; j < oldnvdevs; j++) {
1497 if (vd != NULL && guid == vd->vdev_guid) {
1499 * Retain previous vdev for add/remove ops.
1507 if (newvdevs[i] == NULL) {
1511 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1512 VDEV_ALLOC_L2CACHE) == 0);
1517 * Commit this vdev as an l2cache device,
1518 * even if it fails to open.
1520 spa_l2cache_add(vd);
1525 spa_l2cache_activate(vd);
1527 if (vdev_open(vd) != 0)
1530 (void) vdev_validate_aux(vd);
1532 if (!vdev_is_dead(vd))
1533 l2arc_add_vdev(spa, vd);
1538 * Purge vdevs that were dropped
1540 for (i = 0; i < oldnvdevs; i++) {
1545 ASSERT(vd->vdev_isl2cache);
1547 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1548 pool != 0ULL && l2arc_vdev_present(vd))
1549 l2arc_remove_vdev(vd);
1550 vdev_clear_stats(vd);
1556 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1558 if (sav->sav_config == NULL)
1561 sav->sav_vdevs = newvdevs;
1562 sav->sav_count = (int)nl2cache;
1565 * Recompute the stashed list of l2cache devices, with status
1566 * information this time.
1568 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1569 DATA_TYPE_NVLIST_ARRAY) == 0);
1571 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1572 for (i = 0; i < sav->sav_count; i++)
1573 l2cache[i] = vdev_config_generate(spa,
1574 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1575 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1576 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1578 for (i = 0; i < sav->sav_count; i++)
1579 nvlist_free(l2cache[i]);
1581 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1585 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1588 char *packed = NULL;
1593 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
1594 nvsize = *(uint64_t *)db->db_data;
1595 dmu_buf_rele(db, FTAG);
1597 packed = kmem_alloc(nvsize, KM_SLEEP);
1598 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1601 error = nvlist_unpack(packed, nvsize, value, 0);
1602 kmem_free(packed, nvsize);
1608 * Checks to see if the given vdev could not be opened, in which case we post a
1609 * sysevent to notify the autoreplace code that the device has been removed.
1612 spa_check_removed(vdev_t *vd)
1614 for (int c = 0; c < vd->vdev_children; c++)
1615 spa_check_removed(vd->vdev_child[c]);
1617 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1619 zfs_post_autoreplace(vd->vdev_spa, vd);
1620 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1625 * Validate the current config against the MOS config
1628 spa_config_valid(spa_t *spa, nvlist_t *config)
1630 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1633 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1635 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1636 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1638 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1641 * If we're doing a normal import, then build up any additional
1642 * diagnostic information about missing devices in this config.
1643 * We'll pass this up to the user for further processing.
1645 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1646 nvlist_t **child, *nv;
1649 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1651 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1653 for (int c = 0; c < rvd->vdev_children; c++) {
1654 vdev_t *tvd = rvd->vdev_child[c];
1655 vdev_t *mtvd = mrvd->vdev_child[c];
1657 if (tvd->vdev_ops == &vdev_missing_ops &&
1658 mtvd->vdev_ops != &vdev_missing_ops &&
1660 child[idx++] = vdev_config_generate(spa, mtvd,
1665 VERIFY(nvlist_add_nvlist_array(nv,
1666 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1667 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1668 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1670 for (int i = 0; i < idx; i++)
1671 nvlist_free(child[i]);
1674 kmem_free(child, rvd->vdev_children * sizeof (char **));
1678 * Compare the root vdev tree with the information we have
1679 * from the MOS config (mrvd). Check each top-level vdev
1680 * with the corresponding MOS config top-level (mtvd).
1682 for (int c = 0; c < rvd->vdev_children; c++) {
1683 vdev_t *tvd = rvd->vdev_child[c];
1684 vdev_t *mtvd = mrvd->vdev_child[c];
1687 * Resolve any "missing" vdevs in the current configuration.
1688 * If we find that the MOS config has more accurate information
1689 * about the top-level vdev then use that vdev instead.
1691 if (tvd->vdev_ops == &vdev_missing_ops &&
1692 mtvd->vdev_ops != &vdev_missing_ops) {
1694 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1698 * Device specific actions.
1700 if (mtvd->vdev_islog) {
1701 spa_set_log_state(spa, SPA_LOG_CLEAR);
1704 * XXX - once we have 'readonly' pool
1705 * support we should be able to handle
1706 * missing data devices by transitioning
1707 * the pool to readonly.
1713 * Swap the missing vdev with the data we were
1714 * able to obtain from the MOS config.
1716 vdev_remove_child(rvd, tvd);
1717 vdev_remove_child(mrvd, mtvd);
1719 vdev_add_child(rvd, mtvd);
1720 vdev_add_child(mrvd, tvd);
1722 spa_config_exit(spa, SCL_ALL, FTAG);
1724 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1727 } else if (mtvd->vdev_islog) {
1729 * Load the slog device's state from the MOS config
1730 * since it's possible that the label does not
1731 * contain the most up-to-date information.
1733 vdev_load_log_state(tvd, mtvd);
1738 spa_config_exit(spa, SCL_ALL, FTAG);
1741 * Ensure we were able to validate the config.
1743 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1747 * Check for missing log devices
1750 spa_check_logs(spa_t *spa)
1752 boolean_t rv = B_FALSE;
1754 switch (spa->spa_log_state) {
1755 case SPA_LOG_MISSING:
1756 /* need to recheck in case slog has been restored */
1757 case SPA_LOG_UNKNOWN:
1758 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1759 NULL, DS_FIND_CHILDREN) != 0);
1761 spa_set_log_state(spa, SPA_LOG_MISSING);
1768 spa_passivate_log(spa_t *spa)
1770 vdev_t *rvd = spa->spa_root_vdev;
1771 boolean_t slog_found = B_FALSE;
1773 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1775 if (!spa_has_slogs(spa))
1778 for (int c = 0; c < rvd->vdev_children; c++) {
1779 vdev_t *tvd = rvd->vdev_child[c];
1780 metaslab_group_t *mg = tvd->vdev_mg;
1782 if (tvd->vdev_islog) {
1783 metaslab_group_passivate(mg);
1784 slog_found = B_TRUE;
1788 return (slog_found);
1792 spa_activate_log(spa_t *spa)
1794 vdev_t *rvd = spa->spa_root_vdev;
1796 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1798 for (int c = 0; c < rvd->vdev_children; c++) {
1799 vdev_t *tvd = rvd->vdev_child[c];
1800 metaslab_group_t *mg = tvd->vdev_mg;
1802 if (tvd->vdev_islog)
1803 metaslab_group_activate(mg);
1808 spa_offline_log(spa_t *spa)
1812 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1813 NULL, DS_FIND_CHILDREN);
1816 * We successfully offlined the log device, sync out the
1817 * current txg so that the "stubby" block can be removed
1820 txg_wait_synced(spa->spa_dsl_pool, 0);
1826 spa_aux_check_removed(spa_aux_vdev_t *sav)
1830 for (i = 0; i < sav->sav_count; i++)
1831 spa_check_removed(sav->sav_vdevs[i]);
1835 spa_claim_notify(zio_t *zio)
1837 spa_t *spa = zio->io_spa;
1842 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1843 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1844 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1845 mutex_exit(&spa->spa_props_lock);
1848 typedef struct spa_load_error {
1849 uint64_t sle_meta_count;
1850 uint64_t sle_data_count;
1854 spa_load_verify_done(zio_t *zio)
1856 blkptr_t *bp = zio->io_bp;
1857 spa_load_error_t *sle = zio->io_private;
1858 dmu_object_type_t type = BP_GET_TYPE(bp);
1859 int error = zio->io_error;
1862 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1863 type != DMU_OT_INTENT_LOG)
1864 atomic_add_64(&sle->sle_meta_count, 1);
1866 atomic_add_64(&sle->sle_data_count, 1);
1868 zio_data_buf_free(zio->io_data, zio->io_size);
1873 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1874 const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1878 size_t size = BP_GET_PSIZE(bp);
1879 void *data = zio_data_buf_alloc(size);
1881 zio_nowait(zio_read(rio, spa, bp, data, size,
1882 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1883 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1884 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1890 spa_load_verify(spa_t *spa)
1893 spa_load_error_t sle = { 0 };
1894 zpool_rewind_policy_t policy;
1895 boolean_t verify_ok = B_FALSE;
1898 zpool_get_rewind_policy(spa->spa_config, &policy);
1900 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1903 rio = zio_root(spa, NULL, &sle,
1904 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1906 error = traverse_pool(spa, spa->spa_verify_min_txg,
1907 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1909 (void) zio_wait(rio);
1911 spa->spa_load_meta_errors = sle.sle_meta_count;
1912 spa->spa_load_data_errors = sle.sle_data_count;
1914 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1915 sle.sle_data_count <= policy.zrp_maxdata) {
1919 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1920 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1922 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1923 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1924 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1925 VERIFY(nvlist_add_int64(spa->spa_load_info,
1926 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1927 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1928 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1930 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1934 if (error != ENXIO && error != EIO)
1935 error = SET_ERROR(EIO);
1939 return (verify_ok ? 0 : EIO);
1943 * Find a value in the pool props object.
1946 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1948 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1949 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1953 * Find a value in the pool directory object.
1956 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1958 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1959 name, sizeof (uint64_t), 1, val));
1963 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1965 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1970 * Fix up config after a partly-completed split. This is done with the
1971 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1972 * pool have that entry in their config, but only the splitting one contains
1973 * a list of all the guids of the vdevs that are being split off.
1975 * This function determines what to do with that list: either rejoin
1976 * all the disks to the pool, or complete the splitting process. To attempt
1977 * the rejoin, each disk that is offlined is marked online again, and
1978 * we do a reopen() call. If the vdev label for every disk that was
1979 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1980 * then we call vdev_split() on each disk, and complete the split.
1982 * Otherwise we leave the config alone, with all the vdevs in place in
1983 * the original pool.
1986 spa_try_repair(spa_t *spa, nvlist_t *config)
1993 boolean_t attempt_reopen;
1995 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1998 /* check that the config is complete */
1999 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2000 &glist, &gcount) != 0)
2003 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2005 /* attempt to online all the vdevs & validate */
2006 attempt_reopen = B_TRUE;
2007 for (i = 0; i < gcount; i++) {
2008 if (glist[i] == 0) /* vdev is hole */
2011 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2012 if (vd[i] == NULL) {
2014 * Don't bother attempting to reopen the disks;
2015 * just do the split.
2017 attempt_reopen = B_FALSE;
2019 /* attempt to re-online it */
2020 vd[i]->vdev_offline = B_FALSE;
2024 if (attempt_reopen) {
2025 vdev_reopen(spa->spa_root_vdev);
2027 /* check each device to see what state it's in */
2028 for (extracted = 0, i = 0; i < gcount; i++) {
2029 if (vd[i] != NULL &&
2030 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2037 * If every disk has been moved to the new pool, or if we never
2038 * even attempted to look at them, then we split them off for
2041 if (!attempt_reopen || gcount == extracted) {
2042 for (i = 0; i < gcount; i++)
2045 vdev_reopen(spa->spa_root_vdev);
2048 kmem_free(vd, gcount * sizeof (vdev_t *));
2052 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2053 boolean_t mosconfig)
2055 nvlist_t *config = spa->spa_config;
2056 char *ereport = FM_EREPORT_ZFS_POOL;
2062 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2063 return (SET_ERROR(EINVAL));
2065 ASSERT(spa->spa_comment == NULL);
2066 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2067 spa->spa_comment = spa_strdup(comment);
2070 * Versioning wasn't explicitly added to the label until later, so if
2071 * it's not present treat it as the initial version.
2073 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2074 &spa->spa_ubsync.ub_version) != 0)
2075 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2077 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2078 &spa->spa_config_txg);
2080 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2081 spa_guid_exists(pool_guid, 0)) {
2082 error = SET_ERROR(EEXIST);
2084 spa->spa_config_guid = pool_guid;
2086 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2088 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2092 nvlist_free(spa->spa_load_info);
2093 spa->spa_load_info = fnvlist_alloc();
2095 gethrestime(&spa->spa_loaded_ts);
2096 error = spa_load_impl(spa, pool_guid, config, state, type,
2097 mosconfig, &ereport);
2100 spa->spa_minref = refcount_count(&spa->spa_refcount);
2102 if (error != EEXIST) {
2103 spa->spa_loaded_ts.tv_sec = 0;
2104 spa->spa_loaded_ts.tv_nsec = 0;
2106 if (error != EBADF) {
2107 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2110 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2117 * Load an existing storage pool, using the pool's builtin spa_config as a
2118 * source of configuration information.
2121 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2122 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2126 nvlist_t *nvroot = NULL;
2129 uberblock_t *ub = &spa->spa_uberblock;
2130 uint64_t children, config_cache_txg = spa->spa_config_txg;
2131 int orig_mode = spa->spa_mode;
2134 boolean_t missing_feat_write = B_FALSE;
2137 * If this is an untrusted config, access the pool in read-only mode.
2138 * This prevents things like resilvering recently removed devices.
2141 spa->spa_mode = FREAD;
2143 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2145 spa->spa_load_state = state;
2147 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2148 return (SET_ERROR(EINVAL));
2150 parse = (type == SPA_IMPORT_EXISTING ?
2151 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2154 * Create "The Godfather" zio to hold all async IOs
2156 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2157 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2160 * Parse the configuration into a vdev tree. We explicitly set the
2161 * value that will be returned by spa_version() since parsing the
2162 * configuration requires knowing the version number.
2164 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2165 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2166 spa_config_exit(spa, SCL_ALL, FTAG);
2171 ASSERT(spa->spa_root_vdev == rvd);
2173 if (type != SPA_IMPORT_ASSEMBLE) {
2174 ASSERT(spa_guid(spa) == pool_guid);
2178 * Try to open all vdevs, loading each label in the process.
2180 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2181 error = vdev_open(rvd);
2182 spa_config_exit(spa, SCL_ALL, FTAG);
2187 * We need to validate the vdev labels against the configuration that
2188 * we have in hand, which is dependent on the setting of mosconfig. If
2189 * mosconfig is true then we're validating the vdev labels based on
2190 * that config. Otherwise, we're validating against the cached config
2191 * (zpool.cache) that was read when we loaded the zfs module, and then
2192 * later we will recursively call spa_load() and validate against
2195 * If we're assembling a new pool that's been split off from an
2196 * existing pool, the labels haven't yet been updated so we skip
2197 * validation for now.
2199 if (type != SPA_IMPORT_ASSEMBLE) {
2200 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2201 error = vdev_validate(rvd, mosconfig);
2202 spa_config_exit(spa, SCL_ALL, FTAG);
2207 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2208 return (SET_ERROR(ENXIO));
2212 * Find the best uberblock.
2214 vdev_uberblock_load(rvd, ub, &label);
2217 * If we weren't able to find a single valid uberblock, return failure.
2219 if (ub->ub_txg == 0) {
2221 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2225 * If the pool has an unsupported version we can't open it.
2227 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2229 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2232 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2236 * If we weren't able to find what's necessary for reading the
2237 * MOS in the label, return failure.
2239 if (label == NULL || nvlist_lookup_nvlist(label,
2240 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2242 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2247 * Update our in-core representation with the definitive values
2250 nvlist_free(spa->spa_label_features);
2251 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2257 * Look through entries in the label nvlist's features_for_read. If
2258 * there is a feature listed there which we don't understand then we
2259 * cannot open a pool.
2261 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2262 nvlist_t *unsup_feat;
2264 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2267 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2269 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2270 if (!zfeature_is_supported(nvpair_name(nvp))) {
2271 VERIFY(nvlist_add_string(unsup_feat,
2272 nvpair_name(nvp), "") == 0);
2276 if (!nvlist_empty(unsup_feat)) {
2277 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2278 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2279 nvlist_free(unsup_feat);
2280 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2284 nvlist_free(unsup_feat);
2288 * If the vdev guid sum doesn't match the uberblock, we have an
2289 * incomplete configuration. We first check to see if the pool
2290 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2291 * If it is, defer the vdev_guid_sum check till later so we
2292 * can handle missing vdevs.
2294 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2295 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2296 rvd->vdev_guid_sum != ub->ub_guid_sum)
2297 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2299 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2300 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2301 spa_try_repair(spa, config);
2302 spa_config_exit(spa, SCL_ALL, FTAG);
2303 nvlist_free(spa->spa_config_splitting);
2304 spa->spa_config_splitting = NULL;
2308 * Initialize internal SPA structures.
2310 spa->spa_state = POOL_STATE_ACTIVE;
2311 spa->spa_ubsync = spa->spa_uberblock;
2312 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2313 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2314 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2315 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2316 spa->spa_claim_max_txg = spa->spa_first_txg;
2317 spa->spa_prev_software_version = ub->ub_software_version;
2319 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2321 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2322 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2324 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2325 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2327 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2328 boolean_t missing_feat_read = B_FALSE;
2329 nvlist_t *unsup_feat, *enabled_feat;
2331 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2332 &spa->spa_feat_for_read_obj) != 0) {
2333 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2336 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2337 &spa->spa_feat_for_write_obj) != 0) {
2338 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2341 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2342 &spa->spa_feat_desc_obj) != 0) {
2343 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2346 enabled_feat = fnvlist_alloc();
2347 unsup_feat = fnvlist_alloc();
2349 if (!feature_is_supported(spa->spa_meta_objset,
2350 spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj,
2351 unsup_feat, enabled_feat))
2352 missing_feat_read = B_TRUE;
2354 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2355 if (!feature_is_supported(spa->spa_meta_objset,
2356 spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj,
2357 unsup_feat, enabled_feat)) {
2358 missing_feat_write = B_TRUE;
2362 fnvlist_add_nvlist(spa->spa_load_info,
2363 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2365 if (!nvlist_empty(unsup_feat)) {
2366 fnvlist_add_nvlist(spa->spa_load_info,
2367 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2370 fnvlist_free(enabled_feat);
2371 fnvlist_free(unsup_feat);
2373 if (!missing_feat_read) {
2374 fnvlist_add_boolean(spa->spa_load_info,
2375 ZPOOL_CONFIG_CAN_RDONLY);
2379 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2380 * twofold: to determine whether the pool is available for
2381 * import in read-write mode and (if it is not) whether the
2382 * pool is available for import in read-only mode. If the pool
2383 * is available for import in read-write mode, it is displayed
2384 * as available in userland; if it is not available for import
2385 * in read-only mode, it is displayed as unavailable in
2386 * userland. If the pool is available for import in read-only
2387 * mode but not read-write mode, it is displayed as unavailable
2388 * in userland with a special note that the pool is actually
2389 * available for open in read-only mode.
2391 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2392 * missing a feature for write, we must first determine whether
2393 * the pool can be opened read-only before returning to
2394 * userland in order to know whether to display the
2395 * abovementioned note.
2397 if (missing_feat_read || (missing_feat_write &&
2398 spa_writeable(spa))) {
2399 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2404 spa->spa_is_initializing = B_TRUE;
2405 error = dsl_pool_open(spa->spa_dsl_pool);
2406 spa->spa_is_initializing = B_FALSE;
2408 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2412 nvlist_t *policy = NULL, *nvconfig;
2414 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2415 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2417 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2418 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2420 unsigned long myhostid = 0;
2422 VERIFY(nvlist_lookup_string(nvconfig,
2423 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2426 myhostid = zone_get_hostid(NULL);
2429 * We're emulating the system's hostid in userland, so
2430 * we can't use zone_get_hostid().
2432 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2433 #endif /* _KERNEL */
2434 if (check_hostid && hostid != 0 && myhostid != 0 &&
2435 hostid != myhostid) {
2436 nvlist_free(nvconfig);
2437 cmn_err(CE_WARN, "pool '%s' could not be "
2438 "loaded as it was last accessed by "
2439 "another system (host: %s hostid: 0x%lx). "
2440 "See: http://illumos.org/msg/ZFS-8000-EY",
2441 spa_name(spa), hostname,
2442 (unsigned long)hostid);
2443 return (SET_ERROR(EBADF));
2446 if (nvlist_lookup_nvlist(spa->spa_config,
2447 ZPOOL_REWIND_POLICY, &policy) == 0)
2448 VERIFY(nvlist_add_nvlist(nvconfig,
2449 ZPOOL_REWIND_POLICY, policy) == 0);
2451 spa_config_set(spa, nvconfig);
2453 spa_deactivate(spa);
2454 spa_activate(spa, orig_mode);
2456 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2459 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2460 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2461 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2463 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2466 * Load the bit that tells us to use the new accounting function
2467 * (raid-z deflation). If we have an older pool, this will not
2470 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2471 if (error != 0 && error != ENOENT)
2472 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2474 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2475 &spa->spa_creation_version);
2476 if (error != 0 && error != ENOENT)
2477 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2480 * Load the persistent error log. If we have an older pool, this will
2483 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2484 if (error != 0 && error != ENOENT)
2485 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2487 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2488 &spa->spa_errlog_scrub);
2489 if (error != 0 && error != ENOENT)
2490 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2493 * Load the history object. If we have an older pool, this
2494 * will not be present.
2496 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2497 if (error != 0 && error != ENOENT)
2498 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2501 * If we're assembling the pool from the split-off vdevs of
2502 * an existing pool, we don't want to attach the spares & cache
2507 * Load any hot spares for this pool.
2509 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2510 if (error != 0 && error != ENOENT)
2511 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2512 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2513 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2514 if (load_nvlist(spa, spa->spa_spares.sav_object,
2515 &spa->spa_spares.sav_config) != 0)
2516 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2518 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2519 spa_load_spares(spa);
2520 spa_config_exit(spa, SCL_ALL, FTAG);
2521 } else if (error == 0) {
2522 spa->spa_spares.sav_sync = B_TRUE;
2526 * Load any level 2 ARC devices for this pool.
2528 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2529 &spa->spa_l2cache.sav_object);
2530 if (error != 0 && error != ENOENT)
2531 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2532 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2533 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2534 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2535 &spa->spa_l2cache.sav_config) != 0)
2536 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2538 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2539 spa_load_l2cache(spa);
2540 spa_config_exit(spa, SCL_ALL, FTAG);
2541 } else if (error == 0) {
2542 spa->spa_l2cache.sav_sync = B_TRUE;
2545 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2547 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2548 if (error && error != ENOENT)
2549 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2552 uint64_t autoreplace;
2554 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2555 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2556 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2557 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2558 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2559 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2560 &spa->spa_dedup_ditto);
2562 spa->spa_autoreplace = (autoreplace != 0);
2566 * If the 'autoreplace' property is set, then post a resource notifying
2567 * the ZFS DE that it should not issue any faults for unopenable
2568 * devices. We also iterate over the vdevs, and post a sysevent for any
2569 * unopenable vdevs so that the normal autoreplace handler can take
2572 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2573 spa_check_removed(spa->spa_root_vdev);
2575 * For the import case, this is done in spa_import(), because
2576 * at this point we're using the spare definitions from
2577 * the MOS config, not necessarily from the userland config.
2579 if (state != SPA_LOAD_IMPORT) {
2580 spa_aux_check_removed(&spa->spa_spares);
2581 spa_aux_check_removed(&spa->spa_l2cache);
2586 * Load the vdev state for all toplevel vdevs.
2591 * Propagate the leaf DTLs we just loaded all the way up the tree.
2593 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2594 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2595 spa_config_exit(spa, SCL_ALL, FTAG);
2598 * Load the DDTs (dedup tables).
2600 error = ddt_load(spa);
2602 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2604 spa_update_dspace(spa);
2607 * Validate the config, using the MOS config to fill in any
2608 * information which might be missing. If we fail to validate
2609 * the config then declare the pool unfit for use. If we're
2610 * assembling a pool from a split, the log is not transferred
2613 if (type != SPA_IMPORT_ASSEMBLE) {
2616 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2617 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2619 if (!spa_config_valid(spa, nvconfig)) {
2620 nvlist_free(nvconfig);
2621 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2624 nvlist_free(nvconfig);
2627 * Now that we've validated the config, check the state of the
2628 * root vdev. If it can't be opened, it indicates one or
2629 * more toplevel vdevs are faulted.
2631 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2632 return (SET_ERROR(ENXIO));
2634 if (spa_check_logs(spa)) {
2635 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2636 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2640 if (missing_feat_write) {
2641 ASSERT(state == SPA_LOAD_TRYIMPORT);
2644 * At this point, we know that we can open the pool in
2645 * read-only mode but not read-write mode. We now have enough
2646 * information and can return to userland.
2648 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2652 * We've successfully opened the pool, verify that we're ready
2653 * to start pushing transactions.
2655 if (state != SPA_LOAD_TRYIMPORT) {
2656 if (error = spa_load_verify(spa))
2657 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2661 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2662 spa->spa_load_max_txg == UINT64_MAX)) {
2664 int need_update = B_FALSE;
2666 ASSERT(state != SPA_LOAD_TRYIMPORT);
2669 * Claim log blocks that haven't been committed yet.
2670 * This must all happen in a single txg.
2671 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2672 * invoked from zil_claim_log_block()'s i/o done callback.
2673 * Price of rollback is that we abandon the log.
2675 spa->spa_claiming = B_TRUE;
2677 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2678 spa_first_txg(spa));
2679 (void) dmu_objset_find(spa_name(spa),
2680 zil_claim, tx, DS_FIND_CHILDREN);
2683 spa->spa_claiming = B_FALSE;
2685 spa_set_log_state(spa, SPA_LOG_GOOD);
2686 spa->spa_sync_on = B_TRUE;
2687 txg_sync_start(spa->spa_dsl_pool);
2690 * Wait for all claims to sync. We sync up to the highest
2691 * claimed log block birth time so that claimed log blocks
2692 * don't appear to be from the future. spa_claim_max_txg
2693 * will have been set for us by either zil_check_log_chain()
2694 * (invoked from spa_check_logs()) or zil_claim() above.
2696 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2699 * If the config cache is stale, or we have uninitialized
2700 * metaslabs (see spa_vdev_add()), then update the config.
2702 * If this is a verbatim import, trust the current
2703 * in-core spa_config and update the disk labels.
2705 if (config_cache_txg != spa->spa_config_txg ||
2706 state == SPA_LOAD_IMPORT ||
2707 state == SPA_LOAD_RECOVER ||
2708 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2709 need_update = B_TRUE;
2711 for (int c = 0; c < rvd->vdev_children; c++)
2712 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2713 need_update = B_TRUE;
2716 * Update the config cache asychronously in case we're the
2717 * root pool, in which case the config cache isn't writable yet.
2720 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2723 * Check all DTLs to see if anything needs resilvering.
2725 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2726 vdev_resilver_needed(rvd, NULL, NULL))
2727 spa_async_request(spa, SPA_ASYNC_RESILVER);
2730 * Log the fact that we booted up (so that we can detect if
2731 * we rebooted in the middle of an operation).
2733 spa_history_log_version(spa, "open");
2736 * Delete any inconsistent datasets.
2738 (void) dmu_objset_find(spa_name(spa),
2739 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2742 * Clean up any stale temporary dataset userrefs.
2744 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2751 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2753 int mode = spa->spa_mode;
2756 spa_deactivate(spa);
2758 spa->spa_load_max_txg--;
2760 spa_activate(spa, mode);
2761 spa_async_suspend(spa);
2763 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2767 * If spa_load() fails this function will try loading prior txg's. If
2768 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2769 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2770 * function will not rewind the pool and will return the same error as
2774 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2775 uint64_t max_request, int rewind_flags)
2777 nvlist_t *loadinfo = NULL;
2778 nvlist_t *config = NULL;
2779 int load_error, rewind_error;
2780 uint64_t safe_rewind_txg;
2783 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2784 spa->spa_load_max_txg = spa->spa_load_txg;
2785 spa_set_log_state(spa, SPA_LOG_CLEAR);
2787 spa->spa_load_max_txg = max_request;
2790 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2792 if (load_error == 0)
2795 if (spa->spa_root_vdev != NULL)
2796 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2798 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2799 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2801 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2802 nvlist_free(config);
2803 return (load_error);
2806 if (state == SPA_LOAD_RECOVER) {
2807 /* Price of rolling back is discarding txgs, including log */
2808 spa_set_log_state(spa, SPA_LOG_CLEAR);
2811 * If we aren't rolling back save the load info from our first
2812 * import attempt so that we can restore it after attempting
2815 loadinfo = spa->spa_load_info;
2816 spa->spa_load_info = fnvlist_alloc();
2819 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2820 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2821 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2822 TXG_INITIAL : safe_rewind_txg;
2825 * Continue as long as we're finding errors, we're still within
2826 * the acceptable rewind range, and we're still finding uberblocks
2828 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2829 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2830 if (spa->spa_load_max_txg < safe_rewind_txg)
2831 spa->spa_extreme_rewind = B_TRUE;
2832 rewind_error = spa_load_retry(spa, state, mosconfig);
2835 spa->spa_extreme_rewind = B_FALSE;
2836 spa->spa_load_max_txg = UINT64_MAX;
2838 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2839 spa_config_set(spa, config);
2841 if (state == SPA_LOAD_RECOVER) {
2842 ASSERT3P(loadinfo, ==, NULL);
2843 return (rewind_error);
2845 /* Store the rewind info as part of the initial load info */
2846 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2847 spa->spa_load_info);
2849 /* Restore the initial load info */
2850 fnvlist_free(spa->spa_load_info);
2851 spa->spa_load_info = loadinfo;
2853 return (load_error);
2860 * The import case is identical to an open except that the configuration is sent
2861 * down from userland, instead of grabbed from the configuration cache. For the
2862 * case of an open, the pool configuration will exist in the
2863 * POOL_STATE_UNINITIALIZED state.
2865 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2866 * the same time open the pool, without having to keep around the spa_t in some
2870 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2874 spa_load_state_t state = SPA_LOAD_OPEN;
2876 int locked = B_FALSE;
2877 int firstopen = B_FALSE;
2882 * As disgusting as this is, we need to support recursive calls to this
2883 * function because dsl_dir_open() is called during spa_load(), and ends
2884 * up calling spa_open() again. The real fix is to figure out how to
2885 * avoid dsl_dir_open() calling this in the first place.
2887 if (mutex_owner(&spa_namespace_lock) != curthread) {
2888 mutex_enter(&spa_namespace_lock);
2892 if ((spa = spa_lookup(pool)) == NULL) {
2894 mutex_exit(&spa_namespace_lock);
2895 return (SET_ERROR(ENOENT));
2898 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2899 zpool_rewind_policy_t policy;
2903 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2905 if (policy.zrp_request & ZPOOL_DO_REWIND)
2906 state = SPA_LOAD_RECOVER;
2908 spa_activate(spa, spa_mode_global);
2910 if (state != SPA_LOAD_RECOVER)
2911 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2913 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2914 policy.zrp_request);
2916 if (error == EBADF) {
2918 * If vdev_validate() returns failure (indicated by
2919 * EBADF), it indicates that one of the vdevs indicates
2920 * that the pool has been exported or destroyed. If
2921 * this is the case, the config cache is out of sync and
2922 * we should remove the pool from the namespace.
2925 spa_deactivate(spa);
2926 spa_config_sync(spa, B_TRUE, B_TRUE);
2929 mutex_exit(&spa_namespace_lock);
2930 return (SET_ERROR(ENOENT));
2935 * We can't open the pool, but we still have useful
2936 * information: the state of each vdev after the
2937 * attempted vdev_open(). Return this to the user.
2939 if (config != NULL && spa->spa_config) {
2940 VERIFY(nvlist_dup(spa->spa_config, config,
2942 VERIFY(nvlist_add_nvlist(*config,
2943 ZPOOL_CONFIG_LOAD_INFO,
2944 spa->spa_load_info) == 0);
2947 spa_deactivate(spa);
2948 spa->spa_last_open_failed = error;
2950 mutex_exit(&spa_namespace_lock);
2956 spa_open_ref(spa, tag);
2959 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2962 * If we've recovered the pool, pass back any information we
2963 * gathered while doing the load.
2965 if (state == SPA_LOAD_RECOVER) {
2966 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2967 spa->spa_load_info) == 0);
2971 spa->spa_last_open_failed = 0;
2972 spa->spa_last_ubsync_txg = 0;
2973 spa->spa_load_txg = 0;
2974 mutex_exit(&spa_namespace_lock);
2978 zvol_create_minors(spa->spa_name);
2989 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2992 return (spa_open_common(name, spapp, tag, policy, config));
2996 spa_open(const char *name, spa_t **spapp, void *tag)
2998 return (spa_open_common(name, spapp, tag, NULL, NULL));
3002 * Lookup the given spa_t, incrementing the inject count in the process,
3003 * preventing it from being exported or destroyed.
3006 spa_inject_addref(char *name)
3010 mutex_enter(&spa_namespace_lock);
3011 if ((spa = spa_lookup(name)) == NULL) {
3012 mutex_exit(&spa_namespace_lock);
3015 spa->spa_inject_ref++;
3016 mutex_exit(&spa_namespace_lock);
3022 spa_inject_delref(spa_t *spa)
3024 mutex_enter(&spa_namespace_lock);
3025 spa->spa_inject_ref--;
3026 mutex_exit(&spa_namespace_lock);
3030 * Add spares device information to the nvlist.
3033 spa_add_spares(spa_t *spa, nvlist_t *config)
3043 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3045 if (spa->spa_spares.sav_count == 0)
3048 VERIFY(nvlist_lookup_nvlist(config,
3049 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3050 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3051 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3053 VERIFY(nvlist_add_nvlist_array(nvroot,
3054 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3055 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3056 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3059 * Go through and find any spares which have since been
3060 * repurposed as an active spare. If this is the case, update
3061 * their status appropriately.
3063 for (i = 0; i < nspares; i++) {
3064 VERIFY(nvlist_lookup_uint64(spares[i],
3065 ZPOOL_CONFIG_GUID, &guid) == 0);
3066 if (spa_spare_exists(guid, &pool, NULL) &&
3068 VERIFY(nvlist_lookup_uint64_array(
3069 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3070 (uint64_t **)&vs, &vsc) == 0);
3071 vs->vs_state = VDEV_STATE_CANT_OPEN;
3072 vs->vs_aux = VDEV_AUX_SPARED;
3079 * Add l2cache device information to the nvlist, including vdev stats.
3082 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3085 uint_t i, j, nl2cache;
3092 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3094 if (spa->spa_l2cache.sav_count == 0)
3097 VERIFY(nvlist_lookup_nvlist(config,
3098 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3099 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3100 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3101 if (nl2cache != 0) {
3102 VERIFY(nvlist_add_nvlist_array(nvroot,
3103 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3104 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3105 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3108 * Update level 2 cache device stats.
3111 for (i = 0; i < nl2cache; i++) {
3112 VERIFY(nvlist_lookup_uint64(l2cache[i],
3113 ZPOOL_CONFIG_GUID, &guid) == 0);
3116 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3118 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3119 vd = spa->spa_l2cache.sav_vdevs[j];
3125 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3126 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3128 vdev_get_stats(vd, vs);
3134 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3140 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3141 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3143 /* We may be unable to read features if pool is suspended. */
3144 if (spa_suspended(spa))
3147 if (spa->spa_feat_for_read_obj != 0) {
3148 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3149 spa->spa_feat_for_read_obj);
3150 zap_cursor_retrieve(&zc, &za) == 0;
3151 zap_cursor_advance(&zc)) {
3152 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3153 za.za_num_integers == 1);
3154 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3155 za.za_first_integer));
3157 zap_cursor_fini(&zc);
3160 if (spa->spa_feat_for_write_obj != 0) {
3161 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3162 spa->spa_feat_for_write_obj);
3163 zap_cursor_retrieve(&zc, &za) == 0;
3164 zap_cursor_advance(&zc)) {
3165 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3166 za.za_num_integers == 1);
3167 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3168 za.za_first_integer));
3170 zap_cursor_fini(&zc);
3174 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3176 nvlist_free(features);
3180 spa_get_stats(const char *name, nvlist_t **config,
3181 char *altroot, size_t buflen)
3187 error = spa_open_common(name, &spa, FTAG, NULL, config);
3191 * This still leaves a window of inconsistency where the spares
3192 * or l2cache devices could change and the config would be
3193 * self-inconsistent.
3195 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3197 if (*config != NULL) {
3198 uint64_t loadtimes[2];
3200 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3201 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3202 VERIFY(nvlist_add_uint64_array(*config,
3203 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3205 VERIFY(nvlist_add_uint64(*config,
3206 ZPOOL_CONFIG_ERRCOUNT,
3207 spa_get_errlog_size(spa)) == 0);
3209 if (spa_suspended(spa))
3210 VERIFY(nvlist_add_uint64(*config,
3211 ZPOOL_CONFIG_SUSPENDED,
3212 spa->spa_failmode) == 0);
3214 spa_add_spares(spa, *config);
3215 spa_add_l2cache(spa, *config);
3216 spa_add_feature_stats(spa, *config);
3221 * We want to get the alternate root even for faulted pools, so we cheat
3222 * and call spa_lookup() directly.
3226 mutex_enter(&spa_namespace_lock);
3227 spa = spa_lookup(name);
3229 spa_altroot(spa, altroot, buflen);
3233 mutex_exit(&spa_namespace_lock);
3235 spa_altroot(spa, altroot, buflen);
3240 spa_config_exit(spa, SCL_CONFIG, FTAG);
3241 spa_close(spa, FTAG);
3248 * Validate that the auxiliary device array is well formed. We must have an
3249 * array of nvlists, each which describes a valid leaf vdev. If this is an
3250 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3251 * specified, as long as they are well-formed.
3254 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3255 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3256 vdev_labeltype_t label)
3263 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3266 * It's acceptable to have no devs specified.
3268 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3272 return (SET_ERROR(EINVAL));
3275 * Make sure the pool is formatted with a version that supports this
3278 if (spa_version(spa) < version)
3279 return (SET_ERROR(ENOTSUP));
3282 * Set the pending device list so we correctly handle device in-use
3285 sav->sav_pending = dev;
3286 sav->sav_npending = ndev;
3288 for (i = 0; i < ndev; i++) {
3289 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3293 if (!vd->vdev_ops->vdev_op_leaf) {
3295 error = SET_ERROR(EINVAL);
3300 * The L2ARC currently only supports disk devices in
3301 * kernel context. For user-level testing, we allow it.
3304 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3305 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3306 error = SET_ERROR(ENOTBLK);
3313 if ((error = vdev_open(vd)) == 0 &&
3314 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3315 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3316 vd->vdev_guid) == 0);
3322 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3329 sav->sav_pending = NULL;
3330 sav->sav_npending = 0;
3335 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3339 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3341 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3342 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3343 VDEV_LABEL_SPARE)) != 0) {
3347 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3348 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3349 VDEV_LABEL_L2CACHE));
3353 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3358 if (sav->sav_config != NULL) {
3364 * Generate new dev list by concatentating with the
3367 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3368 &olddevs, &oldndevs) == 0);
3370 newdevs = kmem_alloc(sizeof (void *) *
3371 (ndevs + oldndevs), KM_SLEEP);
3372 for (i = 0; i < oldndevs; i++)
3373 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3375 for (i = 0; i < ndevs; i++)
3376 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3379 VERIFY(nvlist_remove(sav->sav_config, config,
3380 DATA_TYPE_NVLIST_ARRAY) == 0);
3382 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3383 config, newdevs, ndevs + oldndevs) == 0);
3384 for (i = 0; i < oldndevs + ndevs; i++)
3385 nvlist_free(newdevs[i]);
3386 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3389 * Generate a new dev list.
3391 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3393 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3399 * Stop and drop level 2 ARC devices
3402 spa_l2cache_drop(spa_t *spa)
3406 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3408 for (i = 0; i < sav->sav_count; i++) {
3411 vd = sav->sav_vdevs[i];
3414 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3415 pool != 0ULL && l2arc_vdev_present(vd))
3416 l2arc_remove_vdev(vd);
3424 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3428 char *altroot = NULL;
3433 uint64_t txg = TXG_INITIAL;
3434 nvlist_t **spares, **l2cache;
3435 uint_t nspares, nl2cache;
3436 uint64_t version, obj;
3437 boolean_t has_features;
3440 * If this pool already exists, return failure.
3442 mutex_enter(&spa_namespace_lock);
3443 if (spa_lookup(pool) != NULL) {
3444 mutex_exit(&spa_namespace_lock);
3445 return (SET_ERROR(EEXIST));
3449 * Allocate a new spa_t structure.
3451 (void) nvlist_lookup_string(props,
3452 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3453 spa = spa_add(pool, NULL, altroot);
3454 spa_activate(spa, spa_mode_global);
3456 if (props && (error = spa_prop_validate(spa, props))) {
3457 spa_deactivate(spa);
3459 mutex_exit(&spa_namespace_lock);
3463 has_features = B_FALSE;
3464 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3465 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3466 if (zpool_prop_feature(nvpair_name(elem)))
3467 has_features = B_TRUE;
3470 if (has_features || nvlist_lookup_uint64(props,
3471 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3472 version = SPA_VERSION;
3474 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3476 spa->spa_first_txg = txg;
3477 spa->spa_uberblock.ub_txg = txg - 1;
3478 spa->spa_uberblock.ub_version = version;
3479 spa->spa_ubsync = spa->spa_uberblock;
3482 * Create "The Godfather" zio to hold all async IOs
3484 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3485 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3488 * Create the root vdev.
3490 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3492 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3494 ASSERT(error != 0 || rvd != NULL);
3495 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3497 if (error == 0 && !zfs_allocatable_devs(nvroot))
3498 error = SET_ERROR(EINVAL);
3501 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3502 (error = spa_validate_aux(spa, nvroot, txg,
3503 VDEV_ALLOC_ADD)) == 0) {
3504 for (int c = 0; c < rvd->vdev_children; c++) {
3505 vdev_metaslab_set_size(rvd->vdev_child[c]);
3506 vdev_expand(rvd->vdev_child[c], txg);
3510 spa_config_exit(spa, SCL_ALL, FTAG);
3514 spa_deactivate(spa);
3516 mutex_exit(&spa_namespace_lock);
3521 * Get the list of spares, if specified.
3523 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3524 &spares, &nspares) == 0) {
3525 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3527 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3528 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3529 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3530 spa_load_spares(spa);
3531 spa_config_exit(spa, SCL_ALL, FTAG);
3532 spa->spa_spares.sav_sync = B_TRUE;
3536 * Get the list of level 2 cache devices, if specified.
3538 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3539 &l2cache, &nl2cache) == 0) {
3540 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3541 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3542 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3543 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3544 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3545 spa_load_l2cache(spa);
3546 spa_config_exit(spa, SCL_ALL, FTAG);
3547 spa->spa_l2cache.sav_sync = B_TRUE;
3550 spa->spa_is_initializing = B_TRUE;
3551 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3552 spa->spa_meta_objset = dp->dp_meta_objset;
3553 spa->spa_is_initializing = B_FALSE;
3556 * Create DDTs (dedup tables).
3560 spa_update_dspace(spa);
3562 tx = dmu_tx_create_assigned(dp, txg);
3565 * Create the pool config object.
3567 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3568 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3569 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3571 if (zap_add(spa->spa_meta_objset,
3572 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3573 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3574 cmn_err(CE_PANIC, "failed to add pool config");
3577 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3578 spa_feature_create_zap_objects(spa, tx);
3580 if (zap_add(spa->spa_meta_objset,
3581 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3582 sizeof (uint64_t), 1, &version, tx) != 0) {
3583 cmn_err(CE_PANIC, "failed to add pool version");
3586 /* Newly created pools with the right version are always deflated. */
3587 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3588 spa->spa_deflate = TRUE;
3589 if (zap_add(spa->spa_meta_objset,
3590 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3591 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3592 cmn_err(CE_PANIC, "failed to add deflate");
3597 * Create the deferred-free bpobj. Turn off compression
3598 * because sync-to-convergence takes longer if the blocksize
3601 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3602 dmu_object_set_compress(spa->spa_meta_objset, obj,
3603 ZIO_COMPRESS_OFF, tx);
3604 if (zap_add(spa->spa_meta_objset,
3605 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3606 sizeof (uint64_t), 1, &obj, tx) != 0) {
3607 cmn_err(CE_PANIC, "failed to add bpobj");
3609 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3610 spa->spa_meta_objset, obj));
3613 * Create the pool's history object.
3615 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3616 spa_history_create_obj(spa, tx);
3619 * Set pool properties.
3621 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3622 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3623 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3624 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3626 if (props != NULL) {
3627 spa_configfile_set(spa, props, B_FALSE);
3628 spa_sync_props(props, tx);
3633 spa->spa_sync_on = B_TRUE;
3634 txg_sync_start(spa->spa_dsl_pool);
3637 * We explicitly wait for the first transaction to complete so that our
3638 * bean counters are appropriately updated.
3640 txg_wait_synced(spa->spa_dsl_pool, txg);
3642 spa_config_sync(spa, B_FALSE, B_TRUE);
3644 spa_history_log_version(spa, "create");
3646 spa->spa_minref = refcount_count(&spa->spa_refcount);
3648 mutex_exit(&spa_namespace_lock);
3656 * Get the root pool information from the root disk, then import the root pool
3657 * during the system boot up time.
3659 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3662 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3665 nvlist_t *nvtop, *nvroot;
3668 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3672 * Add this top-level vdev to the child array.
3674 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3676 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3678 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3681 * Put this pool's top-level vdevs into a root vdev.
3683 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3684 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3685 VDEV_TYPE_ROOT) == 0);
3686 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3687 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3688 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3692 * Replace the existing vdev_tree with the new root vdev in
3693 * this pool's configuration (remove the old, add the new).
3695 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3696 nvlist_free(nvroot);
3701 * Walk the vdev tree and see if we can find a device with "better"
3702 * configuration. A configuration is "better" if the label on that
3703 * device has a more recent txg.
3706 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3708 for (int c = 0; c < vd->vdev_children; c++)
3709 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3711 if (vd->vdev_ops->vdev_op_leaf) {
3715 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3719 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3723 * Do we have a better boot device?
3725 if (label_txg > *txg) {
3734 * Import a root pool.
3736 * For x86. devpath_list will consist of devid and/or physpath name of
3737 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3738 * The GRUB "findroot" command will return the vdev we should boot.
3740 * For Sparc, devpath_list consists the physpath name of the booting device
3741 * no matter the rootpool is a single device pool or a mirrored pool.
3743 * "/pci@1f,0/ide@d/disk@0,0:a"
3746 spa_import_rootpool(char *devpath, char *devid)
3749 vdev_t *rvd, *bvd, *avd = NULL;
3750 nvlist_t *config, *nvtop;
3756 * Read the label from the boot device and generate a configuration.
3758 config = spa_generate_rootconf(devpath, devid, &guid);
3759 #if defined(_OBP) && defined(_KERNEL)
3760 if (config == NULL) {
3761 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3763 get_iscsi_bootpath_phy(devpath);
3764 config = spa_generate_rootconf(devpath, devid, &guid);
3768 if (config == NULL) {
3769 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3771 return (SET_ERROR(EIO));
3774 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3776 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3778 mutex_enter(&spa_namespace_lock);
3779 if ((spa = spa_lookup(pname)) != NULL) {
3781 * Remove the existing root pool from the namespace so that we
3782 * can replace it with the correct config we just read in.
3787 spa = spa_add(pname, config, NULL);
3788 spa->spa_is_root = B_TRUE;
3789 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3792 * Build up a vdev tree based on the boot device's label config.
3794 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3796 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3797 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3798 VDEV_ALLOC_ROOTPOOL);
3799 spa_config_exit(spa, SCL_ALL, FTAG);
3801 mutex_exit(&spa_namespace_lock);
3802 nvlist_free(config);
3803 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3809 * Get the boot vdev.
3811 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3812 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3813 (u_longlong_t)guid);
3814 error = SET_ERROR(ENOENT);
3819 * Determine if there is a better boot device.
3822 spa_alt_rootvdev(rvd, &avd, &txg);
3824 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3825 "try booting from '%s'", avd->vdev_path);
3826 error = SET_ERROR(EINVAL);
3831 * If the boot device is part of a spare vdev then ensure that
3832 * we're booting off the active spare.
3834 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3835 !bvd->vdev_isspare) {
3836 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3837 "try booting from '%s'",
3839 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3840 error = SET_ERROR(EINVAL);
3846 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3848 spa_config_exit(spa, SCL_ALL, FTAG);
3849 mutex_exit(&spa_namespace_lock);
3851 nvlist_free(config);
3857 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3861 spa_generate_rootconf(const char *name)
3863 nvlist_t **configs, **tops;
3865 nvlist_t *best_cfg, *nvtop, *nvroot;
3874 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3877 ASSERT3U(count, !=, 0);
3879 for (i = 0; i < count; i++) {
3882 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
3884 if (txg > best_txg) {
3886 best_cfg = configs[i];
3891 * Multi-vdev root pool configuration discovery is not supported yet.
3894 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
3896 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
3899 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
3900 for (i = 0; i < nchildren; i++) {
3903 if (configs[i] == NULL)
3905 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
3907 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
3909 for (i = 0; holes != NULL && i < nholes; i++) {
3912 if (tops[holes[i]] != NULL)
3914 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
3915 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
3916 VDEV_TYPE_HOLE) == 0);
3917 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
3919 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
3922 for (i = 0; i < nchildren; i++) {
3923 if (tops[i] != NULL)
3925 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
3926 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
3927 VDEV_TYPE_MISSING) == 0);
3928 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
3930 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
3935 * Create pool config based on the best vdev config.
3937 nvlist_dup(best_cfg, &config, KM_SLEEP);
3940 * Put this pool's top-level vdevs into a root vdev.
3942 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3944 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3945 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3946 VDEV_TYPE_ROOT) == 0);
3947 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3948 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3949 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3950 tops, nchildren) == 0);
3953 * Replace the existing vdev_tree with the new root vdev in
3954 * this pool's configuration (remove the old, add the new).
3956 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3959 * Drop vdev config elements that should not be present at pool level.
3961 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
3962 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
3964 for (i = 0; i < count; i++)
3965 nvlist_free(configs[i]);
3966 kmem_free(configs, count * sizeof(void *));
3967 for (i = 0; i < nchildren; i++)
3968 nvlist_free(tops[i]);
3969 kmem_free(tops, nchildren * sizeof(void *));
3970 nvlist_free(nvroot);
3975 spa_import_rootpool(const char *name)
3978 vdev_t *rvd, *bvd, *avd = NULL;
3979 nvlist_t *config, *nvtop;
3985 * Read the label from the boot device and generate a configuration.
3987 config = spa_generate_rootconf(name);
3989 mutex_enter(&spa_namespace_lock);
3990 if (config != NULL) {
3991 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3992 &pname) == 0 && strcmp(name, pname) == 0);
3993 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
3996 if ((spa = spa_lookup(pname)) != NULL) {
3998 * Remove the existing root pool from the namespace so
3999 * that we can replace it with the correct config
4004 spa = spa_add(pname, config, NULL);
4007 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4008 * via spa_version().
4010 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4011 &spa->spa_ubsync.ub_version) != 0)
4012 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4013 } else if ((spa = spa_lookup(name)) == NULL) {
4014 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4018 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4020 spa->spa_is_root = B_TRUE;
4021 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4024 * Build up a vdev tree based on the boot device's label config.
4026 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4028 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4029 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4030 VDEV_ALLOC_ROOTPOOL);
4031 spa_config_exit(spa, SCL_ALL, FTAG);
4033 mutex_exit(&spa_namespace_lock);
4034 nvlist_free(config);
4035 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4040 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4042 spa_config_exit(spa, SCL_ALL, FTAG);
4043 mutex_exit(&spa_namespace_lock);
4045 nvlist_free(config);
4053 * Import a non-root pool into the system.
4056 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4059 char *altroot = NULL;
4060 spa_load_state_t state = SPA_LOAD_IMPORT;
4061 zpool_rewind_policy_t policy;
4062 uint64_t mode = spa_mode_global;
4063 uint64_t readonly = B_FALSE;
4066 nvlist_t **spares, **l2cache;
4067 uint_t nspares, nl2cache;
4070 * If a pool with this name exists, return failure.
4072 mutex_enter(&spa_namespace_lock);
4073 if (spa_lookup(pool) != NULL) {
4074 mutex_exit(&spa_namespace_lock);
4075 return (SET_ERROR(EEXIST));
4079 * Create and initialize the spa structure.
4081 (void) nvlist_lookup_string(props,
4082 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4083 (void) nvlist_lookup_uint64(props,
4084 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4087 spa = spa_add(pool, config, altroot);
4088 spa->spa_import_flags = flags;
4091 * Verbatim import - Take a pool and insert it into the namespace
4092 * as if it had been loaded at boot.
4094 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4096 spa_configfile_set(spa, props, B_FALSE);
4098 spa_config_sync(spa, B_FALSE, B_TRUE);
4100 mutex_exit(&spa_namespace_lock);
4101 spa_history_log_version(spa, "import");
4106 spa_activate(spa, mode);
4109 * Don't start async tasks until we know everything is healthy.
4111 spa_async_suspend(spa);
4113 zpool_get_rewind_policy(config, &policy);
4114 if (policy.zrp_request & ZPOOL_DO_REWIND)
4115 state = SPA_LOAD_RECOVER;
4118 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4119 * because the user-supplied config is actually the one to trust when
4122 if (state != SPA_LOAD_RECOVER)
4123 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4125 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4126 policy.zrp_request);
4129 * Propagate anything learned while loading the pool and pass it
4130 * back to caller (i.e. rewind info, missing devices, etc).
4132 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4133 spa->spa_load_info) == 0);
4135 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4137 * Toss any existing sparelist, as it doesn't have any validity
4138 * anymore, and conflicts with spa_has_spare().
4140 if (spa->spa_spares.sav_config) {
4141 nvlist_free(spa->spa_spares.sav_config);
4142 spa->spa_spares.sav_config = NULL;
4143 spa_load_spares(spa);
4145 if (spa->spa_l2cache.sav_config) {
4146 nvlist_free(spa->spa_l2cache.sav_config);
4147 spa->spa_l2cache.sav_config = NULL;
4148 spa_load_l2cache(spa);
4151 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4154 error = spa_validate_aux(spa, nvroot, -1ULL,
4157 error = spa_validate_aux(spa, nvroot, -1ULL,
4158 VDEV_ALLOC_L2CACHE);
4159 spa_config_exit(spa, SCL_ALL, FTAG);
4162 spa_configfile_set(spa, props, B_FALSE);
4164 if (error != 0 || (props && spa_writeable(spa) &&
4165 (error = spa_prop_set(spa, props)))) {
4167 spa_deactivate(spa);
4169 mutex_exit(&spa_namespace_lock);
4173 spa_async_resume(spa);
4176 * Override any spares and level 2 cache devices as specified by
4177 * the user, as these may have correct device names/devids, etc.
4179 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4180 &spares, &nspares) == 0) {
4181 if (spa->spa_spares.sav_config)
4182 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4183 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4185 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4186 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4187 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4188 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4189 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4190 spa_load_spares(spa);
4191 spa_config_exit(spa, SCL_ALL, FTAG);
4192 spa->spa_spares.sav_sync = B_TRUE;
4194 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4195 &l2cache, &nl2cache) == 0) {
4196 if (spa->spa_l2cache.sav_config)
4197 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4198 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4200 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4201 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4202 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4203 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4204 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4205 spa_load_l2cache(spa);
4206 spa_config_exit(spa, SCL_ALL, FTAG);
4207 spa->spa_l2cache.sav_sync = B_TRUE;
4211 * Check for any removed devices.
4213 if (spa->spa_autoreplace) {
4214 spa_aux_check_removed(&spa->spa_spares);
4215 spa_aux_check_removed(&spa->spa_l2cache);
4218 if (spa_writeable(spa)) {
4220 * Update the config cache to include the newly-imported pool.
4222 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4226 * It's possible that the pool was expanded while it was exported.
4227 * We kick off an async task to handle this for us.
4229 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4231 mutex_exit(&spa_namespace_lock);
4232 spa_history_log_version(spa, "import");
4236 zvol_create_minors(pool);
4243 spa_tryimport(nvlist_t *tryconfig)
4245 nvlist_t *config = NULL;
4251 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4254 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4258 * Create and initialize the spa structure.
4260 mutex_enter(&spa_namespace_lock);
4261 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4262 spa_activate(spa, FREAD);
4265 * Pass off the heavy lifting to spa_load().
4266 * Pass TRUE for mosconfig because the user-supplied config
4267 * is actually the one to trust when doing an import.
4269 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4272 * If 'tryconfig' was at least parsable, return the current config.
4274 if (spa->spa_root_vdev != NULL) {
4275 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4276 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4278 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4280 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4281 spa->spa_uberblock.ub_timestamp) == 0);
4282 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4283 spa->spa_load_info) == 0);
4286 * If the bootfs property exists on this pool then we
4287 * copy it out so that external consumers can tell which
4288 * pools are bootable.
4290 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4291 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4294 * We have to play games with the name since the
4295 * pool was opened as TRYIMPORT_NAME.
4297 if (dsl_dsobj_to_dsname(spa_name(spa),
4298 spa->spa_bootfs, tmpname) == 0) {
4300 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4302 cp = strchr(tmpname, '/');
4304 (void) strlcpy(dsname, tmpname,
4307 (void) snprintf(dsname, MAXPATHLEN,
4308 "%s/%s", poolname, ++cp);
4310 VERIFY(nvlist_add_string(config,
4311 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4312 kmem_free(dsname, MAXPATHLEN);
4314 kmem_free(tmpname, MAXPATHLEN);
4318 * Add the list of hot spares and level 2 cache devices.
4320 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4321 spa_add_spares(spa, config);
4322 spa_add_l2cache(spa, config);
4323 spa_config_exit(spa, SCL_CONFIG, FTAG);
4327 spa_deactivate(spa);
4329 mutex_exit(&spa_namespace_lock);
4335 * Pool export/destroy
4337 * The act of destroying or exporting a pool is very simple. We make sure there
4338 * is no more pending I/O and any references to the pool are gone. Then, we
4339 * update the pool state and sync all the labels to disk, removing the
4340 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4341 * we don't sync the labels or remove the configuration cache.
4344 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4345 boolean_t force, boolean_t hardforce)
4352 if (!(spa_mode_global & FWRITE))
4353 return (SET_ERROR(EROFS));
4355 mutex_enter(&spa_namespace_lock);
4356 if ((spa = spa_lookup(pool)) == NULL) {
4357 mutex_exit(&spa_namespace_lock);
4358 return (SET_ERROR(ENOENT));
4362 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4363 * reacquire the namespace lock, and see if we can export.
4365 spa_open_ref(spa, FTAG);
4366 mutex_exit(&spa_namespace_lock);
4367 spa_async_suspend(spa);
4368 mutex_enter(&spa_namespace_lock);
4369 spa_close(spa, FTAG);
4372 * The pool will be in core if it's openable,
4373 * in which case we can modify its state.
4375 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4377 * Objsets may be open only because they're dirty, so we
4378 * have to force it to sync before checking spa_refcnt.
4380 txg_wait_synced(spa->spa_dsl_pool, 0);
4383 * A pool cannot be exported or destroyed if there are active
4384 * references. If we are resetting a pool, allow references by
4385 * fault injection handlers.
4387 if (!spa_refcount_zero(spa) ||
4388 (spa->spa_inject_ref != 0 &&
4389 new_state != POOL_STATE_UNINITIALIZED)) {
4390 spa_async_resume(spa);
4391 mutex_exit(&spa_namespace_lock);
4392 return (SET_ERROR(EBUSY));
4396 * A pool cannot be exported if it has an active shared spare.
4397 * This is to prevent other pools stealing the active spare
4398 * from an exported pool. At user's own will, such pool can
4399 * be forcedly exported.
4401 if (!force && new_state == POOL_STATE_EXPORTED &&
4402 spa_has_active_shared_spare(spa)) {
4403 spa_async_resume(spa);
4404 mutex_exit(&spa_namespace_lock);
4405 return (SET_ERROR(EXDEV));
4409 * We want this to be reflected on every label,
4410 * so mark them all dirty. spa_unload() will do the
4411 * final sync that pushes these changes out.
4413 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4414 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4415 spa->spa_state = new_state;
4416 spa->spa_final_txg = spa_last_synced_txg(spa) +
4418 vdev_config_dirty(spa->spa_root_vdev);
4419 spa_config_exit(spa, SCL_ALL, FTAG);
4423 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4425 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4427 spa_deactivate(spa);
4430 if (oldconfig && spa->spa_config)
4431 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4433 if (new_state != POOL_STATE_UNINITIALIZED) {
4435 spa_config_sync(spa, B_TRUE, B_TRUE);
4438 mutex_exit(&spa_namespace_lock);
4444 * Destroy a storage pool.
4447 spa_destroy(char *pool)
4449 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4454 * Export a storage pool.
4457 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4458 boolean_t hardforce)
4460 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4465 * Similar to spa_export(), this unloads the spa_t without actually removing it
4466 * from the namespace in any way.
4469 spa_reset(char *pool)
4471 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4476 * ==========================================================================
4477 * Device manipulation
4478 * ==========================================================================
4482 * Add a device to a storage pool.
4485 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4489 vdev_t *rvd = spa->spa_root_vdev;
4491 nvlist_t **spares, **l2cache;
4492 uint_t nspares, nl2cache;
4494 ASSERT(spa_writeable(spa));
4496 txg = spa_vdev_enter(spa);
4498 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4499 VDEV_ALLOC_ADD)) != 0)
4500 return (spa_vdev_exit(spa, NULL, txg, error));
4502 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4504 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4508 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4512 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4513 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4515 if (vd->vdev_children != 0 &&
4516 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4517 return (spa_vdev_exit(spa, vd, txg, error));
4520 * We must validate the spares and l2cache devices after checking the
4521 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4523 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4524 return (spa_vdev_exit(spa, vd, txg, error));
4527 * Transfer each new top-level vdev from vd to rvd.
4529 for (int c = 0; c < vd->vdev_children; c++) {
4532 * Set the vdev id to the first hole, if one exists.
4534 for (id = 0; id < rvd->vdev_children; id++) {
4535 if (rvd->vdev_child[id]->vdev_ishole) {
4536 vdev_free(rvd->vdev_child[id]);
4540 tvd = vd->vdev_child[c];
4541 vdev_remove_child(vd, tvd);
4543 vdev_add_child(rvd, tvd);
4544 vdev_config_dirty(tvd);
4548 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4549 ZPOOL_CONFIG_SPARES);
4550 spa_load_spares(spa);
4551 spa->spa_spares.sav_sync = B_TRUE;
4554 if (nl2cache != 0) {
4555 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4556 ZPOOL_CONFIG_L2CACHE);
4557 spa_load_l2cache(spa);
4558 spa->spa_l2cache.sav_sync = B_TRUE;
4562 * We have to be careful when adding new vdevs to an existing pool.
4563 * If other threads start allocating from these vdevs before we
4564 * sync the config cache, and we lose power, then upon reboot we may
4565 * fail to open the pool because there are DVAs that the config cache
4566 * can't translate. Therefore, we first add the vdevs without
4567 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4568 * and then let spa_config_update() initialize the new metaslabs.
4570 * spa_load() checks for added-but-not-initialized vdevs, so that
4571 * if we lose power at any point in this sequence, the remaining
4572 * steps will be completed the next time we load the pool.
4574 (void) spa_vdev_exit(spa, vd, txg, 0);
4576 mutex_enter(&spa_namespace_lock);
4577 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4578 mutex_exit(&spa_namespace_lock);
4584 * Attach a device to a mirror. The arguments are the path to any device
4585 * in the mirror, and the nvroot for the new device. If the path specifies
4586 * a device that is not mirrored, we automatically insert the mirror vdev.
4588 * If 'replacing' is specified, the new device is intended to replace the
4589 * existing device; in this case the two devices are made into their own
4590 * mirror using the 'replacing' vdev, which is functionally identical to
4591 * the mirror vdev (it actually reuses all the same ops) but has a few
4592 * extra rules: you can't attach to it after it's been created, and upon
4593 * completion of resilvering, the first disk (the one being replaced)
4594 * is automatically detached.
4597 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4599 uint64_t txg, dtl_max_txg;
4600 vdev_t *rvd = spa->spa_root_vdev;
4601 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4603 char *oldvdpath, *newvdpath;
4607 ASSERT(spa_writeable(spa));
4609 txg = spa_vdev_enter(spa);
4611 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4614 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4616 if (!oldvd->vdev_ops->vdev_op_leaf)
4617 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4619 pvd = oldvd->vdev_parent;
4621 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4622 VDEV_ALLOC_ATTACH)) != 0)
4623 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4625 if (newrootvd->vdev_children != 1)
4626 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4628 newvd = newrootvd->vdev_child[0];
4630 if (!newvd->vdev_ops->vdev_op_leaf)
4631 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4633 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4634 return (spa_vdev_exit(spa, newrootvd, txg, error));
4637 * Spares can't replace logs
4639 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4640 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4644 * For attach, the only allowable parent is a mirror or the root
4647 if (pvd->vdev_ops != &vdev_mirror_ops &&
4648 pvd->vdev_ops != &vdev_root_ops)
4649 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4651 pvops = &vdev_mirror_ops;
4654 * Active hot spares can only be replaced by inactive hot
4657 if (pvd->vdev_ops == &vdev_spare_ops &&
4658 oldvd->vdev_isspare &&
4659 !spa_has_spare(spa, newvd->vdev_guid))
4660 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4663 * If the source is a hot spare, and the parent isn't already a
4664 * spare, then we want to create a new hot spare. Otherwise, we
4665 * want to create a replacing vdev. The user is not allowed to
4666 * attach to a spared vdev child unless the 'isspare' state is
4667 * the same (spare replaces spare, non-spare replaces
4670 if (pvd->vdev_ops == &vdev_replacing_ops &&
4671 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4672 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4673 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4674 newvd->vdev_isspare != oldvd->vdev_isspare) {
4675 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4678 if (newvd->vdev_isspare)
4679 pvops = &vdev_spare_ops;
4681 pvops = &vdev_replacing_ops;
4685 * Make sure the new device is big enough.
4687 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4688 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4691 * The new device cannot have a higher alignment requirement
4692 * than the top-level vdev.
4694 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4695 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4698 * If this is an in-place replacement, update oldvd's path and devid
4699 * to make it distinguishable from newvd, and unopenable from now on.
4701 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4702 spa_strfree(oldvd->vdev_path);
4703 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4705 (void) sprintf(oldvd->vdev_path, "%s/%s",
4706 newvd->vdev_path, "old");
4707 if (oldvd->vdev_devid != NULL) {
4708 spa_strfree(oldvd->vdev_devid);
4709 oldvd->vdev_devid = NULL;
4713 /* mark the device being resilvered */
4714 newvd->vdev_resilvering = B_TRUE;
4717 * If the parent is not a mirror, or if we're replacing, insert the new
4718 * mirror/replacing/spare vdev above oldvd.
4720 if (pvd->vdev_ops != pvops)
4721 pvd = vdev_add_parent(oldvd, pvops);
4723 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4724 ASSERT(pvd->vdev_ops == pvops);
4725 ASSERT(oldvd->vdev_parent == pvd);
4728 * Extract the new device from its root and add it to pvd.
4730 vdev_remove_child(newrootvd, newvd);
4731 newvd->vdev_id = pvd->vdev_children;
4732 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4733 vdev_add_child(pvd, newvd);
4735 tvd = newvd->vdev_top;
4736 ASSERT(pvd->vdev_top == tvd);
4737 ASSERT(tvd->vdev_parent == rvd);
4739 vdev_config_dirty(tvd);
4742 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4743 * for any dmu_sync-ed blocks. It will propagate upward when
4744 * spa_vdev_exit() calls vdev_dtl_reassess().
4746 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4748 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4749 dtl_max_txg - TXG_INITIAL);
4751 if (newvd->vdev_isspare) {
4752 spa_spare_activate(newvd);
4753 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4756 oldvdpath = spa_strdup(oldvd->vdev_path);
4757 newvdpath = spa_strdup(newvd->vdev_path);
4758 newvd_isspare = newvd->vdev_isspare;
4761 * Mark newvd's DTL dirty in this txg.
4763 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4766 * Restart the resilver
4768 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4773 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4775 spa_history_log_internal(spa, "vdev attach", NULL,
4776 "%s vdev=%s %s vdev=%s",
4777 replacing && newvd_isspare ? "spare in" :
4778 replacing ? "replace" : "attach", newvdpath,
4779 replacing ? "for" : "to", oldvdpath);
4781 spa_strfree(oldvdpath);
4782 spa_strfree(newvdpath);
4784 if (spa->spa_bootfs)
4785 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4791 * Detach a device from a mirror or replacing vdev.
4793 * If 'replace_done' is specified, only detach if the parent
4794 * is a replacing vdev.
4797 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4801 vdev_t *rvd = spa->spa_root_vdev;
4802 vdev_t *vd, *pvd, *cvd, *tvd;
4803 boolean_t unspare = B_FALSE;
4804 uint64_t unspare_guid = 0;
4807 ASSERT(spa_writeable(spa));
4809 txg = spa_vdev_enter(spa);
4811 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4814 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4816 if (!vd->vdev_ops->vdev_op_leaf)
4817 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4819 pvd = vd->vdev_parent;
4822 * If the parent/child relationship is not as expected, don't do it.
4823 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4824 * vdev that's replacing B with C. The user's intent in replacing
4825 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4826 * the replace by detaching C, the expected behavior is to end up
4827 * M(A,B). But suppose that right after deciding to detach C,
4828 * the replacement of B completes. We would have M(A,C), and then
4829 * ask to detach C, which would leave us with just A -- not what
4830 * the user wanted. To prevent this, we make sure that the
4831 * parent/child relationship hasn't changed -- in this example,
4832 * that C's parent is still the replacing vdev R.
4834 if (pvd->vdev_guid != pguid && pguid != 0)
4835 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4838 * Only 'replacing' or 'spare' vdevs can be replaced.
4840 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4841 pvd->vdev_ops != &vdev_spare_ops)
4842 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4844 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4845 spa_version(spa) >= SPA_VERSION_SPARES);
4848 * Only mirror, replacing, and spare vdevs support detach.
4850 if (pvd->vdev_ops != &vdev_replacing_ops &&
4851 pvd->vdev_ops != &vdev_mirror_ops &&
4852 pvd->vdev_ops != &vdev_spare_ops)
4853 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4856 * If this device has the only valid copy of some data,
4857 * we cannot safely detach it.
4859 if (vdev_dtl_required(vd))
4860 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4862 ASSERT(pvd->vdev_children >= 2);
4865 * If we are detaching the second disk from a replacing vdev, then
4866 * check to see if we changed the original vdev's path to have "/old"
4867 * at the end in spa_vdev_attach(). If so, undo that change now.
4869 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4870 vd->vdev_path != NULL) {
4871 size_t len = strlen(vd->vdev_path);
4873 for (int c = 0; c < pvd->vdev_children; c++) {
4874 cvd = pvd->vdev_child[c];
4876 if (cvd == vd || cvd->vdev_path == NULL)
4879 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4880 strcmp(cvd->vdev_path + len, "/old") == 0) {
4881 spa_strfree(cvd->vdev_path);
4882 cvd->vdev_path = spa_strdup(vd->vdev_path);
4889 * If we are detaching the original disk from a spare, then it implies
4890 * that the spare should become a real disk, and be removed from the
4891 * active spare list for the pool.
4893 if (pvd->vdev_ops == &vdev_spare_ops &&
4895 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4899 * Erase the disk labels so the disk can be used for other things.
4900 * This must be done after all other error cases are handled,
4901 * but before we disembowel vd (so we can still do I/O to it).
4902 * But if we can't do it, don't treat the error as fatal --
4903 * it may be that the unwritability of the disk is the reason
4904 * it's being detached!
4906 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4909 * Remove vd from its parent and compact the parent's children.
4911 vdev_remove_child(pvd, vd);
4912 vdev_compact_children(pvd);
4915 * Remember one of the remaining children so we can get tvd below.
4917 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4920 * If we need to remove the remaining child from the list of hot spares,
4921 * do it now, marking the vdev as no longer a spare in the process.
4922 * We must do this before vdev_remove_parent(), because that can
4923 * change the GUID if it creates a new toplevel GUID. For a similar
4924 * reason, we must remove the spare now, in the same txg as the detach;
4925 * otherwise someone could attach a new sibling, change the GUID, and
4926 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4929 ASSERT(cvd->vdev_isspare);
4930 spa_spare_remove(cvd);
4931 unspare_guid = cvd->vdev_guid;
4932 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4933 cvd->vdev_unspare = B_TRUE;
4937 * If the parent mirror/replacing vdev only has one child,
4938 * the parent is no longer needed. Remove it from the tree.
4940 if (pvd->vdev_children == 1) {
4941 if (pvd->vdev_ops == &vdev_spare_ops)
4942 cvd->vdev_unspare = B_FALSE;
4943 vdev_remove_parent(cvd);
4948 * We don't set tvd until now because the parent we just removed
4949 * may have been the previous top-level vdev.
4951 tvd = cvd->vdev_top;
4952 ASSERT(tvd->vdev_parent == rvd);
4955 * Reevaluate the parent vdev state.
4957 vdev_propagate_state(cvd);
4960 * If the 'autoexpand' property is set on the pool then automatically
4961 * try to expand the size of the pool. For example if the device we
4962 * just detached was smaller than the others, it may be possible to
4963 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4964 * first so that we can obtain the updated sizes of the leaf vdevs.
4966 if (spa->spa_autoexpand) {
4968 vdev_expand(tvd, txg);
4971 vdev_config_dirty(tvd);
4974 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4975 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4976 * But first make sure we're not on any *other* txg's DTL list, to
4977 * prevent vd from being accessed after it's freed.
4979 vdpath = spa_strdup(vd->vdev_path);
4980 for (int t = 0; t < TXG_SIZE; t++)
4981 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4982 vd->vdev_detached = B_TRUE;
4983 vdev_dirty(tvd, VDD_DTL, vd, txg);
4985 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
4987 /* hang on to the spa before we release the lock */
4988 spa_open_ref(spa, FTAG);
4990 error = spa_vdev_exit(spa, vd, txg, 0);
4992 spa_history_log_internal(spa, "detach", NULL,
4994 spa_strfree(vdpath);
4997 * If this was the removal of the original device in a hot spare vdev,
4998 * then we want to go through and remove the device from the hot spare
4999 * list of every other pool.
5002 spa_t *altspa = NULL;
5004 mutex_enter(&spa_namespace_lock);
5005 while ((altspa = spa_next(altspa)) != NULL) {
5006 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5010 spa_open_ref(altspa, FTAG);
5011 mutex_exit(&spa_namespace_lock);
5012 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5013 mutex_enter(&spa_namespace_lock);
5014 spa_close(altspa, FTAG);
5016 mutex_exit(&spa_namespace_lock);
5018 /* search the rest of the vdevs for spares to remove */
5019 spa_vdev_resilver_done(spa);
5022 /* all done with the spa; OK to release */
5023 mutex_enter(&spa_namespace_lock);
5024 spa_close(spa, FTAG);
5025 mutex_exit(&spa_namespace_lock);
5031 * Split a set of devices from their mirrors, and create a new pool from them.
5034 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5035 nvlist_t *props, boolean_t exp)
5038 uint64_t txg, *glist;
5040 uint_t c, children, lastlog;
5041 nvlist_t **child, *nvl, *tmp;
5043 char *altroot = NULL;
5044 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5045 boolean_t activate_slog;
5047 ASSERT(spa_writeable(spa));
5049 txg = spa_vdev_enter(spa);
5051 /* clear the log and flush everything up to now */
5052 activate_slog = spa_passivate_log(spa);
5053 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5054 error = spa_offline_log(spa);
5055 txg = spa_vdev_config_enter(spa);
5058 spa_activate_log(spa);
5061 return (spa_vdev_exit(spa, NULL, txg, error));
5063 /* check new spa name before going any further */
5064 if (spa_lookup(newname) != NULL)
5065 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5068 * scan through all the children to ensure they're all mirrors
5070 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5071 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5073 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5075 /* first, check to ensure we've got the right child count */
5076 rvd = spa->spa_root_vdev;
5078 for (c = 0; c < rvd->vdev_children; c++) {
5079 vdev_t *vd = rvd->vdev_child[c];
5081 /* don't count the holes & logs as children */
5082 if (vd->vdev_islog || vd->vdev_ishole) {
5090 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5091 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5093 /* next, ensure no spare or cache devices are part of the split */
5094 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5095 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5096 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5098 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5099 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5101 /* then, loop over each vdev and validate it */
5102 for (c = 0; c < children; c++) {
5103 uint64_t is_hole = 0;
5105 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5109 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5110 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5113 error = SET_ERROR(EINVAL);
5118 /* which disk is going to be split? */
5119 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5121 error = SET_ERROR(EINVAL);
5125 /* look it up in the spa */
5126 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5127 if (vml[c] == NULL) {
5128 error = SET_ERROR(ENODEV);
5132 /* make sure there's nothing stopping the split */
5133 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5134 vml[c]->vdev_islog ||
5135 vml[c]->vdev_ishole ||
5136 vml[c]->vdev_isspare ||
5137 vml[c]->vdev_isl2cache ||
5138 !vdev_writeable(vml[c]) ||
5139 vml[c]->vdev_children != 0 ||
5140 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5141 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5142 error = SET_ERROR(EINVAL);
5146 if (vdev_dtl_required(vml[c])) {
5147 error = SET_ERROR(EBUSY);
5151 /* we need certain info from the top level */
5152 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5153 vml[c]->vdev_top->vdev_ms_array) == 0);
5154 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5155 vml[c]->vdev_top->vdev_ms_shift) == 0);
5156 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5157 vml[c]->vdev_top->vdev_asize) == 0);
5158 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5159 vml[c]->vdev_top->vdev_ashift) == 0);
5163 kmem_free(vml, children * sizeof (vdev_t *));
5164 kmem_free(glist, children * sizeof (uint64_t));
5165 return (spa_vdev_exit(spa, NULL, txg, error));
5168 /* stop writers from using the disks */
5169 for (c = 0; c < children; c++) {
5171 vml[c]->vdev_offline = B_TRUE;
5173 vdev_reopen(spa->spa_root_vdev);
5176 * Temporarily record the splitting vdevs in the spa config. This
5177 * will disappear once the config is regenerated.
5179 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5180 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5181 glist, children) == 0);
5182 kmem_free(glist, children * sizeof (uint64_t));
5184 mutex_enter(&spa->spa_props_lock);
5185 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5187 mutex_exit(&spa->spa_props_lock);
5188 spa->spa_config_splitting = nvl;
5189 vdev_config_dirty(spa->spa_root_vdev);
5191 /* configure and create the new pool */
5192 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5193 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5194 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5195 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5196 spa_version(spa)) == 0);
5197 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5198 spa->spa_config_txg) == 0);
5199 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5200 spa_generate_guid(NULL)) == 0);
5201 (void) nvlist_lookup_string(props,
5202 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5204 /* add the new pool to the namespace */
5205 newspa = spa_add(newname, config, altroot);
5206 newspa->spa_config_txg = spa->spa_config_txg;
5207 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5209 /* release the spa config lock, retaining the namespace lock */
5210 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5212 if (zio_injection_enabled)
5213 zio_handle_panic_injection(spa, FTAG, 1);
5215 spa_activate(newspa, spa_mode_global);
5216 spa_async_suspend(newspa);
5219 /* mark that we are creating new spa by splitting */
5220 newspa->spa_splitting_newspa = B_TRUE;
5222 /* create the new pool from the disks of the original pool */
5223 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5225 newspa->spa_splitting_newspa = B_FALSE;
5230 /* if that worked, generate a real config for the new pool */
5231 if (newspa->spa_root_vdev != NULL) {
5232 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5233 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5234 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5235 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5236 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5241 if (props != NULL) {
5242 spa_configfile_set(newspa, props, B_FALSE);
5243 error = spa_prop_set(newspa, props);
5248 /* flush everything */
5249 txg = spa_vdev_config_enter(newspa);
5250 vdev_config_dirty(newspa->spa_root_vdev);
5251 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5253 if (zio_injection_enabled)
5254 zio_handle_panic_injection(spa, FTAG, 2);
5256 spa_async_resume(newspa);
5258 /* finally, update the original pool's config */
5259 txg = spa_vdev_config_enter(spa);
5260 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5261 error = dmu_tx_assign(tx, TXG_WAIT);
5264 for (c = 0; c < children; c++) {
5265 if (vml[c] != NULL) {
5268 spa_history_log_internal(spa, "detach", tx,
5269 "vdev=%s", vml[c]->vdev_path);
5273 vdev_config_dirty(spa->spa_root_vdev);
5274 spa->spa_config_splitting = NULL;
5278 (void) spa_vdev_exit(spa, NULL, txg, 0);
5280 if (zio_injection_enabled)
5281 zio_handle_panic_injection(spa, FTAG, 3);
5283 /* split is complete; log a history record */
5284 spa_history_log_internal(newspa, "split", NULL,
5285 "from pool %s", spa_name(spa));
5287 kmem_free(vml, children * sizeof (vdev_t *));
5289 /* if we're not going to mount the filesystems in userland, export */
5291 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5298 spa_deactivate(newspa);
5301 txg = spa_vdev_config_enter(spa);
5303 /* re-online all offlined disks */
5304 for (c = 0; c < children; c++) {
5306 vml[c]->vdev_offline = B_FALSE;
5308 vdev_reopen(spa->spa_root_vdev);
5310 nvlist_free(spa->spa_config_splitting);
5311 spa->spa_config_splitting = NULL;
5312 (void) spa_vdev_exit(spa, NULL, txg, error);
5314 kmem_free(vml, children * sizeof (vdev_t *));
5319 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5321 for (int i = 0; i < count; i++) {
5324 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5327 if (guid == target_guid)
5335 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5336 nvlist_t *dev_to_remove)
5338 nvlist_t **newdev = NULL;
5341 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5343 for (int i = 0, j = 0; i < count; i++) {
5344 if (dev[i] == dev_to_remove)
5346 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5349 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5350 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5352 for (int i = 0; i < count - 1; i++)
5353 nvlist_free(newdev[i]);
5356 kmem_free(newdev, (count - 1) * sizeof (void *));
5360 * Evacuate the device.
5363 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5368 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5369 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5370 ASSERT(vd == vd->vdev_top);
5373 * Evacuate the device. We don't hold the config lock as writer
5374 * since we need to do I/O but we do keep the
5375 * spa_namespace_lock held. Once this completes the device
5376 * should no longer have any blocks allocated on it.
5378 if (vd->vdev_islog) {
5379 if (vd->vdev_stat.vs_alloc != 0)
5380 error = spa_offline_log(spa);
5382 error = SET_ERROR(ENOTSUP);
5389 * The evacuation succeeded. Remove any remaining MOS metadata
5390 * associated with this vdev, and wait for these changes to sync.
5392 ASSERT0(vd->vdev_stat.vs_alloc);
5393 txg = spa_vdev_config_enter(spa);
5394 vd->vdev_removing = B_TRUE;
5395 vdev_dirty(vd, 0, NULL, txg);
5396 vdev_config_dirty(vd);
5397 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5403 * Complete the removal by cleaning up the namespace.
5406 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5408 vdev_t *rvd = spa->spa_root_vdev;
5409 uint64_t id = vd->vdev_id;
5410 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5412 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5413 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5414 ASSERT(vd == vd->vdev_top);
5417 * Only remove any devices which are empty.
5419 if (vd->vdev_stat.vs_alloc != 0)
5422 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5424 if (list_link_active(&vd->vdev_state_dirty_node))
5425 vdev_state_clean(vd);
5426 if (list_link_active(&vd->vdev_config_dirty_node))
5427 vdev_config_clean(vd);
5432 vdev_compact_children(rvd);
5434 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5435 vdev_add_child(rvd, vd);
5437 vdev_config_dirty(rvd);
5440 * Reassess the health of our root vdev.
5446 * Remove a device from the pool -
5448 * Removing a device from the vdev namespace requires several steps
5449 * and can take a significant amount of time. As a result we use
5450 * the spa_vdev_config_[enter/exit] functions which allow us to
5451 * grab and release the spa_config_lock while still holding the namespace
5452 * lock. During each step the configuration is synced out.
5454 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5458 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5461 metaslab_group_t *mg;
5462 nvlist_t **spares, **l2cache, *nv;
5464 uint_t nspares, nl2cache;
5466 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5468 ASSERT(spa_writeable(spa));
5471 txg = spa_vdev_enter(spa);
5473 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5475 if (spa->spa_spares.sav_vdevs != NULL &&
5476 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5477 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5478 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5480 * Only remove the hot spare if it's not currently in use
5483 if (vd == NULL || unspare) {
5484 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5485 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5486 spa_load_spares(spa);
5487 spa->spa_spares.sav_sync = B_TRUE;
5489 error = SET_ERROR(EBUSY);
5491 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5492 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5493 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5494 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5496 * Cache devices can always be removed.
5498 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5499 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5500 spa_load_l2cache(spa);
5501 spa->spa_l2cache.sav_sync = B_TRUE;
5502 } else if (vd != NULL && vd->vdev_islog) {
5504 ASSERT(vd == vd->vdev_top);
5507 * XXX - Once we have bp-rewrite this should
5508 * become the common case.
5514 * Stop allocating from this vdev.
5516 metaslab_group_passivate(mg);
5519 * Wait for the youngest allocations and frees to sync,
5520 * and then wait for the deferral of those frees to finish.
5522 spa_vdev_config_exit(spa, NULL,
5523 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5526 * Attempt to evacuate the vdev.
5528 error = spa_vdev_remove_evacuate(spa, vd);
5530 txg = spa_vdev_config_enter(spa);
5533 * If we couldn't evacuate the vdev, unwind.
5536 metaslab_group_activate(mg);
5537 return (spa_vdev_exit(spa, NULL, txg, error));
5541 * Clean up the vdev namespace.
5543 spa_vdev_remove_from_namespace(spa, vd);
5545 } else if (vd != NULL) {
5547 * Normal vdevs cannot be removed (yet).
5549 error = SET_ERROR(ENOTSUP);
5552 * There is no vdev of any kind with the specified guid.
5554 error = SET_ERROR(ENOENT);
5558 return (spa_vdev_exit(spa, NULL, txg, error));
5564 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5565 * currently spared, so we can detach it.
5568 spa_vdev_resilver_done_hunt(vdev_t *vd)
5570 vdev_t *newvd, *oldvd;
5572 for (int c = 0; c < vd->vdev_children; c++) {
5573 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5578 if (vd->vdev_resilvering && vdev_dtl_empty(vd, DTL_MISSING) &&
5579 vdev_dtl_empty(vd, DTL_OUTAGE)) {
5580 ASSERT(vd->vdev_ops->vdev_op_leaf);
5581 vd->vdev_resilvering = B_FALSE;
5582 vdev_config_dirty(vd->vdev_top);
5586 * Check for a completed replacement. We always consider the first
5587 * vdev in the list to be the oldest vdev, and the last one to be
5588 * the newest (see spa_vdev_attach() for how that works). In
5589 * the case where the newest vdev is faulted, we will not automatically
5590 * remove it after a resilver completes. This is OK as it will require
5591 * user intervention to determine which disk the admin wishes to keep.
5593 if (vd->vdev_ops == &vdev_replacing_ops) {
5594 ASSERT(vd->vdev_children > 1);
5596 newvd = vd->vdev_child[vd->vdev_children - 1];
5597 oldvd = vd->vdev_child[0];
5599 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5600 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5601 !vdev_dtl_required(oldvd))
5606 * Check for a completed resilver with the 'unspare' flag set.
5608 if (vd->vdev_ops == &vdev_spare_ops) {
5609 vdev_t *first = vd->vdev_child[0];
5610 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5612 if (last->vdev_unspare) {
5615 } else if (first->vdev_unspare) {
5622 if (oldvd != NULL &&
5623 vdev_dtl_empty(newvd, DTL_MISSING) &&
5624 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5625 !vdev_dtl_required(oldvd))
5629 * If there are more than two spares attached to a disk,
5630 * and those spares are not required, then we want to
5631 * attempt to free them up now so that they can be used
5632 * by other pools. Once we're back down to a single
5633 * disk+spare, we stop removing them.
5635 if (vd->vdev_children > 2) {
5636 newvd = vd->vdev_child[1];
5638 if (newvd->vdev_isspare && last->vdev_isspare &&
5639 vdev_dtl_empty(last, DTL_MISSING) &&
5640 vdev_dtl_empty(last, DTL_OUTAGE) &&
5641 !vdev_dtl_required(newvd))
5650 spa_vdev_resilver_done(spa_t *spa)
5652 vdev_t *vd, *pvd, *ppvd;
5653 uint64_t guid, sguid, pguid, ppguid;
5655 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5657 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5658 pvd = vd->vdev_parent;
5659 ppvd = pvd->vdev_parent;
5660 guid = vd->vdev_guid;
5661 pguid = pvd->vdev_guid;
5662 ppguid = ppvd->vdev_guid;
5665 * If we have just finished replacing a hot spared device, then
5666 * we need to detach the parent's first child (the original hot
5669 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5670 ppvd->vdev_children == 2) {
5671 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5672 sguid = ppvd->vdev_child[1]->vdev_guid;
5674 spa_config_exit(spa, SCL_ALL, FTAG);
5675 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5677 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5679 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5682 spa_config_exit(spa, SCL_ALL, FTAG);
5686 * Update the stored path or FRU for this vdev.
5689 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5693 boolean_t sync = B_FALSE;
5695 ASSERT(spa_writeable(spa));
5697 spa_vdev_state_enter(spa, SCL_ALL);
5699 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5700 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5702 if (!vd->vdev_ops->vdev_op_leaf)
5703 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5706 if (strcmp(value, vd->vdev_path) != 0) {
5707 spa_strfree(vd->vdev_path);
5708 vd->vdev_path = spa_strdup(value);
5712 if (vd->vdev_fru == NULL) {
5713 vd->vdev_fru = spa_strdup(value);
5715 } else if (strcmp(value, vd->vdev_fru) != 0) {
5716 spa_strfree(vd->vdev_fru);
5717 vd->vdev_fru = spa_strdup(value);
5722 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5726 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5728 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5732 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5734 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5738 * ==========================================================================
5740 * ==========================================================================
5744 spa_scan_stop(spa_t *spa)
5746 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5747 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5748 return (SET_ERROR(EBUSY));
5749 return (dsl_scan_cancel(spa->spa_dsl_pool));
5753 spa_scan(spa_t *spa, pool_scan_func_t func)
5755 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5757 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5758 return (SET_ERROR(ENOTSUP));
5761 * If a resilver was requested, but there is no DTL on a
5762 * writeable leaf device, we have nothing to do.
5764 if (func == POOL_SCAN_RESILVER &&
5765 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5766 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5770 return (dsl_scan(spa->spa_dsl_pool, func));
5774 * ==========================================================================
5775 * SPA async task processing
5776 * ==========================================================================
5780 spa_async_remove(spa_t *spa, vdev_t *vd)
5782 if (vd->vdev_remove_wanted) {
5783 vd->vdev_remove_wanted = B_FALSE;
5784 vd->vdev_delayed_close = B_FALSE;
5785 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5788 * We want to clear the stats, but we don't want to do a full
5789 * vdev_clear() as that will cause us to throw away
5790 * degraded/faulted state as well as attempt to reopen the
5791 * device, all of which is a waste.
5793 vd->vdev_stat.vs_read_errors = 0;
5794 vd->vdev_stat.vs_write_errors = 0;
5795 vd->vdev_stat.vs_checksum_errors = 0;
5797 vdev_state_dirty(vd->vdev_top);
5800 for (int c = 0; c < vd->vdev_children; c++)
5801 spa_async_remove(spa, vd->vdev_child[c]);
5805 spa_async_probe(spa_t *spa, vdev_t *vd)
5807 if (vd->vdev_probe_wanted) {
5808 vd->vdev_probe_wanted = B_FALSE;
5809 vdev_reopen(vd); /* vdev_open() does the actual probe */
5812 for (int c = 0; c < vd->vdev_children; c++)
5813 spa_async_probe(spa, vd->vdev_child[c]);
5817 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5823 if (!spa->spa_autoexpand)
5826 for (int c = 0; c < vd->vdev_children; c++) {
5827 vdev_t *cvd = vd->vdev_child[c];
5828 spa_async_autoexpand(spa, cvd);
5831 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5834 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5835 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5837 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5838 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5840 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5841 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5844 kmem_free(physpath, MAXPATHLEN);
5848 spa_async_thread(void *arg)
5853 ASSERT(spa->spa_sync_on);
5855 mutex_enter(&spa->spa_async_lock);
5856 tasks = spa->spa_async_tasks;
5857 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
5858 mutex_exit(&spa->spa_async_lock);
5861 * See if the config needs to be updated.
5863 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5864 uint64_t old_space, new_space;
5866 mutex_enter(&spa_namespace_lock);
5867 old_space = metaslab_class_get_space(spa_normal_class(spa));
5868 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5869 new_space = metaslab_class_get_space(spa_normal_class(spa));
5870 mutex_exit(&spa_namespace_lock);
5873 * If the pool grew as a result of the config update,
5874 * then log an internal history event.
5876 if (new_space != old_space) {
5877 spa_history_log_internal(spa, "vdev online", NULL,
5878 "pool '%s' size: %llu(+%llu)",
5879 spa_name(spa), new_space, new_space - old_space);
5883 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5884 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5885 spa_async_autoexpand(spa, spa->spa_root_vdev);
5886 spa_config_exit(spa, SCL_CONFIG, FTAG);
5890 * See if any devices need to be probed.
5892 if (tasks & SPA_ASYNC_PROBE) {
5893 spa_vdev_state_enter(spa, SCL_NONE);
5894 spa_async_probe(spa, spa->spa_root_vdev);
5895 (void) spa_vdev_state_exit(spa, NULL, 0);
5899 * If any devices are done replacing, detach them.
5901 if (tasks & SPA_ASYNC_RESILVER_DONE)
5902 spa_vdev_resilver_done(spa);
5905 * Kick off a resilver.
5907 if (tasks & SPA_ASYNC_RESILVER)
5908 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5911 * Let the world know that we're done.
5913 mutex_enter(&spa->spa_async_lock);
5914 spa->spa_async_thread = NULL;
5915 cv_broadcast(&spa->spa_async_cv);
5916 mutex_exit(&spa->spa_async_lock);
5921 spa_async_thread_vd(void *arg)
5926 ASSERT(spa->spa_sync_on);
5928 mutex_enter(&spa->spa_async_lock);
5929 tasks = spa->spa_async_tasks;
5931 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
5932 mutex_exit(&spa->spa_async_lock);
5935 * See if any devices need to be marked REMOVED.
5937 if (tasks & SPA_ASYNC_REMOVE) {
5938 spa_vdev_state_enter(spa, SCL_NONE);
5939 spa_async_remove(spa, spa->spa_root_vdev);
5940 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
5941 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5942 for (int i = 0; i < spa->spa_spares.sav_count; i++)
5943 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5944 (void) spa_vdev_state_exit(spa, NULL, 0);
5948 * Let the world know that we're done.
5950 mutex_enter(&spa->spa_async_lock);
5951 tasks = spa->spa_async_tasks;
5952 if ((tasks & SPA_ASYNC_REMOVE) != 0)
5954 spa->spa_async_thread_vd = NULL;
5955 cv_broadcast(&spa->spa_async_cv);
5956 mutex_exit(&spa->spa_async_lock);
5961 spa_async_suspend(spa_t *spa)
5963 mutex_enter(&spa->spa_async_lock);
5964 spa->spa_async_suspended++;
5965 while (spa->spa_async_thread != NULL &&
5966 spa->spa_async_thread_vd != NULL)
5967 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5968 mutex_exit(&spa->spa_async_lock);
5972 spa_async_resume(spa_t *spa)
5974 mutex_enter(&spa->spa_async_lock);
5975 ASSERT(spa->spa_async_suspended != 0);
5976 spa->spa_async_suspended--;
5977 mutex_exit(&spa->spa_async_lock);
5981 spa_async_tasks_pending(spa_t *spa)
5983 uint_t non_config_tasks;
5985 boolean_t config_task_suspended;
5987 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
5989 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
5990 if (spa->spa_ccw_fail_time == 0) {
5991 config_task_suspended = B_FALSE;
5993 config_task_suspended =
5994 (gethrtime() - spa->spa_ccw_fail_time) <
5995 (zfs_ccw_retry_interval * NANOSEC);
5998 return (non_config_tasks || (config_task && !config_task_suspended));
6002 spa_async_dispatch(spa_t *spa)
6004 mutex_enter(&spa->spa_async_lock);
6005 if (spa_async_tasks_pending(spa) &&
6006 !spa->spa_async_suspended &&
6007 spa->spa_async_thread == NULL &&
6009 spa->spa_async_thread = thread_create(NULL, 0,
6010 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6011 mutex_exit(&spa->spa_async_lock);
6015 spa_async_dispatch_vd(spa_t *spa)
6017 mutex_enter(&spa->spa_async_lock);
6018 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6019 !spa->spa_async_suspended &&
6020 spa->spa_async_thread_vd == NULL &&
6022 spa->spa_async_thread_vd = thread_create(NULL, 0,
6023 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6024 mutex_exit(&spa->spa_async_lock);
6028 spa_async_request(spa_t *spa, int task)
6030 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6031 mutex_enter(&spa->spa_async_lock);
6032 spa->spa_async_tasks |= task;
6033 mutex_exit(&spa->spa_async_lock);
6034 spa_async_dispatch_vd(spa);
6038 * ==========================================================================
6039 * SPA syncing routines
6040 * ==========================================================================
6044 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6047 bpobj_enqueue(bpo, bp, tx);
6052 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6056 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6057 BP_GET_PSIZE(bp), zio->io_flags));
6062 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6064 char *packed = NULL;
6069 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6072 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6073 * information. This avoids the dbuf_will_dirty() path and
6074 * saves us a pre-read to get data we don't actually care about.
6076 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6077 packed = kmem_alloc(bufsize, KM_SLEEP);
6079 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6081 bzero(packed + nvsize, bufsize - nvsize);
6083 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6085 kmem_free(packed, bufsize);
6087 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6088 dmu_buf_will_dirty(db, tx);
6089 *(uint64_t *)db->db_data = nvsize;
6090 dmu_buf_rele(db, FTAG);
6094 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6095 const char *config, const char *entry)
6105 * Update the MOS nvlist describing the list of available devices.
6106 * spa_validate_aux() will have already made sure this nvlist is
6107 * valid and the vdevs are labeled appropriately.
6109 if (sav->sav_object == 0) {
6110 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6111 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6112 sizeof (uint64_t), tx);
6113 VERIFY(zap_update(spa->spa_meta_objset,
6114 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6115 &sav->sav_object, tx) == 0);
6118 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6119 if (sav->sav_count == 0) {
6120 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6122 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6123 for (i = 0; i < sav->sav_count; i++)
6124 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6125 B_FALSE, VDEV_CONFIG_L2CACHE);
6126 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6127 sav->sav_count) == 0);
6128 for (i = 0; i < sav->sav_count; i++)
6129 nvlist_free(list[i]);
6130 kmem_free(list, sav->sav_count * sizeof (void *));
6133 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6134 nvlist_free(nvroot);
6136 sav->sav_sync = B_FALSE;
6140 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6144 if (list_is_empty(&spa->spa_config_dirty_list))
6147 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6149 config = spa_config_generate(spa, spa->spa_root_vdev,
6150 dmu_tx_get_txg(tx), B_FALSE);
6153 * If we're upgrading the spa version then make sure that
6154 * the config object gets updated with the correct version.
6156 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6157 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6158 spa->spa_uberblock.ub_version);
6160 spa_config_exit(spa, SCL_STATE, FTAG);
6162 if (spa->spa_config_syncing)
6163 nvlist_free(spa->spa_config_syncing);
6164 spa->spa_config_syncing = config;
6166 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6170 spa_sync_version(void *arg, dmu_tx_t *tx)
6172 uint64_t *versionp = arg;
6173 uint64_t version = *versionp;
6174 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6177 * Setting the version is special cased when first creating the pool.
6179 ASSERT(tx->tx_txg != TXG_INITIAL);
6181 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6182 ASSERT(version >= spa_version(spa));
6184 spa->spa_uberblock.ub_version = version;
6185 vdev_config_dirty(spa->spa_root_vdev);
6186 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6190 * Set zpool properties.
6193 spa_sync_props(void *arg, dmu_tx_t *tx)
6195 nvlist_t *nvp = arg;
6196 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6197 objset_t *mos = spa->spa_meta_objset;
6198 nvpair_t *elem = NULL;
6200 mutex_enter(&spa->spa_props_lock);
6202 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6204 char *strval, *fname;
6206 const char *propname;
6207 zprop_type_t proptype;
6208 zfeature_info_t *feature;
6210 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6213 * We checked this earlier in spa_prop_validate().
6215 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6217 fname = strchr(nvpair_name(elem), '@') + 1;
6218 VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature));
6220 spa_feature_enable(spa, feature, tx);
6221 spa_history_log_internal(spa, "set", tx,
6222 "%s=enabled", nvpair_name(elem));
6225 case ZPOOL_PROP_VERSION:
6226 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
6228 * The version is synced seperatly before other
6229 * properties and should be correct by now.
6231 ASSERT3U(spa_version(spa), >=, intval);
6234 case ZPOOL_PROP_ALTROOT:
6236 * 'altroot' is a non-persistent property. It should
6237 * have been set temporarily at creation or import time.
6239 ASSERT(spa->spa_root != NULL);
6242 case ZPOOL_PROP_READONLY:
6243 case ZPOOL_PROP_CACHEFILE:
6245 * 'readonly' and 'cachefile' are also non-persisitent
6249 case ZPOOL_PROP_COMMENT:
6250 VERIFY(nvpair_value_string(elem, &strval) == 0);
6251 if (spa->spa_comment != NULL)
6252 spa_strfree(spa->spa_comment);
6253 spa->spa_comment = spa_strdup(strval);
6255 * We need to dirty the configuration on all the vdevs
6256 * so that their labels get updated. It's unnecessary
6257 * to do this for pool creation since the vdev's
6258 * configuratoin has already been dirtied.
6260 if (tx->tx_txg != TXG_INITIAL)
6261 vdev_config_dirty(spa->spa_root_vdev);
6262 spa_history_log_internal(spa, "set", tx,
6263 "%s=%s", nvpair_name(elem), strval);
6267 * Set pool property values in the poolprops mos object.
6269 if (spa->spa_pool_props_object == 0) {
6270 spa->spa_pool_props_object =
6271 zap_create_link(mos, DMU_OT_POOL_PROPS,
6272 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6276 /* normalize the property name */
6277 propname = zpool_prop_to_name(prop);
6278 proptype = zpool_prop_get_type(prop);
6280 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6281 ASSERT(proptype == PROP_TYPE_STRING);
6282 VERIFY(nvpair_value_string(elem, &strval) == 0);
6283 VERIFY(zap_update(mos,
6284 spa->spa_pool_props_object, propname,
6285 1, strlen(strval) + 1, strval, tx) == 0);
6286 spa_history_log_internal(spa, "set", tx,
6287 "%s=%s", nvpair_name(elem), strval);
6288 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6289 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
6291 if (proptype == PROP_TYPE_INDEX) {
6293 VERIFY(zpool_prop_index_to_string(
6294 prop, intval, &unused) == 0);
6296 VERIFY(zap_update(mos,
6297 spa->spa_pool_props_object, propname,
6298 8, 1, &intval, tx) == 0);
6299 spa_history_log_internal(spa, "set", tx,
6300 "%s=%lld", nvpair_name(elem), intval);
6302 ASSERT(0); /* not allowed */
6306 case ZPOOL_PROP_DELEGATION:
6307 spa->spa_delegation = intval;
6309 case ZPOOL_PROP_BOOTFS:
6310 spa->spa_bootfs = intval;
6312 case ZPOOL_PROP_FAILUREMODE:
6313 spa->spa_failmode = intval;
6315 case ZPOOL_PROP_AUTOEXPAND:
6316 spa->spa_autoexpand = intval;
6317 if (tx->tx_txg != TXG_INITIAL)
6318 spa_async_request(spa,
6319 SPA_ASYNC_AUTOEXPAND);
6321 case ZPOOL_PROP_DEDUPDITTO:
6322 spa->spa_dedup_ditto = intval;
6331 mutex_exit(&spa->spa_props_lock);
6335 * Perform one-time upgrade on-disk changes. spa_version() does not
6336 * reflect the new version this txg, so there must be no changes this
6337 * txg to anything that the upgrade code depends on after it executes.
6338 * Therefore this must be called after dsl_pool_sync() does the sync
6342 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6344 dsl_pool_t *dp = spa->spa_dsl_pool;
6346 ASSERT(spa->spa_sync_pass == 1);
6348 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6350 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6351 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6352 dsl_pool_create_origin(dp, tx);
6354 /* Keeping the origin open increases spa_minref */
6355 spa->spa_minref += 3;
6358 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6359 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6360 dsl_pool_upgrade_clones(dp, tx);
6363 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6364 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6365 dsl_pool_upgrade_dir_clones(dp, tx);
6367 /* Keeping the freedir open increases spa_minref */
6368 spa->spa_minref += 3;
6371 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6372 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6373 spa_feature_create_zap_objects(spa, tx);
6375 rrw_exit(&dp->dp_config_rwlock, FTAG);
6379 * Sync the specified transaction group. New blocks may be dirtied as
6380 * part of the process, so we iterate until it converges.
6383 spa_sync(spa_t *spa, uint64_t txg)
6385 dsl_pool_t *dp = spa->spa_dsl_pool;
6386 objset_t *mos = spa->spa_meta_objset;
6387 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
6388 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6389 vdev_t *rvd = spa->spa_root_vdev;
6394 VERIFY(spa_writeable(spa));
6397 * Lock out configuration changes.
6399 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6401 spa->spa_syncing_txg = txg;
6402 spa->spa_sync_pass = 0;
6405 * If there are any pending vdev state changes, convert them
6406 * into config changes that go out with this transaction group.
6408 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6409 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6411 * We need the write lock here because, for aux vdevs,
6412 * calling vdev_config_dirty() modifies sav_config.
6413 * This is ugly and will become unnecessary when we
6414 * eliminate the aux vdev wart by integrating all vdevs
6415 * into the root vdev tree.
6417 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6418 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6419 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6420 vdev_state_clean(vd);
6421 vdev_config_dirty(vd);
6423 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6424 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6426 spa_config_exit(spa, SCL_STATE, FTAG);
6428 tx = dmu_tx_create_assigned(dp, txg);
6430 spa->spa_sync_starttime = gethrtime();
6432 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6433 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6436 callout_reset(&spa->spa_deadman_cycid,
6437 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6442 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6443 * set spa_deflate if we have no raid-z vdevs.
6445 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6446 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6449 for (i = 0; i < rvd->vdev_children; i++) {
6450 vd = rvd->vdev_child[i];
6451 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6454 if (i == rvd->vdev_children) {
6455 spa->spa_deflate = TRUE;
6456 VERIFY(0 == zap_add(spa->spa_meta_objset,
6457 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6458 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6463 * If anything has changed in this txg, or if someone is waiting
6464 * for this txg to sync (eg, spa_vdev_remove()), push the
6465 * deferred frees from the previous txg. If not, leave them
6466 * alone so that we don't generate work on an otherwise idle
6469 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6470 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6471 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6472 ((dsl_scan_active(dp->dp_scan) ||
6473 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6474 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6475 VERIFY3U(bpobj_iterate(defer_bpo,
6476 spa_free_sync_cb, zio, tx), ==, 0);
6477 VERIFY0(zio_wait(zio));
6481 * Iterate to convergence.
6484 int pass = ++spa->spa_sync_pass;
6486 spa_sync_config_object(spa, tx);
6487 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6488 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6489 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6490 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6491 spa_errlog_sync(spa, txg);
6492 dsl_pool_sync(dp, txg);
6494 if (pass < zfs_sync_pass_deferred_free) {
6495 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6496 bplist_iterate(free_bpl, spa_free_sync_cb,
6498 VERIFY(zio_wait(zio) == 0);
6500 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6505 dsl_scan_sync(dp, tx);
6507 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6511 spa_sync_upgrades(spa, tx);
6513 } while (dmu_objset_is_dirty(mos, txg));
6516 * Rewrite the vdev configuration (which includes the uberblock)
6517 * to commit the transaction group.
6519 * If there are no dirty vdevs, we sync the uberblock to a few
6520 * random top-level vdevs that are known to be visible in the
6521 * config cache (see spa_vdev_add() for a complete description).
6522 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6526 * We hold SCL_STATE to prevent vdev open/close/etc.
6527 * while we're attempting to write the vdev labels.
6529 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6531 if (list_is_empty(&spa->spa_config_dirty_list)) {
6532 vdev_t *svd[SPA_DVAS_PER_BP];
6534 int children = rvd->vdev_children;
6535 int c0 = spa_get_random(children);
6537 for (int c = 0; c < children; c++) {
6538 vd = rvd->vdev_child[(c0 + c) % children];
6539 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6541 svd[svdcount++] = vd;
6542 if (svdcount == SPA_DVAS_PER_BP)
6545 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6547 error = vdev_config_sync(svd, svdcount, txg,
6550 error = vdev_config_sync(rvd->vdev_child,
6551 rvd->vdev_children, txg, B_FALSE);
6553 error = vdev_config_sync(rvd->vdev_child,
6554 rvd->vdev_children, txg, B_TRUE);
6558 spa->spa_last_synced_guid = rvd->vdev_guid;
6560 spa_config_exit(spa, SCL_STATE, FTAG);
6564 zio_suspend(spa, NULL);
6565 zio_resume_wait(spa);
6570 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6573 callout_drain(&spa->spa_deadman_cycid);
6578 * Clear the dirty config list.
6580 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6581 vdev_config_clean(vd);
6584 * Now that the new config has synced transactionally,
6585 * let it become visible to the config cache.
6587 if (spa->spa_config_syncing != NULL) {
6588 spa_config_set(spa, spa->spa_config_syncing);
6589 spa->spa_config_txg = txg;
6590 spa->spa_config_syncing = NULL;
6593 spa->spa_ubsync = spa->spa_uberblock;
6595 dsl_pool_sync_done(dp, txg);
6598 * Update usable space statistics.
6600 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6601 vdev_sync_done(vd, txg);
6603 spa_update_dspace(spa);
6606 * It had better be the case that we didn't dirty anything
6607 * since vdev_config_sync().
6609 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6610 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6611 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6613 spa->spa_sync_pass = 0;
6615 spa_config_exit(spa, SCL_CONFIG, FTAG);
6617 spa_handle_ignored_writes(spa);
6620 * If any async tasks have been requested, kick them off.
6622 spa_async_dispatch(spa);
6623 spa_async_dispatch_vd(spa);
6627 * Sync all pools. We don't want to hold the namespace lock across these
6628 * operations, so we take a reference on the spa_t and drop the lock during the
6632 spa_sync_allpools(void)
6635 mutex_enter(&spa_namespace_lock);
6636 while ((spa = spa_next(spa)) != NULL) {
6637 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6638 !spa_writeable(spa) || spa_suspended(spa))
6640 spa_open_ref(spa, FTAG);
6641 mutex_exit(&spa_namespace_lock);
6642 txg_wait_synced(spa_get_dsl(spa), 0);
6643 mutex_enter(&spa_namespace_lock);
6644 spa_close(spa, FTAG);
6646 mutex_exit(&spa_namespace_lock);
6650 * ==========================================================================
6651 * Miscellaneous routines
6652 * ==========================================================================
6656 * Remove all pools in the system.
6664 * Remove all cached state. All pools should be closed now,
6665 * so every spa in the AVL tree should be unreferenced.
6667 mutex_enter(&spa_namespace_lock);
6668 while ((spa = spa_next(NULL)) != NULL) {
6670 * Stop async tasks. The async thread may need to detach
6671 * a device that's been replaced, which requires grabbing
6672 * spa_namespace_lock, so we must drop it here.
6674 spa_open_ref(spa, FTAG);
6675 mutex_exit(&spa_namespace_lock);
6676 spa_async_suspend(spa);
6677 mutex_enter(&spa_namespace_lock);
6678 spa_close(spa, FTAG);
6680 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6682 spa_deactivate(spa);
6686 mutex_exit(&spa_namespace_lock);
6690 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6695 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6699 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6700 vd = spa->spa_l2cache.sav_vdevs[i];
6701 if (vd->vdev_guid == guid)
6705 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6706 vd = spa->spa_spares.sav_vdevs[i];
6707 if (vd->vdev_guid == guid)
6716 spa_upgrade(spa_t *spa, uint64_t version)
6718 ASSERT(spa_writeable(spa));
6720 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6723 * This should only be called for a non-faulted pool, and since a
6724 * future version would result in an unopenable pool, this shouldn't be
6727 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6728 ASSERT(version >= spa->spa_uberblock.ub_version);
6730 spa->spa_uberblock.ub_version = version;
6731 vdev_config_dirty(spa->spa_root_vdev);
6733 spa_config_exit(spa, SCL_ALL, FTAG);
6735 txg_wait_synced(spa_get_dsl(spa), 0);
6739 spa_has_spare(spa_t *spa, uint64_t guid)
6743 spa_aux_vdev_t *sav = &spa->spa_spares;
6745 for (i = 0; i < sav->sav_count; i++)
6746 if (sav->sav_vdevs[i]->vdev_guid == guid)
6749 for (i = 0; i < sav->sav_npending; i++) {
6750 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6751 &spareguid) == 0 && spareguid == guid)
6759 * Check if a pool has an active shared spare device.
6760 * Note: reference count of an active spare is 2, as a spare and as a replace
6763 spa_has_active_shared_spare(spa_t *spa)
6767 spa_aux_vdev_t *sav = &spa->spa_spares;
6769 for (i = 0; i < sav->sav_count; i++) {
6770 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6771 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6780 * Post a sysevent corresponding to the given event. The 'name' must be one of
6781 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6782 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6783 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6784 * or zdb as real changes.
6787 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6791 sysevent_attr_list_t *attr = NULL;
6792 sysevent_value_t value;
6795 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6798 value.value_type = SE_DATA_TYPE_STRING;
6799 value.value.sv_string = spa_name(spa);
6800 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6803 value.value_type = SE_DATA_TYPE_UINT64;
6804 value.value.sv_uint64 = spa_guid(spa);
6805 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6809 value.value_type = SE_DATA_TYPE_UINT64;
6810 value.value.sv_uint64 = vd->vdev_guid;
6811 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6815 if (vd->vdev_path) {
6816 value.value_type = SE_DATA_TYPE_STRING;
6817 value.value.sv_string = vd->vdev_path;
6818 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6819 &value, SE_SLEEP) != 0)
6824 if (sysevent_attach_attributes(ev, attr) != 0)
6828 (void) log_sysevent(ev, SE_SLEEP, &eid);
6832 sysevent_free_attr(attr);