4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright 2013 Saso Kiselkov. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Toomas Soome <tsoome@me.com>
34 * SPA: Storage Pool Allocator
36 * This file contains all the routines used when modifying on-disk SPA state.
37 * This includes opening, importing, destroying, exporting a pool, and syncing a
41 #include <sys/zfs_context.h>
42 #include <sys/fm/fs/zfs.h>
43 #include <sys/spa_impl.h>
45 #include <sys/zio_checksum.h>
47 #include <sys/dmu_tx.h>
51 #include <sys/vdev_impl.h>
52 #include <sys/metaslab.h>
53 #include <sys/metaslab_impl.h>
54 #include <sys/uberblock_impl.h>
57 #include <sys/dmu_traverse.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/unique.h>
60 #include <sys/dsl_pool.h>
61 #include <sys/dsl_dataset.h>
62 #include <sys/dsl_dir.h>
63 #include <sys/dsl_prop.h>
64 #include <sys/dsl_synctask.h>
65 #include <sys/fs/zfs.h>
67 #include <sys/callb.h>
68 #include <sys/spa_boot.h>
69 #include <sys/zfs_ioctl.h>
70 #include <sys/dsl_scan.h>
71 #include <sys/dmu_send.h>
72 #include <sys/dsl_destroy.h>
73 #include <sys/dsl_userhold.h>
74 #include <sys/zfeature.h>
76 #include <sys/trim_map.h>
79 #include <sys/callb.h>
80 #include <sys/cpupart.h>
85 #include "zfs_comutil.h"
87 /* Check hostid on import? */
88 static int check_hostid = 1;
91 * The interval, in seconds, at which failed configuration cache file writes
94 static int zfs_ccw_retry_interval = 300;
96 SYSCTL_DECL(_vfs_zfs);
97 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RWTUN, &check_hostid, 0,
98 "Check hostid on import?");
99 TUNABLE_INT("vfs.zfs.ccw_retry_interval", &zfs_ccw_retry_interval);
100 SYSCTL_INT(_vfs_zfs, OID_AUTO, ccw_retry_interval, CTLFLAG_RW,
101 &zfs_ccw_retry_interval, 0,
102 "Configuration cache file write, retry after failure, interval (seconds)");
104 typedef enum zti_modes {
105 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
106 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
107 ZTI_MODE_NULL, /* don't create a taskq */
111 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
112 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
113 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
115 #define ZTI_N(n) ZTI_P(n, 1)
116 #define ZTI_ONE ZTI_N(1)
118 typedef struct zio_taskq_info {
119 zti_modes_t zti_mode;
124 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
125 "issue", "issue_high", "intr", "intr_high"
129 * This table defines the taskq settings for each ZFS I/O type. When
130 * initializing a pool, we use this table to create an appropriately sized
131 * taskq. Some operations are low volume and therefore have a small, static
132 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
133 * macros. Other operations process a large amount of data; the ZTI_BATCH
134 * macro causes us to create a taskq oriented for throughput. Some operations
135 * are so high frequency and short-lived that the taskq itself can become a a
136 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
137 * additional degree of parallelism specified by the number of threads per-
138 * taskq and the number of taskqs; when dispatching an event in this case, the
139 * particular taskq is chosen at random.
141 * The different taskq priorities are to handle the different contexts (issue
142 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
143 * need to be handled with minimum delay.
145 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
146 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
147 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
148 { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */
149 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
150 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
151 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
152 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
155 static sysevent_t *spa_event_create(spa_t *spa, vdev_t *vd, const char *name);
156 static void spa_event_post(sysevent_t *ev);
157 static void spa_sync_version(void *arg, dmu_tx_t *tx);
158 static void spa_sync_props(void *arg, dmu_tx_t *tx);
159 static boolean_t spa_has_active_shared_spare(spa_t *spa);
160 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
161 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
163 static void spa_vdev_resilver_done(spa_t *spa);
165 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
167 id_t zio_taskq_psrset_bind = PS_NONE;
170 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
171 uint_t zio_taskq_basedc = 80; /* base duty cycle */
174 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
175 extern int zfs_sync_pass_deferred_free;
178 * This (illegal) pool name is used when temporarily importing a spa_t in order
179 * to get the vdev stats associated with the imported devices.
181 #define TRYIMPORT_NAME "$import"
184 * ==========================================================================
185 * SPA properties routines
186 * ==========================================================================
190 * Add a (source=src, propname=propval) list to an nvlist.
193 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
194 uint64_t intval, zprop_source_t src)
196 const char *propname = zpool_prop_to_name(prop);
199 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
200 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
203 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
205 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
207 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
208 nvlist_free(propval);
212 * Get property values from the spa configuration.
215 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
217 vdev_t *rvd = spa->spa_root_vdev;
218 dsl_pool_t *pool = spa->spa_dsl_pool;
219 uint64_t size, alloc, cap, version;
220 zprop_source_t src = ZPROP_SRC_NONE;
221 spa_config_dirent_t *dp;
222 metaslab_class_t *mc = spa_normal_class(spa);
224 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
227 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
228 size = metaslab_class_get_space(spa_normal_class(spa));
229 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
230 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
231 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
232 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
235 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
236 metaslab_class_fragmentation(mc), src);
237 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
238 metaslab_class_expandable_space(mc), src);
239 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
240 (spa_mode(spa) == FREAD), src);
242 cap = (size == 0) ? 0 : (alloc * 100 / size);
243 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
245 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
246 ddt_get_pool_dedup_ratio(spa), src);
248 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
249 rvd->vdev_state, src);
251 version = spa_version(spa);
252 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
253 src = ZPROP_SRC_DEFAULT;
255 src = ZPROP_SRC_LOCAL;
256 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
261 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
262 * when opening pools before this version freedir will be NULL.
264 if (pool->dp_free_dir != NULL) {
265 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
266 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
269 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
273 if (pool->dp_leak_dir != NULL) {
274 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
275 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
278 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
283 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
285 if (spa->spa_comment != NULL) {
286 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
290 if (spa->spa_root != NULL)
291 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
294 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
295 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
296 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
298 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
299 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
302 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
303 if (dp->scd_path == NULL) {
304 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
305 "none", 0, ZPROP_SRC_LOCAL);
306 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
307 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
308 dp->scd_path, 0, ZPROP_SRC_LOCAL);
314 * Get zpool property values.
317 spa_prop_get(spa_t *spa, nvlist_t **nvp)
319 objset_t *mos = spa->spa_meta_objset;
324 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
326 mutex_enter(&spa->spa_props_lock);
329 * Get properties from the spa config.
331 spa_prop_get_config(spa, nvp);
333 /* If no pool property object, no more prop to get. */
334 if (mos == NULL || spa->spa_pool_props_object == 0) {
335 mutex_exit(&spa->spa_props_lock);
340 * Get properties from the MOS pool property object.
342 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
343 (err = zap_cursor_retrieve(&zc, &za)) == 0;
344 zap_cursor_advance(&zc)) {
347 zprop_source_t src = ZPROP_SRC_DEFAULT;
350 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
353 switch (za.za_integer_length) {
355 /* integer property */
356 if (za.za_first_integer !=
357 zpool_prop_default_numeric(prop))
358 src = ZPROP_SRC_LOCAL;
360 if (prop == ZPOOL_PROP_BOOTFS) {
362 dsl_dataset_t *ds = NULL;
364 dp = spa_get_dsl(spa);
365 dsl_pool_config_enter(dp, FTAG);
366 if (err = dsl_dataset_hold_obj(dp,
367 za.za_first_integer, FTAG, &ds)) {
368 dsl_pool_config_exit(dp, FTAG);
372 strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
374 dsl_dataset_name(ds, strval);
375 dsl_dataset_rele(ds, FTAG);
376 dsl_pool_config_exit(dp, FTAG);
379 intval = za.za_first_integer;
382 spa_prop_add_list(*nvp, prop, strval, intval, src);
385 kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
390 /* string property */
391 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
392 err = zap_lookup(mos, spa->spa_pool_props_object,
393 za.za_name, 1, za.za_num_integers, strval);
395 kmem_free(strval, za.za_num_integers);
398 spa_prop_add_list(*nvp, prop, strval, 0, src);
399 kmem_free(strval, za.za_num_integers);
406 zap_cursor_fini(&zc);
407 mutex_exit(&spa->spa_props_lock);
409 if (err && err != ENOENT) {
419 * Validate the given pool properties nvlist and modify the list
420 * for the property values to be set.
423 spa_prop_validate(spa_t *spa, nvlist_t *props)
426 int error = 0, reset_bootfs = 0;
428 boolean_t has_feature = B_FALSE;
431 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
433 char *strval, *slash, *check, *fname;
434 const char *propname = nvpair_name(elem);
435 zpool_prop_t prop = zpool_name_to_prop(propname);
439 if (!zpool_prop_feature(propname)) {
440 error = SET_ERROR(EINVAL);
445 * Sanitize the input.
447 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
448 error = SET_ERROR(EINVAL);
452 if (nvpair_value_uint64(elem, &intval) != 0) {
453 error = SET_ERROR(EINVAL);
458 error = SET_ERROR(EINVAL);
462 fname = strchr(propname, '@') + 1;
463 if (zfeature_lookup_name(fname, NULL) != 0) {
464 error = SET_ERROR(EINVAL);
468 has_feature = B_TRUE;
471 case ZPOOL_PROP_VERSION:
472 error = nvpair_value_uint64(elem, &intval);
474 (intval < spa_version(spa) ||
475 intval > SPA_VERSION_BEFORE_FEATURES ||
477 error = SET_ERROR(EINVAL);
480 case ZPOOL_PROP_DELEGATION:
481 case ZPOOL_PROP_AUTOREPLACE:
482 case ZPOOL_PROP_LISTSNAPS:
483 case ZPOOL_PROP_AUTOEXPAND:
484 error = nvpair_value_uint64(elem, &intval);
485 if (!error && intval > 1)
486 error = SET_ERROR(EINVAL);
489 case ZPOOL_PROP_BOOTFS:
491 * If the pool version is less than SPA_VERSION_BOOTFS,
492 * or the pool is still being created (version == 0),
493 * the bootfs property cannot be set.
495 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
496 error = SET_ERROR(ENOTSUP);
501 * Make sure the vdev config is bootable
503 if (!vdev_is_bootable(spa->spa_root_vdev)) {
504 error = SET_ERROR(ENOTSUP);
510 error = nvpair_value_string(elem, &strval);
516 if (strval == NULL || strval[0] == '\0') {
517 objnum = zpool_prop_default_numeric(
522 if (error = dmu_objset_hold(strval, FTAG, &os))
526 * Must be ZPL, and its property settings
527 * must be supported by GRUB (compression
528 * is not gzip, and large blocks are not used).
531 if (dmu_objset_type(os) != DMU_OST_ZFS) {
532 error = SET_ERROR(ENOTSUP);
534 dsl_prop_get_int_ds(dmu_objset_ds(os),
535 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
537 !BOOTFS_COMPRESS_VALID(propval)) {
538 error = SET_ERROR(ENOTSUP);
540 objnum = dmu_objset_id(os);
542 dmu_objset_rele(os, FTAG);
546 case ZPOOL_PROP_FAILUREMODE:
547 error = nvpair_value_uint64(elem, &intval);
548 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
549 intval > ZIO_FAILURE_MODE_PANIC))
550 error = SET_ERROR(EINVAL);
553 * This is a special case which only occurs when
554 * the pool has completely failed. This allows
555 * the user to change the in-core failmode property
556 * without syncing it out to disk (I/Os might
557 * currently be blocked). We do this by returning
558 * EIO to the caller (spa_prop_set) to trick it
559 * into thinking we encountered a property validation
562 if (!error && spa_suspended(spa)) {
563 spa->spa_failmode = intval;
564 error = SET_ERROR(EIO);
568 case ZPOOL_PROP_CACHEFILE:
569 if ((error = nvpair_value_string(elem, &strval)) != 0)
572 if (strval[0] == '\0')
575 if (strcmp(strval, "none") == 0)
578 if (strval[0] != '/') {
579 error = SET_ERROR(EINVAL);
583 slash = strrchr(strval, '/');
584 ASSERT(slash != NULL);
586 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
587 strcmp(slash, "/..") == 0)
588 error = SET_ERROR(EINVAL);
591 case ZPOOL_PROP_COMMENT:
592 if ((error = nvpair_value_string(elem, &strval)) != 0)
594 for (check = strval; *check != '\0'; check++) {
596 * The kernel doesn't have an easy isprint()
597 * check. For this kernel check, we merely
598 * check ASCII apart from DEL. Fix this if
599 * there is an easy-to-use kernel isprint().
601 if (*check >= 0x7f) {
602 error = SET_ERROR(EINVAL);
606 if (strlen(strval) > ZPROP_MAX_COMMENT)
610 case ZPOOL_PROP_DEDUPDITTO:
611 if (spa_version(spa) < SPA_VERSION_DEDUP)
612 error = SET_ERROR(ENOTSUP);
614 error = nvpair_value_uint64(elem, &intval);
616 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
617 error = SET_ERROR(EINVAL);
625 if (!error && reset_bootfs) {
626 error = nvlist_remove(props,
627 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
630 error = nvlist_add_uint64(props,
631 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
639 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
642 spa_config_dirent_t *dp;
644 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
648 dp = kmem_alloc(sizeof (spa_config_dirent_t),
651 if (cachefile[0] == '\0')
652 dp->scd_path = spa_strdup(spa_config_path);
653 else if (strcmp(cachefile, "none") == 0)
656 dp->scd_path = spa_strdup(cachefile);
658 list_insert_head(&spa->spa_config_list, dp);
660 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
664 spa_prop_set(spa_t *spa, nvlist_t *nvp)
667 nvpair_t *elem = NULL;
668 boolean_t need_sync = B_FALSE;
670 if ((error = spa_prop_validate(spa, nvp)) != 0)
673 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
674 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
676 if (prop == ZPOOL_PROP_CACHEFILE ||
677 prop == ZPOOL_PROP_ALTROOT ||
678 prop == ZPOOL_PROP_READONLY)
681 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
684 if (prop == ZPOOL_PROP_VERSION) {
685 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
687 ASSERT(zpool_prop_feature(nvpair_name(elem)));
688 ver = SPA_VERSION_FEATURES;
692 /* Save time if the version is already set. */
693 if (ver == spa_version(spa))
697 * In addition to the pool directory object, we might
698 * create the pool properties object, the features for
699 * read object, the features for write object, or the
700 * feature descriptions object.
702 error = dsl_sync_task(spa->spa_name, NULL,
703 spa_sync_version, &ver,
704 6, ZFS_SPACE_CHECK_RESERVED);
715 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
716 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
723 * If the bootfs property value is dsobj, clear it.
726 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
728 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
729 VERIFY(zap_remove(spa->spa_meta_objset,
730 spa->spa_pool_props_object,
731 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
738 spa_change_guid_check(void *arg, dmu_tx_t *tx)
740 uint64_t *newguid = arg;
741 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
742 vdev_t *rvd = spa->spa_root_vdev;
745 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
746 vdev_state = rvd->vdev_state;
747 spa_config_exit(spa, SCL_STATE, FTAG);
749 if (vdev_state != VDEV_STATE_HEALTHY)
750 return (SET_ERROR(ENXIO));
752 ASSERT3U(spa_guid(spa), !=, *newguid);
758 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
760 uint64_t *newguid = arg;
761 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
763 vdev_t *rvd = spa->spa_root_vdev;
765 oldguid = spa_guid(spa);
767 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
768 rvd->vdev_guid = *newguid;
769 rvd->vdev_guid_sum += (*newguid - oldguid);
770 vdev_config_dirty(rvd);
771 spa_config_exit(spa, SCL_STATE, FTAG);
773 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
778 * Change the GUID for the pool. This is done so that we can later
779 * re-import a pool built from a clone of our own vdevs. We will modify
780 * the root vdev's guid, our own pool guid, and then mark all of our
781 * vdevs dirty. Note that we must make sure that all our vdevs are
782 * online when we do this, or else any vdevs that weren't present
783 * would be orphaned from our pool. We are also going to issue a
784 * sysevent to update any watchers.
787 spa_change_guid(spa_t *spa)
792 mutex_enter(&spa->spa_vdev_top_lock);
793 mutex_enter(&spa_namespace_lock);
794 guid = spa_generate_guid(NULL);
796 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
797 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
800 spa_config_sync(spa, B_FALSE, B_TRUE);
801 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
804 mutex_exit(&spa_namespace_lock);
805 mutex_exit(&spa->spa_vdev_top_lock);
811 * ==========================================================================
812 * SPA state manipulation (open/create/destroy/import/export)
813 * ==========================================================================
817 spa_error_entry_compare(const void *a, const void *b)
819 spa_error_entry_t *sa = (spa_error_entry_t *)a;
820 spa_error_entry_t *sb = (spa_error_entry_t *)b;
823 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
824 sizeof (zbookmark_phys_t));
835 * Utility function which retrieves copies of the current logs and
836 * re-initializes them in the process.
839 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
841 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
843 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
844 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
846 avl_create(&spa->spa_errlist_scrub,
847 spa_error_entry_compare, sizeof (spa_error_entry_t),
848 offsetof(spa_error_entry_t, se_avl));
849 avl_create(&spa->spa_errlist_last,
850 spa_error_entry_compare, sizeof (spa_error_entry_t),
851 offsetof(spa_error_entry_t, se_avl));
855 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
857 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
858 enum zti_modes mode = ztip->zti_mode;
859 uint_t value = ztip->zti_value;
860 uint_t count = ztip->zti_count;
861 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
864 boolean_t batch = B_FALSE;
866 if (mode == ZTI_MODE_NULL) {
868 tqs->stqs_taskq = NULL;
872 ASSERT3U(count, >, 0);
874 tqs->stqs_count = count;
875 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
879 ASSERT3U(value, >=, 1);
880 value = MAX(value, 1);
885 flags |= TASKQ_THREADS_CPU_PCT;
886 value = zio_taskq_batch_pct;
890 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
892 zio_type_name[t], zio_taskq_types[q], mode, value);
896 for (uint_t i = 0; i < count; i++) {
900 (void) snprintf(name, sizeof (name), "%s_%s_%u",
901 zio_type_name[t], zio_taskq_types[q], i);
903 (void) snprintf(name, sizeof (name), "%s_%s",
904 zio_type_name[t], zio_taskq_types[q]);
908 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
910 flags |= TASKQ_DC_BATCH;
912 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
913 spa->spa_proc, zio_taskq_basedc, flags);
916 pri_t pri = maxclsyspri;
918 * The write issue taskq can be extremely CPU
919 * intensive. Run it at slightly lower priority
920 * than the other taskqs.
922 * - numerically higher priorities are lower priorities;
923 * - if priorities divided by four (RQ_PPQ) are equal
924 * then a difference between them is insignificant.
926 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
933 tq = taskq_create_proc(name, value, pri, 50,
934 INT_MAX, spa->spa_proc, flags);
939 tqs->stqs_taskq[i] = tq;
944 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
946 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
948 if (tqs->stqs_taskq == NULL) {
949 ASSERT0(tqs->stqs_count);
953 for (uint_t i = 0; i < tqs->stqs_count; i++) {
954 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
955 taskq_destroy(tqs->stqs_taskq[i]);
958 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
959 tqs->stqs_taskq = NULL;
963 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
964 * Note that a type may have multiple discrete taskqs to avoid lock contention
965 * on the taskq itself. In that case we choose which taskq at random by using
966 * the low bits of gethrtime().
969 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
970 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
972 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
975 ASSERT3P(tqs->stqs_taskq, !=, NULL);
976 ASSERT3U(tqs->stqs_count, !=, 0);
978 if (tqs->stqs_count == 1) {
979 tq = tqs->stqs_taskq[0];
982 tq = tqs->stqs_taskq[cpu_ticks() % tqs->stqs_count];
984 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
988 taskq_dispatch_ent(tq, func, arg, flags, ent);
992 spa_create_zio_taskqs(spa_t *spa)
994 for (int t = 0; t < ZIO_TYPES; t++) {
995 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
996 spa_taskqs_init(spa, t, q);
1004 spa_thread(void *arg)
1006 callb_cpr_t cprinfo;
1009 user_t *pu = PTOU(curproc);
1011 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1014 ASSERT(curproc != &p0);
1015 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1016 "zpool-%s", spa->spa_name);
1017 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1020 /* bind this thread to the requested psrset */
1021 if (zio_taskq_psrset_bind != PS_NONE) {
1023 mutex_enter(&cpu_lock);
1024 mutex_enter(&pidlock);
1025 mutex_enter(&curproc->p_lock);
1027 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1028 0, NULL, NULL) == 0) {
1029 curthread->t_bind_pset = zio_taskq_psrset_bind;
1032 "Couldn't bind process for zfs pool \"%s\" to "
1033 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1036 mutex_exit(&curproc->p_lock);
1037 mutex_exit(&pidlock);
1038 mutex_exit(&cpu_lock);
1044 if (zio_taskq_sysdc) {
1045 sysdc_thread_enter(curthread, 100, 0);
1049 spa->spa_proc = curproc;
1050 spa->spa_did = curthread->t_did;
1052 spa_create_zio_taskqs(spa);
1054 mutex_enter(&spa->spa_proc_lock);
1055 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1057 spa->spa_proc_state = SPA_PROC_ACTIVE;
1058 cv_broadcast(&spa->spa_proc_cv);
1060 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1061 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1062 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1063 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1065 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1066 spa->spa_proc_state = SPA_PROC_GONE;
1067 spa->spa_proc = &p0;
1068 cv_broadcast(&spa->spa_proc_cv);
1069 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1071 mutex_enter(&curproc->p_lock);
1074 #endif /* SPA_PROCESS */
1078 * Activate an uninitialized pool.
1081 spa_activate(spa_t *spa, int mode)
1083 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1085 spa->spa_state = POOL_STATE_ACTIVE;
1086 spa->spa_mode = mode;
1088 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1089 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1091 /* Try to create a covering process */
1092 mutex_enter(&spa->spa_proc_lock);
1093 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1094 ASSERT(spa->spa_proc == &p0);
1098 /* Only create a process if we're going to be around a while. */
1099 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1100 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1102 spa->spa_proc_state = SPA_PROC_CREATED;
1103 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1104 cv_wait(&spa->spa_proc_cv,
1105 &spa->spa_proc_lock);
1107 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1108 ASSERT(spa->spa_proc != &p0);
1109 ASSERT(spa->spa_did != 0);
1113 "Couldn't create process for zfs pool \"%s\"\n",
1118 #endif /* SPA_PROCESS */
1119 mutex_exit(&spa->spa_proc_lock);
1121 /* If we didn't create a process, we need to create our taskqs. */
1122 ASSERT(spa->spa_proc == &p0);
1123 if (spa->spa_proc == &p0) {
1124 spa_create_zio_taskqs(spa);
1128 * Start TRIM thread.
1130 trim_thread_create(spa);
1132 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1133 offsetof(vdev_t, vdev_config_dirty_node));
1134 list_create(&spa->spa_evicting_os_list, sizeof (objset_t),
1135 offsetof(objset_t, os_evicting_node));
1136 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1137 offsetof(vdev_t, vdev_state_dirty_node));
1139 txg_list_create(&spa->spa_vdev_txg_list,
1140 offsetof(struct vdev, vdev_txg_node));
1142 avl_create(&spa->spa_errlist_scrub,
1143 spa_error_entry_compare, sizeof (spa_error_entry_t),
1144 offsetof(spa_error_entry_t, se_avl));
1145 avl_create(&spa->spa_errlist_last,
1146 spa_error_entry_compare, sizeof (spa_error_entry_t),
1147 offsetof(spa_error_entry_t, se_avl));
1151 * Opposite of spa_activate().
1154 spa_deactivate(spa_t *spa)
1156 ASSERT(spa->spa_sync_on == B_FALSE);
1157 ASSERT(spa->spa_dsl_pool == NULL);
1158 ASSERT(spa->spa_root_vdev == NULL);
1159 ASSERT(spa->spa_async_zio_root == NULL);
1160 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1163 * Stop TRIM thread in case spa_unload() wasn't called directly
1164 * before spa_deactivate().
1166 trim_thread_destroy(spa);
1168 spa_evicting_os_wait(spa);
1170 txg_list_destroy(&spa->spa_vdev_txg_list);
1172 list_destroy(&spa->spa_config_dirty_list);
1173 list_destroy(&spa->spa_evicting_os_list);
1174 list_destroy(&spa->spa_state_dirty_list);
1176 for (int t = 0; t < ZIO_TYPES; t++) {
1177 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1178 spa_taskqs_fini(spa, t, q);
1182 metaslab_class_destroy(spa->spa_normal_class);
1183 spa->spa_normal_class = NULL;
1185 metaslab_class_destroy(spa->spa_log_class);
1186 spa->spa_log_class = NULL;
1189 * If this was part of an import or the open otherwise failed, we may
1190 * still have errors left in the queues. Empty them just in case.
1192 spa_errlog_drain(spa);
1194 avl_destroy(&spa->spa_errlist_scrub);
1195 avl_destroy(&spa->spa_errlist_last);
1197 spa->spa_state = POOL_STATE_UNINITIALIZED;
1199 mutex_enter(&spa->spa_proc_lock);
1200 if (spa->spa_proc_state != SPA_PROC_NONE) {
1201 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1202 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1203 cv_broadcast(&spa->spa_proc_cv);
1204 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1205 ASSERT(spa->spa_proc != &p0);
1206 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1208 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1209 spa->spa_proc_state = SPA_PROC_NONE;
1211 ASSERT(spa->spa_proc == &p0);
1212 mutex_exit(&spa->spa_proc_lock);
1216 * We want to make sure spa_thread() has actually exited the ZFS
1217 * module, so that the module can't be unloaded out from underneath
1220 if (spa->spa_did != 0) {
1221 thread_join(spa->spa_did);
1224 #endif /* SPA_PROCESS */
1228 * Verify a pool configuration, and construct the vdev tree appropriately. This
1229 * will create all the necessary vdevs in the appropriate layout, with each vdev
1230 * in the CLOSED state. This will prep the pool before open/creation/import.
1231 * All vdev validation is done by the vdev_alloc() routine.
1234 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1235 uint_t id, int atype)
1241 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1244 if ((*vdp)->vdev_ops->vdev_op_leaf)
1247 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1250 if (error == ENOENT)
1256 return (SET_ERROR(EINVAL));
1259 for (int c = 0; c < children; c++) {
1261 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1269 ASSERT(*vdp != NULL);
1275 * Opposite of spa_load().
1278 spa_unload(spa_t *spa)
1282 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1287 trim_thread_destroy(spa);
1292 spa_async_suspend(spa);
1297 if (spa->spa_sync_on) {
1298 txg_sync_stop(spa->spa_dsl_pool);
1299 spa->spa_sync_on = B_FALSE;
1303 * Wait for any outstanding async I/O to complete.
1305 if (spa->spa_async_zio_root != NULL) {
1306 for (int i = 0; i < max_ncpus; i++)
1307 (void) zio_wait(spa->spa_async_zio_root[i]);
1308 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
1309 spa->spa_async_zio_root = NULL;
1312 bpobj_close(&spa->spa_deferred_bpobj);
1314 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1319 if (spa->spa_root_vdev)
1320 vdev_free(spa->spa_root_vdev);
1321 ASSERT(spa->spa_root_vdev == NULL);
1324 * Close the dsl pool.
1326 if (spa->spa_dsl_pool) {
1327 dsl_pool_close(spa->spa_dsl_pool);
1328 spa->spa_dsl_pool = NULL;
1329 spa->spa_meta_objset = NULL;
1335 * Drop and purge level 2 cache
1337 spa_l2cache_drop(spa);
1339 for (i = 0; i < spa->spa_spares.sav_count; i++)
1340 vdev_free(spa->spa_spares.sav_vdevs[i]);
1341 if (spa->spa_spares.sav_vdevs) {
1342 kmem_free(spa->spa_spares.sav_vdevs,
1343 spa->spa_spares.sav_count * sizeof (void *));
1344 spa->spa_spares.sav_vdevs = NULL;
1346 if (spa->spa_spares.sav_config) {
1347 nvlist_free(spa->spa_spares.sav_config);
1348 spa->spa_spares.sav_config = NULL;
1350 spa->spa_spares.sav_count = 0;
1352 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1353 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1354 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1356 if (spa->spa_l2cache.sav_vdevs) {
1357 kmem_free(spa->spa_l2cache.sav_vdevs,
1358 spa->spa_l2cache.sav_count * sizeof (void *));
1359 spa->spa_l2cache.sav_vdevs = NULL;
1361 if (spa->spa_l2cache.sav_config) {
1362 nvlist_free(spa->spa_l2cache.sav_config);
1363 spa->spa_l2cache.sav_config = NULL;
1365 spa->spa_l2cache.sav_count = 0;
1367 spa->spa_async_suspended = 0;
1369 if (spa->spa_comment != NULL) {
1370 spa_strfree(spa->spa_comment);
1371 spa->spa_comment = NULL;
1374 spa_config_exit(spa, SCL_ALL, FTAG);
1378 * Load (or re-load) the current list of vdevs describing the active spares for
1379 * this pool. When this is called, we have some form of basic information in
1380 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1381 * then re-generate a more complete list including status information.
1384 spa_load_spares(spa_t *spa)
1391 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1394 * First, close and free any existing spare vdevs.
1396 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1397 vd = spa->spa_spares.sav_vdevs[i];
1399 /* Undo the call to spa_activate() below */
1400 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1401 B_FALSE)) != NULL && tvd->vdev_isspare)
1402 spa_spare_remove(tvd);
1407 if (spa->spa_spares.sav_vdevs)
1408 kmem_free(spa->spa_spares.sav_vdevs,
1409 spa->spa_spares.sav_count * sizeof (void *));
1411 if (spa->spa_spares.sav_config == NULL)
1414 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1415 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1417 spa->spa_spares.sav_count = (int)nspares;
1418 spa->spa_spares.sav_vdevs = NULL;
1424 * Construct the array of vdevs, opening them to get status in the
1425 * process. For each spare, there is potentially two different vdev_t
1426 * structures associated with it: one in the list of spares (used only
1427 * for basic validation purposes) and one in the active vdev
1428 * configuration (if it's spared in). During this phase we open and
1429 * validate each vdev on the spare list. If the vdev also exists in the
1430 * active configuration, then we also mark this vdev as an active spare.
1432 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1434 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1435 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1436 VDEV_ALLOC_SPARE) == 0);
1439 spa->spa_spares.sav_vdevs[i] = vd;
1441 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1442 B_FALSE)) != NULL) {
1443 if (!tvd->vdev_isspare)
1447 * We only mark the spare active if we were successfully
1448 * able to load the vdev. Otherwise, importing a pool
1449 * with a bad active spare would result in strange
1450 * behavior, because multiple pool would think the spare
1451 * is actively in use.
1453 * There is a vulnerability here to an equally bizarre
1454 * circumstance, where a dead active spare is later
1455 * brought back to life (onlined or otherwise). Given
1456 * the rarity of this scenario, and the extra complexity
1457 * it adds, we ignore the possibility.
1459 if (!vdev_is_dead(tvd))
1460 spa_spare_activate(tvd);
1464 vd->vdev_aux = &spa->spa_spares;
1466 if (vdev_open(vd) != 0)
1469 if (vdev_validate_aux(vd) == 0)
1474 * Recompute the stashed list of spares, with status information
1477 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1478 DATA_TYPE_NVLIST_ARRAY) == 0);
1480 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1482 for (i = 0; i < spa->spa_spares.sav_count; i++)
1483 spares[i] = vdev_config_generate(spa,
1484 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1485 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1486 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1487 for (i = 0; i < spa->spa_spares.sav_count; i++)
1488 nvlist_free(spares[i]);
1489 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1493 * Load (or re-load) the current list of vdevs describing the active l2cache for
1494 * this pool. When this is called, we have some form of basic information in
1495 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1496 * then re-generate a more complete list including status information.
1497 * Devices which are already active have their details maintained, and are
1501 spa_load_l2cache(spa_t *spa)
1505 int i, j, oldnvdevs;
1507 vdev_t *vd, **oldvdevs, **newvdevs;
1508 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1510 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1512 if (sav->sav_config != NULL) {
1513 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1514 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1515 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1521 oldvdevs = sav->sav_vdevs;
1522 oldnvdevs = sav->sav_count;
1523 sav->sav_vdevs = NULL;
1527 * Process new nvlist of vdevs.
1529 for (i = 0; i < nl2cache; i++) {
1530 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1534 for (j = 0; j < oldnvdevs; j++) {
1536 if (vd != NULL && guid == vd->vdev_guid) {
1538 * Retain previous vdev for add/remove ops.
1546 if (newvdevs[i] == NULL) {
1550 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1551 VDEV_ALLOC_L2CACHE) == 0);
1556 * Commit this vdev as an l2cache device,
1557 * even if it fails to open.
1559 spa_l2cache_add(vd);
1564 spa_l2cache_activate(vd);
1566 if (vdev_open(vd) != 0)
1569 (void) vdev_validate_aux(vd);
1571 if (!vdev_is_dead(vd))
1572 l2arc_add_vdev(spa, vd);
1577 * Purge vdevs that were dropped
1579 for (i = 0; i < oldnvdevs; i++) {
1584 ASSERT(vd->vdev_isl2cache);
1586 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1587 pool != 0ULL && l2arc_vdev_present(vd))
1588 l2arc_remove_vdev(vd);
1589 vdev_clear_stats(vd);
1595 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1597 if (sav->sav_config == NULL)
1600 sav->sav_vdevs = newvdevs;
1601 sav->sav_count = (int)nl2cache;
1604 * Recompute the stashed list of l2cache devices, with status
1605 * information this time.
1607 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1608 DATA_TYPE_NVLIST_ARRAY) == 0);
1610 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1611 for (i = 0; i < sav->sav_count; i++)
1612 l2cache[i] = vdev_config_generate(spa,
1613 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1614 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1615 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1617 for (i = 0; i < sav->sav_count; i++)
1618 nvlist_free(l2cache[i]);
1620 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1624 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1627 char *packed = NULL;
1632 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1636 nvsize = *(uint64_t *)db->db_data;
1637 dmu_buf_rele(db, FTAG);
1639 packed = kmem_alloc(nvsize, KM_SLEEP);
1640 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1643 error = nvlist_unpack(packed, nvsize, value, 0);
1644 kmem_free(packed, nvsize);
1650 * Checks to see if the given vdev could not be opened, in which case we post a
1651 * sysevent to notify the autoreplace code that the device has been removed.
1654 spa_check_removed(vdev_t *vd)
1656 for (int c = 0; c < vd->vdev_children; c++)
1657 spa_check_removed(vd->vdev_child[c]);
1659 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1661 zfs_post_autoreplace(vd->vdev_spa, vd);
1662 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1667 spa_config_valid_zaps(vdev_t *vd, vdev_t *mvd)
1669 ASSERT3U(vd->vdev_children, ==, mvd->vdev_children);
1671 vd->vdev_top_zap = mvd->vdev_top_zap;
1672 vd->vdev_leaf_zap = mvd->vdev_leaf_zap;
1674 for (uint64_t i = 0; i < vd->vdev_children; i++) {
1675 spa_config_valid_zaps(vd->vdev_child[i], mvd->vdev_child[i]);
1680 * Validate the current config against the MOS config
1683 spa_config_valid(spa_t *spa, nvlist_t *config)
1685 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1688 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1690 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1691 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1693 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1696 * If we're doing a normal import, then build up any additional
1697 * diagnostic information about missing devices in this config.
1698 * We'll pass this up to the user for further processing.
1700 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1701 nvlist_t **child, *nv;
1704 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1706 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1708 for (int c = 0; c < rvd->vdev_children; c++) {
1709 vdev_t *tvd = rvd->vdev_child[c];
1710 vdev_t *mtvd = mrvd->vdev_child[c];
1712 if (tvd->vdev_ops == &vdev_missing_ops &&
1713 mtvd->vdev_ops != &vdev_missing_ops &&
1715 child[idx++] = vdev_config_generate(spa, mtvd,
1720 VERIFY(nvlist_add_nvlist_array(nv,
1721 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1722 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1723 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1725 for (int i = 0; i < idx; i++)
1726 nvlist_free(child[i]);
1729 kmem_free(child, rvd->vdev_children * sizeof (char **));
1733 * Compare the root vdev tree with the information we have
1734 * from the MOS config (mrvd). Check each top-level vdev
1735 * with the corresponding MOS config top-level (mtvd).
1737 for (int c = 0; c < rvd->vdev_children; c++) {
1738 vdev_t *tvd = rvd->vdev_child[c];
1739 vdev_t *mtvd = mrvd->vdev_child[c];
1742 * Resolve any "missing" vdevs in the current configuration.
1743 * If we find that the MOS config has more accurate information
1744 * about the top-level vdev then use that vdev instead.
1746 if (tvd->vdev_ops == &vdev_missing_ops &&
1747 mtvd->vdev_ops != &vdev_missing_ops) {
1749 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1753 * Device specific actions.
1755 if (mtvd->vdev_islog) {
1756 spa_set_log_state(spa, SPA_LOG_CLEAR);
1759 * XXX - once we have 'readonly' pool
1760 * support we should be able to handle
1761 * missing data devices by transitioning
1762 * the pool to readonly.
1768 * Swap the missing vdev with the data we were
1769 * able to obtain from the MOS config.
1771 vdev_remove_child(rvd, tvd);
1772 vdev_remove_child(mrvd, mtvd);
1774 vdev_add_child(rvd, mtvd);
1775 vdev_add_child(mrvd, tvd);
1777 spa_config_exit(spa, SCL_ALL, FTAG);
1779 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1783 if (mtvd->vdev_islog) {
1785 * Load the slog device's state from the MOS
1786 * config since it's possible that the label
1787 * does not contain the most up-to-date
1790 vdev_load_log_state(tvd, mtvd);
1795 * Per-vdev ZAP info is stored exclusively in the MOS.
1797 spa_config_valid_zaps(tvd, mtvd);
1802 spa_config_exit(spa, SCL_ALL, FTAG);
1805 * Ensure we were able to validate the config.
1807 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1811 * Check for missing log devices
1814 spa_check_logs(spa_t *spa)
1816 boolean_t rv = B_FALSE;
1817 dsl_pool_t *dp = spa_get_dsl(spa);
1819 switch (spa->spa_log_state) {
1820 case SPA_LOG_MISSING:
1821 /* need to recheck in case slog has been restored */
1822 case SPA_LOG_UNKNOWN:
1823 rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1824 zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
1826 spa_set_log_state(spa, SPA_LOG_MISSING);
1833 spa_passivate_log(spa_t *spa)
1835 vdev_t *rvd = spa->spa_root_vdev;
1836 boolean_t slog_found = B_FALSE;
1838 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1840 if (!spa_has_slogs(spa))
1843 for (int c = 0; c < rvd->vdev_children; c++) {
1844 vdev_t *tvd = rvd->vdev_child[c];
1845 metaslab_group_t *mg = tvd->vdev_mg;
1847 if (tvd->vdev_islog) {
1848 metaslab_group_passivate(mg);
1849 slog_found = B_TRUE;
1853 return (slog_found);
1857 spa_activate_log(spa_t *spa)
1859 vdev_t *rvd = spa->spa_root_vdev;
1861 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1863 for (int c = 0; c < rvd->vdev_children; c++) {
1864 vdev_t *tvd = rvd->vdev_child[c];
1865 metaslab_group_t *mg = tvd->vdev_mg;
1867 if (tvd->vdev_islog)
1868 metaslab_group_activate(mg);
1873 spa_offline_log(spa_t *spa)
1877 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1878 NULL, DS_FIND_CHILDREN);
1881 * We successfully offlined the log device, sync out the
1882 * current txg so that the "stubby" block can be removed
1885 txg_wait_synced(spa->spa_dsl_pool, 0);
1891 spa_aux_check_removed(spa_aux_vdev_t *sav)
1895 for (i = 0; i < sav->sav_count; i++)
1896 spa_check_removed(sav->sav_vdevs[i]);
1900 spa_claim_notify(zio_t *zio)
1902 spa_t *spa = zio->io_spa;
1907 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1908 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1909 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1910 mutex_exit(&spa->spa_props_lock);
1913 typedef struct spa_load_error {
1914 uint64_t sle_meta_count;
1915 uint64_t sle_data_count;
1919 spa_load_verify_done(zio_t *zio)
1921 blkptr_t *bp = zio->io_bp;
1922 spa_load_error_t *sle = zio->io_private;
1923 dmu_object_type_t type = BP_GET_TYPE(bp);
1924 int error = zio->io_error;
1925 spa_t *spa = zio->io_spa;
1928 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1929 type != DMU_OT_INTENT_LOG)
1930 atomic_inc_64(&sle->sle_meta_count);
1932 atomic_inc_64(&sle->sle_data_count);
1934 zio_data_buf_free(zio->io_data, zio->io_size);
1936 mutex_enter(&spa->spa_scrub_lock);
1937 spa->spa_scrub_inflight--;
1938 cv_broadcast(&spa->spa_scrub_io_cv);
1939 mutex_exit(&spa->spa_scrub_lock);
1943 * Maximum number of concurrent scrub i/os to create while verifying
1944 * a pool while importing it.
1946 int spa_load_verify_maxinflight = 10000;
1947 boolean_t spa_load_verify_metadata = B_TRUE;
1948 boolean_t spa_load_verify_data = B_TRUE;
1950 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1951 &spa_load_verify_maxinflight, 0,
1952 "Maximum number of concurrent scrub I/Os to create while verifying a "
1953 "pool while importing it");
1955 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1956 &spa_load_verify_metadata, 0,
1957 "Check metadata on import?");
1959 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1960 &spa_load_verify_data, 0,
1961 "Check user data on import?");
1965 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1966 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1968 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1971 * Note: normally this routine will not be called if
1972 * spa_load_verify_metadata is not set. However, it may be useful
1973 * to manually set the flag after the traversal has begun.
1975 if (!spa_load_verify_metadata)
1977 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1981 size_t size = BP_GET_PSIZE(bp);
1982 void *data = zio_data_buf_alloc(size);
1984 mutex_enter(&spa->spa_scrub_lock);
1985 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1986 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1987 spa->spa_scrub_inflight++;
1988 mutex_exit(&spa->spa_scrub_lock);
1990 zio_nowait(zio_read(rio, spa, bp, data, size,
1991 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1992 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1993 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1999 verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
2001 if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN)
2002 return (SET_ERROR(ENAMETOOLONG));
2008 spa_load_verify(spa_t *spa)
2011 spa_load_error_t sle = { 0 };
2012 zpool_rewind_policy_t policy;
2013 boolean_t verify_ok = B_FALSE;
2016 zpool_get_rewind_policy(spa->spa_config, &policy);
2018 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
2021 dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
2022 error = dmu_objset_find_dp(spa->spa_dsl_pool,
2023 spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
2025 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
2029 rio = zio_root(spa, NULL, &sle,
2030 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
2032 if (spa_load_verify_metadata) {
2033 error = traverse_pool(spa, spa->spa_verify_min_txg,
2034 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2035 spa_load_verify_cb, rio);
2038 (void) zio_wait(rio);
2040 spa->spa_load_meta_errors = sle.sle_meta_count;
2041 spa->spa_load_data_errors = sle.sle_data_count;
2043 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
2044 sle.sle_data_count <= policy.zrp_maxdata) {
2048 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2049 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2051 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2052 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2053 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2054 VERIFY(nvlist_add_int64(spa->spa_load_info,
2055 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2056 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2057 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2059 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2063 if (error != ENXIO && error != EIO)
2064 error = SET_ERROR(EIO);
2068 return (verify_ok ? 0 : EIO);
2072 * Find a value in the pool props object.
2075 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2077 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2078 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2082 * Find a value in the pool directory object.
2085 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2087 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2088 name, sizeof (uint64_t), 1, val));
2092 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2094 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2099 * Fix up config after a partly-completed split. This is done with the
2100 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2101 * pool have that entry in their config, but only the splitting one contains
2102 * a list of all the guids of the vdevs that are being split off.
2104 * This function determines what to do with that list: either rejoin
2105 * all the disks to the pool, or complete the splitting process. To attempt
2106 * the rejoin, each disk that is offlined is marked online again, and
2107 * we do a reopen() call. If the vdev label for every disk that was
2108 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2109 * then we call vdev_split() on each disk, and complete the split.
2111 * Otherwise we leave the config alone, with all the vdevs in place in
2112 * the original pool.
2115 spa_try_repair(spa_t *spa, nvlist_t *config)
2122 boolean_t attempt_reopen;
2124 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2127 /* check that the config is complete */
2128 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2129 &glist, &gcount) != 0)
2132 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2134 /* attempt to online all the vdevs & validate */
2135 attempt_reopen = B_TRUE;
2136 for (i = 0; i < gcount; i++) {
2137 if (glist[i] == 0) /* vdev is hole */
2140 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2141 if (vd[i] == NULL) {
2143 * Don't bother attempting to reopen the disks;
2144 * just do the split.
2146 attempt_reopen = B_FALSE;
2148 /* attempt to re-online it */
2149 vd[i]->vdev_offline = B_FALSE;
2153 if (attempt_reopen) {
2154 vdev_reopen(spa->spa_root_vdev);
2156 /* check each device to see what state it's in */
2157 for (extracted = 0, i = 0; i < gcount; i++) {
2158 if (vd[i] != NULL &&
2159 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2166 * If every disk has been moved to the new pool, or if we never
2167 * even attempted to look at them, then we split them off for
2170 if (!attempt_reopen || gcount == extracted) {
2171 for (i = 0; i < gcount; i++)
2174 vdev_reopen(spa->spa_root_vdev);
2177 kmem_free(vd, gcount * sizeof (vdev_t *));
2181 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2182 boolean_t mosconfig)
2184 nvlist_t *config = spa->spa_config;
2185 char *ereport = FM_EREPORT_ZFS_POOL;
2191 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2192 return (SET_ERROR(EINVAL));
2194 ASSERT(spa->spa_comment == NULL);
2195 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2196 spa->spa_comment = spa_strdup(comment);
2199 * Versioning wasn't explicitly added to the label until later, so if
2200 * it's not present treat it as the initial version.
2202 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2203 &spa->spa_ubsync.ub_version) != 0)
2204 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2206 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2207 &spa->spa_config_txg);
2209 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2210 spa_guid_exists(pool_guid, 0)) {
2211 error = SET_ERROR(EEXIST);
2213 spa->spa_config_guid = pool_guid;
2215 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2217 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2221 nvlist_free(spa->spa_load_info);
2222 spa->spa_load_info = fnvlist_alloc();
2224 gethrestime(&spa->spa_loaded_ts);
2225 error = spa_load_impl(spa, pool_guid, config, state, type,
2226 mosconfig, &ereport);
2230 * Don't count references from objsets that are already closed
2231 * and are making their way through the eviction process.
2233 spa_evicting_os_wait(spa);
2234 spa->spa_minref = refcount_count(&spa->spa_refcount);
2236 if (error != EEXIST) {
2237 spa->spa_loaded_ts.tv_sec = 0;
2238 spa->spa_loaded_ts.tv_nsec = 0;
2240 if (error != EBADF) {
2241 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2244 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2251 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2252 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2253 * spa's per-vdev ZAP list.
2256 vdev_count_verify_zaps(vdev_t *vd)
2258 spa_t *spa = vd->vdev_spa;
2260 if (vd->vdev_top_zap != 0) {
2262 ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2263 spa->spa_all_vdev_zaps, vd->vdev_top_zap));
2265 if (vd->vdev_leaf_zap != 0) {
2267 ASSERT0(zap_lookup_int(spa->spa_meta_objset,
2268 spa->spa_all_vdev_zaps, vd->vdev_leaf_zap));
2271 for (uint64_t i = 0; i < vd->vdev_children; i++) {
2272 total += vdev_count_verify_zaps(vd->vdev_child[i]);
2279 * Load an existing storage pool, using the pool's builtin spa_config as a
2280 * source of configuration information.
2283 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2284 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2288 nvlist_t *nvroot = NULL;
2291 uberblock_t *ub = &spa->spa_uberblock;
2292 uint64_t children, config_cache_txg = spa->spa_config_txg;
2293 int orig_mode = spa->spa_mode;
2296 boolean_t missing_feat_write = B_FALSE;
2299 * If this is an untrusted config, access the pool in read-only mode.
2300 * This prevents things like resilvering recently removed devices.
2303 spa->spa_mode = FREAD;
2305 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2307 spa->spa_load_state = state;
2309 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2310 return (SET_ERROR(EINVAL));
2312 parse = (type == SPA_IMPORT_EXISTING ?
2313 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2316 * Create "The Godfather" zio to hold all async IOs
2318 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2320 for (int i = 0; i < max_ncpus; i++) {
2321 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2322 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2323 ZIO_FLAG_GODFATHER);
2327 * Parse the configuration into a vdev tree. We explicitly set the
2328 * value that will be returned by spa_version() since parsing the
2329 * configuration requires knowing the version number.
2331 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2332 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2333 spa_config_exit(spa, SCL_ALL, FTAG);
2338 ASSERT(spa->spa_root_vdev == rvd);
2339 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2340 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
2342 if (type != SPA_IMPORT_ASSEMBLE) {
2343 ASSERT(spa_guid(spa) == pool_guid);
2347 * Try to open all vdevs, loading each label in the process.
2349 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2350 error = vdev_open(rvd);
2351 spa_config_exit(spa, SCL_ALL, FTAG);
2356 * We need to validate the vdev labels against the configuration that
2357 * we have in hand, which is dependent on the setting of mosconfig. If
2358 * mosconfig is true then we're validating the vdev labels based on
2359 * that config. Otherwise, we're validating against the cached config
2360 * (zpool.cache) that was read when we loaded the zfs module, and then
2361 * later we will recursively call spa_load() and validate against
2364 * If we're assembling a new pool that's been split off from an
2365 * existing pool, the labels haven't yet been updated so we skip
2366 * validation for now.
2368 if (type != SPA_IMPORT_ASSEMBLE) {
2369 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2370 error = vdev_validate(rvd, mosconfig);
2371 spa_config_exit(spa, SCL_ALL, FTAG);
2376 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2377 return (SET_ERROR(ENXIO));
2381 * Find the best uberblock.
2383 vdev_uberblock_load(rvd, ub, &label);
2386 * If we weren't able to find a single valid uberblock, return failure.
2388 if (ub->ub_txg == 0) {
2390 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2394 * If the pool has an unsupported version we can't open it.
2396 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2398 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2401 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2405 * If we weren't able to find what's necessary for reading the
2406 * MOS in the label, return failure.
2408 if (label == NULL || nvlist_lookup_nvlist(label,
2409 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2411 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2416 * Update our in-core representation with the definitive values
2419 nvlist_free(spa->spa_label_features);
2420 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2426 * Look through entries in the label nvlist's features_for_read. If
2427 * there is a feature listed there which we don't understand then we
2428 * cannot open a pool.
2430 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2431 nvlist_t *unsup_feat;
2433 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2436 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2438 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2439 if (!zfeature_is_supported(nvpair_name(nvp))) {
2440 VERIFY(nvlist_add_string(unsup_feat,
2441 nvpair_name(nvp), "") == 0);
2445 if (!nvlist_empty(unsup_feat)) {
2446 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2447 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2448 nvlist_free(unsup_feat);
2449 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2453 nvlist_free(unsup_feat);
2457 * If the vdev guid sum doesn't match the uberblock, we have an
2458 * incomplete configuration. We first check to see if the pool
2459 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2460 * If it is, defer the vdev_guid_sum check till later so we
2461 * can handle missing vdevs.
2463 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2464 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2465 rvd->vdev_guid_sum != ub->ub_guid_sum)
2466 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2468 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2469 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2470 spa_try_repair(spa, config);
2471 spa_config_exit(spa, SCL_ALL, FTAG);
2472 nvlist_free(spa->spa_config_splitting);
2473 spa->spa_config_splitting = NULL;
2477 * Initialize internal SPA structures.
2479 spa->spa_state = POOL_STATE_ACTIVE;
2480 spa->spa_ubsync = spa->spa_uberblock;
2481 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2482 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2483 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2484 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2485 spa->spa_claim_max_txg = spa->spa_first_txg;
2486 spa->spa_prev_software_version = ub->ub_software_version;
2488 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2490 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2491 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2493 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2494 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2496 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2497 boolean_t missing_feat_read = B_FALSE;
2498 nvlist_t *unsup_feat, *enabled_feat;
2500 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2501 &spa->spa_feat_for_read_obj) != 0) {
2502 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2505 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2506 &spa->spa_feat_for_write_obj) != 0) {
2507 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2510 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2511 &spa->spa_feat_desc_obj) != 0) {
2512 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2515 enabled_feat = fnvlist_alloc();
2516 unsup_feat = fnvlist_alloc();
2518 if (!spa_features_check(spa, B_FALSE,
2519 unsup_feat, enabled_feat))
2520 missing_feat_read = B_TRUE;
2522 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2523 if (!spa_features_check(spa, B_TRUE,
2524 unsup_feat, enabled_feat)) {
2525 missing_feat_write = B_TRUE;
2529 fnvlist_add_nvlist(spa->spa_load_info,
2530 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2532 if (!nvlist_empty(unsup_feat)) {
2533 fnvlist_add_nvlist(spa->spa_load_info,
2534 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2537 fnvlist_free(enabled_feat);
2538 fnvlist_free(unsup_feat);
2540 if (!missing_feat_read) {
2541 fnvlist_add_boolean(spa->spa_load_info,
2542 ZPOOL_CONFIG_CAN_RDONLY);
2546 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2547 * twofold: to determine whether the pool is available for
2548 * import in read-write mode and (if it is not) whether the
2549 * pool is available for import in read-only mode. If the pool
2550 * is available for import in read-write mode, it is displayed
2551 * as available in userland; if it is not available for import
2552 * in read-only mode, it is displayed as unavailable in
2553 * userland. If the pool is available for import in read-only
2554 * mode but not read-write mode, it is displayed as unavailable
2555 * in userland with a special note that the pool is actually
2556 * available for open in read-only mode.
2558 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2559 * missing a feature for write, we must first determine whether
2560 * the pool can be opened read-only before returning to
2561 * userland in order to know whether to display the
2562 * abovementioned note.
2564 if (missing_feat_read || (missing_feat_write &&
2565 spa_writeable(spa))) {
2566 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2571 * Load refcounts for ZFS features from disk into an in-memory
2572 * cache during SPA initialization.
2574 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2577 error = feature_get_refcount_from_disk(spa,
2578 &spa_feature_table[i], &refcount);
2580 spa->spa_feat_refcount_cache[i] = refcount;
2581 } else if (error == ENOTSUP) {
2582 spa->spa_feat_refcount_cache[i] =
2583 SPA_FEATURE_DISABLED;
2585 return (spa_vdev_err(rvd,
2586 VDEV_AUX_CORRUPT_DATA, EIO));
2591 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2592 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2593 &spa->spa_feat_enabled_txg_obj) != 0)
2594 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2597 spa->spa_is_initializing = B_TRUE;
2598 error = dsl_pool_open(spa->spa_dsl_pool);
2599 spa->spa_is_initializing = B_FALSE;
2601 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2605 nvlist_t *policy = NULL, *nvconfig;
2607 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2608 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2610 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2611 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2613 unsigned long myhostid = 0;
2615 VERIFY(nvlist_lookup_string(nvconfig,
2616 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2619 myhostid = zone_get_hostid(NULL);
2622 * We're emulating the system's hostid in userland, so
2623 * we can't use zone_get_hostid().
2625 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2626 #endif /* _KERNEL */
2627 if (check_hostid && hostid != 0 && myhostid != 0 &&
2628 hostid != myhostid) {
2629 nvlist_free(nvconfig);
2630 cmn_err(CE_WARN, "pool '%s' could not be "
2631 "loaded as it was last accessed by "
2632 "another system (host: %s hostid: 0x%lx). "
2633 "See: http://illumos.org/msg/ZFS-8000-EY",
2634 spa_name(spa), hostname,
2635 (unsigned long)hostid);
2636 return (SET_ERROR(EBADF));
2639 if (nvlist_lookup_nvlist(spa->spa_config,
2640 ZPOOL_REWIND_POLICY, &policy) == 0)
2641 VERIFY(nvlist_add_nvlist(nvconfig,
2642 ZPOOL_REWIND_POLICY, policy) == 0);
2644 spa_config_set(spa, nvconfig);
2646 spa_deactivate(spa);
2647 spa_activate(spa, orig_mode);
2649 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2652 /* Grab the secret checksum salt from the MOS. */
2653 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2654 DMU_POOL_CHECKSUM_SALT, 1,
2655 sizeof (spa->spa_cksum_salt.zcs_bytes),
2656 spa->spa_cksum_salt.zcs_bytes);
2657 if (error == ENOENT) {
2658 /* Generate a new salt for subsequent use */
2659 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
2660 sizeof (spa->spa_cksum_salt.zcs_bytes));
2661 } else if (error != 0) {
2662 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2665 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2666 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2667 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2669 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2672 * Load the bit that tells us to use the new accounting function
2673 * (raid-z deflation). If we have an older pool, this will not
2676 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2677 if (error != 0 && error != ENOENT)
2678 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2680 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2681 &spa->spa_creation_version);
2682 if (error != 0 && error != ENOENT)
2683 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2686 * Load the persistent error log. If we have an older pool, this will
2689 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2690 if (error != 0 && error != ENOENT)
2691 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2693 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2694 &spa->spa_errlog_scrub);
2695 if (error != 0 && error != ENOENT)
2696 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2699 * Load the history object. If we have an older pool, this
2700 * will not be present.
2702 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2703 if (error != 0 && error != ENOENT)
2704 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2707 * Load the per-vdev ZAP map. If we have an older pool, this will not
2708 * be present; in this case, defer its creation to a later time to
2709 * avoid dirtying the MOS this early / out of sync context. See
2710 * spa_sync_config_object.
2713 /* The sentinel is only available in the MOS config. */
2714 nvlist_t *mos_config;
2715 if (load_nvlist(spa, spa->spa_config_object, &mos_config) != 0)
2716 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2718 error = spa_dir_prop(spa, DMU_POOL_VDEV_ZAP_MAP,
2719 &spa->spa_all_vdev_zaps);
2721 if (error != ENOENT && error != 0) {
2722 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2723 } else if (error == 0 && !nvlist_exists(mos_config,
2724 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS)) {
2726 * An older version of ZFS overwrote the sentinel value, so
2727 * we have orphaned per-vdev ZAPs in the MOS. Defer their
2728 * destruction to later; see spa_sync_config_object.
2730 spa->spa_avz_action = AVZ_ACTION_DESTROY;
2732 * We're assuming that no vdevs have had their ZAPs created
2733 * before this. Better be sure of it.
2735 ASSERT0(vdev_count_verify_zaps(spa->spa_root_vdev));
2737 nvlist_free(mos_config);
2740 * If we're assembling the pool from the split-off vdevs of
2741 * an existing pool, we don't want to attach the spares & cache
2746 * Load any hot spares for this pool.
2748 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2749 if (error != 0 && error != ENOENT)
2750 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2751 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2752 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2753 if (load_nvlist(spa, spa->spa_spares.sav_object,
2754 &spa->spa_spares.sav_config) != 0)
2755 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2757 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2758 spa_load_spares(spa);
2759 spa_config_exit(spa, SCL_ALL, FTAG);
2760 } else if (error == 0) {
2761 spa->spa_spares.sav_sync = B_TRUE;
2765 * Load any level 2 ARC devices for this pool.
2767 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2768 &spa->spa_l2cache.sav_object);
2769 if (error != 0 && error != ENOENT)
2770 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2771 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2772 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2773 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2774 &spa->spa_l2cache.sav_config) != 0)
2775 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2777 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2778 spa_load_l2cache(spa);
2779 spa_config_exit(spa, SCL_ALL, FTAG);
2780 } else if (error == 0) {
2781 spa->spa_l2cache.sav_sync = B_TRUE;
2784 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2786 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2787 if (error && error != ENOENT)
2788 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2791 uint64_t autoreplace;
2793 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2794 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2795 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2796 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2797 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2798 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2799 &spa->spa_dedup_ditto);
2801 spa->spa_autoreplace = (autoreplace != 0);
2805 * If the 'autoreplace' property is set, then post a resource notifying
2806 * the ZFS DE that it should not issue any faults for unopenable
2807 * devices. We also iterate over the vdevs, and post a sysevent for any
2808 * unopenable vdevs so that the normal autoreplace handler can take
2811 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2812 spa_check_removed(spa->spa_root_vdev);
2814 * For the import case, this is done in spa_import(), because
2815 * at this point we're using the spare definitions from
2816 * the MOS config, not necessarily from the userland config.
2818 if (state != SPA_LOAD_IMPORT) {
2819 spa_aux_check_removed(&spa->spa_spares);
2820 spa_aux_check_removed(&spa->spa_l2cache);
2825 * Load the vdev state for all toplevel vdevs.
2830 * Propagate the leaf DTLs we just loaded all the way up the tree.
2832 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2833 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2834 spa_config_exit(spa, SCL_ALL, FTAG);
2837 * Load the DDTs (dedup tables).
2839 error = ddt_load(spa);
2841 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2843 spa_update_dspace(spa);
2846 * Validate the config, using the MOS config to fill in any
2847 * information which might be missing. If we fail to validate
2848 * the config then declare the pool unfit for use. If we're
2849 * assembling a pool from a split, the log is not transferred
2852 if (type != SPA_IMPORT_ASSEMBLE) {
2855 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2856 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2858 if (!spa_config_valid(spa, nvconfig)) {
2859 nvlist_free(nvconfig);
2860 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2863 nvlist_free(nvconfig);
2866 * Now that we've validated the config, check the state of the
2867 * root vdev. If it can't be opened, it indicates one or
2868 * more toplevel vdevs are faulted.
2870 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2871 return (SET_ERROR(ENXIO));
2873 if (spa_writeable(spa) && spa_check_logs(spa)) {
2874 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2875 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2879 if (missing_feat_write) {
2880 ASSERT(state == SPA_LOAD_TRYIMPORT);
2883 * At this point, we know that we can open the pool in
2884 * read-only mode but not read-write mode. We now have enough
2885 * information and can return to userland.
2887 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2891 * We've successfully opened the pool, verify that we're ready
2892 * to start pushing transactions.
2894 if (state != SPA_LOAD_TRYIMPORT) {
2895 if (error = spa_load_verify(spa))
2896 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2900 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2901 spa->spa_load_max_txg == UINT64_MAX)) {
2903 int need_update = B_FALSE;
2904 dsl_pool_t *dp = spa_get_dsl(spa);
2906 ASSERT(state != SPA_LOAD_TRYIMPORT);
2909 * Claim log blocks that haven't been committed yet.
2910 * This must all happen in a single txg.
2911 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2912 * invoked from zil_claim_log_block()'s i/o done callback.
2913 * Price of rollback is that we abandon the log.
2915 spa->spa_claiming = B_TRUE;
2917 tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
2918 (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2919 zil_claim, tx, DS_FIND_CHILDREN);
2922 spa->spa_claiming = B_FALSE;
2924 spa_set_log_state(spa, SPA_LOG_GOOD);
2925 spa->spa_sync_on = B_TRUE;
2926 txg_sync_start(spa->spa_dsl_pool);
2929 * Wait for all claims to sync. We sync up to the highest
2930 * claimed log block birth time so that claimed log blocks
2931 * don't appear to be from the future. spa_claim_max_txg
2932 * will have been set for us by either zil_check_log_chain()
2933 * (invoked from spa_check_logs()) or zil_claim() above.
2935 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2938 * If the config cache is stale, or we have uninitialized
2939 * metaslabs (see spa_vdev_add()), then update the config.
2941 * If this is a verbatim import, trust the current
2942 * in-core spa_config and update the disk labels.
2944 if (config_cache_txg != spa->spa_config_txg ||
2945 state == SPA_LOAD_IMPORT ||
2946 state == SPA_LOAD_RECOVER ||
2947 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2948 need_update = B_TRUE;
2950 for (int c = 0; c < rvd->vdev_children; c++)
2951 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2952 need_update = B_TRUE;
2955 * Update the config cache asychronously in case we're the
2956 * root pool, in which case the config cache isn't writable yet.
2959 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2962 * Check all DTLs to see if anything needs resilvering.
2964 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2965 vdev_resilver_needed(rvd, NULL, NULL))
2966 spa_async_request(spa, SPA_ASYNC_RESILVER);
2969 * Log the fact that we booted up (so that we can detect if
2970 * we rebooted in the middle of an operation).
2972 spa_history_log_version(spa, "open");
2975 * Delete any inconsistent datasets.
2977 (void) dmu_objset_find(spa_name(spa),
2978 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2981 * Clean up any stale temporary dataset userrefs.
2983 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2990 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2992 int mode = spa->spa_mode;
2995 spa_deactivate(spa);
2997 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2999 spa_activate(spa, mode);
3000 spa_async_suspend(spa);
3002 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
3006 * If spa_load() fails this function will try loading prior txg's. If
3007 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3008 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3009 * function will not rewind the pool and will return the same error as
3013 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
3014 uint64_t max_request, int rewind_flags)
3016 nvlist_t *loadinfo = NULL;
3017 nvlist_t *config = NULL;
3018 int load_error, rewind_error;
3019 uint64_t safe_rewind_txg;
3022 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
3023 spa->spa_load_max_txg = spa->spa_load_txg;
3024 spa_set_log_state(spa, SPA_LOG_CLEAR);
3026 spa->spa_load_max_txg = max_request;
3027 if (max_request != UINT64_MAX)
3028 spa->spa_extreme_rewind = B_TRUE;
3031 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
3033 if (load_error == 0)
3036 if (spa->spa_root_vdev != NULL)
3037 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3039 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
3040 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
3042 if (rewind_flags & ZPOOL_NEVER_REWIND) {
3043 nvlist_free(config);
3044 return (load_error);
3047 if (state == SPA_LOAD_RECOVER) {
3048 /* Price of rolling back is discarding txgs, including log */
3049 spa_set_log_state(spa, SPA_LOG_CLEAR);
3052 * If we aren't rolling back save the load info from our first
3053 * import attempt so that we can restore it after attempting
3056 loadinfo = spa->spa_load_info;
3057 spa->spa_load_info = fnvlist_alloc();
3060 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
3061 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
3062 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
3063 TXG_INITIAL : safe_rewind_txg;
3066 * Continue as long as we're finding errors, we're still within
3067 * the acceptable rewind range, and we're still finding uberblocks
3069 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
3070 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
3071 if (spa->spa_load_max_txg < safe_rewind_txg)
3072 spa->spa_extreme_rewind = B_TRUE;
3073 rewind_error = spa_load_retry(spa, state, mosconfig);
3076 spa->spa_extreme_rewind = B_FALSE;
3077 spa->spa_load_max_txg = UINT64_MAX;
3079 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
3080 spa_config_set(spa, config);
3082 if (state == SPA_LOAD_RECOVER) {
3083 ASSERT3P(loadinfo, ==, NULL);
3084 return (rewind_error);
3086 /* Store the rewind info as part of the initial load info */
3087 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
3088 spa->spa_load_info);
3090 /* Restore the initial load info */
3091 fnvlist_free(spa->spa_load_info);
3092 spa->spa_load_info = loadinfo;
3094 return (load_error);
3101 * The import case is identical to an open except that the configuration is sent
3102 * down from userland, instead of grabbed from the configuration cache. For the
3103 * case of an open, the pool configuration will exist in the
3104 * POOL_STATE_UNINITIALIZED state.
3106 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3107 * the same time open the pool, without having to keep around the spa_t in some
3111 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
3115 spa_load_state_t state = SPA_LOAD_OPEN;
3117 int locked = B_FALSE;
3118 int firstopen = B_FALSE;
3123 * As disgusting as this is, we need to support recursive calls to this
3124 * function because dsl_dir_open() is called during spa_load(), and ends
3125 * up calling spa_open() again. The real fix is to figure out how to
3126 * avoid dsl_dir_open() calling this in the first place.
3128 if (mutex_owner(&spa_namespace_lock) != curthread) {
3129 mutex_enter(&spa_namespace_lock);
3133 if ((spa = spa_lookup(pool)) == NULL) {
3135 mutex_exit(&spa_namespace_lock);
3136 return (SET_ERROR(ENOENT));
3139 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
3140 zpool_rewind_policy_t policy;
3144 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
3146 if (policy.zrp_request & ZPOOL_DO_REWIND)
3147 state = SPA_LOAD_RECOVER;
3149 spa_activate(spa, spa_mode_global);
3151 if (state != SPA_LOAD_RECOVER)
3152 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3154 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3155 policy.zrp_request);
3157 if (error == EBADF) {
3159 * If vdev_validate() returns failure (indicated by
3160 * EBADF), it indicates that one of the vdevs indicates
3161 * that the pool has been exported or destroyed. If
3162 * this is the case, the config cache is out of sync and
3163 * we should remove the pool from the namespace.
3166 spa_deactivate(spa);
3167 spa_config_sync(spa, B_TRUE, B_TRUE);
3170 mutex_exit(&spa_namespace_lock);
3171 return (SET_ERROR(ENOENT));
3176 * We can't open the pool, but we still have useful
3177 * information: the state of each vdev after the
3178 * attempted vdev_open(). Return this to the user.
3180 if (config != NULL && spa->spa_config) {
3181 VERIFY(nvlist_dup(spa->spa_config, config,
3183 VERIFY(nvlist_add_nvlist(*config,
3184 ZPOOL_CONFIG_LOAD_INFO,
3185 spa->spa_load_info) == 0);
3188 spa_deactivate(spa);
3189 spa->spa_last_open_failed = error;
3191 mutex_exit(&spa_namespace_lock);
3197 spa_open_ref(spa, tag);
3200 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3203 * If we've recovered the pool, pass back any information we
3204 * gathered while doing the load.
3206 if (state == SPA_LOAD_RECOVER) {
3207 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3208 spa->spa_load_info) == 0);
3212 spa->spa_last_open_failed = 0;
3213 spa->spa_last_ubsync_txg = 0;
3214 spa->spa_load_txg = 0;
3215 mutex_exit(&spa_namespace_lock);
3219 zvol_create_minors(spa->spa_name);
3230 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3233 return (spa_open_common(name, spapp, tag, policy, config));
3237 spa_open(const char *name, spa_t **spapp, void *tag)
3239 return (spa_open_common(name, spapp, tag, NULL, NULL));
3243 * Lookup the given spa_t, incrementing the inject count in the process,
3244 * preventing it from being exported or destroyed.
3247 spa_inject_addref(char *name)
3251 mutex_enter(&spa_namespace_lock);
3252 if ((spa = spa_lookup(name)) == NULL) {
3253 mutex_exit(&spa_namespace_lock);
3256 spa->spa_inject_ref++;
3257 mutex_exit(&spa_namespace_lock);
3263 spa_inject_delref(spa_t *spa)
3265 mutex_enter(&spa_namespace_lock);
3266 spa->spa_inject_ref--;
3267 mutex_exit(&spa_namespace_lock);
3271 * Add spares device information to the nvlist.
3274 spa_add_spares(spa_t *spa, nvlist_t *config)
3284 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3286 if (spa->spa_spares.sav_count == 0)
3289 VERIFY(nvlist_lookup_nvlist(config,
3290 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3291 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3292 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3294 VERIFY(nvlist_add_nvlist_array(nvroot,
3295 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3296 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3297 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3300 * Go through and find any spares which have since been
3301 * repurposed as an active spare. If this is the case, update
3302 * their status appropriately.
3304 for (i = 0; i < nspares; i++) {
3305 VERIFY(nvlist_lookup_uint64(spares[i],
3306 ZPOOL_CONFIG_GUID, &guid) == 0);
3307 if (spa_spare_exists(guid, &pool, NULL) &&
3309 VERIFY(nvlist_lookup_uint64_array(
3310 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3311 (uint64_t **)&vs, &vsc) == 0);
3312 vs->vs_state = VDEV_STATE_CANT_OPEN;
3313 vs->vs_aux = VDEV_AUX_SPARED;
3320 * Add l2cache device information to the nvlist, including vdev stats.
3323 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3326 uint_t i, j, nl2cache;
3333 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3335 if (spa->spa_l2cache.sav_count == 0)
3338 VERIFY(nvlist_lookup_nvlist(config,
3339 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3340 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3341 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3342 if (nl2cache != 0) {
3343 VERIFY(nvlist_add_nvlist_array(nvroot,
3344 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3345 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3346 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3349 * Update level 2 cache device stats.
3352 for (i = 0; i < nl2cache; i++) {
3353 VERIFY(nvlist_lookup_uint64(l2cache[i],
3354 ZPOOL_CONFIG_GUID, &guid) == 0);
3357 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3359 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3360 vd = spa->spa_l2cache.sav_vdevs[j];
3366 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3367 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3369 vdev_get_stats(vd, vs);
3375 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3381 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3382 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3384 /* We may be unable to read features if pool is suspended. */
3385 if (spa_suspended(spa))
3388 if (spa->spa_feat_for_read_obj != 0) {
3389 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3390 spa->spa_feat_for_read_obj);
3391 zap_cursor_retrieve(&zc, &za) == 0;
3392 zap_cursor_advance(&zc)) {
3393 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3394 za.za_num_integers == 1);
3395 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3396 za.za_first_integer));
3398 zap_cursor_fini(&zc);
3401 if (spa->spa_feat_for_write_obj != 0) {
3402 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3403 spa->spa_feat_for_write_obj);
3404 zap_cursor_retrieve(&zc, &za) == 0;
3405 zap_cursor_advance(&zc)) {
3406 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3407 za.za_num_integers == 1);
3408 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3409 za.za_first_integer));
3411 zap_cursor_fini(&zc);
3415 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3417 nvlist_free(features);
3421 spa_get_stats(const char *name, nvlist_t **config,
3422 char *altroot, size_t buflen)
3428 error = spa_open_common(name, &spa, FTAG, NULL, config);
3432 * This still leaves a window of inconsistency where the spares
3433 * or l2cache devices could change and the config would be
3434 * self-inconsistent.
3436 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3438 if (*config != NULL) {
3439 uint64_t loadtimes[2];
3441 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3442 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3443 VERIFY(nvlist_add_uint64_array(*config,
3444 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3446 VERIFY(nvlist_add_uint64(*config,
3447 ZPOOL_CONFIG_ERRCOUNT,
3448 spa_get_errlog_size(spa)) == 0);
3450 if (spa_suspended(spa))
3451 VERIFY(nvlist_add_uint64(*config,
3452 ZPOOL_CONFIG_SUSPENDED,
3453 spa->spa_failmode) == 0);
3455 spa_add_spares(spa, *config);
3456 spa_add_l2cache(spa, *config);
3457 spa_add_feature_stats(spa, *config);
3462 * We want to get the alternate root even for faulted pools, so we cheat
3463 * and call spa_lookup() directly.
3467 mutex_enter(&spa_namespace_lock);
3468 spa = spa_lookup(name);
3470 spa_altroot(spa, altroot, buflen);
3474 mutex_exit(&spa_namespace_lock);
3476 spa_altroot(spa, altroot, buflen);
3481 spa_config_exit(spa, SCL_CONFIG, FTAG);
3482 spa_close(spa, FTAG);
3489 * Validate that the auxiliary device array is well formed. We must have an
3490 * array of nvlists, each which describes a valid leaf vdev. If this is an
3491 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3492 * specified, as long as they are well-formed.
3495 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3496 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3497 vdev_labeltype_t label)
3504 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3507 * It's acceptable to have no devs specified.
3509 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3513 return (SET_ERROR(EINVAL));
3516 * Make sure the pool is formatted with a version that supports this
3519 if (spa_version(spa) < version)
3520 return (SET_ERROR(ENOTSUP));
3523 * Set the pending device list so we correctly handle device in-use
3526 sav->sav_pending = dev;
3527 sav->sav_npending = ndev;
3529 for (i = 0; i < ndev; i++) {
3530 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3534 if (!vd->vdev_ops->vdev_op_leaf) {
3536 error = SET_ERROR(EINVAL);
3541 * The L2ARC currently only supports disk devices in
3542 * kernel context. For user-level testing, we allow it.
3545 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3546 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3547 error = SET_ERROR(ENOTBLK);
3554 if ((error = vdev_open(vd)) == 0 &&
3555 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3556 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3557 vd->vdev_guid) == 0);
3563 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3570 sav->sav_pending = NULL;
3571 sav->sav_npending = 0;
3576 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3580 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3582 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3583 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3584 VDEV_LABEL_SPARE)) != 0) {
3588 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3589 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3590 VDEV_LABEL_L2CACHE));
3594 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3599 if (sav->sav_config != NULL) {
3605 * Generate new dev list by concatentating with the
3608 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3609 &olddevs, &oldndevs) == 0);
3611 newdevs = kmem_alloc(sizeof (void *) *
3612 (ndevs + oldndevs), KM_SLEEP);
3613 for (i = 0; i < oldndevs; i++)
3614 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3616 for (i = 0; i < ndevs; i++)
3617 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3620 VERIFY(nvlist_remove(sav->sav_config, config,
3621 DATA_TYPE_NVLIST_ARRAY) == 0);
3623 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3624 config, newdevs, ndevs + oldndevs) == 0);
3625 for (i = 0; i < oldndevs + ndevs; i++)
3626 nvlist_free(newdevs[i]);
3627 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3630 * Generate a new dev list.
3632 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3634 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3640 * Stop and drop level 2 ARC devices
3643 spa_l2cache_drop(spa_t *spa)
3647 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3649 for (i = 0; i < sav->sav_count; i++) {
3652 vd = sav->sav_vdevs[i];
3655 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3656 pool != 0ULL && l2arc_vdev_present(vd))
3657 l2arc_remove_vdev(vd);
3665 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3669 char *altroot = NULL;
3674 uint64_t txg = TXG_INITIAL;
3675 nvlist_t **spares, **l2cache;
3676 uint_t nspares, nl2cache;
3677 uint64_t version, obj;
3678 boolean_t has_features;
3681 * If this pool already exists, return failure.
3683 mutex_enter(&spa_namespace_lock);
3684 if (spa_lookup(pool) != NULL) {
3685 mutex_exit(&spa_namespace_lock);
3686 return (SET_ERROR(EEXIST));
3690 * Allocate a new spa_t structure.
3692 (void) nvlist_lookup_string(props,
3693 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3694 spa = spa_add(pool, NULL, altroot);
3695 spa_activate(spa, spa_mode_global);
3697 if (props && (error = spa_prop_validate(spa, props))) {
3698 spa_deactivate(spa);
3700 mutex_exit(&spa_namespace_lock);
3704 has_features = B_FALSE;
3705 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3706 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3707 if (zpool_prop_feature(nvpair_name(elem)))
3708 has_features = B_TRUE;
3711 if (has_features || nvlist_lookup_uint64(props,
3712 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3713 version = SPA_VERSION;
3715 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3717 spa->spa_first_txg = txg;
3718 spa->spa_uberblock.ub_txg = txg - 1;
3719 spa->spa_uberblock.ub_version = version;
3720 spa->spa_ubsync = spa->spa_uberblock;
3721 spa->spa_load_state = SPA_LOAD_CREATE;
3724 * Create "The Godfather" zio to hold all async IOs
3726 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3728 for (int i = 0; i < max_ncpus; i++) {
3729 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3730 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3731 ZIO_FLAG_GODFATHER);
3735 * Create the root vdev.
3737 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3739 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3741 ASSERT(error != 0 || rvd != NULL);
3742 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3744 if (error == 0 && !zfs_allocatable_devs(nvroot))
3745 error = SET_ERROR(EINVAL);
3748 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3749 (error = spa_validate_aux(spa, nvroot, txg,
3750 VDEV_ALLOC_ADD)) == 0) {
3751 for (int c = 0; c < rvd->vdev_children; c++) {
3752 vdev_ashift_optimize(rvd->vdev_child[c]);
3753 vdev_metaslab_set_size(rvd->vdev_child[c]);
3754 vdev_expand(rvd->vdev_child[c], txg);
3758 spa_config_exit(spa, SCL_ALL, FTAG);
3762 spa_deactivate(spa);
3764 mutex_exit(&spa_namespace_lock);
3769 * Get the list of spares, if specified.
3771 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3772 &spares, &nspares) == 0) {
3773 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3775 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3776 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3777 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3778 spa_load_spares(spa);
3779 spa_config_exit(spa, SCL_ALL, FTAG);
3780 spa->spa_spares.sav_sync = B_TRUE;
3784 * Get the list of level 2 cache devices, if specified.
3786 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3787 &l2cache, &nl2cache) == 0) {
3788 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3789 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3790 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3791 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3792 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3793 spa_load_l2cache(spa);
3794 spa_config_exit(spa, SCL_ALL, FTAG);
3795 spa->spa_l2cache.sav_sync = B_TRUE;
3798 spa->spa_is_initializing = B_TRUE;
3799 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3800 spa->spa_meta_objset = dp->dp_meta_objset;
3801 spa->spa_is_initializing = B_FALSE;
3804 * Create DDTs (dedup tables).
3808 spa_update_dspace(spa);
3810 tx = dmu_tx_create_assigned(dp, txg);
3813 * Create the pool config object.
3815 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3816 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3817 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3819 if (zap_add(spa->spa_meta_objset,
3820 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3821 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3822 cmn_err(CE_PANIC, "failed to add pool config");
3825 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3826 spa_feature_create_zap_objects(spa, tx);
3828 if (zap_add(spa->spa_meta_objset,
3829 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3830 sizeof (uint64_t), 1, &version, tx) != 0) {
3831 cmn_err(CE_PANIC, "failed to add pool version");
3834 /* Newly created pools with the right version are always deflated. */
3835 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3836 spa->spa_deflate = TRUE;
3837 if (zap_add(spa->spa_meta_objset,
3838 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3839 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3840 cmn_err(CE_PANIC, "failed to add deflate");
3845 * Create the deferred-free bpobj. Turn off compression
3846 * because sync-to-convergence takes longer if the blocksize
3849 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3850 dmu_object_set_compress(spa->spa_meta_objset, obj,
3851 ZIO_COMPRESS_OFF, tx);
3852 if (zap_add(spa->spa_meta_objset,
3853 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3854 sizeof (uint64_t), 1, &obj, tx) != 0) {
3855 cmn_err(CE_PANIC, "failed to add bpobj");
3857 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3858 spa->spa_meta_objset, obj));
3861 * Create the pool's history object.
3863 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3864 spa_history_create_obj(spa, tx);
3867 * Generate some random noise for salted checksums to operate on.
3869 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
3870 sizeof (spa->spa_cksum_salt.zcs_bytes));
3873 * Set pool properties.
3875 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3876 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3877 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3878 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3880 if (props != NULL) {
3881 spa_configfile_set(spa, props, B_FALSE);
3882 spa_sync_props(props, tx);
3887 spa->spa_sync_on = B_TRUE;
3888 txg_sync_start(spa->spa_dsl_pool);
3891 * We explicitly wait for the first transaction to complete so that our
3892 * bean counters are appropriately updated.
3894 txg_wait_synced(spa->spa_dsl_pool, txg);
3896 spa_config_sync(spa, B_FALSE, B_TRUE);
3897 spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE);
3899 spa_history_log_version(spa, "create");
3902 * Don't count references from objsets that are already closed
3903 * and are making their way through the eviction process.
3905 spa_evicting_os_wait(spa);
3906 spa->spa_minref = refcount_count(&spa->spa_refcount);
3907 spa->spa_load_state = SPA_LOAD_NONE;
3909 mutex_exit(&spa_namespace_lock);
3917 * Get the root pool information from the root disk, then import the root pool
3918 * during the system boot up time.
3920 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3923 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3926 nvlist_t *nvtop, *nvroot;
3929 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3933 * Add this top-level vdev to the child array.
3935 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3937 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3939 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3942 * Put this pool's top-level vdevs into a root vdev.
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,
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);
3957 nvlist_free(nvroot);
3962 * Walk the vdev tree and see if we can find a device with "better"
3963 * configuration. A configuration is "better" if the label on that
3964 * device has a more recent txg.
3967 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3969 for (int c = 0; c < vd->vdev_children; c++)
3970 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3972 if (vd->vdev_ops->vdev_op_leaf) {
3976 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3980 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3984 * Do we have a better boot device?
3986 if (label_txg > *txg) {
3995 * Import a root pool.
3997 * For x86. devpath_list will consist of devid and/or physpath name of
3998 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3999 * The GRUB "findroot" command will return the vdev we should boot.
4001 * For Sparc, devpath_list consists the physpath name of the booting device
4002 * no matter the rootpool is a single device pool or a mirrored pool.
4004 * "/pci@1f,0/ide@d/disk@0,0:a"
4007 spa_import_rootpool(char *devpath, char *devid)
4010 vdev_t *rvd, *bvd, *avd = NULL;
4011 nvlist_t *config, *nvtop;
4017 * Read the label from the boot device and generate a configuration.
4019 config = spa_generate_rootconf(devpath, devid, &guid);
4020 #if defined(_OBP) && defined(_KERNEL)
4021 if (config == NULL) {
4022 if (strstr(devpath, "/iscsi/ssd") != NULL) {
4024 get_iscsi_bootpath_phy(devpath);
4025 config = spa_generate_rootconf(devpath, devid, &guid);
4029 if (config == NULL) {
4030 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
4032 return (SET_ERROR(EIO));
4035 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4037 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
4039 mutex_enter(&spa_namespace_lock);
4040 if ((spa = spa_lookup(pname)) != NULL) {
4042 * Remove the existing root pool from the namespace so that we
4043 * can replace it with the correct config we just read in.
4048 spa = spa_add(pname, config, NULL);
4049 spa->spa_is_root = B_TRUE;
4050 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4053 * Build up a vdev tree based on the boot device's label config.
4055 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4057 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4058 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4059 VDEV_ALLOC_ROOTPOOL);
4060 spa_config_exit(spa, SCL_ALL, FTAG);
4062 mutex_exit(&spa_namespace_lock);
4063 nvlist_free(config);
4064 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4070 * Get the boot vdev.
4072 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
4073 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
4074 (u_longlong_t)guid);
4075 error = SET_ERROR(ENOENT);
4080 * Determine if there is a better boot device.
4083 spa_alt_rootvdev(rvd, &avd, &txg);
4085 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
4086 "try booting from '%s'", avd->vdev_path);
4087 error = SET_ERROR(EINVAL);
4092 * If the boot device is part of a spare vdev then ensure that
4093 * we're booting off the active spare.
4095 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4096 !bvd->vdev_isspare) {
4097 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
4098 "try booting from '%s'",
4100 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
4101 error = SET_ERROR(EINVAL);
4107 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4109 spa_config_exit(spa, SCL_ALL, FTAG);
4110 mutex_exit(&spa_namespace_lock);
4112 nvlist_free(config);
4116 #else /* !illumos */
4118 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
4122 spa_generate_rootconf(const char *name)
4124 nvlist_t **configs, **tops;
4126 nvlist_t *best_cfg, *nvtop, *nvroot;
4135 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
4138 ASSERT3U(count, !=, 0);
4140 for (i = 0; i < count; i++) {
4143 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
4145 if (txg > best_txg) {
4147 best_cfg = configs[i];
4152 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
4154 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
4157 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
4158 for (i = 0; i < nchildren; i++) {
4161 if (configs[i] == NULL)
4163 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
4165 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
4167 for (i = 0; holes != NULL && i < nholes; i++) {
4170 if (tops[holes[i]] != NULL)
4172 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
4173 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
4174 VDEV_TYPE_HOLE) == 0);
4175 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
4177 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4180 for (i = 0; i < nchildren; i++) {
4181 if (tops[i] != NULL)
4183 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4184 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4185 VDEV_TYPE_MISSING) == 0);
4186 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4188 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4193 * Create pool config based on the best vdev config.
4195 nvlist_dup(best_cfg, &config, KM_SLEEP);
4198 * Put this pool's top-level vdevs into a root vdev.
4200 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4202 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4203 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4204 VDEV_TYPE_ROOT) == 0);
4205 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4206 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4207 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4208 tops, nchildren) == 0);
4211 * Replace the existing vdev_tree with the new root vdev in
4212 * this pool's configuration (remove the old, add the new).
4214 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4217 * Drop vdev config elements that should not be present at pool level.
4219 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4220 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4222 for (i = 0; i < count; i++)
4223 nvlist_free(configs[i]);
4224 kmem_free(configs, count * sizeof(void *));
4225 for (i = 0; i < nchildren; i++)
4226 nvlist_free(tops[i]);
4227 kmem_free(tops, nchildren * sizeof(void *));
4228 nvlist_free(nvroot);
4233 spa_import_rootpool(const char *name)
4236 vdev_t *rvd, *bvd, *avd = NULL;
4237 nvlist_t *config, *nvtop;
4243 * Read the label from the boot device and generate a configuration.
4245 config = spa_generate_rootconf(name);
4247 mutex_enter(&spa_namespace_lock);
4248 if (config != NULL) {
4249 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4250 &pname) == 0 && strcmp(name, pname) == 0);
4251 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4254 if ((spa = spa_lookup(pname)) != NULL) {
4256 * Remove the existing root pool from the namespace so
4257 * that we can replace it with the correct config
4262 spa = spa_add(pname, config, NULL);
4265 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4266 * via spa_version().
4268 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4269 &spa->spa_ubsync.ub_version) != 0)
4270 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4271 } else if ((spa = spa_lookup(name)) == NULL) {
4272 mutex_exit(&spa_namespace_lock);
4273 nvlist_free(config);
4274 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4278 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4280 spa->spa_is_root = B_TRUE;
4281 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4284 * Build up a vdev tree based on the boot device's label config.
4286 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4288 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4289 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4290 VDEV_ALLOC_ROOTPOOL);
4291 spa_config_exit(spa, SCL_ALL, FTAG);
4293 mutex_exit(&spa_namespace_lock);
4294 nvlist_free(config);
4295 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4300 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4302 spa_config_exit(spa, SCL_ALL, FTAG);
4303 mutex_exit(&spa_namespace_lock);
4305 nvlist_free(config);
4309 #endif /* illumos */
4310 #endif /* _KERNEL */
4313 * Import a non-root pool into the system.
4316 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4319 char *altroot = NULL;
4320 spa_load_state_t state = SPA_LOAD_IMPORT;
4321 zpool_rewind_policy_t policy;
4322 uint64_t mode = spa_mode_global;
4323 uint64_t readonly = B_FALSE;
4326 nvlist_t **spares, **l2cache;
4327 uint_t nspares, nl2cache;
4330 * If a pool with this name exists, return failure.
4332 mutex_enter(&spa_namespace_lock);
4333 if (spa_lookup(pool) != NULL) {
4334 mutex_exit(&spa_namespace_lock);
4335 return (SET_ERROR(EEXIST));
4339 * Create and initialize the spa structure.
4341 (void) nvlist_lookup_string(props,
4342 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4343 (void) nvlist_lookup_uint64(props,
4344 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4347 spa = spa_add(pool, config, altroot);
4348 spa->spa_import_flags = flags;
4351 * Verbatim import - Take a pool and insert it into the namespace
4352 * as if it had been loaded at boot.
4354 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4356 spa_configfile_set(spa, props, B_FALSE);
4358 spa_config_sync(spa, B_FALSE, B_TRUE);
4359 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4361 mutex_exit(&spa_namespace_lock);
4365 spa_activate(spa, mode);
4368 * Don't start async tasks until we know everything is healthy.
4370 spa_async_suspend(spa);
4372 zpool_get_rewind_policy(config, &policy);
4373 if (policy.zrp_request & ZPOOL_DO_REWIND)
4374 state = SPA_LOAD_RECOVER;
4377 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4378 * because the user-supplied config is actually the one to trust when
4381 if (state != SPA_LOAD_RECOVER)
4382 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4384 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4385 policy.zrp_request);
4388 * Propagate anything learned while loading the pool and pass it
4389 * back to caller (i.e. rewind info, missing devices, etc).
4391 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4392 spa->spa_load_info) == 0);
4394 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4396 * Toss any existing sparelist, as it doesn't have any validity
4397 * anymore, and conflicts with spa_has_spare().
4399 if (spa->spa_spares.sav_config) {
4400 nvlist_free(spa->spa_spares.sav_config);
4401 spa->spa_spares.sav_config = NULL;
4402 spa_load_spares(spa);
4404 if (spa->spa_l2cache.sav_config) {
4405 nvlist_free(spa->spa_l2cache.sav_config);
4406 spa->spa_l2cache.sav_config = NULL;
4407 spa_load_l2cache(spa);
4410 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4413 error = spa_validate_aux(spa, nvroot, -1ULL,
4416 error = spa_validate_aux(spa, nvroot, -1ULL,
4417 VDEV_ALLOC_L2CACHE);
4418 spa_config_exit(spa, SCL_ALL, FTAG);
4421 spa_configfile_set(spa, props, B_FALSE);
4423 if (error != 0 || (props && spa_writeable(spa) &&
4424 (error = spa_prop_set(spa, props)))) {
4426 spa_deactivate(spa);
4428 mutex_exit(&spa_namespace_lock);
4432 spa_async_resume(spa);
4435 * Override any spares and level 2 cache devices as specified by
4436 * the user, as these may have correct device names/devids, etc.
4438 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4439 &spares, &nspares) == 0) {
4440 if (spa->spa_spares.sav_config)
4441 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4442 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4444 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4445 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4446 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4447 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4448 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4449 spa_load_spares(spa);
4450 spa_config_exit(spa, SCL_ALL, FTAG);
4451 spa->spa_spares.sav_sync = B_TRUE;
4453 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4454 &l2cache, &nl2cache) == 0) {
4455 if (spa->spa_l2cache.sav_config)
4456 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4457 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4459 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4460 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4461 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4462 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4463 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4464 spa_load_l2cache(spa);
4465 spa_config_exit(spa, SCL_ALL, FTAG);
4466 spa->spa_l2cache.sav_sync = B_TRUE;
4470 * Check for any removed devices.
4472 if (spa->spa_autoreplace) {
4473 spa_aux_check_removed(&spa->spa_spares);
4474 spa_aux_check_removed(&spa->spa_l2cache);
4477 if (spa_writeable(spa)) {
4479 * Update the config cache to include the newly-imported pool.
4481 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4485 * It's possible that the pool was expanded while it was exported.
4486 * We kick off an async task to handle this for us.
4488 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4490 spa_history_log_version(spa, "import");
4492 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4494 mutex_exit(&spa_namespace_lock);
4498 zvol_create_minors(pool);
4505 spa_tryimport(nvlist_t *tryconfig)
4507 nvlist_t *config = NULL;
4513 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4516 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4520 * Create and initialize the spa structure.
4522 mutex_enter(&spa_namespace_lock);
4523 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4524 spa_activate(spa, FREAD);
4527 * Pass off the heavy lifting to spa_load().
4528 * Pass TRUE for mosconfig because the user-supplied config
4529 * is actually the one to trust when doing an import.
4531 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4534 * If 'tryconfig' was at least parsable, return the current config.
4536 if (spa->spa_root_vdev != NULL) {
4537 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4538 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4540 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4542 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4543 spa->spa_uberblock.ub_timestamp) == 0);
4544 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4545 spa->spa_load_info) == 0);
4548 * If the bootfs property exists on this pool then we
4549 * copy it out so that external consumers can tell which
4550 * pools are bootable.
4552 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4553 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4556 * We have to play games with the name since the
4557 * pool was opened as TRYIMPORT_NAME.
4559 if (dsl_dsobj_to_dsname(spa_name(spa),
4560 spa->spa_bootfs, tmpname) == 0) {
4562 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4564 cp = strchr(tmpname, '/');
4566 (void) strlcpy(dsname, tmpname,
4569 (void) snprintf(dsname, MAXPATHLEN,
4570 "%s/%s", poolname, ++cp);
4572 VERIFY(nvlist_add_string(config,
4573 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4574 kmem_free(dsname, MAXPATHLEN);
4576 kmem_free(tmpname, MAXPATHLEN);
4580 * Add the list of hot spares and level 2 cache devices.
4582 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4583 spa_add_spares(spa, config);
4584 spa_add_l2cache(spa, config);
4585 spa_config_exit(spa, SCL_CONFIG, FTAG);
4589 spa_deactivate(spa);
4591 mutex_exit(&spa_namespace_lock);
4597 * Pool export/destroy
4599 * The act of destroying or exporting a pool is very simple. We make sure there
4600 * is no more pending I/O and any references to the pool are gone. Then, we
4601 * update the pool state and sync all the labels to disk, removing the
4602 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4603 * we don't sync the labels or remove the configuration cache.
4606 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4607 boolean_t force, boolean_t hardforce)
4614 if (!(spa_mode_global & FWRITE))
4615 return (SET_ERROR(EROFS));
4617 mutex_enter(&spa_namespace_lock);
4618 if ((spa = spa_lookup(pool)) == NULL) {
4619 mutex_exit(&spa_namespace_lock);
4620 return (SET_ERROR(ENOENT));
4624 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4625 * reacquire the namespace lock, and see if we can export.
4627 spa_open_ref(spa, FTAG);
4628 mutex_exit(&spa_namespace_lock);
4629 spa_async_suspend(spa);
4630 mutex_enter(&spa_namespace_lock);
4631 spa_close(spa, FTAG);
4634 * The pool will be in core if it's openable,
4635 * in which case we can modify its state.
4637 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4639 * Objsets may be open only because they're dirty, so we
4640 * have to force it to sync before checking spa_refcnt.
4642 txg_wait_synced(spa->spa_dsl_pool, 0);
4643 spa_evicting_os_wait(spa);
4646 * A pool cannot be exported or destroyed if there are active
4647 * references. If we are resetting a pool, allow references by
4648 * fault injection handlers.
4650 if (!spa_refcount_zero(spa) ||
4651 (spa->spa_inject_ref != 0 &&
4652 new_state != POOL_STATE_UNINITIALIZED)) {
4653 spa_async_resume(spa);
4654 mutex_exit(&spa_namespace_lock);
4655 return (SET_ERROR(EBUSY));
4659 * A pool cannot be exported if it has an active shared spare.
4660 * This is to prevent other pools stealing the active spare
4661 * from an exported pool. At user's own will, such pool can
4662 * be forcedly exported.
4664 if (!force && new_state == POOL_STATE_EXPORTED &&
4665 spa_has_active_shared_spare(spa)) {
4666 spa_async_resume(spa);
4667 mutex_exit(&spa_namespace_lock);
4668 return (SET_ERROR(EXDEV));
4672 * We want this to be reflected on every label,
4673 * so mark them all dirty. spa_unload() will do the
4674 * final sync that pushes these changes out.
4676 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4677 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4678 spa->spa_state = new_state;
4679 spa->spa_final_txg = spa_last_synced_txg(spa) +
4681 vdev_config_dirty(spa->spa_root_vdev);
4682 spa_config_exit(spa, SCL_ALL, FTAG);
4686 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4688 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4690 spa_deactivate(spa);
4693 if (oldconfig && spa->spa_config)
4694 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4696 if (new_state != POOL_STATE_UNINITIALIZED) {
4698 spa_config_sync(spa, B_TRUE, B_TRUE);
4701 mutex_exit(&spa_namespace_lock);
4707 * Destroy a storage pool.
4710 spa_destroy(char *pool)
4712 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4717 * Export a storage pool.
4720 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4721 boolean_t hardforce)
4723 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4728 * Similar to spa_export(), this unloads the spa_t without actually removing it
4729 * from the namespace in any way.
4732 spa_reset(char *pool)
4734 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4739 * ==========================================================================
4740 * Device manipulation
4741 * ==========================================================================
4745 * Add a device to a storage pool.
4748 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4752 vdev_t *rvd = spa->spa_root_vdev;
4754 nvlist_t **spares, **l2cache;
4755 uint_t nspares, nl2cache;
4757 ASSERT(spa_writeable(spa));
4759 txg = spa_vdev_enter(spa);
4761 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4762 VDEV_ALLOC_ADD)) != 0)
4763 return (spa_vdev_exit(spa, NULL, txg, error));
4765 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4767 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4771 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4775 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4776 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4778 if (vd->vdev_children != 0 &&
4779 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4780 return (spa_vdev_exit(spa, vd, txg, error));
4783 * We must validate the spares and l2cache devices after checking the
4784 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4786 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4787 return (spa_vdev_exit(spa, vd, txg, error));
4790 * Transfer each new top-level vdev from vd to rvd.
4792 for (int c = 0; c < vd->vdev_children; c++) {
4795 * Set the vdev id to the first hole, if one exists.
4797 for (id = 0; id < rvd->vdev_children; id++) {
4798 if (rvd->vdev_child[id]->vdev_ishole) {
4799 vdev_free(rvd->vdev_child[id]);
4803 tvd = vd->vdev_child[c];
4804 vdev_remove_child(vd, tvd);
4806 vdev_add_child(rvd, tvd);
4807 vdev_config_dirty(tvd);
4811 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4812 ZPOOL_CONFIG_SPARES);
4813 spa_load_spares(spa);
4814 spa->spa_spares.sav_sync = B_TRUE;
4817 if (nl2cache != 0) {
4818 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4819 ZPOOL_CONFIG_L2CACHE);
4820 spa_load_l2cache(spa);
4821 spa->spa_l2cache.sav_sync = B_TRUE;
4825 * We have to be careful when adding new vdevs to an existing pool.
4826 * If other threads start allocating from these vdevs before we
4827 * sync the config cache, and we lose power, then upon reboot we may
4828 * fail to open the pool because there are DVAs that the config cache
4829 * can't translate. Therefore, we first add the vdevs without
4830 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4831 * and then let spa_config_update() initialize the new metaslabs.
4833 * spa_load() checks for added-but-not-initialized vdevs, so that
4834 * if we lose power at any point in this sequence, the remaining
4835 * steps will be completed the next time we load the pool.
4837 (void) spa_vdev_exit(spa, vd, txg, 0);
4839 mutex_enter(&spa_namespace_lock);
4840 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4841 spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD);
4842 mutex_exit(&spa_namespace_lock);
4848 * Attach a device to a mirror. The arguments are the path to any device
4849 * in the mirror, and the nvroot for the new device. If the path specifies
4850 * a device that is not mirrored, we automatically insert the mirror vdev.
4852 * If 'replacing' is specified, the new device is intended to replace the
4853 * existing device; in this case the two devices are made into their own
4854 * mirror using the 'replacing' vdev, which is functionally identical to
4855 * the mirror vdev (it actually reuses all the same ops) but has a few
4856 * extra rules: you can't attach to it after it's been created, and upon
4857 * completion of resilvering, the first disk (the one being replaced)
4858 * is automatically detached.
4861 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4863 uint64_t txg, dtl_max_txg;
4864 vdev_t *rvd = spa->spa_root_vdev;
4865 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4867 char *oldvdpath, *newvdpath;
4871 ASSERT(spa_writeable(spa));
4873 txg = spa_vdev_enter(spa);
4875 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4878 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4880 if (!oldvd->vdev_ops->vdev_op_leaf)
4881 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4883 pvd = oldvd->vdev_parent;
4885 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4886 VDEV_ALLOC_ATTACH)) != 0)
4887 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4889 if (newrootvd->vdev_children != 1)
4890 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4892 newvd = newrootvd->vdev_child[0];
4894 if (!newvd->vdev_ops->vdev_op_leaf)
4895 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4897 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4898 return (spa_vdev_exit(spa, newrootvd, txg, error));
4901 * Spares can't replace logs
4903 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4904 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4908 * For attach, the only allowable parent is a mirror or the root
4911 if (pvd->vdev_ops != &vdev_mirror_ops &&
4912 pvd->vdev_ops != &vdev_root_ops)
4913 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4915 pvops = &vdev_mirror_ops;
4918 * Active hot spares can only be replaced by inactive hot
4921 if (pvd->vdev_ops == &vdev_spare_ops &&
4922 oldvd->vdev_isspare &&
4923 !spa_has_spare(spa, newvd->vdev_guid))
4924 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4927 * If the source is a hot spare, and the parent isn't already a
4928 * spare, then we want to create a new hot spare. Otherwise, we
4929 * want to create a replacing vdev. The user is not allowed to
4930 * attach to a spared vdev child unless the 'isspare' state is
4931 * the same (spare replaces spare, non-spare replaces
4934 if (pvd->vdev_ops == &vdev_replacing_ops &&
4935 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4936 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4937 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4938 newvd->vdev_isspare != oldvd->vdev_isspare) {
4939 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4942 if (newvd->vdev_isspare)
4943 pvops = &vdev_spare_ops;
4945 pvops = &vdev_replacing_ops;
4949 * Make sure the new device is big enough.
4951 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4952 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4955 * The new device cannot have a higher alignment requirement
4956 * than the top-level vdev.
4958 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4959 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4962 * If this is an in-place replacement, update oldvd's path and devid
4963 * to make it distinguishable from newvd, and unopenable from now on.
4965 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4966 spa_strfree(oldvd->vdev_path);
4967 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4969 (void) sprintf(oldvd->vdev_path, "%s/%s",
4970 newvd->vdev_path, "old");
4971 if (oldvd->vdev_devid != NULL) {
4972 spa_strfree(oldvd->vdev_devid);
4973 oldvd->vdev_devid = NULL;
4977 /* mark the device being resilvered */
4978 newvd->vdev_resilver_txg = txg;
4981 * If the parent is not a mirror, or if we're replacing, insert the new
4982 * mirror/replacing/spare vdev above oldvd.
4984 if (pvd->vdev_ops != pvops)
4985 pvd = vdev_add_parent(oldvd, pvops);
4987 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4988 ASSERT(pvd->vdev_ops == pvops);
4989 ASSERT(oldvd->vdev_parent == pvd);
4992 * Extract the new device from its root and add it to pvd.
4994 vdev_remove_child(newrootvd, newvd);
4995 newvd->vdev_id = pvd->vdev_children;
4996 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4997 vdev_add_child(pvd, newvd);
4999 tvd = newvd->vdev_top;
5000 ASSERT(pvd->vdev_top == tvd);
5001 ASSERT(tvd->vdev_parent == rvd);
5003 vdev_config_dirty(tvd);
5006 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
5007 * for any dmu_sync-ed blocks. It will propagate upward when
5008 * spa_vdev_exit() calls vdev_dtl_reassess().
5010 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
5012 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
5013 dtl_max_txg - TXG_INITIAL);
5015 if (newvd->vdev_isspare) {
5016 spa_spare_activate(newvd);
5017 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
5020 oldvdpath = spa_strdup(oldvd->vdev_path);
5021 newvdpath = spa_strdup(newvd->vdev_path);
5022 newvd_isspare = newvd->vdev_isspare;
5025 * Mark newvd's DTL dirty in this txg.
5027 vdev_dirty(tvd, VDD_DTL, newvd, txg);
5030 * Schedule the resilver to restart in the future. We do this to
5031 * ensure that dmu_sync-ed blocks have been stitched into the
5032 * respective datasets.
5034 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
5036 if (spa->spa_bootfs)
5037 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
5039 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH);
5044 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
5046 spa_history_log_internal(spa, "vdev attach", NULL,
5047 "%s vdev=%s %s vdev=%s",
5048 replacing && newvd_isspare ? "spare in" :
5049 replacing ? "replace" : "attach", newvdpath,
5050 replacing ? "for" : "to", oldvdpath);
5052 spa_strfree(oldvdpath);
5053 spa_strfree(newvdpath);
5059 * Detach a device from a mirror or replacing vdev.
5061 * If 'replace_done' is specified, only detach if the parent
5062 * is a replacing vdev.
5065 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
5069 vdev_t *rvd = spa->spa_root_vdev;
5070 vdev_t *vd, *pvd, *cvd, *tvd;
5071 boolean_t unspare = B_FALSE;
5072 uint64_t unspare_guid = 0;
5075 ASSERT(spa_writeable(spa));
5077 txg = spa_vdev_enter(spa);
5079 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5082 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
5084 if (!vd->vdev_ops->vdev_op_leaf)
5085 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5087 pvd = vd->vdev_parent;
5090 * If the parent/child relationship is not as expected, don't do it.
5091 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
5092 * vdev that's replacing B with C. The user's intent in replacing
5093 * is to go from M(A,B) to M(A,C). If the user decides to cancel
5094 * the replace by detaching C, the expected behavior is to end up
5095 * M(A,B). But suppose that right after deciding to detach C,
5096 * the replacement of B completes. We would have M(A,C), and then
5097 * ask to detach C, which would leave us with just A -- not what
5098 * the user wanted. To prevent this, we make sure that the
5099 * parent/child relationship hasn't changed -- in this example,
5100 * that C's parent is still the replacing vdev R.
5102 if (pvd->vdev_guid != pguid && pguid != 0)
5103 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5106 * Only 'replacing' or 'spare' vdevs can be replaced.
5108 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
5109 pvd->vdev_ops != &vdev_spare_ops)
5110 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5112 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
5113 spa_version(spa) >= SPA_VERSION_SPARES);
5116 * Only mirror, replacing, and spare vdevs support detach.
5118 if (pvd->vdev_ops != &vdev_replacing_ops &&
5119 pvd->vdev_ops != &vdev_mirror_ops &&
5120 pvd->vdev_ops != &vdev_spare_ops)
5121 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5124 * If this device has the only valid copy of some data,
5125 * we cannot safely detach it.
5127 if (vdev_dtl_required(vd))
5128 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5130 ASSERT(pvd->vdev_children >= 2);
5133 * If we are detaching the second disk from a replacing vdev, then
5134 * check to see if we changed the original vdev's path to have "/old"
5135 * at the end in spa_vdev_attach(). If so, undo that change now.
5137 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
5138 vd->vdev_path != NULL) {
5139 size_t len = strlen(vd->vdev_path);
5141 for (int c = 0; c < pvd->vdev_children; c++) {
5142 cvd = pvd->vdev_child[c];
5144 if (cvd == vd || cvd->vdev_path == NULL)
5147 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
5148 strcmp(cvd->vdev_path + len, "/old") == 0) {
5149 spa_strfree(cvd->vdev_path);
5150 cvd->vdev_path = spa_strdup(vd->vdev_path);
5157 * If we are detaching the original disk from a spare, then it implies
5158 * that the spare should become a real disk, and be removed from the
5159 * active spare list for the pool.
5161 if (pvd->vdev_ops == &vdev_spare_ops &&
5163 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
5167 * Erase the disk labels so the disk can be used for other things.
5168 * This must be done after all other error cases are handled,
5169 * but before we disembowel vd (so we can still do I/O to it).
5170 * But if we can't do it, don't treat the error as fatal --
5171 * it may be that the unwritability of the disk is the reason
5172 * it's being detached!
5174 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5177 * Remove vd from its parent and compact the parent's children.
5179 vdev_remove_child(pvd, vd);
5180 vdev_compact_children(pvd);
5183 * Remember one of the remaining children so we can get tvd below.
5185 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5188 * If we need to remove the remaining child from the list of hot spares,
5189 * do it now, marking the vdev as no longer a spare in the process.
5190 * We must do this before vdev_remove_parent(), because that can
5191 * change the GUID if it creates a new toplevel GUID. For a similar
5192 * reason, we must remove the spare now, in the same txg as the detach;
5193 * otherwise someone could attach a new sibling, change the GUID, and
5194 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5197 ASSERT(cvd->vdev_isspare);
5198 spa_spare_remove(cvd);
5199 unspare_guid = cvd->vdev_guid;
5200 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5201 cvd->vdev_unspare = B_TRUE;
5205 * If the parent mirror/replacing vdev only has one child,
5206 * the parent is no longer needed. Remove it from the tree.
5208 if (pvd->vdev_children == 1) {
5209 if (pvd->vdev_ops == &vdev_spare_ops)
5210 cvd->vdev_unspare = B_FALSE;
5211 vdev_remove_parent(cvd);
5216 * We don't set tvd until now because the parent we just removed
5217 * may have been the previous top-level vdev.
5219 tvd = cvd->vdev_top;
5220 ASSERT(tvd->vdev_parent == rvd);
5223 * Reevaluate the parent vdev state.
5225 vdev_propagate_state(cvd);
5228 * If the 'autoexpand' property is set on the pool then automatically
5229 * try to expand the size of the pool. For example if the device we
5230 * just detached was smaller than the others, it may be possible to
5231 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5232 * first so that we can obtain the updated sizes of the leaf vdevs.
5234 if (spa->spa_autoexpand) {
5236 vdev_expand(tvd, txg);
5239 vdev_config_dirty(tvd);
5242 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5243 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5244 * But first make sure we're not on any *other* txg's DTL list, to
5245 * prevent vd from being accessed after it's freed.
5247 vdpath = spa_strdup(vd->vdev_path);
5248 for (int t = 0; t < TXG_SIZE; t++)
5249 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5250 vd->vdev_detached = B_TRUE;
5251 vdev_dirty(tvd, VDD_DTL, vd, txg);
5253 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5255 /* hang on to the spa before we release the lock */
5256 spa_open_ref(spa, FTAG);
5258 error = spa_vdev_exit(spa, vd, txg, 0);
5260 spa_history_log_internal(spa, "detach", NULL,
5262 spa_strfree(vdpath);
5265 * If this was the removal of the original device in a hot spare vdev,
5266 * then we want to go through and remove the device from the hot spare
5267 * list of every other pool.
5270 spa_t *altspa = NULL;
5272 mutex_enter(&spa_namespace_lock);
5273 while ((altspa = spa_next(altspa)) != NULL) {
5274 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5278 spa_open_ref(altspa, FTAG);
5279 mutex_exit(&spa_namespace_lock);
5280 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5281 mutex_enter(&spa_namespace_lock);
5282 spa_close(altspa, FTAG);
5284 mutex_exit(&spa_namespace_lock);
5286 /* search the rest of the vdevs for spares to remove */
5287 spa_vdev_resilver_done(spa);
5290 /* all done with the spa; OK to release */
5291 mutex_enter(&spa_namespace_lock);
5292 spa_close(spa, FTAG);
5293 mutex_exit(&spa_namespace_lock);
5299 * Split a set of devices from their mirrors, and create a new pool from them.
5302 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5303 nvlist_t *props, boolean_t exp)
5306 uint64_t txg, *glist;
5308 uint_t c, children, lastlog;
5309 nvlist_t **child, *nvl, *tmp;
5311 char *altroot = NULL;
5312 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5313 boolean_t activate_slog;
5315 ASSERT(spa_writeable(spa));
5317 txg = spa_vdev_enter(spa);
5319 /* clear the log and flush everything up to now */
5320 activate_slog = spa_passivate_log(spa);
5321 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5322 error = spa_offline_log(spa);
5323 txg = spa_vdev_config_enter(spa);
5326 spa_activate_log(spa);
5329 return (spa_vdev_exit(spa, NULL, txg, error));
5331 /* check new spa name before going any further */
5332 if (spa_lookup(newname) != NULL)
5333 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5336 * scan through all the children to ensure they're all mirrors
5338 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5339 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5341 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5343 /* first, check to ensure we've got the right child count */
5344 rvd = spa->spa_root_vdev;
5346 for (c = 0; c < rvd->vdev_children; c++) {
5347 vdev_t *vd = rvd->vdev_child[c];
5349 /* don't count the holes & logs as children */
5350 if (vd->vdev_islog || vd->vdev_ishole) {
5358 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5359 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5361 /* next, ensure no spare or cache devices are part of the split */
5362 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5363 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5364 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5366 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5367 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5369 /* then, loop over each vdev and validate it */
5370 for (c = 0; c < children; c++) {
5371 uint64_t is_hole = 0;
5373 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5377 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5378 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5381 error = SET_ERROR(EINVAL);
5386 /* which disk is going to be split? */
5387 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5389 error = SET_ERROR(EINVAL);
5393 /* look it up in the spa */
5394 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5395 if (vml[c] == NULL) {
5396 error = SET_ERROR(ENODEV);
5400 /* make sure there's nothing stopping the split */
5401 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5402 vml[c]->vdev_islog ||
5403 vml[c]->vdev_ishole ||
5404 vml[c]->vdev_isspare ||
5405 vml[c]->vdev_isl2cache ||
5406 !vdev_writeable(vml[c]) ||
5407 vml[c]->vdev_children != 0 ||
5408 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5409 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5410 error = SET_ERROR(EINVAL);
5414 if (vdev_dtl_required(vml[c])) {
5415 error = SET_ERROR(EBUSY);
5419 /* we need certain info from the top level */
5420 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5421 vml[c]->vdev_top->vdev_ms_array) == 0);
5422 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5423 vml[c]->vdev_top->vdev_ms_shift) == 0);
5424 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5425 vml[c]->vdev_top->vdev_asize) == 0);
5426 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5427 vml[c]->vdev_top->vdev_ashift) == 0);
5429 /* transfer per-vdev ZAPs */
5430 ASSERT3U(vml[c]->vdev_leaf_zap, !=, 0);
5431 VERIFY0(nvlist_add_uint64(child[c],
5432 ZPOOL_CONFIG_VDEV_LEAF_ZAP, vml[c]->vdev_leaf_zap));
5434 ASSERT3U(vml[c]->vdev_top->vdev_top_zap, !=, 0);
5435 VERIFY0(nvlist_add_uint64(child[c],
5436 ZPOOL_CONFIG_VDEV_TOP_ZAP,
5437 vml[c]->vdev_parent->vdev_top_zap));
5441 kmem_free(vml, children * sizeof (vdev_t *));
5442 kmem_free(glist, children * sizeof (uint64_t));
5443 return (spa_vdev_exit(spa, NULL, txg, error));
5446 /* stop writers from using the disks */
5447 for (c = 0; c < children; c++) {
5449 vml[c]->vdev_offline = B_TRUE;
5451 vdev_reopen(spa->spa_root_vdev);
5454 * Temporarily record the splitting vdevs in the spa config. This
5455 * will disappear once the config is regenerated.
5457 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5458 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5459 glist, children) == 0);
5460 kmem_free(glist, children * sizeof (uint64_t));
5462 mutex_enter(&spa->spa_props_lock);
5463 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5465 mutex_exit(&spa->spa_props_lock);
5466 spa->spa_config_splitting = nvl;
5467 vdev_config_dirty(spa->spa_root_vdev);
5469 /* configure and create the new pool */
5470 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5471 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5472 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5473 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5474 spa_version(spa)) == 0);
5475 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5476 spa->spa_config_txg) == 0);
5477 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5478 spa_generate_guid(NULL)) == 0);
5479 VERIFY0(nvlist_add_boolean(config, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS));
5480 (void) nvlist_lookup_string(props,
5481 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5483 /* add the new pool to the namespace */
5484 newspa = spa_add(newname, config, altroot);
5485 newspa->spa_avz_action = AVZ_ACTION_REBUILD;
5486 newspa->spa_config_txg = spa->spa_config_txg;
5487 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5489 /* release the spa config lock, retaining the namespace lock */
5490 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5492 if (zio_injection_enabled)
5493 zio_handle_panic_injection(spa, FTAG, 1);
5495 spa_activate(newspa, spa_mode_global);
5496 spa_async_suspend(newspa);
5499 /* mark that we are creating new spa by splitting */
5500 newspa->spa_splitting_newspa = B_TRUE;
5502 /* create the new pool from the disks of the original pool */
5503 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5505 newspa->spa_splitting_newspa = B_FALSE;
5510 /* if that worked, generate a real config for the new pool */
5511 if (newspa->spa_root_vdev != NULL) {
5512 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5513 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5514 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5515 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5516 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5521 if (props != NULL) {
5522 spa_configfile_set(newspa, props, B_FALSE);
5523 error = spa_prop_set(newspa, props);
5528 /* flush everything */
5529 txg = spa_vdev_config_enter(newspa);
5530 vdev_config_dirty(newspa->spa_root_vdev);
5531 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5533 if (zio_injection_enabled)
5534 zio_handle_panic_injection(spa, FTAG, 2);
5536 spa_async_resume(newspa);
5538 /* finally, update the original pool's config */
5539 txg = spa_vdev_config_enter(spa);
5540 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5541 error = dmu_tx_assign(tx, TXG_WAIT);
5544 for (c = 0; c < children; c++) {
5545 if (vml[c] != NULL) {
5548 spa_history_log_internal(spa, "detach", tx,
5549 "vdev=%s", vml[c]->vdev_path);
5554 spa->spa_avz_action = AVZ_ACTION_REBUILD;
5555 vdev_config_dirty(spa->spa_root_vdev);
5556 spa->spa_config_splitting = NULL;
5560 (void) spa_vdev_exit(spa, NULL, txg, 0);
5562 if (zio_injection_enabled)
5563 zio_handle_panic_injection(spa, FTAG, 3);
5565 /* split is complete; log a history record */
5566 spa_history_log_internal(newspa, "split", NULL,
5567 "from pool %s", spa_name(spa));
5569 kmem_free(vml, children * sizeof (vdev_t *));
5571 /* if we're not going to mount the filesystems in userland, export */
5573 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5580 spa_deactivate(newspa);
5583 txg = spa_vdev_config_enter(spa);
5585 /* re-online all offlined disks */
5586 for (c = 0; c < children; c++) {
5588 vml[c]->vdev_offline = B_FALSE;
5590 vdev_reopen(spa->spa_root_vdev);
5592 nvlist_free(spa->spa_config_splitting);
5593 spa->spa_config_splitting = NULL;
5594 (void) spa_vdev_exit(spa, NULL, txg, error);
5596 kmem_free(vml, children * sizeof (vdev_t *));
5601 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5603 for (int i = 0; i < count; i++) {
5606 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5609 if (guid == target_guid)
5617 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5618 nvlist_t *dev_to_remove)
5620 nvlist_t **newdev = NULL;
5623 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5625 for (int i = 0, j = 0; i < count; i++) {
5626 if (dev[i] == dev_to_remove)
5628 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5631 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5632 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5634 for (int i = 0; i < count - 1; i++)
5635 nvlist_free(newdev[i]);
5638 kmem_free(newdev, (count - 1) * sizeof (void *));
5642 * Evacuate the device.
5645 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5650 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5651 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5652 ASSERT(vd == vd->vdev_top);
5655 * Evacuate the device. We don't hold the config lock as writer
5656 * since we need to do I/O but we do keep the
5657 * spa_namespace_lock held. Once this completes the device
5658 * should no longer have any blocks allocated on it.
5660 if (vd->vdev_islog) {
5661 if (vd->vdev_stat.vs_alloc != 0)
5662 error = spa_offline_log(spa);
5664 error = SET_ERROR(ENOTSUP);
5671 * The evacuation succeeded. Remove any remaining MOS metadata
5672 * associated with this vdev, and wait for these changes to sync.
5674 ASSERT0(vd->vdev_stat.vs_alloc);
5675 txg = spa_vdev_config_enter(spa);
5676 vd->vdev_removing = B_TRUE;
5677 vdev_dirty_leaves(vd, VDD_DTL, txg);
5678 vdev_config_dirty(vd);
5679 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5685 * Complete the removal by cleaning up the namespace.
5688 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5690 vdev_t *rvd = spa->spa_root_vdev;
5691 uint64_t id = vd->vdev_id;
5692 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5694 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5695 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5696 ASSERT(vd == vd->vdev_top);
5699 * Only remove any devices which are empty.
5701 if (vd->vdev_stat.vs_alloc != 0)
5704 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5706 if (list_link_active(&vd->vdev_state_dirty_node))
5707 vdev_state_clean(vd);
5708 if (list_link_active(&vd->vdev_config_dirty_node))
5709 vdev_config_clean(vd);
5714 vdev_compact_children(rvd);
5716 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5717 vdev_add_child(rvd, vd);
5719 vdev_config_dirty(rvd);
5722 * Reassess the health of our root vdev.
5728 * Remove a device from the pool -
5730 * Removing a device from the vdev namespace requires several steps
5731 * and can take a significant amount of time. As a result we use
5732 * the spa_vdev_config_[enter/exit] functions which allow us to
5733 * grab and release the spa_config_lock while still holding the namespace
5734 * lock. During each step the configuration is synced out.
5736 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5740 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5743 sysevent_t *ev = NULL;
5744 metaslab_group_t *mg;
5745 nvlist_t **spares, **l2cache, *nv;
5747 uint_t nspares, nl2cache;
5749 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5751 ASSERT(spa_writeable(spa));
5754 txg = spa_vdev_enter(spa);
5756 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5758 if (spa->spa_spares.sav_vdevs != NULL &&
5759 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5760 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5761 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5763 * Only remove the hot spare if it's not currently in use
5766 if (vd == NULL || unspare) {
5768 vd = spa_lookup_by_guid(spa, guid, B_TRUE);
5769 ev = spa_event_create(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX);
5770 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5771 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5772 spa_load_spares(spa);
5773 spa->spa_spares.sav_sync = B_TRUE;
5775 error = SET_ERROR(EBUSY);
5777 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5778 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5779 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5780 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5782 * Cache devices can always be removed.
5784 vd = spa_lookup_by_guid(spa, guid, B_TRUE);
5785 ev = spa_event_create(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX);
5786 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5787 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5788 spa_load_l2cache(spa);
5789 spa->spa_l2cache.sav_sync = B_TRUE;
5790 } else if (vd != NULL && vd->vdev_islog) {
5792 ASSERT(vd == vd->vdev_top);
5797 * Stop allocating from this vdev.
5799 metaslab_group_passivate(mg);
5802 * Wait for the youngest allocations and frees to sync,
5803 * and then wait for the deferral of those frees to finish.
5805 spa_vdev_config_exit(spa, NULL,
5806 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5809 * Attempt to evacuate the vdev.
5811 error = spa_vdev_remove_evacuate(spa, vd);
5813 txg = spa_vdev_config_enter(spa);
5816 * If we couldn't evacuate the vdev, unwind.
5819 metaslab_group_activate(mg);
5820 return (spa_vdev_exit(spa, NULL, txg, error));
5824 * Clean up the vdev namespace.
5826 ev = spa_event_create(spa, vd, ESC_ZFS_VDEV_REMOVE_DEV);
5827 spa_vdev_remove_from_namespace(spa, vd);
5829 } else if (vd != NULL) {
5831 * Normal vdevs cannot be removed (yet).
5833 error = SET_ERROR(ENOTSUP);
5836 * There is no vdev of any kind with the specified guid.
5838 error = SET_ERROR(ENOENT);
5842 error = spa_vdev_exit(spa, NULL, txg, error);
5851 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5852 * currently spared, so we can detach it.
5855 spa_vdev_resilver_done_hunt(vdev_t *vd)
5857 vdev_t *newvd, *oldvd;
5859 for (int c = 0; c < vd->vdev_children; c++) {
5860 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5866 * Check for a completed replacement. We always consider the first
5867 * vdev in the list to be the oldest vdev, and the last one to be
5868 * the newest (see spa_vdev_attach() for how that works). In
5869 * the case where the newest vdev is faulted, we will not automatically
5870 * remove it after a resilver completes. This is OK as it will require
5871 * user intervention to determine which disk the admin wishes to keep.
5873 if (vd->vdev_ops == &vdev_replacing_ops) {
5874 ASSERT(vd->vdev_children > 1);
5876 newvd = vd->vdev_child[vd->vdev_children - 1];
5877 oldvd = vd->vdev_child[0];
5879 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5880 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5881 !vdev_dtl_required(oldvd))
5886 * Check for a completed resilver with the 'unspare' flag set.
5888 if (vd->vdev_ops == &vdev_spare_ops) {
5889 vdev_t *first = vd->vdev_child[0];
5890 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5892 if (last->vdev_unspare) {
5895 } else if (first->vdev_unspare) {
5902 if (oldvd != NULL &&
5903 vdev_dtl_empty(newvd, DTL_MISSING) &&
5904 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5905 !vdev_dtl_required(oldvd))
5909 * If there are more than two spares attached to a disk,
5910 * and those spares are not required, then we want to
5911 * attempt to free them up now so that they can be used
5912 * by other pools. Once we're back down to a single
5913 * disk+spare, we stop removing them.
5915 if (vd->vdev_children > 2) {
5916 newvd = vd->vdev_child[1];
5918 if (newvd->vdev_isspare && last->vdev_isspare &&
5919 vdev_dtl_empty(last, DTL_MISSING) &&
5920 vdev_dtl_empty(last, DTL_OUTAGE) &&
5921 !vdev_dtl_required(newvd))
5930 spa_vdev_resilver_done(spa_t *spa)
5932 vdev_t *vd, *pvd, *ppvd;
5933 uint64_t guid, sguid, pguid, ppguid;
5935 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5937 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5938 pvd = vd->vdev_parent;
5939 ppvd = pvd->vdev_parent;
5940 guid = vd->vdev_guid;
5941 pguid = pvd->vdev_guid;
5942 ppguid = ppvd->vdev_guid;
5945 * If we have just finished replacing a hot spared device, then
5946 * we need to detach the parent's first child (the original hot
5949 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5950 ppvd->vdev_children == 2) {
5951 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5952 sguid = ppvd->vdev_child[1]->vdev_guid;
5954 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5956 spa_config_exit(spa, SCL_ALL, FTAG);
5957 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5959 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5961 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5964 spa_config_exit(spa, SCL_ALL, FTAG);
5968 * Update the stored path or FRU for this vdev.
5971 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5975 boolean_t sync = B_FALSE;
5977 ASSERT(spa_writeable(spa));
5979 spa_vdev_state_enter(spa, SCL_ALL);
5981 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5982 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5984 if (!vd->vdev_ops->vdev_op_leaf)
5985 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5988 if (strcmp(value, vd->vdev_path) != 0) {
5989 spa_strfree(vd->vdev_path);
5990 vd->vdev_path = spa_strdup(value);
5994 if (vd->vdev_fru == NULL) {
5995 vd->vdev_fru = spa_strdup(value);
5997 } else if (strcmp(value, vd->vdev_fru) != 0) {
5998 spa_strfree(vd->vdev_fru);
5999 vd->vdev_fru = spa_strdup(value);
6004 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
6008 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
6010 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
6014 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
6016 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
6020 * ==========================================================================
6022 * ==========================================================================
6026 spa_scan_stop(spa_t *spa)
6028 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
6029 if (dsl_scan_resilvering(spa->spa_dsl_pool))
6030 return (SET_ERROR(EBUSY));
6031 return (dsl_scan_cancel(spa->spa_dsl_pool));
6035 spa_scan(spa_t *spa, pool_scan_func_t func)
6037 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
6039 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
6040 return (SET_ERROR(ENOTSUP));
6043 * If a resilver was requested, but there is no DTL on a
6044 * writeable leaf device, we have nothing to do.
6046 if (func == POOL_SCAN_RESILVER &&
6047 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
6048 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
6052 return (dsl_scan(spa->spa_dsl_pool, func));
6056 * ==========================================================================
6057 * SPA async task processing
6058 * ==========================================================================
6062 spa_async_remove(spa_t *spa, vdev_t *vd)
6064 if (vd->vdev_remove_wanted) {
6065 vd->vdev_remove_wanted = B_FALSE;
6066 vd->vdev_delayed_close = B_FALSE;
6067 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
6070 * We want to clear the stats, but we don't want to do a full
6071 * vdev_clear() as that will cause us to throw away
6072 * degraded/faulted state as well as attempt to reopen the
6073 * device, all of which is a waste.
6075 vd->vdev_stat.vs_read_errors = 0;
6076 vd->vdev_stat.vs_write_errors = 0;
6077 vd->vdev_stat.vs_checksum_errors = 0;
6079 vdev_state_dirty(vd->vdev_top);
6080 /* Tell userspace that the vdev is gone. */
6081 zfs_post_remove(spa, vd);
6084 for (int c = 0; c < vd->vdev_children; c++)
6085 spa_async_remove(spa, vd->vdev_child[c]);
6089 spa_async_probe(spa_t *spa, vdev_t *vd)
6091 if (vd->vdev_probe_wanted) {
6092 vd->vdev_probe_wanted = B_FALSE;
6093 vdev_reopen(vd); /* vdev_open() does the actual probe */
6096 for (int c = 0; c < vd->vdev_children; c++)
6097 spa_async_probe(spa, vd->vdev_child[c]);
6101 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
6107 if (!spa->spa_autoexpand)
6110 for (int c = 0; c < vd->vdev_children; c++) {
6111 vdev_t *cvd = vd->vdev_child[c];
6112 spa_async_autoexpand(spa, cvd);
6115 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
6118 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
6119 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
6121 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6122 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
6124 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
6125 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
6128 kmem_free(physpath, MAXPATHLEN);
6132 spa_async_thread(void *arg)
6137 ASSERT(spa->spa_sync_on);
6139 mutex_enter(&spa->spa_async_lock);
6140 tasks = spa->spa_async_tasks;
6141 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
6142 mutex_exit(&spa->spa_async_lock);
6145 * See if the config needs to be updated.
6147 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
6148 uint64_t old_space, new_space;
6150 mutex_enter(&spa_namespace_lock);
6151 old_space = metaslab_class_get_space(spa_normal_class(spa));
6152 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
6153 new_space = metaslab_class_get_space(spa_normal_class(spa));
6154 mutex_exit(&spa_namespace_lock);
6157 * If the pool grew as a result of the config update,
6158 * then log an internal history event.
6160 if (new_space != old_space) {
6161 spa_history_log_internal(spa, "vdev online", NULL,
6162 "pool '%s' size: %llu(+%llu)",
6163 spa_name(spa), new_space, new_space - old_space);
6167 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
6168 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6169 spa_async_autoexpand(spa, spa->spa_root_vdev);
6170 spa_config_exit(spa, SCL_CONFIG, FTAG);
6174 * See if any devices need to be probed.
6176 if (tasks & SPA_ASYNC_PROBE) {
6177 spa_vdev_state_enter(spa, SCL_NONE);
6178 spa_async_probe(spa, spa->spa_root_vdev);
6179 (void) spa_vdev_state_exit(spa, NULL, 0);
6183 * If any devices are done replacing, detach them.
6185 if (tasks & SPA_ASYNC_RESILVER_DONE)
6186 spa_vdev_resilver_done(spa);
6189 * Kick off a resilver.
6191 if (tasks & SPA_ASYNC_RESILVER)
6192 dsl_resilver_restart(spa->spa_dsl_pool, 0);
6195 * Let the world know that we're done.
6197 mutex_enter(&spa->spa_async_lock);
6198 spa->spa_async_thread = NULL;
6199 cv_broadcast(&spa->spa_async_cv);
6200 mutex_exit(&spa->spa_async_lock);
6205 spa_async_thread_vd(void *arg)
6210 ASSERT(spa->spa_sync_on);
6212 mutex_enter(&spa->spa_async_lock);
6213 tasks = spa->spa_async_tasks;
6215 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6216 mutex_exit(&spa->spa_async_lock);
6219 * See if any devices need to be marked REMOVED.
6221 if (tasks & SPA_ASYNC_REMOVE) {
6222 spa_vdev_state_enter(spa, SCL_NONE);
6223 spa_async_remove(spa, spa->spa_root_vdev);
6224 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6225 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6226 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6227 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6228 (void) spa_vdev_state_exit(spa, NULL, 0);
6232 * Let the world know that we're done.
6234 mutex_enter(&spa->spa_async_lock);
6235 tasks = spa->spa_async_tasks;
6236 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6238 spa->spa_async_thread_vd = NULL;
6239 cv_broadcast(&spa->spa_async_cv);
6240 mutex_exit(&spa->spa_async_lock);
6245 spa_async_suspend(spa_t *spa)
6247 mutex_enter(&spa->spa_async_lock);
6248 spa->spa_async_suspended++;
6249 while (spa->spa_async_thread != NULL &&
6250 spa->spa_async_thread_vd != NULL)
6251 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6252 mutex_exit(&spa->spa_async_lock);
6256 spa_async_resume(spa_t *spa)
6258 mutex_enter(&spa->spa_async_lock);
6259 ASSERT(spa->spa_async_suspended != 0);
6260 spa->spa_async_suspended--;
6261 mutex_exit(&spa->spa_async_lock);
6265 spa_async_tasks_pending(spa_t *spa)
6267 uint_t non_config_tasks;
6269 boolean_t config_task_suspended;
6271 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6273 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6274 if (spa->spa_ccw_fail_time == 0) {
6275 config_task_suspended = B_FALSE;
6277 config_task_suspended =
6278 (gethrtime() - spa->spa_ccw_fail_time) <
6279 (zfs_ccw_retry_interval * NANOSEC);
6282 return (non_config_tasks || (config_task && !config_task_suspended));
6286 spa_async_dispatch(spa_t *spa)
6288 mutex_enter(&spa->spa_async_lock);
6289 if (spa_async_tasks_pending(spa) &&
6290 !spa->spa_async_suspended &&
6291 spa->spa_async_thread == NULL &&
6293 spa->spa_async_thread = thread_create(NULL, 0,
6294 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6295 mutex_exit(&spa->spa_async_lock);
6299 spa_async_dispatch_vd(spa_t *spa)
6301 mutex_enter(&spa->spa_async_lock);
6302 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6303 !spa->spa_async_suspended &&
6304 spa->spa_async_thread_vd == NULL &&
6306 spa->spa_async_thread_vd = thread_create(NULL, 0,
6307 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6308 mutex_exit(&spa->spa_async_lock);
6312 spa_async_request(spa_t *spa, int task)
6314 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6315 mutex_enter(&spa->spa_async_lock);
6316 spa->spa_async_tasks |= task;
6317 mutex_exit(&spa->spa_async_lock);
6318 spa_async_dispatch_vd(spa);
6322 * ==========================================================================
6323 * SPA syncing routines
6324 * ==========================================================================
6328 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6331 bpobj_enqueue(bpo, bp, tx);
6336 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6340 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6341 BP_GET_PSIZE(bp), zio->io_flags));
6346 * Note: this simple function is not inlined to make it easier to dtrace the
6347 * amount of time spent syncing frees.
6350 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6352 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6353 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6354 VERIFY(zio_wait(zio) == 0);
6358 * Note: this simple function is not inlined to make it easier to dtrace the
6359 * amount of time spent syncing deferred frees.
6362 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6364 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6365 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6366 spa_free_sync_cb, zio, tx), ==, 0);
6367 VERIFY0(zio_wait(zio));
6372 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6374 char *packed = NULL;
6379 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6382 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6383 * information. This avoids the dmu_buf_will_dirty() path and
6384 * saves us a pre-read to get data we don't actually care about.
6386 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6387 packed = kmem_alloc(bufsize, KM_SLEEP);
6389 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6391 bzero(packed + nvsize, bufsize - nvsize);
6393 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6395 kmem_free(packed, bufsize);
6397 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6398 dmu_buf_will_dirty(db, tx);
6399 *(uint64_t *)db->db_data = nvsize;
6400 dmu_buf_rele(db, FTAG);
6404 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6405 const char *config, const char *entry)
6415 * Update the MOS nvlist describing the list of available devices.
6416 * spa_validate_aux() will have already made sure this nvlist is
6417 * valid and the vdevs are labeled appropriately.
6419 if (sav->sav_object == 0) {
6420 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6421 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6422 sizeof (uint64_t), tx);
6423 VERIFY(zap_update(spa->spa_meta_objset,
6424 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6425 &sav->sav_object, tx) == 0);
6428 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6429 if (sav->sav_count == 0) {
6430 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6432 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6433 for (i = 0; i < sav->sav_count; i++)
6434 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6435 B_FALSE, VDEV_CONFIG_L2CACHE);
6436 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6437 sav->sav_count) == 0);
6438 for (i = 0; i < sav->sav_count; i++)
6439 nvlist_free(list[i]);
6440 kmem_free(list, sav->sav_count * sizeof (void *));
6443 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6444 nvlist_free(nvroot);
6446 sav->sav_sync = B_FALSE;
6450 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6451 * The all-vdev ZAP must be empty.
6454 spa_avz_build(vdev_t *vd, uint64_t avz, dmu_tx_t *tx)
6456 spa_t *spa = vd->vdev_spa;
6457 if (vd->vdev_top_zap != 0) {
6458 VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
6459 vd->vdev_top_zap, tx));
6461 if (vd->vdev_leaf_zap != 0) {
6462 VERIFY0(zap_add_int(spa->spa_meta_objset, avz,
6463 vd->vdev_leaf_zap, tx));
6465 for (uint64_t i = 0; i < vd->vdev_children; i++) {
6466 spa_avz_build(vd->vdev_child[i], avz, tx);
6471 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6476 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6477 * its config may not be dirty but we still need to build per-vdev ZAPs.
6478 * Similarly, if the pool is being assembled (e.g. after a split), we
6479 * need to rebuild the AVZ although the config may not be dirty.
6481 if (list_is_empty(&spa->spa_config_dirty_list) &&
6482 spa->spa_avz_action == AVZ_ACTION_NONE)
6485 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6487 ASSERT(spa->spa_avz_action == AVZ_ACTION_NONE ||
6488 spa->spa_all_vdev_zaps != 0);
6490 if (spa->spa_avz_action == AVZ_ACTION_REBUILD) {
6491 /* Make and build the new AVZ */
6492 uint64_t new_avz = zap_create(spa->spa_meta_objset,
6493 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
6494 spa_avz_build(spa->spa_root_vdev, new_avz, tx);
6496 /* Diff old AVZ with new one */
6500 for (zap_cursor_init(&zc, spa->spa_meta_objset,
6501 spa->spa_all_vdev_zaps);
6502 zap_cursor_retrieve(&zc, &za) == 0;
6503 zap_cursor_advance(&zc)) {
6504 uint64_t vdzap = za.za_first_integer;
6505 if (zap_lookup_int(spa->spa_meta_objset, new_avz,
6508 * ZAP is listed in old AVZ but not in new one;
6511 VERIFY0(zap_destroy(spa->spa_meta_objset, vdzap,
6516 zap_cursor_fini(&zc);
6518 /* Destroy the old AVZ */
6519 VERIFY0(zap_destroy(spa->spa_meta_objset,
6520 spa->spa_all_vdev_zaps, tx));
6522 /* Replace the old AVZ in the dir obj with the new one */
6523 VERIFY0(zap_update(spa->spa_meta_objset,
6524 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP,
6525 sizeof (new_avz), 1, &new_avz, tx));
6527 spa->spa_all_vdev_zaps = new_avz;
6528 } else if (spa->spa_avz_action == AVZ_ACTION_DESTROY) {
6532 /* Walk through the AVZ and destroy all listed ZAPs */
6533 for (zap_cursor_init(&zc, spa->spa_meta_objset,
6534 spa->spa_all_vdev_zaps);
6535 zap_cursor_retrieve(&zc, &za) == 0;
6536 zap_cursor_advance(&zc)) {
6537 uint64_t zap = za.za_first_integer;
6538 VERIFY0(zap_destroy(spa->spa_meta_objset, zap, tx));
6541 zap_cursor_fini(&zc);
6543 /* Destroy and unlink the AVZ itself */
6544 VERIFY0(zap_destroy(spa->spa_meta_objset,
6545 spa->spa_all_vdev_zaps, tx));
6546 VERIFY0(zap_remove(spa->spa_meta_objset,
6547 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_VDEV_ZAP_MAP, tx));
6548 spa->spa_all_vdev_zaps = 0;
6551 if (spa->spa_all_vdev_zaps == 0) {
6552 spa->spa_all_vdev_zaps = zap_create_link(spa->spa_meta_objset,
6553 DMU_OTN_ZAP_METADATA, DMU_POOL_DIRECTORY_OBJECT,
6554 DMU_POOL_VDEV_ZAP_MAP, tx);
6556 spa->spa_avz_action = AVZ_ACTION_NONE;
6558 /* Create ZAPs for vdevs that don't have them. */
6559 vdev_construct_zaps(spa->spa_root_vdev, tx);
6561 config = spa_config_generate(spa, spa->spa_root_vdev,
6562 dmu_tx_get_txg(tx), B_FALSE);
6565 * If we're upgrading the spa version then make sure that
6566 * the config object gets updated with the correct version.
6568 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6569 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6570 spa->spa_uberblock.ub_version);
6572 spa_config_exit(spa, SCL_STATE, FTAG);
6574 nvlist_free(spa->spa_config_syncing);
6575 spa->spa_config_syncing = config;
6577 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6581 spa_sync_version(void *arg, dmu_tx_t *tx)
6583 uint64_t *versionp = arg;
6584 uint64_t version = *versionp;
6585 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6588 * Setting the version is special cased when first creating the pool.
6590 ASSERT(tx->tx_txg != TXG_INITIAL);
6592 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6593 ASSERT(version >= spa_version(spa));
6595 spa->spa_uberblock.ub_version = version;
6596 vdev_config_dirty(spa->spa_root_vdev);
6597 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6601 * Set zpool properties.
6604 spa_sync_props(void *arg, dmu_tx_t *tx)
6606 nvlist_t *nvp = arg;
6607 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6608 objset_t *mos = spa->spa_meta_objset;
6609 nvpair_t *elem = NULL;
6611 mutex_enter(&spa->spa_props_lock);
6613 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6615 char *strval, *fname;
6617 const char *propname;
6618 zprop_type_t proptype;
6621 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6624 * We checked this earlier in spa_prop_validate().
6626 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6628 fname = strchr(nvpair_name(elem), '@') + 1;
6629 VERIFY0(zfeature_lookup_name(fname, &fid));
6631 spa_feature_enable(spa, fid, tx);
6632 spa_history_log_internal(spa, "set", tx,
6633 "%s=enabled", nvpair_name(elem));
6636 case ZPOOL_PROP_VERSION:
6637 intval = fnvpair_value_uint64(elem);
6639 * The version is synced seperatly before other
6640 * properties and should be correct by now.
6642 ASSERT3U(spa_version(spa), >=, intval);
6645 case ZPOOL_PROP_ALTROOT:
6647 * 'altroot' is a non-persistent property. It should
6648 * have been set temporarily at creation or import time.
6650 ASSERT(spa->spa_root != NULL);
6653 case ZPOOL_PROP_READONLY:
6654 case ZPOOL_PROP_CACHEFILE:
6656 * 'readonly' and 'cachefile' are also non-persisitent
6660 case ZPOOL_PROP_COMMENT:
6661 strval = fnvpair_value_string(elem);
6662 if (spa->spa_comment != NULL)
6663 spa_strfree(spa->spa_comment);
6664 spa->spa_comment = spa_strdup(strval);
6666 * We need to dirty the configuration on all the vdevs
6667 * so that their labels get updated. It's unnecessary
6668 * to do this for pool creation since the vdev's
6669 * configuratoin has already been dirtied.
6671 if (tx->tx_txg != TXG_INITIAL)
6672 vdev_config_dirty(spa->spa_root_vdev);
6673 spa_history_log_internal(spa, "set", tx,
6674 "%s=%s", nvpair_name(elem), strval);
6678 * Set pool property values in the poolprops mos object.
6680 if (spa->spa_pool_props_object == 0) {
6681 spa->spa_pool_props_object =
6682 zap_create_link(mos, DMU_OT_POOL_PROPS,
6683 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6687 /* normalize the property name */
6688 propname = zpool_prop_to_name(prop);
6689 proptype = zpool_prop_get_type(prop);
6691 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6692 ASSERT(proptype == PROP_TYPE_STRING);
6693 strval = fnvpair_value_string(elem);
6694 VERIFY0(zap_update(mos,
6695 spa->spa_pool_props_object, propname,
6696 1, strlen(strval) + 1, strval, tx));
6697 spa_history_log_internal(spa, "set", tx,
6698 "%s=%s", nvpair_name(elem), strval);
6699 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6700 intval = fnvpair_value_uint64(elem);
6702 if (proptype == PROP_TYPE_INDEX) {
6704 VERIFY0(zpool_prop_index_to_string(
6705 prop, intval, &unused));
6707 VERIFY0(zap_update(mos,
6708 spa->spa_pool_props_object, propname,
6709 8, 1, &intval, tx));
6710 spa_history_log_internal(spa, "set", tx,
6711 "%s=%lld", nvpair_name(elem), intval);
6713 ASSERT(0); /* not allowed */
6717 case ZPOOL_PROP_DELEGATION:
6718 spa->spa_delegation = intval;
6720 case ZPOOL_PROP_BOOTFS:
6721 spa->spa_bootfs = intval;
6723 case ZPOOL_PROP_FAILUREMODE:
6724 spa->spa_failmode = intval;
6726 case ZPOOL_PROP_AUTOEXPAND:
6727 spa->spa_autoexpand = intval;
6728 if (tx->tx_txg != TXG_INITIAL)
6729 spa_async_request(spa,
6730 SPA_ASYNC_AUTOEXPAND);
6732 case ZPOOL_PROP_DEDUPDITTO:
6733 spa->spa_dedup_ditto = intval;
6742 mutex_exit(&spa->spa_props_lock);
6746 * Perform one-time upgrade on-disk changes. spa_version() does not
6747 * reflect the new version this txg, so there must be no changes this
6748 * txg to anything that the upgrade code depends on after it executes.
6749 * Therefore this must be called after dsl_pool_sync() does the sync
6753 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6755 dsl_pool_t *dp = spa->spa_dsl_pool;
6757 ASSERT(spa->spa_sync_pass == 1);
6759 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6761 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6762 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6763 dsl_pool_create_origin(dp, tx);
6765 /* Keeping the origin open increases spa_minref */
6766 spa->spa_minref += 3;
6769 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6770 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6771 dsl_pool_upgrade_clones(dp, tx);
6774 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6775 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6776 dsl_pool_upgrade_dir_clones(dp, tx);
6778 /* Keeping the freedir open increases spa_minref */
6779 spa->spa_minref += 3;
6782 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6783 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6784 spa_feature_create_zap_objects(spa, tx);
6788 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6789 * when possibility to use lz4 compression for metadata was added
6790 * Old pools that have this feature enabled must be upgraded to have
6791 * this feature active
6793 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6794 boolean_t lz4_en = spa_feature_is_enabled(spa,
6795 SPA_FEATURE_LZ4_COMPRESS);
6796 boolean_t lz4_ac = spa_feature_is_active(spa,
6797 SPA_FEATURE_LZ4_COMPRESS);
6799 if (lz4_en && !lz4_ac)
6800 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6804 * If we haven't written the salt, do so now. Note that the
6805 * feature may not be activated yet, but that's fine since
6806 * the presence of this ZAP entry is backwards compatible.
6808 if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
6809 DMU_POOL_CHECKSUM_SALT) == ENOENT) {
6810 VERIFY0(zap_add(spa->spa_meta_objset,
6811 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
6812 sizeof (spa->spa_cksum_salt.zcs_bytes),
6813 spa->spa_cksum_salt.zcs_bytes, tx));
6816 rrw_exit(&dp->dp_config_rwlock, FTAG);
6820 * Sync the specified transaction group. New blocks may be dirtied as
6821 * part of the process, so we iterate until it converges.
6824 spa_sync(spa_t *spa, uint64_t txg)
6826 dsl_pool_t *dp = spa->spa_dsl_pool;
6827 objset_t *mos = spa->spa_meta_objset;
6828 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6829 vdev_t *rvd = spa->spa_root_vdev;
6833 uint32_t max_queue_depth = zfs_vdev_async_write_max_active *
6834 zfs_vdev_queue_depth_pct / 100;
6836 VERIFY(spa_writeable(spa));
6839 * Lock out configuration changes.
6841 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6843 spa->spa_syncing_txg = txg;
6844 spa->spa_sync_pass = 0;
6846 mutex_enter(&spa->spa_alloc_lock);
6847 VERIFY0(avl_numnodes(&spa->spa_alloc_tree));
6848 mutex_exit(&spa->spa_alloc_lock);
6851 * If there are any pending vdev state changes, convert them
6852 * into config changes that go out with this transaction group.
6854 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6855 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6857 * We need the write lock here because, for aux vdevs,
6858 * calling vdev_config_dirty() modifies sav_config.
6859 * This is ugly and will become unnecessary when we
6860 * eliminate the aux vdev wart by integrating all vdevs
6861 * into the root vdev tree.
6863 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6864 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6865 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6866 vdev_state_clean(vd);
6867 vdev_config_dirty(vd);
6869 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6870 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6872 spa_config_exit(spa, SCL_STATE, FTAG);
6874 tx = dmu_tx_create_assigned(dp, txg);
6876 spa->spa_sync_starttime = gethrtime();
6878 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6879 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6880 #else /* !illumos */
6882 callout_schedule(&spa->spa_deadman_cycid,
6883 hz * spa->spa_deadman_synctime / NANOSEC);
6885 #endif /* illumos */
6888 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6889 * set spa_deflate if we have no raid-z vdevs.
6891 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6892 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6895 for (i = 0; i < rvd->vdev_children; i++) {
6896 vd = rvd->vdev_child[i];
6897 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6900 if (i == rvd->vdev_children) {
6901 spa->spa_deflate = TRUE;
6902 VERIFY(0 == zap_add(spa->spa_meta_objset,
6903 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6904 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6909 * Set the top-level vdev's max queue depth. Evaluate each
6910 * top-level's async write queue depth in case it changed.
6911 * The max queue depth will not change in the middle of syncing
6914 uint64_t queue_depth_total = 0;
6915 for (int c = 0; c < rvd->vdev_children; c++) {
6916 vdev_t *tvd = rvd->vdev_child[c];
6917 metaslab_group_t *mg = tvd->vdev_mg;
6919 if (mg == NULL || mg->mg_class != spa_normal_class(spa) ||
6920 !metaslab_group_initialized(mg))
6924 * It is safe to do a lock-free check here because only async
6925 * allocations look at mg_max_alloc_queue_depth, and async
6926 * allocations all happen from spa_sync().
6928 ASSERT0(refcount_count(&mg->mg_alloc_queue_depth));
6929 mg->mg_max_alloc_queue_depth = max_queue_depth;
6930 queue_depth_total += mg->mg_max_alloc_queue_depth;
6932 metaslab_class_t *mc = spa_normal_class(spa);
6933 ASSERT0(refcount_count(&mc->mc_alloc_slots));
6934 mc->mc_alloc_max_slots = queue_depth_total;
6935 mc->mc_alloc_throttle_enabled = zio_dva_throttle_enabled;
6937 ASSERT3U(mc->mc_alloc_max_slots, <=,
6938 max_queue_depth * rvd->vdev_children);
6941 * Iterate to convergence.
6944 int pass = ++spa->spa_sync_pass;
6946 spa_sync_config_object(spa, tx);
6947 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6948 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6949 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6950 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6951 spa_errlog_sync(spa, txg);
6952 dsl_pool_sync(dp, txg);
6954 if (pass < zfs_sync_pass_deferred_free) {
6955 spa_sync_frees(spa, free_bpl, tx);
6958 * We can not defer frees in pass 1, because
6959 * we sync the deferred frees later in pass 1.
6961 ASSERT3U(pass, >, 1);
6962 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6963 &spa->spa_deferred_bpobj, tx);
6967 dsl_scan_sync(dp, tx);
6969 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6973 spa_sync_upgrades(spa, tx);
6975 spa->spa_uberblock.ub_rootbp.blk_birth);
6977 * Note: We need to check if the MOS is dirty
6978 * because we could have marked the MOS dirty
6979 * without updating the uberblock (e.g. if we
6980 * have sync tasks but no dirty user data). We
6981 * need to check the uberblock's rootbp because
6982 * it is updated if we have synced out dirty
6983 * data (though in this case the MOS will most
6984 * likely also be dirty due to second order
6985 * effects, we don't want to rely on that here).
6987 if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6988 !dmu_objset_is_dirty(mos, txg)) {
6990 * Nothing changed on the first pass,
6991 * therefore this TXG is a no-op. Avoid
6992 * syncing deferred frees, so that we
6993 * can keep this TXG as a no-op.
6995 ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6997 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6998 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
7001 spa_sync_deferred_frees(spa, tx);
7004 } while (dmu_objset_is_dirty(mos, txg));
7006 if (!list_is_empty(&spa->spa_config_dirty_list)) {
7008 * Make sure that the number of ZAPs for all the vdevs matches
7009 * the number of ZAPs in the per-vdev ZAP list. This only gets
7010 * called if the config is dirty; otherwise there may be
7011 * outstanding AVZ operations that weren't completed in
7012 * spa_sync_config_object.
7014 uint64_t all_vdev_zap_entry_count;
7015 ASSERT0(zap_count(spa->spa_meta_objset,
7016 spa->spa_all_vdev_zaps, &all_vdev_zap_entry_count));
7017 ASSERT3U(vdev_count_verify_zaps(spa->spa_root_vdev), ==,
7018 all_vdev_zap_entry_count);
7022 * Rewrite the vdev configuration (which includes the uberblock)
7023 * to commit the transaction group.
7025 * If there are no dirty vdevs, we sync the uberblock to a few
7026 * random top-level vdevs that are known to be visible in the
7027 * config cache (see spa_vdev_add() for a complete description).
7028 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
7032 * We hold SCL_STATE to prevent vdev open/close/etc.
7033 * while we're attempting to write the vdev labels.
7035 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
7037 if (list_is_empty(&spa->spa_config_dirty_list)) {
7038 vdev_t *svd[SPA_DVAS_PER_BP];
7040 int children = rvd->vdev_children;
7041 int c0 = spa_get_random(children);
7043 for (int c = 0; c < children; c++) {
7044 vd = rvd->vdev_child[(c0 + c) % children];
7045 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
7047 svd[svdcount++] = vd;
7048 if (svdcount == SPA_DVAS_PER_BP)
7051 error = vdev_config_sync(svd, svdcount, txg);
7053 error = vdev_config_sync(rvd->vdev_child,
7054 rvd->vdev_children, txg);
7058 spa->spa_last_synced_guid = rvd->vdev_guid;
7060 spa_config_exit(spa, SCL_STATE, FTAG);
7064 zio_suspend(spa, NULL);
7065 zio_resume_wait(spa);
7070 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
7071 #else /* !illumos */
7073 callout_drain(&spa->spa_deadman_cycid);
7075 #endif /* illumos */
7078 * Clear the dirty config list.
7080 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
7081 vdev_config_clean(vd);
7084 * Now that the new config has synced transactionally,
7085 * let it become visible to the config cache.
7087 if (spa->spa_config_syncing != NULL) {
7088 spa_config_set(spa, spa->spa_config_syncing);
7089 spa->spa_config_txg = txg;
7090 spa->spa_config_syncing = NULL;
7093 dsl_pool_sync_done(dp, txg);
7095 mutex_enter(&spa->spa_alloc_lock);
7096 VERIFY0(avl_numnodes(&spa->spa_alloc_tree));
7097 mutex_exit(&spa->spa_alloc_lock);
7100 * Update usable space statistics.
7102 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
7103 vdev_sync_done(vd, txg);
7105 spa_update_dspace(spa);
7108 * It had better be the case that we didn't dirty anything
7109 * since vdev_config_sync().
7111 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
7112 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
7113 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
7115 spa->spa_sync_pass = 0;
7118 * Update the last synced uberblock here. We want to do this at
7119 * the end of spa_sync() so that consumers of spa_last_synced_txg()
7120 * will be guaranteed that all the processing associated with
7121 * that txg has been completed.
7123 spa->spa_ubsync = spa->spa_uberblock;
7124 spa_config_exit(spa, SCL_CONFIG, FTAG);
7126 spa_handle_ignored_writes(spa);
7129 * If any async tasks have been requested, kick them off.
7131 spa_async_dispatch(spa);
7132 spa_async_dispatch_vd(spa);
7136 * Sync all pools. We don't want to hold the namespace lock across these
7137 * operations, so we take a reference on the spa_t and drop the lock during the
7141 spa_sync_allpools(void)
7144 mutex_enter(&spa_namespace_lock);
7145 while ((spa = spa_next(spa)) != NULL) {
7146 if (spa_state(spa) != POOL_STATE_ACTIVE ||
7147 !spa_writeable(spa) || spa_suspended(spa))
7149 spa_open_ref(spa, FTAG);
7150 mutex_exit(&spa_namespace_lock);
7151 txg_wait_synced(spa_get_dsl(spa), 0);
7152 mutex_enter(&spa_namespace_lock);
7153 spa_close(spa, FTAG);
7155 mutex_exit(&spa_namespace_lock);
7159 * ==========================================================================
7160 * Miscellaneous routines
7161 * ==========================================================================
7165 * Remove all pools in the system.
7173 * Remove all cached state. All pools should be closed now,
7174 * so every spa in the AVL tree should be unreferenced.
7176 mutex_enter(&spa_namespace_lock);
7177 while ((spa = spa_next(NULL)) != NULL) {
7179 * Stop async tasks. The async thread may need to detach
7180 * a device that's been replaced, which requires grabbing
7181 * spa_namespace_lock, so we must drop it here.
7183 spa_open_ref(spa, FTAG);
7184 mutex_exit(&spa_namespace_lock);
7185 spa_async_suspend(spa);
7186 mutex_enter(&spa_namespace_lock);
7187 spa_close(spa, FTAG);
7189 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
7191 spa_deactivate(spa);
7195 mutex_exit(&spa_namespace_lock);
7199 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
7204 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
7208 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
7209 vd = spa->spa_l2cache.sav_vdevs[i];
7210 if (vd->vdev_guid == guid)
7214 for (i = 0; i < spa->spa_spares.sav_count; i++) {
7215 vd = spa->spa_spares.sav_vdevs[i];
7216 if (vd->vdev_guid == guid)
7225 spa_upgrade(spa_t *spa, uint64_t version)
7227 ASSERT(spa_writeable(spa));
7229 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
7232 * This should only be called for a non-faulted pool, and since a
7233 * future version would result in an unopenable pool, this shouldn't be
7236 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
7237 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
7239 spa->spa_uberblock.ub_version = version;
7240 vdev_config_dirty(spa->spa_root_vdev);
7242 spa_config_exit(spa, SCL_ALL, FTAG);
7244 txg_wait_synced(spa_get_dsl(spa), 0);
7248 spa_has_spare(spa_t *spa, uint64_t guid)
7252 spa_aux_vdev_t *sav = &spa->spa_spares;
7254 for (i = 0; i < sav->sav_count; i++)
7255 if (sav->sav_vdevs[i]->vdev_guid == guid)
7258 for (i = 0; i < sav->sav_npending; i++) {
7259 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
7260 &spareguid) == 0 && spareguid == guid)
7268 * Check if a pool has an active shared spare device.
7269 * Note: reference count of an active spare is 2, as a spare and as a replace
7272 spa_has_active_shared_spare(spa_t *spa)
7276 spa_aux_vdev_t *sav = &spa->spa_spares;
7278 for (i = 0; i < sav->sav_count; i++) {
7279 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
7280 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
7289 spa_event_create(spa_t *spa, vdev_t *vd, const char *name)
7291 sysevent_t *ev = NULL;
7293 sysevent_attr_list_t *attr = NULL;
7294 sysevent_value_t value;
7296 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
7300 value.value_type = SE_DATA_TYPE_STRING;
7301 value.value.sv_string = spa_name(spa);
7302 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
7305 value.value_type = SE_DATA_TYPE_UINT64;
7306 value.value.sv_uint64 = spa_guid(spa);
7307 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
7311 value.value_type = SE_DATA_TYPE_UINT64;
7312 value.value.sv_uint64 = vd->vdev_guid;
7313 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
7317 if (vd->vdev_path) {
7318 value.value_type = SE_DATA_TYPE_STRING;
7319 value.value.sv_string = vd->vdev_path;
7320 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
7321 &value, SE_SLEEP) != 0)
7326 if (sysevent_attach_attributes(ev, attr) != 0)
7332 sysevent_free_attr(attr);
7339 spa_event_post(sysevent_t *ev)
7344 (void) log_sysevent(ev, SE_SLEEP, &eid);
7350 * Post a sysevent corresponding to the given event. The 'name' must be one of
7351 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
7352 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7353 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7354 * or zdb as real changes.
7357 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
7359 spa_event_post(spa_event_create(spa, vd, name));