4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * SPA: Storage Pool Allocator
32 * This file contains all the routines used when modifying on-disk SPA state.
33 * This includes opening, importing, destroying, exporting a pool, and syncing a
37 #include <sys/zfs_context.h>
38 #include <sys/fm/fs/zfs.h>
39 #include <sys/spa_impl.h>
41 #include <sys/zio_checksum.h>
43 #include <sys/dmu_tx.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/metaslab.h>
49 #include <sys/metaslab_impl.h>
50 #include <sys/uberblock_impl.h>
53 #include <sys/dmu_traverse.h>
54 #include <sys/dmu_objset.h>
55 #include <sys/unique.h>
56 #include <sys/dsl_pool.h>
57 #include <sys/dsl_dataset.h>
58 #include <sys/dsl_dir.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/dsl_synctask.h>
61 #include <sys/fs/zfs.h>
63 #include <sys/callb.h>
64 #include <sys/spa_boot.h>
65 #include <sys/zfs_ioctl.h>
66 #include <sys/dsl_scan.h>
67 #include <sys/dmu_send.h>
68 #include <sys/dsl_destroy.h>
69 #include <sys/dsl_userhold.h>
70 #include <sys/zfeature.h>
72 #include <sys/trim_map.h>
75 #include <sys/callb.h>
76 #include <sys/cpupart.h>
81 #include "zfs_comutil.h"
83 /* Check hostid on import? */
84 static int check_hostid = 1;
87 * The interval, in seconds, at which failed configuration cache file writes
90 static int zfs_ccw_retry_interval = 300;
92 SYSCTL_DECL(_vfs_zfs);
93 SYSCTL_INT(_vfs_zfs, OID_AUTO, check_hostid, CTLFLAG_RWTUN, &check_hostid, 0,
94 "Check hostid on import?");
95 TUNABLE_INT("vfs.zfs.ccw_retry_interval", &zfs_ccw_retry_interval);
96 SYSCTL_INT(_vfs_zfs, OID_AUTO, ccw_retry_interval, CTLFLAG_RW,
97 &zfs_ccw_retry_interval, 0,
98 "Configuration cache file write, retry after failure, interval (seconds)");
100 typedef enum zti_modes {
101 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
102 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
103 ZTI_MODE_NULL, /* don't create a taskq */
107 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
108 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
109 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
111 #define ZTI_N(n) ZTI_P(n, 1)
112 #define ZTI_ONE ZTI_N(1)
114 typedef struct zio_taskq_info {
115 zti_modes_t zti_mode;
120 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
121 "issue", "issue_high", "intr", "intr_high"
125 * This table defines the taskq settings for each ZFS I/O type. When
126 * initializing a pool, we use this table to create an appropriately sized
127 * taskq. Some operations are low volume and therefore have a small, static
128 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
129 * macros. Other operations process a large amount of data; the ZTI_BATCH
130 * macro causes us to create a taskq oriented for throughput. Some operations
131 * are so high frequency and short-lived that the taskq itself can become a a
132 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
133 * additional degree of parallelism specified by the number of threads per-
134 * taskq and the number of taskqs; when dispatching an event in this case, the
135 * particular taskq is chosen at random.
137 * The different taskq priorities are to handle the different contexts (issue
138 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
139 * need to be handled with minimum delay.
141 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
142 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
143 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
144 { ZTI_N(8), ZTI_NULL, ZTI_P(12, 8), ZTI_NULL }, /* READ */
145 { ZTI_BATCH, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
146 { ZTI_P(12, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
147 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
148 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
151 static void spa_sync_version(void *arg, dmu_tx_t *tx);
152 static void spa_sync_props(void *arg, dmu_tx_t *tx);
153 static boolean_t spa_has_active_shared_spare(spa_t *spa);
154 static int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
155 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
157 static void spa_vdev_resilver_done(spa_t *spa);
159 uint_t zio_taskq_batch_pct = 75; /* 1 thread per cpu in pset */
161 id_t zio_taskq_psrset_bind = PS_NONE;
164 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
166 uint_t zio_taskq_basedc = 80; /* base duty cycle */
168 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
169 extern int zfs_sync_pass_deferred_free;
172 extern void spa_deadman(void *arg);
176 * This (illegal) pool name is used when temporarily importing a spa_t in order
177 * to get the vdev stats associated with the imported devices.
179 #define TRYIMPORT_NAME "$import"
182 * ==========================================================================
183 * SPA properties routines
184 * ==========================================================================
188 * Add a (source=src, propname=propval) list to an nvlist.
191 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
192 uint64_t intval, zprop_source_t src)
194 const char *propname = zpool_prop_to_name(prop);
197 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
198 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
201 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
203 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
205 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
206 nvlist_free(propval);
210 * Get property values from the spa configuration.
213 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
215 vdev_t *rvd = spa->spa_root_vdev;
216 dsl_pool_t *pool = spa->spa_dsl_pool;
217 uint64_t size, alloc, cap, version;
218 zprop_source_t src = ZPROP_SRC_NONE;
219 spa_config_dirent_t *dp;
220 metaslab_class_t *mc = spa_normal_class(spa);
222 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
225 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
226 size = metaslab_class_get_space(spa_normal_class(spa));
227 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
228 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
229 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
230 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
233 spa_prop_add_list(*nvp, ZPOOL_PROP_FRAGMENTATION, NULL,
234 metaslab_class_fragmentation(mc), src);
235 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL,
236 metaslab_class_expandable_space(mc), src);
237 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
238 (spa_mode(spa) == FREAD), src);
240 cap = (size == 0) ? 0 : (alloc * 100 / size);
241 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
243 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
244 ddt_get_pool_dedup_ratio(spa), src);
246 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
247 rvd->vdev_state, src);
249 version = spa_version(spa);
250 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
251 src = ZPROP_SRC_DEFAULT;
253 src = ZPROP_SRC_LOCAL;
254 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
259 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
260 * when opening pools before this version freedir will be NULL.
262 if (pool->dp_free_dir != NULL) {
263 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
264 dsl_dir_phys(pool->dp_free_dir)->dd_used_bytes,
267 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
271 if (pool->dp_leak_dir != NULL) {
272 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED, NULL,
273 dsl_dir_phys(pool->dp_leak_dir)->dd_used_bytes,
276 spa_prop_add_list(*nvp, ZPOOL_PROP_LEAKED,
281 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
283 if (spa->spa_comment != NULL) {
284 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
288 if (spa->spa_root != NULL)
289 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
292 if (spa_feature_is_enabled(spa, SPA_FEATURE_LARGE_BLOCKS)) {
293 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
294 MIN(zfs_max_recordsize, SPA_MAXBLOCKSIZE), ZPROP_SRC_NONE);
296 spa_prop_add_list(*nvp, ZPOOL_PROP_MAXBLOCKSIZE, NULL,
297 SPA_OLD_MAXBLOCKSIZE, ZPROP_SRC_NONE);
300 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
301 if (dp->scd_path == NULL) {
302 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
303 "none", 0, ZPROP_SRC_LOCAL);
304 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
305 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
306 dp->scd_path, 0, ZPROP_SRC_LOCAL);
312 * Get zpool property values.
315 spa_prop_get(spa_t *spa, nvlist_t **nvp)
317 objset_t *mos = spa->spa_meta_objset;
322 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
324 mutex_enter(&spa->spa_props_lock);
327 * Get properties from the spa config.
329 spa_prop_get_config(spa, nvp);
331 /* If no pool property object, no more prop to get. */
332 if (mos == NULL || spa->spa_pool_props_object == 0) {
333 mutex_exit(&spa->spa_props_lock);
338 * Get properties from the MOS pool property object.
340 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
341 (err = zap_cursor_retrieve(&zc, &za)) == 0;
342 zap_cursor_advance(&zc)) {
345 zprop_source_t src = ZPROP_SRC_DEFAULT;
348 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
351 switch (za.za_integer_length) {
353 /* integer property */
354 if (za.za_first_integer !=
355 zpool_prop_default_numeric(prop))
356 src = ZPROP_SRC_LOCAL;
358 if (prop == ZPOOL_PROP_BOOTFS) {
360 dsl_dataset_t *ds = NULL;
362 dp = spa_get_dsl(spa);
363 dsl_pool_config_enter(dp, FTAG);
364 if (err = dsl_dataset_hold_obj(dp,
365 za.za_first_integer, FTAG, &ds)) {
366 dsl_pool_config_exit(dp, FTAG);
371 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
373 dsl_dataset_name(ds, strval);
374 dsl_dataset_rele(ds, FTAG);
375 dsl_pool_config_exit(dp, FTAG);
378 intval = za.za_first_integer;
381 spa_prop_add_list(*nvp, prop, strval, intval, src);
385 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
390 /* string property */
391 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
392 err = zap_lookup(mos, spa->spa_pool_props_object,
393 za.za_name, 1, za.za_num_integers, strval);
395 kmem_free(strval, za.za_num_integers);
398 spa_prop_add_list(*nvp, prop, strval, 0, src);
399 kmem_free(strval, za.za_num_integers);
406 zap_cursor_fini(&zc);
407 mutex_exit(&spa->spa_props_lock);
409 if (err && err != ENOENT) {
419 * Validate the given pool properties nvlist and modify the list
420 * for the property values to be set.
423 spa_prop_validate(spa_t *spa, nvlist_t *props)
426 int error = 0, reset_bootfs = 0;
428 boolean_t has_feature = B_FALSE;
431 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
433 char *strval, *slash, *check, *fname;
434 const char *propname = nvpair_name(elem);
435 zpool_prop_t prop = zpool_name_to_prop(propname);
439 if (!zpool_prop_feature(propname)) {
440 error = SET_ERROR(EINVAL);
445 * Sanitize the input.
447 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
448 error = SET_ERROR(EINVAL);
452 if (nvpair_value_uint64(elem, &intval) != 0) {
453 error = SET_ERROR(EINVAL);
458 error = SET_ERROR(EINVAL);
462 fname = strchr(propname, '@') + 1;
463 if (zfeature_lookup_name(fname, NULL) != 0) {
464 error = SET_ERROR(EINVAL);
468 has_feature = B_TRUE;
471 case ZPOOL_PROP_VERSION:
472 error = nvpair_value_uint64(elem, &intval);
474 (intval < spa_version(spa) ||
475 intval > SPA_VERSION_BEFORE_FEATURES ||
477 error = SET_ERROR(EINVAL);
480 case ZPOOL_PROP_DELEGATION:
481 case ZPOOL_PROP_AUTOREPLACE:
482 case ZPOOL_PROP_LISTSNAPS:
483 case ZPOOL_PROP_AUTOEXPAND:
484 error = nvpair_value_uint64(elem, &intval);
485 if (!error && intval > 1)
486 error = SET_ERROR(EINVAL);
489 case ZPOOL_PROP_BOOTFS:
491 * If the pool version is less than SPA_VERSION_BOOTFS,
492 * or the pool is still being created (version == 0),
493 * the bootfs property cannot be set.
495 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
496 error = SET_ERROR(ENOTSUP);
501 * Make sure the vdev config is bootable
503 if (!vdev_is_bootable(spa->spa_root_vdev)) {
504 error = SET_ERROR(ENOTSUP);
510 error = nvpair_value_string(elem, &strval);
516 if (strval == NULL || strval[0] == '\0') {
517 objnum = zpool_prop_default_numeric(
522 if (error = dmu_objset_hold(strval, FTAG, &os))
526 * Must be ZPL, and its property settings
527 * must be supported by GRUB (compression
528 * is not gzip, and large blocks are not used).
531 if (dmu_objset_type(os) != DMU_OST_ZFS) {
532 error = SET_ERROR(ENOTSUP);
534 dsl_prop_get_int_ds(dmu_objset_ds(os),
535 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
537 !BOOTFS_COMPRESS_VALID(propval)) {
538 error = SET_ERROR(ENOTSUP);
540 dsl_prop_get_int_ds(dmu_objset_ds(os),
541 zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
543 propval > SPA_OLD_MAXBLOCKSIZE) {
544 error = SET_ERROR(ENOTSUP);
546 objnum = dmu_objset_id(os);
548 dmu_objset_rele(os, FTAG);
552 case ZPOOL_PROP_FAILUREMODE:
553 error = nvpair_value_uint64(elem, &intval);
554 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
555 intval > ZIO_FAILURE_MODE_PANIC))
556 error = SET_ERROR(EINVAL);
559 * This is a special case which only occurs when
560 * the pool has completely failed. This allows
561 * the user to change the in-core failmode property
562 * without syncing it out to disk (I/Os might
563 * currently be blocked). We do this by returning
564 * EIO to the caller (spa_prop_set) to trick it
565 * into thinking we encountered a property validation
568 if (!error && spa_suspended(spa)) {
569 spa->spa_failmode = intval;
570 error = SET_ERROR(EIO);
574 case ZPOOL_PROP_CACHEFILE:
575 if ((error = nvpair_value_string(elem, &strval)) != 0)
578 if (strval[0] == '\0')
581 if (strcmp(strval, "none") == 0)
584 if (strval[0] != '/') {
585 error = SET_ERROR(EINVAL);
589 slash = strrchr(strval, '/');
590 ASSERT(slash != NULL);
592 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
593 strcmp(slash, "/..") == 0)
594 error = SET_ERROR(EINVAL);
597 case ZPOOL_PROP_COMMENT:
598 if ((error = nvpair_value_string(elem, &strval)) != 0)
600 for (check = strval; *check != '\0'; check++) {
602 * The kernel doesn't have an easy isprint()
603 * check. For this kernel check, we merely
604 * check ASCII apart from DEL. Fix this if
605 * there is an easy-to-use kernel isprint().
607 if (*check >= 0x7f) {
608 error = SET_ERROR(EINVAL);
613 if (strlen(strval) > ZPROP_MAX_COMMENT)
617 case ZPOOL_PROP_DEDUPDITTO:
618 if (spa_version(spa) < SPA_VERSION_DEDUP)
619 error = SET_ERROR(ENOTSUP);
621 error = nvpair_value_uint64(elem, &intval);
623 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
624 error = SET_ERROR(EINVAL);
632 if (!error && reset_bootfs) {
633 error = nvlist_remove(props,
634 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
637 error = nvlist_add_uint64(props,
638 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
646 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
649 spa_config_dirent_t *dp;
651 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
655 dp = kmem_alloc(sizeof (spa_config_dirent_t),
658 if (cachefile[0] == '\0')
659 dp->scd_path = spa_strdup(spa_config_path);
660 else if (strcmp(cachefile, "none") == 0)
663 dp->scd_path = spa_strdup(cachefile);
665 list_insert_head(&spa->spa_config_list, dp);
667 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
671 spa_prop_set(spa_t *spa, nvlist_t *nvp)
674 nvpair_t *elem = NULL;
675 boolean_t need_sync = B_FALSE;
677 if ((error = spa_prop_validate(spa, nvp)) != 0)
680 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
681 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
683 if (prop == ZPOOL_PROP_CACHEFILE ||
684 prop == ZPOOL_PROP_ALTROOT ||
685 prop == ZPOOL_PROP_READONLY)
688 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
691 if (prop == ZPOOL_PROP_VERSION) {
692 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
694 ASSERT(zpool_prop_feature(nvpair_name(elem)));
695 ver = SPA_VERSION_FEATURES;
699 /* Save time if the version is already set. */
700 if (ver == spa_version(spa))
704 * In addition to the pool directory object, we might
705 * create the pool properties object, the features for
706 * read object, the features for write object, or the
707 * feature descriptions object.
709 error = dsl_sync_task(spa->spa_name, NULL,
710 spa_sync_version, &ver,
711 6, ZFS_SPACE_CHECK_RESERVED);
722 return (dsl_sync_task(spa->spa_name, NULL, spa_sync_props,
723 nvp, 6, ZFS_SPACE_CHECK_RESERVED));
730 * If the bootfs property value is dsobj, clear it.
733 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
735 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
736 VERIFY(zap_remove(spa->spa_meta_objset,
737 spa->spa_pool_props_object,
738 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
745 spa_change_guid_check(void *arg, dmu_tx_t *tx)
747 uint64_t *newguid = arg;
748 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
749 vdev_t *rvd = spa->spa_root_vdev;
752 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
753 vdev_state = rvd->vdev_state;
754 spa_config_exit(spa, SCL_STATE, FTAG);
756 if (vdev_state != VDEV_STATE_HEALTHY)
757 return (SET_ERROR(ENXIO));
759 ASSERT3U(spa_guid(spa), !=, *newguid);
765 spa_change_guid_sync(void *arg, dmu_tx_t *tx)
767 uint64_t *newguid = arg;
768 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
770 vdev_t *rvd = spa->spa_root_vdev;
772 oldguid = spa_guid(spa);
774 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
775 rvd->vdev_guid = *newguid;
776 rvd->vdev_guid_sum += (*newguid - oldguid);
777 vdev_config_dirty(rvd);
778 spa_config_exit(spa, SCL_STATE, FTAG);
780 spa_history_log_internal(spa, "guid change", tx, "old=%llu new=%llu",
785 * Change the GUID for the pool. This is done so that we can later
786 * re-import a pool built from a clone of our own vdevs. We will modify
787 * the root vdev's guid, our own pool guid, and then mark all of our
788 * vdevs dirty. Note that we must make sure that all our vdevs are
789 * online when we do this, or else any vdevs that weren't present
790 * would be orphaned from our pool. We are also going to issue a
791 * sysevent to update any watchers.
794 spa_change_guid(spa_t *spa)
799 mutex_enter(&spa->spa_vdev_top_lock);
800 mutex_enter(&spa_namespace_lock);
801 guid = spa_generate_guid(NULL);
803 error = dsl_sync_task(spa->spa_name, spa_change_guid_check,
804 spa_change_guid_sync, &guid, 5, ZFS_SPACE_CHECK_RESERVED);
807 spa_config_sync(spa, B_FALSE, B_TRUE);
808 spa_event_notify(spa, NULL, ESC_ZFS_POOL_REGUID);
811 mutex_exit(&spa_namespace_lock);
812 mutex_exit(&spa->spa_vdev_top_lock);
818 * ==========================================================================
819 * SPA state manipulation (open/create/destroy/import/export)
820 * ==========================================================================
824 spa_error_entry_compare(const void *a, const void *b)
826 spa_error_entry_t *sa = (spa_error_entry_t *)a;
827 spa_error_entry_t *sb = (spa_error_entry_t *)b;
830 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
831 sizeof (zbookmark_phys_t));
842 * Utility function which retrieves copies of the current logs and
843 * re-initializes them in the process.
846 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
848 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
850 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
851 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
853 avl_create(&spa->spa_errlist_scrub,
854 spa_error_entry_compare, sizeof (spa_error_entry_t),
855 offsetof(spa_error_entry_t, se_avl));
856 avl_create(&spa->spa_errlist_last,
857 spa_error_entry_compare, sizeof (spa_error_entry_t),
858 offsetof(spa_error_entry_t, se_avl));
862 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
864 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
865 enum zti_modes mode = ztip->zti_mode;
866 uint_t value = ztip->zti_value;
867 uint_t count = ztip->zti_count;
868 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
871 boolean_t batch = B_FALSE;
873 if (mode == ZTI_MODE_NULL) {
875 tqs->stqs_taskq = NULL;
879 ASSERT3U(count, >, 0);
881 tqs->stqs_count = count;
882 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
886 ASSERT3U(value, >=, 1);
887 value = MAX(value, 1);
892 flags |= TASKQ_THREADS_CPU_PCT;
893 value = zio_taskq_batch_pct;
897 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
899 zio_type_name[t], zio_taskq_types[q], mode, value);
903 for (uint_t i = 0; i < count; i++) {
907 (void) snprintf(name, sizeof (name), "%s_%s_%u",
908 zio_type_name[t], zio_taskq_types[q], i);
910 (void) snprintf(name, sizeof (name), "%s_%s",
911 zio_type_name[t], zio_taskq_types[q]);
915 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
917 flags |= TASKQ_DC_BATCH;
919 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
920 spa->spa_proc, zio_taskq_basedc, flags);
923 pri_t pri = maxclsyspri;
925 * The write issue taskq can be extremely CPU
926 * intensive. Run it at slightly lower priority
927 * than the other taskqs.
929 if (t == ZIO_TYPE_WRITE && q == ZIO_TASKQ_ISSUE)
932 tq = taskq_create_proc(name, value, pri, 50,
933 INT_MAX, spa->spa_proc, flags);
938 tqs->stqs_taskq[i] = tq;
943 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
945 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
947 if (tqs->stqs_taskq == NULL) {
948 ASSERT0(tqs->stqs_count);
952 for (uint_t i = 0; i < tqs->stqs_count; i++) {
953 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
954 taskq_destroy(tqs->stqs_taskq[i]);
957 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
958 tqs->stqs_taskq = NULL;
962 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
963 * Note that a type may have multiple discrete taskqs to avoid lock contention
964 * on the taskq itself. In that case we choose which taskq at random by using
965 * the low bits of gethrtime().
968 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
969 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
971 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
974 ASSERT3P(tqs->stqs_taskq, !=, NULL);
975 ASSERT3U(tqs->stqs_count, !=, 0);
977 if (tqs->stqs_count == 1) {
978 tq = tqs->stqs_taskq[0];
981 tq = tqs->stqs_taskq[cpu_ticks() % tqs->stqs_count];
983 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
987 taskq_dispatch_ent(tq, func, arg, flags, ent);
991 spa_create_zio_taskqs(spa_t *spa)
993 for (int t = 0; t < ZIO_TYPES; t++) {
994 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
995 spa_taskqs_init(spa, t, q);
1003 spa_thread(void *arg)
1005 callb_cpr_t cprinfo;
1008 user_t *pu = PTOU(curproc);
1010 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
1013 ASSERT(curproc != &p0);
1014 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
1015 "zpool-%s", spa->spa_name);
1016 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
1019 /* bind this thread to the requested psrset */
1020 if (zio_taskq_psrset_bind != PS_NONE) {
1022 mutex_enter(&cpu_lock);
1023 mutex_enter(&pidlock);
1024 mutex_enter(&curproc->p_lock);
1026 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
1027 0, NULL, NULL) == 0) {
1028 curthread->t_bind_pset = zio_taskq_psrset_bind;
1031 "Couldn't bind process for zfs pool \"%s\" to "
1032 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
1035 mutex_exit(&curproc->p_lock);
1036 mutex_exit(&pidlock);
1037 mutex_exit(&cpu_lock);
1043 if (zio_taskq_sysdc) {
1044 sysdc_thread_enter(curthread, 100, 0);
1048 spa->spa_proc = curproc;
1049 spa->spa_did = curthread->t_did;
1051 spa_create_zio_taskqs(spa);
1053 mutex_enter(&spa->spa_proc_lock);
1054 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
1056 spa->spa_proc_state = SPA_PROC_ACTIVE;
1057 cv_broadcast(&spa->spa_proc_cv);
1059 CALLB_CPR_SAFE_BEGIN(&cprinfo);
1060 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
1061 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1062 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1064 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1065 spa->spa_proc_state = SPA_PROC_GONE;
1066 spa->spa_proc = &p0;
1067 cv_broadcast(&spa->spa_proc_cv);
1068 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1070 mutex_enter(&curproc->p_lock);
1073 #endif /* SPA_PROCESS */
1077 * Activate an uninitialized pool.
1080 spa_activate(spa_t *spa, int mode)
1082 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1084 spa->spa_state = POOL_STATE_ACTIVE;
1085 spa->spa_mode = mode;
1087 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1088 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1090 /* Try to create a covering process */
1091 mutex_enter(&spa->spa_proc_lock);
1092 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1093 ASSERT(spa->spa_proc == &p0);
1097 /* Only create a process if we're going to be around a while. */
1098 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1099 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1101 spa->spa_proc_state = SPA_PROC_CREATED;
1102 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1103 cv_wait(&spa->spa_proc_cv,
1104 &spa->spa_proc_lock);
1106 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1107 ASSERT(spa->spa_proc != &p0);
1108 ASSERT(spa->spa_did != 0);
1112 "Couldn't create process for zfs pool \"%s\"\n",
1117 #endif /* SPA_PROCESS */
1118 mutex_exit(&spa->spa_proc_lock);
1120 /* If we didn't create a process, we need to create our taskqs. */
1121 ASSERT(spa->spa_proc == &p0);
1122 if (spa->spa_proc == &p0) {
1123 spa_create_zio_taskqs(spa);
1127 * Start TRIM thread.
1129 trim_thread_create(spa);
1131 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1132 offsetof(vdev_t, vdev_config_dirty_node));
1133 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1134 offsetof(vdev_t, vdev_state_dirty_node));
1136 txg_list_create(&spa->spa_vdev_txg_list,
1137 offsetof(struct vdev, vdev_txg_node));
1139 avl_create(&spa->spa_errlist_scrub,
1140 spa_error_entry_compare, sizeof (spa_error_entry_t),
1141 offsetof(spa_error_entry_t, se_avl));
1142 avl_create(&spa->spa_errlist_last,
1143 spa_error_entry_compare, sizeof (spa_error_entry_t),
1144 offsetof(spa_error_entry_t, se_avl));
1148 * Opposite of spa_activate().
1151 spa_deactivate(spa_t *spa)
1153 ASSERT(spa->spa_sync_on == B_FALSE);
1154 ASSERT(spa->spa_dsl_pool == NULL);
1155 ASSERT(spa->spa_root_vdev == NULL);
1156 ASSERT(spa->spa_async_zio_root == NULL);
1157 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1160 * Stop TRIM thread in case spa_unload() wasn't called directly
1161 * before spa_deactivate().
1163 trim_thread_destroy(spa);
1165 txg_list_destroy(&spa->spa_vdev_txg_list);
1167 list_destroy(&spa->spa_config_dirty_list);
1168 list_destroy(&spa->spa_state_dirty_list);
1170 for (int t = 0; t < ZIO_TYPES; t++) {
1171 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
1172 spa_taskqs_fini(spa, t, q);
1176 metaslab_class_destroy(spa->spa_normal_class);
1177 spa->spa_normal_class = NULL;
1179 metaslab_class_destroy(spa->spa_log_class);
1180 spa->spa_log_class = NULL;
1183 * If this was part of an import or the open otherwise failed, we may
1184 * still have errors left in the queues. Empty them just in case.
1186 spa_errlog_drain(spa);
1188 avl_destroy(&spa->spa_errlist_scrub);
1189 avl_destroy(&spa->spa_errlist_last);
1191 spa->spa_state = POOL_STATE_UNINITIALIZED;
1193 mutex_enter(&spa->spa_proc_lock);
1194 if (spa->spa_proc_state != SPA_PROC_NONE) {
1195 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1196 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1197 cv_broadcast(&spa->spa_proc_cv);
1198 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1199 ASSERT(spa->spa_proc != &p0);
1200 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1202 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1203 spa->spa_proc_state = SPA_PROC_NONE;
1205 ASSERT(spa->spa_proc == &p0);
1206 mutex_exit(&spa->spa_proc_lock);
1210 * We want to make sure spa_thread() has actually exited the ZFS
1211 * module, so that the module can't be unloaded out from underneath
1214 if (spa->spa_did != 0) {
1215 thread_join(spa->spa_did);
1218 #endif /* SPA_PROCESS */
1222 * Verify a pool configuration, and construct the vdev tree appropriately. This
1223 * will create all the necessary vdevs in the appropriate layout, with each vdev
1224 * in the CLOSED state. This will prep the pool before open/creation/import.
1225 * All vdev validation is done by the vdev_alloc() routine.
1228 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1229 uint_t id, int atype)
1235 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1238 if ((*vdp)->vdev_ops->vdev_op_leaf)
1241 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1244 if (error == ENOENT)
1250 return (SET_ERROR(EINVAL));
1253 for (int c = 0; c < children; c++) {
1255 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1263 ASSERT(*vdp != NULL);
1269 * Opposite of spa_load().
1272 spa_unload(spa_t *spa)
1276 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1281 trim_thread_destroy(spa);
1286 spa_async_suspend(spa);
1291 if (spa->spa_sync_on) {
1292 txg_sync_stop(spa->spa_dsl_pool);
1293 spa->spa_sync_on = B_FALSE;
1297 * Wait for any outstanding async I/O to complete.
1299 if (spa->spa_async_zio_root != NULL) {
1300 for (int i = 0; i < max_ncpus; i++)
1301 (void) zio_wait(spa->spa_async_zio_root[i]);
1302 kmem_free(spa->spa_async_zio_root, max_ncpus * sizeof (void *));
1303 spa->spa_async_zio_root = NULL;
1306 bpobj_close(&spa->spa_deferred_bpobj);
1308 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1313 if (spa->spa_root_vdev)
1314 vdev_free(spa->spa_root_vdev);
1315 ASSERT(spa->spa_root_vdev == NULL);
1318 * Close the dsl pool.
1320 if (spa->spa_dsl_pool) {
1321 dsl_pool_close(spa->spa_dsl_pool);
1322 spa->spa_dsl_pool = NULL;
1323 spa->spa_meta_objset = NULL;
1330 * Drop and purge level 2 cache
1332 spa_l2cache_drop(spa);
1334 for (i = 0; i < spa->spa_spares.sav_count; i++)
1335 vdev_free(spa->spa_spares.sav_vdevs[i]);
1336 if (spa->spa_spares.sav_vdevs) {
1337 kmem_free(spa->spa_spares.sav_vdevs,
1338 spa->spa_spares.sav_count * sizeof (void *));
1339 spa->spa_spares.sav_vdevs = NULL;
1341 if (spa->spa_spares.sav_config) {
1342 nvlist_free(spa->spa_spares.sav_config);
1343 spa->spa_spares.sav_config = NULL;
1345 spa->spa_spares.sav_count = 0;
1347 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1348 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1349 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1351 if (spa->spa_l2cache.sav_vdevs) {
1352 kmem_free(spa->spa_l2cache.sav_vdevs,
1353 spa->spa_l2cache.sav_count * sizeof (void *));
1354 spa->spa_l2cache.sav_vdevs = NULL;
1356 if (spa->spa_l2cache.sav_config) {
1357 nvlist_free(spa->spa_l2cache.sav_config);
1358 spa->spa_l2cache.sav_config = NULL;
1360 spa->spa_l2cache.sav_count = 0;
1362 spa->spa_async_suspended = 0;
1364 if (spa->spa_comment != NULL) {
1365 spa_strfree(spa->spa_comment);
1366 spa->spa_comment = NULL;
1369 spa_config_exit(spa, SCL_ALL, FTAG);
1373 * Load (or re-load) the current list of vdevs describing the active spares for
1374 * this pool. When this is called, we have some form of basic information in
1375 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1376 * then re-generate a more complete list including status information.
1379 spa_load_spares(spa_t *spa)
1386 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1389 * First, close and free any existing spare vdevs.
1391 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1392 vd = spa->spa_spares.sav_vdevs[i];
1394 /* Undo the call to spa_activate() below */
1395 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1396 B_FALSE)) != NULL && tvd->vdev_isspare)
1397 spa_spare_remove(tvd);
1402 if (spa->spa_spares.sav_vdevs)
1403 kmem_free(spa->spa_spares.sav_vdevs,
1404 spa->spa_spares.sav_count * sizeof (void *));
1406 if (spa->spa_spares.sav_config == NULL)
1409 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1410 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1412 spa->spa_spares.sav_count = (int)nspares;
1413 spa->spa_spares.sav_vdevs = NULL;
1419 * Construct the array of vdevs, opening them to get status in the
1420 * process. For each spare, there is potentially two different vdev_t
1421 * structures associated with it: one in the list of spares (used only
1422 * for basic validation purposes) and one in the active vdev
1423 * configuration (if it's spared in). During this phase we open and
1424 * validate each vdev on the spare list. If the vdev also exists in the
1425 * active configuration, then we also mark this vdev as an active spare.
1427 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1429 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1430 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1431 VDEV_ALLOC_SPARE) == 0);
1434 spa->spa_spares.sav_vdevs[i] = vd;
1436 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1437 B_FALSE)) != NULL) {
1438 if (!tvd->vdev_isspare)
1442 * We only mark the spare active if we were successfully
1443 * able to load the vdev. Otherwise, importing a pool
1444 * with a bad active spare would result in strange
1445 * behavior, because multiple pool would think the spare
1446 * is actively in use.
1448 * There is a vulnerability here to an equally bizarre
1449 * circumstance, where a dead active spare is later
1450 * brought back to life (onlined or otherwise). Given
1451 * the rarity of this scenario, and the extra complexity
1452 * it adds, we ignore the possibility.
1454 if (!vdev_is_dead(tvd))
1455 spa_spare_activate(tvd);
1459 vd->vdev_aux = &spa->spa_spares;
1461 if (vdev_open(vd) != 0)
1464 if (vdev_validate_aux(vd) == 0)
1469 * Recompute the stashed list of spares, with status information
1472 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1473 DATA_TYPE_NVLIST_ARRAY) == 0);
1475 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1477 for (i = 0; i < spa->spa_spares.sav_count; i++)
1478 spares[i] = vdev_config_generate(spa,
1479 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1480 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1481 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1482 for (i = 0; i < spa->spa_spares.sav_count; i++)
1483 nvlist_free(spares[i]);
1484 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1488 * Load (or re-load) the current list of vdevs describing the active l2cache for
1489 * this pool. When this is called, we have some form of basic information in
1490 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1491 * then re-generate a more complete list including status information.
1492 * Devices which are already active have their details maintained, and are
1496 spa_load_l2cache(spa_t *spa)
1500 int i, j, oldnvdevs;
1502 vdev_t *vd, **oldvdevs, **newvdevs;
1503 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1505 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1507 if (sav->sav_config != NULL) {
1508 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1509 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1510 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
1516 oldvdevs = sav->sav_vdevs;
1517 oldnvdevs = sav->sav_count;
1518 sav->sav_vdevs = NULL;
1522 * Process new nvlist of vdevs.
1524 for (i = 0; i < nl2cache; i++) {
1525 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1529 for (j = 0; j < oldnvdevs; j++) {
1531 if (vd != NULL && guid == vd->vdev_guid) {
1533 * Retain previous vdev for add/remove ops.
1541 if (newvdevs[i] == NULL) {
1545 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1546 VDEV_ALLOC_L2CACHE) == 0);
1551 * Commit this vdev as an l2cache device,
1552 * even if it fails to open.
1554 spa_l2cache_add(vd);
1559 spa_l2cache_activate(vd);
1561 if (vdev_open(vd) != 0)
1564 (void) vdev_validate_aux(vd);
1566 if (!vdev_is_dead(vd))
1567 l2arc_add_vdev(spa, vd);
1572 * Purge vdevs that were dropped
1574 for (i = 0; i < oldnvdevs; i++) {
1579 ASSERT(vd->vdev_isl2cache);
1581 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1582 pool != 0ULL && l2arc_vdev_present(vd))
1583 l2arc_remove_vdev(vd);
1584 vdev_clear_stats(vd);
1590 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1592 if (sav->sav_config == NULL)
1595 sav->sav_vdevs = newvdevs;
1596 sav->sav_count = (int)nl2cache;
1599 * Recompute the stashed list of l2cache devices, with status
1600 * information this time.
1602 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1603 DATA_TYPE_NVLIST_ARRAY) == 0);
1605 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
1606 for (i = 0; i < sav->sav_count; i++)
1607 l2cache[i] = vdev_config_generate(spa,
1608 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1609 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1610 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1612 for (i = 0; i < sav->sav_count; i++)
1613 nvlist_free(l2cache[i]);
1615 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1619 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1622 char *packed = NULL;
1627 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1630 nvsize = *(uint64_t *)db->db_data;
1631 dmu_buf_rele(db, FTAG);
1633 packed = kmem_alloc(nvsize, KM_SLEEP);
1634 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1637 error = nvlist_unpack(packed, nvsize, value, 0);
1638 kmem_free(packed, nvsize);
1644 * Checks to see if the given vdev could not be opened, in which case we post a
1645 * sysevent to notify the autoreplace code that the device has been removed.
1648 spa_check_removed(vdev_t *vd)
1650 for (int c = 0; c < vd->vdev_children; c++)
1651 spa_check_removed(vd->vdev_child[c]);
1653 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd) &&
1655 zfs_post_autoreplace(vd->vdev_spa, vd);
1656 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
1661 * Validate the current config against the MOS config
1664 spa_config_valid(spa_t *spa, nvlist_t *config)
1666 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1669 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1671 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1672 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1674 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1677 * If we're doing a normal import, then build up any additional
1678 * diagnostic information about missing devices in this config.
1679 * We'll pass this up to the user for further processing.
1681 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1682 nvlist_t **child, *nv;
1685 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1687 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1689 for (int c = 0; c < rvd->vdev_children; c++) {
1690 vdev_t *tvd = rvd->vdev_child[c];
1691 vdev_t *mtvd = mrvd->vdev_child[c];
1693 if (tvd->vdev_ops == &vdev_missing_ops &&
1694 mtvd->vdev_ops != &vdev_missing_ops &&
1696 child[idx++] = vdev_config_generate(spa, mtvd,
1701 VERIFY(nvlist_add_nvlist_array(nv,
1702 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1703 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1704 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1706 for (int i = 0; i < idx; i++)
1707 nvlist_free(child[i]);
1710 kmem_free(child, rvd->vdev_children * sizeof (char **));
1714 * Compare the root vdev tree with the information we have
1715 * from the MOS config (mrvd). Check each top-level vdev
1716 * with the corresponding MOS config top-level (mtvd).
1718 for (int c = 0; c < rvd->vdev_children; c++) {
1719 vdev_t *tvd = rvd->vdev_child[c];
1720 vdev_t *mtvd = mrvd->vdev_child[c];
1723 * Resolve any "missing" vdevs in the current configuration.
1724 * If we find that the MOS config has more accurate information
1725 * about the top-level vdev then use that vdev instead.
1727 if (tvd->vdev_ops == &vdev_missing_ops &&
1728 mtvd->vdev_ops != &vdev_missing_ops) {
1730 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1734 * Device specific actions.
1736 if (mtvd->vdev_islog) {
1737 spa_set_log_state(spa, SPA_LOG_CLEAR);
1740 * XXX - once we have 'readonly' pool
1741 * support we should be able to handle
1742 * missing data devices by transitioning
1743 * the pool to readonly.
1749 * Swap the missing vdev with the data we were
1750 * able to obtain from the MOS config.
1752 vdev_remove_child(rvd, tvd);
1753 vdev_remove_child(mrvd, mtvd);
1755 vdev_add_child(rvd, mtvd);
1756 vdev_add_child(mrvd, tvd);
1758 spa_config_exit(spa, SCL_ALL, FTAG);
1760 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1763 } else if (mtvd->vdev_islog) {
1765 * Load the slog device's state from the MOS config
1766 * since it's possible that the label does not
1767 * contain the most up-to-date information.
1769 vdev_load_log_state(tvd, mtvd);
1774 spa_config_exit(spa, SCL_ALL, FTAG);
1777 * Ensure we were able to validate the config.
1779 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1783 * Check for missing log devices
1786 spa_check_logs(spa_t *spa)
1788 boolean_t rv = B_FALSE;
1790 switch (spa->spa_log_state) {
1791 case SPA_LOG_MISSING:
1792 /* need to recheck in case slog has been restored */
1793 case SPA_LOG_UNKNOWN:
1794 rv = (dmu_objset_find(spa->spa_name, zil_check_log_chain,
1795 NULL, DS_FIND_CHILDREN) != 0);
1797 spa_set_log_state(spa, SPA_LOG_MISSING);
1804 spa_passivate_log(spa_t *spa)
1806 vdev_t *rvd = spa->spa_root_vdev;
1807 boolean_t slog_found = B_FALSE;
1809 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1811 if (!spa_has_slogs(spa))
1814 for (int c = 0; c < rvd->vdev_children; c++) {
1815 vdev_t *tvd = rvd->vdev_child[c];
1816 metaslab_group_t *mg = tvd->vdev_mg;
1818 if (tvd->vdev_islog) {
1819 metaslab_group_passivate(mg);
1820 slog_found = B_TRUE;
1824 return (slog_found);
1828 spa_activate_log(spa_t *spa)
1830 vdev_t *rvd = spa->spa_root_vdev;
1832 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1834 for (int c = 0; c < rvd->vdev_children; c++) {
1835 vdev_t *tvd = rvd->vdev_child[c];
1836 metaslab_group_t *mg = tvd->vdev_mg;
1838 if (tvd->vdev_islog)
1839 metaslab_group_activate(mg);
1844 spa_offline_log(spa_t *spa)
1848 error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1849 NULL, DS_FIND_CHILDREN);
1852 * We successfully offlined the log device, sync out the
1853 * current txg so that the "stubby" block can be removed
1856 txg_wait_synced(spa->spa_dsl_pool, 0);
1862 spa_aux_check_removed(spa_aux_vdev_t *sav)
1866 for (i = 0; i < sav->sav_count; i++)
1867 spa_check_removed(sav->sav_vdevs[i]);
1871 spa_claim_notify(zio_t *zio)
1873 spa_t *spa = zio->io_spa;
1878 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1879 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1880 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1881 mutex_exit(&spa->spa_props_lock);
1884 typedef struct spa_load_error {
1885 uint64_t sle_meta_count;
1886 uint64_t sle_data_count;
1890 spa_load_verify_done(zio_t *zio)
1892 blkptr_t *bp = zio->io_bp;
1893 spa_load_error_t *sle = zio->io_private;
1894 dmu_object_type_t type = BP_GET_TYPE(bp);
1895 int error = zio->io_error;
1896 spa_t *spa = zio->io_spa;
1899 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1900 type != DMU_OT_INTENT_LOG)
1901 atomic_inc_64(&sle->sle_meta_count);
1903 atomic_inc_64(&sle->sle_data_count);
1905 zio_data_buf_free(zio->io_data, zio->io_size);
1907 mutex_enter(&spa->spa_scrub_lock);
1908 spa->spa_scrub_inflight--;
1909 cv_broadcast(&spa->spa_scrub_io_cv);
1910 mutex_exit(&spa->spa_scrub_lock);
1914 * Maximum number of concurrent scrub i/os to create while verifying
1915 * a pool while importing it.
1917 int spa_load_verify_maxinflight = 10000;
1918 boolean_t spa_load_verify_metadata = B_TRUE;
1919 boolean_t spa_load_verify_data = B_TRUE;
1921 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_maxinflight, CTLFLAG_RWTUN,
1922 &spa_load_verify_maxinflight, 0,
1923 "Maximum number of concurrent scrub I/Os to create while verifying a "
1924 "pool while importing it");
1926 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_metadata, CTLFLAG_RWTUN,
1927 &spa_load_verify_metadata, 0,
1928 "Check metadata on import?");
1930 SYSCTL_INT(_vfs_zfs, OID_AUTO, spa_load_verify_data, CTLFLAG_RWTUN,
1931 &spa_load_verify_data, 0,
1932 "Check user data on import?");
1936 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1937 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1939 if (BP_IS_HOLE(bp) || BP_IS_EMBEDDED(bp))
1942 * Note: normally this routine will not be called if
1943 * spa_load_verify_metadata is not set. However, it may be useful
1944 * to manually set the flag after the traversal has begun.
1946 if (!spa_load_verify_metadata)
1948 if (BP_GET_BUFC_TYPE(bp) == ARC_BUFC_DATA && !spa_load_verify_data)
1952 size_t size = BP_GET_PSIZE(bp);
1953 void *data = zio_data_buf_alloc(size);
1955 mutex_enter(&spa->spa_scrub_lock);
1956 while (spa->spa_scrub_inflight >= spa_load_verify_maxinflight)
1957 cv_wait(&spa->spa_scrub_io_cv, &spa->spa_scrub_lock);
1958 spa->spa_scrub_inflight++;
1959 mutex_exit(&spa->spa_scrub_lock);
1961 zio_nowait(zio_read(rio, spa, bp, data, size,
1962 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1963 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1964 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1969 spa_load_verify(spa_t *spa)
1972 spa_load_error_t sle = { 0 };
1973 zpool_rewind_policy_t policy;
1974 boolean_t verify_ok = B_FALSE;
1977 zpool_get_rewind_policy(spa->spa_config, &policy);
1979 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1982 rio = zio_root(spa, NULL, &sle,
1983 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1985 if (spa_load_verify_metadata) {
1986 error = traverse_pool(spa, spa->spa_verify_min_txg,
1987 TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA,
1988 spa_load_verify_cb, rio);
1991 (void) zio_wait(rio);
1993 spa->spa_load_meta_errors = sle.sle_meta_count;
1994 spa->spa_load_data_errors = sle.sle_data_count;
1996 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1997 sle.sle_data_count <= policy.zrp_maxdata) {
2001 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
2002 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
2004 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
2005 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2006 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
2007 VERIFY(nvlist_add_int64(spa->spa_load_info,
2008 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
2009 VERIFY(nvlist_add_uint64(spa->spa_load_info,
2010 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
2012 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
2016 if (error != ENXIO && error != EIO)
2017 error = SET_ERROR(EIO);
2021 return (verify_ok ? 0 : EIO);
2025 * Find a value in the pool props object.
2028 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
2030 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
2031 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
2035 * Find a value in the pool directory object.
2038 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
2040 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
2041 name, sizeof (uint64_t), 1, val));
2045 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
2047 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
2052 * Fix up config after a partly-completed split. This is done with the
2053 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2054 * pool have that entry in their config, but only the splitting one contains
2055 * a list of all the guids of the vdevs that are being split off.
2057 * This function determines what to do with that list: either rejoin
2058 * all the disks to the pool, or complete the splitting process. To attempt
2059 * the rejoin, each disk that is offlined is marked online again, and
2060 * we do a reopen() call. If the vdev label for every disk that was
2061 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2062 * then we call vdev_split() on each disk, and complete the split.
2064 * Otherwise we leave the config alone, with all the vdevs in place in
2065 * the original pool.
2068 spa_try_repair(spa_t *spa, nvlist_t *config)
2075 boolean_t attempt_reopen;
2077 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
2080 /* check that the config is complete */
2081 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
2082 &glist, &gcount) != 0)
2085 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_SLEEP);
2087 /* attempt to online all the vdevs & validate */
2088 attempt_reopen = B_TRUE;
2089 for (i = 0; i < gcount; i++) {
2090 if (glist[i] == 0) /* vdev is hole */
2093 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
2094 if (vd[i] == NULL) {
2096 * Don't bother attempting to reopen the disks;
2097 * just do the split.
2099 attempt_reopen = B_FALSE;
2101 /* attempt to re-online it */
2102 vd[i]->vdev_offline = B_FALSE;
2106 if (attempt_reopen) {
2107 vdev_reopen(spa->spa_root_vdev);
2109 /* check each device to see what state it's in */
2110 for (extracted = 0, i = 0; i < gcount; i++) {
2111 if (vd[i] != NULL &&
2112 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
2119 * If every disk has been moved to the new pool, or if we never
2120 * even attempted to look at them, then we split them off for
2123 if (!attempt_reopen || gcount == extracted) {
2124 for (i = 0; i < gcount; i++)
2127 vdev_reopen(spa->spa_root_vdev);
2130 kmem_free(vd, gcount * sizeof (vdev_t *));
2134 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2135 boolean_t mosconfig)
2137 nvlist_t *config = spa->spa_config;
2138 char *ereport = FM_EREPORT_ZFS_POOL;
2144 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2145 return (SET_ERROR(EINVAL));
2147 ASSERT(spa->spa_comment == NULL);
2148 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2149 spa->spa_comment = spa_strdup(comment);
2152 * Versioning wasn't explicitly added to the label until later, so if
2153 * it's not present treat it as the initial version.
2155 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2156 &spa->spa_ubsync.ub_version) != 0)
2157 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2159 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2160 &spa->spa_config_txg);
2162 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2163 spa_guid_exists(pool_guid, 0)) {
2164 error = SET_ERROR(EEXIST);
2166 spa->spa_config_guid = pool_guid;
2168 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2170 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2174 nvlist_free(spa->spa_load_info);
2175 spa->spa_load_info = fnvlist_alloc();
2177 gethrestime(&spa->spa_loaded_ts);
2178 error = spa_load_impl(spa, pool_guid, config, state, type,
2179 mosconfig, &ereport);
2182 spa->spa_minref = refcount_count(&spa->spa_refcount);
2184 if (error != EEXIST) {
2185 spa->spa_loaded_ts.tv_sec = 0;
2186 spa->spa_loaded_ts.tv_nsec = 0;
2188 if (error != EBADF) {
2189 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2192 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2199 * Load an existing storage pool, using the pool's builtin spa_config as a
2200 * source of configuration information.
2203 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2204 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2208 nvlist_t *nvroot = NULL;
2211 uberblock_t *ub = &spa->spa_uberblock;
2212 uint64_t children, config_cache_txg = spa->spa_config_txg;
2213 int orig_mode = spa->spa_mode;
2216 boolean_t missing_feat_write = B_FALSE;
2219 * If this is an untrusted config, access the pool in read-only mode.
2220 * This prevents things like resilvering recently removed devices.
2223 spa->spa_mode = FREAD;
2225 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2227 spa->spa_load_state = state;
2229 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2230 return (SET_ERROR(EINVAL));
2232 parse = (type == SPA_IMPORT_EXISTING ?
2233 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2236 * Create "The Godfather" zio to hold all async IOs
2238 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
2240 for (int i = 0; i < max_ncpus; i++) {
2241 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
2242 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
2243 ZIO_FLAG_GODFATHER);
2247 * Parse the configuration into a vdev tree. We explicitly set the
2248 * value that will be returned by spa_version() since parsing the
2249 * configuration requires knowing the version number.
2251 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2252 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2253 spa_config_exit(spa, SCL_ALL, FTAG);
2258 ASSERT(spa->spa_root_vdev == rvd);
2260 if (type != SPA_IMPORT_ASSEMBLE) {
2261 ASSERT(spa_guid(spa) == pool_guid);
2265 * Try to open all vdevs, loading each label in the process.
2267 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2268 error = vdev_open(rvd);
2269 spa_config_exit(spa, SCL_ALL, FTAG);
2274 * We need to validate the vdev labels against the configuration that
2275 * we have in hand, which is dependent on the setting of mosconfig. If
2276 * mosconfig is true then we're validating the vdev labels based on
2277 * that config. Otherwise, we're validating against the cached config
2278 * (zpool.cache) that was read when we loaded the zfs module, and then
2279 * later we will recursively call spa_load() and validate against
2282 * If we're assembling a new pool that's been split off from an
2283 * existing pool, the labels haven't yet been updated so we skip
2284 * validation for now.
2286 if (type != SPA_IMPORT_ASSEMBLE) {
2287 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2288 error = vdev_validate(rvd, mosconfig);
2289 spa_config_exit(spa, SCL_ALL, FTAG);
2294 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2295 return (SET_ERROR(ENXIO));
2299 * Find the best uberblock.
2301 vdev_uberblock_load(rvd, ub, &label);
2304 * If we weren't able to find a single valid uberblock, return failure.
2306 if (ub->ub_txg == 0) {
2308 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2312 * If the pool has an unsupported version we can't open it.
2314 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2316 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2319 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2323 * If we weren't able to find what's necessary for reading the
2324 * MOS in the label, return failure.
2326 if (label == NULL || nvlist_lookup_nvlist(label,
2327 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2329 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2334 * Update our in-core representation with the definitive values
2337 nvlist_free(spa->spa_label_features);
2338 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2344 * Look through entries in the label nvlist's features_for_read. If
2345 * there is a feature listed there which we don't understand then we
2346 * cannot open a pool.
2348 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2349 nvlist_t *unsup_feat;
2351 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2354 for (nvpair_t *nvp = nvlist_next_nvpair(spa->spa_label_features,
2356 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2357 if (!zfeature_is_supported(nvpair_name(nvp))) {
2358 VERIFY(nvlist_add_string(unsup_feat,
2359 nvpair_name(nvp), "") == 0);
2363 if (!nvlist_empty(unsup_feat)) {
2364 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2365 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2366 nvlist_free(unsup_feat);
2367 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2371 nvlist_free(unsup_feat);
2375 * If the vdev guid sum doesn't match the uberblock, we have an
2376 * incomplete configuration. We first check to see if the pool
2377 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2378 * If it is, defer the vdev_guid_sum check till later so we
2379 * can handle missing vdevs.
2381 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2382 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2383 rvd->vdev_guid_sum != ub->ub_guid_sum)
2384 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2386 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2387 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2388 spa_try_repair(spa, config);
2389 spa_config_exit(spa, SCL_ALL, FTAG);
2390 nvlist_free(spa->spa_config_splitting);
2391 spa->spa_config_splitting = NULL;
2395 * Initialize internal SPA structures.
2397 spa->spa_state = POOL_STATE_ACTIVE;
2398 spa->spa_ubsync = spa->spa_uberblock;
2399 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2400 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2401 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2402 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2403 spa->spa_claim_max_txg = spa->spa_first_txg;
2404 spa->spa_prev_software_version = ub->ub_software_version;
2406 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2408 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2409 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2411 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2412 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2414 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2415 boolean_t missing_feat_read = B_FALSE;
2416 nvlist_t *unsup_feat, *enabled_feat;
2418 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2419 &spa->spa_feat_for_read_obj) != 0) {
2420 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2423 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2424 &spa->spa_feat_for_write_obj) != 0) {
2425 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2428 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2429 &spa->spa_feat_desc_obj) != 0) {
2430 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2433 enabled_feat = fnvlist_alloc();
2434 unsup_feat = fnvlist_alloc();
2436 if (!spa_features_check(spa, B_FALSE,
2437 unsup_feat, enabled_feat))
2438 missing_feat_read = B_TRUE;
2440 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2441 if (!spa_features_check(spa, B_TRUE,
2442 unsup_feat, enabled_feat)) {
2443 missing_feat_write = B_TRUE;
2447 fnvlist_add_nvlist(spa->spa_load_info,
2448 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2450 if (!nvlist_empty(unsup_feat)) {
2451 fnvlist_add_nvlist(spa->spa_load_info,
2452 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2455 fnvlist_free(enabled_feat);
2456 fnvlist_free(unsup_feat);
2458 if (!missing_feat_read) {
2459 fnvlist_add_boolean(spa->spa_load_info,
2460 ZPOOL_CONFIG_CAN_RDONLY);
2464 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2465 * twofold: to determine whether the pool is available for
2466 * import in read-write mode and (if it is not) whether the
2467 * pool is available for import in read-only mode. If the pool
2468 * is available for import in read-write mode, it is displayed
2469 * as available in userland; if it is not available for import
2470 * in read-only mode, it is displayed as unavailable in
2471 * userland. If the pool is available for import in read-only
2472 * mode but not read-write mode, it is displayed as unavailable
2473 * in userland with a special note that the pool is actually
2474 * available for open in read-only mode.
2476 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2477 * missing a feature for write, we must first determine whether
2478 * the pool can be opened read-only before returning to
2479 * userland in order to know whether to display the
2480 * abovementioned note.
2482 if (missing_feat_read || (missing_feat_write &&
2483 spa_writeable(spa))) {
2484 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2489 * Load refcounts for ZFS features from disk into an in-memory
2490 * cache during SPA initialization.
2492 for (spa_feature_t i = 0; i < SPA_FEATURES; i++) {
2495 error = feature_get_refcount_from_disk(spa,
2496 &spa_feature_table[i], &refcount);
2498 spa->spa_feat_refcount_cache[i] = refcount;
2499 } else if (error == ENOTSUP) {
2500 spa->spa_feat_refcount_cache[i] =
2501 SPA_FEATURE_DISABLED;
2503 return (spa_vdev_err(rvd,
2504 VDEV_AUX_CORRUPT_DATA, EIO));
2509 if (spa_feature_is_active(spa, SPA_FEATURE_ENABLED_TXG)) {
2510 if (spa_dir_prop(spa, DMU_POOL_FEATURE_ENABLED_TXG,
2511 &spa->spa_feat_enabled_txg_obj) != 0)
2512 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2515 spa->spa_is_initializing = B_TRUE;
2516 error = dsl_pool_open(spa->spa_dsl_pool);
2517 spa->spa_is_initializing = B_FALSE;
2519 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2523 nvlist_t *policy = NULL, *nvconfig;
2525 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2526 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2528 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2529 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2531 unsigned long myhostid = 0;
2533 VERIFY(nvlist_lookup_string(nvconfig,
2534 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2537 myhostid = zone_get_hostid(NULL);
2540 * We're emulating the system's hostid in userland, so
2541 * we can't use zone_get_hostid().
2543 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2544 #endif /* _KERNEL */
2545 if (check_hostid && hostid != 0 && myhostid != 0 &&
2546 hostid != myhostid) {
2547 nvlist_free(nvconfig);
2548 cmn_err(CE_WARN, "pool '%s' could not be "
2549 "loaded as it was last accessed by "
2550 "another system (host: %s hostid: 0x%lx). "
2551 "See: http://illumos.org/msg/ZFS-8000-EY",
2552 spa_name(spa), hostname,
2553 (unsigned long)hostid);
2554 return (SET_ERROR(EBADF));
2557 if (nvlist_lookup_nvlist(spa->spa_config,
2558 ZPOOL_REWIND_POLICY, &policy) == 0)
2559 VERIFY(nvlist_add_nvlist(nvconfig,
2560 ZPOOL_REWIND_POLICY, policy) == 0);
2562 spa_config_set(spa, nvconfig);
2564 spa_deactivate(spa);
2565 spa_activate(spa, orig_mode);
2567 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2570 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2571 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2572 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2574 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2577 * Load the bit that tells us to use the new accounting function
2578 * (raid-z deflation). If we have an older pool, this will not
2581 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2582 if (error != 0 && error != ENOENT)
2583 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2585 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2586 &spa->spa_creation_version);
2587 if (error != 0 && error != ENOENT)
2588 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2591 * Load the persistent error log. If we have an older pool, this will
2594 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2595 if (error != 0 && error != ENOENT)
2596 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2598 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2599 &spa->spa_errlog_scrub);
2600 if (error != 0 && error != ENOENT)
2601 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2604 * Load the history object. If we have an older pool, this
2605 * will not be present.
2607 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2608 if (error != 0 && error != ENOENT)
2609 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2612 * If we're assembling the pool from the split-off vdevs of
2613 * an existing pool, we don't want to attach the spares & cache
2618 * Load any hot spares for this pool.
2620 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2621 if (error != 0 && error != ENOENT)
2622 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2623 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2624 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2625 if (load_nvlist(spa, spa->spa_spares.sav_object,
2626 &spa->spa_spares.sav_config) != 0)
2627 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2629 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2630 spa_load_spares(spa);
2631 spa_config_exit(spa, SCL_ALL, FTAG);
2632 } else if (error == 0) {
2633 spa->spa_spares.sav_sync = B_TRUE;
2637 * Load any level 2 ARC devices for this pool.
2639 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2640 &spa->spa_l2cache.sav_object);
2641 if (error != 0 && error != ENOENT)
2642 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2643 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2644 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2645 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2646 &spa->spa_l2cache.sav_config) != 0)
2647 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2649 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2650 spa_load_l2cache(spa);
2651 spa_config_exit(spa, SCL_ALL, FTAG);
2652 } else if (error == 0) {
2653 spa->spa_l2cache.sav_sync = B_TRUE;
2656 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2658 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2659 if (error && error != ENOENT)
2660 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2663 uint64_t autoreplace;
2665 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2666 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2667 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2668 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2669 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2670 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2671 &spa->spa_dedup_ditto);
2673 spa->spa_autoreplace = (autoreplace != 0);
2677 * If the 'autoreplace' property is set, then post a resource notifying
2678 * the ZFS DE that it should not issue any faults for unopenable
2679 * devices. We also iterate over the vdevs, and post a sysevent for any
2680 * unopenable vdevs so that the normal autoreplace handler can take
2683 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2684 spa_check_removed(spa->spa_root_vdev);
2686 * For the import case, this is done in spa_import(), because
2687 * at this point we're using the spare definitions from
2688 * the MOS config, not necessarily from the userland config.
2690 if (state != SPA_LOAD_IMPORT) {
2691 spa_aux_check_removed(&spa->spa_spares);
2692 spa_aux_check_removed(&spa->spa_l2cache);
2697 * Load the vdev state for all toplevel vdevs.
2702 * Propagate the leaf DTLs we just loaded all the way up the tree.
2704 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2705 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2706 spa_config_exit(spa, SCL_ALL, FTAG);
2709 * Load the DDTs (dedup tables).
2711 error = ddt_load(spa);
2713 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2715 spa_update_dspace(spa);
2718 * Validate the config, using the MOS config to fill in any
2719 * information which might be missing. If we fail to validate
2720 * the config then declare the pool unfit for use. If we're
2721 * assembling a pool from a split, the log is not transferred
2724 if (type != SPA_IMPORT_ASSEMBLE) {
2727 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2728 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2730 if (!spa_config_valid(spa, nvconfig)) {
2731 nvlist_free(nvconfig);
2732 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2735 nvlist_free(nvconfig);
2738 * Now that we've validated the config, check the state of the
2739 * root vdev. If it can't be opened, it indicates one or
2740 * more toplevel vdevs are faulted.
2742 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2743 return (SET_ERROR(ENXIO));
2745 if (spa_check_logs(spa)) {
2746 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2747 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2751 if (missing_feat_write) {
2752 ASSERT(state == SPA_LOAD_TRYIMPORT);
2755 * At this point, we know that we can open the pool in
2756 * read-only mode but not read-write mode. We now have enough
2757 * information and can return to userland.
2759 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2763 * We've successfully opened the pool, verify that we're ready
2764 * to start pushing transactions.
2766 if (state != SPA_LOAD_TRYIMPORT) {
2767 if (error = spa_load_verify(spa))
2768 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2772 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2773 spa->spa_load_max_txg == UINT64_MAX)) {
2775 int need_update = B_FALSE;
2777 ASSERT(state != SPA_LOAD_TRYIMPORT);
2780 * Claim log blocks that haven't been committed yet.
2781 * This must all happen in a single txg.
2782 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2783 * invoked from zil_claim_log_block()'s i/o done callback.
2784 * Price of rollback is that we abandon the log.
2786 spa->spa_claiming = B_TRUE;
2788 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2789 spa_first_txg(spa));
2790 (void) dmu_objset_find(spa_name(spa),
2791 zil_claim, tx, DS_FIND_CHILDREN);
2794 spa->spa_claiming = B_FALSE;
2796 spa_set_log_state(spa, SPA_LOG_GOOD);
2797 spa->spa_sync_on = B_TRUE;
2798 txg_sync_start(spa->spa_dsl_pool);
2801 * Wait for all claims to sync. We sync up to the highest
2802 * claimed log block birth time so that claimed log blocks
2803 * don't appear to be from the future. spa_claim_max_txg
2804 * will have been set for us by either zil_check_log_chain()
2805 * (invoked from spa_check_logs()) or zil_claim() above.
2807 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2810 * If the config cache is stale, or we have uninitialized
2811 * metaslabs (see spa_vdev_add()), then update the config.
2813 * If this is a verbatim import, trust the current
2814 * in-core spa_config and update the disk labels.
2816 if (config_cache_txg != spa->spa_config_txg ||
2817 state == SPA_LOAD_IMPORT ||
2818 state == SPA_LOAD_RECOVER ||
2819 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2820 need_update = B_TRUE;
2822 for (int c = 0; c < rvd->vdev_children; c++)
2823 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2824 need_update = B_TRUE;
2827 * Update the config cache asychronously in case we're the
2828 * root pool, in which case the config cache isn't writable yet.
2831 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2834 * Check all DTLs to see if anything needs resilvering.
2836 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2837 vdev_resilver_needed(rvd, NULL, NULL))
2838 spa_async_request(spa, SPA_ASYNC_RESILVER);
2841 * Log the fact that we booted up (so that we can detect if
2842 * we rebooted in the middle of an operation).
2844 spa_history_log_version(spa, "open");
2847 * Delete any inconsistent datasets.
2849 (void) dmu_objset_find(spa_name(spa),
2850 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2853 * Clean up any stale temporary dataset userrefs.
2855 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2862 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2864 int mode = spa->spa_mode;
2867 spa_deactivate(spa);
2869 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg - 1;
2871 spa_activate(spa, mode);
2872 spa_async_suspend(spa);
2874 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2878 * If spa_load() fails this function will try loading prior txg's. If
2879 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2880 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2881 * function will not rewind the pool and will return the same error as
2885 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2886 uint64_t max_request, int rewind_flags)
2888 nvlist_t *loadinfo = NULL;
2889 nvlist_t *config = NULL;
2890 int load_error, rewind_error;
2891 uint64_t safe_rewind_txg;
2894 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2895 spa->spa_load_max_txg = spa->spa_load_txg;
2896 spa_set_log_state(spa, SPA_LOG_CLEAR);
2898 spa->spa_load_max_txg = max_request;
2899 if (max_request != UINT64_MAX)
2900 spa->spa_extreme_rewind = B_TRUE;
2903 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2905 if (load_error == 0)
2908 if (spa->spa_root_vdev != NULL)
2909 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2911 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2912 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2914 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2915 nvlist_free(config);
2916 return (load_error);
2919 if (state == SPA_LOAD_RECOVER) {
2920 /* Price of rolling back is discarding txgs, including log */
2921 spa_set_log_state(spa, SPA_LOG_CLEAR);
2924 * If we aren't rolling back save the load info from our first
2925 * import attempt so that we can restore it after attempting
2928 loadinfo = spa->spa_load_info;
2929 spa->spa_load_info = fnvlist_alloc();
2932 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2933 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2934 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2935 TXG_INITIAL : safe_rewind_txg;
2938 * Continue as long as we're finding errors, we're still within
2939 * the acceptable rewind range, and we're still finding uberblocks
2941 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2942 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2943 if (spa->spa_load_max_txg < safe_rewind_txg)
2944 spa->spa_extreme_rewind = B_TRUE;
2945 rewind_error = spa_load_retry(spa, state, mosconfig);
2948 spa->spa_extreme_rewind = B_FALSE;
2949 spa->spa_load_max_txg = UINT64_MAX;
2951 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2952 spa_config_set(spa, config);
2954 if (state == SPA_LOAD_RECOVER) {
2955 ASSERT3P(loadinfo, ==, NULL);
2956 return (rewind_error);
2958 /* Store the rewind info as part of the initial load info */
2959 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2960 spa->spa_load_info);
2962 /* Restore the initial load info */
2963 fnvlist_free(spa->spa_load_info);
2964 spa->spa_load_info = loadinfo;
2966 return (load_error);
2973 * The import case is identical to an open except that the configuration is sent
2974 * down from userland, instead of grabbed from the configuration cache. For the
2975 * case of an open, the pool configuration will exist in the
2976 * POOL_STATE_UNINITIALIZED state.
2978 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2979 * the same time open the pool, without having to keep around the spa_t in some
2983 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2987 spa_load_state_t state = SPA_LOAD_OPEN;
2989 int locked = B_FALSE;
2990 int firstopen = B_FALSE;
2995 * As disgusting as this is, we need to support recursive calls to this
2996 * function because dsl_dir_open() is called during spa_load(), and ends
2997 * up calling spa_open() again. The real fix is to figure out how to
2998 * avoid dsl_dir_open() calling this in the first place.
3000 if (mutex_owner(&spa_namespace_lock) != curthread) {
3001 mutex_enter(&spa_namespace_lock);
3005 if ((spa = spa_lookup(pool)) == NULL) {
3007 mutex_exit(&spa_namespace_lock);
3008 return (SET_ERROR(ENOENT));
3011 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
3012 zpool_rewind_policy_t policy;
3016 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
3018 if (policy.zrp_request & ZPOOL_DO_REWIND)
3019 state = SPA_LOAD_RECOVER;
3021 spa_activate(spa, spa_mode_global);
3023 if (state != SPA_LOAD_RECOVER)
3024 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3026 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
3027 policy.zrp_request);
3029 if (error == EBADF) {
3031 * If vdev_validate() returns failure (indicated by
3032 * EBADF), it indicates that one of the vdevs indicates
3033 * that the pool has been exported or destroyed. If
3034 * this is the case, the config cache is out of sync and
3035 * we should remove the pool from the namespace.
3038 spa_deactivate(spa);
3039 spa_config_sync(spa, B_TRUE, B_TRUE);
3042 mutex_exit(&spa_namespace_lock);
3043 return (SET_ERROR(ENOENT));
3048 * We can't open the pool, but we still have useful
3049 * information: the state of each vdev after the
3050 * attempted vdev_open(). Return this to the user.
3052 if (config != NULL && spa->spa_config) {
3053 VERIFY(nvlist_dup(spa->spa_config, config,
3055 VERIFY(nvlist_add_nvlist(*config,
3056 ZPOOL_CONFIG_LOAD_INFO,
3057 spa->spa_load_info) == 0);
3060 spa_deactivate(spa);
3061 spa->spa_last_open_failed = error;
3063 mutex_exit(&spa_namespace_lock);
3069 spa_open_ref(spa, tag);
3072 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
3075 * If we've recovered the pool, pass back any information we
3076 * gathered while doing the load.
3078 if (state == SPA_LOAD_RECOVER) {
3079 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
3080 spa->spa_load_info) == 0);
3084 spa->spa_last_open_failed = 0;
3085 spa->spa_last_ubsync_txg = 0;
3086 spa->spa_load_txg = 0;
3087 mutex_exit(&spa_namespace_lock);
3091 zvol_create_minors(spa->spa_name);
3102 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
3105 return (spa_open_common(name, spapp, tag, policy, config));
3109 spa_open(const char *name, spa_t **spapp, void *tag)
3111 return (spa_open_common(name, spapp, tag, NULL, NULL));
3115 * Lookup the given spa_t, incrementing the inject count in the process,
3116 * preventing it from being exported or destroyed.
3119 spa_inject_addref(char *name)
3123 mutex_enter(&spa_namespace_lock);
3124 if ((spa = spa_lookup(name)) == NULL) {
3125 mutex_exit(&spa_namespace_lock);
3128 spa->spa_inject_ref++;
3129 mutex_exit(&spa_namespace_lock);
3135 spa_inject_delref(spa_t *spa)
3137 mutex_enter(&spa_namespace_lock);
3138 spa->spa_inject_ref--;
3139 mutex_exit(&spa_namespace_lock);
3143 * Add spares device information to the nvlist.
3146 spa_add_spares(spa_t *spa, nvlist_t *config)
3156 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3158 if (spa->spa_spares.sav_count == 0)
3161 VERIFY(nvlist_lookup_nvlist(config,
3162 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3163 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3164 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3166 VERIFY(nvlist_add_nvlist_array(nvroot,
3167 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3168 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3169 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3172 * Go through and find any spares which have since been
3173 * repurposed as an active spare. If this is the case, update
3174 * their status appropriately.
3176 for (i = 0; i < nspares; i++) {
3177 VERIFY(nvlist_lookup_uint64(spares[i],
3178 ZPOOL_CONFIG_GUID, &guid) == 0);
3179 if (spa_spare_exists(guid, &pool, NULL) &&
3181 VERIFY(nvlist_lookup_uint64_array(
3182 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3183 (uint64_t **)&vs, &vsc) == 0);
3184 vs->vs_state = VDEV_STATE_CANT_OPEN;
3185 vs->vs_aux = VDEV_AUX_SPARED;
3192 * Add l2cache device information to the nvlist, including vdev stats.
3195 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3198 uint_t i, j, nl2cache;
3205 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3207 if (spa->spa_l2cache.sav_count == 0)
3210 VERIFY(nvlist_lookup_nvlist(config,
3211 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3212 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3213 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3214 if (nl2cache != 0) {
3215 VERIFY(nvlist_add_nvlist_array(nvroot,
3216 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3217 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3218 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3221 * Update level 2 cache device stats.
3224 for (i = 0; i < nl2cache; i++) {
3225 VERIFY(nvlist_lookup_uint64(l2cache[i],
3226 ZPOOL_CONFIG_GUID, &guid) == 0);
3229 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3231 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3232 vd = spa->spa_l2cache.sav_vdevs[j];
3238 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3239 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3241 vdev_get_stats(vd, vs);
3247 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3253 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3254 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3256 /* We may be unable to read features if pool is suspended. */
3257 if (spa_suspended(spa))
3260 if (spa->spa_feat_for_read_obj != 0) {
3261 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3262 spa->spa_feat_for_read_obj);
3263 zap_cursor_retrieve(&zc, &za) == 0;
3264 zap_cursor_advance(&zc)) {
3265 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3266 za.za_num_integers == 1);
3267 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3268 za.za_first_integer));
3270 zap_cursor_fini(&zc);
3273 if (spa->spa_feat_for_write_obj != 0) {
3274 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3275 spa->spa_feat_for_write_obj);
3276 zap_cursor_retrieve(&zc, &za) == 0;
3277 zap_cursor_advance(&zc)) {
3278 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3279 za.za_num_integers == 1);
3280 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3281 za.za_first_integer));
3283 zap_cursor_fini(&zc);
3287 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3289 nvlist_free(features);
3293 spa_get_stats(const char *name, nvlist_t **config,
3294 char *altroot, size_t buflen)
3300 error = spa_open_common(name, &spa, FTAG, NULL, config);
3304 * This still leaves a window of inconsistency where the spares
3305 * or l2cache devices could change and the config would be
3306 * self-inconsistent.
3308 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3310 if (*config != NULL) {
3311 uint64_t loadtimes[2];
3313 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3314 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3315 VERIFY(nvlist_add_uint64_array(*config,
3316 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3318 VERIFY(nvlist_add_uint64(*config,
3319 ZPOOL_CONFIG_ERRCOUNT,
3320 spa_get_errlog_size(spa)) == 0);
3322 if (spa_suspended(spa))
3323 VERIFY(nvlist_add_uint64(*config,
3324 ZPOOL_CONFIG_SUSPENDED,
3325 spa->spa_failmode) == 0);
3327 spa_add_spares(spa, *config);
3328 spa_add_l2cache(spa, *config);
3329 spa_add_feature_stats(spa, *config);
3334 * We want to get the alternate root even for faulted pools, so we cheat
3335 * and call spa_lookup() directly.
3339 mutex_enter(&spa_namespace_lock);
3340 spa = spa_lookup(name);
3342 spa_altroot(spa, altroot, buflen);
3346 mutex_exit(&spa_namespace_lock);
3348 spa_altroot(spa, altroot, buflen);
3353 spa_config_exit(spa, SCL_CONFIG, FTAG);
3354 spa_close(spa, FTAG);
3361 * Validate that the auxiliary device array is well formed. We must have an
3362 * array of nvlists, each which describes a valid leaf vdev. If this is an
3363 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3364 * specified, as long as they are well-formed.
3367 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3368 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3369 vdev_labeltype_t label)
3376 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3379 * It's acceptable to have no devs specified.
3381 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3385 return (SET_ERROR(EINVAL));
3388 * Make sure the pool is formatted with a version that supports this
3391 if (spa_version(spa) < version)
3392 return (SET_ERROR(ENOTSUP));
3395 * Set the pending device list so we correctly handle device in-use
3398 sav->sav_pending = dev;
3399 sav->sav_npending = ndev;
3401 for (i = 0; i < ndev; i++) {
3402 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3406 if (!vd->vdev_ops->vdev_op_leaf) {
3408 error = SET_ERROR(EINVAL);
3413 * The L2ARC currently only supports disk devices in
3414 * kernel context. For user-level testing, we allow it.
3417 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3418 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3419 error = SET_ERROR(ENOTBLK);
3426 if ((error = vdev_open(vd)) == 0 &&
3427 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3428 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3429 vd->vdev_guid) == 0);
3435 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3442 sav->sav_pending = NULL;
3443 sav->sav_npending = 0;
3448 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3452 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3454 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3455 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3456 VDEV_LABEL_SPARE)) != 0) {
3460 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3461 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3462 VDEV_LABEL_L2CACHE));
3466 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3471 if (sav->sav_config != NULL) {
3477 * Generate new dev list by concatentating with the
3480 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3481 &olddevs, &oldndevs) == 0);
3483 newdevs = kmem_alloc(sizeof (void *) *
3484 (ndevs + oldndevs), KM_SLEEP);
3485 for (i = 0; i < oldndevs; i++)
3486 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3488 for (i = 0; i < ndevs; i++)
3489 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3492 VERIFY(nvlist_remove(sav->sav_config, config,
3493 DATA_TYPE_NVLIST_ARRAY) == 0);
3495 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3496 config, newdevs, ndevs + oldndevs) == 0);
3497 for (i = 0; i < oldndevs + ndevs; i++)
3498 nvlist_free(newdevs[i]);
3499 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3502 * Generate a new dev list.
3504 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3506 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3512 * Stop and drop level 2 ARC devices
3515 spa_l2cache_drop(spa_t *spa)
3519 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3521 for (i = 0; i < sav->sav_count; i++) {
3524 vd = sav->sav_vdevs[i];
3527 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3528 pool != 0ULL && l2arc_vdev_present(vd))
3529 l2arc_remove_vdev(vd);
3537 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3541 char *altroot = NULL;
3546 uint64_t txg = TXG_INITIAL;
3547 nvlist_t **spares, **l2cache;
3548 uint_t nspares, nl2cache;
3549 uint64_t version, obj;
3550 boolean_t has_features;
3553 * If this pool already exists, return failure.
3555 mutex_enter(&spa_namespace_lock);
3556 if (spa_lookup(pool) != NULL) {
3557 mutex_exit(&spa_namespace_lock);
3558 return (SET_ERROR(EEXIST));
3562 * Allocate a new spa_t structure.
3564 (void) nvlist_lookup_string(props,
3565 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3566 spa = spa_add(pool, NULL, altroot);
3567 spa_activate(spa, spa_mode_global);
3569 if (props && (error = spa_prop_validate(spa, props))) {
3570 spa_deactivate(spa);
3572 mutex_exit(&spa_namespace_lock);
3576 has_features = B_FALSE;
3577 for (nvpair_t *elem = nvlist_next_nvpair(props, NULL);
3578 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3579 if (zpool_prop_feature(nvpair_name(elem)))
3580 has_features = B_TRUE;
3583 if (has_features || nvlist_lookup_uint64(props,
3584 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3585 version = SPA_VERSION;
3587 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3589 spa->spa_first_txg = txg;
3590 spa->spa_uberblock.ub_txg = txg - 1;
3591 spa->spa_uberblock.ub_version = version;
3592 spa->spa_ubsync = spa->spa_uberblock;
3595 * Create "The Godfather" zio to hold all async IOs
3597 spa->spa_async_zio_root = kmem_alloc(max_ncpus * sizeof (void *),
3599 for (int i = 0; i < max_ncpus; i++) {
3600 spa->spa_async_zio_root[i] = zio_root(spa, NULL, NULL,
3601 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE |
3602 ZIO_FLAG_GODFATHER);
3606 * Create the root vdev.
3608 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3610 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3612 ASSERT(error != 0 || rvd != NULL);
3613 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3615 if (error == 0 && !zfs_allocatable_devs(nvroot))
3616 error = SET_ERROR(EINVAL);
3619 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3620 (error = spa_validate_aux(spa, nvroot, txg,
3621 VDEV_ALLOC_ADD)) == 0) {
3622 for (int c = 0; c < rvd->vdev_children; c++) {
3623 vdev_ashift_optimize(rvd->vdev_child[c]);
3624 vdev_metaslab_set_size(rvd->vdev_child[c]);
3625 vdev_expand(rvd->vdev_child[c], txg);
3629 spa_config_exit(spa, SCL_ALL, FTAG);
3633 spa_deactivate(spa);
3635 mutex_exit(&spa_namespace_lock);
3640 * Get the list of spares, if specified.
3642 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3643 &spares, &nspares) == 0) {
3644 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3646 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3647 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3648 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3649 spa_load_spares(spa);
3650 spa_config_exit(spa, SCL_ALL, FTAG);
3651 spa->spa_spares.sav_sync = B_TRUE;
3655 * Get the list of level 2 cache devices, if specified.
3657 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3658 &l2cache, &nl2cache) == 0) {
3659 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3660 NV_UNIQUE_NAME, KM_SLEEP) == 0);
3661 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3662 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3663 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3664 spa_load_l2cache(spa);
3665 spa_config_exit(spa, SCL_ALL, FTAG);
3666 spa->spa_l2cache.sav_sync = B_TRUE;
3669 spa->spa_is_initializing = B_TRUE;
3670 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3671 spa->spa_meta_objset = dp->dp_meta_objset;
3672 spa->spa_is_initializing = B_FALSE;
3675 * Create DDTs (dedup tables).
3679 spa_update_dspace(spa);
3681 tx = dmu_tx_create_assigned(dp, txg);
3684 * Create the pool config object.
3686 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3687 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3688 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3690 if (zap_add(spa->spa_meta_objset,
3691 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3692 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3693 cmn_err(CE_PANIC, "failed to add pool config");
3696 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3697 spa_feature_create_zap_objects(spa, tx);
3699 if (zap_add(spa->spa_meta_objset,
3700 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3701 sizeof (uint64_t), 1, &version, tx) != 0) {
3702 cmn_err(CE_PANIC, "failed to add pool version");
3705 /* Newly created pools with the right version are always deflated. */
3706 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3707 spa->spa_deflate = TRUE;
3708 if (zap_add(spa->spa_meta_objset,
3709 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3710 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3711 cmn_err(CE_PANIC, "failed to add deflate");
3716 * Create the deferred-free bpobj. Turn off compression
3717 * because sync-to-convergence takes longer if the blocksize
3720 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3721 dmu_object_set_compress(spa->spa_meta_objset, obj,
3722 ZIO_COMPRESS_OFF, tx);
3723 if (zap_add(spa->spa_meta_objset,
3724 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3725 sizeof (uint64_t), 1, &obj, tx) != 0) {
3726 cmn_err(CE_PANIC, "failed to add bpobj");
3728 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3729 spa->spa_meta_objset, obj));
3732 * Create the pool's history object.
3734 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3735 spa_history_create_obj(spa, tx);
3738 * Set pool properties.
3740 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3741 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3742 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3743 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3745 if (props != NULL) {
3746 spa_configfile_set(spa, props, B_FALSE);
3747 spa_sync_props(props, tx);
3752 spa->spa_sync_on = B_TRUE;
3753 txg_sync_start(spa->spa_dsl_pool);
3756 * We explicitly wait for the first transaction to complete so that our
3757 * bean counters are appropriately updated.
3759 txg_wait_synced(spa->spa_dsl_pool, txg);
3761 spa_config_sync(spa, B_FALSE, B_TRUE);
3763 spa_history_log_version(spa, "create");
3765 spa->spa_minref = refcount_count(&spa->spa_refcount);
3767 mutex_exit(&spa_namespace_lock);
3775 * Get the root pool information from the root disk, then import the root pool
3776 * during the system boot up time.
3778 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3781 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3784 nvlist_t *nvtop, *nvroot;
3787 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3791 * Add this top-level vdev to the child array.
3793 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3795 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3797 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3800 * Put this pool's top-level vdevs into a root vdev.
3802 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3803 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3804 VDEV_TYPE_ROOT) == 0);
3805 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3806 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3807 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3811 * Replace the existing vdev_tree with the new root vdev in
3812 * this pool's configuration (remove the old, add the new).
3814 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3815 nvlist_free(nvroot);
3820 * Walk the vdev tree and see if we can find a device with "better"
3821 * configuration. A configuration is "better" if the label on that
3822 * device has a more recent txg.
3825 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3827 for (int c = 0; c < vd->vdev_children; c++)
3828 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3830 if (vd->vdev_ops->vdev_op_leaf) {
3834 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3838 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3842 * Do we have a better boot device?
3844 if (label_txg > *txg) {
3853 * Import a root pool.
3855 * For x86. devpath_list will consist of devid and/or physpath name of
3856 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3857 * The GRUB "findroot" command will return the vdev we should boot.
3859 * For Sparc, devpath_list consists the physpath name of the booting device
3860 * no matter the rootpool is a single device pool or a mirrored pool.
3862 * "/pci@1f,0/ide@d/disk@0,0:a"
3865 spa_import_rootpool(char *devpath, char *devid)
3868 vdev_t *rvd, *bvd, *avd = NULL;
3869 nvlist_t *config, *nvtop;
3875 * Read the label from the boot device and generate a configuration.
3877 config = spa_generate_rootconf(devpath, devid, &guid);
3878 #if defined(_OBP) && defined(_KERNEL)
3879 if (config == NULL) {
3880 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3882 get_iscsi_bootpath_phy(devpath);
3883 config = spa_generate_rootconf(devpath, devid, &guid);
3887 if (config == NULL) {
3888 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3890 return (SET_ERROR(EIO));
3893 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3895 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3897 mutex_enter(&spa_namespace_lock);
3898 if ((spa = spa_lookup(pname)) != NULL) {
3900 * Remove the existing root pool from the namespace so that we
3901 * can replace it with the correct config we just read in.
3906 spa = spa_add(pname, config, NULL);
3907 spa->spa_is_root = B_TRUE;
3908 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3911 * Build up a vdev tree based on the boot device's label config.
3913 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3915 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3916 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3917 VDEV_ALLOC_ROOTPOOL);
3918 spa_config_exit(spa, SCL_ALL, FTAG);
3920 mutex_exit(&spa_namespace_lock);
3921 nvlist_free(config);
3922 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3928 * Get the boot vdev.
3930 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3931 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3932 (u_longlong_t)guid);
3933 error = SET_ERROR(ENOENT);
3938 * Determine if there is a better boot device.
3941 spa_alt_rootvdev(rvd, &avd, &txg);
3943 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3944 "try booting from '%s'", avd->vdev_path);
3945 error = SET_ERROR(EINVAL);
3950 * If the boot device is part of a spare vdev then ensure that
3951 * we're booting off the active spare.
3953 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3954 !bvd->vdev_isspare) {
3955 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3956 "try booting from '%s'",
3958 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3959 error = SET_ERROR(EINVAL);
3965 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3967 spa_config_exit(spa, SCL_ALL, FTAG);
3968 mutex_exit(&spa_namespace_lock);
3970 nvlist_free(config);
3974 #else /* !illumos */
3976 extern int vdev_geom_read_pool_label(const char *name, nvlist_t ***configs,
3980 spa_generate_rootconf(const char *name)
3982 nvlist_t **configs, **tops;
3984 nvlist_t *best_cfg, *nvtop, *nvroot;
3993 if (vdev_geom_read_pool_label(name, &configs, &count) != 0)
3996 ASSERT3U(count, !=, 0);
3998 for (i = 0; i < count; i++) {
4001 VERIFY(nvlist_lookup_uint64(configs[i], ZPOOL_CONFIG_POOL_TXG,
4003 if (txg > best_txg) {
4005 best_cfg = configs[i];
4010 nvlist_lookup_uint64(best_cfg, ZPOOL_CONFIG_VDEV_CHILDREN, &nchildren);
4012 nvlist_lookup_uint64_array(best_cfg, ZPOOL_CONFIG_HOLE_ARRAY,
4015 tops = kmem_zalloc(nchildren * sizeof(void *), KM_SLEEP);
4016 for (i = 0; i < nchildren; i++) {
4019 if (configs[i] == NULL)
4021 VERIFY(nvlist_lookup_nvlist(configs[i], ZPOOL_CONFIG_VDEV_TREE,
4023 nvlist_dup(nvtop, &tops[i], KM_SLEEP);
4025 for (i = 0; holes != NULL && i < nholes; i++) {
4028 if (tops[holes[i]] != NULL)
4030 nvlist_alloc(&tops[holes[i]], NV_UNIQUE_NAME, KM_SLEEP);
4031 VERIFY(nvlist_add_string(tops[holes[i]], ZPOOL_CONFIG_TYPE,
4032 VDEV_TYPE_HOLE) == 0);
4033 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_ID,
4035 VERIFY(nvlist_add_uint64(tops[holes[i]], ZPOOL_CONFIG_GUID,
4038 for (i = 0; i < nchildren; i++) {
4039 if (tops[i] != NULL)
4041 nvlist_alloc(&tops[i], NV_UNIQUE_NAME, KM_SLEEP);
4042 VERIFY(nvlist_add_string(tops[i], ZPOOL_CONFIG_TYPE,
4043 VDEV_TYPE_MISSING) == 0);
4044 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_ID,
4046 VERIFY(nvlist_add_uint64(tops[i], ZPOOL_CONFIG_GUID,
4051 * Create pool config based on the best vdev config.
4053 nvlist_dup(best_cfg, &config, KM_SLEEP);
4056 * Put this pool's top-level vdevs into a root vdev.
4058 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4060 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
4061 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
4062 VDEV_TYPE_ROOT) == 0);
4063 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
4064 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
4065 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4066 tops, nchildren) == 0);
4069 * Replace the existing vdev_tree with the new root vdev in
4070 * this pool's configuration (remove the old, add the new).
4072 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
4075 * Drop vdev config elements that should not be present at pool level.
4077 nvlist_remove(config, ZPOOL_CONFIG_GUID, DATA_TYPE_UINT64);
4078 nvlist_remove(config, ZPOOL_CONFIG_TOP_GUID, DATA_TYPE_UINT64);
4080 for (i = 0; i < count; i++)
4081 nvlist_free(configs[i]);
4082 kmem_free(configs, count * sizeof(void *));
4083 for (i = 0; i < nchildren; i++)
4084 nvlist_free(tops[i]);
4085 kmem_free(tops, nchildren * sizeof(void *));
4086 nvlist_free(nvroot);
4091 spa_import_rootpool(const char *name)
4094 vdev_t *rvd, *bvd, *avd = NULL;
4095 nvlist_t *config, *nvtop;
4101 * Read the label from the boot device and generate a configuration.
4103 config = spa_generate_rootconf(name);
4105 mutex_enter(&spa_namespace_lock);
4106 if (config != NULL) {
4107 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
4108 &pname) == 0 && strcmp(name, pname) == 0);
4109 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg)
4112 if ((spa = spa_lookup(pname)) != NULL) {
4114 * Remove the existing root pool from the namespace so
4115 * that we can replace it with the correct config
4120 spa = spa_add(pname, config, NULL);
4123 * Set spa_ubsync.ub_version as it can be used in vdev_alloc()
4124 * via spa_version().
4126 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
4127 &spa->spa_ubsync.ub_version) != 0)
4128 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
4129 } else if ((spa = spa_lookup(name)) == NULL) {
4130 cmn_err(CE_NOTE, "Cannot find the pool label for '%s'",
4134 VERIFY(nvlist_dup(spa->spa_config, &config, KM_SLEEP) == 0);
4136 spa->spa_is_root = B_TRUE;
4137 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
4140 * Build up a vdev tree based on the boot device's label config.
4142 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4144 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4145 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
4146 VDEV_ALLOC_ROOTPOOL);
4147 spa_config_exit(spa, SCL_ALL, FTAG);
4149 mutex_exit(&spa_namespace_lock);
4150 nvlist_free(config);
4151 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
4156 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4158 spa_config_exit(spa, SCL_ALL, FTAG);
4159 mutex_exit(&spa_namespace_lock);
4161 nvlist_free(config);
4165 #endif /* illumos */
4166 #endif /* _KERNEL */
4169 * Import a non-root pool into the system.
4172 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
4175 char *altroot = NULL;
4176 spa_load_state_t state = SPA_LOAD_IMPORT;
4177 zpool_rewind_policy_t policy;
4178 uint64_t mode = spa_mode_global;
4179 uint64_t readonly = B_FALSE;
4182 nvlist_t **spares, **l2cache;
4183 uint_t nspares, nl2cache;
4186 * If a pool with this name exists, return failure.
4188 mutex_enter(&spa_namespace_lock);
4189 if (spa_lookup(pool) != NULL) {
4190 mutex_exit(&spa_namespace_lock);
4191 return (SET_ERROR(EEXIST));
4195 * Create and initialize the spa structure.
4197 (void) nvlist_lookup_string(props,
4198 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4199 (void) nvlist_lookup_uint64(props,
4200 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
4203 spa = spa_add(pool, config, altroot);
4204 spa->spa_import_flags = flags;
4207 * Verbatim import - Take a pool and insert it into the namespace
4208 * as if it had been loaded at boot.
4210 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
4212 spa_configfile_set(spa, props, B_FALSE);
4214 spa_config_sync(spa, B_FALSE, B_TRUE);
4216 mutex_exit(&spa_namespace_lock);
4220 spa_activate(spa, mode);
4223 * Don't start async tasks until we know everything is healthy.
4225 spa_async_suspend(spa);
4227 zpool_get_rewind_policy(config, &policy);
4228 if (policy.zrp_request & ZPOOL_DO_REWIND)
4229 state = SPA_LOAD_RECOVER;
4232 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4233 * because the user-supplied config is actually the one to trust when
4236 if (state != SPA_LOAD_RECOVER)
4237 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
4239 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
4240 policy.zrp_request);
4243 * Propagate anything learned while loading the pool and pass it
4244 * back to caller (i.e. rewind info, missing devices, etc).
4246 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4247 spa->spa_load_info) == 0);
4249 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4251 * Toss any existing sparelist, as it doesn't have any validity
4252 * anymore, and conflicts with spa_has_spare().
4254 if (spa->spa_spares.sav_config) {
4255 nvlist_free(spa->spa_spares.sav_config);
4256 spa->spa_spares.sav_config = NULL;
4257 spa_load_spares(spa);
4259 if (spa->spa_l2cache.sav_config) {
4260 nvlist_free(spa->spa_l2cache.sav_config);
4261 spa->spa_l2cache.sav_config = NULL;
4262 spa_load_l2cache(spa);
4265 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4268 error = spa_validate_aux(spa, nvroot, -1ULL,
4271 error = spa_validate_aux(spa, nvroot, -1ULL,
4272 VDEV_ALLOC_L2CACHE);
4273 spa_config_exit(spa, SCL_ALL, FTAG);
4276 spa_configfile_set(spa, props, B_FALSE);
4278 if (error != 0 || (props && spa_writeable(spa) &&
4279 (error = spa_prop_set(spa, props)))) {
4281 spa_deactivate(spa);
4283 mutex_exit(&spa_namespace_lock);
4287 spa_async_resume(spa);
4290 * Override any spares and level 2 cache devices as specified by
4291 * the user, as these may have correct device names/devids, etc.
4293 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
4294 &spares, &nspares) == 0) {
4295 if (spa->spa_spares.sav_config)
4296 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
4297 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
4299 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
4300 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4301 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
4302 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
4303 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4304 spa_load_spares(spa);
4305 spa_config_exit(spa, SCL_ALL, FTAG);
4306 spa->spa_spares.sav_sync = B_TRUE;
4308 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
4309 &l2cache, &nl2cache) == 0) {
4310 if (spa->spa_l2cache.sav_config)
4311 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
4312 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
4314 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
4315 NV_UNIQUE_NAME, KM_SLEEP) == 0);
4316 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
4317 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
4318 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4319 spa_load_l2cache(spa);
4320 spa_config_exit(spa, SCL_ALL, FTAG);
4321 spa->spa_l2cache.sav_sync = B_TRUE;
4325 * Check for any removed devices.
4327 if (spa->spa_autoreplace) {
4328 spa_aux_check_removed(&spa->spa_spares);
4329 spa_aux_check_removed(&spa->spa_l2cache);
4332 if (spa_writeable(spa)) {
4334 * Update the config cache to include the newly-imported pool.
4336 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4340 * It's possible that the pool was expanded while it was exported.
4341 * We kick off an async task to handle this for us.
4343 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
4345 mutex_exit(&spa_namespace_lock);
4346 spa_history_log_version(spa, "import");
4350 zvol_create_minors(pool);
4357 spa_tryimport(nvlist_t *tryconfig)
4359 nvlist_t *config = NULL;
4365 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4368 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4372 * Create and initialize the spa structure.
4374 mutex_enter(&spa_namespace_lock);
4375 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4376 spa_activate(spa, FREAD);
4379 * Pass off the heavy lifting to spa_load().
4380 * Pass TRUE for mosconfig because the user-supplied config
4381 * is actually the one to trust when doing an import.
4383 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4386 * If 'tryconfig' was at least parsable, return the current config.
4388 if (spa->spa_root_vdev != NULL) {
4389 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4390 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4392 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4394 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4395 spa->spa_uberblock.ub_timestamp) == 0);
4396 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4397 spa->spa_load_info) == 0);
4400 * If the bootfs property exists on this pool then we
4401 * copy it out so that external consumers can tell which
4402 * pools are bootable.
4404 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4405 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4408 * We have to play games with the name since the
4409 * pool was opened as TRYIMPORT_NAME.
4411 if (dsl_dsobj_to_dsname(spa_name(spa),
4412 spa->spa_bootfs, tmpname) == 0) {
4414 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4416 cp = strchr(tmpname, '/');
4418 (void) strlcpy(dsname, tmpname,
4421 (void) snprintf(dsname, MAXPATHLEN,
4422 "%s/%s", poolname, ++cp);
4424 VERIFY(nvlist_add_string(config,
4425 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4426 kmem_free(dsname, MAXPATHLEN);
4428 kmem_free(tmpname, MAXPATHLEN);
4432 * Add the list of hot spares and level 2 cache devices.
4434 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4435 spa_add_spares(spa, config);
4436 spa_add_l2cache(spa, config);
4437 spa_config_exit(spa, SCL_CONFIG, FTAG);
4441 spa_deactivate(spa);
4443 mutex_exit(&spa_namespace_lock);
4449 * Pool export/destroy
4451 * The act of destroying or exporting a pool is very simple. We make sure there
4452 * is no more pending I/O and any references to the pool are gone. Then, we
4453 * update the pool state and sync all the labels to disk, removing the
4454 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4455 * we don't sync the labels or remove the configuration cache.
4458 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4459 boolean_t force, boolean_t hardforce)
4466 if (!(spa_mode_global & FWRITE))
4467 return (SET_ERROR(EROFS));
4469 mutex_enter(&spa_namespace_lock);
4470 if ((spa = spa_lookup(pool)) == NULL) {
4471 mutex_exit(&spa_namespace_lock);
4472 return (SET_ERROR(ENOENT));
4476 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4477 * reacquire the namespace lock, and see if we can export.
4479 spa_open_ref(spa, FTAG);
4480 mutex_exit(&spa_namespace_lock);
4481 spa_async_suspend(spa);
4482 mutex_enter(&spa_namespace_lock);
4483 spa_close(spa, FTAG);
4486 * The pool will be in core if it's openable,
4487 * in which case we can modify its state.
4489 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4491 * Objsets may be open only because they're dirty, so we
4492 * have to force it to sync before checking spa_refcnt.
4494 txg_wait_synced(spa->spa_dsl_pool, 0);
4497 * A pool cannot be exported or destroyed if there are active
4498 * references. If we are resetting a pool, allow references by
4499 * fault injection handlers.
4501 if (!spa_refcount_zero(spa) ||
4502 (spa->spa_inject_ref != 0 &&
4503 new_state != POOL_STATE_UNINITIALIZED)) {
4504 spa_async_resume(spa);
4505 mutex_exit(&spa_namespace_lock);
4506 return (SET_ERROR(EBUSY));
4510 * A pool cannot be exported if it has an active shared spare.
4511 * This is to prevent other pools stealing the active spare
4512 * from an exported pool. At user's own will, such pool can
4513 * be forcedly exported.
4515 if (!force && new_state == POOL_STATE_EXPORTED &&
4516 spa_has_active_shared_spare(spa)) {
4517 spa_async_resume(spa);
4518 mutex_exit(&spa_namespace_lock);
4519 return (SET_ERROR(EXDEV));
4523 * We want this to be reflected on every label,
4524 * so mark them all dirty. spa_unload() will do the
4525 * final sync that pushes these changes out.
4527 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4528 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4529 spa->spa_state = new_state;
4530 spa->spa_final_txg = spa_last_synced_txg(spa) +
4532 vdev_config_dirty(spa->spa_root_vdev);
4533 spa_config_exit(spa, SCL_ALL, FTAG);
4537 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
4539 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4541 spa_deactivate(spa);
4544 if (oldconfig && spa->spa_config)
4545 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4547 if (new_state != POOL_STATE_UNINITIALIZED) {
4549 spa_config_sync(spa, B_TRUE, B_TRUE);
4552 mutex_exit(&spa_namespace_lock);
4558 * Destroy a storage pool.
4561 spa_destroy(char *pool)
4563 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4568 * Export a storage pool.
4571 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4572 boolean_t hardforce)
4574 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4579 * Similar to spa_export(), this unloads the spa_t without actually removing it
4580 * from the namespace in any way.
4583 spa_reset(char *pool)
4585 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4590 * ==========================================================================
4591 * Device manipulation
4592 * ==========================================================================
4596 * Add a device to a storage pool.
4599 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4603 vdev_t *rvd = spa->spa_root_vdev;
4605 nvlist_t **spares, **l2cache;
4606 uint_t nspares, nl2cache;
4608 ASSERT(spa_writeable(spa));
4610 txg = spa_vdev_enter(spa);
4612 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4613 VDEV_ALLOC_ADD)) != 0)
4614 return (spa_vdev_exit(spa, NULL, txg, error));
4616 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4618 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4622 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4626 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4627 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4629 if (vd->vdev_children != 0 &&
4630 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4631 return (spa_vdev_exit(spa, vd, txg, error));
4634 * We must validate the spares and l2cache devices after checking the
4635 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4637 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4638 return (spa_vdev_exit(spa, vd, txg, error));
4641 * Transfer each new top-level vdev from vd to rvd.
4643 for (int c = 0; c < vd->vdev_children; c++) {
4646 * Set the vdev id to the first hole, if one exists.
4648 for (id = 0; id < rvd->vdev_children; id++) {
4649 if (rvd->vdev_child[id]->vdev_ishole) {
4650 vdev_free(rvd->vdev_child[id]);
4654 tvd = vd->vdev_child[c];
4655 vdev_remove_child(vd, tvd);
4657 vdev_add_child(rvd, tvd);
4658 vdev_config_dirty(tvd);
4662 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4663 ZPOOL_CONFIG_SPARES);
4664 spa_load_spares(spa);
4665 spa->spa_spares.sav_sync = B_TRUE;
4668 if (nl2cache != 0) {
4669 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4670 ZPOOL_CONFIG_L2CACHE);
4671 spa_load_l2cache(spa);
4672 spa->spa_l2cache.sav_sync = B_TRUE;
4676 * We have to be careful when adding new vdevs to an existing pool.
4677 * If other threads start allocating from these vdevs before we
4678 * sync the config cache, and we lose power, then upon reboot we may
4679 * fail to open the pool because there are DVAs that the config cache
4680 * can't translate. Therefore, we first add the vdevs without
4681 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4682 * and then let spa_config_update() initialize the new metaslabs.
4684 * spa_load() checks for added-but-not-initialized vdevs, so that
4685 * if we lose power at any point in this sequence, the remaining
4686 * steps will be completed the next time we load the pool.
4688 (void) spa_vdev_exit(spa, vd, txg, 0);
4690 mutex_enter(&spa_namespace_lock);
4691 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4692 mutex_exit(&spa_namespace_lock);
4698 * Attach a device to a mirror. The arguments are the path to any device
4699 * in the mirror, and the nvroot for the new device. If the path specifies
4700 * a device that is not mirrored, we automatically insert the mirror vdev.
4702 * If 'replacing' is specified, the new device is intended to replace the
4703 * existing device; in this case the two devices are made into their own
4704 * mirror using the 'replacing' vdev, which is functionally identical to
4705 * the mirror vdev (it actually reuses all the same ops) but has a few
4706 * extra rules: you can't attach to it after it's been created, and upon
4707 * completion of resilvering, the first disk (the one being replaced)
4708 * is automatically detached.
4711 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4713 uint64_t txg, dtl_max_txg;
4714 vdev_t *rvd = spa->spa_root_vdev;
4715 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4717 char *oldvdpath, *newvdpath;
4721 ASSERT(spa_writeable(spa));
4723 txg = spa_vdev_enter(spa);
4725 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4728 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4730 if (!oldvd->vdev_ops->vdev_op_leaf)
4731 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4733 pvd = oldvd->vdev_parent;
4735 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4736 VDEV_ALLOC_ATTACH)) != 0)
4737 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4739 if (newrootvd->vdev_children != 1)
4740 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4742 newvd = newrootvd->vdev_child[0];
4744 if (!newvd->vdev_ops->vdev_op_leaf)
4745 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4747 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4748 return (spa_vdev_exit(spa, newrootvd, txg, error));
4751 * Spares can't replace logs
4753 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4754 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4758 * For attach, the only allowable parent is a mirror or the root
4761 if (pvd->vdev_ops != &vdev_mirror_ops &&
4762 pvd->vdev_ops != &vdev_root_ops)
4763 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4765 pvops = &vdev_mirror_ops;
4768 * Active hot spares can only be replaced by inactive hot
4771 if (pvd->vdev_ops == &vdev_spare_ops &&
4772 oldvd->vdev_isspare &&
4773 !spa_has_spare(spa, newvd->vdev_guid))
4774 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4777 * If the source is a hot spare, and the parent isn't already a
4778 * spare, then we want to create a new hot spare. Otherwise, we
4779 * want to create a replacing vdev. The user is not allowed to
4780 * attach to a spared vdev child unless the 'isspare' state is
4781 * the same (spare replaces spare, non-spare replaces
4784 if (pvd->vdev_ops == &vdev_replacing_ops &&
4785 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4786 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4787 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4788 newvd->vdev_isspare != oldvd->vdev_isspare) {
4789 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4792 if (newvd->vdev_isspare)
4793 pvops = &vdev_spare_ops;
4795 pvops = &vdev_replacing_ops;
4799 * Make sure the new device is big enough.
4801 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4802 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4805 * The new device cannot have a higher alignment requirement
4806 * than the top-level vdev.
4808 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4809 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4812 * If this is an in-place replacement, update oldvd's path and devid
4813 * to make it distinguishable from newvd, and unopenable from now on.
4815 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4816 spa_strfree(oldvd->vdev_path);
4817 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4819 (void) sprintf(oldvd->vdev_path, "%s/%s",
4820 newvd->vdev_path, "old");
4821 if (oldvd->vdev_devid != NULL) {
4822 spa_strfree(oldvd->vdev_devid);
4823 oldvd->vdev_devid = NULL;
4827 /* mark the device being resilvered */
4828 newvd->vdev_resilver_txg = txg;
4831 * If the parent is not a mirror, or if we're replacing, insert the new
4832 * mirror/replacing/spare vdev above oldvd.
4834 if (pvd->vdev_ops != pvops)
4835 pvd = vdev_add_parent(oldvd, pvops);
4837 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4838 ASSERT(pvd->vdev_ops == pvops);
4839 ASSERT(oldvd->vdev_parent == pvd);
4842 * Extract the new device from its root and add it to pvd.
4844 vdev_remove_child(newrootvd, newvd);
4845 newvd->vdev_id = pvd->vdev_children;
4846 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4847 vdev_add_child(pvd, newvd);
4849 tvd = newvd->vdev_top;
4850 ASSERT(pvd->vdev_top == tvd);
4851 ASSERT(tvd->vdev_parent == rvd);
4853 vdev_config_dirty(tvd);
4856 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4857 * for any dmu_sync-ed blocks. It will propagate upward when
4858 * spa_vdev_exit() calls vdev_dtl_reassess().
4860 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4862 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4863 dtl_max_txg - TXG_INITIAL);
4865 if (newvd->vdev_isspare) {
4866 spa_spare_activate(newvd);
4867 spa_event_notify(spa, newvd, ESC_ZFS_VDEV_SPARE);
4870 oldvdpath = spa_strdup(oldvd->vdev_path);
4871 newvdpath = spa_strdup(newvd->vdev_path);
4872 newvd_isspare = newvd->vdev_isspare;
4875 * Mark newvd's DTL dirty in this txg.
4877 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4880 * Schedule the resilver to restart in the future. We do this to
4881 * ensure that dmu_sync-ed blocks have been stitched into the
4882 * respective datasets.
4884 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4889 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4891 spa_history_log_internal(spa, "vdev attach", NULL,
4892 "%s vdev=%s %s vdev=%s",
4893 replacing && newvd_isspare ? "spare in" :
4894 replacing ? "replace" : "attach", newvdpath,
4895 replacing ? "for" : "to", oldvdpath);
4897 spa_strfree(oldvdpath);
4898 spa_strfree(newvdpath);
4900 if (spa->spa_bootfs)
4901 spa_event_notify(spa, newvd, ESC_ZFS_BOOTFS_VDEV_ATTACH);
4907 * Detach a device from a mirror or replacing vdev.
4909 * If 'replace_done' is specified, only detach if the parent
4910 * is a replacing vdev.
4913 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4917 vdev_t *rvd = spa->spa_root_vdev;
4918 vdev_t *vd, *pvd, *cvd, *tvd;
4919 boolean_t unspare = B_FALSE;
4920 uint64_t unspare_guid = 0;
4923 ASSERT(spa_writeable(spa));
4925 txg = spa_vdev_enter(spa);
4927 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4930 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4932 if (!vd->vdev_ops->vdev_op_leaf)
4933 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4935 pvd = vd->vdev_parent;
4938 * If the parent/child relationship is not as expected, don't do it.
4939 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4940 * vdev that's replacing B with C. The user's intent in replacing
4941 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4942 * the replace by detaching C, the expected behavior is to end up
4943 * M(A,B). But suppose that right after deciding to detach C,
4944 * the replacement of B completes. We would have M(A,C), and then
4945 * ask to detach C, which would leave us with just A -- not what
4946 * the user wanted. To prevent this, we make sure that the
4947 * parent/child relationship hasn't changed -- in this example,
4948 * that C's parent is still the replacing vdev R.
4950 if (pvd->vdev_guid != pguid && pguid != 0)
4951 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4954 * Only 'replacing' or 'spare' vdevs can be replaced.
4956 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4957 pvd->vdev_ops != &vdev_spare_ops)
4958 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4960 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4961 spa_version(spa) >= SPA_VERSION_SPARES);
4964 * Only mirror, replacing, and spare vdevs support detach.
4966 if (pvd->vdev_ops != &vdev_replacing_ops &&
4967 pvd->vdev_ops != &vdev_mirror_ops &&
4968 pvd->vdev_ops != &vdev_spare_ops)
4969 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4972 * If this device has the only valid copy of some data,
4973 * we cannot safely detach it.
4975 if (vdev_dtl_required(vd))
4976 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4978 ASSERT(pvd->vdev_children >= 2);
4981 * If we are detaching the second disk from a replacing vdev, then
4982 * check to see if we changed the original vdev's path to have "/old"
4983 * at the end in spa_vdev_attach(). If so, undo that change now.
4985 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4986 vd->vdev_path != NULL) {
4987 size_t len = strlen(vd->vdev_path);
4989 for (int c = 0; c < pvd->vdev_children; c++) {
4990 cvd = pvd->vdev_child[c];
4992 if (cvd == vd || cvd->vdev_path == NULL)
4995 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4996 strcmp(cvd->vdev_path + len, "/old") == 0) {
4997 spa_strfree(cvd->vdev_path);
4998 cvd->vdev_path = spa_strdup(vd->vdev_path);
5005 * If we are detaching the original disk from a spare, then it implies
5006 * that the spare should become a real disk, and be removed from the
5007 * active spare list for the pool.
5009 if (pvd->vdev_ops == &vdev_spare_ops &&
5011 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
5015 * Erase the disk labels so the disk can be used for other things.
5016 * This must be done after all other error cases are handled,
5017 * but before we disembowel vd (so we can still do I/O to it).
5018 * But if we can't do it, don't treat the error as fatal --
5019 * it may be that the unwritability of the disk is the reason
5020 * it's being detached!
5022 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5025 * Remove vd from its parent and compact the parent's children.
5027 vdev_remove_child(pvd, vd);
5028 vdev_compact_children(pvd);
5031 * Remember one of the remaining children so we can get tvd below.
5033 cvd = pvd->vdev_child[pvd->vdev_children - 1];
5036 * If we need to remove the remaining child from the list of hot spares,
5037 * do it now, marking the vdev as no longer a spare in the process.
5038 * We must do this before vdev_remove_parent(), because that can
5039 * change the GUID if it creates a new toplevel GUID. For a similar
5040 * reason, we must remove the spare now, in the same txg as the detach;
5041 * otherwise someone could attach a new sibling, change the GUID, and
5042 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5045 ASSERT(cvd->vdev_isspare);
5046 spa_spare_remove(cvd);
5047 unspare_guid = cvd->vdev_guid;
5048 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
5049 cvd->vdev_unspare = B_TRUE;
5053 * If the parent mirror/replacing vdev only has one child,
5054 * the parent is no longer needed. Remove it from the tree.
5056 if (pvd->vdev_children == 1) {
5057 if (pvd->vdev_ops == &vdev_spare_ops)
5058 cvd->vdev_unspare = B_FALSE;
5059 vdev_remove_parent(cvd);
5064 * We don't set tvd until now because the parent we just removed
5065 * may have been the previous top-level vdev.
5067 tvd = cvd->vdev_top;
5068 ASSERT(tvd->vdev_parent == rvd);
5071 * Reevaluate the parent vdev state.
5073 vdev_propagate_state(cvd);
5076 * If the 'autoexpand' property is set on the pool then automatically
5077 * try to expand the size of the pool. For example if the device we
5078 * just detached was smaller than the others, it may be possible to
5079 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5080 * first so that we can obtain the updated sizes of the leaf vdevs.
5082 if (spa->spa_autoexpand) {
5084 vdev_expand(tvd, txg);
5087 vdev_config_dirty(tvd);
5090 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5091 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5092 * But first make sure we're not on any *other* txg's DTL list, to
5093 * prevent vd from being accessed after it's freed.
5095 vdpath = spa_strdup(vd->vdev_path);
5096 for (int t = 0; t < TXG_SIZE; t++)
5097 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
5098 vd->vdev_detached = B_TRUE;
5099 vdev_dirty(tvd, VDD_DTL, vd, txg);
5101 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
5103 /* hang on to the spa before we release the lock */
5104 spa_open_ref(spa, FTAG);
5106 error = spa_vdev_exit(spa, vd, txg, 0);
5108 spa_history_log_internal(spa, "detach", NULL,
5110 spa_strfree(vdpath);
5113 * If this was the removal of the original device in a hot spare vdev,
5114 * then we want to go through and remove the device from the hot spare
5115 * list of every other pool.
5118 spa_t *altspa = NULL;
5120 mutex_enter(&spa_namespace_lock);
5121 while ((altspa = spa_next(altspa)) != NULL) {
5122 if (altspa->spa_state != POOL_STATE_ACTIVE ||
5126 spa_open_ref(altspa, FTAG);
5127 mutex_exit(&spa_namespace_lock);
5128 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
5129 mutex_enter(&spa_namespace_lock);
5130 spa_close(altspa, FTAG);
5132 mutex_exit(&spa_namespace_lock);
5134 /* search the rest of the vdevs for spares to remove */
5135 spa_vdev_resilver_done(spa);
5138 /* all done with the spa; OK to release */
5139 mutex_enter(&spa_namespace_lock);
5140 spa_close(spa, FTAG);
5141 mutex_exit(&spa_namespace_lock);
5147 * Split a set of devices from their mirrors, and create a new pool from them.
5150 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
5151 nvlist_t *props, boolean_t exp)
5154 uint64_t txg, *glist;
5156 uint_t c, children, lastlog;
5157 nvlist_t **child, *nvl, *tmp;
5159 char *altroot = NULL;
5160 vdev_t *rvd, **vml = NULL; /* vdev modify list */
5161 boolean_t activate_slog;
5163 ASSERT(spa_writeable(spa));
5165 txg = spa_vdev_enter(spa);
5167 /* clear the log and flush everything up to now */
5168 activate_slog = spa_passivate_log(spa);
5169 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5170 error = spa_offline_log(spa);
5171 txg = spa_vdev_config_enter(spa);
5174 spa_activate_log(spa);
5177 return (spa_vdev_exit(spa, NULL, txg, error));
5179 /* check new spa name before going any further */
5180 if (spa_lookup(newname) != NULL)
5181 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
5184 * scan through all the children to ensure they're all mirrors
5186 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
5187 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
5189 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5191 /* first, check to ensure we've got the right child count */
5192 rvd = spa->spa_root_vdev;
5194 for (c = 0; c < rvd->vdev_children; c++) {
5195 vdev_t *vd = rvd->vdev_child[c];
5197 /* don't count the holes & logs as children */
5198 if (vd->vdev_islog || vd->vdev_ishole) {
5206 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
5207 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5209 /* next, ensure no spare or cache devices are part of the split */
5210 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
5211 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
5212 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
5214 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_SLEEP);
5215 glist = kmem_zalloc(children * sizeof (uint64_t), KM_SLEEP);
5217 /* then, loop over each vdev and validate it */
5218 for (c = 0; c < children; c++) {
5219 uint64_t is_hole = 0;
5221 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
5225 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
5226 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
5229 error = SET_ERROR(EINVAL);
5234 /* which disk is going to be split? */
5235 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
5237 error = SET_ERROR(EINVAL);
5241 /* look it up in the spa */
5242 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
5243 if (vml[c] == NULL) {
5244 error = SET_ERROR(ENODEV);
5248 /* make sure there's nothing stopping the split */
5249 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
5250 vml[c]->vdev_islog ||
5251 vml[c]->vdev_ishole ||
5252 vml[c]->vdev_isspare ||
5253 vml[c]->vdev_isl2cache ||
5254 !vdev_writeable(vml[c]) ||
5255 vml[c]->vdev_children != 0 ||
5256 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
5257 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
5258 error = SET_ERROR(EINVAL);
5262 if (vdev_dtl_required(vml[c])) {
5263 error = SET_ERROR(EBUSY);
5267 /* we need certain info from the top level */
5268 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
5269 vml[c]->vdev_top->vdev_ms_array) == 0);
5270 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
5271 vml[c]->vdev_top->vdev_ms_shift) == 0);
5272 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
5273 vml[c]->vdev_top->vdev_asize) == 0);
5274 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
5275 vml[c]->vdev_top->vdev_ashift) == 0);
5279 kmem_free(vml, children * sizeof (vdev_t *));
5280 kmem_free(glist, children * sizeof (uint64_t));
5281 return (spa_vdev_exit(spa, NULL, txg, error));
5284 /* stop writers from using the disks */
5285 for (c = 0; c < children; c++) {
5287 vml[c]->vdev_offline = B_TRUE;
5289 vdev_reopen(spa->spa_root_vdev);
5292 * Temporarily record the splitting vdevs in the spa config. This
5293 * will disappear once the config is regenerated.
5295 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5296 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
5297 glist, children) == 0);
5298 kmem_free(glist, children * sizeof (uint64_t));
5300 mutex_enter(&spa->spa_props_lock);
5301 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
5303 mutex_exit(&spa->spa_props_lock);
5304 spa->spa_config_splitting = nvl;
5305 vdev_config_dirty(spa->spa_root_vdev);
5307 /* configure and create the new pool */
5308 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
5309 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
5310 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
5311 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5312 spa_version(spa)) == 0);
5313 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
5314 spa->spa_config_txg) == 0);
5315 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
5316 spa_generate_guid(NULL)) == 0);
5317 (void) nvlist_lookup_string(props,
5318 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
5320 /* add the new pool to the namespace */
5321 newspa = spa_add(newname, config, altroot);
5322 newspa->spa_config_txg = spa->spa_config_txg;
5323 spa_set_log_state(newspa, SPA_LOG_CLEAR);
5325 /* release the spa config lock, retaining the namespace lock */
5326 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5328 if (zio_injection_enabled)
5329 zio_handle_panic_injection(spa, FTAG, 1);
5331 spa_activate(newspa, spa_mode_global);
5332 spa_async_suspend(newspa);
5335 /* mark that we are creating new spa by splitting */
5336 newspa->spa_splitting_newspa = B_TRUE;
5338 /* create the new pool from the disks of the original pool */
5339 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
5341 newspa->spa_splitting_newspa = B_FALSE;
5346 /* if that worked, generate a real config for the new pool */
5347 if (newspa->spa_root_vdev != NULL) {
5348 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
5349 NV_UNIQUE_NAME, KM_SLEEP) == 0);
5350 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
5351 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
5352 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
5357 if (props != NULL) {
5358 spa_configfile_set(newspa, props, B_FALSE);
5359 error = spa_prop_set(newspa, props);
5364 /* flush everything */
5365 txg = spa_vdev_config_enter(newspa);
5366 vdev_config_dirty(newspa->spa_root_vdev);
5367 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
5369 if (zio_injection_enabled)
5370 zio_handle_panic_injection(spa, FTAG, 2);
5372 spa_async_resume(newspa);
5374 /* finally, update the original pool's config */
5375 txg = spa_vdev_config_enter(spa);
5376 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5377 error = dmu_tx_assign(tx, TXG_WAIT);
5380 for (c = 0; c < children; c++) {
5381 if (vml[c] != NULL) {
5384 spa_history_log_internal(spa, "detach", tx,
5385 "vdev=%s", vml[c]->vdev_path);
5389 vdev_config_dirty(spa->spa_root_vdev);
5390 spa->spa_config_splitting = NULL;
5394 (void) spa_vdev_exit(spa, NULL, txg, 0);
5396 if (zio_injection_enabled)
5397 zio_handle_panic_injection(spa, FTAG, 3);
5399 /* split is complete; log a history record */
5400 spa_history_log_internal(newspa, "split", NULL,
5401 "from pool %s", spa_name(spa));
5403 kmem_free(vml, children * sizeof (vdev_t *));
5405 /* if we're not going to mount the filesystems in userland, export */
5407 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5414 spa_deactivate(newspa);
5417 txg = spa_vdev_config_enter(spa);
5419 /* re-online all offlined disks */
5420 for (c = 0; c < children; c++) {
5422 vml[c]->vdev_offline = B_FALSE;
5424 vdev_reopen(spa->spa_root_vdev);
5426 nvlist_free(spa->spa_config_splitting);
5427 spa->spa_config_splitting = NULL;
5428 (void) spa_vdev_exit(spa, NULL, txg, error);
5430 kmem_free(vml, children * sizeof (vdev_t *));
5435 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5437 for (int i = 0; i < count; i++) {
5440 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5443 if (guid == target_guid)
5451 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5452 nvlist_t *dev_to_remove)
5454 nvlist_t **newdev = NULL;
5457 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
5459 for (int i = 0, j = 0; i < count; i++) {
5460 if (dev[i] == dev_to_remove)
5462 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
5465 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5466 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5468 for (int i = 0; i < count - 1; i++)
5469 nvlist_free(newdev[i]);
5472 kmem_free(newdev, (count - 1) * sizeof (void *));
5476 * Evacuate the device.
5479 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5484 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5485 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5486 ASSERT(vd == vd->vdev_top);
5489 * Evacuate the device. We don't hold the config lock as writer
5490 * since we need to do I/O but we do keep the
5491 * spa_namespace_lock held. Once this completes the device
5492 * should no longer have any blocks allocated on it.
5494 if (vd->vdev_islog) {
5495 if (vd->vdev_stat.vs_alloc != 0)
5496 error = spa_offline_log(spa);
5498 error = SET_ERROR(ENOTSUP);
5505 * The evacuation succeeded. Remove any remaining MOS metadata
5506 * associated with this vdev, and wait for these changes to sync.
5508 ASSERT0(vd->vdev_stat.vs_alloc);
5509 txg = spa_vdev_config_enter(spa);
5510 vd->vdev_removing = B_TRUE;
5511 vdev_dirty_leaves(vd, VDD_DTL, txg);
5512 vdev_config_dirty(vd);
5513 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5519 * Complete the removal by cleaning up the namespace.
5522 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5524 vdev_t *rvd = spa->spa_root_vdev;
5525 uint64_t id = vd->vdev_id;
5526 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5528 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5529 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5530 ASSERT(vd == vd->vdev_top);
5533 * Only remove any devices which are empty.
5535 if (vd->vdev_stat.vs_alloc != 0)
5538 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5540 if (list_link_active(&vd->vdev_state_dirty_node))
5541 vdev_state_clean(vd);
5542 if (list_link_active(&vd->vdev_config_dirty_node))
5543 vdev_config_clean(vd);
5548 vdev_compact_children(rvd);
5550 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5551 vdev_add_child(rvd, vd);
5553 vdev_config_dirty(rvd);
5556 * Reassess the health of our root vdev.
5562 * Remove a device from the pool -
5564 * Removing a device from the vdev namespace requires several steps
5565 * and can take a significant amount of time. As a result we use
5566 * the spa_vdev_config_[enter/exit] functions which allow us to
5567 * grab and release the spa_config_lock while still holding the namespace
5568 * lock. During each step the configuration is synced out.
5570 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5574 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5577 metaslab_group_t *mg;
5578 nvlist_t **spares, **l2cache, *nv;
5580 uint_t nspares, nl2cache;
5582 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5584 ASSERT(spa_writeable(spa));
5587 txg = spa_vdev_enter(spa);
5589 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5591 if (spa->spa_spares.sav_vdevs != NULL &&
5592 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5593 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5594 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5596 * Only remove the hot spare if it's not currently in use
5599 if (vd == NULL || unspare) {
5600 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5601 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5602 spa_load_spares(spa);
5603 spa->spa_spares.sav_sync = B_TRUE;
5605 error = SET_ERROR(EBUSY);
5607 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5608 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5609 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5610 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5612 * Cache devices can always be removed.
5614 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5615 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5616 spa_load_l2cache(spa);
5617 spa->spa_l2cache.sav_sync = B_TRUE;
5618 } else if (vd != NULL && vd->vdev_islog) {
5620 ASSERT(vd == vd->vdev_top);
5625 * Stop allocating from this vdev.
5627 metaslab_group_passivate(mg);
5630 * Wait for the youngest allocations and frees to sync,
5631 * and then wait for the deferral of those frees to finish.
5633 spa_vdev_config_exit(spa, NULL,
5634 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5637 * Attempt to evacuate the vdev.
5639 error = spa_vdev_remove_evacuate(spa, vd);
5641 txg = spa_vdev_config_enter(spa);
5644 * If we couldn't evacuate the vdev, unwind.
5647 metaslab_group_activate(mg);
5648 return (spa_vdev_exit(spa, NULL, txg, error));
5652 * Clean up the vdev namespace.
5654 spa_vdev_remove_from_namespace(spa, vd);
5656 } else if (vd != NULL) {
5658 * Normal vdevs cannot be removed (yet).
5660 error = SET_ERROR(ENOTSUP);
5663 * There is no vdev of any kind with the specified guid.
5665 error = SET_ERROR(ENOENT);
5669 return (spa_vdev_exit(spa, NULL, txg, error));
5675 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5676 * currently spared, so we can detach it.
5679 spa_vdev_resilver_done_hunt(vdev_t *vd)
5681 vdev_t *newvd, *oldvd;
5683 for (int c = 0; c < vd->vdev_children; c++) {
5684 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5690 * Check for a completed replacement. We always consider the first
5691 * vdev in the list to be the oldest vdev, and the last one to be
5692 * the newest (see spa_vdev_attach() for how that works). In
5693 * the case where the newest vdev is faulted, we will not automatically
5694 * remove it after a resilver completes. This is OK as it will require
5695 * user intervention to determine which disk the admin wishes to keep.
5697 if (vd->vdev_ops == &vdev_replacing_ops) {
5698 ASSERT(vd->vdev_children > 1);
5700 newvd = vd->vdev_child[vd->vdev_children - 1];
5701 oldvd = vd->vdev_child[0];
5703 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5704 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5705 !vdev_dtl_required(oldvd))
5710 * Check for a completed resilver with the 'unspare' flag set.
5712 if (vd->vdev_ops == &vdev_spare_ops) {
5713 vdev_t *first = vd->vdev_child[0];
5714 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5716 if (last->vdev_unspare) {
5719 } else if (first->vdev_unspare) {
5726 if (oldvd != NULL &&
5727 vdev_dtl_empty(newvd, DTL_MISSING) &&
5728 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5729 !vdev_dtl_required(oldvd))
5733 * If there are more than two spares attached to a disk,
5734 * and those spares are not required, then we want to
5735 * attempt to free them up now so that they can be used
5736 * by other pools. Once we're back down to a single
5737 * disk+spare, we stop removing them.
5739 if (vd->vdev_children > 2) {
5740 newvd = vd->vdev_child[1];
5742 if (newvd->vdev_isspare && last->vdev_isspare &&
5743 vdev_dtl_empty(last, DTL_MISSING) &&
5744 vdev_dtl_empty(last, DTL_OUTAGE) &&
5745 !vdev_dtl_required(newvd))
5754 spa_vdev_resilver_done(spa_t *spa)
5756 vdev_t *vd, *pvd, *ppvd;
5757 uint64_t guid, sguid, pguid, ppguid;
5759 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5761 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5762 pvd = vd->vdev_parent;
5763 ppvd = pvd->vdev_parent;
5764 guid = vd->vdev_guid;
5765 pguid = pvd->vdev_guid;
5766 ppguid = ppvd->vdev_guid;
5769 * If we have just finished replacing a hot spared device, then
5770 * we need to detach the parent's first child (the original hot
5773 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5774 ppvd->vdev_children == 2) {
5775 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5776 sguid = ppvd->vdev_child[1]->vdev_guid;
5778 ASSERT(vd->vdev_resilver_txg == 0 || !vdev_dtl_required(vd));
5780 spa_config_exit(spa, SCL_ALL, FTAG);
5781 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5783 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5785 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5788 spa_config_exit(spa, SCL_ALL, FTAG);
5792 * Update the stored path or FRU for this vdev.
5795 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5799 boolean_t sync = B_FALSE;
5801 ASSERT(spa_writeable(spa));
5803 spa_vdev_state_enter(spa, SCL_ALL);
5805 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5806 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5808 if (!vd->vdev_ops->vdev_op_leaf)
5809 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5812 if (strcmp(value, vd->vdev_path) != 0) {
5813 spa_strfree(vd->vdev_path);
5814 vd->vdev_path = spa_strdup(value);
5818 if (vd->vdev_fru == NULL) {
5819 vd->vdev_fru = spa_strdup(value);
5821 } else if (strcmp(value, vd->vdev_fru) != 0) {
5822 spa_strfree(vd->vdev_fru);
5823 vd->vdev_fru = spa_strdup(value);
5828 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5832 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5834 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5838 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5840 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5844 * ==========================================================================
5846 * ==========================================================================
5850 spa_scan_stop(spa_t *spa)
5852 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5853 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5854 return (SET_ERROR(EBUSY));
5855 return (dsl_scan_cancel(spa->spa_dsl_pool));
5859 spa_scan(spa_t *spa, pool_scan_func_t func)
5861 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5863 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5864 return (SET_ERROR(ENOTSUP));
5867 * If a resilver was requested, but there is no DTL on a
5868 * writeable leaf device, we have nothing to do.
5870 if (func == POOL_SCAN_RESILVER &&
5871 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5872 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5876 return (dsl_scan(spa->spa_dsl_pool, func));
5880 * ==========================================================================
5881 * SPA async task processing
5882 * ==========================================================================
5886 spa_async_remove(spa_t *spa, vdev_t *vd)
5888 if (vd->vdev_remove_wanted) {
5889 vd->vdev_remove_wanted = B_FALSE;
5890 vd->vdev_delayed_close = B_FALSE;
5891 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5894 * We want to clear the stats, but we don't want to do a full
5895 * vdev_clear() as that will cause us to throw away
5896 * degraded/faulted state as well as attempt to reopen the
5897 * device, all of which is a waste.
5899 vd->vdev_stat.vs_read_errors = 0;
5900 vd->vdev_stat.vs_write_errors = 0;
5901 vd->vdev_stat.vs_checksum_errors = 0;
5903 vdev_state_dirty(vd->vdev_top);
5906 for (int c = 0; c < vd->vdev_children; c++)
5907 spa_async_remove(spa, vd->vdev_child[c]);
5911 spa_async_probe(spa_t *spa, vdev_t *vd)
5913 if (vd->vdev_probe_wanted) {
5914 vd->vdev_probe_wanted = B_FALSE;
5915 vdev_reopen(vd); /* vdev_open() does the actual probe */
5918 for (int c = 0; c < vd->vdev_children; c++)
5919 spa_async_probe(spa, vd->vdev_child[c]);
5923 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5929 if (!spa->spa_autoexpand)
5932 for (int c = 0; c < vd->vdev_children; c++) {
5933 vdev_t *cvd = vd->vdev_child[c];
5934 spa_async_autoexpand(spa, cvd);
5937 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5940 physpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
5941 (void) snprintf(physpath, MAXPATHLEN, "/devices%s", vd->vdev_physpath);
5943 VERIFY(nvlist_alloc(&attr, NV_UNIQUE_NAME, KM_SLEEP) == 0);
5944 VERIFY(nvlist_add_string(attr, DEV_PHYS_PATH, physpath) == 0);
5946 (void) ddi_log_sysevent(zfs_dip, SUNW_VENDOR, EC_DEV_STATUS,
5947 ESC_ZFS_VDEV_AUTOEXPAND, attr, &eid, DDI_SLEEP);
5950 kmem_free(physpath, MAXPATHLEN);
5954 spa_async_thread(void *arg)
5959 ASSERT(spa->spa_sync_on);
5961 mutex_enter(&spa->spa_async_lock);
5962 tasks = spa->spa_async_tasks;
5963 spa->spa_async_tasks &= SPA_ASYNC_REMOVE;
5964 mutex_exit(&spa->spa_async_lock);
5967 * See if the config needs to be updated.
5969 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5970 uint64_t old_space, new_space;
5972 mutex_enter(&spa_namespace_lock);
5973 old_space = metaslab_class_get_space(spa_normal_class(spa));
5974 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5975 new_space = metaslab_class_get_space(spa_normal_class(spa));
5976 mutex_exit(&spa_namespace_lock);
5979 * If the pool grew as a result of the config update,
5980 * then log an internal history event.
5982 if (new_space != old_space) {
5983 spa_history_log_internal(spa, "vdev online", NULL,
5984 "pool '%s' size: %llu(+%llu)",
5985 spa_name(spa), new_space, new_space - old_space);
5989 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5990 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5991 spa_async_autoexpand(spa, spa->spa_root_vdev);
5992 spa_config_exit(spa, SCL_CONFIG, FTAG);
5996 * See if any devices need to be probed.
5998 if (tasks & SPA_ASYNC_PROBE) {
5999 spa_vdev_state_enter(spa, SCL_NONE);
6000 spa_async_probe(spa, spa->spa_root_vdev);
6001 (void) spa_vdev_state_exit(spa, NULL, 0);
6005 * If any devices are done replacing, detach them.
6007 if (tasks & SPA_ASYNC_RESILVER_DONE)
6008 spa_vdev_resilver_done(spa);
6011 * Kick off a resilver.
6013 if (tasks & SPA_ASYNC_RESILVER)
6014 dsl_resilver_restart(spa->spa_dsl_pool, 0);
6017 * Let the world know that we're done.
6019 mutex_enter(&spa->spa_async_lock);
6020 spa->spa_async_thread = NULL;
6021 cv_broadcast(&spa->spa_async_cv);
6022 mutex_exit(&spa->spa_async_lock);
6027 spa_async_thread_vd(void *arg)
6032 ASSERT(spa->spa_sync_on);
6034 mutex_enter(&spa->spa_async_lock);
6035 tasks = spa->spa_async_tasks;
6037 spa->spa_async_tasks &= ~SPA_ASYNC_REMOVE;
6038 mutex_exit(&spa->spa_async_lock);
6041 * See if any devices need to be marked REMOVED.
6043 if (tasks & SPA_ASYNC_REMOVE) {
6044 spa_vdev_state_enter(spa, SCL_NONE);
6045 spa_async_remove(spa, spa->spa_root_vdev);
6046 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
6047 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
6048 for (int i = 0; i < spa->spa_spares.sav_count; i++)
6049 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
6050 (void) spa_vdev_state_exit(spa, NULL, 0);
6054 * Let the world know that we're done.
6056 mutex_enter(&spa->spa_async_lock);
6057 tasks = spa->spa_async_tasks;
6058 if ((tasks & SPA_ASYNC_REMOVE) != 0)
6060 spa->spa_async_thread_vd = NULL;
6061 cv_broadcast(&spa->spa_async_cv);
6062 mutex_exit(&spa->spa_async_lock);
6067 spa_async_suspend(spa_t *spa)
6069 mutex_enter(&spa->spa_async_lock);
6070 spa->spa_async_suspended++;
6071 while (spa->spa_async_thread != NULL &&
6072 spa->spa_async_thread_vd != NULL)
6073 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
6074 mutex_exit(&spa->spa_async_lock);
6078 spa_async_resume(spa_t *spa)
6080 mutex_enter(&spa->spa_async_lock);
6081 ASSERT(spa->spa_async_suspended != 0);
6082 spa->spa_async_suspended--;
6083 mutex_exit(&spa->spa_async_lock);
6087 spa_async_tasks_pending(spa_t *spa)
6089 uint_t non_config_tasks;
6091 boolean_t config_task_suspended;
6093 non_config_tasks = spa->spa_async_tasks & ~(SPA_ASYNC_CONFIG_UPDATE |
6095 config_task = spa->spa_async_tasks & SPA_ASYNC_CONFIG_UPDATE;
6096 if (spa->spa_ccw_fail_time == 0) {
6097 config_task_suspended = B_FALSE;
6099 config_task_suspended =
6100 (gethrtime() - spa->spa_ccw_fail_time) <
6101 (zfs_ccw_retry_interval * NANOSEC);
6104 return (non_config_tasks || (config_task && !config_task_suspended));
6108 spa_async_dispatch(spa_t *spa)
6110 mutex_enter(&spa->spa_async_lock);
6111 if (spa_async_tasks_pending(spa) &&
6112 !spa->spa_async_suspended &&
6113 spa->spa_async_thread == NULL &&
6115 spa->spa_async_thread = thread_create(NULL, 0,
6116 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
6117 mutex_exit(&spa->spa_async_lock);
6121 spa_async_dispatch_vd(spa_t *spa)
6123 mutex_enter(&spa->spa_async_lock);
6124 if ((spa->spa_async_tasks & SPA_ASYNC_REMOVE) != 0 &&
6125 !spa->spa_async_suspended &&
6126 spa->spa_async_thread_vd == NULL &&
6128 spa->spa_async_thread_vd = thread_create(NULL, 0,
6129 spa_async_thread_vd, spa, 0, &p0, TS_RUN, maxclsyspri);
6130 mutex_exit(&spa->spa_async_lock);
6134 spa_async_request(spa_t *spa, int task)
6136 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
6137 mutex_enter(&spa->spa_async_lock);
6138 spa->spa_async_tasks |= task;
6139 mutex_exit(&spa->spa_async_lock);
6140 spa_async_dispatch_vd(spa);
6144 * ==========================================================================
6145 * SPA syncing routines
6146 * ==========================================================================
6150 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6153 bpobj_enqueue(bpo, bp, tx);
6158 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
6162 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
6163 BP_GET_PSIZE(bp), zio->io_flags));
6168 * Note: this simple function is not inlined to make it easier to dtrace the
6169 * amount of time spent syncing frees.
6172 spa_sync_frees(spa_t *spa, bplist_t *bpl, dmu_tx_t *tx)
6174 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6175 bplist_iterate(bpl, spa_free_sync_cb, zio, tx);
6176 VERIFY(zio_wait(zio) == 0);
6180 * Note: this simple function is not inlined to make it easier to dtrace the
6181 * amount of time spent syncing deferred frees.
6184 spa_sync_deferred_frees(spa_t *spa, dmu_tx_t *tx)
6186 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6187 VERIFY3U(bpobj_iterate(&spa->spa_deferred_bpobj,
6188 spa_free_sync_cb, zio, tx), ==, 0);
6189 VERIFY0(zio_wait(zio));
6194 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
6196 char *packed = NULL;
6201 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
6204 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6205 * information. This avoids the dmu_buf_will_dirty() path and
6206 * saves us a pre-read to get data we don't actually care about.
6208 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
6209 packed = kmem_alloc(bufsize, KM_SLEEP);
6211 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
6213 bzero(packed + nvsize, bufsize - nvsize);
6215 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
6217 kmem_free(packed, bufsize);
6219 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
6220 dmu_buf_will_dirty(db, tx);
6221 *(uint64_t *)db->db_data = nvsize;
6222 dmu_buf_rele(db, FTAG);
6226 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
6227 const char *config, const char *entry)
6237 * Update the MOS nvlist describing the list of available devices.
6238 * spa_validate_aux() will have already made sure this nvlist is
6239 * valid and the vdevs are labeled appropriately.
6241 if (sav->sav_object == 0) {
6242 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
6243 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
6244 sizeof (uint64_t), tx);
6245 VERIFY(zap_update(spa->spa_meta_objset,
6246 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
6247 &sav->sav_object, tx) == 0);
6250 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6251 if (sav->sav_count == 0) {
6252 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
6254 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
6255 for (i = 0; i < sav->sav_count; i++)
6256 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
6257 B_FALSE, VDEV_CONFIG_L2CACHE);
6258 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
6259 sav->sav_count) == 0);
6260 for (i = 0; i < sav->sav_count; i++)
6261 nvlist_free(list[i]);
6262 kmem_free(list, sav->sav_count * sizeof (void *));
6265 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
6266 nvlist_free(nvroot);
6268 sav->sav_sync = B_FALSE;
6272 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
6276 if (list_is_empty(&spa->spa_config_dirty_list))
6279 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6281 config = spa_config_generate(spa, spa->spa_root_vdev,
6282 dmu_tx_get_txg(tx), B_FALSE);
6285 * If we're upgrading the spa version then make sure that
6286 * the config object gets updated with the correct version.
6288 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
6289 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
6290 spa->spa_uberblock.ub_version);
6292 spa_config_exit(spa, SCL_STATE, FTAG);
6294 if (spa->spa_config_syncing)
6295 nvlist_free(spa->spa_config_syncing);
6296 spa->spa_config_syncing = config;
6298 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
6302 spa_sync_version(void *arg, dmu_tx_t *tx)
6304 uint64_t *versionp = arg;
6305 uint64_t version = *versionp;
6306 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6309 * Setting the version is special cased when first creating the pool.
6311 ASSERT(tx->tx_txg != TXG_INITIAL);
6313 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
6314 ASSERT(version >= spa_version(spa));
6316 spa->spa_uberblock.ub_version = version;
6317 vdev_config_dirty(spa->spa_root_vdev);
6318 spa_history_log_internal(spa, "set", tx, "version=%lld", version);
6322 * Set zpool properties.
6325 spa_sync_props(void *arg, dmu_tx_t *tx)
6327 nvlist_t *nvp = arg;
6328 spa_t *spa = dmu_tx_pool(tx)->dp_spa;
6329 objset_t *mos = spa->spa_meta_objset;
6330 nvpair_t *elem = NULL;
6332 mutex_enter(&spa->spa_props_lock);
6334 while ((elem = nvlist_next_nvpair(nvp, elem))) {
6336 char *strval, *fname;
6338 const char *propname;
6339 zprop_type_t proptype;
6342 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
6345 * We checked this earlier in spa_prop_validate().
6347 ASSERT(zpool_prop_feature(nvpair_name(elem)));
6349 fname = strchr(nvpair_name(elem), '@') + 1;
6350 VERIFY0(zfeature_lookup_name(fname, &fid));
6352 spa_feature_enable(spa, fid, tx);
6353 spa_history_log_internal(spa, "set", tx,
6354 "%s=enabled", nvpair_name(elem));
6357 case ZPOOL_PROP_VERSION:
6358 intval = fnvpair_value_uint64(elem);
6360 * The version is synced seperatly before other
6361 * properties and should be correct by now.
6363 ASSERT3U(spa_version(spa), >=, intval);
6366 case ZPOOL_PROP_ALTROOT:
6368 * 'altroot' is a non-persistent property. It should
6369 * have been set temporarily at creation or import time.
6371 ASSERT(spa->spa_root != NULL);
6374 case ZPOOL_PROP_READONLY:
6375 case ZPOOL_PROP_CACHEFILE:
6377 * 'readonly' and 'cachefile' are also non-persisitent
6381 case ZPOOL_PROP_COMMENT:
6382 strval = fnvpair_value_string(elem);
6383 if (spa->spa_comment != NULL)
6384 spa_strfree(spa->spa_comment);
6385 spa->spa_comment = spa_strdup(strval);
6387 * We need to dirty the configuration on all the vdevs
6388 * so that their labels get updated. It's unnecessary
6389 * to do this for pool creation since the vdev's
6390 * configuratoin has already been dirtied.
6392 if (tx->tx_txg != TXG_INITIAL)
6393 vdev_config_dirty(spa->spa_root_vdev);
6394 spa_history_log_internal(spa, "set", tx,
6395 "%s=%s", nvpair_name(elem), strval);
6399 * Set pool property values in the poolprops mos object.
6401 if (spa->spa_pool_props_object == 0) {
6402 spa->spa_pool_props_object =
6403 zap_create_link(mos, DMU_OT_POOL_PROPS,
6404 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
6408 /* normalize the property name */
6409 propname = zpool_prop_to_name(prop);
6410 proptype = zpool_prop_get_type(prop);
6412 if (nvpair_type(elem) == DATA_TYPE_STRING) {
6413 ASSERT(proptype == PROP_TYPE_STRING);
6414 strval = fnvpair_value_string(elem);
6415 VERIFY0(zap_update(mos,
6416 spa->spa_pool_props_object, propname,
6417 1, strlen(strval) + 1, strval, tx));
6418 spa_history_log_internal(spa, "set", tx,
6419 "%s=%s", nvpair_name(elem), strval);
6420 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
6421 intval = fnvpair_value_uint64(elem);
6423 if (proptype == PROP_TYPE_INDEX) {
6425 VERIFY0(zpool_prop_index_to_string(
6426 prop, intval, &unused));
6428 VERIFY0(zap_update(mos,
6429 spa->spa_pool_props_object, propname,
6430 8, 1, &intval, tx));
6431 spa_history_log_internal(spa, "set", tx,
6432 "%s=%lld", nvpair_name(elem), intval);
6434 ASSERT(0); /* not allowed */
6438 case ZPOOL_PROP_DELEGATION:
6439 spa->spa_delegation = intval;
6441 case ZPOOL_PROP_BOOTFS:
6442 spa->spa_bootfs = intval;
6444 case ZPOOL_PROP_FAILUREMODE:
6445 spa->spa_failmode = intval;
6447 case ZPOOL_PROP_AUTOEXPAND:
6448 spa->spa_autoexpand = intval;
6449 if (tx->tx_txg != TXG_INITIAL)
6450 spa_async_request(spa,
6451 SPA_ASYNC_AUTOEXPAND);
6453 case ZPOOL_PROP_DEDUPDITTO:
6454 spa->spa_dedup_ditto = intval;
6463 mutex_exit(&spa->spa_props_lock);
6467 * Perform one-time upgrade on-disk changes. spa_version() does not
6468 * reflect the new version this txg, so there must be no changes this
6469 * txg to anything that the upgrade code depends on after it executes.
6470 * Therefore this must be called after dsl_pool_sync() does the sync
6474 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6476 dsl_pool_t *dp = spa->spa_dsl_pool;
6478 ASSERT(spa->spa_sync_pass == 1);
6480 rrw_enter(&dp->dp_config_rwlock, RW_WRITER, FTAG);
6482 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6483 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6484 dsl_pool_create_origin(dp, tx);
6486 /* Keeping the origin open increases spa_minref */
6487 spa->spa_minref += 3;
6490 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6491 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6492 dsl_pool_upgrade_clones(dp, tx);
6495 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6496 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6497 dsl_pool_upgrade_dir_clones(dp, tx);
6499 /* Keeping the freedir open increases spa_minref */
6500 spa->spa_minref += 3;
6503 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6504 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6505 spa_feature_create_zap_objects(spa, tx);
6509 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6510 * when possibility to use lz4 compression for metadata was added
6511 * Old pools that have this feature enabled must be upgraded to have
6512 * this feature active
6514 if (spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6515 boolean_t lz4_en = spa_feature_is_enabled(spa,
6516 SPA_FEATURE_LZ4_COMPRESS);
6517 boolean_t lz4_ac = spa_feature_is_active(spa,
6518 SPA_FEATURE_LZ4_COMPRESS);
6520 if (lz4_en && !lz4_ac)
6521 spa_feature_incr(spa, SPA_FEATURE_LZ4_COMPRESS, tx);
6523 rrw_exit(&dp->dp_config_rwlock, FTAG);
6527 * Sync the specified transaction group. New blocks may be dirtied as
6528 * part of the process, so we iterate until it converges.
6531 spa_sync(spa_t *spa, uint64_t txg)
6533 dsl_pool_t *dp = spa->spa_dsl_pool;
6534 objset_t *mos = spa->spa_meta_objset;
6535 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6536 vdev_t *rvd = spa->spa_root_vdev;
6541 VERIFY(spa_writeable(spa));
6544 * Lock out configuration changes.
6546 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6548 spa->spa_syncing_txg = txg;
6549 spa->spa_sync_pass = 0;
6552 * If there are any pending vdev state changes, convert them
6553 * into config changes that go out with this transaction group.
6555 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6556 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6558 * We need the write lock here because, for aux vdevs,
6559 * calling vdev_config_dirty() modifies sav_config.
6560 * This is ugly and will become unnecessary when we
6561 * eliminate the aux vdev wart by integrating all vdevs
6562 * into the root vdev tree.
6564 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6565 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6566 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6567 vdev_state_clean(vd);
6568 vdev_config_dirty(vd);
6570 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6571 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6573 spa_config_exit(spa, SCL_STATE, FTAG);
6575 tx = dmu_tx_create_assigned(dp, txg);
6577 spa->spa_sync_starttime = gethrtime();
6579 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid,
6580 spa->spa_sync_starttime + spa->spa_deadman_synctime));
6581 #else /* !illumos */
6583 callout_reset(&spa->spa_deadman_cycid,
6584 hz * spa->spa_deadman_synctime / NANOSEC, spa_deadman, spa);
6586 #endif /* illumos */
6589 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6590 * set spa_deflate if we have no raid-z vdevs.
6592 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6593 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6596 for (i = 0; i < rvd->vdev_children; i++) {
6597 vd = rvd->vdev_child[i];
6598 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6601 if (i == rvd->vdev_children) {
6602 spa->spa_deflate = TRUE;
6603 VERIFY(0 == zap_add(spa->spa_meta_objset,
6604 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6605 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6610 * Iterate to convergence.
6613 int pass = ++spa->spa_sync_pass;
6615 spa_sync_config_object(spa, tx);
6616 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6617 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6618 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6619 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6620 spa_errlog_sync(spa, txg);
6621 dsl_pool_sync(dp, txg);
6623 if (pass < zfs_sync_pass_deferred_free) {
6624 spa_sync_frees(spa, free_bpl, tx);
6627 * We can not defer frees in pass 1, because
6628 * we sync the deferred frees later in pass 1.
6630 ASSERT3U(pass, >, 1);
6631 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6632 &spa->spa_deferred_bpobj, tx);
6636 dsl_scan_sync(dp, tx);
6638 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg))
6642 spa_sync_upgrades(spa, tx);
6644 spa->spa_uberblock.ub_rootbp.blk_birth);
6646 * Note: We need to check if the MOS is dirty
6647 * because we could have marked the MOS dirty
6648 * without updating the uberblock (e.g. if we
6649 * have sync tasks but no dirty user data). We
6650 * need to check the uberblock's rootbp because
6651 * it is updated if we have synced out dirty
6652 * data (though in this case the MOS will most
6653 * likely also be dirty due to second order
6654 * effects, we don't want to rely on that here).
6656 if (spa->spa_uberblock.ub_rootbp.blk_birth < txg &&
6657 !dmu_objset_is_dirty(mos, txg)) {
6659 * Nothing changed on the first pass,
6660 * therefore this TXG is a no-op. Avoid
6661 * syncing deferred frees, so that we
6662 * can keep this TXG as a no-op.
6664 ASSERT(txg_list_empty(&dp->dp_dirty_datasets,
6666 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6667 ASSERT(txg_list_empty(&dp->dp_sync_tasks, txg));
6670 spa_sync_deferred_frees(spa, tx);
6673 } while (dmu_objset_is_dirty(mos, txg));
6676 * Rewrite the vdev configuration (which includes the uberblock)
6677 * to commit the transaction group.
6679 * If there are no dirty vdevs, we sync the uberblock to a few
6680 * random top-level vdevs that are known to be visible in the
6681 * config cache (see spa_vdev_add() for a complete description).
6682 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6686 * We hold SCL_STATE to prevent vdev open/close/etc.
6687 * while we're attempting to write the vdev labels.
6689 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6691 if (list_is_empty(&spa->spa_config_dirty_list)) {
6692 vdev_t *svd[SPA_DVAS_PER_BP];
6694 int children = rvd->vdev_children;
6695 int c0 = spa_get_random(children);
6697 for (int c = 0; c < children; c++) {
6698 vd = rvd->vdev_child[(c0 + c) % children];
6699 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6701 svd[svdcount++] = vd;
6702 if (svdcount == SPA_DVAS_PER_BP)
6705 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6707 error = vdev_config_sync(svd, svdcount, txg,
6710 error = vdev_config_sync(rvd->vdev_child,
6711 rvd->vdev_children, txg, B_FALSE);
6713 error = vdev_config_sync(rvd->vdev_child,
6714 rvd->vdev_children, txg, B_TRUE);
6718 spa->spa_last_synced_guid = rvd->vdev_guid;
6720 spa_config_exit(spa, SCL_STATE, FTAG);
6724 zio_suspend(spa, NULL);
6725 zio_resume_wait(spa);
6730 VERIFY(cyclic_reprogram(spa->spa_deadman_cycid, CY_INFINITY));
6731 #else /* !illumos */
6733 callout_drain(&spa->spa_deadman_cycid);
6735 #endif /* illumos */
6738 * Clear the dirty config list.
6740 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6741 vdev_config_clean(vd);
6744 * Now that the new config has synced transactionally,
6745 * let it become visible to the config cache.
6747 if (spa->spa_config_syncing != NULL) {
6748 spa_config_set(spa, spa->spa_config_syncing);
6749 spa->spa_config_txg = txg;
6750 spa->spa_config_syncing = NULL;
6753 spa->spa_ubsync = spa->spa_uberblock;
6755 dsl_pool_sync_done(dp, txg);
6758 * Update usable space statistics.
6760 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
6761 vdev_sync_done(vd, txg);
6763 spa_update_dspace(spa);
6766 * It had better be the case that we didn't dirty anything
6767 * since vdev_config_sync().
6769 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6770 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6771 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6773 spa->spa_sync_pass = 0;
6775 spa_config_exit(spa, SCL_CONFIG, FTAG);
6777 spa_handle_ignored_writes(spa);
6780 * If any async tasks have been requested, kick them off.
6782 spa_async_dispatch(spa);
6783 spa_async_dispatch_vd(spa);
6787 * Sync all pools. We don't want to hold the namespace lock across these
6788 * operations, so we take a reference on the spa_t and drop the lock during the
6792 spa_sync_allpools(void)
6795 mutex_enter(&spa_namespace_lock);
6796 while ((spa = spa_next(spa)) != NULL) {
6797 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6798 !spa_writeable(spa) || spa_suspended(spa))
6800 spa_open_ref(spa, FTAG);
6801 mutex_exit(&spa_namespace_lock);
6802 txg_wait_synced(spa_get_dsl(spa), 0);
6803 mutex_enter(&spa_namespace_lock);
6804 spa_close(spa, FTAG);
6806 mutex_exit(&spa_namespace_lock);
6810 * ==========================================================================
6811 * Miscellaneous routines
6812 * ==========================================================================
6816 * Remove all pools in the system.
6824 * Remove all cached state. All pools should be closed now,
6825 * so every spa in the AVL tree should be unreferenced.
6827 mutex_enter(&spa_namespace_lock);
6828 while ((spa = spa_next(NULL)) != NULL) {
6830 * Stop async tasks. The async thread may need to detach
6831 * a device that's been replaced, which requires grabbing
6832 * spa_namespace_lock, so we must drop it here.
6834 spa_open_ref(spa, FTAG);
6835 mutex_exit(&spa_namespace_lock);
6836 spa_async_suspend(spa);
6837 mutex_enter(&spa_namespace_lock);
6838 spa_close(spa, FTAG);
6840 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6842 spa_deactivate(spa);
6846 mutex_exit(&spa_namespace_lock);
6850 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6855 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6859 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6860 vd = spa->spa_l2cache.sav_vdevs[i];
6861 if (vd->vdev_guid == guid)
6865 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6866 vd = spa->spa_spares.sav_vdevs[i];
6867 if (vd->vdev_guid == guid)
6876 spa_upgrade(spa_t *spa, uint64_t version)
6878 ASSERT(spa_writeable(spa));
6880 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6883 * This should only be called for a non-faulted pool, and since a
6884 * future version would result in an unopenable pool, this shouldn't be
6887 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6888 ASSERT3U(version, >=, spa->spa_uberblock.ub_version);
6890 spa->spa_uberblock.ub_version = version;
6891 vdev_config_dirty(spa->spa_root_vdev);
6893 spa_config_exit(spa, SCL_ALL, FTAG);
6895 txg_wait_synced(spa_get_dsl(spa), 0);
6899 spa_has_spare(spa_t *spa, uint64_t guid)
6903 spa_aux_vdev_t *sav = &spa->spa_spares;
6905 for (i = 0; i < sav->sav_count; i++)
6906 if (sav->sav_vdevs[i]->vdev_guid == guid)
6909 for (i = 0; i < sav->sav_npending; i++) {
6910 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6911 &spareguid) == 0 && spareguid == guid)
6919 * Check if a pool has an active shared spare device.
6920 * Note: reference count of an active spare is 2, as a spare and as a replace
6923 spa_has_active_shared_spare(spa_t *spa)
6927 spa_aux_vdev_t *sav = &spa->spa_spares;
6929 for (i = 0; i < sav->sav_count; i++) {
6930 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6931 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6940 * Post a sysevent corresponding to the given event. The 'name' must be one of
6941 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6942 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6943 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6944 * or zdb as real changes.
6947 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6951 sysevent_attr_list_t *attr = NULL;
6952 sysevent_value_t value;
6955 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
6958 value.value_type = SE_DATA_TYPE_STRING;
6959 value.value.sv_string = spa_name(spa);
6960 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
6963 value.value_type = SE_DATA_TYPE_UINT64;
6964 value.value.sv_uint64 = spa_guid(spa);
6965 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
6969 value.value_type = SE_DATA_TYPE_UINT64;
6970 value.value.sv_uint64 = vd->vdev_guid;
6971 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
6975 if (vd->vdev_path) {
6976 value.value_type = SE_DATA_TYPE_STRING;
6977 value.value.sv_string = vd->vdev_path;
6978 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
6979 &value, SE_SLEEP) != 0)
6984 if (sysevent_attach_attributes(ev, attr) != 0)
6988 (void) log_sysevent(ev, SE_SLEEP, &eid);
6992 sysevent_free_attr(attr);