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]
33 * SPA: Storage Pool Allocator
35 * This file contains all the routines used when modifying on-disk SPA state.
36 * This includes opening, importing, destroying, exporting a pool, and syncing a
40 #include <sys/zfs_context.h>
41 #include <sys/fm/fs/zfs.h>
42 #include <sys/spa_impl.h>
44 #include <sys/zio_checksum.h>
46 #include <sys/dmu_tx.h>
50 #include <sys/vdev_impl.h>
51 #include <sys/metaslab.h>
52 #include <sys/metaslab_impl.h>
53 #include <sys/uberblock_impl.h>
56 #include <sys/dmu_traverse.h>
57 #include <sys/dmu_objset.h>
58 #include <sys/unique.h>
59 #include <sys/dsl_pool.h>
60 #include <sys/dsl_dataset.h>
61 #include <sys/dsl_dir.h>
62 #include <sys/dsl_prop.h>
63 #include <sys/dsl_synctask.h>
64 #include <sys/fs/zfs.h>
66 #include <sys/callb.h>
67 #include <sys/spa_boot.h>
68 #include <sys/zfs_ioctl.h>
69 #include <sys/dsl_scan.h>
70 #include <sys/dmu_send.h>
71 #include <sys/dsl_destroy.h>
72 #include <sys/dsl_userhold.h>
73 #include <sys/zfeature.h>
75 #include <sys/trim_map.h>
78 #include <sys/callb.h>
79 #include <sys/cpupart.h>
84 #include "zfs_comutil.h"
86 /* Check hostid on import? */
87 static int check_hostid = 1;
90 * The interval, in seconds, at which failed configuration cache file writes
93 static int zfs_ccw_retry_interval = 300;
95 SYSCTL_DECL(_vfs_zfs);
96 TUNABLE_INT("vfs.zfs.check_hostid", &check_hostid);
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 void spa_sync_version(void *arg, dmu_tx_t *tx);
156 static void spa_sync_props(void *arg, dmu_tx_t *tx);
157 static boolean_t spa_has_active_shared_spare(spa_t *spa);
158 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
159 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
161 static void spa_vdev_resilver_done(spa_t *spa);
163 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
165 id_t zio_taskq_psrset_bind = PS_NONE;
168 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
170 uint_t zio_taskq_basedc = 80; /* base duty cycle */
172 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
173 extern int zfs_sync_pass_deferred_free;
176 extern void spa_deadman(void *arg);
180 * This (illegal) pool name is used when temporarily importing a spa_t in order
181 * to get the vdev stats associated with the imported devices.
183 #define TRYIMPORT_NAME "$import"
186 * ==========================================================================
187 * SPA properties routines
188 * ==========================================================================
192 * Add a (source=src, propname=propval) list to an nvlist.
195 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
196 uint64_t intval, zprop_source_t src)
198 const char *propname = zpool_prop_to_name(prop);
201 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
202 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
205 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
207 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
209 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
210 nvlist_free(propval);
214 * Get property values from the spa configuration.
217 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
219 vdev_t *rvd = spa->spa_root_vdev;
220 dsl_pool_t *pool = spa->spa_dsl_pool;
221 uint64_t size, alloc, cap, version;
222 zprop_source_t src = ZPROP_SRC_NONE;
223 spa_config_dirent_t *dp;
224 metaslab_class_t *mc = spa_normal_class(spa);
226 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
229 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
230 size = metaslab_class_get_space(spa_normal_class(spa));
231 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
232 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
233 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
234 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
237 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
238 metaslab_class_fragmentation(mc), src);
239 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
240 metaslab_class_expandable_space(mc), src);
241 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
242 (spa_mode(spa) == FREAD), src);
244 cap = (size == 0) ? 0 : (alloc * 100 / size);
245 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
247 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
248 ddt_get_pool_dedup_ratio(spa), src);
250 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
251 rvd->vdev_state, src);
253 version = spa_version(spa);
254 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
255 src = ZPROP_SRC_DEFAULT;
257 src = ZPROP_SRC_LOCAL;
258 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
263 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
264 * when opening pools before this version freedir will be NULL.
266 if (pool->dp_free_dir != NULL) {
267 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
268 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
271 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
275 if (pool->dp_leak_dir != NULL) {
276 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
277 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
280 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
285 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
287 if (spa->spa_comment != NULL) {
288 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
292 if (spa->spa_root != NULL)
293 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
296 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
297 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
298 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
300 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
301 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
304 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
305 if (dp->scd_path == NULL) {
306 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
307 "none", 0, ZPROP_SRC_LOCAL);
308 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
309 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
310 dp->scd_path, 0, ZPROP_SRC_LOCAL);
316 * Get zpool property values.
319 spa_prop_get(spa_t *spa, nvlist_t **nvp)
321 objset_t *mos = spa->spa_meta_objset;
326 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
328 mutex_enter(&spa->spa_props_lock);
331 * Get properties from the spa config.
333 spa_prop_get_config(spa, nvp);
335 /* If no pool property object, no more prop to get. */
336 if (mos == NULL || spa->spa_pool_props_object == 0) {
337 mutex_exit(&spa->spa_props_lock);
342 * Get properties from the MOS pool property object.
344 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
345 (err = zap_cursor_retrieve(&zc, &za)) == 0;
346 zap_cursor_advance(&zc)) {
349 zprop_source_t src = ZPROP_SRC_DEFAULT;
352 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
355 switch (za.za_integer_length) {
357 /* integer property */
358 if (za.za_first_integer !=
359 zpool_prop_default_numeric(prop))
360 src = ZPROP_SRC_LOCAL;
362 if (prop == ZPOOL_PROP_BOOTFS) {
364 dsl_dataset_t *ds = NULL;
366 dp = spa_get_dsl(spa);
367 dsl_pool_config_enter(dp, FTAG);
368 if (err = dsl_dataset_hold_obj(dp,
369 za.za_first_integer, FTAG, &ds)) {
370 dsl_pool_config_exit(dp, FTAG);
374 strval = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN,
376 dsl_dataset_name(ds, strval);
377 dsl_dataset_rele(ds, FTAG);
378 dsl_pool_config_exit(dp, FTAG);
381 intval = za.za_first_integer;
384 spa_prop_add_list(*nvp, prop, strval, intval, src);
387 kmem_free(strval, ZFS_MAX_DATASET_NAME_LEN);
392 /* string property */
393 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
394 err = zap_lookup(mos, spa->spa_pool_props_object,
395 za.za_name, 1, za.za_num_integers, strval);
397 kmem_free(strval, za.za_num_integers);
400 spa_prop_add_list(*nvp, prop, strval, 0, src);
401 kmem_free(strval, za.za_num_integers);
408 zap_cursor_fini(&zc);
409 mutex_exit(&spa->spa_props_lock);
411 if (err && err != ENOENT) {
421 * Validate the given pool properties nvlist and modify the list
422 * for the property values to be set.
425 spa_prop_validate(spa_t *spa, nvlist_t *props)
428 int error = 0, reset_bootfs = 0;
430 boolean_t has_feature = B_FALSE;
433 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
435 char *strval, *slash, *check, *fname;
436 const char *propname = nvpair_name(elem);
437 zpool_prop_t prop = zpool_name_to_prop(propname);
441 if (!zpool_prop_feature(propname)) {
442 error = SET_ERROR(EINVAL);
447 * Sanitize the input.
449 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
450 error = SET_ERROR(EINVAL);
454 if (nvpair_value_uint64(elem, &intval) != 0) {
455 error = SET_ERROR(EINVAL);
460 error = SET_ERROR(EINVAL);
464 fname = strchr(propname, '@') + 1;
465 if (zfeature_lookup_name(fname, NULL) != 0) {
466 error = SET_ERROR(EINVAL);
470 has_feature = B_TRUE;
473 case ZPOOL_PROP_VERSION:
474 error = nvpair_value_uint64(elem, &intval);
476 (intval < spa_version(spa) ||
477 intval > SPA_VERSION_BEFORE_FEATURES ||
479 error = SET_ERROR(EINVAL);
482 case ZPOOL_PROP_DELEGATION:
483 case ZPOOL_PROP_AUTOREPLACE:
484 case ZPOOL_PROP_LISTSNAPS:
485 case ZPOOL_PROP_AUTOEXPAND:
486 error = nvpair_value_uint64(elem, &intval);
487 if (!error && intval > 1)
488 error = SET_ERROR(EINVAL);
491 case ZPOOL_PROP_BOOTFS:
493 * If the pool version is less than SPA_VERSION_BOOTFS,
494 * or the pool is still being created (version == 0),
495 * the bootfs property cannot be set.
497 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
498 error = SET_ERROR(ENOTSUP);
503 * Make sure the vdev config is bootable
505 if (!vdev_is_bootable(spa->spa_root_vdev)) {
506 error = SET_ERROR(ENOTSUP);
512 error = nvpair_value_string(elem, &strval);
518 if (strval == NULL || strval[0] == '\0') {
519 objnum = zpool_prop_default_numeric(
524 if (error = dmu_objset_hold(strval, FTAG, &os))
528 * Must be ZPL, and its property settings
529 * must be supported by GRUB (compression
530 * is not gzip, and large blocks are not used).
533 if (dmu_objset_type(os) != DMU_OST_ZFS) {
534 error = SET_ERROR(ENOTSUP);
536 dsl_prop_get_int_ds(dmu_objset_ds(os),
537 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
539 !BOOTFS_COMPRESS_VALID(propval)) {
540 error = SET_ERROR(ENOTSUP);
542 dsl_prop_get_int_ds(dmu_objset_ds(os),
543 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
545 propval > SPA_OLD_MAXBLOCKSIZE) {
546 error = SET_ERROR(ENOTSUP);
548 objnum = dmu_objset_id(os);
550 dmu_objset_rele(os, FTAG);
554 case ZPOOL_PROP_FAILUREMODE:
555 error = nvpair_value_uint64(elem, &intval);
556 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
557 intval > ZIO_FAILURE_MODE_PANIC))
558 error = SET_ERROR(EINVAL);
561 * This is a special case which only occurs when
562 * the pool has completely failed. This allows
563 * the user to change the in-core failmode property
564 * without syncing it out to disk (I/Os might
565 * currently be blocked). We do this by returning
566 * EIO to the caller (spa_prop_set) to trick it
567 * into thinking we encountered a property validation
570 if (!error && spa_suspended(spa)) {
571 spa->spa_failmode = intval;
572 error = SET_ERROR(EIO);
576 case ZPOOL_PROP_CACHEFILE:
577 if ((error = nvpair_value_string(elem, &strval)) != 0)
580 if (strval[0] == '\0')
583 if (strcmp(strval, "none") == 0)
586 if (strval[0] != '/') {
587 error = SET_ERROR(EINVAL);
591 slash = strrchr(strval, '/');
592 ASSERT(slash != NULL);
594 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
595 strcmp(slash, "/..") == 0)
596 error = SET_ERROR(EINVAL);
599 case ZPOOL_PROP_COMMENT:
600 if ((error = nvpair_value_string(elem, &strval)) != 0)
602 for (check = strval; *check != '\0'; check++) {
604 * The kernel doesn't have an easy isprint()
605 * check. For this kernel check, we merely
606 * check ASCII apart from DEL. Fix this if
607 * there is an easy-to-use kernel isprint().
609 if (*check >= 0x7f) {
610 error = SET_ERROR(EINVAL);
614 if (strlen(strval) > ZPROP_MAX_COMMENT)
618 case ZPOOL_PROP_DEDUPDITTO:
619 if (spa_version(spa) < SPA_VERSION_DEDUP)
620 error = SET_ERROR(ENOTSUP);
622 error = nvpair_value_uint64(elem, &intval);
624 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
625 error = SET_ERROR(EINVAL);
633 if (!error && reset_bootfs) {
634 error = nvlist_remove(props,
635 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
638 error = nvlist_add_uint64(props,
639 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
647 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
650 spa_config_dirent_t *dp;
652 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
656 dp = kmem_alloc(sizeof (spa_config_dirent_t),
659 if (cachefile[0] == '\0')
660 dp->scd_path = spa_strdup(spa_config_path);
661 else if (strcmp(cachefile, "none") == 0)
664 dp->scd_path = spa_strdup(cachefile);
666 list_insert_head(&spa->spa_config_list, dp);
668 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
672 spa_prop_set(spa_t *spa, nvlist_t *nvp)
675 nvpair_t *elem = NULL;
676 boolean_t need_sync = B_FALSE;
678 if ((error = spa_prop_validate(spa, nvp)) != 0)
681 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
682 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
684 if (prop == ZPOOL_PROP_CACHEFILE ||
685 prop == ZPOOL_PROP_ALTROOT ||
686 prop == ZPOOL_PROP_READONLY)
689 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
692 if (prop == ZPOOL_PROP_VERSION) {
693 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
695 ASSERT(zpool_prop_feature(nvpair_name(elem)));
696 ver = SPA_VERSION_FEATURES;
700 /* Save time if the version is already set. */
701 if (ver == spa_version(spa))
705 * In addition to the pool directory object, we might
706 * create the pool properties object, the features for
707 * read object, the features for write object, or the
708 * feature descriptions object.
710 error = dsl_sync_task(spa->spa_name, NULL,
711 spa_sync_version, &ver,
712 6, ZFS_SPACE_CHECK_RESERVED);
723 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
724 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
731 * If the bootfs property value is dsobj, clear it.
734 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
736 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
737 VERIFY(zap_remove(spa->spa_meta_objset,
738 spa->spa_pool_props_object,
739 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
746 spa_change_guid_check(void *arg, dmu_tx_t *tx)
748 uint64_t *newguid = arg;
749 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
750 vdev_t *rvd = spa->spa_root_vdev;
753 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
754 vdev_state = rvd->vdev_state;
755 spa_config_exit(spa, SCL_STATE, FTAG);
757 if (vdev_state != VDEV_STATE_HEALTHY)
758 return (SET_ERROR(ENXIO));
760 ASSERT3U(spa_guid(spa), !=, *newguid);
766 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
768 uint64_t *newguid = arg;
769 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
771 vdev_t *rvd = spa->spa_root_vdev;
773 oldguid = spa_guid(spa);
775 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
776 rvd->vdev_guid = *newguid;
777 rvd->vdev_guid_sum += (*newguid - oldguid);
778 vdev_config_dirty(rvd);
779 spa_config_exit(spa, SCL_STATE, FTAG);
781 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
786 * Change the GUID for the pool. This is done so that we can later
787 * re-import a pool built from a clone of our own vdevs. We will modify
788 * the root vdev's guid, our own pool guid, and then mark all of our
789 * vdevs dirty. Note that we must make sure that all our vdevs are
790 * online when we do this, or else any vdevs that weren't present
791 * would be orphaned from our pool. We are also going to issue a
792 * sysevent to update any watchers.
795 spa_change_guid(spa_t *spa)
800 mutex_enter(&spa->spa_vdev_top_lock);
801 mutex_enter(&spa_namespace_lock);
802 guid = spa_generate_guid(NULL);
804 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
805 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
808 spa_config_sync(spa, B_FALSE, B_TRUE);
809 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
812 mutex_exit(&spa_namespace_lock);
813 mutex_exit(&spa->spa_vdev_top_lock);
819 * ==========================================================================
820 * SPA state manipulation (open/create/destroy/import/export)
821 * ==========================================================================
825 spa_error_entry_compare(const void *a, const void *b)
827 spa_error_entry_t *sa = (spa_error_entry_t *)a;
828 spa_error_entry_t *sb = (spa_error_entry_t *)b;
831 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
832 sizeof (zbookmark_phys_t));
843 * Utility function which retrieves copies of the current logs and
844 * re-initializes them in the process.
847 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
849 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
851 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
852 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
854 avl_create(&spa->spa_errlist_scrub,
855 spa_error_entry_compare, sizeof (spa_error_entry_t),
856 offsetof(spa_error_entry_t, se_avl));
857 avl_create(&spa->spa_errlist_last,
858 spa_error_entry_compare, sizeof (spa_error_entry_t),
859 offsetof(spa_error_entry_t, se_avl));
863 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
865 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
866 enum zti_modes mode = ztip->zti_mode;
867 uint_t value = ztip->zti_value;
868 uint_t count = ztip->zti_count;
869 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
872 boolean_t batch = B_FALSE;
874 if (mode == ZTI_MODE_NULL) {
876 tqs->stqs_taskq = NULL;
880 ASSERT3U(count, >, 0);
882 tqs->stqs_count = count;
883 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
887 ASSERT3U(value, >=, 1);
888 value = MAX(value, 1);
893 flags |= TASKQ_THREADS_CPU_PCT;
894 value = zio_taskq_batch_pct;
898 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
900 zio_type_name[t], zio_taskq_types[q], mode, value);
904 for (uint_t i = 0; i < count; i++) {
908 (void) snprintf(name, sizeof (name), "%s_%s_%u",
909 zio_type_name[t], zio_taskq_types[q], i);
911 (void) snprintf(name, sizeof (name), "%s_%s",
912 zio_type_name[t], zio_taskq_types[q]);
916 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
918 flags |= TASKQ_DC_BATCH;
920 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
921 spa->spa_proc, zio_taskq_basedc, flags);
924 pri_t pri = maxclsyspri;
926 * The write issue taskq can be extremely CPU
927 * intensive. Run it at slightly lower priority
928 * than the other taskqs.
930 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;
1336 * Drop and purge level 2 cache
1338 spa_l2cache_drop(spa);
1340 for (i = 0; i < spa->spa_spares.sav_count; i++)
1341 vdev_free(spa->spa_spares.sav_vdevs[i]);
1342 if (spa->spa_spares.sav_vdevs) {
1343 kmem_free(spa->spa_spares.sav_vdevs,
1344 spa->spa_spares.sav_count * sizeof (void *));
1345 spa->spa_spares.sav_vdevs = NULL;
1347 if (spa->spa_spares.sav_config) {
1348 nvlist_free(spa->spa_spares.sav_config);
1349 spa->spa_spares.sav_config = NULL;
1351 spa->spa_spares.sav_count = 0;
1353 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1354 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1355 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1357 if (spa->spa_l2cache.sav_vdevs) {
1358 kmem_free(spa->spa_l2cache.sav_vdevs,
1359 spa->spa_l2cache.sav_count * sizeof (void *));
1360 spa->spa_l2cache.sav_vdevs = NULL;
1362 if (spa->spa_l2cache.sav_config) {
1363 nvlist_free(spa->spa_l2cache.sav_config);
1364 spa->spa_l2cache.sav_config = NULL;
1366 spa->spa_l2cache.sav_count = 0;
1368 spa->spa_async_suspended = 0;
1370 if (spa->spa_comment != NULL) {
1371 spa_strfree(spa->spa_comment);
1372 spa->spa_comment = NULL;
1375 spa_config_exit(spa, SCL_ALL, FTAG);
1379 * Load (or re-load) the current list of vdevs describing the active spares for
1380 * this pool. When this is called, we have some form of basic information in
1381 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1382 * then re-generate a more complete list including status information.
1385 spa_load_spares(spa_t *spa)
1392 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1395 * First, close and free any existing spare vdevs.
1397 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1398 vd = spa->spa_spares.sav_vdevs[i];
1400 /* Undo the call to spa_activate() below */
1401 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1402 B_FALSE)) != NULL && tvd->vdev_isspare)
1403 spa_spare_remove(tvd);
1408 if (spa->spa_spares.sav_vdevs)
1409 kmem_free(spa->spa_spares.sav_vdevs,
1410 spa->spa_spares.sav_count * sizeof (void *));
1412 if (spa->spa_spares.sav_config == NULL)
1415 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1416 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1418 spa->spa_spares.sav_count = (int)nspares;
1419 spa->spa_spares.sav_vdevs = NULL;
1425 * Construct the array of vdevs, opening them to get status in the
1426 * process. For each spare, there is potentially two different vdev_t
1427 * structures associated with it: one in the list of spares (used only
1428 * for basic validation purposes) and one in the active vdev
1429 * configuration (if it's spared in). During this phase we open and
1430 * validate each vdev on the spare list. If the vdev also exists in the
1431 * active configuration, then we also mark this vdev as an active spare.
1433 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1435 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1436 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1437 VDEV_ALLOC_SPARE) == 0);
1440 spa->spa_spares.sav_vdevs[i] = vd;
1442 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1443 B_FALSE)) != NULL) {
1444 if (!tvd->vdev_isspare)
1448 * We only mark the spare active if we were successfully
1449 * able to load the vdev. Otherwise, importing a pool
1450 * with a bad active spare would result in strange
1451 * behavior, because multiple pool would think the spare
1452 * is actively in use.
1454 * There is a vulnerability here to an equally bizarre
1455 * circumstance, where a dead active spare is later
1456 * brought back to life (onlined or otherwise). Given
1457 * the rarity of this scenario, and the extra complexity
1458 * it adds, we ignore the possibility.
1460 if (!vdev_is_dead(tvd))
1461 spa_spare_activate(tvd);
1465 vd->vdev_aux = &spa->spa_spares;
1467 if (vdev_open(vd) != 0)
1470 if (vdev_validate_aux(vd) == 0)
1475 * Recompute the stashed list of spares, with status information
1478 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1479 DATA_TYPE_NVLIST_ARRAY) == 0);
1481 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1483 for (i = 0; i < spa->spa_spares.sav_count; i++)
1484 spares[i] = vdev_config_generate(spa,
1485 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1486 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1487 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1488 for (i = 0; i < spa->spa_spares.sav_count; i++)
1489 nvlist_free(spares[i]);
1490 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1494 * Load (or re-load) the current list of vdevs describing the active l2cache for
1495 * this pool. When this is called, we have some form of basic information in
1496 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1497 * then re-generate a more complete list including status information.
1498 * Devices which are already active have their details maintained, and are
1502 spa_load_l2cache(spa_t *spa)
1506 int i, j, oldnvdevs;
1508 vdev_t *vd, **oldvdevs, **newvdevs;
1509 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1511 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1513 if (sav->sav_config != NULL) {
1514 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1515 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1516 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1522 oldvdevs = sav->sav_vdevs;
1523 oldnvdevs = sav->sav_count;
1524 sav->sav_vdevs = NULL;
1528 * Process new nvlist of vdevs.
1530 for (i = 0; i < nl2cache; i++) {
1531 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1535 for (j = 0; j < oldnvdevs; j++) {
1537 if (vd != NULL && guid == vd->vdev_guid) {
1539 * Retain previous vdev for add/remove ops.
1547 if (newvdevs[i] == NULL) {
1551 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1552 VDEV_ALLOC_L2CACHE) == 0);
1557 * Commit this vdev as an l2cache device,
1558 * even if it fails to open.
1560 spa_l2cache_add(vd);
1565 spa_l2cache_activate(vd);
1567 if (vdev_open(vd) != 0)
1570 (void) vdev_validate_aux(vd);
1572 if (!vdev_is_dead(vd))
1573 l2arc_add_vdev(spa, vd);
1578 * Purge vdevs that were dropped
1580 for (i = 0; i < oldnvdevs; i++) {
1585 ASSERT(vd->vdev_isl2cache);
1587 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1588 pool != 0ULL && l2arc_vdev_present(vd))
1589 l2arc_remove_vdev(vd);
1590 vdev_clear_stats(vd);
1596 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1598 if (sav->sav_config == NULL)
1601 sav->sav_vdevs = newvdevs;
1602 sav->sav_count = (int)nl2cache;
1605 * Recompute the stashed list of l2cache devices, with status
1606 * information this time.
1608 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1609 DATA_TYPE_NVLIST_ARRAY) == 0);
1611 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1612 for (i = 0; i < sav->sav_count; i++)
1613 l2cache[i] = vdev_config_generate(spa,
1614 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1615 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1616 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1618 for (i = 0; i < sav->sav_count; i++)
1619 nvlist_free(l2cache[i]);
1621 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1625 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1628 char *packed = NULL;
1633 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1637 nvsize = *(uint64_t *)db->db_data;
1638 dmu_buf_rele(db, FTAG);
1640 packed = kmem_alloc(nvsize, KM_SLEEP);
1641 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1644 error = nvlist_unpack(packed, nvsize, value, 0);
1645 kmem_free(packed, nvsize);
1651 * Checks to see if the given vdev could not be opened, in which case we post a
1652 * sysevent to notify the autoreplace code that the device has been removed.
1655 spa_check_removed(vdev_t *vd)
1657 for (int c = 0; c < vd->vdev_children; c++)
1658 spa_check_removed(vd->vdev_child[c]);
1660 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1662 zfs_post_autoreplace(vd->vdev_spa, vd);
1663 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1668 * Validate the current config against the MOS config
1671 spa_config_valid(spa_t *spa, nvlist_t *config)
1673 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1676 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1678 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1679 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1681 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1684 * If we're doing a normal import, then build up any additional
1685 * diagnostic information about missing devices in this config.
1686 * We'll pass this up to the user for further processing.
1688 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1689 nvlist_t **child, *nv;
1692 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1694 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1696 for (int c = 0; c < rvd->vdev_children; c++) {
1697 vdev_t *tvd = rvd->vdev_child[c];
1698 vdev_t *mtvd = mrvd->vdev_child[c];
1700 if (tvd->vdev_ops == &vdev_missing_ops &&
1701 mtvd->vdev_ops != &vdev_missing_ops &&
1703 child[idx++] = vdev_config_generate(spa, mtvd,
1708 VERIFY(nvlist_add_nvlist_array(nv,
1709 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1710 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1711 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1713 for (int i = 0; i < idx; i++)
1714 nvlist_free(child[i]);
1717 kmem_free(child, rvd->vdev_children * sizeof (char **));
1721 * Compare the root vdev tree with the information we have
1722 * from the MOS config (mrvd). Check each top-level vdev
1723 * with the corresponding MOS config top-level (mtvd).
1725 for (int c = 0; c < rvd->vdev_children; c++) {
1726 vdev_t *tvd = rvd->vdev_child[c];
1727 vdev_t *mtvd = mrvd->vdev_child[c];
1730 * Resolve any "missing" vdevs in the current configuration.
1731 * If we find that the MOS config has more accurate information
1732 * about the top-level vdev then use that vdev instead.
1734 if (tvd->vdev_ops == &vdev_missing_ops &&
1735 mtvd->vdev_ops != &vdev_missing_ops) {
1737 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1741 * Device specific actions.
1743 if (mtvd->vdev_islog) {
1744 spa_set_log_state(spa, SPA_LOG_CLEAR);
1747 * XXX - once we have 'readonly' pool
1748 * support we should be able to handle
1749 * missing data devices by transitioning
1750 * the pool to readonly.
1756 * Swap the missing vdev with the data we were
1757 * able to obtain from the MOS config.
1759 vdev_remove_child(rvd, tvd);
1760 vdev_remove_child(mrvd, mtvd);
1762 vdev_add_child(rvd, mtvd);
1763 vdev_add_child(mrvd, tvd);
1765 spa_config_exit(spa, SCL_ALL, FTAG);
1767 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1770 } else if (mtvd->vdev_islog) {
1772 * Load the slog device's state from the MOS config
1773 * since it's possible that the label does not
1774 * contain the most up-to-date information.
1776 vdev_load_log_state(tvd, mtvd);
1781 spa_config_exit(spa, SCL_ALL, FTAG);
1784 * Ensure we were able to validate the config.
1786 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1790 * Check for missing log devices
1793 spa_check_logs(spa_t *spa)
1795 boolean_t rv = B_FALSE;
1796 dsl_pool_t *dp = spa_get_dsl(spa);
1798 switch (spa->spa_log_state) {
1799 case SPA_LOG_MISSING:
1800 /* need to recheck in case slog has been restored */
1801 case SPA_LOG_UNKNOWN:
1802 rv = (dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
1803 zil_check_log_chain, NULL, DS_FIND_CHILDREN) != 0);
1805 spa_set_log_state(spa, SPA_LOG_MISSING);
1812 spa_passivate_log(spa_t *spa)
1814 vdev_t *rvd = spa->spa_root_vdev;
1815 boolean_t slog_found = B_FALSE;
1817 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1819 if (!spa_has_slogs(spa))
1822 for (int c = 0; c < rvd->vdev_children; c++) {
1823 vdev_t *tvd = rvd->vdev_child[c];
1824 metaslab_group_t *mg = tvd->vdev_mg;
1826 if (tvd->vdev_islog) {
1827 metaslab_group_passivate(mg);
1828 slog_found = B_TRUE;
1832 return (slog_found);
1836 spa_activate_log(spa_t *spa)
1838 vdev_t *rvd = spa->spa_root_vdev;
1840 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1842 for (int c = 0; c < rvd->vdev_children; c++) {
1843 vdev_t *tvd = rvd->vdev_child[c];
1844 metaslab_group_t *mg = tvd->vdev_mg;
1846 if (tvd->vdev_islog)
1847 metaslab_group_activate(mg);
1852 spa_offline_log(spa_t *spa)
1856 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1857 NULL, DS_FIND_CHILDREN);
1860 * We successfully offlined the log device, sync out the
1861 * current txg so that the "stubby" block can be removed
1864 txg_wait_synced(spa->spa_dsl_pool, 0);
1870 spa_aux_check_removed(spa_aux_vdev_t *sav)
1874 for (i = 0; i < sav->sav_count; i++)
1875 spa_check_removed(sav->sav_vdevs[i]);
1879 spa_claim_notify(zio_t *zio)
1881 spa_t *spa = zio->io_spa;
1886 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1887 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1888 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1889 mutex_exit(&spa->spa_props_lock);
1892 typedef struct spa_load_error {
1893 uint64_t sle_meta_count;
1894 uint64_t sle_data_count;
1898 spa_load_verify_done(zio_t *zio)
1900 blkptr_t *bp = zio->io_bp;
1901 spa_load_error_t *sle = zio->io_private;
1902 dmu_object_type_t type = BP_GET_TYPE(bp);
1903 int error = zio->io_error;
1904 spa_t *spa = zio->io_spa;
1907 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1908 type != DMU_OT_INTENT_LOG)
1909 atomic_inc_64(&sle->sle_meta_count);
1911 atomic_inc_64(&sle->sle_data_count);
1913 zio_data_buf_free(zio->io_data, zio->io_size);
1915 mutex_enter(&spa->spa_scrub_lock);
1916 spa->spa_scrub_inflight--;
1917 cv_broadcast(&spa->spa_scrub_io_cv);
1918 mutex_exit(&spa->spa_scrub_lock);
1922 * Maximum number of concurrent scrub i/os to create while verifying
1923 * a pool while importing it.
1925 int spa_load_verify_maxinflight = 10000;
1926 boolean_t spa_load_verify_metadata = B_TRUE;
1927 boolean_t spa_load_verify_data = B_TRUE;
1929 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1930 &spa_load_verify_maxinflight, 0,
1931 "Maximum number of concurrent scrub I/Os to create while verifying a "
1932 "pool while importing it");
1934 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1935 &spa_load_verify_metadata, 0,
1936 "Check metadata on import?");
1938 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1939 &spa_load_verify_data, 0,
1940 "Check user data on import?");
1944 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1945 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1947 if (bp == NULL || BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1950 * Note: normally this routine will not be called if
1951 * spa_load_verify_metadata is not set. However, it may be useful
1952 * to manually set the flag after the traversal has begun.
1954 if (!spa_load_verify_metadata)
1956 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1960 size_t size = BP_GET_PSIZE(bp);
1961 void *data = zio_data_buf_alloc(size);
1963 mutex_enter(&spa->spa_scrub_lock);
1964 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1965 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1966 spa->spa_scrub_inflight++;
1967 mutex_exit(&spa->spa_scrub_lock);
1969 zio_nowait(zio_read(rio, spa, bp, data, size,
1970 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1971 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1972 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1978 verify_dataset_name_len(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
1980 if (dsl_dataset_namelen(ds) >= ZFS_MAX_DATASET_NAME_LEN)
1981 return (SET_ERROR(ENAMETOOLONG));
1987 spa_load_verify(spa_t *spa)
1990 spa_load_error_t sle = { 0 };
1991 zpool_rewind_policy_t policy;
1992 boolean_t verify_ok = B_FALSE;
1995 zpool_get_rewind_policy(spa->spa_config, &policy);
1997 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
2000 dsl_pool_config_enter(spa->spa_dsl_pool, FTAG);
2001 error = dmu_objset_find_dp(spa->spa_dsl_pool,
2002 spa->spa_dsl_pool->dp_root_dir_obj, verify_dataset_name_len, NULL,
2004 dsl_pool_config_exit(spa->spa_dsl_pool, FTAG);
2008 rio = zio_root(spa, NULL, &sle,
2009 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
2011 if (spa_load_verify_metadata) {
2012 error = traverse_pool(spa, spa->spa_verify_min_txg,
2013 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
2014 spa_load_verify_cb, rio);
2017 (void) zio_wait(rio);
2019 spa->spa_load_meta_errors = sle.sle_meta_count;
2020 spa->spa_load_data_errors = sle.sle_data_count;
2022 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
2023 sle.sle_data_count <= policy.zrp_maxdata) {
2027 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2028 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2030 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2031 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2032 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2033 VERIFY(nvlist_add_int64(spa->spa_load_info,
2034 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2035 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2036 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2038 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2042 if (error != ENXIO && error != EIO)
2043 error = SET_ERROR(EIO);
2047 return (verify_ok ? 0 : EIO);
2051 * Find a value in the pool props object.
2054 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2056 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2057 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2061 * Find a value in the pool directory object.
2064 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2066 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2067 name, sizeof (uint64_t), 1, val));
2071 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2073 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2078 * Fix up config after a partly-completed split. This is done with the
2079 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2080 * pool have that entry in their config, but only the splitting one contains
2081 * a list of all the guids of the vdevs that are being split off.
2083 * This function determines what to do with that list: either rejoin
2084 * all the disks to the pool, or complete the splitting process. To attempt
2085 * the rejoin, each disk that is offlined is marked online again, and
2086 * we do a reopen() call. If the vdev label for every disk that was
2087 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2088 * then we call vdev_split() on each disk, and complete the split.
2090 * Otherwise we leave the config alone, with all the vdevs in place in
2091 * the original pool.
2094 spa_try_repair(spa_t *spa, nvlist_t *config)
2101 boolean_t attempt_reopen;
2103 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2106 /* check that the config is complete */
2107 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2108 &glist, &gcount) != 0)
2111 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2113 /* attempt to online all the vdevs & validate */
2114 attempt_reopen = B_TRUE;
2115 for (i = 0; i < gcount; i++) {
2116 if (glist[i] == 0) /* vdev is hole */
2119 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2120 if (vd[i] == NULL) {
2122 * Don't bother attempting to reopen the disks;
2123 * just do the split.
2125 attempt_reopen = B_FALSE;
2127 /* attempt to re-online it */
2128 vd[i]->vdev_offline = B_FALSE;
2132 if (attempt_reopen) {
2133 vdev_reopen(spa->spa_root_vdev);
2135 /* check each device to see what state it's in */
2136 for (extracted = 0, i = 0; i < gcount; i++) {
2137 if (vd[i] != NULL &&
2138 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2145 * If every disk has been moved to the new pool, or if we never
2146 * even attempted to look at them, then we split them off for
2149 if (!attempt_reopen || gcount == extracted) {
2150 for (i = 0; i < gcount; i++)
2153 vdev_reopen(spa->spa_root_vdev);
2156 kmem_free(vd, gcount * sizeof (vdev_t *));
2160 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2161 boolean_t mosconfig)
2163 nvlist_t *config = spa->spa_config;
2164 char *ereport = FM_EREPORT_ZFS_POOL;
2170 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2171 return (SET_ERROR(EINVAL));
2173 ASSERT(spa->spa_comment == NULL);
2174 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2175 spa->spa_comment = spa_strdup(comment);
2178 * Versioning wasn't explicitly added to the label until later, so if
2179 * it's not present treat it as the initial version.
2181 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2182 &spa->spa_ubsync.ub_version) != 0)
2183 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2185 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2186 &spa->spa_config_txg);
2188 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2189 spa_guid_exists(pool_guid, 0)) {
2190 error = SET_ERROR(EEXIST);
2192 spa->spa_config_guid = pool_guid;
2194 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2196 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2200 nvlist_free(spa->spa_load_info);
2201 spa->spa_load_info = fnvlist_alloc();
2203 gethrestime(&spa->spa_loaded_ts);
2204 error = spa_load_impl(spa, pool_guid, config, state, type,
2205 mosconfig, &ereport);
2209 * Don't count references from objsets that are already closed
2210 * and are making their way through the eviction process.
2212 spa_evicting_os_wait(spa);
2213 spa->spa_minref = refcount_count(&spa->spa_refcount);
2215 if (error != EEXIST) {
2216 spa->spa_loaded_ts.tv_sec = 0;
2217 spa->spa_loaded_ts.tv_nsec = 0;
2219 if (error != EBADF) {
2220 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2223 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2230 * Load an existing storage pool, using the pool's builtin spa_config as a
2231 * source of configuration information.
2234 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2235 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2239 nvlist_t *nvroot = NULL;
2242 uberblock_t *ub = &spa->spa_uberblock;
2243 uint64_t children, config_cache_txg = spa->spa_config_txg;
2244 int orig_mode = spa->spa_mode;
2247 boolean_t missing_feat_write = B_FALSE;
2250 * If this is an untrusted config, access the pool in read-only mode.
2251 * This prevents things like resilvering recently removed devices.
2254 spa->spa_mode = FREAD;
2256 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2258 spa->spa_load_state = state;
2260 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2261 return (SET_ERROR(EINVAL));
2263 parse = (type == SPA_IMPORT_EXISTING ?
2264 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2267 * Create "The Godfather" zio to hold all async IOs
2269 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2271 for (int i = 0; i < max_ncpus; i++) {
2272 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2273 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2274 ZIO_FLAG_GODFATHER);
2278 * Parse the configuration into a vdev tree. We explicitly set the
2279 * value that will be returned by spa_version() since parsing the
2280 * configuration requires knowing the version number.
2282 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2283 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2284 spa_config_exit(spa, SCL_ALL, FTAG);
2289 ASSERT(spa->spa_root_vdev == rvd);
2290 ASSERT3U(spa->spa_min_ashift, >=, SPA_MINBLOCKSHIFT);
2291 ASSERT3U(spa->spa_max_ashift, <=, SPA_MAXBLOCKSHIFT);
2293 if (type != SPA_IMPORT_ASSEMBLE) {
2294 ASSERT(spa_guid(spa) == pool_guid);
2298 * Try to open all vdevs, loading each label in the process.
2300 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2301 error = vdev_open(rvd);
2302 spa_config_exit(spa, SCL_ALL, FTAG);
2307 * We need to validate the vdev labels against the configuration that
2308 * we have in hand, which is dependent on the setting of mosconfig. If
2309 * mosconfig is true then we're validating the vdev labels based on
2310 * that config. Otherwise, we're validating against the cached config
2311 * (zpool.cache) that was read when we loaded the zfs module, and then
2312 * later we will recursively call spa_load() and validate against
2315 * If we're assembling a new pool that's been split off from an
2316 * existing pool, the labels haven't yet been updated so we skip
2317 * validation for now.
2319 if (type != SPA_IMPORT_ASSEMBLE) {
2320 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2321 error = vdev_validate(rvd, mosconfig);
2322 spa_config_exit(spa, SCL_ALL, FTAG);
2327 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2328 return (SET_ERROR(ENXIO));
2332 * Find the best uberblock.
2334 vdev_uberblock_load(rvd, ub, &label);
2337 * If we weren't able to find a single valid uberblock, return failure.
2339 if (ub->ub_txg == 0) {
2341 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2345 * If the pool has an unsupported version we can't open it.
2347 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2349 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2352 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2356 * If we weren't able to find what's necessary for reading the
2357 * MOS in the label, return failure.
2359 if (label == NULL || nvlist_lookup_nvlist(label,
2360 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2362 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2367 * Update our in-core representation with the definitive values
2370 nvlist_free(spa->spa_label_features);
2371 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2377 * Look through entries in the label nvlist's features_for_read. If
2378 * there is a feature listed there which we don't understand then we
2379 * cannot open a pool.
2381 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2382 nvlist_t *unsup_feat;
2384 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2387 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2389 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2390 if (!zfeature_is_supported(nvpair_name(nvp))) {
2391 VERIFY(nvlist_add_string(unsup_feat,
2392 nvpair_name(nvp), "") == 0);
2396 if (!nvlist_empty(unsup_feat)) {
2397 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2398 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2399 nvlist_free(unsup_feat);
2400 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2404 nvlist_free(unsup_feat);
2408 * If the vdev guid sum doesn't match the uberblock, we have an
2409 * incomplete configuration. We first check to see if the pool
2410 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2411 * If it is, defer the vdev_guid_sum check till later so we
2412 * can handle missing vdevs.
2414 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2415 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2416 rvd->vdev_guid_sum != ub->ub_guid_sum)
2417 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2419 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2420 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2421 spa_try_repair(spa, config);
2422 spa_config_exit(spa, SCL_ALL, FTAG);
2423 nvlist_free(spa->spa_config_splitting);
2424 spa->spa_config_splitting = NULL;
2428 * Initialize internal SPA structures.
2430 spa->spa_state = POOL_STATE_ACTIVE;
2431 spa->spa_ubsync = spa->spa_uberblock;
2432 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2433 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2434 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2435 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2436 spa->spa_claim_max_txg = spa->spa_first_txg;
2437 spa->spa_prev_software_version = ub->ub_software_version;
2439 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2441 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2442 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2444 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2445 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2447 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2448 boolean_t missing_feat_read = B_FALSE;
2449 nvlist_t *unsup_feat, *enabled_feat;
2451 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2452 &spa->spa_feat_for_read_obj) != 0) {
2453 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2456 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2457 &spa->spa_feat_for_write_obj) != 0) {
2458 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2461 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2462 &spa->spa_feat_desc_obj) != 0) {
2463 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2466 enabled_feat = fnvlist_alloc();
2467 unsup_feat = fnvlist_alloc();
2469 if (!spa_features_check(spa, B_FALSE,
2470 unsup_feat, enabled_feat))
2471 missing_feat_read = B_TRUE;
2473 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2474 if (!spa_features_check(spa, B_TRUE,
2475 unsup_feat, enabled_feat)) {
2476 missing_feat_write = B_TRUE;
2480 fnvlist_add_nvlist(spa->spa_load_info,
2481 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2483 if (!nvlist_empty(unsup_feat)) {
2484 fnvlist_add_nvlist(spa->spa_load_info,
2485 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2488 fnvlist_free(enabled_feat);
2489 fnvlist_free(unsup_feat);
2491 if (!missing_feat_read) {
2492 fnvlist_add_boolean(spa->spa_load_info,
2493 ZPOOL_CONFIG_CAN_RDONLY);
2497 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2498 * twofold: to determine whether the pool is available for
2499 * import in read-write mode and (if it is not) whether the
2500 * pool is available for import in read-only mode. If the pool
2501 * is available for import in read-write mode, it is displayed
2502 * as available in userland; if it is not available for import
2503 * in read-only mode, it is displayed as unavailable in
2504 * userland. If the pool is available for import in read-only
2505 * mode but not read-write mode, it is displayed as unavailable
2506 * in userland with a special note that the pool is actually
2507 * available for open in read-only mode.
2509 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2510 * missing a feature for write, we must first determine whether
2511 * the pool can be opened read-only before returning to
2512 * userland in order to know whether to display the
2513 * abovementioned note.
2515 if (missing_feat_read || (missing_feat_write &&
2516 spa_writeable(spa))) {
2517 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2522 * Load refcounts for ZFS features from disk into an in-memory
2523 * cache during SPA initialization.
2525 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2528 error = feature_get_refcount_from_disk(spa,
2529 &spa_feature_table[i], &refcount);
2531 spa->spa_feat_refcount_cache[i] = refcount;
2532 } else if (error == ENOTSUP) {
2533 spa->spa_feat_refcount_cache[i] =
2534 SPA_FEATURE_DISABLED;
2536 return (spa_vdev_err(rvd,
2537 VDEV_AUX_CORRUPT_DATA, EIO));
2542 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2543 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2544 &spa->spa_feat_enabled_txg_obj) != 0)
2545 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2548 spa->spa_is_initializing = B_TRUE;
2549 error = dsl_pool_open(spa->spa_dsl_pool);
2550 spa->spa_is_initializing = B_FALSE;
2552 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2556 nvlist_t *policy = NULL, *nvconfig;
2558 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2559 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2561 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2562 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2564 unsigned long myhostid = 0;
2566 VERIFY(nvlist_lookup_string(nvconfig,
2567 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2570 myhostid = zone_get_hostid(NULL);
2573 * We're emulating the system's hostid in userland, so
2574 * we can't use zone_get_hostid().
2576 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2577 #endif /* _KERNEL */
2578 if (check_hostid && hostid != 0 && myhostid != 0 &&
2579 hostid != myhostid) {
2580 nvlist_free(nvconfig);
2581 cmn_err(CE_WARN, "pool '%s' could not be "
2582 "loaded as it was last accessed by "
2583 "another system (host: %s hostid: 0x%lx). "
2584 "See: http://illumos.org/msg/ZFS-8000-EY",
2585 spa_name(spa), hostname,
2586 (unsigned long)hostid);
2587 return (SET_ERROR(EBADF));
2590 if (nvlist_lookup_nvlist(spa->spa_config,
2591 ZPOOL_REWIND_POLICY, &policy) == 0)
2592 VERIFY(nvlist_add_nvlist(nvconfig,
2593 ZPOOL_REWIND_POLICY, policy) == 0);
2595 spa_config_set(spa, nvconfig);
2597 spa_deactivate(spa);
2598 spa_activate(spa, orig_mode);
2600 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2603 /* Grab the secret checksum salt from the MOS. */
2604 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2605 DMU_POOL_CHECKSUM_SALT, 1,
2606 sizeof (spa->spa_cksum_salt.zcs_bytes),
2607 spa->spa_cksum_salt.zcs_bytes);
2608 if (error == ENOENT) {
2609 /* Generate a new salt for subsequent use */
2610 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
2611 sizeof (spa->spa_cksum_salt.zcs_bytes));
2612 } else if (error != 0) {
2613 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2616 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2617 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2618 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2620 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2623 * Load the bit that tells us to use the new accounting function
2624 * (raid-z deflation). If we have an older pool, this will not
2627 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2628 if (error != 0 && error != ENOENT)
2629 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2631 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2632 &spa->spa_creation_version);
2633 if (error != 0 && error != ENOENT)
2634 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2637 * Load the persistent error log. If we have an older pool, this will
2640 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2641 if (error != 0 && error != ENOENT)
2642 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2644 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2645 &spa->spa_errlog_scrub);
2646 if (error != 0 && error != ENOENT)
2647 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2650 * Load the history object. If we have an older pool, this
2651 * will not be present.
2653 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2654 if (error != 0 && error != ENOENT)
2655 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2658 * If we're assembling the pool from the split-off vdevs of
2659 * an existing pool, we don't want to attach the spares & cache
2664 * Load any hot spares for this pool.
2666 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2667 if (error != 0 && error != ENOENT)
2668 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2669 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2670 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2671 if (load_nvlist(spa, spa->spa_spares.sav_object,
2672 &spa->spa_spares.sav_config) != 0)
2673 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2675 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2676 spa_load_spares(spa);
2677 spa_config_exit(spa, SCL_ALL, FTAG);
2678 } else if (error == 0) {
2679 spa->spa_spares.sav_sync = B_TRUE;
2683 * Load any level 2 ARC devices for this pool.
2685 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2686 &spa->spa_l2cache.sav_object);
2687 if (error != 0 && error != ENOENT)
2688 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2689 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2690 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2691 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2692 &spa->spa_l2cache.sav_config) != 0)
2693 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2695 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2696 spa_load_l2cache(spa);
2697 spa_config_exit(spa, SCL_ALL, FTAG);
2698 } else if (error == 0) {
2699 spa->spa_l2cache.sav_sync = B_TRUE;
2702 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2704 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2705 if (error && error != ENOENT)
2706 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2709 uint64_t autoreplace;
2711 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2712 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2713 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2714 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2715 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2716 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2717 &spa->spa_dedup_ditto);
2719 spa->spa_autoreplace = (autoreplace != 0);
2723 * If the 'autoreplace' property is set, then post a resource notifying
2724 * the ZFS DE that it should not issue any faults for unopenable
2725 * devices. We also iterate over the vdevs, and post a sysevent for any
2726 * unopenable vdevs so that the normal autoreplace handler can take
2729 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2730 spa_check_removed(spa->spa_root_vdev);
2732 * For the import case, this is done in spa_import(), because
2733 * at this point we're using the spare definitions from
2734 * the MOS config, not necessarily from the userland config.
2736 if (state != SPA_LOAD_IMPORT) {
2737 spa_aux_check_removed(&spa->spa_spares);
2738 spa_aux_check_removed(&spa->spa_l2cache);
2743 * Load the vdev state for all toplevel vdevs.
2748 * Propagate the leaf DTLs we just loaded all the way up the tree.
2750 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2751 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2752 spa_config_exit(spa, SCL_ALL, FTAG);
2755 * Load the DDTs (dedup tables).
2757 error = ddt_load(spa);
2759 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2761 spa_update_dspace(spa);
2764 * Validate the config, using the MOS config to fill in any
2765 * information which might be missing. If we fail to validate
2766 * the config then declare the pool unfit for use. If we're
2767 * assembling a pool from a split, the log is not transferred
2770 if (type != SPA_IMPORT_ASSEMBLE) {
2773 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2774 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2776 if (!spa_config_valid(spa, nvconfig)) {
2777 nvlist_free(nvconfig);
2778 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2781 nvlist_free(nvconfig);
2784 * Now that we've validated the config, check the state of the
2785 * root vdev. If it can't be opened, it indicates one or
2786 * more toplevel vdevs are faulted.
2788 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2789 return (SET_ERROR(ENXIO));
2791 if (spa_writeable(spa) && spa_check_logs(spa)) {
2792 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2793 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2797 if (missing_feat_write) {
2798 ASSERT(state == SPA_LOAD_TRYIMPORT);
2801 * At this point, we know that we can open the pool in
2802 * read-only mode but not read-write mode. We now have enough
2803 * information and can return to userland.
2805 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2809 * We've successfully opened the pool, verify that we're ready
2810 * to start pushing transactions.
2812 if (state != SPA_LOAD_TRYIMPORT) {
2813 if (error = spa_load_verify(spa))
2814 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2818 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2819 spa->spa_load_max_txg == UINT64_MAX)) {
2821 int need_update = B_FALSE;
2822 dsl_pool_t *dp = spa_get_dsl(spa);
2824 ASSERT(state != SPA_LOAD_TRYIMPORT);
2827 * Claim log blocks that haven't been committed yet.
2828 * This must all happen in a single txg.
2829 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2830 * invoked from zil_claim_log_block()'s i/o done callback.
2831 * Price of rollback is that we abandon the log.
2833 spa->spa_claiming = B_TRUE;
2835 tx = dmu_tx_create_assigned(dp, spa_first_txg(spa));
2836 (void) dmu_objset_find_dp(dp, dp->dp_root_dir_obj,
2837 zil_claim, tx, DS_FIND_CHILDREN);
2840 spa->spa_claiming = B_FALSE;
2842 spa_set_log_state(spa, SPA_LOG_GOOD);
2843 spa->spa_sync_on = B_TRUE;
2844 txg_sync_start(spa->spa_dsl_pool);
2847 * Wait for all claims to sync. We sync up to the highest
2848 * claimed log block birth time so that claimed log blocks
2849 * don't appear to be from the future. spa_claim_max_txg
2850 * will have been set for us by either zil_check_log_chain()
2851 * (invoked from spa_check_logs()) or zil_claim() above.
2853 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2856 * If the config cache is stale, or we have uninitialized
2857 * metaslabs (see spa_vdev_add()), then update the config.
2859 * If this is a verbatim import, trust the current
2860 * in-core spa_config and update the disk labels.
2862 if (config_cache_txg != spa->spa_config_txg ||
2863 state == SPA_LOAD_IMPORT ||
2864 state == SPA_LOAD_RECOVER ||
2865 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2866 need_update = B_TRUE;
2868 for (int c = 0; c < rvd->vdev_children; c++)
2869 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2870 need_update = B_TRUE;
2873 * Update the config cache asychronously in case we're the
2874 * root pool, in which case the config cache isn't writable yet.
2877 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2880 * Check all DTLs to see if anything needs resilvering.
2882 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2883 vdev_resilver_needed(rvd, NULL, NULL))
2884 spa_async_request(spa, SPA_ASYNC_RESILVER);
2887 * Log the fact that we booted up (so that we can detect if
2888 * we rebooted in the middle of an operation).
2890 spa_history_log_version(spa, "open");
2893 * Delete any inconsistent datasets.
2895 (void) dmu_objset_find(spa_name(spa),
2896 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2899 * Clean up any stale temporary dataset userrefs.
2901 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2908 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2910 int mode = spa->spa_mode;
2913 spa_deactivate(spa);
2915 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2917 spa_activate(spa, mode);
2918 spa_async_suspend(spa);
2920 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2924 * If spa_load() fails this function will try loading prior txg's. If
2925 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2926 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2927 * function will not rewind the pool and will return the same error as
2931 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2932 uint64_t max_request, int rewind_flags)
2934 nvlist_t *loadinfo = NULL;
2935 nvlist_t *config = NULL;
2936 int load_error, rewind_error;
2937 uint64_t safe_rewind_txg;
2940 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2941 spa->spa_load_max_txg = spa->spa_load_txg;
2942 spa_set_log_state(spa, SPA_LOG_CLEAR);
2944 spa->spa_load_max_txg = max_request;
2945 if (max_request != UINT64_MAX)
2946 spa->spa_extreme_rewind = B_TRUE;
2949 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2951 if (load_error == 0)
2954 if (spa->spa_root_vdev != NULL)
2955 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2957 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2958 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2960 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2961 nvlist_free(config);
2962 return (load_error);
2965 if (state == SPA_LOAD_RECOVER) {
2966 /* Price of rolling back is discarding txgs, including log */
2967 spa_set_log_state(spa, SPA_LOG_CLEAR);
2970 * If we aren't rolling back save the load info from our first
2971 * import attempt so that we can restore it after attempting
2974 loadinfo = spa->spa_load_info;
2975 spa->spa_load_info = fnvlist_alloc();
2978 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2979 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2980 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2981 TXG_INITIAL : safe_rewind_txg;
2984 * Continue as long as we're finding errors, we're still within
2985 * the acceptable rewind range, and we're still finding uberblocks
2987 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2988 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2989 if (spa->spa_load_max_txg < safe_rewind_txg)
2990 spa->spa_extreme_rewind = B_TRUE;
2991 rewind_error = spa_load_retry(spa, state, mosconfig);
2994 spa->spa_extreme_rewind = B_FALSE;
2995 spa->spa_load_max_txg = UINT64_MAX;
2997 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2998 spa_config_set(spa, config);
3000 if (state == SPA_LOAD_RECOVER) {
3001 ASSERT3P(loadinfo, ==, NULL);
3002 return (rewind_error);
3004 /* Store the rewind info as part of the initial load info */
3005 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
3006 spa->spa_load_info);
3008 /* Restore the initial load info */
3009 fnvlist_free(spa->spa_load_info);
3010 spa->spa_load_info = loadinfo;
3012 return (load_error);
3019 * The import case is identical to an open except that the configuration is sent
3020 * down from userland, instead of grabbed from the configuration cache. For the
3021 * case of an open, the pool configuration will exist in the
3022 * POOL_STATE_UNINITIALIZED state.
3024 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3025 * the same time open the pool, without having to keep around the spa_t in some
3029 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
3033 spa_load_state_t state = SPA_LOAD_OPEN;
3035 int locked = B_FALSE;
3036 int firstopen = B_FALSE;
3041 * As disgusting as this is, we need to support recursive calls to this
3042 * function because dsl_dir_open() is called during spa_load(), and ends
3043 * up calling spa_open() again. The real fix is to figure out how to
3044 * avoid dsl_dir_open() calling this in the first place.
3046 if (mutex_owner(&spa_namespace_lock) != curthread) {
3047 mutex_enter(&spa_namespace_lock);
3051 if ((spa = spa_lookup(pool)) == NULL) {
3053 mutex_exit(&spa_namespace_lock);
3054 return (SET_ERROR(ENOENT));
3057 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
3058 zpool_rewind_policy_t policy;
3062 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
3064 if (policy.zrp_request & ZPOOL_DO_REWIND)
3065 state = SPA_LOAD_RECOVER;
3067 spa_activate(spa, spa_mode_global);
3069 if (state != SPA_LOAD_RECOVER)
3070 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3072 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3073 policy.zrp_request);
3075 if (error == EBADF) {
3077 * If vdev_validate() returns failure (indicated by
3078 * EBADF), it indicates that one of the vdevs indicates
3079 * that the pool has been exported or destroyed. If
3080 * this is the case, the config cache is out of sync and
3081 * we should remove the pool from the namespace.
3084 spa_deactivate(spa);
3085 spa_config_sync(spa, B_TRUE, B_TRUE);
3088 mutex_exit(&spa_namespace_lock);
3089 return (SET_ERROR(ENOENT));
3094 * We can't open the pool, but we still have useful
3095 * information: the state of each vdev after the
3096 * attempted vdev_open(). Return this to the user.
3098 if (config != NULL && spa->spa_config) {
3099 VERIFY(nvlist_dup(spa->spa_config, config,
3101 VERIFY(nvlist_add_nvlist(*config,
3102 ZPOOL_CONFIG_LOAD_INFO,
3103 spa->spa_load_info) == 0);
3106 spa_deactivate(spa);
3107 spa->spa_last_open_failed = error;
3109 mutex_exit(&spa_namespace_lock);
3115 spa_open_ref(spa, tag);
3118 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3121 * If we've recovered the pool, pass back any information we
3122 * gathered while doing the load.
3124 if (state == SPA_LOAD_RECOVER) {
3125 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3126 spa->spa_load_info) == 0);
3130 spa->spa_last_open_failed = 0;
3131 spa->spa_last_ubsync_txg = 0;
3132 spa->spa_load_txg = 0;
3133 mutex_exit(&spa_namespace_lock);
3137 zvol_create_minors(spa->spa_name);
3148 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3151 return (spa_open_common(name, spapp, tag, policy, config));
3155 spa_open(const char *name, spa_t **spapp, void *tag)
3157 return (spa_open_common(name, spapp, tag, NULL, NULL));
3161 * Lookup the given spa_t, incrementing the inject count in the process,
3162 * preventing it from being exported or destroyed.
3165 spa_inject_addref(char *name)
3169 mutex_enter(&spa_namespace_lock);
3170 if ((spa = spa_lookup(name)) == NULL) {
3171 mutex_exit(&spa_namespace_lock);
3174 spa->spa_inject_ref++;
3175 mutex_exit(&spa_namespace_lock);
3181 spa_inject_delref(spa_t *spa)
3183 mutex_enter(&spa_namespace_lock);
3184 spa->spa_inject_ref--;
3185 mutex_exit(&spa_namespace_lock);
3189 * Add spares device information to the nvlist.
3192 spa_add_spares(spa_t *spa, nvlist_t *config)
3202 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3204 if (spa->spa_spares.sav_count == 0)
3207 VERIFY(nvlist_lookup_nvlist(config,
3208 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3209 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3210 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3212 VERIFY(nvlist_add_nvlist_array(nvroot,
3213 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3214 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3215 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3218 * Go through and find any spares which have since been
3219 * repurposed as an active spare. If this is the case, update
3220 * their status appropriately.
3222 for (i = 0; i < nspares; i++) {
3223 VERIFY(nvlist_lookup_uint64(spares[i],
3224 ZPOOL_CONFIG_GUID, &guid) == 0);
3225 if (spa_spare_exists(guid, &pool, NULL) &&
3227 VERIFY(nvlist_lookup_uint64_array(
3228 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3229 (uint64_t **)&vs, &vsc) == 0);
3230 vs->vs_state = VDEV_STATE_CANT_OPEN;
3231 vs->vs_aux = VDEV_AUX_SPARED;
3238 * Add l2cache device information to the nvlist, including vdev stats.
3241 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3244 uint_t i, j, nl2cache;
3251 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3253 if (spa->spa_l2cache.sav_count == 0)
3256 VERIFY(nvlist_lookup_nvlist(config,
3257 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3258 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3259 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3260 if (nl2cache != 0) {
3261 VERIFY(nvlist_add_nvlist_array(nvroot,
3262 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3263 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3264 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3267 * Update level 2 cache device stats.
3270 for (i = 0; i < nl2cache; i++) {
3271 VERIFY(nvlist_lookup_uint64(l2cache[i],
3272 ZPOOL_CONFIG_GUID, &guid) == 0);
3275 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3277 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3278 vd = spa->spa_l2cache.sav_vdevs[j];
3284 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3285 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3287 vdev_get_stats(vd, vs);
3293 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3299 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3300 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3302 /* We may be unable to read features if pool is suspended. */
3303 if (spa_suspended(spa))
3306 if (spa->spa_feat_for_read_obj != 0) {
3307 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3308 spa->spa_feat_for_read_obj);
3309 zap_cursor_retrieve(&zc, &za) == 0;
3310 zap_cursor_advance(&zc)) {
3311 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3312 za.za_num_integers == 1);
3313 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3314 za.za_first_integer));
3316 zap_cursor_fini(&zc);
3319 if (spa->spa_feat_for_write_obj != 0) {
3320 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3321 spa->spa_feat_for_write_obj);
3322 zap_cursor_retrieve(&zc, &za) == 0;
3323 zap_cursor_advance(&zc)) {
3324 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3325 za.za_num_integers == 1);
3326 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3327 za.za_first_integer));
3329 zap_cursor_fini(&zc);
3333 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3335 nvlist_free(features);
3339 spa_get_stats(const char *name, nvlist_t **config,
3340 char *altroot, size_t buflen)
3346 error = spa_open_common(name, &spa, FTAG, NULL, config);
3350 * This still leaves a window of inconsistency where the spares
3351 * or l2cache devices could change and the config would be
3352 * self-inconsistent.
3354 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3356 if (*config != NULL) {
3357 uint64_t loadtimes[2];
3359 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3360 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3361 VERIFY(nvlist_add_uint64_array(*config,
3362 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3364 VERIFY(nvlist_add_uint64(*config,
3365 ZPOOL_CONFIG_ERRCOUNT,
3366 spa_get_errlog_size(spa)) == 0);
3368 if (spa_suspended(spa))
3369 VERIFY(nvlist_add_uint64(*config,
3370 ZPOOL_CONFIG_SUSPENDED,
3371 spa->spa_failmode) == 0);
3373 spa_add_spares(spa, *config);
3374 spa_add_l2cache(spa, *config);
3375 spa_add_feature_stats(spa, *config);
3380 * We want to get the alternate root even for faulted pools, so we cheat
3381 * and call spa_lookup() directly.
3385 mutex_enter(&spa_namespace_lock);
3386 spa = spa_lookup(name);
3388 spa_altroot(spa, altroot, buflen);
3392 mutex_exit(&spa_namespace_lock);
3394 spa_altroot(spa, altroot, buflen);
3399 spa_config_exit(spa, SCL_CONFIG, FTAG);
3400 spa_close(spa, FTAG);
3407 * Validate that the auxiliary device array is well formed. We must have an
3408 * array of nvlists, each which describes a valid leaf vdev. If this is an
3409 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3410 * specified, as long as they are well-formed.
3413 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3414 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3415 vdev_labeltype_t label)
3422 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3425 * It's acceptable to have no devs specified.
3427 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3431 return (SET_ERROR(EINVAL));
3434 * Make sure the pool is formatted with a version that supports this
3437 if (spa_version(spa) < version)
3438 return (SET_ERROR(ENOTSUP));
3441 * Set the pending device list so we correctly handle device in-use
3444 sav->sav_pending = dev;
3445 sav->sav_npending = ndev;
3447 for (i = 0; i < ndev; i++) {
3448 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3452 if (!vd->vdev_ops->vdev_op_leaf) {
3454 error = SET_ERROR(EINVAL);
3459 * The L2ARC currently only supports disk devices in
3460 * kernel context. For user-level testing, we allow it.
3463 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3464 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3465 error = SET_ERROR(ENOTBLK);
3472 if ((error = vdev_open(vd)) == 0 &&
3473 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3474 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3475 vd->vdev_guid) == 0);
3481 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3488 sav->sav_pending = NULL;
3489 sav->sav_npending = 0;
3494 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3498 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3500 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3501 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3502 VDEV_LABEL_SPARE)) != 0) {
3506 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3507 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3508 VDEV_LABEL_L2CACHE));
3512 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3517 if (sav->sav_config != NULL) {
3523 * Generate new dev list by concatentating with the
3526 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3527 &olddevs, &oldndevs) == 0);
3529 newdevs = kmem_alloc(sizeof (void *) *
3530 (ndevs + oldndevs), KM_SLEEP);
3531 for (i = 0; i < oldndevs; i++)
3532 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3534 for (i = 0; i < ndevs; i++)
3535 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3538 VERIFY(nvlist_remove(sav->sav_config, config,
3539 DATA_TYPE_NVLIST_ARRAY) == 0);
3541 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3542 config, newdevs, ndevs + oldndevs) == 0);
3543 for (i = 0; i < oldndevs + ndevs; i++)
3544 nvlist_free(newdevs[i]);
3545 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3548 * Generate a new dev list.
3550 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3552 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3558 * Stop and drop level 2 ARC devices
3561 spa_l2cache_drop(spa_t *spa)
3565 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3567 for (i = 0; i < sav->sav_count; i++) {
3570 vd = sav->sav_vdevs[i];
3573 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3574 pool != 0ULL && l2arc_vdev_present(vd))
3575 l2arc_remove_vdev(vd);
3583 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3587 char *altroot = NULL;
3592 uint64_t txg = TXG_INITIAL;
3593 nvlist_t **spares, **l2cache;
3594 uint_t nspares, nl2cache;
3595 uint64_t version, obj;
3596 boolean_t has_features;
3599 * If this pool already exists, return failure.
3601 mutex_enter(&spa_namespace_lock);
3602 if (spa_lookup(pool) != NULL) {
3603 mutex_exit(&spa_namespace_lock);
3604 return (SET_ERROR(EEXIST));
3608 * Allocate a new spa_t structure.
3610 (void) nvlist_lookup_string(props,
3611 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3612 spa = spa_add(pool, NULL, altroot);
3613 spa_activate(spa, spa_mode_global);
3615 if (props && (error = spa_prop_validate(spa, props))) {
3616 spa_deactivate(spa);
3618 mutex_exit(&spa_namespace_lock);
3622 has_features = B_FALSE;
3623 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3624 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3625 if (zpool_prop_feature(nvpair_name(elem)))
3626 has_features = B_TRUE;
3629 if (has_features || nvlist_lookup_uint64(props,
3630 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3631 version = SPA_VERSION;
3633 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3635 spa->spa_first_txg = txg;
3636 spa->spa_uberblock.ub_txg = txg - 1;
3637 spa->spa_uberblock.ub_version = version;
3638 spa->spa_ubsync = spa->spa_uberblock;
3641 * Create "The Godfather" zio to hold all async IOs
3643 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3645 for (int i = 0; i < max_ncpus; i++) {
3646 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3647 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3648 ZIO_FLAG_GODFATHER);
3652 * Create the root vdev.
3654 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3656 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3658 ASSERT(error != 0 || rvd != NULL);
3659 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3661 if (error == 0 && !zfs_allocatable_devs(nvroot))
3662 error = SET_ERROR(EINVAL);
3665 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3666 (error = spa_validate_aux(spa, nvroot, txg,
3667 VDEV_ALLOC_ADD)) == 0) {
3668 for (int c = 0; c < rvd->vdev_children; c++) {
3669 vdev_ashift_optimize(rvd->vdev_child[c]);
3670 vdev_metaslab_set_size(rvd->vdev_child[c]);
3671 vdev_expand(rvd->vdev_child[c], txg);
3675 spa_config_exit(spa, SCL_ALL, FTAG);
3679 spa_deactivate(spa);
3681 mutex_exit(&spa_namespace_lock);
3686 * Get the list of spares, if specified.
3688 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3689 &spares, &nspares) == 0) {
3690 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3692 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3693 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3694 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3695 spa_load_spares(spa);
3696 spa_config_exit(spa, SCL_ALL, FTAG);
3697 spa->spa_spares.sav_sync = B_TRUE;
3701 * Get the list of level 2 cache devices, if specified.
3703 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3704 &l2cache, &nl2cache) == 0) {
3705 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3706 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3707 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3708 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3709 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3710 spa_load_l2cache(spa);
3711 spa_config_exit(spa, SCL_ALL, FTAG);
3712 spa->spa_l2cache.sav_sync = B_TRUE;
3715 spa->spa_is_initializing = B_TRUE;
3716 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3717 spa->spa_meta_objset = dp->dp_meta_objset;
3718 spa->spa_is_initializing = B_FALSE;
3721 * Create DDTs (dedup tables).
3725 spa_update_dspace(spa);
3727 tx = dmu_tx_create_assigned(dp, txg);
3730 * Create the pool config object.
3732 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3733 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3734 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3736 if (zap_add(spa->spa_meta_objset,
3737 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3738 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3739 cmn_err(CE_PANIC, "failed to add pool config");
3742 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3743 spa_feature_create_zap_objects(spa, tx);
3745 if (zap_add(spa->spa_meta_objset,
3746 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3747 sizeof (uint64_t), 1, &version, tx) != 0) {
3748 cmn_err(CE_PANIC, "failed to add pool version");
3751 /* Newly created pools with the right version are always deflated. */
3752 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3753 spa->spa_deflate = TRUE;
3754 if (zap_add(spa->spa_meta_objset,
3755 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3756 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3757 cmn_err(CE_PANIC, "failed to add deflate");
3762 * Create the deferred-free bpobj. Turn off compression
3763 * because sync-to-convergence takes longer if the blocksize
3766 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3767 dmu_object_set_compress(spa->spa_meta_objset, obj,
3768 ZIO_COMPRESS_OFF, tx);
3769 if (zap_add(spa->spa_meta_objset,
3770 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3771 sizeof (uint64_t), 1, &obj, tx) != 0) {
3772 cmn_err(CE_PANIC, "failed to add bpobj");
3774 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3775 spa->spa_meta_objset, obj));
3778 * Create the pool's history object.
3780 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3781 spa_history_create_obj(spa, tx);
3784 * Generate some random noise for salted checksums to operate on.
3786 (void) random_get_pseudo_bytes(spa->spa_cksum_salt.zcs_bytes,
3787 sizeof (spa->spa_cksum_salt.zcs_bytes));
3790 * Set pool properties.
3792 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3793 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3794 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3795 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3797 if (props != NULL) {
3798 spa_configfile_set(spa, props, B_FALSE);
3799 spa_sync_props(props, tx);
3804 spa->spa_sync_on = B_TRUE;
3805 txg_sync_start(spa->spa_dsl_pool);
3808 * We explicitly wait for the first transaction to complete so that our
3809 * bean counters are appropriately updated.
3811 txg_wait_synced(spa->spa_dsl_pool, txg);
3813 spa_config_sync(spa, B_FALSE, B_TRUE);
3814 spa_event_notify(spa, NULL, ESC_ZFS_POOL_CREATE);
3816 spa_history_log_version(spa, "create");
3819 * Don't count references from objsets that are already closed
3820 * and are making their way through the eviction process.
3822 spa_evicting_os_wait(spa);
3823 spa->spa_minref = refcount_count(&spa->spa_refcount);
3825 mutex_exit(&spa_namespace_lock);
3833 * Get the root pool information from the root disk, then import the root pool
3834 * during the system boot up time.
3836 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3839 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3842 nvlist_t *nvtop, *nvroot;
3845 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3849 * Add this top-level vdev to the child array.
3851 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3853 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3855 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3858 * Put this pool's top-level vdevs into a root vdev.
3860 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3861 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3862 VDEV_TYPE_ROOT) == 0);
3863 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3864 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3865 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3869 * Replace the existing vdev_tree with the new root vdev in
3870 * this pool's configuration (remove the old, add the new).
3872 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3873 nvlist_free(nvroot);
3878 * Walk the vdev tree and see if we can find a device with "better"
3879 * configuration. A configuration is "better" if the label on that
3880 * device has a more recent txg.
3883 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3885 for (int c = 0; c < vd->vdev_children; c++)
3886 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3888 if (vd->vdev_ops->vdev_op_leaf) {
3892 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3896 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3900 * Do we have a better boot device?
3902 if (label_txg > *txg) {
3911 * Import a root pool.
3913 * For x86. devpath_list will consist of devid and/or physpath name of
3914 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3915 * The GRUB "findroot" command will return the vdev we should boot.
3917 * For Sparc, devpath_list consists the physpath name of the booting device
3918 * no matter the rootpool is a single device pool or a mirrored pool.
3920 * "/pci@1f,0/ide@d/disk@0,0:a"
3923 spa_import_rootpool(char *devpath, char *devid)
3926 vdev_t *rvd, *bvd, *avd = NULL;
3927 nvlist_t *config, *nvtop;
3933 * Read the label from the boot device and generate a configuration.
3935 config = spa_generate_rootconf(devpath, devid, &guid);
3936 #if defined(_OBP) && defined(_KERNEL)
3937 if (config == NULL) {
3938 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3940 get_iscsi_bootpath_phy(devpath);
3941 config = spa_generate_rootconf(devpath, devid, &guid);
3945 if (config == NULL) {
3946 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3948 return (SET_ERROR(EIO));
3951 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3953 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3955 mutex_enter(&spa_namespace_lock);
3956 if ((spa = spa_lookup(pname)) != NULL) {
3958 * Remove the existing root pool from the namespace so that we
3959 * can replace it with the correct config we just read in.
3964 spa = spa_add(pname, config, NULL);
3965 spa->spa_is_root = B_TRUE;
3966 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3969 * Build up a vdev tree based on the boot device's label config.
3971 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3973 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3974 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3975 VDEV_ALLOC_ROOTPOOL);
3976 spa_config_exit(spa, SCL_ALL, FTAG);
3978 mutex_exit(&spa_namespace_lock);
3979 nvlist_free(config);
3980 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3986 * Get the boot vdev.
3988 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3989 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3990 (u_longlong_t)guid);
3991 error = SET_ERROR(ENOENT);
3996 * Determine if there is a better boot device.
3999 spa_alt_rootvdev(rvd, &avd, &txg);
4001 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
4002 "try booting from '%s'", avd->vdev_path);
4003 error = SET_ERROR(EINVAL);
4008 * If the boot device is part of a spare vdev then ensure that
4009 * we're booting off the active spare.
4011 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
4012 !bvd->vdev_isspare) {
4013 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
4014 "try booting from '%s'",
4016 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
4017 error = SET_ERROR(EINVAL);
4023 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4025 spa_config_exit(spa, SCL_ALL, FTAG);
4026 mutex_exit(&spa_namespace_lock);
4028 nvlist_free(config);
4032 #else /* !illumos */
4034 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
4038 spa_generate_rootconf(const char *name)
4040 nvlist_t **configs, **tops;
4042 nvlist_t *best_cfg, *nvtop, *nvroot;
4051 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
4054 ASSERT3U(count, !=, 0);
4056 for (i = 0; i < count; i++) {
4059 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
4061 if (txg > best_txg) {
4063 best_cfg = configs[i];
4068 * Multi-vdev root pool configuration discovery is not supported yet.
4071 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
4073 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
4076 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
4077 for (i = 0; i < nchildren; i++) {
4080 if (configs[i] == NULL)
4082 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
4084 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
4086 for (i = 0; holes != NULL && i < nholes; i++) {
4089 if (tops[holes[i]] != NULL)
4091 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
4092 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
4093 VDEV_TYPE_HOLE) == 0);
4094 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
4096 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4099 for (i = 0; i < nchildren; i++) {
4100 if (tops[i] != NULL)
4102 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4103 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4104 VDEV_TYPE_MISSING) == 0);
4105 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4107 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4112 * Create pool config based on the best vdev config.
4114 nvlist_dup(best_cfg, &config, KM_SLEEP);
4117 * Put this pool's top-level vdevs into a root vdev.
4119 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4121 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4122 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4123 VDEV_TYPE_ROOT) == 0);
4124 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4125 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4126 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4127 tops, nchildren) == 0);
4130 * Replace the existing vdev_tree with the new root vdev in
4131 * this pool's configuration (remove the old, add the new).
4133 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4136 * Drop vdev config elements that should not be present at pool level.
4138 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4139 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4141 for (i = 0; i < count; i++)
4142 nvlist_free(configs[i]);
4143 kmem_free(configs, count * sizeof(void *));
4144 for (i = 0; i < nchildren; i++)
4145 nvlist_free(tops[i]);
4146 kmem_free(tops, nchildren * sizeof(void *));
4147 nvlist_free(nvroot);
4152 spa_import_rootpool(const char *name)
4155 vdev_t *rvd, *bvd, *avd = NULL;
4156 nvlist_t *config, *nvtop;
4162 * Read the label from the boot device and generate a configuration.
4164 config = spa_generate_rootconf(name);
4166 mutex_enter(&spa_namespace_lock);
4167 if (config != NULL) {
4168 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4169 &pname) == 0 && strcmp(name, pname) == 0);
4170 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4173 if ((spa = spa_lookup(pname)) != NULL) {
4175 * Remove the existing root pool from the namespace so
4176 * that we can replace it with the correct config
4181 spa = spa_add(pname, config, NULL);
4184 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4185 * via spa_version().
4187 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4188 &spa->spa_ubsync.ub_version) != 0)
4189 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4190 } else if ((spa = spa_lookup(name)) == NULL) {
4191 mutex_exit(&spa_namespace_lock);
4192 nvlist_free(config);
4193 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4197 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4199 spa->spa_is_root = B_TRUE;
4200 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4203 * Build up a vdev tree based on the boot device's label config.
4205 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4207 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4208 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4209 VDEV_ALLOC_ROOTPOOL);
4210 spa_config_exit(spa, SCL_ALL, FTAG);
4212 mutex_exit(&spa_namespace_lock);
4213 nvlist_free(config);
4214 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4219 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4221 spa_config_exit(spa, SCL_ALL, FTAG);
4222 mutex_exit(&spa_namespace_lock);
4224 nvlist_free(config);
4228 #endif /* illumos */
4229 #endif /* _KERNEL */
4232 * Import a non-root pool into the system.
4235 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4238 char *altroot = NULL;
4239 spa_load_state_t state = SPA_LOAD_IMPORT;
4240 zpool_rewind_policy_t policy;
4241 uint64_t mode = spa_mode_global;
4242 uint64_t readonly = B_FALSE;
4245 nvlist_t **spares, **l2cache;
4246 uint_t nspares, nl2cache;
4249 * If a pool with this name exists, return failure.
4251 mutex_enter(&spa_namespace_lock);
4252 if (spa_lookup(pool) != NULL) {
4253 mutex_exit(&spa_namespace_lock);
4254 return (SET_ERROR(EEXIST));
4258 * Create and initialize the spa structure.
4260 (void) nvlist_lookup_string(props,
4261 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4262 (void) nvlist_lookup_uint64(props,
4263 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4266 spa = spa_add(pool, config, altroot);
4267 spa->spa_import_flags = flags;
4270 * Verbatim import - Take a pool and insert it into the namespace
4271 * as if it had been loaded at boot.
4273 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4275 spa_configfile_set(spa, props, B_FALSE);
4277 spa_config_sync(spa, B_FALSE, B_TRUE);
4278 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4280 mutex_exit(&spa_namespace_lock);
4284 spa_activate(spa, mode);
4287 * Don't start async tasks until we know everything is healthy.
4289 spa_async_suspend(spa);
4291 zpool_get_rewind_policy(config, &policy);
4292 if (policy.zrp_request & ZPOOL_DO_REWIND)
4293 state = SPA_LOAD_RECOVER;
4296 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4297 * because the user-supplied config is actually the one to trust when
4300 if (state != SPA_LOAD_RECOVER)
4301 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4303 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4304 policy.zrp_request);
4307 * Propagate anything learned while loading the pool and pass it
4308 * back to caller (i.e. rewind info, missing devices, etc).
4310 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4311 spa->spa_load_info) == 0);
4313 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4315 * Toss any existing sparelist, as it doesn't have any validity
4316 * anymore, and conflicts with spa_has_spare().
4318 if (spa->spa_spares.sav_config) {
4319 nvlist_free(spa->spa_spares.sav_config);
4320 spa->spa_spares.sav_config = NULL;
4321 spa_load_spares(spa);
4323 if (spa->spa_l2cache.sav_config) {
4324 nvlist_free(spa->spa_l2cache.sav_config);
4325 spa->spa_l2cache.sav_config = NULL;
4326 spa_load_l2cache(spa);
4329 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4332 error = spa_validate_aux(spa, nvroot, -1ULL,
4335 error = spa_validate_aux(spa, nvroot, -1ULL,
4336 VDEV_ALLOC_L2CACHE);
4337 spa_config_exit(spa, SCL_ALL, FTAG);
4340 spa_configfile_set(spa, props, B_FALSE);
4342 if (error != 0 || (props && spa_writeable(spa) &&
4343 (error = spa_prop_set(spa, props)))) {
4345 spa_deactivate(spa);
4347 mutex_exit(&spa_namespace_lock);
4351 spa_async_resume(spa);
4354 * Override any spares and level 2 cache devices as specified by
4355 * the user, as these may have correct device names/devids, etc.
4357 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4358 &spares, &nspares) == 0) {
4359 if (spa->spa_spares.sav_config)
4360 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4361 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4363 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4364 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4365 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4366 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4367 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4368 spa_load_spares(spa);
4369 spa_config_exit(spa, SCL_ALL, FTAG);
4370 spa->spa_spares.sav_sync = B_TRUE;
4372 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4373 &l2cache, &nl2cache) == 0) {
4374 if (spa->spa_l2cache.sav_config)
4375 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4376 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4378 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4379 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4380 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4381 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4382 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4383 spa_load_l2cache(spa);
4384 spa_config_exit(spa, SCL_ALL, FTAG);
4385 spa->spa_l2cache.sav_sync = B_TRUE;
4389 * Check for any removed devices.
4391 if (spa->spa_autoreplace) {
4392 spa_aux_check_removed(&spa->spa_spares);
4393 spa_aux_check_removed(&spa->spa_l2cache);
4396 if (spa_writeable(spa)) {
4398 * Update the config cache to include the newly-imported pool.
4400 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4404 * It's possible that the pool was expanded while it was exported.
4405 * We kick off an async task to handle this for us.
4407 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4409 spa_history_log_version(spa, "import");
4411 spa_event_notify(spa, NULL, ESC_ZFS_POOL_IMPORT);
4413 mutex_exit(&spa_namespace_lock);
4417 zvol_create_minors(pool);
4424 spa_tryimport(nvlist_t *tryconfig)
4426 nvlist_t *config = NULL;
4432 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4435 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4439 * Create and initialize the spa structure.
4441 mutex_enter(&spa_namespace_lock);
4442 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4443 spa_activate(spa, FREAD);
4446 * Pass off the heavy lifting to spa_load().
4447 * Pass TRUE for mosconfig because the user-supplied config
4448 * is actually the one to trust when doing an import.
4450 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4453 * If 'tryconfig' was at least parsable, return the current config.
4455 if (spa->spa_root_vdev != NULL) {
4456 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4457 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4459 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4461 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4462 spa->spa_uberblock.ub_timestamp) == 0);
4463 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4464 spa->spa_load_info) == 0);
4467 * If the bootfs property exists on this pool then we
4468 * copy it out so that external consumers can tell which
4469 * pools are bootable.
4471 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4472 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4475 * We have to play games with the name since the
4476 * pool was opened as TRYIMPORT_NAME.
4478 if (dsl_dsobj_to_dsname(spa_name(spa),
4479 spa->spa_bootfs, tmpname) == 0) {
4481 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4483 cp = strchr(tmpname, '/');
4485 (void) strlcpy(dsname, tmpname,
4488 (void) snprintf(dsname, MAXPATHLEN,
4489 "%s/%s", poolname, ++cp);
4491 VERIFY(nvlist_add_string(config,
4492 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4493 kmem_free(dsname, MAXPATHLEN);
4495 kmem_free(tmpname, MAXPATHLEN);
4499 * Add the list of hot spares and level 2 cache devices.
4501 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4502 spa_add_spares(spa, config);
4503 spa_add_l2cache(spa, config);
4504 spa_config_exit(spa, SCL_CONFIG, FTAG);
4508 spa_deactivate(spa);
4510 mutex_exit(&spa_namespace_lock);
4516 * Pool export/destroy
4518 * The act of destroying or exporting a pool is very simple. We make sure there
4519 * is no more pending I/O and any references to the pool are gone. Then, we
4520 * update the pool state and sync all the labels to disk, removing the
4521 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4522 * we don't sync the labels or remove the configuration cache.
4525 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4526 boolean_t force, boolean_t hardforce)
4533 if (!(spa_mode_global & FWRITE))
4534 return (SET_ERROR(EROFS));
4536 mutex_enter(&spa_namespace_lock);
4537 if ((spa = spa_lookup(pool)) == NULL) {
4538 mutex_exit(&spa_namespace_lock);
4539 return (SET_ERROR(ENOENT));
4543 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4544 * reacquire the namespace lock, and see if we can export.
4546 spa_open_ref(spa, FTAG);
4547 mutex_exit(&spa_namespace_lock);
4548 spa_async_suspend(spa);
4549 mutex_enter(&spa_namespace_lock);
4550 spa_close(spa, FTAG);
4553 * The pool will be in core if it's openable,
4554 * in which case we can modify its state.
4556 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4558 * Objsets may be open only because they're dirty, so we
4559 * have to force it to sync before checking spa_refcnt.
4561 txg_wait_synced(spa->spa_dsl_pool, 0);
4562 spa_evicting_os_wait(spa);
4565 * A pool cannot be exported or destroyed if there are active
4566 * references. If we are resetting a pool, allow references by
4567 * fault injection handlers.
4569 if (!spa_refcount_zero(spa) ||
4570 (spa->spa_inject_ref != 0 &&
4571 new_state != POOL_STATE_UNINITIALIZED)) {
4572 spa_async_resume(spa);
4573 mutex_exit(&spa_namespace_lock);
4574 return (SET_ERROR(EBUSY));
4578 * A pool cannot be exported if it has an active shared spare.
4579 * This is to prevent other pools stealing the active spare
4580 * from an exported pool. At user's own will, such pool can
4581 * be forcedly exported.
4583 if (!force && new_state == POOL_STATE_EXPORTED &&
4584 spa_has_active_shared_spare(spa)) {
4585 spa_async_resume(spa);
4586 mutex_exit(&spa_namespace_lock);
4587 return (SET_ERROR(EXDEV));
4591 * We want this to be reflected on every label,
4592 * so mark them all dirty. spa_unload() will do the
4593 * final sync that pushes these changes out.
4595 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4596 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4597 spa->spa_state = new_state;
4598 spa->spa_final_txg = spa_last_synced_txg(spa) +
4600 vdev_config_dirty(spa->spa_root_vdev);
4601 spa_config_exit(spa, SCL_ALL, FTAG);
4605 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4607 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4609 spa_deactivate(spa);
4612 if (oldconfig && spa->spa_config)
4613 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4615 if (new_state != POOL_STATE_UNINITIALIZED) {
4617 spa_config_sync(spa, B_TRUE, B_TRUE);
4620 mutex_exit(&spa_namespace_lock);
4626 * Destroy a storage pool.
4629 spa_destroy(char *pool)
4631 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4636 * Export a storage pool.
4639 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4640 boolean_t hardforce)
4642 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4647 * Similar to spa_export(), this unloads the spa_t without actually removing it
4648 * from the namespace in any way.
4651 spa_reset(char *pool)
4653 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4658 * ==========================================================================
4659 * Device manipulation
4660 * ==========================================================================
4664 * Add a device to a storage pool.
4667 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4671 vdev_t *rvd = spa->spa_root_vdev;
4673 nvlist_t **spares, **l2cache;
4674 uint_t nspares, nl2cache;
4676 ASSERT(spa_writeable(spa));
4678 txg = spa_vdev_enter(spa);
4680 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4681 VDEV_ALLOC_ADD)) != 0)
4682 return (spa_vdev_exit(spa, NULL, txg, error));
4684 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4686 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4690 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4694 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4695 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4697 if (vd->vdev_children != 0 &&
4698 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4699 return (spa_vdev_exit(spa, vd, txg, error));
4702 * We must validate the spares and l2cache devices after checking the
4703 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4705 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4706 return (spa_vdev_exit(spa, vd, txg, error));
4709 * Transfer each new top-level vdev from vd to rvd.
4711 for (int c = 0; c < vd->vdev_children; c++) {
4714 * Set the vdev id to the first hole, if one exists.
4716 for (id = 0; id < rvd->vdev_children; id++) {
4717 if (rvd->vdev_child[id]->vdev_ishole) {
4718 vdev_free(rvd->vdev_child[id]);
4722 tvd = vd->vdev_child[c];
4723 vdev_remove_child(vd, tvd);
4725 vdev_add_child(rvd, tvd);
4726 vdev_config_dirty(tvd);
4730 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4731 ZPOOL_CONFIG_SPARES);
4732 spa_load_spares(spa);
4733 spa->spa_spares.sav_sync = B_TRUE;
4736 if (nl2cache != 0) {
4737 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4738 ZPOOL_CONFIG_L2CACHE);
4739 spa_load_l2cache(spa);
4740 spa->spa_l2cache.sav_sync = B_TRUE;
4744 * We have to be careful when adding new vdevs to an existing pool.
4745 * If other threads start allocating from these vdevs before we
4746 * sync the config cache, and we lose power, then upon reboot we may
4747 * fail to open the pool because there are DVAs that the config cache
4748 * can't translate. Therefore, we first add the vdevs without
4749 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4750 * and then let spa_config_update() initialize the new metaslabs.
4752 * spa_load() checks for added-but-not-initialized vdevs, so that
4753 * if we lose power at any point in this sequence, the remaining
4754 * steps will be completed the next time we load the pool.
4756 (void) spa_vdev_exit(spa, vd, txg, 0);
4758 mutex_enter(&spa_namespace_lock);
4759 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4760 spa_event_notify(spa, NULL, ESC_ZFS_VDEV_ADD);
4761 mutex_exit(&spa_namespace_lock);
4767 * Attach a device to a mirror. The arguments are the path to any device
4768 * in the mirror, and the nvroot for the new device. If the path specifies
4769 * a device that is not mirrored, we automatically insert the mirror vdev.
4771 * If 'replacing' is specified, the new device is intended to replace the
4772 * existing device; in this case the two devices are made into their own
4773 * mirror using the 'replacing' vdev, which is functionally identical to
4774 * the mirror vdev (it actually reuses all the same ops) but has a few
4775 * extra rules: you can't attach to it after it's been created, and upon
4776 * completion of resilvering, the first disk (the one being replaced)
4777 * is automatically detached.
4780 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4782 uint64_t txg, dtl_max_txg;
4783 vdev_t *rvd = spa->spa_root_vdev;
4784 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4786 char *oldvdpath, *newvdpath;
4790 ASSERT(spa_writeable(spa));
4792 txg = spa_vdev_enter(spa);
4794 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4797 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4799 if (!oldvd->vdev_ops->vdev_op_leaf)
4800 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4802 pvd = oldvd->vdev_parent;
4804 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4805 VDEV_ALLOC_ATTACH)) != 0)
4806 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4808 if (newrootvd->vdev_children != 1)
4809 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4811 newvd = newrootvd->vdev_child[0];
4813 if (!newvd->vdev_ops->vdev_op_leaf)
4814 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4816 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4817 return (spa_vdev_exit(spa, newrootvd, txg, error));
4820 * Spares can't replace logs
4822 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4823 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4827 * For attach, the only allowable parent is a mirror or the root
4830 if (pvd->vdev_ops != &vdev_mirror_ops &&
4831 pvd->vdev_ops != &vdev_root_ops)
4832 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4834 pvops = &vdev_mirror_ops;
4837 * Active hot spares can only be replaced by inactive hot
4840 if (pvd->vdev_ops == &vdev_spare_ops &&
4841 oldvd->vdev_isspare &&
4842 !spa_has_spare(spa, newvd->vdev_guid))
4843 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4846 * If the source is a hot spare, and the parent isn't already a
4847 * spare, then we want to create a new hot spare. Otherwise, we
4848 * want to create a replacing vdev. The user is not allowed to
4849 * attach to a spared vdev child unless the 'isspare' state is
4850 * the same (spare replaces spare, non-spare replaces
4853 if (pvd->vdev_ops == &vdev_replacing_ops &&
4854 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4855 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4856 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4857 newvd->vdev_isspare != oldvd->vdev_isspare) {
4858 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4861 if (newvd->vdev_isspare)
4862 pvops = &vdev_spare_ops;
4864 pvops = &vdev_replacing_ops;
4868 * Make sure the new device is big enough.
4870 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4871 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4874 * The new device cannot have a higher alignment requirement
4875 * than the top-level vdev.
4877 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4878 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4881 * If this is an in-place replacement, update oldvd's path and devid
4882 * to make it distinguishable from newvd, and unopenable from now on.
4884 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4885 spa_strfree(oldvd->vdev_path);
4886 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4888 (void) sprintf(oldvd->vdev_path, "%s/%s",
4889 newvd->vdev_path, "old");
4890 if (oldvd->vdev_devid != NULL) {
4891 spa_strfree(oldvd->vdev_devid);
4892 oldvd->vdev_devid = NULL;
4896 /* mark the device being resilvered */
4897 newvd->vdev_resilver_txg = txg;
4900 * If the parent is not a mirror, or if we're replacing, insert the new
4901 * mirror/replacing/spare vdev above oldvd.
4903 if (pvd->vdev_ops != pvops)
4904 pvd = vdev_add_parent(oldvd, pvops);
4906 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4907 ASSERT(pvd->vdev_ops == pvops);
4908 ASSERT(oldvd->vdev_parent == pvd);
4911 * Extract the new device from its root and add it to pvd.
4913 vdev_remove_child(newrootvd, newvd);
4914 newvd->vdev_id = pvd->vdev_children;
4915 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4916 vdev_add_child(pvd, newvd);
4918 tvd = newvd->vdev_top;
4919 ASSERT(pvd->vdev_top == tvd);
4920 ASSERT(tvd->vdev_parent == rvd);
4922 vdev_config_dirty(tvd);
4925 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4926 * for any dmu_sync-ed blocks. It will propagate upward when
4927 * spa_vdev_exit() calls vdev_dtl_reassess().
4929 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4931 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4932 dtl_max_txg - TXG_INITIAL);
4934 if (newvd->vdev_isspare) {
4935 spa_spare_activate(newvd);
4936 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4939 oldvdpath = spa_strdup(oldvd->vdev_path);
4940 newvdpath = spa_strdup(newvd->vdev_path);
4941 newvd_isspare = newvd->vdev_isspare;
4944 * Mark newvd's DTL dirty in this txg.
4946 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4949 * Schedule the resilver to restart in the future. We do this to
4950 * ensure that dmu_sync-ed blocks have been stitched into the
4951 * respective datasets.
4953 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4955 if (spa->spa_bootfs)
4956 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4958 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_ATTACH);
4963 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4965 spa_history_log_internal(spa, "vdev attach", NULL,
4966 "%s vdev=%s %s vdev=%s",
4967 replacing && newvd_isspare ? "spare in" :
4968 replacing ? "replace" : "attach", newvdpath,
4969 replacing ? "for" : "to", oldvdpath);
4971 spa_strfree(oldvdpath);
4972 spa_strfree(newvdpath);
4978 * Detach a device from a mirror or replacing vdev.
4980 * If 'replace_done' is specified, only detach if the parent
4981 * is a replacing vdev.
4984 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4988 vdev_t *rvd = spa->spa_root_vdev;
4989 vdev_t *vd, *pvd, *cvd, *tvd;
4990 boolean_t unspare = B_FALSE;
4991 uint64_t unspare_guid = 0;
4994 ASSERT(spa_writeable(spa));
4996 txg = spa_vdev_enter(spa);
4998 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5001 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
5003 if (!vd->vdev_ops->vdev_op_leaf)
5004 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5006 pvd = vd->vdev_parent;
5009 * If the parent/child relationship is not as expected, don't do it.
5010 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
5011 * vdev that's replacing B with C. The user's intent in replacing
5012 * is to go from M(A,B) to M(A,C). If the user decides to cancel
5013 * the replace by detaching C, the expected behavior is to end up
5014 * M(A,B). But suppose that right after deciding to detach C,
5015 * the replacement of B completes. We would have M(A,C), and then
5016 * ask to detach C, which would leave us with just A -- not what
5017 * the user wanted. To prevent this, we make sure that the
5018 * parent/child relationship hasn't changed -- in this example,
5019 * that C's parent is still the replacing vdev R.
5021 if (pvd->vdev_guid != pguid && pguid != 0)
5022 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5025 * Only 'replacing' or 'spare' vdevs can be replaced.
5027 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
5028 pvd->vdev_ops != &vdev_spare_ops)
5029 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5031 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
5032 spa_version(spa) >= SPA_VERSION_SPARES);
5035 * Only mirror, replacing, and spare vdevs support detach.
5037 if (pvd->vdev_ops != &vdev_replacing_ops &&
5038 pvd->vdev_ops != &vdev_mirror_ops &&
5039 pvd->vdev_ops != &vdev_spare_ops)
5040 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
5043 * If this device has the only valid copy of some data,
5044 * we cannot safely detach it.
5046 if (vdev_dtl_required(vd))
5047 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
5049 ASSERT(pvd->vdev_children >= 2);
5052 * If we are detaching the second disk from a replacing vdev, then
5053 * check to see if we changed the original vdev's path to have "/old"
5054 * at the end in spa_vdev_attach(). If so, undo that change now.
5056 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
5057 vd->vdev_path != NULL) {
5058 size_t len = strlen(vd->vdev_path);
5060 for (int c = 0; c < pvd->vdev_children; c++) {
5061 cvd = pvd->vdev_child[c];
5063 if (cvd == vd || cvd->vdev_path == NULL)
5066 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
5067 strcmp(cvd->vdev_path + len, "/old") == 0) {
5068 spa_strfree(cvd->vdev_path);
5069 cvd->vdev_path = spa_strdup(vd->vdev_path);
5076 * If we are detaching the original disk from a spare, then it implies
5077 * that the spare should become a real disk, and be removed from the
5078 * active spare list for the pool.
5080 if (pvd->vdev_ops == &vdev_spare_ops &&
5082 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
5086 * Erase the disk labels so the disk can be used for other things.
5087 * This must be done after all other error cases are handled,
5088 * but before we disembowel vd (so we can still do I/O to it).
5089 * But if we can't do it, don't treat the error as fatal --
5090 * it may be that the unwritability of the disk is the reason
5091 * it's being detached!
5093 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5096 * Remove vd from its parent and compact the parent's children.
5098 vdev_remove_child(pvd, vd);
5099 vdev_compact_children(pvd);
5102 * Remember one of the remaining children so we can get tvd below.
5104 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5107 * If we need to remove the remaining child from the list of hot spares,
5108 * do it now, marking the vdev as no longer a spare in the process.
5109 * We must do this before vdev_remove_parent(), because that can
5110 * change the GUID if it creates a new toplevel GUID. For a similar
5111 * reason, we must remove the spare now, in the same txg as the detach;
5112 * otherwise someone could attach a new sibling, change the GUID, and
5113 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5116 ASSERT(cvd->vdev_isspare);
5117 spa_spare_remove(cvd);
5118 unspare_guid = cvd->vdev_guid;
5119 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5120 cvd->vdev_unspare = B_TRUE;
5124 * If the parent mirror/replacing vdev only has one child,
5125 * the parent is no longer needed. Remove it from the tree.
5127 if (pvd->vdev_children == 1) {
5128 if (pvd->vdev_ops == &vdev_spare_ops)
5129 cvd->vdev_unspare = B_FALSE;
5130 vdev_remove_parent(cvd);
5135 * We don't set tvd until now because the parent we just removed
5136 * may have been the previous top-level vdev.
5138 tvd = cvd->vdev_top;
5139 ASSERT(tvd->vdev_parent == rvd);
5142 * Reevaluate the parent vdev state.
5144 vdev_propagate_state(cvd);
5147 * If the 'autoexpand' property is set on the pool then automatically
5148 * try to expand the size of the pool. For example if the device we
5149 * just detached was smaller than the others, it may be possible to
5150 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5151 * first so that we can obtain the updated sizes of the leaf vdevs.
5153 if (spa->spa_autoexpand) {
5155 vdev_expand(tvd, txg);
5158 vdev_config_dirty(tvd);
5161 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5162 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5163 * But first make sure we're not on any *other* txg's DTL list, to
5164 * prevent vd from being accessed after it's freed.
5166 vdpath = spa_strdup(vd->vdev_path);
5167 for (int t = 0; t < TXG_SIZE; t++)
5168 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5169 vd->vdev_detached = B_TRUE;
5170 vdev_dirty(tvd, VDD_DTL, vd, txg);
5172 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5174 /* hang on to the spa before we release the lock */
5175 spa_open_ref(spa, FTAG);
5177 error = spa_vdev_exit(spa, vd, txg, 0);
5179 spa_history_log_internal(spa, "detach", NULL,
5181 spa_strfree(vdpath);
5184 * If this was the removal of the original device in a hot spare vdev,
5185 * then we want to go through and remove the device from the hot spare
5186 * list of every other pool.
5189 spa_t *altspa = NULL;
5191 mutex_enter(&spa_namespace_lock);
5192 while ((altspa = spa_next(altspa)) != NULL) {
5193 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5197 spa_open_ref(altspa, FTAG);
5198 mutex_exit(&spa_namespace_lock);
5199 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5200 mutex_enter(&spa_namespace_lock);
5201 spa_close(altspa, FTAG);
5203 mutex_exit(&spa_namespace_lock);
5205 /* search the rest of the vdevs for spares to remove */
5206 spa_vdev_resilver_done(spa);
5209 /* all done with the spa; OK to release */
5210 mutex_enter(&spa_namespace_lock);
5211 spa_close(spa, FTAG);
5212 mutex_exit(&spa_namespace_lock);
5218 * Split a set of devices from their mirrors, and create a new pool from them.
5221 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5222 nvlist_t *props, boolean_t exp)
5225 uint64_t txg, *glist;
5227 uint_t c, children, lastlog;
5228 nvlist_t **child, *nvl, *tmp;
5230 char *altroot = NULL;
5231 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5232 boolean_t activate_slog;
5234 ASSERT(spa_writeable(spa));
5236 txg = spa_vdev_enter(spa);
5238 /* clear the log and flush everything up to now */
5239 activate_slog = spa_passivate_log(spa);
5240 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5241 error = spa_offline_log(spa);
5242 txg = spa_vdev_config_enter(spa);
5245 spa_activate_log(spa);
5248 return (spa_vdev_exit(spa, NULL, txg, error));
5250 /* check new spa name before going any further */
5251 if (spa_lookup(newname) != NULL)
5252 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5255 * scan through all the children to ensure they're all mirrors
5257 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5258 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5260 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5262 /* first, check to ensure we've got the right child count */
5263 rvd = spa->spa_root_vdev;
5265 for (c = 0; c < rvd->vdev_children; c++) {
5266 vdev_t *vd = rvd->vdev_child[c];
5268 /* don't count the holes & logs as children */
5269 if (vd->vdev_islog || vd->vdev_ishole) {
5277 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5278 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5280 /* next, ensure no spare or cache devices are part of the split */
5281 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5282 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5283 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5285 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5286 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5288 /* then, loop over each vdev and validate it */
5289 for (c = 0; c < children; c++) {
5290 uint64_t is_hole = 0;
5292 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5296 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5297 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5300 error = SET_ERROR(EINVAL);
5305 /* which disk is going to be split? */
5306 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5308 error = SET_ERROR(EINVAL);
5312 /* look it up in the spa */
5313 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5314 if (vml[c] == NULL) {
5315 error = SET_ERROR(ENODEV);
5319 /* make sure there's nothing stopping the split */
5320 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5321 vml[c]->vdev_islog ||
5322 vml[c]->vdev_ishole ||
5323 vml[c]->vdev_isspare ||
5324 vml[c]->vdev_isl2cache ||
5325 !vdev_writeable(vml[c]) ||
5326 vml[c]->vdev_children != 0 ||
5327 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5328 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5329 error = SET_ERROR(EINVAL);
5333 if (vdev_dtl_required(vml[c])) {
5334 error = SET_ERROR(EBUSY);
5338 /* we need certain info from the top level */
5339 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5340 vml[c]->vdev_top->vdev_ms_array) == 0);
5341 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5342 vml[c]->vdev_top->vdev_ms_shift) == 0);
5343 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5344 vml[c]->vdev_top->vdev_asize) == 0);
5345 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5346 vml[c]->vdev_top->vdev_ashift) == 0);
5350 kmem_free(vml, children * sizeof (vdev_t *));
5351 kmem_free(glist, children * sizeof (uint64_t));
5352 return (spa_vdev_exit(spa, NULL, txg, error));
5355 /* stop writers from using the disks */
5356 for (c = 0; c < children; c++) {
5358 vml[c]->vdev_offline = B_TRUE;
5360 vdev_reopen(spa->spa_root_vdev);
5363 * Temporarily record the splitting vdevs in the spa config. This
5364 * will disappear once the config is regenerated.
5366 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5367 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5368 glist, children) == 0);
5369 kmem_free(glist, children * sizeof (uint64_t));
5371 mutex_enter(&spa->spa_props_lock);
5372 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5374 mutex_exit(&spa->spa_props_lock);
5375 spa->spa_config_splitting = nvl;
5376 vdev_config_dirty(spa->spa_root_vdev);
5378 /* configure and create the new pool */
5379 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5380 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5381 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5382 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5383 spa_version(spa)) == 0);
5384 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5385 spa->spa_config_txg) == 0);
5386 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5387 spa_generate_guid(NULL)) == 0);
5388 (void) nvlist_lookup_string(props,
5389 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5391 /* add the new pool to the namespace */
5392 newspa = spa_add(newname, config, altroot);
5393 newspa->spa_config_txg = spa->spa_config_txg;
5394 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5396 /* release the spa config lock, retaining the namespace lock */
5397 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5399 if (zio_injection_enabled)
5400 zio_handle_panic_injection(spa, FTAG, 1);
5402 spa_activate(newspa, spa_mode_global);
5403 spa_async_suspend(newspa);
5406 /* mark that we are creating new spa by splitting */
5407 newspa->spa_splitting_newspa = B_TRUE;
5409 /* create the new pool from the disks of the original pool */
5410 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5412 newspa->spa_splitting_newspa = B_FALSE;
5417 /* if that worked, generate a real config for the new pool */
5418 if (newspa->spa_root_vdev != NULL) {
5419 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5420 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5421 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5422 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5423 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5428 if (props != NULL) {
5429 spa_configfile_set(newspa, props, B_FALSE);
5430 error = spa_prop_set(newspa, props);
5435 /* flush everything */
5436 txg = spa_vdev_config_enter(newspa);
5437 vdev_config_dirty(newspa->spa_root_vdev);
5438 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5440 if (zio_injection_enabled)
5441 zio_handle_panic_injection(spa, FTAG, 2);
5443 spa_async_resume(newspa);
5445 /* finally, update the original pool's config */
5446 txg = spa_vdev_config_enter(spa);
5447 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5448 error = dmu_tx_assign(tx, TXG_WAIT);
5451 for (c = 0; c < children; c++) {
5452 if (vml[c] != NULL) {
5455 spa_history_log_internal(spa, "detach", tx,
5456 "vdev=%s", vml[c]->vdev_path);
5460 vdev_config_dirty(spa->spa_root_vdev);
5461 spa->spa_config_splitting = NULL;
5465 (void) spa_vdev_exit(spa, NULL, txg, 0);
5467 if (zio_injection_enabled)
5468 zio_handle_panic_injection(spa, FTAG, 3);
5470 /* split is complete; log a history record */
5471 spa_history_log_internal(newspa, "split", NULL,
5472 "from pool %s", spa_name(spa));
5474 kmem_free(vml, children * sizeof (vdev_t *));
5476 /* if we're not going to mount the filesystems in userland, export */
5478 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5485 spa_deactivate(newspa);
5488 txg = spa_vdev_config_enter(spa);
5490 /* re-online all offlined disks */
5491 for (c = 0; c < children; c++) {
5493 vml[c]->vdev_offline = B_FALSE;
5495 vdev_reopen(spa->spa_root_vdev);
5497 nvlist_free(spa->spa_config_splitting);
5498 spa->spa_config_splitting = NULL;
5499 (void) spa_vdev_exit(spa, NULL, txg, error);
5501 kmem_free(vml, children * sizeof (vdev_t *));
5506 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5508 for (int i = 0; i < count; i++) {
5511 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5514 if (guid == target_guid)
5522 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5523 nvlist_t *dev_to_remove)
5525 nvlist_t **newdev = NULL;
5528 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5530 for (int i = 0, j = 0; i < count; i++) {
5531 if (dev[i] == dev_to_remove)
5533 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5536 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5537 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5539 for (int i = 0; i < count - 1; i++)
5540 nvlist_free(newdev[i]);
5543 kmem_free(newdev, (count - 1) * sizeof (void *));
5547 * Evacuate the device.
5550 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5555 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5556 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5557 ASSERT(vd == vd->vdev_top);
5560 * Evacuate the device. We don't hold the config lock as writer
5561 * since we need to do I/O but we do keep the
5562 * spa_namespace_lock held. Once this completes the device
5563 * should no longer have any blocks allocated on it.
5565 if (vd->vdev_islog) {
5566 if (vd->vdev_stat.vs_alloc != 0)
5567 error = spa_offline_log(spa);
5569 error = SET_ERROR(ENOTSUP);
5576 * The evacuation succeeded. Remove any remaining MOS metadata
5577 * associated with this vdev, and wait for these changes to sync.
5579 ASSERT0(vd->vdev_stat.vs_alloc);
5580 txg = spa_vdev_config_enter(spa);
5581 vd->vdev_removing = B_TRUE;
5582 vdev_dirty_leaves(vd, VDD_DTL, txg);
5583 vdev_config_dirty(vd);
5584 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5590 * Complete the removal by cleaning up the namespace.
5593 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5595 vdev_t *rvd = spa->spa_root_vdev;
5596 uint64_t id = vd->vdev_id;
5597 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5599 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5600 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5601 ASSERT(vd == vd->vdev_top);
5604 * Only remove any devices which are empty.
5606 if (vd->vdev_stat.vs_alloc != 0)
5609 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5611 if (list_link_active(&vd->vdev_state_dirty_node))
5612 vdev_state_clean(vd);
5613 if (list_link_active(&vd->vdev_config_dirty_node))
5614 vdev_config_clean(vd);
5619 vdev_compact_children(rvd);
5621 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5622 vdev_add_child(rvd, vd);
5624 vdev_config_dirty(rvd);
5627 * Reassess the health of our root vdev.
5633 * Remove a device from the pool -
5635 * Removing a device from the vdev namespace requires several steps
5636 * and can take a significant amount of time. As a result we use
5637 * the spa_vdev_config_[enter/exit] functions which allow us to
5638 * grab and release the spa_config_lock while still holding the namespace
5639 * lock. During each step the configuration is synced out.
5641 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5645 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5648 metaslab_group_t *mg;
5649 nvlist_t **spares, **l2cache, *nv;
5651 uint_t nspares, nl2cache;
5653 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5655 ASSERT(spa_writeable(spa));
5658 txg = spa_vdev_enter(spa);
5660 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5662 if (spa->spa_spares.sav_vdevs != NULL &&
5663 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5664 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5665 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5667 * Only remove the hot spare if it's not currently in use
5670 if (vd == NULL || unspare) {
5671 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5672 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5673 spa_load_spares(spa);
5674 spa->spa_spares.sav_sync = B_TRUE;
5676 error = SET_ERROR(EBUSY);
5678 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX);
5679 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5680 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5681 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5682 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5684 * Cache devices can always be removed.
5686 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5687 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5688 spa_load_l2cache(spa);
5689 spa->spa_l2cache.sav_sync = B_TRUE;
5690 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE_AUX);
5691 } else if (vd != NULL && vd->vdev_islog) {
5693 ASSERT(vd == vd->vdev_top);
5698 * Stop allocating from this vdev.
5700 metaslab_group_passivate(mg);
5703 * Wait for the youngest allocations and frees to sync,
5704 * and then wait for the deferral of those frees to finish.
5706 spa_vdev_config_exit(spa, NULL,
5707 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5710 * Attempt to evacuate the vdev.
5712 error = spa_vdev_remove_evacuate(spa, vd);
5714 txg = spa_vdev_config_enter(spa);
5717 * If we couldn't evacuate the vdev, unwind.
5720 metaslab_group_activate(mg);
5721 return (spa_vdev_exit(spa, NULL, txg, error));
5725 * Clean up the vdev namespace.
5727 spa_vdev_remove_from_namespace(spa, vd);
5729 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE_DEV);
5730 } else if (vd != NULL) {
5732 * Normal vdevs cannot be removed (yet).
5734 error = SET_ERROR(ENOTSUP);
5737 * There is no vdev of any kind with the specified guid.
5739 error = SET_ERROR(ENOENT);
5743 error = spa_vdev_exit(spa, NULL, txg, error);
5749 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5750 * currently spared, so we can detach it.
5753 spa_vdev_resilver_done_hunt(vdev_t *vd)
5755 vdev_t *newvd, *oldvd;
5757 for (int c = 0; c < vd->vdev_children; c++) {
5758 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5764 * Check for a completed replacement. We always consider the first
5765 * vdev in the list to be the oldest vdev, and the last one to be
5766 * the newest (see spa_vdev_attach() for how that works). In
5767 * the case where the newest vdev is faulted, we will not automatically
5768 * remove it after a resilver completes. This is OK as it will require
5769 * user intervention to determine which disk the admin wishes to keep.
5771 if (vd->vdev_ops == &vdev_replacing_ops) {
5772 ASSERT(vd->vdev_children > 1);
5774 newvd = vd->vdev_child[vd->vdev_children - 1];
5775 oldvd = vd->vdev_child[0];
5777 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5778 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5779 !vdev_dtl_required(oldvd))
5784 * Check for a completed resilver with the 'unspare' flag set.
5786 if (vd->vdev_ops == &vdev_spare_ops) {
5787 vdev_t *first = vd->vdev_child[0];
5788 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5790 if (last->vdev_unspare) {
5793 } else if (first->vdev_unspare) {
5800 if (oldvd != NULL &&
5801 vdev_dtl_empty(newvd, DTL_MISSING) &&
5802 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5803 !vdev_dtl_required(oldvd))
5807 * If there are more than two spares attached to a disk,
5808 * and those spares are not required, then we want to
5809 * attempt to free them up now so that they can be used
5810 * by other pools. Once we're back down to a single
5811 * disk+spare, we stop removing them.
5813 if (vd->vdev_children > 2) {
5814 newvd = vd->vdev_child[1];
5816 if (newvd->vdev_isspare && last->vdev_isspare &&
5817 vdev_dtl_empty(last, DTL_MISSING) &&
5818 vdev_dtl_empty(last, DTL_OUTAGE) &&
5819 !vdev_dtl_required(newvd))
5828 spa_vdev_resilver_done(spa_t *spa)
5830 vdev_t *vd, *pvd, *ppvd;
5831 uint64_t guid, sguid, pguid, ppguid;
5833 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5835 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5836 pvd = vd->vdev_parent;
5837 ppvd = pvd->vdev_parent;
5838 guid = vd->vdev_guid;
5839 pguid = pvd->vdev_guid;
5840 ppguid = ppvd->vdev_guid;
5843 * If we have just finished replacing a hot spared device, then
5844 * we need to detach the parent's first child (the original hot
5847 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5848 ppvd->vdev_children == 2) {
5849 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5850 sguid = ppvd->vdev_child[1]->vdev_guid;
5852 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5854 spa_config_exit(spa, SCL_ALL, FTAG);
5855 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5857 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5859 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5862 spa_config_exit(spa, SCL_ALL, FTAG);
5866 * Update the stored path or FRU for this vdev.
5869 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5873 boolean_t sync = B_FALSE;
5875 ASSERT(spa_writeable(spa));
5877 spa_vdev_state_enter(spa, SCL_ALL);
5879 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5880 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5882 if (!vd->vdev_ops->vdev_op_leaf)
5883 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5886 if (strcmp(value, vd->vdev_path) != 0) {
5887 spa_strfree(vd->vdev_path);
5888 vd->vdev_path = spa_strdup(value);
5892 if (vd->vdev_fru == NULL) {
5893 vd->vdev_fru = spa_strdup(value);
5895 } else if (strcmp(value, vd->vdev_fru) != 0) {
5896 spa_strfree(vd->vdev_fru);
5897 vd->vdev_fru = spa_strdup(value);
5902 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5906 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5908 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5912 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5914 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5918 * ==========================================================================
5920 * ==========================================================================
5924 spa_scan_stop(spa_t *spa)
5926 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5927 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5928 return (SET_ERROR(EBUSY));
5929 return (dsl_scan_cancel(spa->spa_dsl_pool));
5933 spa_scan(spa_t *spa, pool_scan_func_t func)
5935 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5937 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5938 return (SET_ERROR(ENOTSUP));
5941 * If a resilver was requested, but there is no DTL on a
5942 * writeable leaf device, we have nothing to do.
5944 if (func == POOL_SCAN_RESILVER &&
5945 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5946 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5950 return (dsl_scan(spa->spa_dsl_pool, func));
5954 * ==========================================================================
5955 * SPA async task processing
5956 * ==========================================================================
5960 spa_async_remove(spa_t *spa, vdev_t *vd)
5962 if (vd->vdev_remove_wanted) {
5963 vd->vdev_remove_wanted = B_FALSE;
5964 vd->vdev_delayed_close = B_FALSE;
5965 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5968 * We want to clear the stats, but we don't want to do a full
5969 * vdev_clear() as that will cause us to throw away
5970 * degraded/faulted state as well as attempt to reopen the
5971 * device, all of which is a waste.
5973 vd->vdev_stat.vs_read_errors = 0;
5974 vd->vdev_stat.vs_write_errors = 0;
5975 vd->vdev_stat.vs_checksum_errors = 0;
5977 vdev_state_dirty(vd->vdev_top);
5978 /* Tell userspace that the vdev is gone. */
5979 zfs_post_remove(spa, vd);
5982 for (int c = 0; c < vd->vdev_children; c++)
5983 spa_async_remove(spa, vd->vdev_child[c]);
5987 spa_async_probe(spa_t *spa, vdev_t *vd)
5989 if (vd->vdev_probe_wanted) {
5990 vd->vdev_probe_wanted = B_FALSE;
5991 vdev_reopen(vd); /* vdev_open() does the actual probe */
5994 for (int c = 0; c < vd->vdev_children; c++)
5995 spa_async_probe(spa, vd->vdev_child[c]);
5999 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
6005 if (!spa->spa_autoexpand)
6008 for (int c = 0; c < vd->vdev_children; c++) {
6009 vdev_t *cvd = vd->vdev_child[c];
6010 spa_async_autoexpand(spa, cvd);
6013 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
6016 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
6017 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
6019 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6020 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
6022 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
6023 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
6026 kmem_free(physpath, MAXPATHLEN);
6030 spa_async_thread(void *arg)
6035 ASSERT(spa->spa_sync_on);
6037 mutex_enter(&spa->spa_async_lock);
6038 tasks = spa->spa_async_tasks;
6039 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
6040 mutex_exit(&spa->spa_async_lock);
6043 * See if the config needs to be updated.
6045 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
6046 uint64_t old_space, new_space;
6048 mutex_enter(&spa_namespace_lock);
6049 old_space = metaslab_class_get_space(spa_normal_class(spa));
6050 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
6051 new_space = metaslab_class_get_space(spa_normal_class(spa));
6052 mutex_exit(&spa_namespace_lock);
6055 * If the pool grew as a result of the config update,
6056 * then log an internal history event.
6058 if (new_space != old_space) {
6059 spa_history_log_internal(spa, "vdev online", NULL,
6060 "pool '%s' size: %llu(+%llu)",
6061 spa_name(spa), new_space, new_space - old_space);
6065 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
6066 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6067 spa_async_autoexpand(spa, spa->spa_root_vdev);
6068 spa_config_exit(spa, SCL_CONFIG, FTAG);
6072 * See if any devices need to be probed.
6074 if (tasks & SPA_ASYNC_PROBE) {
6075 spa_vdev_state_enter(spa, SCL_NONE);
6076 spa_async_probe(spa, spa->spa_root_vdev);
6077 (void) spa_vdev_state_exit(spa, NULL, 0);
6081 * If any devices are done replacing, detach them.
6083 if (tasks & SPA_ASYNC_RESILVER_DONE)
6084 spa_vdev_resilver_done(spa);
6087 * Kick off a resilver.
6089 if (tasks & SPA_ASYNC_RESILVER)
6090 dsl_resilver_restart(spa->spa_dsl_pool, 0);
6093 * Let the world know that we're done.
6095 mutex_enter(&spa->spa_async_lock);
6096 spa->spa_async_thread = NULL;
6097 cv_broadcast(&spa->spa_async_cv);
6098 mutex_exit(&spa->spa_async_lock);
6103 spa_async_thread_vd(void *arg)
6108 ASSERT(spa->spa_sync_on);
6110 mutex_enter(&spa->spa_async_lock);
6111 tasks = spa->spa_async_tasks;
6113 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6114 mutex_exit(&spa->spa_async_lock);
6117 * See if any devices need to be marked REMOVED.
6119 if (tasks & SPA_ASYNC_REMOVE) {
6120 spa_vdev_state_enter(spa, SCL_NONE);
6121 spa_async_remove(spa, spa->spa_root_vdev);
6122 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6123 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6124 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6125 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6126 (void) spa_vdev_state_exit(spa, NULL, 0);
6130 * Let the world know that we're done.
6132 mutex_enter(&spa->spa_async_lock);
6133 tasks = spa->spa_async_tasks;
6134 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6136 spa->spa_async_thread_vd = NULL;
6137 cv_broadcast(&spa->spa_async_cv);
6138 mutex_exit(&spa->spa_async_lock);
6143 spa_async_suspend(spa_t *spa)
6145 mutex_enter(&spa->spa_async_lock);
6146 spa->spa_async_suspended++;
6147 while (spa->spa_async_thread != NULL &&
6148 spa->spa_async_thread_vd != NULL)
6149 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6150 mutex_exit(&spa->spa_async_lock);
6154 spa_async_resume(spa_t *spa)
6156 mutex_enter(&spa->spa_async_lock);
6157 ASSERT(spa->spa_async_suspended != 0);
6158 spa->spa_async_suspended--;
6159 mutex_exit(&spa->spa_async_lock);
6163 spa_async_tasks_pending(spa_t *spa)
6165 uint_t non_config_tasks;
6167 boolean_t config_task_suspended;
6169 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6171 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6172 if (spa->spa_ccw_fail_time == 0) {
6173 config_task_suspended = B_FALSE;
6175 config_task_suspended =
6176 (gethrtime() - spa->spa_ccw_fail_time) <
6177 (zfs_ccw_retry_interval * NANOSEC);
6180 return (non_config_tasks || (config_task && !config_task_suspended));
6184 spa_async_dispatch(spa_t *spa)
6186 mutex_enter(&spa->spa_async_lock);
6187 if (spa_async_tasks_pending(spa) &&
6188 !spa->spa_async_suspended &&
6189 spa->spa_async_thread == NULL &&
6191 spa->spa_async_thread = thread_create(NULL, 0,
6192 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6193 mutex_exit(&spa->spa_async_lock);
6197 spa_async_dispatch_vd(spa_t *spa)
6199 mutex_enter(&spa->spa_async_lock);
6200 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6201 !spa->spa_async_suspended &&
6202 spa->spa_async_thread_vd == NULL &&
6204 spa->spa_async_thread_vd = thread_create(NULL, 0,
6205 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6206 mutex_exit(&spa->spa_async_lock);
6210 spa_async_request(spa_t *spa, int task)
6212 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6213 mutex_enter(&spa->spa_async_lock);
6214 spa->spa_async_tasks |= task;
6215 mutex_exit(&spa->spa_async_lock);
6216 spa_async_dispatch_vd(spa);
6220 * ==========================================================================
6221 * SPA syncing routines
6222 * ==========================================================================
6226 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6229 bpobj_enqueue(bpo, bp, tx);
6234 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6238 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6239 BP_GET_PSIZE(bp), zio->io_flags));
6244 * Note: this simple function is not inlined to make it easier to dtrace the
6245 * amount of time spent syncing frees.
6248 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6250 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6251 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6252 VERIFY(zio_wait(zio) == 0);
6256 * Note: this simple function is not inlined to make it easier to dtrace the
6257 * amount of time spent syncing deferred frees.
6260 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6262 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6263 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6264 spa_free_sync_cb, zio, tx), ==, 0);
6265 VERIFY0(zio_wait(zio));
6270 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6272 char *packed = NULL;
6277 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6280 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6281 * information. This avoids the dmu_buf_will_dirty() path and
6282 * saves us a pre-read to get data we don't actually care about.
6284 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6285 packed = kmem_alloc(bufsize, KM_SLEEP);
6287 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6289 bzero(packed + nvsize, bufsize - nvsize);
6291 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6293 kmem_free(packed, bufsize);
6295 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6296 dmu_buf_will_dirty(db, tx);
6297 *(uint64_t *)db->db_data = nvsize;
6298 dmu_buf_rele(db, FTAG);
6302 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6303 const char *config, const char *entry)
6313 * Update the MOS nvlist describing the list of available devices.
6314 * spa_validate_aux() will have already made sure this nvlist is
6315 * valid and the vdevs are labeled appropriately.
6317 if (sav->sav_object == 0) {
6318 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6319 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6320 sizeof (uint64_t), tx);
6321 VERIFY(zap_update(spa->spa_meta_objset,
6322 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6323 &sav->sav_object, tx) == 0);
6326 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6327 if (sav->sav_count == 0) {
6328 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6330 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6331 for (i = 0; i < sav->sav_count; i++)
6332 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6333 B_FALSE, VDEV_CONFIG_L2CACHE);
6334 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6335 sav->sav_count) == 0);
6336 for (i = 0; i < sav->sav_count; i++)
6337 nvlist_free(list[i]);
6338 kmem_free(list, sav->sav_count * sizeof (void *));
6341 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6342 nvlist_free(nvroot);
6344 sav->sav_sync = B_FALSE;
6348 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6352 if (list_is_empty(&spa->spa_config_dirty_list))
6355 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6357 config = spa_config_generate(spa, spa->spa_root_vdev,
6358 dmu_tx_get_txg(tx), B_FALSE);
6361 * If we're upgrading the spa version then make sure that
6362 * the config object gets updated with the correct version.
6364 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6365 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6366 spa->spa_uberblock.ub_version);
6368 spa_config_exit(spa, SCL_STATE, FTAG);
6370 nvlist_free(spa->spa_config_syncing);
6371 spa->spa_config_syncing = config;
6373 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6377 spa_sync_version(void *arg, dmu_tx_t *tx)
6379 uint64_t *versionp = arg;
6380 uint64_t version = *versionp;
6381 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6384 * Setting the version is special cased when first creating the pool.
6386 ASSERT(tx->tx_txg != TXG_INITIAL);
6388 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6389 ASSERT(version >= spa_version(spa));
6391 spa->spa_uberblock.ub_version = version;
6392 vdev_config_dirty(spa->spa_root_vdev);
6393 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6397 * Set zpool properties.
6400 spa_sync_props(void *arg, dmu_tx_t *tx)
6402 nvlist_t *nvp = arg;
6403 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6404 objset_t *mos = spa->spa_meta_objset;
6405 nvpair_t *elem = NULL;
6407 mutex_enter(&spa->spa_props_lock);
6409 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6411 char *strval, *fname;
6413 const char *propname;
6414 zprop_type_t proptype;
6417 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6420 * We checked this earlier in spa_prop_validate().
6422 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6424 fname = strchr(nvpair_name(elem), '@') + 1;
6425 VERIFY0(zfeature_lookup_name(fname, &fid));
6427 spa_feature_enable(spa, fid, tx);
6428 spa_history_log_internal(spa, "set", tx,
6429 "%s=enabled", nvpair_name(elem));
6432 case ZPOOL_PROP_VERSION:
6433 intval = fnvpair_value_uint64(elem);
6435 * The version is synced seperatly before other
6436 * properties and should be correct by now.
6438 ASSERT3U(spa_version(spa), >=, intval);
6441 case ZPOOL_PROP_ALTROOT:
6443 * 'altroot' is a non-persistent property. It should
6444 * have been set temporarily at creation or import time.
6446 ASSERT(spa->spa_root != NULL);
6449 case ZPOOL_PROP_READONLY:
6450 case ZPOOL_PROP_CACHEFILE:
6452 * 'readonly' and 'cachefile' are also non-persisitent
6456 case ZPOOL_PROP_COMMENT:
6457 strval = fnvpair_value_string(elem);
6458 if (spa->spa_comment != NULL)
6459 spa_strfree(spa->spa_comment);
6460 spa->spa_comment = spa_strdup(strval);
6462 * We need to dirty the configuration on all the vdevs
6463 * so that their labels get updated. It's unnecessary
6464 * to do this for pool creation since the vdev's
6465 * configuratoin has already been dirtied.
6467 if (tx->tx_txg != TXG_INITIAL)
6468 vdev_config_dirty(spa->spa_root_vdev);
6469 spa_history_log_internal(spa, "set", tx,
6470 "%s=%s", nvpair_name(elem), strval);
6474 * Set pool property values in the poolprops mos object.
6476 if (spa->spa_pool_props_object == 0) {
6477 spa->spa_pool_props_object =
6478 zap_create_link(mos, DMU_OT_POOL_PROPS,
6479 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6483 /* normalize the property name */
6484 propname = zpool_prop_to_name(prop);
6485 proptype = zpool_prop_get_type(prop);
6487 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6488 ASSERT(proptype == PROP_TYPE_STRING);
6489 strval = fnvpair_value_string(elem);
6490 VERIFY0(zap_update(mos,
6491 spa->spa_pool_props_object, propname,
6492 1, strlen(strval) + 1, strval, tx));
6493 spa_history_log_internal(spa, "set", tx,
6494 "%s=%s", nvpair_name(elem), strval);
6495 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6496 intval = fnvpair_value_uint64(elem);
6498 if (proptype == PROP_TYPE_INDEX) {
6500 VERIFY0(zpool_prop_index_to_string(
6501 prop, intval, &unused));
6503 VERIFY0(zap_update(mos,
6504 spa->spa_pool_props_object, propname,
6505 8, 1, &intval, tx));
6506 spa_history_log_internal(spa, "set", tx,
6507 "%s=%lld", nvpair_name(elem), intval);
6509 ASSERT(0); /* not allowed */
6513 case ZPOOL_PROP_DELEGATION:
6514 spa->spa_delegation = intval;
6516 case ZPOOL_PROP_BOOTFS:
6517 spa->spa_bootfs = intval;
6519 case ZPOOL_PROP_FAILUREMODE:
6520 spa->spa_failmode = intval;
6522 case ZPOOL_PROP_AUTOEXPAND:
6523 spa->spa_autoexpand = intval;
6524 if (tx->tx_txg != TXG_INITIAL)
6525 spa_async_request(spa,
6526 SPA_ASYNC_AUTOEXPAND);
6528 case ZPOOL_PROP_DEDUPDITTO:
6529 spa->spa_dedup_ditto = intval;
6538 mutex_exit(&spa->spa_props_lock);
6542 * Perform one-time upgrade on-disk changes. spa_version() does not
6543 * reflect the new version this txg, so there must be no changes this
6544 * txg to anything that the upgrade code depends on after it executes.
6545 * Therefore this must be called after dsl_pool_sync() does the sync
6549 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6551 dsl_pool_t *dp = spa->spa_dsl_pool;
6553 ASSERT(spa->spa_sync_pass == 1);
6555 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6557 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6558 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6559 dsl_pool_create_origin(dp, tx);
6561 /* Keeping the origin open increases spa_minref */
6562 spa->spa_minref += 3;
6565 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6566 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6567 dsl_pool_upgrade_clones(dp, tx);
6570 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6571 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6572 dsl_pool_upgrade_dir_clones(dp, tx);
6574 /* Keeping the freedir open increases spa_minref */
6575 spa->spa_minref += 3;
6578 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6579 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6580 spa_feature_create_zap_objects(spa, tx);
6584 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6585 * when possibility to use lz4 compression for metadata was added
6586 * Old pools that have this feature enabled must be upgraded to have
6587 * this feature active
6589 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6590 boolean_t lz4_en = spa_feature_is_enabled(spa,
6591 SPA_FEATURE_LZ4_COMPRESS);
6592 boolean_t lz4_ac = spa_feature_is_active(spa,
6593 SPA_FEATURE_LZ4_COMPRESS);
6595 if (lz4_en && !lz4_ac)
6596 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6600 * If we haven't written the salt, do so now. Note that the
6601 * feature may not be activated yet, but that's fine since
6602 * the presence of this ZAP entry is backwards compatible.
6604 if (zap_contains(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
6605 DMU_POOL_CHECKSUM_SALT) == ENOENT) {
6606 VERIFY0(zap_add(spa->spa_meta_objset,
6607 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CHECKSUM_SALT, 1,
6608 sizeof (spa->spa_cksum_salt.zcs_bytes),
6609 spa->spa_cksum_salt.zcs_bytes, tx));
6612 rrw_exit(&dp->dp_config_rwlock, FTAG);
6616 * Sync the specified transaction group. New blocks may be dirtied as
6617 * part of the process, so we iterate until it converges.
6620 spa_sync(spa_t *spa, uint64_t txg)
6622 dsl_pool_t *dp = spa->spa_dsl_pool;
6623 objset_t *mos = spa->spa_meta_objset;
6624 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6625 vdev_t *rvd = spa->spa_root_vdev;
6630 VERIFY(spa_writeable(spa));
6633 * Lock out configuration changes.
6635 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6637 spa->spa_syncing_txg = txg;
6638 spa->spa_sync_pass = 0;
6641 * If there are any pending vdev state changes, convert them
6642 * into config changes that go out with this transaction group.
6644 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6645 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6647 * We need the write lock here because, for aux vdevs,
6648 * calling vdev_config_dirty() modifies sav_config.
6649 * This is ugly and will become unnecessary when we
6650 * eliminate the aux vdev wart by integrating all vdevs
6651 * into the root vdev tree.
6653 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6654 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6655 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6656 vdev_state_clean(vd);
6657 vdev_config_dirty(vd);
6659 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6660 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6662 spa_config_exit(spa, SCL_STATE, FTAG);
6664 tx = dmu_tx_create_assigned(dp, txg);
6666 spa->spa_sync_starttime = gethrtime();
6668 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6669 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6670 #else /* !illumos */
6672 callout_reset(&spa->spa_deadman_cycid,
6673 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6675 #endif /* illumos */
6678 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6679 * set spa_deflate if we have no raid-z vdevs.
6681 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6682 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6685 for (i = 0; i < rvd->vdev_children; i++) {
6686 vd = rvd->vdev_child[i];
6687 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6690 if (i == rvd->vdev_children) {
6691 spa->spa_deflate = TRUE;
6692 VERIFY(0 == zap_add(spa->spa_meta_objset,
6693 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6694 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6699 * Iterate to convergence.
6702 int pass = ++spa->spa_sync_pass;
6704 spa_sync_config_object(spa, tx);
6705 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6706 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6707 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6708 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6709 spa_errlog_sync(spa, txg);
6710 dsl_pool_sync(dp, txg);
6712 if (pass < zfs_sync_pass_deferred_free) {
6713 spa_sync_frees(spa, free_bpl, tx);
6716 * We can not defer frees in pass 1, because
6717 * we sync the deferred frees later in pass 1.
6719 ASSERT3U(pass, >, 1);
6720 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6721 &spa->spa_deferred_bpobj, tx);
6725 dsl_scan_sync(dp, tx);
6727 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6731 spa_sync_upgrades(spa, tx);
6733 spa->spa_uberblock.ub_rootbp.blk_birth);
6735 * Note: We need to check if the MOS is dirty
6736 * because we could have marked the MOS dirty
6737 * without updating the uberblock (e.g. if we
6738 * have sync tasks but no dirty user data). We
6739 * need to check the uberblock's rootbp because
6740 * it is updated if we have synced out dirty
6741 * data (though in this case the MOS will most
6742 * likely also be dirty due to second order
6743 * effects, we don't want to rely on that here).
6745 if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6746 !dmu_objset_is_dirty(mos, txg)) {
6748 * Nothing changed on the first pass,
6749 * therefore this TXG is a no-op. Avoid
6750 * syncing deferred frees, so that we
6751 * can keep this TXG as a no-op.
6753 ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6755 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6756 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
6759 spa_sync_deferred_frees(spa, tx);
6762 } while (dmu_objset_is_dirty(mos, txg));
6765 * Rewrite the vdev configuration (which includes the uberblock)
6766 * to commit the transaction group.
6768 * If there are no dirty vdevs, we sync the uberblock to a few
6769 * random top-level vdevs that are known to be visible in the
6770 * config cache (see spa_vdev_add() for a complete description).
6771 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6775 * We hold SCL_STATE to prevent vdev open/close/etc.
6776 * while we're attempting to write the vdev labels.
6778 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6780 if (list_is_empty(&spa->spa_config_dirty_list)) {
6781 vdev_t *svd[SPA_DVAS_PER_BP];
6783 int children = rvd->vdev_children;
6784 int c0 = spa_get_random(children);
6786 for (int c = 0; c < children; c++) {
6787 vd = rvd->vdev_child[(c0 + c) % children];
6788 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6790 svd[svdcount++] = vd;
6791 if (svdcount == SPA_DVAS_PER_BP)
6794 error = vdev_config_sync(svd, svdcount, txg);
6796 error = vdev_config_sync(rvd->vdev_child,
6797 rvd->vdev_children, txg);
6801 spa->spa_last_synced_guid = rvd->vdev_guid;
6803 spa_config_exit(spa, SCL_STATE, FTAG);
6807 zio_suspend(spa, NULL);
6808 zio_resume_wait(spa);
6813 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6814 #else /* !illumos */
6816 callout_drain(&spa->spa_deadman_cycid);
6818 #endif /* illumos */
6821 * Clear the dirty config list.
6823 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6824 vdev_config_clean(vd);
6827 * Now that the new config has synced transactionally,
6828 * let it become visible to the config cache.
6830 if (spa->spa_config_syncing != NULL) {
6831 spa_config_set(spa, spa->spa_config_syncing);
6832 spa->spa_config_txg = txg;
6833 spa->spa_config_syncing = NULL;
6836 spa->spa_ubsync = spa->spa_uberblock;
6838 dsl_pool_sync_done(dp, txg);
6841 * Update usable space statistics.
6843 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6844 vdev_sync_done(vd, txg);
6846 spa_update_dspace(spa);
6849 * It had better be the case that we didn't dirty anything
6850 * since vdev_config_sync().
6852 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6853 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6854 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6856 spa->spa_sync_pass = 0;
6858 spa_config_exit(spa, SCL_CONFIG, FTAG);
6860 spa_handle_ignored_writes(spa);
6863 * If any async tasks have been requested, kick them off.
6865 spa_async_dispatch(spa);
6866 spa_async_dispatch_vd(spa);
6870 * Sync all pools. We don't want to hold the namespace lock across these
6871 * operations, so we take a reference on the spa_t and drop the lock during the
6875 spa_sync_allpools(void)
6878 mutex_enter(&spa_namespace_lock);
6879 while ((spa = spa_next(spa)) != NULL) {
6880 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6881 !spa_writeable(spa) || spa_suspended(spa))
6883 spa_open_ref(spa, FTAG);
6884 mutex_exit(&spa_namespace_lock);
6885 txg_wait_synced(spa_get_dsl(spa), 0);
6886 mutex_enter(&spa_namespace_lock);
6887 spa_close(spa, FTAG);
6889 mutex_exit(&spa_namespace_lock);
6893 * ==========================================================================
6894 * Miscellaneous routines
6895 * ==========================================================================
6899 * Remove all pools in the system.
6907 * Remove all cached state. All pools should be closed now,
6908 * so every spa in the AVL tree should be unreferenced.
6910 mutex_enter(&spa_namespace_lock);
6911 while ((spa = spa_next(NULL)) != NULL) {
6913 * Stop async tasks. The async thread may need to detach
6914 * a device that's been replaced, which requires grabbing
6915 * spa_namespace_lock, so we must drop it here.
6917 spa_open_ref(spa, FTAG);
6918 mutex_exit(&spa_namespace_lock);
6919 spa_async_suspend(spa);
6920 mutex_enter(&spa_namespace_lock);
6921 spa_close(spa, FTAG);
6923 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6925 spa_deactivate(spa);
6929 mutex_exit(&spa_namespace_lock);
6933 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6938 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6942 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6943 vd = spa->spa_l2cache.sav_vdevs[i];
6944 if (vd->vdev_guid == guid)
6948 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6949 vd = spa->spa_spares.sav_vdevs[i];
6950 if (vd->vdev_guid == guid)
6959 spa_upgrade(spa_t *spa, uint64_t version)
6961 ASSERT(spa_writeable(spa));
6963 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6966 * This should only be called for a non-faulted pool, and since a
6967 * future version would result in an unopenable pool, this shouldn't be
6970 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6971 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6973 spa->spa_uberblock.ub_version = version;
6974 vdev_config_dirty(spa->spa_root_vdev);
6976 spa_config_exit(spa, SCL_ALL, FTAG);
6978 txg_wait_synced(spa_get_dsl(spa), 0);
6982 spa_has_spare(spa_t *spa, uint64_t guid)
6986 spa_aux_vdev_t *sav = &spa->spa_spares;
6988 for (i = 0; i < sav->sav_count; i++)
6989 if (sav->sav_vdevs[i]->vdev_guid == guid)
6992 for (i = 0; i < sav->sav_npending; i++) {
6993 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6994 &spareguid) == 0 && spareguid == guid)
7002 * Check if a pool has an active shared spare device.
7003 * Note: reference count of an active spare is 2, as a spare and as a replace
7006 spa_has_active_shared_spare(spa_t *spa)
7010 spa_aux_vdev_t *sav = &spa->spa_spares;
7012 for (i = 0; i < sav->sav_count; i++) {
7013 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
7014 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
7023 * Post a sysevent corresponding to the given event. The 'name' must be one of
7024 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
7025 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7026 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7027 * or zdb as real changes.
7030 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
7034 sysevent_attr_list_t *attr = NULL;
7035 sysevent_value_t value;
7038 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
7041 value.value_type = SE_DATA_TYPE_STRING;
7042 value.value.sv_string = spa_name(spa);
7043 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
7046 value.value_type = SE_DATA_TYPE_UINT64;
7047 value.value.sv_uint64 = spa_guid(spa);
7048 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
7052 value.value_type = SE_DATA_TYPE_UINT64;
7053 value.value.sv_uint64 = vd->vdev_guid;
7054 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
7058 if (vd->vdev_path) {
7059 value.value_type = SE_DATA_TYPE_STRING;
7060 value.value.sv_string = vd->vdev_path;
7061 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
7062 &value, SE_SLEEP) != 0)
7067 if (sysevent_attach_attributes(ev, attr) != 0)
7071 (void) log_sysevent(ev, SE_SLEEP, &eid);
7075 sysevent_free_attr(attr);