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 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
29 * This file contains all the routines used when modifying on-disk SPA state.
30 * This includes opening, importing, destroying, exporting a pool, and syncing a
34 #include <sys/zfs_context.h>
35 #include <sys/fm/fs/zfs.h>
36 #include <sys/spa_impl.h>
38 #include <sys/zio_checksum.h>
40 #include <sys/dmu_tx.h>
44 #include <sys/vdev_impl.h>
45 #include <sys/vdev_disk.h>
46 #include <sys/metaslab.h>
47 #include <sys/metaslab_impl.h>
48 #include <sys/uberblock_impl.h>
51 #include <sys/dmu_traverse.h>
52 #include <sys/dmu_objset.h>
53 #include <sys/unique.h>
54 #include <sys/dsl_pool.h>
55 #include <sys/dsl_dataset.h>
56 #include <sys/dsl_dir.h>
57 #include <sys/dsl_prop.h>
58 #include <sys/dsl_synctask.h>
59 #include <sys/fs/zfs.h>
61 #include <sys/callb.h>
62 #include <sys/systeminfo.h>
63 #include <sys/spa_boot.h>
64 #include <sys/zfs_ioctl.h>
65 #include <sys/dsl_scan.h>
66 #include <sys/zfeature.h>
69 #include <sys/bootprops.h>
70 #include <sys/callb.h>
71 #include <sys/cpupart.h>
73 #include <sys/sysdc.h>
78 #include "zfs_comutil.h"
80 typedef enum zti_modes {
81 ZTI_MODE_FIXED, /* value is # of threads (min 1) */
82 ZTI_MODE_ONLINE_PERCENT, /* value is % of online CPUs */
83 ZTI_MODE_BATCH, /* cpu-intensive; value is ignored */
84 ZTI_MODE_NULL, /* don't create a taskq */
88 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
89 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
90 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
91 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
93 #define ZTI_N(n) ZTI_P(n, 1)
94 #define ZTI_ONE ZTI_N(1)
96 typedef struct zio_taskq_info {
102 static const char *const zio_taskq_types[ZIO_TASKQ_TYPES] = {
103 "iss", "iss_h", "int", "int_h"
107 * This table defines the taskq settings for each ZFS I/O type. When
108 * initializing a pool, we use this table to create an appropriately sized
109 * taskq. Some operations are low volume and therefore have a small, static
110 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
111 * macros. Other operations process a large amount of data; the ZTI_BATCH
112 * macro causes us to create a taskq oriented for throughput. Some operations
113 * are so high frequency and short-lived that the taskq itself can become a a
114 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
115 * additional degree of parallelism specified by the number of threads per-
116 * taskq and the number of taskqs; when dispatching an event in this case, the
117 * particular taskq is chosen at random.
119 * The different taskq priorities are to handle the different contexts (issue
120 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
121 * need to be handled with minimum delay.
123 const zio_taskq_info_t zio_taskqs[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
124 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
125 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* NULL */
126 { ZTI_N(8), ZTI_NULL, ZTI_BATCH, ZTI_NULL }, /* READ */
127 { ZTI_BATCH, ZTI_N(5), ZTI_N(16), ZTI_N(5) }, /* WRITE */
128 { ZTI_P(4, 8), ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* FREE */
129 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* CLAIM */
130 { ZTI_ONE, ZTI_NULL, ZTI_ONE, ZTI_NULL }, /* IOCTL */
133 static dsl_syncfunc_t spa_sync_version;
134 static dsl_syncfunc_t spa_sync_props;
135 static dsl_checkfunc_t spa_change_guid_check;
136 static dsl_syncfunc_t spa_change_guid_sync;
137 static boolean_t spa_has_active_shared_spare(spa_t *spa);
138 static inline int spa_load_impl(spa_t *spa, uint64_t, nvlist_t *config,
139 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
141 static void spa_vdev_resilver_done(spa_t *spa);
143 uint_t zio_taskq_batch_pct = 100; /* 1 thread per cpu in pset */
144 id_t zio_taskq_psrset_bind = PS_NONE;
145 boolean_t zio_taskq_sysdc = B_TRUE; /* use SDC scheduling class */
146 uint_t zio_taskq_basedc = 80; /* base duty cycle */
148 boolean_t spa_create_process = B_TRUE; /* no process ==> no sysdc */
151 * This (illegal) pool name is used when temporarily importing a spa_t in order
152 * to get the vdev stats associated with the imported devices.
154 #define TRYIMPORT_NAME "$import"
157 * ==========================================================================
158 * SPA properties routines
159 * ==========================================================================
163 * Add a (source=src, propname=propval) list to an nvlist.
166 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
167 uint64_t intval, zprop_source_t src)
169 const char *propname = zpool_prop_to_name(prop);
172 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
173 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
176 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
178 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
180 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
181 nvlist_free(propval);
185 * Get property values from the spa configuration.
188 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
190 vdev_t *rvd = spa->spa_root_vdev;
191 dsl_pool_t *pool = spa->spa_dsl_pool;
195 uint64_t cap, version;
196 zprop_source_t src = ZPROP_SRC_NONE;
197 spa_config_dirent_t *dp;
200 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
203 alloc = metaslab_class_get_alloc(spa_normal_class(spa));
204 size = metaslab_class_get_space(spa_normal_class(spa));
205 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
206 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
207 spa_prop_add_list(*nvp, ZPOOL_PROP_ALLOCATED, NULL, alloc, src);
208 spa_prop_add_list(*nvp, ZPOOL_PROP_FREE, NULL,
212 for (c = 0; c < rvd->vdev_children; c++) {
213 vdev_t *tvd = rvd->vdev_child[c];
214 space += tvd->vdev_max_asize - tvd->vdev_asize;
216 spa_prop_add_list(*nvp, ZPOOL_PROP_EXPANDSZ, NULL, space,
219 spa_prop_add_list(*nvp, ZPOOL_PROP_READONLY, NULL,
220 (spa_mode(spa) == FREAD), src);
222 cap = (size == 0) ? 0 : (alloc * 100 / size);
223 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
225 spa_prop_add_list(*nvp, ZPOOL_PROP_DEDUPRATIO, NULL,
226 ddt_get_pool_dedup_ratio(spa), src);
228 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
229 rvd->vdev_state, src);
231 version = spa_version(spa);
232 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
233 src = ZPROP_SRC_DEFAULT;
235 src = ZPROP_SRC_LOCAL;
236 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
240 dsl_dir_t *freedir = pool->dp_free_dir;
243 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
244 * when opening pools before this version freedir will be NULL.
246 if (freedir != NULL) {
247 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING, NULL,
248 freedir->dd_phys->dd_used_bytes, src);
250 spa_prop_add_list(*nvp, ZPOOL_PROP_FREEING,
255 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
257 if (spa->spa_comment != NULL) {
258 spa_prop_add_list(*nvp, ZPOOL_PROP_COMMENT, spa->spa_comment,
262 if (spa->spa_root != NULL)
263 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
266 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
267 if (dp->scd_path == NULL) {
268 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
269 "none", 0, ZPROP_SRC_LOCAL);
270 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
271 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
272 dp->scd_path, 0, ZPROP_SRC_LOCAL);
278 * Get zpool property values.
281 spa_prop_get(spa_t *spa, nvlist_t **nvp)
283 objset_t *mos = spa->spa_meta_objset;
288 err = nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_PUSHPAGE);
292 mutex_enter(&spa->spa_props_lock);
295 * Get properties from the spa config.
297 spa_prop_get_config(spa, nvp);
299 /* If no pool property object, no more prop to get. */
300 if (mos == NULL || spa->spa_pool_props_object == 0) {
301 mutex_exit(&spa->spa_props_lock);
306 * Get properties from the MOS pool property object.
308 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
309 (err = zap_cursor_retrieve(&zc, &za)) == 0;
310 zap_cursor_advance(&zc)) {
313 zprop_source_t src = ZPROP_SRC_DEFAULT;
316 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
319 switch (za.za_integer_length) {
321 /* integer property */
322 if (za.za_first_integer !=
323 zpool_prop_default_numeric(prop))
324 src = ZPROP_SRC_LOCAL;
326 if (prop == ZPOOL_PROP_BOOTFS) {
328 dsl_dataset_t *ds = NULL;
330 dp = spa_get_dsl(spa);
331 rw_enter(&dp->dp_config_rwlock, RW_READER);
332 if ((err = dsl_dataset_hold_obj(dp,
333 za.za_first_integer, FTAG, &ds))) {
334 rw_exit(&dp->dp_config_rwlock);
339 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
341 dsl_dataset_name(ds, strval);
342 dsl_dataset_rele(ds, FTAG);
343 rw_exit(&dp->dp_config_rwlock);
346 intval = za.za_first_integer;
349 spa_prop_add_list(*nvp, prop, strval, intval, src);
353 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
358 /* string property */
359 strval = kmem_alloc(za.za_num_integers, KM_PUSHPAGE);
360 err = zap_lookup(mos, spa->spa_pool_props_object,
361 za.za_name, 1, za.za_num_integers, strval);
363 kmem_free(strval, za.za_num_integers);
366 spa_prop_add_list(*nvp, prop, strval, 0, src);
367 kmem_free(strval, za.za_num_integers);
374 zap_cursor_fini(&zc);
375 mutex_exit(&spa->spa_props_lock);
377 if (err && err != ENOENT) {
387 * Validate the given pool properties nvlist and modify the list
388 * for the property values to be set.
391 spa_prop_validate(spa_t *spa, nvlist_t *props)
394 int error = 0, reset_bootfs = 0;
396 boolean_t has_feature = B_FALSE;
399 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
401 char *strval, *slash, *check, *fname;
402 const char *propname = nvpair_name(elem);
403 zpool_prop_t prop = zpool_name_to_prop(propname);
407 if (!zpool_prop_feature(propname)) {
413 * Sanitize the input.
415 if (nvpair_type(elem) != DATA_TYPE_UINT64) {
420 if (nvpair_value_uint64(elem, &intval) != 0) {
430 fname = strchr(propname, '@') + 1;
431 if (zfeature_lookup_name(fname, NULL) != 0) {
436 has_feature = B_TRUE;
439 case ZPOOL_PROP_VERSION:
440 error = nvpair_value_uint64(elem, &intval);
442 (intval < spa_version(spa) ||
443 intval > SPA_VERSION_BEFORE_FEATURES ||
448 case ZPOOL_PROP_DELEGATION:
449 case ZPOOL_PROP_AUTOREPLACE:
450 case ZPOOL_PROP_LISTSNAPS:
451 case ZPOOL_PROP_AUTOEXPAND:
452 error = nvpair_value_uint64(elem, &intval);
453 if (!error && intval > 1)
457 case ZPOOL_PROP_BOOTFS:
459 * If the pool version is less than SPA_VERSION_BOOTFS,
460 * or the pool is still being created (version == 0),
461 * the bootfs property cannot be set.
463 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
469 * Make sure the vdev config is bootable
471 if (!vdev_is_bootable(spa->spa_root_vdev)) {
478 error = nvpair_value_string(elem, &strval);
484 if (strval == NULL || strval[0] == '\0') {
485 objnum = zpool_prop_default_numeric(
490 if ((error = dmu_objset_hold(strval,FTAG,&os)))
493 /* Must be ZPL and not gzip compressed. */
495 if (dmu_objset_type(os) != DMU_OST_ZFS) {
497 } else if ((error = dsl_prop_get_integer(strval,
498 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
499 &compress, NULL)) == 0 &&
500 !BOOTFS_COMPRESS_VALID(compress)) {
503 objnum = dmu_objset_id(os);
505 dmu_objset_rele(os, FTAG);
509 case ZPOOL_PROP_FAILUREMODE:
510 error = nvpair_value_uint64(elem, &intval);
511 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
512 intval > ZIO_FAILURE_MODE_PANIC))
516 * This is a special case which only occurs when
517 * the pool has completely failed. This allows
518 * the user to change the in-core failmode property
519 * without syncing it out to disk (I/Os might
520 * currently be blocked). We do this by returning
521 * EIO to the caller (spa_prop_set) to trick it
522 * into thinking we encountered a property validation
525 if (!error && spa_suspended(spa)) {
526 spa->spa_failmode = intval;
531 case ZPOOL_PROP_CACHEFILE:
532 if ((error = nvpair_value_string(elem, &strval)) != 0)
535 if (strval[0] == '\0')
538 if (strcmp(strval, "none") == 0)
541 if (strval[0] != '/') {
546 slash = strrchr(strval, '/');
547 ASSERT(slash != NULL);
549 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
550 strcmp(slash, "/..") == 0)
554 case ZPOOL_PROP_COMMENT:
555 if ((error = nvpair_value_string(elem, &strval)) != 0)
557 for (check = strval; *check != '\0'; check++) {
558 if (!isprint(*check)) {
564 if (strlen(strval) > ZPROP_MAX_COMMENT)
568 case ZPOOL_PROP_DEDUPDITTO:
569 if (spa_version(spa) < SPA_VERSION_DEDUP)
572 error = nvpair_value_uint64(elem, &intval);
574 intval != 0 && intval < ZIO_DEDUPDITTO_MIN)
586 if (!error && reset_bootfs) {
587 error = nvlist_remove(props,
588 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
591 error = nvlist_add_uint64(props,
592 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
600 spa_configfile_set(spa_t *spa, nvlist_t *nvp, boolean_t need_sync)
603 spa_config_dirent_t *dp;
605 if (nvlist_lookup_string(nvp, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE),
609 dp = kmem_alloc(sizeof (spa_config_dirent_t),
612 if (cachefile[0] == '\0')
613 dp->scd_path = spa_strdup(spa_config_path);
614 else if (strcmp(cachefile, "none") == 0)
617 dp->scd_path = spa_strdup(cachefile);
619 list_insert_head(&spa->spa_config_list, dp);
621 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
625 spa_prop_set(spa_t *spa, nvlist_t *nvp)
628 nvpair_t *elem = NULL;
629 boolean_t need_sync = B_FALSE;
631 if ((error = spa_prop_validate(spa, nvp)) != 0)
634 while ((elem = nvlist_next_nvpair(nvp, elem)) != NULL) {
635 zpool_prop_t prop = zpool_name_to_prop(nvpair_name(elem));
637 if (prop == ZPOOL_PROP_CACHEFILE ||
638 prop == ZPOOL_PROP_ALTROOT ||
639 prop == ZPOOL_PROP_READONLY)
642 if (prop == ZPOOL_PROP_VERSION || prop == ZPROP_INVAL) {
645 if (prop == ZPOOL_PROP_VERSION) {
646 VERIFY(nvpair_value_uint64(elem, &ver) == 0);
648 ASSERT(zpool_prop_feature(nvpair_name(elem)));
649 ver = SPA_VERSION_FEATURES;
653 /* Save time if the version is already set. */
654 if (ver == spa_version(spa))
658 * In addition to the pool directory object, we might
659 * create the pool properties object, the features for
660 * read object, the features for write object, or the
661 * feature descriptions object.
663 error = dsl_sync_task_do(spa_get_dsl(spa), NULL,
664 spa_sync_version, spa, &ver, 6);
675 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
683 * If the bootfs property value is dsobj, clear it.
686 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
688 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
689 VERIFY(zap_remove(spa->spa_meta_objset,
690 spa->spa_pool_props_object,
691 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
698 spa_change_guid_check(void *arg1, void *arg2, dmu_tx_t *tx)
701 vdev_t *rvd = spa->spa_root_vdev;
703 ASSERTV(uint64_t *newguid = arg2);
705 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
706 vdev_state = rvd->vdev_state;
707 spa_config_exit(spa, SCL_STATE, FTAG);
709 if (vdev_state != VDEV_STATE_HEALTHY)
712 ASSERT3U(spa_guid(spa), !=, *newguid);
718 spa_change_guid_sync(void *arg1, void *arg2, dmu_tx_t *tx)
721 uint64_t *newguid = arg2;
723 vdev_t *rvd = spa->spa_root_vdev;
725 oldguid = spa_guid(spa);
727 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
728 rvd->vdev_guid = *newguid;
729 rvd->vdev_guid_sum += (*newguid - oldguid);
730 vdev_config_dirty(rvd);
731 spa_config_exit(spa, SCL_STATE, FTAG);
733 spa_history_log_internal(LOG_POOL_GUID_CHANGE, spa, tx,
734 "old=%lld new=%lld", oldguid, *newguid);
738 * Change the GUID for the pool. This is done so that we can later
739 * re-import a pool built from a clone of our own vdevs. We will modify
740 * the root vdev's guid, our own pool guid, and then mark all of our
741 * vdevs dirty. Note that we must make sure that all our vdevs are
742 * online when we do this, or else any vdevs that weren't present
743 * would be orphaned from our pool. We are also going to issue a
744 * sysevent to update any watchers.
747 spa_change_guid(spa_t *spa)
752 mutex_enter(&spa_namespace_lock);
753 guid = spa_generate_guid(NULL);
755 error = dsl_sync_task_do(spa_get_dsl(spa), spa_change_guid_check,
756 spa_change_guid_sync, spa, &guid, 5);
759 spa_config_sync(spa, B_FALSE, B_TRUE);
760 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_REGUID);
763 mutex_exit(&spa_namespace_lock);
769 * ==========================================================================
770 * SPA state manipulation (open/create/destroy/import/export)
771 * ==========================================================================
775 spa_error_entry_compare(const void *a, const void *b)
777 spa_error_entry_t *sa = (spa_error_entry_t *)a;
778 spa_error_entry_t *sb = (spa_error_entry_t *)b;
781 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
782 sizeof (zbookmark_t));
793 * Utility function which retrieves copies of the current logs and
794 * re-initializes them in the process.
797 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
799 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
801 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
802 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
804 avl_create(&spa->spa_errlist_scrub,
805 spa_error_entry_compare, sizeof (spa_error_entry_t),
806 offsetof(spa_error_entry_t, se_avl));
807 avl_create(&spa->spa_errlist_last,
808 spa_error_entry_compare, sizeof (spa_error_entry_t),
809 offsetof(spa_error_entry_t, se_avl));
813 spa_taskqs_init(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
815 const zio_taskq_info_t *ztip = &zio_taskqs[t][q];
816 enum zti_modes mode = ztip->zti_mode;
817 uint_t value = ztip->zti_value;
818 uint_t count = ztip->zti_count;
819 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
822 boolean_t batch = B_FALSE;
824 if (mode == ZTI_MODE_NULL) {
826 tqs->stqs_taskq = NULL;
830 ASSERT3U(count, >, 0);
832 tqs->stqs_count = count;
833 tqs->stqs_taskq = kmem_alloc(count * sizeof (taskq_t *), KM_SLEEP);
835 for (i = 0; i < count; i++) {
840 ASSERT3U(value, >=, 1);
841 value = MAX(value, 1);
846 flags |= TASKQ_THREADS_CPU_PCT;
847 value = zio_taskq_batch_pct;
850 case ZTI_MODE_ONLINE_PERCENT:
851 flags |= TASKQ_THREADS_CPU_PCT;
855 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
857 zio_type_name[t], zio_taskq_types[q], mode, value);
862 (void) snprintf(name, sizeof (name), "%s_%s_%u",
863 zio_type_name[t], zio_taskq_types[q], i);
865 (void) snprintf(name, sizeof (name), "%s_%s",
866 zio_type_name[t], zio_taskq_types[q]);
869 if (zio_taskq_sysdc && spa->spa_proc != &p0) {
871 flags |= TASKQ_DC_BATCH;
873 tq = taskq_create_sysdc(name, value, 50, INT_MAX,
874 spa->spa_proc, zio_taskq_basedc, flags);
876 tq = taskq_create_proc(name, value, maxclsyspri, 50,
877 INT_MAX, spa->spa_proc, flags);
880 tqs->stqs_taskq[i] = tq;
885 spa_taskqs_fini(spa_t *spa, zio_type_t t, zio_taskq_type_t q)
887 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
890 if (tqs->stqs_taskq == NULL) {
891 ASSERT3U(tqs->stqs_count, ==, 0);
895 for (i = 0; i < tqs->stqs_count; i++) {
896 ASSERT3P(tqs->stqs_taskq[i], !=, NULL);
897 taskq_destroy(tqs->stqs_taskq[i]);
900 kmem_free(tqs->stqs_taskq, tqs->stqs_count * sizeof (taskq_t *));
901 tqs->stqs_taskq = NULL;
905 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
906 * Note that a type may have multiple discrete taskqs to avoid lock contention
907 * on the taskq itself. In that case we choose which taskq at random by using
908 * the low bits of gethrtime().
911 spa_taskq_dispatch_ent(spa_t *spa, zio_type_t t, zio_taskq_type_t q,
912 task_func_t *func, void *arg, uint_t flags, taskq_ent_t *ent)
914 spa_taskqs_t *tqs = &spa->spa_zio_taskq[t][q];
917 ASSERT3P(tqs->stqs_taskq, !=, NULL);
918 ASSERT3U(tqs->stqs_count, !=, 0);
920 if (tqs->stqs_count == 1) {
921 tq = tqs->stqs_taskq[0];
923 tq = tqs->stqs_taskq[gethrtime() % tqs->stqs_count];
926 taskq_dispatch_ent(tq, func, arg, flags, ent);
930 spa_create_zio_taskqs(spa_t *spa)
934 for (t = 0; t < ZIO_TYPES; t++) {
935 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
936 spa_taskqs_init(spa, t, q);
941 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
943 spa_thread(void *arg)
948 user_t *pu = PTOU(curproc);
950 CALLB_CPR_INIT(&cprinfo, &spa->spa_proc_lock, callb_generic_cpr,
953 ASSERT(curproc != &p0);
954 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs),
955 "zpool-%s", spa->spa_name);
956 (void) strlcpy(pu->u_comm, pu->u_psargs, sizeof (pu->u_comm));
958 /* bind this thread to the requested psrset */
959 if (zio_taskq_psrset_bind != PS_NONE) {
961 mutex_enter(&cpu_lock);
962 mutex_enter(&pidlock);
963 mutex_enter(&curproc->p_lock);
965 if (cpupart_bind_thread(curthread, zio_taskq_psrset_bind,
966 0, NULL, NULL) == 0) {
967 curthread->t_bind_pset = zio_taskq_psrset_bind;
970 "Couldn't bind process for zfs pool \"%s\" to "
971 "pset %d\n", spa->spa_name, zio_taskq_psrset_bind);
974 mutex_exit(&curproc->p_lock);
975 mutex_exit(&pidlock);
976 mutex_exit(&cpu_lock);
980 if (zio_taskq_sysdc) {
981 sysdc_thread_enter(curthread, 100, 0);
984 spa->spa_proc = curproc;
985 spa->spa_did = curthread->t_did;
987 spa_create_zio_taskqs(spa);
989 mutex_enter(&spa->spa_proc_lock);
990 ASSERT(spa->spa_proc_state == SPA_PROC_CREATED);
992 spa->spa_proc_state = SPA_PROC_ACTIVE;
993 cv_broadcast(&spa->spa_proc_cv);
995 CALLB_CPR_SAFE_BEGIN(&cprinfo);
996 while (spa->spa_proc_state == SPA_PROC_ACTIVE)
997 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
998 CALLB_CPR_SAFE_END(&cprinfo, &spa->spa_proc_lock);
1000 ASSERT(spa->spa_proc_state == SPA_PROC_DEACTIVATE);
1001 spa->spa_proc_state = SPA_PROC_GONE;
1002 spa->spa_proc = &p0;
1003 cv_broadcast(&spa->spa_proc_cv);
1004 CALLB_CPR_EXIT(&cprinfo); /* drops spa_proc_lock */
1006 mutex_enter(&curproc->p_lock);
1012 * Activate an uninitialized pool.
1015 spa_activate(spa_t *spa, int mode)
1017 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
1019 spa->spa_state = POOL_STATE_ACTIVE;
1020 spa->spa_mode = mode;
1022 spa->spa_normal_class = metaslab_class_create(spa, zfs_metaslab_ops);
1023 spa->spa_log_class = metaslab_class_create(spa, zfs_metaslab_ops);
1025 /* Try to create a covering process */
1026 mutex_enter(&spa->spa_proc_lock);
1027 ASSERT(spa->spa_proc_state == SPA_PROC_NONE);
1028 ASSERT(spa->spa_proc == &p0);
1031 #ifdef HAVE_SPA_THREAD
1032 /* Only create a process if we're going to be around a while. */
1033 if (spa_create_process && strcmp(spa->spa_name, TRYIMPORT_NAME) != 0) {
1034 if (newproc(spa_thread, (caddr_t)spa, syscid, maxclsyspri,
1036 spa->spa_proc_state = SPA_PROC_CREATED;
1037 while (spa->spa_proc_state == SPA_PROC_CREATED) {
1038 cv_wait(&spa->spa_proc_cv,
1039 &spa->spa_proc_lock);
1041 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1042 ASSERT(spa->spa_proc != &p0);
1043 ASSERT(spa->spa_did != 0);
1047 "Couldn't create process for zfs pool \"%s\"\n",
1052 #endif /* HAVE_SPA_THREAD */
1053 mutex_exit(&spa->spa_proc_lock);
1055 /* If we didn't create a process, we need to create our taskqs. */
1056 if (spa->spa_proc == &p0) {
1057 spa_create_zio_taskqs(spa);
1060 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
1061 offsetof(vdev_t, vdev_config_dirty_node));
1062 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
1063 offsetof(vdev_t, vdev_state_dirty_node));
1065 txg_list_create(&spa->spa_vdev_txg_list,
1066 offsetof(struct vdev, vdev_txg_node));
1068 avl_create(&spa->spa_errlist_scrub,
1069 spa_error_entry_compare, sizeof (spa_error_entry_t),
1070 offsetof(spa_error_entry_t, se_avl));
1071 avl_create(&spa->spa_errlist_last,
1072 spa_error_entry_compare, sizeof (spa_error_entry_t),
1073 offsetof(spa_error_entry_t, se_avl));
1077 * Opposite of spa_activate().
1080 spa_deactivate(spa_t *spa)
1084 ASSERT(spa->spa_sync_on == B_FALSE);
1085 ASSERT(spa->spa_dsl_pool == NULL);
1086 ASSERT(spa->spa_root_vdev == NULL);
1087 ASSERT(spa->spa_async_zio_root == NULL);
1088 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
1090 txg_list_destroy(&spa->spa_vdev_txg_list);
1092 list_destroy(&spa->spa_config_dirty_list);
1093 list_destroy(&spa->spa_state_dirty_list);
1095 taskq_cancel_id(system_taskq, spa->spa_deadman_tqid);
1097 for (t = 0; t < ZIO_TYPES; t++) {
1098 for (q = 0; q < ZIO_TASKQ_TYPES; q++) {
1099 spa_taskqs_fini(spa, t, q);
1103 metaslab_class_destroy(spa->spa_normal_class);
1104 spa->spa_normal_class = NULL;
1106 metaslab_class_destroy(spa->spa_log_class);
1107 spa->spa_log_class = NULL;
1110 * If this was part of an import or the open otherwise failed, we may
1111 * still have errors left in the queues. Empty them just in case.
1113 spa_errlog_drain(spa);
1115 avl_destroy(&spa->spa_errlist_scrub);
1116 avl_destroy(&spa->spa_errlist_last);
1118 spa->spa_state = POOL_STATE_UNINITIALIZED;
1120 mutex_enter(&spa->spa_proc_lock);
1121 if (spa->spa_proc_state != SPA_PROC_NONE) {
1122 ASSERT(spa->spa_proc_state == SPA_PROC_ACTIVE);
1123 spa->spa_proc_state = SPA_PROC_DEACTIVATE;
1124 cv_broadcast(&spa->spa_proc_cv);
1125 while (spa->spa_proc_state == SPA_PROC_DEACTIVATE) {
1126 ASSERT(spa->spa_proc != &p0);
1127 cv_wait(&spa->spa_proc_cv, &spa->spa_proc_lock);
1129 ASSERT(spa->spa_proc_state == SPA_PROC_GONE);
1130 spa->spa_proc_state = SPA_PROC_NONE;
1132 ASSERT(spa->spa_proc == &p0);
1133 mutex_exit(&spa->spa_proc_lock);
1136 * We want to make sure spa_thread() has actually exited the ZFS
1137 * module, so that the module can't be unloaded out from underneath
1140 if (spa->spa_did != 0) {
1141 thread_join(spa->spa_did);
1147 * Verify a pool configuration, and construct the vdev tree appropriately. This
1148 * will create all the necessary vdevs in the appropriate layout, with each vdev
1149 * in the CLOSED state. This will prep the pool before open/creation/import.
1150 * All vdev validation is done by the vdev_alloc() routine.
1153 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
1154 uint_t id, int atype)
1161 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
1164 if ((*vdp)->vdev_ops->vdev_op_leaf)
1167 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1170 if (error == ENOENT)
1179 for (c = 0; c < children; c++) {
1181 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
1189 ASSERT(*vdp != NULL);
1195 * Opposite of spa_load().
1198 spa_unload(spa_t *spa)
1202 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1207 spa_async_suspend(spa);
1212 if (spa->spa_sync_on) {
1213 txg_sync_stop(spa->spa_dsl_pool);
1214 spa->spa_sync_on = B_FALSE;
1218 * Wait for any outstanding async I/O to complete.
1220 if (spa->spa_async_zio_root != NULL) {
1221 (void) zio_wait(spa->spa_async_zio_root);
1222 spa->spa_async_zio_root = NULL;
1225 bpobj_close(&spa->spa_deferred_bpobj);
1228 * Close the dsl pool.
1230 if (spa->spa_dsl_pool) {
1231 dsl_pool_close(spa->spa_dsl_pool);
1232 spa->spa_dsl_pool = NULL;
1233 spa->spa_meta_objset = NULL;
1238 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1241 * Drop and purge level 2 cache
1243 spa_l2cache_drop(spa);
1248 if (spa->spa_root_vdev)
1249 vdev_free(spa->spa_root_vdev);
1250 ASSERT(spa->spa_root_vdev == NULL);
1252 for (i = 0; i < spa->spa_spares.sav_count; i++)
1253 vdev_free(spa->spa_spares.sav_vdevs[i]);
1254 if (spa->spa_spares.sav_vdevs) {
1255 kmem_free(spa->spa_spares.sav_vdevs,
1256 spa->spa_spares.sav_count * sizeof (void *));
1257 spa->spa_spares.sav_vdevs = NULL;
1259 if (spa->spa_spares.sav_config) {
1260 nvlist_free(spa->spa_spares.sav_config);
1261 spa->spa_spares.sav_config = NULL;
1263 spa->spa_spares.sav_count = 0;
1265 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
1266 vdev_clear_stats(spa->spa_l2cache.sav_vdevs[i]);
1267 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
1269 if (spa->spa_l2cache.sav_vdevs) {
1270 kmem_free(spa->spa_l2cache.sav_vdevs,
1271 spa->spa_l2cache.sav_count * sizeof (void *));
1272 spa->spa_l2cache.sav_vdevs = NULL;
1274 if (spa->spa_l2cache.sav_config) {
1275 nvlist_free(spa->spa_l2cache.sav_config);
1276 spa->spa_l2cache.sav_config = NULL;
1278 spa->spa_l2cache.sav_count = 0;
1280 spa->spa_async_suspended = 0;
1282 if (spa->spa_comment != NULL) {
1283 spa_strfree(spa->spa_comment);
1284 spa->spa_comment = NULL;
1287 spa_config_exit(spa, SCL_ALL, FTAG);
1291 * Load (or re-load) the current list of vdevs describing the active spares for
1292 * this pool. When this is called, we have some form of basic information in
1293 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1294 * then re-generate a more complete list including status information.
1297 spa_load_spares(spa_t *spa)
1304 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1307 * First, close and free any existing spare vdevs.
1309 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1310 vd = spa->spa_spares.sav_vdevs[i];
1312 /* Undo the call to spa_activate() below */
1313 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1314 B_FALSE)) != NULL && tvd->vdev_isspare)
1315 spa_spare_remove(tvd);
1320 if (spa->spa_spares.sav_vdevs)
1321 kmem_free(spa->spa_spares.sav_vdevs,
1322 spa->spa_spares.sav_count * sizeof (void *));
1324 if (spa->spa_spares.sav_config == NULL)
1327 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1328 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1330 spa->spa_spares.sav_count = (int)nspares;
1331 spa->spa_spares.sav_vdevs = NULL;
1337 * Construct the array of vdevs, opening them to get status in the
1338 * process. For each spare, there is potentially two different vdev_t
1339 * structures associated with it: one in the list of spares (used only
1340 * for basic validation purposes) and one in the active vdev
1341 * configuration (if it's spared in). During this phase we open and
1342 * validate each vdev on the spare list. If the vdev also exists in the
1343 * active configuration, then we also mark this vdev as an active spare.
1345 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
1347 for (i = 0; i < spa->spa_spares.sav_count; i++) {
1348 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
1349 VDEV_ALLOC_SPARE) == 0);
1352 spa->spa_spares.sav_vdevs[i] = vd;
1354 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
1355 B_FALSE)) != NULL) {
1356 if (!tvd->vdev_isspare)
1360 * We only mark the spare active if we were successfully
1361 * able to load the vdev. Otherwise, importing a pool
1362 * with a bad active spare would result in strange
1363 * behavior, because multiple pool would think the spare
1364 * is actively in use.
1366 * There is a vulnerability here to an equally bizarre
1367 * circumstance, where a dead active spare is later
1368 * brought back to life (onlined or otherwise). Given
1369 * the rarity of this scenario, and the extra complexity
1370 * it adds, we ignore the possibility.
1372 if (!vdev_is_dead(tvd))
1373 spa_spare_activate(tvd);
1377 vd->vdev_aux = &spa->spa_spares;
1379 if (vdev_open(vd) != 0)
1382 if (vdev_validate_aux(vd) == 0)
1387 * Recompute the stashed list of spares, with status information
1390 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
1391 DATA_TYPE_NVLIST_ARRAY) == 0);
1393 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
1395 for (i = 0; i < spa->spa_spares.sav_count; i++)
1396 spares[i] = vdev_config_generate(spa,
1397 spa->spa_spares.sav_vdevs[i], B_TRUE, VDEV_CONFIG_SPARE);
1398 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1399 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
1400 for (i = 0; i < spa->spa_spares.sav_count; i++)
1401 nvlist_free(spares[i]);
1402 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
1406 * Load (or re-load) the current list of vdevs describing the active l2cache for
1407 * this pool. When this is called, we have some form of basic information in
1408 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1409 * then re-generate a more complete list including status information.
1410 * Devices which are already active have their details maintained, and are
1414 spa_load_l2cache(spa_t *spa)
1418 int i, j, oldnvdevs;
1420 vdev_t *vd, **oldvdevs, **newvdevs = NULL;
1421 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1423 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1425 if (sav->sav_config != NULL) {
1426 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
1427 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1428 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_PUSHPAGE);
1433 oldvdevs = sav->sav_vdevs;
1434 oldnvdevs = sav->sav_count;
1435 sav->sav_vdevs = NULL;
1439 * Process new nvlist of vdevs.
1441 for (i = 0; i < nl2cache; i++) {
1442 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
1446 for (j = 0; j < oldnvdevs; j++) {
1448 if (vd != NULL && guid == vd->vdev_guid) {
1450 * Retain previous vdev for add/remove ops.
1458 if (newvdevs[i] == NULL) {
1462 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
1463 VDEV_ALLOC_L2CACHE) == 0);
1468 * Commit this vdev as an l2cache device,
1469 * even if it fails to open.
1471 spa_l2cache_add(vd);
1476 spa_l2cache_activate(vd);
1478 if (vdev_open(vd) != 0)
1481 (void) vdev_validate_aux(vd);
1483 if (!vdev_is_dead(vd))
1484 l2arc_add_vdev(spa, vd);
1489 * Purge vdevs that were dropped
1491 for (i = 0; i < oldnvdevs; i++) {
1496 ASSERT(vd->vdev_isl2cache);
1498 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1499 pool != 0ULL && l2arc_vdev_present(vd))
1500 l2arc_remove_vdev(vd);
1501 vdev_clear_stats(vd);
1507 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
1509 if (sav->sav_config == NULL)
1512 sav->sav_vdevs = newvdevs;
1513 sav->sav_count = (int)nl2cache;
1516 * Recompute the stashed list of l2cache devices, with status
1517 * information this time.
1519 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
1520 DATA_TYPE_NVLIST_ARRAY) == 0);
1522 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE);
1523 for (i = 0; i < sav->sav_count; i++)
1524 l2cache[i] = vdev_config_generate(spa,
1525 sav->sav_vdevs[i], B_TRUE, VDEV_CONFIG_L2CACHE);
1526 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1527 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
1529 for (i = 0; i < sav->sav_count; i++)
1530 nvlist_free(l2cache[i]);
1532 kmem_free(l2cache, sav->sav_count * sizeof (void *));
1536 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
1539 char *packed = NULL;
1544 error = dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db);
1548 nvsize = *(uint64_t *)db->db_data;
1549 dmu_buf_rele(db, FTAG);
1551 packed = kmem_alloc(nvsize, KM_PUSHPAGE | KM_NODEBUG);
1552 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed,
1555 error = nvlist_unpack(packed, nvsize, value, 0);
1556 kmem_free(packed, nvsize);
1562 * Checks to see if the given vdev could not be opened, in which case we post a
1563 * sysevent to notify the autoreplace code that the device has been removed.
1566 spa_check_removed(vdev_t *vd)
1570 for (c = 0; c < vd->vdev_children; c++)
1571 spa_check_removed(vd->vdev_child[c]);
1573 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
1574 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE,
1575 vd->vdev_spa, vd, NULL, 0, 0);
1576 spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_CHECK);
1581 * Validate the current config against the MOS config
1584 spa_config_valid(spa_t *spa, nvlist_t *config)
1586 vdev_t *mrvd, *rvd = spa->spa_root_vdev;
1590 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nv) == 0);
1592 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1593 VERIFY(spa_config_parse(spa, &mrvd, nv, NULL, 0, VDEV_ALLOC_LOAD) == 0);
1595 ASSERT3U(rvd->vdev_children, ==, mrvd->vdev_children);
1598 * If we're doing a normal import, then build up any additional
1599 * diagnostic information about missing devices in this config.
1600 * We'll pass this up to the user for further processing.
1602 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG)) {
1603 nvlist_t **child, *nv;
1606 child = kmem_alloc(rvd->vdev_children * sizeof (nvlist_t **),
1608 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
1610 for (c = 0; c < rvd->vdev_children; c++) {
1611 vdev_t *tvd = rvd->vdev_child[c];
1612 vdev_t *mtvd = mrvd->vdev_child[c];
1614 if (tvd->vdev_ops == &vdev_missing_ops &&
1615 mtvd->vdev_ops != &vdev_missing_ops &&
1617 child[idx++] = vdev_config_generate(spa, mtvd,
1622 VERIFY(nvlist_add_nvlist_array(nv,
1623 ZPOOL_CONFIG_CHILDREN, child, idx) == 0);
1624 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
1625 ZPOOL_CONFIG_MISSING_DEVICES, nv) == 0);
1627 for (i = 0; i < idx; i++)
1628 nvlist_free(child[i]);
1631 kmem_free(child, rvd->vdev_children * sizeof (char **));
1635 * Compare the root vdev tree with the information we have
1636 * from the MOS config (mrvd). Check each top-level vdev
1637 * with the corresponding MOS config top-level (mtvd).
1639 for (c = 0; c < rvd->vdev_children; c++) {
1640 vdev_t *tvd = rvd->vdev_child[c];
1641 vdev_t *mtvd = mrvd->vdev_child[c];
1644 * Resolve any "missing" vdevs in the current configuration.
1645 * If we find that the MOS config has more accurate information
1646 * about the top-level vdev then use that vdev instead.
1648 if (tvd->vdev_ops == &vdev_missing_ops &&
1649 mtvd->vdev_ops != &vdev_missing_ops) {
1651 if (!(spa->spa_import_flags & ZFS_IMPORT_MISSING_LOG))
1655 * Device specific actions.
1657 if (mtvd->vdev_islog) {
1658 spa_set_log_state(spa, SPA_LOG_CLEAR);
1661 * XXX - once we have 'readonly' pool
1662 * support we should be able to handle
1663 * missing data devices by transitioning
1664 * the pool to readonly.
1670 * Swap the missing vdev with the data we were
1671 * able to obtain from the MOS config.
1673 vdev_remove_child(rvd, tvd);
1674 vdev_remove_child(mrvd, mtvd);
1676 vdev_add_child(rvd, mtvd);
1677 vdev_add_child(mrvd, tvd);
1679 spa_config_exit(spa, SCL_ALL, FTAG);
1681 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1684 } else if (mtvd->vdev_islog) {
1686 * Load the slog device's state from the MOS config
1687 * since it's possible that the label does not
1688 * contain the most up-to-date information.
1690 vdev_load_log_state(tvd, mtvd);
1695 spa_config_exit(spa, SCL_ALL, FTAG);
1698 * Ensure we were able to validate the config.
1700 return (rvd->vdev_guid_sum == spa->spa_uberblock.ub_guid_sum);
1704 * Check for missing log devices
1707 spa_check_logs(spa_t *spa)
1709 switch (spa->spa_log_state) {
1712 case SPA_LOG_MISSING:
1713 /* need to recheck in case slog has been restored */
1714 case SPA_LOG_UNKNOWN:
1715 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
1716 DS_FIND_CHILDREN)) {
1717 spa_set_log_state(spa, SPA_LOG_MISSING);
1726 spa_passivate_log(spa_t *spa)
1728 vdev_t *rvd = spa->spa_root_vdev;
1729 boolean_t slog_found = B_FALSE;
1732 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1734 if (!spa_has_slogs(spa))
1737 for (c = 0; c < rvd->vdev_children; c++) {
1738 vdev_t *tvd = rvd->vdev_child[c];
1739 metaslab_group_t *mg = tvd->vdev_mg;
1741 if (tvd->vdev_islog) {
1742 metaslab_group_passivate(mg);
1743 slog_found = B_TRUE;
1747 return (slog_found);
1751 spa_activate_log(spa_t *spa)
1753 vdev_t *rvd = spa->spa_root_vdev;
1756 ASSERT(spa_config_held(spa, SCL_ALLOC, RW_WRITER));
1758 for (c = 0; c < rvd->vdev_children; c++) {
1759 vdev_t *tvd = rvd->vdev_child[c];
1760 metaslab_group_t *mg = tvd->vdev_mg;
1762 if (tvd->vdev_islog)
1763 metaslab_group_activate(mg);
1768 spa_offline_log(spa_t *spa)
1772 if ((error = dmu_objset_find(spa_name(spa), zil_vdev_offline,
1773 NULL, DS_FIND_CHILDREN)) == 0) {
1776 * We successfully offlined the log device, sync out the
1777 * current txg so that the "stubby" block can be removed
1780 txg_wait_synced(spa->spa_dsl_pool, 0);
1786 spa_aux_check_removed(spa_aux_vdev_t *sav)
1790 for (i = 0; i < sav->sav_count; i++)
1791 spa_check_removed(sav->sav_vdevs[i]);
1795 spa_claim_notify(zio_t *zio)
1797 spa_t *spa = zio->io_spa;
1802 mutex_enter(&spa->spa_props_lock); /* any mutex will do */
1803 if (spa->spa_claim_max_txg < zio->io_bp->blk_birth)
1804 spa->spa_claim_max_txg = zio->io_bp->blk_birth;
1805 mutex_exit(&spa->spa_props_lock);
1808 typedef struct spa_load_error {
1809 uint64_t sle_meta_count;
1810 uint64_t sle_data_count;
1814 spa_load_verify_done(zio_t *zio)
1816 blkptr_t *bp = zio->io_bp;
1817 spa_load_error_t *sle = zio->io_private;
1818 dmu_object_type_t type = BP_GET_TYPE(bp);
1819 int error = zio->io_error;
1822 if ((BP_GET_LEVEL(bp) != 0 || DMU_OT_IS_METADATA(type)) &&
1823 type != DMU_OT_INTENT_LOG)
1824 atomic_add_64(&sle->sle_meta_count, 1);
1826 atomic_add_64(&sle->sle_data_count, 1);
1828 zio_data_buf_free(zio->io_data, zio->io_size);
1833 spa_load_verify_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1834 arc_buf_t *pbuf, const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
1838 size_t size = BP_GET_PSIZE(bp);
1839 void *data = zio_data_buf_alloc(size);
1841 zio_nowait(zio_read(rio, spa, bp, data, size,
1842 spa_load_verify_done, rio->io_private, ZIO_PRIORITY_SCRUB,
1843 ZIO_FLAG_SPECULATIVE | ZIO_FLAG_CANFAIL |
1844 ZIO_FLAG_SCRUB | ZIO_FLAG_RAW, zb));
1850 spa_load_verify(spa_t *spa)
1853 spa_load_error_t sle = { 0 };
1854 zpool_rewind_policy_t policy;
1855 boolean_t verify_ok = B_FALSE;
1858 zpool_get_rewind_policy(spa->spa_config, &policy);
1860 if (policy.zrp_request & ZPOOL_NEVER_REWIND)
1863 rio = zio_root(spa, NULL, &sle,
1864 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE);
1866 error = traverse_pool(spa, spa->spa_verify_min_txg,
1867 TRAVERSE_PRE | TRAVERSE_PREFETCH, spa_load_verify_cb, rio);
1869 (void) zio_wait(rio);
1871 spa->spa_load_meta_errors = sle.sle_meta_count;
1872 spa->spa_load_data_errors = sle.sle_data_count;
1874 if (!error && sle.sle_meta_count <= policy.zrp_maxmeta &&
1875 sle.sle_data_count <= policy.zrp_maxdata) {
1879 spa->spa_load_txg = spa->spa_uberblock.ub_txg;
1880 spa->spa_load_txg_ts = spa->spa_uberblock.ub_timestamp;
1882 loss = spa->spa_last_ubsync_txg_ts - spa->spa_load_txg_ts;
1883 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1884 ZPOOL_CONFIG_LOAD_TIME, spa->spa_load_txg_ts) == 0);
1885 VERIFY(nvlist_add_int64(spa->spa_load_info,
1886 ZPOOL_CONFIG_REWIND_TIME, loss) == 0);
1887 VERIFY(nvlist_add_uint64(spa->spa_load_info,
1888 ZPOOL_CONFIG_LOAD_DATA_ERRORS, sle.sle_data_count) == 0);
1890 spa->spa_load_max_txg = spa->spa_uberblock.ub_txg;
1894 if (error != ENXIO && error != EIO)
1899 return (verify_ok ? 0 : EIO);
1903 * Find a value in the pool props object.
1906 spa_prop_find(spa_t *spa, zpool_prop_t prop, uint64_t *val)
1908 (void) zap_lookup(spa->spa_meta_objset, spa->spa_pool_props_object,
1909 zpool_prop_to_name(prop), sizeof (uint64_t), 1, val);
1913 * Find a value in the pool directory object.
1916 spa_dir_prop(spa_t *spa, const char *name, uint64_t *val)
1918 return (zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1919 name, sizeof (uint64_t), 1, val));
1923 spa_vdev_err(vdev_t *vdev, vdev_aux_t aux, int err)
1925 vdev_set_state(vdev, B_TRUE, VDEV_STATE_CANT_OPEN, aux);
1930 * Fix up config after a partly-completed split. This is done with the
1931 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1932 * pool have that entry in their config, but only the splitting one contains
1933 * a list of all the guids of the vdevs that are being split off.
1935 * This function determines what to do with that list: either rejoin
1936 * all the disks to the pool, or complete the splitting process. To attempt
1937 * the rejoin, each disk that is offlined is marked online again, and
1938 * we do a reopen() call. If the vdev label for every disk that was
1939 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1940 * then we call vdev_split() on each disk, and complete the split.
1942 * Otherwise we leave the config alone, with all the vdevs in place in
1943 * the original pool.
1946 spa_try_repair(spa_t *spa, nvlist_t *config)
1953 boolean_t attempt_reopen;
1955 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT, &nvl) != 0)
1958 /* check that the config is complete */
1959 if (nvlist_lookup_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
1960 &glist, &gcount) != 0)
1963 vd = kmem_zalloc(gcount * sizeof (vdev_t *), KM_PUSHPAGE);
1965 /* attempt to online all the vdevs & validate */
1966 attempt_reopen = B_TRUE;
1967 for (i = 0; i < gcount; i++) {
1968 if (glist[i] == 0) /* vdev is hole */
1971 vd[i] = spa_lookup_by_guid(spa, glist[i], B_FALSE);
1972 if (vd[i] == NULL) {
1974 * Don't bother attempting to reopen the disks;
1975 * just do the split.
1977 attempt_reopen = B_FALSE;
1979 /* attempt to re-online it */
1980 vd[i]->vdev_offline = B_FALSE;
1984 if (attempt_reopen) {
1985 vdev_reopen(spa->spa_root_vdev);
1987 /* check each device to see what state it's in */
1988 for (extracted = 0, i = 0; i < gcount; i++) {
1989 if (vd[i] != NULL &&
1990 vd[i]->vdev_stat.vs_aux != VDEV_AUX_SPLIT_POOL)
1997 * If every disk has been moved to the new pool, or if we never
1998 * even attempted to look at them, then we split them off for
2001 if (!attempt_reopen || gcount == extracted) {
2002 for (i = 0; i < gcount; i++)
2005 vdev_reopen(spa->spa_root_vdev);
2008 kmem_free(vd, gcount * sizeof (vdev_t *));
2012 spa_load(spa_t *spa, spa_load_state_t state, spa_import_type_t type,
2013 boolean_t mosconfig)
2015 nvlist_t *config = spa->spa_config;
2016 char *ereport = FM_EREPORT_ZFS_POOL;
2022 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid))
2025 ASSERT(spa->spa_comment == NULL);
2026 if (nvlist_lookup_string(config, ZPOOL_CONFIG_COMMENT, &comment) == 0)
2027 spa->spa_comment = spa_strdup(comment);
2030 * Versioning wasn't explicitly added to the label until later, so if
2031 * it's not present treat it as the initial version.
2033 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2034 &spa->spa_ubsync.ub_version) != 0)
2035 spa->spa_ubsync.ub_version = SPA_VERSION_INITIAL;
2037 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
2038 &spa->spa_config_txg);
2040 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
2041 spa_guid_exists(pool_guid, 0)) {
2044 spa->spa_config_guid = pool_guid;
2046 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_SPLIT,
2048 VERIFY(nvlist_dup(nvl, &spa->spa_config_splitting,
2052 nvlist_free(spa->spa_load_info);
2053 spa->spa_load_info = fnvlist_alloc();
2055 gethrestime(&spa->spa_loaded_ts);
2056 error = spa_load_impl(spa, pool_guid, config, state, type,
2057 mosconfig, &ereport);
2060 spa->spa_minref = refcount_count(&spa->spa_refcount);
2062 if (error != EEXIST) {
2063 spa->spa_loaded_ts.tv_sec = 0;
2064 spa->spa_loaded_ts.tv_nsec = 0;
2066 if (error != EBADF) {
2067 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
2070 spa->spa_load_state = error ? SPA_LOAD_ERROR : SPA_LOAD_NONE;
2077 * Load an existing storage pool, using the pool's builtin spa_config as a
2078 * source of configuration information.
2080 __attribute__((always_inline))
2082 spa_load_impl(spa_t *spa, uint64_t pool_guid, nvlist_t *config,
2083 spa_load_state_t state, spa_import_type_t type, boolean_t mosconfig,
2087 nvlist_t *nvroot = NULL;
2090 uberblock_t *ub = &spa->spa_uberblock;
2091 uint64_t children, config_cache_txg = spa->spa_config_txg;
2092 int orig_mode = spa->spa_mode;
2095 boolean_t missing_feat_write = B_FALSE;
2098 * If this is an untrusted config, access the pool in read-only mode.
2099 * This prevents things like resilvering recently removed devices.
2102 spa->spa_mode = FREAD;
2104 ASSERT(MUTEX_HELD(&spa_namespace_lock));
2106 spa->spa_load_state = state;
2108 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot))
2111 parse = (type == SPA_IMPORT_EXISTING ?
2112 VDEV_ALLOC_LOAD : VDEV_ALLOC_SPLIT);
2115 * Create "The Godfather" zio to hold all async IOs
2117 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
2118 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
2121 * Parse the configuration into a vdev tree. We explicitly set the
2122 * value that will be returned by spa_version() since parsing the
2123 * configuration requires knowing the version number.
2125 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2126 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, parse);
2127 spa_config_exit(spa, SCL_ALL, FTAG);
2132 ASSERT(spa->spa_root_vdev == rvd);
2134 if (type != SPA_IMPORT_ASSEMBLE) {
2135 ASSERT(spa_guid(spa) == pool_guid);
2139 * Try to open all vdevs, loading each label in the process.
2141 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2142 error = vdev_open(rvd);
2143 spa_config_exit(spa, SCL_ALL, FTAG);
2148 * We need to validate the vdev labels against the configuration that
2149 * we have in hand, which is dependent on the setting of mosconfig. If
2150 * mosconfig is true then we're validating the vdev labels based on
2151 * that config. Otherwise, we're validating against the cached config
2152 * (zpool.cache) that was read when we loaded the zfs module, and then
2153 * later we will recursively call spa_load() and validate against
2156 * If we're assembling a new pool that's been split off from an
2157 * existing pool, the labels haven't yet been updated so we skip
2158 * validation for now.
2160 if (type != SPA_IMPORT_ASSEMBLE) {
2161 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2162 error = vdev_validate(rvd, mosconfig);
2163 spa_config_exit(spa, SCL_ALL, FTAG);
2168 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2173 * Find the best uberblock.
2175 vdev_uberblock_load(rvd, ub, &label);
2178 * If we weren't able to find a single valid uberblock, return failure.
2180 if (ub->ub_txg == 0) {
2182 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, ENXIO));
2186 * If the pool has an unsupported version we can't open it.
2188 if (!SPA_VERSION_IS_SUPPORTED(ub->ub_version)) {
2190 return (spa_vdev_err(rvd, VDEV_AUX_VERSION_NEWER, ENOTSUP));
2193 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2197 * If we weren't able to find what's necessary for reading the
2198 * MOS in the label, return failure.
2200 if (label == NULL || nvlist_lookup_nvlist(label,
2201 ZPOOL_CONFIG_FEATURES_FOR_READ, &features) != 0) {
2203 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2208 * Update our in-core representation with the definitive values
2211 nvlist_free(spa->spa_label_features);
2212 VERIFY(nvlist_dup(features, &spa->spa_label_features, 0) == 0);
2218 * Look through entries in the label nvlist's features_for_read. If
2219 * there is a feature listed there which we don't understand then we
2220 * cannot open a pool.
2222 if (ub->ub_version >= SPA_VERSION_FEATURES) {
2223 nvlist_t *unsup_feat;
2226 VERIFY(nvlist_alloc(&unsup_feat, NV_UNIQUE_NAME, KM_SLEEP) ==
2229 for (nvp = nvlist_next_nvpair(spa->spa_label_features, NULL);
2231 nvp = nvlist_next_nvpair(spa->spa_label_features, nvp)) {
2232 if (!zfeature_is_supported(nvpair_name(nvp))) {
2233 VERIFY(nvlist_add_string(unsup_feat,
2234 nvpair_name(nvp), "") == 0);
2238 if (!nvlist_empty(unsup_feat)) {
2239 VERIFY(nvlist_add_nvlist(spa->spa_load_info,
2240 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat) == 0);
2241 nvlist_free(unsup_feat);
2242 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2246 nvlist_free(unsup_feat);
2250 * If the vdev guid sum doesn't match the uberblock, we have an
2251 * incomplete configuration. We first check to see if the pool
2252 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2253 * If it is, defer the vdev_guid_sum check till later so we
2254 * can handle missing vdevs.
2256 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VDEV_CHILDREN,
2257 &children) != 0 && mosconfig && type != SPA_IMPORT_ASSEMBLE &&
2258 rvd->vdev_guid_sum != ub->ub_guid_sum)
2259 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM, ENXIO));
2261 if (type != SPA_IMPORT_ASSEMBLE && spa->spa_config_splitting) {
2262 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2263 spa_try_repair(spa, config);
2264 spa_config_exit(spa, SCL_ALL, FTAG);
2265 nvlist_free(spa->spa_config_splitting);
2266 spa->spa_config_splitting = NULL;
2270 * Initialize internal SPA structures.
2272 spa->spa_state = POOL_STATE_ACTIVE;
2273 spa->spa_ubsync = spa->spa_uberblock;
2274 spa->spa_verify_min_txg = spa->spa_extreme_rewind ?
2275 TXG_INITIAL - 1 : spa_last_synced_txg(spa) - TXG_DEFER_SIZE - 1;
2276 spa->spa_first_txg = spa->spa_last_ubsync_txg ?
2277 spa->spa_last_ubsync_txg : spa_last_synced_txg(spa) + 1;
2278 spa->spa_claim_max_txg = spa->spa_first_txg;
2279 spa->spa_prev_software_version = ub->ub_software_version;
2281 error = dsl_pool_init(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
2283 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2284 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
2286 if (spa_dir_prop(spa, DMU_POOL_CONFIG, &spa->spa_config_object) != 0)
2287 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2289 if (spa_version(spa) >= SPA_VERSION_FEATURES) {
2290 boolean_t missing_feat_read = B_FALSE;
2291 nvlist_t *unsup_feat, *enabled_feat;
2293 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_READ,
2294 &spa->spa_feat_for_read_obj) != 0) {
2295 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2298 if (spa_dir_prop(spa, DMU_POOL_FEATURES_FOR_WRITE,
2299 &spa->spa_feat_for_write_obj) != 0) {
2300 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2303 if (spa_dir_prop(spa, DMU_POOL_FEATURE_DESCRIPTIONS,
2304 &spa->spa_feat_desc_obj) != 0) {
2305 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2308 enabled_feat = fnvlist_alloc();
2309 unsup_feat = fnvlist_alloc();
2311 if (!feature_is_supported(spa->spa_meta_objset,
2312 spa->spa_feat_for_read_obj, spa->spa_feat_desc_obj,
2313 unsup_feat, enabled_feat))
2314 missing_feat_read = B_TRUE;
2316 if (spa_writeable(spa) || state == SPA_LOAD_TRYIMPORT) {
2317 if (!feature_is_supported(spa->spa_meta_objset,
2318 spa->spa_feat_for_write_obj, spa->spa_feat_desc_obj,
2319 unsup_feat, enabled_feat)) {
2320 missing_feat_write = B_TRUE;
2324 fnvlist_add_nvlist(spa->spa_load_info,
2325 ZPOOL_CONFIG_ENABLED_FEAT, enabled_feat);
2327 if (!nvlist_empty(unsup_feat)) {
2328 fnvlist_add_nvlist(spa->spa_load_info,
2329 ZPOOL_CONFIG_UNSUP_FEAT, unsup_feat);
2332 fnvlist_free(enabled_feat);
2333 fnvlist_free(unsup_feat);
2335 if (!missing_feat_read) {
2336 fnvlist_add_boolean(spa->spa_load_info,
2337 ZPOOL_CONFIG_CAN_RDONLY);
2341 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2342 * twofold: to determine whether the pool is available for
2343 * import in read-write mode and (if it is not) whether the
2344 * pool is available for import in read-only mode. If the pool
2345 * is available for import in read-write mode, it is displayed
2346 * as available in userland; if it is not available for import
2347 * in read-only mode, it is displayed as unavailable in
2348 * userland. If the pool is available for import in read-only
2349 * mode but not read-write mode, it is displayed as unavailable
2350 * in userland with a special note that the pool is actually
2351 * available for open in read-only mode.
2353 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2354 * missing a feature for write, we must first determine whether
2355 * the pool can be opened read-only before returning to
2356 * userland in order to know whether to display the
2357 * abovementioned note.
2359 if (missing_feat_read || (missing_feat_write &&
2360 spa_writeable(spa))) {
2361 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT,
2366 spa->spa_is_initializing = B_TRUE;
2367 error = dsl_pool_open(spa->spa_dsl_pool);
2368 spa->spa_is_initializing = B_FALSE;
2370 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2374 nvlist_t *policy = NULL, *nvconfig;
2376 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2377 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2379 if (!spa_is_root(spa) && nvlist_lookup_uint64(nvconfig,
2380 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
2382 unsigned long myhostid = 0;
2384 VERIFY(nvlist_lookup_string(nvconfig,
2385 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
2388 myhostid = zone_get_hostid(NULL);
2391 * We're emulating the system's hostid in userland, so
2392 * we can't use zone_get_hostid().
2394 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
2395 #endif /* _KERNEL */
2396 if (hostid != 0 && myhostid != 0 &&
2397 hostid != myhostid) {
2398 nvlist_free(nvconfig);
2399 cmn_err(CE_WARN, "pool '%s' could not be "
2400 "loaded as it was last accessed by "
2401 "another system (host: %s hostid: 0x%lx). "
2402 "See: http://zfsonlinux.org/msg/ZFS-8000-EY",
2403 spa_name(spa), hostname,
2404 (unsigned long)hostid);
2408 if (nvlist_lookup_nvlist(spa->spa_config,
2409 ZPOOL_REWIND_POLICY, &policy) == 0)
2410 VERIFY(nvlist_add_nvlist(nvconfig,
2411 ZPOOL_REWIND_POLICY, policy) == 0);
2413 spa_config_set(spa, nvconfig);
2415 spa_deactivate(spa);
2416 spa_activate(spa, orig_mode);
2418 return (spa_load(spa, state, SPA_IMPORT_EXISTING, B_TRUE));
2421 if (spa_dir_prop(spa, DMU_POOL_SYNC_BPOBJ, &obj) != 0)
2422 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2423 error = bpobj_open(&spa->spa_deferred_bpobj, spa->spa_meta_objset, obj);
2425 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2428 * Load the bit that tells us to use the new accounting function
2429 * (raid-z deflation). If we have an older pool, this will not
2432 error = spa_dir_prop(spa, DMU_POOL_DEFLATE, &spa->spa_deflate);
2433 if (error != 0 && error != ENOENT)
2434 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2436 error = spa_dir_prop(spa, DMU_POOL_CREATION_VERSION,
2437 &spa->spa_creation_version);
2438 if (error != 0 && error != ENOENT)
2439 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2442 * Load the persistent error log. If we have an older pool, this will
2445 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_LAST, &spa->spa_errlog_last);
2446 if (error != 0 && error != ENOENT)
2447 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2449 error = spa_dir_prop(spa, DMU_POOL_ERRLOG_SCRUB,
2450 &spa->spa_errlog_scrub);
2451 if (error != 0 && error != ENOENT)
2452 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2455 * Load the history object. If we have an older pool, this
2456 * will not be present.
2458 error = spa_dir_prop(spa, DMU_POOL_HISTORY, &spa->spa_history);
2459 if (error != 0 && error != ENOENT)
2460 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2463 * If we're assembling the pool from the split-off vdevs of
2464 * an existing pool, we don't want to attach the spares & cache
2469 * Load any hot spares for this pool.
2471 error = spa_dir_prop(spa, DMU_POOL_SPARES, &spa->spa_spares.sav_object);
2472 if (error != 0 && error != ENOENT)
2473 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2474 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2475 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
2476 if (load_nvlist(spa, spa->spa_spares.sav_object,
2477 &spa->spa_spares.sav_config) != 0)
2478 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2480 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2481 spa_load_spares(spa);
2482 spa_config_exit(spa, SCL_ALL, FTAG);
2483 } else if (error == 0) {
2484 spa->spa_spares.sav_sync = B_TRUE;
2488 * Load any level 2 ARC devices for this pool.
2490 error = spa_dir_prop(spa, DMU_POOL_L2CACHE,
2491 &spa->spa_l2cache.sav_object);
2492 if (error != 0 && error != ENOENT)
2493 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2494 if (error == 0 && type != SPA_IMPORT_ASSEMBLE) {
2495 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
2496 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
2497 &spa->spa_l2cache.sav_config) != 0)
2498 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2500 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2501 spa_load_l2cache(spa);
2502 spa_config_exit(spa, SCL_ALL, FTAG);
2503 } else if (error == 0) {
2504 spa->spa_l2cache.sav_sync = B_TRUE;
2507 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2509 error = spa_dir_prop(spa, DMU_POOL_PROPS, &spa->spa_pool_props_object);
2510 if (error && error != ENOENT)
2511 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2514 uint64_t autoreplace;
2516 spa_prop_find(spa, ZPOOL_PROP_BOOTFS, &spa->spa_bootfs);
2517 spa_prop_find(spa, ZPOOL_PROP_AUTOREPLACE, &autoreplace);
2518 spa_prop_find(spa, ZPOOL_PROP_DELEGATION, &spa->spa_delegation);
2519 spa_prop_find(spa, ZPOOL_PROP_FAILUREMODE, &spa->spa_failmode);
2520 spa_prop_find(spa, ZPOOL_PROP_AUTOEXPAND, &spa->spa_autoexpand);
2521 spa_prop_find(spa, ZPOOL_PROP_DEDUPDITTO,
2522 &spa->spa_dedup_ditto);
2524 spa->spa_autoreplace = (autoreplace != 0);
2528 * If the 'autoreplace' property is set, then post a resource notifying
2529 * the ZFS DE that it should not issue any faults for unopenable
2530 * devices. We also iterate over the vdevs, and post a sysevent for any
2531 * unopenable vdevs so that the normal autoreplace handler can take
2534 if (spa->spa_autoreplace && state != SPA_LOAD_TRYIMPORT) {
2535 spa_check_removed(spa->spa_root_vdev);
2537 * For the import case, this is done in spa_import(), because
2538 * at this point we're using the spare definitions from
2539 * the MOS config, not necessarily from the userland config.
2541 if (state != SPA_LOAD_IMPORT) {
2542 spa_aux_check_removed(&spa->spa_spares);
2543 spa_aux_check_removed(&spa->spa_l2cache);
2548 * Load the vdev state for all toplevel vdevs.
2553 * Propagate the leaf DTLs we just loaded all the way up the tree.
2555 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2556 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
2557 spa_config_exit(spa, SCL_ALL, FTAG);
2560 * Load the DDTs (dedup tables).
2562 error = ddt_load(spa);
2564 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2566 spa_update_dspace(spa);
2569 * Validate the config, using the MOS config to fill in any
2570 * information which might be missing. If we fail to validate
2571 * the config then declare the pool unfit for use. If we're
2572 * assembling a pool from a split, the log is not transferred
2575 if (type != SPA_IMPORT_ASSEMBLE) {
2578 if (load_nvlist(spa, spa->spa_config_object, &nvconfig) != 0)
2579 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA, EIO));
2581 if (!spa_config_valid(spa, nvconfig)) {
2582 nvlist_free(nvconfig);
2583 return (spa_vdev_err(rvd, VDEV_AUX_BAD_GUID_SUM,
2586 nvlist_free(nvconfig);
2589 * Now that we've validated the config, check the state of the
2590 * root vdev. If it can't be opened, it indicates one or
2591 * more toplevel vdevs are faulted.
2593 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN)
2596 if (spa_check_logs(spa)) {
2597 *ereport = FM_EREPORT_ZFS_LOG_REPLAY;
2598 return (spa_vdev_err(rvd, VDEV_AUX_BAD_LOG, ENXIO));
2602 if (missing_feat_write) {
2603 ASSERT(state == SPA_LOAD_TRYIMPORT);
2606 * At this point, we know that we can open the pool in
2607 * read-only mode but not read-write mode. We now have enough
2608 * information and can return to userland.
2610 return (spa_vdev_err(rvd, VDEV_AUX_UNSUP_FEAT, ENOTSUP));
2614 * We've successfully opened the pool, verify that we're ready
2615 * to start pushing transactions.
2617 if (state != SPA_LOAD_TRYIMPORT) {
2618 if ((error = spa_load_verify(spa)))
2619 return (spa_vdev_err(rvd, VDEV_AUX_CORRUPT_DATA,
2623 if (spa_writeable(spa) && (state == SPA_LOAD_RECOVER ||
2624 spa->spa_load_max_txg == UINT64_MAX)) {
2626 int need_update = B_FALSE;
2629 ASSERT(state != SPA_LOAD_TRYIMPORT);
2632 * Claim log blocks that haven't been committed yet.
2633 * This must all happen in a single txg.
2634 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2635 * invoked from zil_claim_log_block()'s i/o done callback.
2636 * Price of rollback is that we abandon the log.
2638 spa->spa_claiming = B_TRUE;
2640 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
2641 spa_first_txg(spa));
2642 (void) dmu_objset_find(spa_name(spa),
2643 zil_claim, tx, DS_FIND_CHILDREN);
2646 spa->spa_claiming = B_FALSE;
2648 spa_set_log_state(spa, SPA_LOG_GOOD);
2649 spa->spa_sync_on = B_TRUE;
2650 txg_sync_start(spa->spa_dsl_pool);
2653 * Wait for all claims to sync. We sync up to the highest
2654 * claimed log block birth time so that claimed log blocks
2655 * don't appear to be from the future. spa_claim_max_txg
2656 * will have been set for us by either zil_check_log_chain()
2657 * (invoked from spa_check_logs()) or zil_claim() above.
2659 txg_wait_synced(spa->spa_dsl_pool, spa->spa_claim_max_txg);
2662 * If the config cache is stale, or we have uninitialized
2663 * metaslabs (see spa_vdev_add()), then update the config.
2665 * If this is a verbatim import, trust the current
2666 * in-core spa_config and update the disk labels.
2668 if (config_cache_txg != spa->spa_config_txg ||
2669 state == SPA_LOAD_IMPORT ||
2670 state == SPA_LOAD_RECOVER ||
2671 (spa->spa_import_flags & ZFS_IMPORT_VERBATIM))
2672 need_update = B_TRUE;
2674 for (c = 0; c < rvd->vdev_children; c++)
2675 if (rvd->vdev_child[c]->vdev_ms_array == 0)
2676 need_update = B_TRUE;
2679 * Update the config cache asychronously in case we're the
2680 * root pool, in which case the config cache isn't writable yet.
2683 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
2686 * Check all DTLs to see if anything needs resilvering.
2688 if (!dsl_scan_resilvering(spa->spa_dsl_pool) &&
2689 vdev_resilver_needed(rvd, NULL, NULL))
2690 spa_async_request(spa, SPA_ASYNC_RESILVER);
2693 * Delete any inconsistent datasets.
2695 (void) dmu_objset_find(spa_name(spa),
2696 dsl_destroy_inconsistent, NULL, DS_FIND_CHILDREN);
2699 * Clean up any stale temporary dataset userrefs.
2701 dsl_pool_clean_tmp_userrefs(spa->spa_dsl_pool);
2708 spa_load_retry(spa_t *spa, spa_load_state_t state, int mosconfig)
2710 int mode = spa->spa_mode;
2713 spa_deactivate(spa);
2715 spa->spa_load_max_txg--;
2717 spa_activate(spa, mode);
2718 spa_async_suspend(spa);
2720 return (spa_load(spa, state, SPA_IMPORT_EXISTING, mosconfig));
2724 * If spa_load() fails this function will try loading prior txg's. If
2725 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2726 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2727 * function will not rewind the pool and will return the same error as
2731 spa_load_best(spa_t *spa, spa_load_state_t state, int mosconfig,
2732 uint64_t max_request, int rewind_flags)
2734 nvlist_t *loadinfo = NULL;
2735 nvlist_t *config = NULL;
2736 int load_error, rewind_error;
2737 uint64_t safe_rewind_txg;
2740 if (spa->spa_load_txg && state == SPA_LOAD_RECOVER) {
2741 spa->spa_load_max_txg = spa->spa_load_txg;
2742 spa_set_log_state(spa, SPA_LOG_CLEAR);
2744 spa->spa_load_max_txg = max_request;
2747 load_error = rewind_error = spa_load(spa, state, SPA_IMPORT_EXISTING,
2749 if (load_error == 0)
2752 if (spa->spa_root_vdev != NULL)
2753 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2755 spa->spa_last_ubsync_txg = spa->spa_uberblock.ub_txg;
2756 spa->spa_last_ubsync_txg_ts = spa->spa_uberblock.ub_timestamp;
2758 if (rewind_flags & ZPOOL_NEVER_REWIND) {
2759 nvlist_free(config);
2760 return (load_error);
2763 if (state == SPA_LOAD_RECOVER) {
2764 /* Price of rolling back is discarding txgs, including log */
2765 spa_set_log_state(spa, SPA_LOG_CLEAR);
2768 * If we aren't rolling back save the load info from our first
2769 * import attempt so that we can restore it after attempting
2772 loadinfo = spa->spa_load_info;
2773 spa->spa_load_info = fnvlist_alloc();
2776 spa->spa_load_max_txg = spa->spa_last_ubsync_txg;
2777 safe_rewind_txg = spa->spa_last_ubsync_txg - TXG_DEFER_SIZE;
2778 min_txg = (rewind_flags & ZPOOL_EXTREME_REWIND) ?
2779 TXG_INITIAL : safe_rewind_txg;
2782 * Continue as long as we're finding errors, we're still within
2783 * the acceptable rewind range, and we're still finding uberblocks
2785 while (rewind_error && spa->spa_uberblock.ub_txg >= min_txg &&
2786 spa->spa_uberblock.ub_txg <= spa->spa_load_max_txg) {
2787 if (spa->spa_load_max_txg < safe_rewind_txg)
2788 spa->spa_extreme_rewind = B_TRUE;
2789 rewind_error = spa_load_retry(spa, state, mosconfig);
2792 spa->spa_extreme_rewind = B_FALSE;
2793 spa->spa_load_max_txg = UINT64_MAX;
2795 if (config && (rewind_error || state != SPA_LOAD_RECOVER))
2796 spa_config_set(spa, config);
2798 if (state == SPA_LOAD_RECOVER) {
2799 ASSERT3P(loadinfo, ==, NULL);
2800 return (rewind_error);
2802 /* Store the rewind info as part of the initial load info */
2803 fnvlist_add_nvlist(loadinfo, ZPOOL_CONFIG_REWIND_INFO,
2804 spa->spa_load_info);
2806 /* Restore the initial load info */
2807 fnvlist_free(spa->spa_load_info);
2808 spa->spa_load_info = loadinfo;
2810 return (load_error);
2817 * The import case is identical to an open except that the configuration is sent
2818 * down from userland, instead of grabbed from the configuration cache. For the
2819 * case of an open, the pool configuration will exist in the
2820 * POOL_STATE_UNINITIALIZED state.
2822 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2823 * the same time open the pool, without having to keep around the spa_t in some
2827 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t *nvpolicy,
2831 spa_load_state_t state = SPA_LOAD_OPEN;
2833 int locked = B_FALSE;
2838 * As disgusting as this is, we need to support recursive calls to this
2839 * function because dsl_dir_open() is called during spa_load(), and ends
2840 * up calling spa_open() again. The real fix is to figure out how to
2841 * avoid dsl_dir_open() calling this in the first place.
2843 if (mutex_owner(&spa_namespace_lock) != curthread) {
2844 mutex_enter(&spa_namespace_lock);
2848 if ((spa = spa_lookup(pool)) == NULL) {
2850 mutex_exit(&spa_namespace_lock);
2854 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
2855 zpool_rewind_policy_t policy;
2857 zpool_get_rewind_policy(nvpolicy ? nvpolicy : spa->spa_config,
2859 if (policy.zrp_request & ZPOOL_DO_REWIND)
2860 state = SPA_LOAD_RECOVER;
2862 spa_activate(spa, spa_mode_global);
2864 if (state != SPA_LOAD_RECOVER)
2865 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
2867 error = spa_load_best(spa, state, B_FALSE, policy.zrp_txg,
2868 policy.zrp_request);
2870 if (error == EBADF) {
2872 * If vdev_validate() returns failure (indicated by
2873 * EBADF), it indicates that one of the vdevs indicates
2874 * that the pool has been exported or destroyed. If
2875 * this is the case, the config cache is out of sync and
2876 * we should remove the pool from the namespace.
2879 spa_deactivate(spa);
2880 spa_config_sync(spa, B_TRUE, B_TRUE);
2883 mutex_exit(&spa_namespace_lock);
2889 * We can't open the pool, but we still have useful
2890 * information: the state of each vdev after the
2891 * attempted vdev_open(). Return this to the user.
2893 if (config != NULL && spa->spa_config) {
2894 VERIFY(nvlist_dup(spa->spa_config, config,
2896 VERIFY(nvlist_add_nvlist(*config,
2897 ZPOOL_CONFIG_LOAD_INFO,
2898 spa->spa_load_info) == 0);
2901 spa_deactivate(spa);
2902 spa->spa_last_open_failed = error;
2904 mutex_exit(&spa_namespace_lock);
2910 spa_open_ref(spa, tag);
2913 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2916 * If we've recovered the pool, pass back any information we
2917 * gathered while doing the load.
2919 if (state == SPA_LOAD_RECOVER) {
2920 VERIFY(nvlist_add_nvlist(*config, ZPOOL_CONFIG_LOAD_INFO,
2921 spa->spa_load_info) == 0);
2925 spa->spa_last_open_failed = 0;
2926 spa->spa_last_ubsync_txg = 0;
2927 spa->spa_load_txg = 0;
2928 mutex_exit(&spa_namespace_lock);
2937 spa_open_rewind(const char *name, spa_t **spapp, void *tag, nvlist_t *policy,
2940 return (spa_open_common(name, spapp, tag, policy, config));
2944 spa_open(const char *name, spa_t **spapp, void *tag)
2946 return (spa_open_common(name, spapp, tag, NULL, NULL));
2950 * Lookup the given spa_t, incrementing the inject count in the process,
2951 * preventing it from being exported or destroyed.
2954 spa_inject_addref(char *name)
2958 mutex_enter(&spa_namespace_lock);
2959 if ((spa = spa_lookup(name)) == NULL) {
2960 mutex_exit(&spa_namespace_lock);
2963 spa->spa_inject_ref++;
2964 mutex_exit(&spa_namespace_lock);
2970 spa_inject_delref(spa_t *spa)
2972 mutex_enter(&spa_namespace_lock);
2973 spa->spa_inject_ref--;
2974 mutex_exit(&spa_namespace_lock);
2978 * Add spares device information to the nvlist.
2981 spa_add_spares(spa_t *spa, nvlist_t *config)
2991 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
2993 if (spa->spa_spares.sav_count == 0)
2996 VERIFY(nvlist_lookup_nvlist(config,
2997 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2998 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
2999 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3001 VERIFY(nvlist_add_nvlist_array(nvroot,
3002 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3003 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3004 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
3007 * Go through and find any spares which have since been
3008 * repurposed as an active spare. If this is the case, update
3009 * their status appropriately.
3011 for (i = 0; i < nspares; i++) {
3012 VERIFY(nvlist_lookup_uint64(spares[i],
3013 ZPOOL_CONFIG_GUID, &guid) == 0);
3014 if (spa_spare_exists(guid, &pool, NULL) &&
3016 VERIFY(nvlist_lookup_uint64_array(
3017 spares[i], ZPOOL_CONFIG_VDEV_STATS,
3018 (uint64_t **)&vs, &vsc) == 0);
3019 vs->vs_state = VDEV_STATE_CANT_OPEN;
3020 vs->vs_aux = VDEV_AUX_SPARED;
3027 * Add l2cache device information to the nvlist, including vdev stats.
3030 spa_add_l2cache(spa_t *spa, nvlist_t *config)
3033 uint_t i, j, nl2cache;
3040 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3042 if (spa->spa_l2cache.sav_count == 0)
3045 VERIFY(nvlist_lookup_nvlist(config,
3046 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3047 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3048 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3049 if (nl2cache != 0) {
3050 VERIFY(nvlist_add_nvlist_array(nvroot,
3051 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3052 VERIFY(nvlist_lookup_nvlist_array(nvroot,
3053 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
3056 * Update level 2 cache device stats.
3059 for (i = 0; i < nl2cache; i++) {
3060 VERIFY(nvlist_lookup_uint64(l2cache[i],
3061 ZPOOL_CONFIG_GUID, &guid) == 0);
3064 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
3066 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
3067 vd = spa->spa_l2cache.sav_vdevs[j];
3073 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
3074 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
3076 vdev_get_stats(vd, vs);
3082 spa_add_feature_stats(spa_t *spa, nvlist_t *config)
3088 ASSERT(spa_config_held(spa, SCL_CONFIG, RW_READER));
3089 VERIFY(nvlist_alloc(&features, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3091 if (spa->spa_feat_for_read_obj != 0) {
3092 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3093 spa->spa_feat_for_read_obj);
3094 zap_cursor_retrieve(&zc, &za) == 0;
3095 zap_cursor_advance(&zc)) {
3096 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3097 za.za_num_integers == 1);
3098 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3099 za.za_first_integer));
3101 zap_cursor_fini(&zc);
3104 if (spa->spa_feat_for_write_obj != 0) {
3105 for (zap_cursor_init(&zc, spa->spa_meta_objset,
3106 spa->spa_feat_for_write_obj);
3107 zap_cursor_retrieve(&zc, &za) == 0;
3108 zap_cursor_advance(&zc)) {
3109 ASSERT(za.za_integer_length == sizeof (uint64_t) &&
3110 za.za_num_integers == 1);
3111 VERIFY3U(0, ==, nvlist_add_uint64(features, za.za_name,
3112 za.za_first_integer));
3114 zap_cursor_fini(&zc);
3117 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_FEATURE_STATS,
3119 nvlist_free(features);
3123 spa_get_stats(const char *name, nvlist_t **config,
3124 char *altroot, size_t buflen)
3130 error = spa_open_common(name, &spa, FTAG, NULL, config);
3134 * This still leaves a window of inconsistency where the spares
3135 * or l2cache devices could change and the config would be
3136 * self-inconsistent.
3138 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3140 if (*config != NULL) {
3141 uint64_t loadtimes[2];
3143 loadtimes[0] = spa->spa_loaded_ts.tv_sec;
3144 loadtimes[1] = spa->spa_loaded_ts.tv_nsec;
3145 VERIFY(nvlist_add_uint64_array(*config,
3146 ZPOOL_CONFIG_LOADED_TIME, loadtimes, 2) == 0);
3148 VERIFY(nvlist_add_uint64(*config,
3149 ZPOOL_CONFIG_ERRCOUNT,
3150 spa_get_errlog_size(spa)) == 0);
3152 if (spa_suspended(spa))
3153 VERIFY(nvlist_add_uint64(*config,
3154 ZPOOL_CONFIG_SUSPENDED,
3155 spa->spa_failmode) == 0);
3157 spa_add_spares(spa, *config);
3158 spa_add_l2cache(spa, *config);
3159 spa_add_feature_stats(spa, *config);
3164 * We want to get the alternate root even for faulted pools, so we cheat
3165 * and call spa_lookup() directly.
3169 mutex_enter(&spa_namespace_lock);
3170 spa = spa_lookup(name);
3172 spa_altroot(spa, altroot, buflen);
3176 mutex_exit(&spa_namespace_lock);
3178 spa_altroot(spa, altroot, buflen);
3183 spa_config_exit(spa, SCL_CONFIG, FTAG);
3184 spa_close(spa, FTAG);
3191 * Validate that the auxiliary device array is well formed. We must have an
3192 * array of nvlists, each which describes a valid leaf vdev. If this is an
3193 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3194 * specified, as long as they are well-formed.
3197 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
3198 spa_aux_vdev_t *sav, const char *config, uint64_t version,
3199 vdev_labeltype_t label)
3206 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3209 * It's acceptable to have no devs specified.
3211 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
3218 * Make sure the pool is formatted with a version that supports this
3221 if (spa_version(spa) < version)
3225 * Set the pending device list so we correctly handle device in-use
3228 sav->sav_pending = dev;
3229 sav->sav_npending = ndev;
3231 for (i = 0; i < ndev; i++) {
3232 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
3236 if (!vd->vdev_ops->vdev_op_leaf) {
3243 * The L2ARC currently only supports disk devices in
3244 * kernel context. For user-level testing, we allow it.
3247 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
3248 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
3256 if ((error = vdev_open(vd)) == 0 &&
3257 (error = vdev_label_init(vd, crtxg, label)) == 0) {
3258 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
3259 vd->vdev_guid) == 0);
3265 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
3272 sav->sav_pending = NULL;
3273 sav->sav_npending = 0;
3278 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
3282 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
3284 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3285 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
3286 VDEV_LABEL_SPARE)) != 0) {
3290 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
3291 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
3292 VDEV_LABEL_L2CACHE));
3296 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
3301 if (sav->sav_config != NULL) {
3307 * Generate new dev list by concatentating with the
3310 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
3311 &olddevs, &oldndevs) == 0);
3313 newdevs = kmem_alloc(sizeof (void *) *
3314 (ndevs + oldndevs), KM_PUSHPAGE);
3315 for (i = 0; i < oldndevs; i++)
3316 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
3318 for (i = 0; i < ndevs; i++)
3319 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
3322 VERIFY(nvlist_remove(sav->sav_config, config,
3323 DATA_TYPE_NVLIST_ARRAY) == 0);
3325 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
3326 config, newdevs, ndevs + oldndevs) == 0);
3327 for (i = 0; i < oldndevs + ndevs; i++)
3328 nvlist_free(newdevs[i]);
3329 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
3332 * Generate a new dev list.
3334 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
3336 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
3342 * Stop and drop level 2 ARC devices
3345 spa_l2cache_drop(spa_t *spa)
3349 spa_aux_vdev_t *sav = &spa->spa_l2cache;
3351 for (i = 0; i < sav->sav_count; i++) {
3354 vd = sav->sav_vdevs[i];
3357 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
3358 pool != 0ULL && l2arc_vdev_present(vd))
3359 l2arc_remove_vdev(vd);
3367 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
3368 const char *history_str, nvlist_t *zplprops)
3371 char *altroot = NULL;
3376 uint64_t txg = TXG_INITIAL;
3377 nvlist_t **spares, **l2cache;
3378 uint_t nspares, nl2cache;
3379 uint64_t version, obj;
3380 boolean_t has_features;
3385 * If this pool already exists, return failure.
3387 mutex_enter(&spa_namespace_lock);
3388 if (spa_lookup(pool) != NULL) {
3389 mutex_exit(&spa_namespace_lock);
3394 * Allocate a new spa_t structure.
3396 (void) nvlist_lookup_string(props,
3397 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3398 spa = spa_add(pool, NULL, altroot);
3399 spa_activate(spa, spa_mode_global);
3401 if (props && (error = spa_prop_validate(spa, props))) {
3402 spa_deactivate(spa);
3404 mutex_exit(&spa_namespace_lock);
3408 has_features = B_FALSE;
3409 for (elem = nvlist_next_nvpair(props, NULL);
3410 elem != NULL; elem = nvlist_next_nvpair(props, elem)) {
3411 if (zpool_prop_feature(nvpair_name(elem)))
3412 has_features = B_TRUE;
3415 if (has_features || nvlist_lookup_uint64(props,
3416 zpool_prop_to_name(ZPOOL_PROP_VERSION), &version) != 0) {
3417 version = SPA_VERSION;
3419 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
3421 spa->spa_first_txg = txg;
3422 spa->spa_uberblock.ub_txg = txg - 1;
3423 spa->spa_uberblock.ub_version = version;
3424 spa->spa_ubsync = spa->spa_uberblock;
3427 * Create "The Godfather" zio to hold all async IOs
3429 spa->spa_async_zio_root = zio_root(spa, NULL, NULL,
3430 ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE | ZIO_FLAG_GODFATHER);
3433 * Create the root vdev.
3435 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3437 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
3439 ASSERT(error != 0 || rvd != NULL);
3440 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
3442 if (error == 0 && !zfs_allocatable_devs(nvroot))
3446 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
3447 (error = spa_validate_aux(spa, nvroot, txg,
3448 VDEV_ALLOC_ADD)) == 0) {
3449 for (c = 0; c < rvd->vdev_children; c++) {
3450 vdev_metaslab_set_size(rvd->vdev_child[c]);
3451 vdev_expand(rvd->vdev_child[c], txg);
3455 spa_config_exit(spa, SCL_ALL, FTAG);
3459 spa_deactivate(spa);
3461 mutex_exit(&spa_namespace_lock);
3466 * Get the list of spares, if specified.
3468 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3469 &spares, &nspares) == 0) {
3470 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
3472 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3473 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3474 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3475 spa_load_spares(spa);
3476 spa_config_exit(spa, SCL_ALL, FTAG);
3477 spa->spa_spares.sav_sync = B_TRUE;
3481 * Get the list of level 2 cache devices, if specified.
3483 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3484 &l2cache, &nl2cache) == 0) {
3485 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3486 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3487 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3488 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3489 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3490 spa_load_l2cache(spa);
3491 spa_config_exit(spa, SCL_ALL, FTAG);
3492 spa->spa_l2cache.sav_sync = B_TRUE;
3495 spa->spa_is_initializing = B_TRUE;
3496 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
3497 spa->spa_meta_objset = dp->dp_meta_objset;
3498 spa->spa_is_initializing = B_FALSE;
3501 * Create DDTs (dedup tables).
3505 spa_update_dspace(spa);
3507 tx = dmu_tx_create_assigned(dp, txg);
3510 * Create the pool config object.
3512 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
3513 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
3514 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
3516 if (zap_add(spa->spa_meta_objset,
3517 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
3518 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
3519 cmn_err(CE_PANIC, "failed to add pool config");
3522 if (spa_version(spa) >= SPA_VERSION_FEATURES)
3523 spa_feature_create_zap_objects(spa, tx);
3525 if (zap_add(spa->spa_meta_objset,
3526 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CREATION_VERSION,
3527 sizeof (uint64_t), 1, &version, tx) != 0) {
3528 cmn_err(CE_PANIC, "failed to add pool version");
3531 /* Newly created pools with the right version are always deflated. */
3532 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
3533 spa->spa_deflate = TRUE;
3534 if (zap_add(spa->spa_meta_objset,
3535 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3536 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
3537 cmn_err(CE_PANIC, "failed to add deflate");
3542 * Create the deferred-free bpobj. Turn off compression
3543 * because sync-to-convergence takes longer if the blocksize
3546 obj = bpobj_alloc(spa->spa_meta_objset, 1 << 14, tx);
3547 dmu_object_set_compress(spa->spa_meta_objset, obj,
3548 ZIO_COMPRESS_OFF, tx);
3549 if (zap_add(spa->spa_meta_objset,
3550 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPOBJ,
3551 sizeof (uint64_t), 1, &obj, tx) != 0) {
3552 cmn_err(CE_PANIC, "failed to add bpobj");
3554 VERIFY3U(0, ==, bpobj_open(&spa->spa_deferred_bpobj,
3555 spa->spa_meta_objset, obj));
3558 * Create the pool's history object.
3560 if (version >= SPA_VERSION_ZPOOL_HISTORY)
3561 spa_history_create_obj(spa, tx);
3564 * Set pool properties.
3566 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
3567 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
3568 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
3569 spa->spa_autoexpand = zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND);
3571 if (props != NULL) {
3572 spa_configfile_set(spa, props, B_FALSE);
3573 spa_sync_props(spa, props, tx);
3578 spa->spa_sync_on = B_TRUE;
3579 txg_sync_start(spa->spa_dsl_pool);
3582 * We explicitly wait for the first transaction to complete so that our
3583 * bean counters are appropriately updated.
3585 txg_wait_synced(spa->spa_dsl_pool, txg);
3587 spa_config_sync(spa, B_FALSE, B_TRUE);
3589 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
3590 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
3591 spa_history_log_version(spa, LOG_POOL_CREATE);
3593 spa->spa_minref = refcount_count(&spa->spa_refcount);
3595 mutex_exit(&spa_namespace_lock);
3602 * Get the root pool information from the root disk, then import the root pool
3603 * during the system boot up time.
3605 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
3608 spa_generate_rootconf(char *devpath, char *devid, uint64_t *guid)
3611 nvlist_t *nvtop, *nvroot;
3614 if (vdev_disk_read_rootlabel(devpath, devid, &config) != 0)
3618 * Add this top-level vdev to the child array.
3620 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3622 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
3624 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_GUID, guid) == 0);
3627 * Put this pool's top-level vdevs into a root vdev.
3629 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3630 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE,
3631 VDEV_TYPE_ROOT) == 0);
3632 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
3633 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
3634 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3638 * Replace the existing vdev_tree with the new root vdev in
3639 * this pool's configuration (remove the old, add the new).
3641 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
3642 nvlist_free(nvroot);
3647 * Walk the vdev tree and see if we can find a device with "better"
3648 * configuration. A configuration is "better" if the label on that
3649 * device has a more recent txg.
3652 spa_alt_rootvdev(vdev_t *vd, vdev_t **avd, uint64_t *txg)
3656 for (c = 0; c < vd->vdev_children; c++)
3657 spa_alt_rootvdev(vd->vdev_child[c], avd, txg);
3659 if (vd->vdev_ops->vdev_op_leaf) {
3663 if (vdev_disk_read_rootlabel(vd->vdev_physpath, vd->vdev_devid,
3667 VERIFY(nvlist_lookup_uint64(label, ZPOOL_CONFIG_POOL_TXG,
3671 * Do we have a better boot device?
3673 if (label_txg > *txg) {
3682 * Import a root pool.
3684 * For x86. devpath_list will consist of devid and/or physpath name of
3685 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3686 * The GRUB "findroot" command will return the vdev we should boot.
3688 * For Sparc, devpath_list consists the physpath name of the booting device
3689 * no matter the rootpool is a single device pool or a mirrored pool.
3691 * "/pci@1f,0/ide@d/disk@0,0:a"
3694 spa_import_rootpool(char *devpath, char *devid)
3697 vdev_t *rvd, *bvd, *avd = NULL;
3698 nvlist_t *config, *nvtop;
3704 * Read the label from the boot device and generate a configuration.
3706 config = spa_generate_rootconf(devpath, devid, &guid);
3707 #if defined(_OBP) && defined(_KERNEL)
3708 if (config == NULL) {
3709 if (strstr(devpath, "/iscsi/ssd") != NULL) {
3711 get_iscsi_bootpath_phy(devpath);
3712 config = spa_generate_rootconf(devpath, devid, &guid);
3716 if (config == NULL) {
3717 cmn_err(CE_NOTE, "Cannot read the pool label from '%s'",
3722 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
3724 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
3726 mutex_enter(&spa_namespace_lock);
3727 if ((spa = spa_lookup(pname)) != NULL) {
3729 * Remove the existing root pool from the namespace so that we
3730 * can replace it with the correct config we just read in.
3735 spa = spa_add(pname, config, NULL);
3736 spa->spa_is_root = B_TRUE;
3737 spa->spa_import_flags = ZFS_IMPORT_VERBATIM;
3740 * Build up a vdev tree based on the boot device's label config.
3742 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3744 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3745 error = spa_config_parse(spa, &rvd, nvtop, NULL, 0,
3746 VDEV_ALLOC_ROOTPOOL);
3747 spa_config_exit(spa, SCL_ALL, FTAG);
3749 mutex_exit(&spa_namespace_lock);
3750 nvlist_free(config);
3751 cmn_err(CE_NOTE, "Can not parse the config for pool '%s'",
3757 * Get the boot vdev.
3759 if ((bvd = vdev_lookup_by_guid(rvd, guid)) == NULL) {
3760 cmn_err(CE_NOTE, "Can not find the boot vdev for guid %llu",
3761 (u_longlong_t)guid);
3767 * Determine if there is a better boot device.
3770 spa_alt_rootvdev(rvd, &avd, &txg);
3772 cmn_err(CE_NOTE, "The boot device is 'degraded'. Please "
3773 "try booting from '%s'", avd->vdev_path);
3779 * If the boot device is part of a spare vdev then ensure that
3780 * we're booting off the active spare.
3782 if (bvd->vdev_parent->vdev_ops == &vdev_spare_ops &&
3783 !bvd->vdev_isspare) {
3784 cmn_err(CE_NOTE, "The boot device is currently spared. Please "
3785 "try booting from '%s'",
3787 vdev_child[bvd->vdev_parent->vdev_children - 1]->vdev_path);
3793 spa_history_log_version(spa, LOG_POOL_IMPORT);
3795 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3797 spa_config_exit(spa, SCL_ALL, FTAG);
3798 mutex_exit(&spa_namespace_lock);
3800 nvlist_free(config);
3807 * Import a non-root pool into the system.
3810 spa_import(const char *pool, nvlist_t *config, nvlist_t *props, uint64_t flags)
3813 char *altroot = NULL;
3814 spa_load_state_t state = SPA_LOAD_IMPORT;
3815 zpool_rewind_policy_t policy;
3816 uint64_t mode = spa_mode_global;
3817 uint64_t readonly = B_FALSE;
3820 nvlist_t **spares, **l2cache;
3821 uint_t nspares, nl2cache;
3824 * If a pool with this name exists, return failure.
3826 mutex_enter(&spa_namespace_lock);
3827 if (spa_lookup(pool) != NULL) {
3828 mutex_exit(&spa_namespace_lock);
3833 * Create and initialize the spa structure.
3835 (void) nvlist_lookup_string(props,
3836 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
3837 (void) nvlist_lookup_uint64(props,
3838 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3841 spa = spa_add(pool, config, altroot);
3842 spa->spa_import_flags = flags;
3845 * Verbatim import - Take a pool and insert it into the namespace
3846 * as if it had been loaded at boot.
3848 if (spa->spa_import_flags & ZFS_IMPORT_VERBATIM) {
3850 spa_configfile_set(spa, props, B_FALSE);
3852 spa_config_sync(spa, B_FALSE, B_TRUE);
3854 mutex_exit(&spa_namespace_lock);
3855 spa_history_log_version(spa, LOG_POOL_IMPORT);
3860 spa_activate(spa, mode);
3863 * Don't start async tasks until we know everything is healthy.
3865 spa_async_suspend(spa);
3867 zpool_get_rewind_policy(config, &policy);
3868 if (policy.zrp_request & ZPOOL_DO_REWIND)
3869 state = SPA_LOAD_RECOVER;
3872 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3873 * because the user-supplied config is actually the one to trust when
3876 if (state != SPA_LOAD_RECOVER)
3877 spa->spa_last_ubsync_txg = spa->spa_load_txg = 0;
3879 error = spa_load_best(spa, state, B_TRUE, policy.zrp_txg,
3880 policy.zrp_request);
3883 * Propagate anything learned while loading the pool and pass it
3884 * back to caller (i.e. rewind info, missing devices, etc).
3886 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
3887 spa->spa_load_info) == 0);
3889 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3891 * Toss any existing sparelist, as it doesn't have any validity
3892 * anymore, and conflicts with spa_has_spare().
3894 if (spa->spa_spares.sav_config) {
3895 nvlist_free(spa->spa_spares.sav_config);
3896 spa->spa_spares.sav_config = NULL;
3897 spa_load_spares(spa);
3899 if (spa->spa_l2cache.sav_config) {
3900 nvlist_free(spa->spa_l2cache.sav_config);
3901 spa->spa_l2cache.sav_config = NULL;
3902 spa_load_l2cache(spa);
3905 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3908 error = spa_validate_aux(spa, nvroot, -1ULL,
3911 error = spa_validate_aux(spa, nvroot, -1ULL,
3912 VDEV_ALLOC_L2CACHE);
3913 spa_config_exit(spa, SCL_ALL, FTAG);
3916 spa_configfile_set(spa, props, B_FALSE);
3918 if (error != 0 || (props && spa_writeable(spa) &&
3919 (error = spa_prop_set(spa, props)))) {
3921 spa_deactivate(spa);
3923 mutex_exit(&spa_namespace_lock);
3927 spa_async_resume(spa);
3930 * Override any spares and level 2 cache devices as specified by
3931 * the user, as these may have correct device names/devids, etc.
3933 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
3934 &spares, &nspares) == 0) {
3935 if (spa->spa_spares.sav_config)
3936 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
3937 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
3939 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
3940 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3941 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
3942 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
3943 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3944 spa_load_spares(spa);
3945 spa_config_exit(spa, SCL_ALL, FTAG);
3946 spa->spa_spares.sav_sync = B_TRUE;
3948 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
3949 &l2cache, &nl2cache) == 0) {
3950 if (spa->spa_l2cache.sav_config)
3951 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
3952 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
3954 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
3955 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
3956 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
3957 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
3958 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3959 spa_load_l2cache(spa);
3960 spa_config_exit(spa, SCL_ALL, FTAG);
3961 spa->spa_l2cache.sav_sync = B_TRUE;
3965 * Check for any removed devices.
3967 if (spa->spa_autoreplace) {
3968 spa_aux_check_removed(&spa->spa_spares);
3969 spa_aux_check_removed(&spa->spa_l2cache);
3972 if (spa_writeable(spa)) {
3974 * Update the config cache to include the newly-imported pool.
3976 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3980 * It's possible that the pool was expanded while it was exported.
3981 * We kick off an async task to handle this for us.
3983 spa_async_request(spa, SPA_ASYNC_AUTOEXPAND);
3985 mutex_exit(&spa_namespace_lock);
3986 spa_history_log_version(spa, LOG_POOL_IMPORT);
3992 spa_tryimport(nvlist_t *tryconfig)
3994 nvlist_t *config = NULL;
4000 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
4003 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
4007 * Create and initialize the spa structure.
4009 mutex_enter(&spa_namespace_lock);
4010 spa = spa_add(TRYIMPORT_NAME, tryconfig, NULL);
4011 spa_activate(spa, FREAD);
4014 * Pass off the heavy lifting to spa_load().
4015 * Pass TRUE for mosconfig because the user-supplied config
4016 * is actually the one to trust when doing an import.
4018 error = spa_load(spa, SPA_LOAD_TRYIMPORT, SPA_IMPORT_EXISTING, B_TRUE);
4021 * If 'tryconfig' was at least parsable, return the current config.
4023 if (spa->spa_root_vdev != NULL) {
4024 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
4025 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
4027 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4029 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
4030 spa->spa_uberblock.ub_timestamp) == 0);
4031 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_LOAD_INFO,
4032 spa->spa_load_info) == 0);
4035 * If the bootfs property exists on this pool then we
4036 * copy it out so that external consumers can tell which
4037 * pools are bootable.
4039 if ((!error || error == EEXIST) && spa->spa_bootfs) {
4040 char *tmpname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
4043 * We have to play games with the name since the
4044 * pool was opened as TRYIMPORT_NAME.
4046 if (dsl_dsobj_to_dsname(spa_name(spa),
4047 spa->spa_bootfs, tmpname) == 0) {
4049 char *dsname = kmem_alloc(MAXPATHLEN, KM_PUSHPAGE);
4051 cp = strchr(tmpname, '/');
4053 (void) strlcpy(dsname, tmpname,
4056 (void) snprintf(dsname, MAXPATHLEN,
4057 "%s/%s", poolname, ++cp);
4059 VERIFY(nvlist_add_string(config,
4060 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
4061 kmem_free(dsname, MAXPATHLEN);
4063 kmem_free(tmpname, MAXPATHLEN);
4067 * Add the list of hot spares and level 2 cache devices.
4069 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
4070 spa_add_spares(spa, config);
4071 spa_add_l2cache(spa, config);
4072 spa_config_exit(spa, SCL_CONFIG, FTAG);
4076 spa_deactivate(spa);
4078 mutex_exit(&spa_namespace_lock);
4084 * Pool export/destroy
4086 * The act of destroying or exporting a pool is very simple. We make sure there
4087 * is no more pending I/O and any references to the pool are gone. Then, we
4088 * update the pool state and sync all the labels to disk, removing the
4089 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4090 * we don't sync the labels or remove the configuration cache.
4093 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
4094 boolean_t force, boolean_t hardforce)
4101 if (!(spa_mode_global & FWRITE))
4104 mutex_enter(&spa_namespace_lock);
4105 if ((spa = spa_lookup(pool)) == NULL) {
4106 mutex_exit(&spa_namespace_lock);
4111 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4112 * reacquire the namespace lock, and see if we can export.
4114 spa_open_ref(spa, FTAG);
4115 mutex_exit(&spa_namespace_lock);
4116 spa_async_suspend(spa);
4117 mutex_enter(&spa_namespace_lock);
4118 spa_close(spa, FTAG);
4121 * The pool will be in core if it's openable,
4122 * in which case we can modify its state.
4124 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
4126 * Objsets may be open only because they're dirty, so we
4127 * have to force it to sync before checking spa_refcnt.
4129 txg_wait_synced(spa->spa_dsl_pool, 0);
4132 * A pool cannot be exported or destroyed if there are active
4133 * references. If we are resetting a pool, allow references by
4134 * fault injection handlers.
4136 if (!spa_refcount_zero(spa) ||
4137 (spa->spa_inject_ref != 0 &&
4138 new_state != POOL_STATE_UNINITIALIZED)) {
4139 spa_async_resume(spa);
4140 mutex_exit(&spa_namespace_lock);
4145 * A pool cannot be exported if it has an active shared spare.
4146 * This is to prevent other pools stealing the active spare
4147 * from an exported pool. At user's own will, such pool can
4148 * be forcedly exported.
4150 if (!force && new_state == POOL_STATE_EXPORTED &&
4151 spa_has_active_shared_spare(spa)) {
4152 spa_async_resume(spa);
4153 mutex_exit(&spa_namespace_lock);
4158 * We want this to be reflected on every label,
4159 * so mark them all dirty. spa_unload() will do the
4160 * final sync that pushes these changes out.
4162 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
4163 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4164 spa->spa_state = new_state;
4165 spa->spa_final_txg = spa_last_synced_txg(spa) +
4167 vdev_config_dirty(spa->spa_root_vdev);
4168 spa_config_exit(spa, SCL_ALL, FTAG);
4172 spa_event_notify(spa, NULL, FM_EREPORT_ZFS_POOL_DESTROY);
4174 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4176 spa_deactivate(spa);
4179 if (oldconfig && spa->spa_config)
4180 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
4182 if (new_state != POOL_STATE_UNINITIALIZED) {
4184 spa_config_sync(spa, B_TRUE, B_TRUE);
4187 mutex_exit(&spa_namespace_lock);
4193 * Destroy a storage pool.
4196 spa_destroy(char *pool)
4198 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
4203 * Export a storage pool.
4206 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
4207 boolean_t hardforce)
4209 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
4214 * Similar to spa_export(), this unloads the spa_t without actually removing it
4215 * from the namespace in any way.
4218 spa_reset(char *pool)
4220 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
4225 * ==========================================================================
4226 * Device manipulation
4227 * ==========================================================================
4231 * Add a device to a storage pool.
4234 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
4238 vdev_t *rvd = spa->spa_root_vdev;
4240 nvlist_t **spares, **l2cache;
4241 uint_t nspares, nl2cache;
4244 ASSERT(spa_writeable(spa));
4246 txg = spa_vdev_enter(spa);
4248 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
4249 VDEV_ALLOC_ADD)) != 0)
4250 return (spa_vdev_exit(spa, NULL, txg, error));
4252 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
4254 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
4258 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
4262 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
4263 return (spa_vdev_exit(spa, vd, txg, EINVAL));
4265 if (vd->vdev_children != 0 &&
4266 (error = vdev_create(vd, txg, B_FALSE)) != 0)
4267 return (spa_vdev_exit(spa, vd, txg, error));
4270 * We must validate the spares and l2cache devices after checking the
4271 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4273 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
4274 return (spa_vdev_exit(spa, vd, txg, error));
4277 * Transfer each new top-level vdev from vd to rvd.
4279 for (c = 0; c < vd->vdev_children; c++) {
4282 * Set the vdev id to the first hole, if one exists.
4284 for (id = 0; id < rvd->vdev_children; id++) {
4285 if (rvd->vdev_child[id]->vdev_ishole) {
4286 vdev_free(rvd->vdev_child[id]);
4290 tvd = vd->vdev_child[c];
4291 vdev_remove_child(vd, tvd);
4293 vdev_add_child(rvd, tvd);
4294 vdev_config_dirty(tvd);
4298 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
4299 ZPOOL_CONFIG_SPARES);
4300 spa_load_spares(spa);
4301 spa->spa_spares.sav_sync = B_TRUE;
4304 if (nl2cache != 0) {
4305 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
4306 ZPOOL_CONFIG_L2CACHE);
4307 spa_load_l2cache(spa);
4308 spa->spa_l2cache.sav_sync = B_TRUE;
4312 * We have to be careful when adding new vdevs to an existing pool.
4313 * If other threads start allocating from these vdevs before we
4314 * sync the config cache, and we lose power, then upon reboot we may
4315 * fail to open the pool because there are DVAs that the config cache
4316 * can't translate. Therefore, we first add the vdevs without
4317 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4318 * and then let spa_config_update() initialize the new metaslabs.
4320 * spa_load() checks for added-but-not-initialized vdevs, so that
4321 * if we lose power at any point in this sequence, the remaining
4322 * steps will be completed the next time we load the pool.
4324 (void) spa_vdev_exit(spa, vd, txg, 0);
4326 mutex_enter(&spa_namespace_lock);
4327 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
4328 mutex_exit(&spa_namespace_lock);
4334 * Attach a device to a mirror. The arguments are the path to any device
4335 * in the mirror, and the nvroot for the new device. If the path specifies
4336 * a device that is not mirrored, we automatically insert the mirror vdev.
4338 * If 'replacing' is specified, the new device is intended to replace the
4339 * existing device; in this case the two devices are made into their own
4340 * mirror using the 'replacing' vdev, which is functionally identical to
4341 * the mirror vdev (it actually reuses all the same ops) but has a few
4342 * extra rules: you can't attach to it after it's been created, and upon
4343 * completion of resilvering, the first disk (the one being replaced)
4344 * is automatically detached.
4347 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
4349 uint64_t txg, dtl_max_txg;
4350 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
4351 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
4353 char *oldvdpath, *newvdpath;
4357 ASSERT(spa_writeable(spa));
4359 txg = spa_vdev_enter(spa);
4361 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
4364 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4366 if (!oldvd->vdev_ops->vdev_op_leaf)
4367 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4369 pvd = oldvd->vdev_parent;
4371 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
4372 VDEV_ALLOC_ATTACH)) != 0)
4373 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4375 if (newrootvd->vdev_children != 1)
4376 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4378 newvd = newrootvd->vdev_child[0];
4380 if (!newvd->vdev_ops->vdev_op_leaf)
4381 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
4383 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
4384 return (spa_vdev_exit(spa, newrootvd, txg, error));
4387 * Spares can't replace logs
4389 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
4390 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4394 * For attach, the only allowable parent is a mirror or the root
4397 if (pvd->vdev_ops != &vdev_mirror_ops &&
4398 pvd->vdev_ops != &vdev_root_ops)
4399 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4401 pvops = &vdev_mirror_ops;
4404 * Active hot spares can only be replaced by inactive hot
4407 if (pvd->vdev_ops == &vdev_spare_ops &&
4408 oldvd->vdev_isspare &&
4409 !spa_has_spare(spa, newvd->vdev_guid))
4410 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4413 * If the source is a hot spare, and the parent isn't already a
4414 * spare, then we want to create a new hot spare. Otherwise, we
4415 * want to create a replacing vdev. The user is not allowed to
4416 * attach to a spared vdev child unless the 'isspare' state is
4417 * the same (spare replaces spare, non-spare replaces
4420 if (pvd->vdev_ops == &vdev_replacing_ops &&
4421 spa_version(spa) < SPA_VERSION_MULTI_REPLACE) {
4422 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4423 } else if (pvd->vdev_ops == &vdev_spare_ops &&
4424 newvd->vdev_isspare != oldvd->vdev_isspare) {
4425 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
4428 if (newvd->vdev_isspare)
4429 pvops = &vdev_spare_ops;
4431 pvops = &vdev_replacing_ops;
4435 * Make sure the new device is big enough.
4437 if (newvd->vdev_asize < vdev_get_min_asize(oldvd))
4438 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
4441 * The new device cannot have a higher alignment requirement
4442 * than the top-level vdev.
4444 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
4445 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
4448 * If this is an in-place replacement, update oldvd's path and devid
4449 * to make it distinguishable from newvd, and unopenable from now on.
4451 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
4452 spa_strfree(oldvd->vdev_path);
4453 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
4455 (void) sprintf(oldvd->vdev_path, "%s/%s",
4456 newvd->vdev_path, "old");
4457 if (oldvd->vdev_devid != NULL) {
4458 spa_strfree(oldvd->vdev_devid);
4459 oldvd->vdev_devid = NULL;
4463 /* mark the device being resilvered */
4464 newvd->vdev_resilvering = B_TRUE;
4467 * If the parent is not a mirror, or if we're replacing, insert the new
4468 * mirror/replacing/spare vdev above oldvd.
4470 if (pvd->vdev_ops != pvops)
4471 pvd = vdev_add_parent(oldvd, pvops);
4473 ASSERT(pvd->vdev_top->vdev_parent == rvd);
4474 ASSERT(pvd->vdev_ops == pvops);
4475 ASSERT(oldvd->vdev_parent == pvd);
4478 * Extract the new device from its root and add it to pvd.
4480 vdev_remove_child(newrootvd, newvd);
4481 newvd->vdev_id = pvd->vdev_children;
4482 newvd->vdev_crtxg = oldvd->vdev_crtxg;
4483 vdev_add_child(pvd, newvd);
4485 tvd = newvd->vdev_top;
4486 ASSERT(pvd->vdev_top == tvd);
4487 ASSERT(tvd->vdev_parent == rvd);
4489 vdev_config_dirty(tvd);
4492 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4493 * for any dmu_sync-ed blocks. It will propagate upward when
4494 * spa_vdev_exit() calls vdev_dtl_reassess().
4496 dtl_max_txg = txg + TXG_CONCURRENT_STATES;
4498 vdev_dtl_dirty(newvd, DTL_MISSING, TXG_INITIAL,
4499 dtl_max_txg - TXG_INITIAL);
4501 if (newvd->vdev_isspare) {
4502 spa_spare_activate(newvd);
4503 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_DEVICE_SPARE);
4506 oldvdpath = spa_strdup(oldvd->vdev_path);
4507 newvdpath = spa_strdup(newvd->vdev_path);
4508 newvd_isspare = newvd->vdev_isspare;
4511 * Mark newvd's DTL dirty in this txg.
4513 vdev_dirty(tvd, VDD_DTL, newvd, txg);
4516 * Restart the resilver
4518 dsl_resilver_restart(spa->spa_dsl_pool, dtl_max_txg);
4523 (void) spa_vdev_exit(spa, newrootvd, dtl_max_txg, 0);
4525 spa_history_log_internal(LOG_POOL_VDEV_ATTACH, spa, NULL,
4526 "%s vdev=%s %s vdev=%s",
4527 replacing && newvd_isspare ? "spare in" :
4528 replacing ? "replace" : "attach", newvdpath,
4529 replacing ? "for" : "to", oldvdpath);
4531 spa_strfree(oldvdpath);
4532 spa_strfree(newvdpath);
4534 if (spa->spa_bootfs)
4535 spa_event_notify(spa, newvd, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH);
4541 * Detach a device from a mirror or replacing vdev.
4542 * If 'replace_done' is specified, only detach if the parent
4543 * is a replacing vdev.
4546 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
4550 ASSERTV(vdev_t *rvd = spa->spa_root_vdev;)
4551 vdev_t *vd, *pvd, *cvd, *tvd;
4552 boolean_t unspare = B_FALSE;
4553 uint64_t unspare_guid = 0;
4557 ASSERT(spa_writeable(spa));
4559 txg = spa_vdev_enter(spa);
4561 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
4564 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
4566 if (!vd->vdev_ops->vdev_op_leaf)
4567 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4569 pvd = vd->vdev_parent;
4572 * If the parent/child relationship is not as expected, don't do it.
4573 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4574 * vdev that's replacing B with C. The user's intent in replacing
4575 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4576 * the replace by detaching C, the expected behavior is to end up
4577 * M(A,B). But suppose that right after deciding to detach C,
4578 * the replacement of B completes. We would have M(A,C), and then
4579 * ask to detach C, which would leave us with just A -- not what
4580 * the user wanted. To prevent this, we make sure that the
4581 * parent/child relationship hasn't changed -- in this example,
4582 * that C's parent is still the replacing vdev R.
4584 if (pvd->vdev_guid != pguid && pguid != 0)
4585 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4588 * Only 'replacing' or 'spare' vdevs can be replaced.
4590 if (replace_done && pvd->vdev_ops != &vdev_replacing_ops &&
4591 pvd->vdev_ops != &vdev_spare_ops)
4592 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4594 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
4595 spa_version(spa) >= SPA_VERSION_SPARES);
4598 * Only mirror, replacing, and spare vdevs support detach.
4600 if (pvd->vdev_ops != &vdev_replacing_ops &&
4601 pvd->vdev_ops != &vdev_mirror_ops &&
4602 pvd->vdev_ops != &vdev_spare_ops)
4603 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
4606 * If this device has the only valid copy of some data,
4607 * we cannot safely detach it.
4609 if (vdev_dtl_required(vd))
4610 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
4612 ASSERT(pvd->vdev_children >= 2);
4615 * If we are detaching the second disk from a replacing vdev, then
4616 * check to see if we changed the original vdev's path to have "/old"
4617 * at the end in spa_vdev_attach(). If so, undo that change now.
4619 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id > 0 &&
4620 vd->vdev_path != NULL) {
4621 size_t len = strlen(vd->vdev_path);
4623 for (c = 0; c < pvd->vdev_children; c++) {
4624 cvd = pvd->vdev_child[c];
4626 if (cvd == vd || cvd->vdev_path == NULL)
4629 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
4630 strcmp(cvd->vdev_path + len, "/old") == 0) {
4631 spa_strfree(cvd->vdev_path);
4632 cvd->vdev_path = spa_strdup(vd->vdev_path);
4639 * If we are detaching the original disk from a spare, then it implies
4640 * that the spare should become a real disk, and be removed from the
4641 * active spare list for the pool.
4643 if (pvd->vdev_ops == &vdev_spare_ops &&
4645 pvd->vdev_child[pvd->vdev_children - 1]->vdev_isspare)
4649 * Erase the disk labels so the disk can be used for other things.
4650 * This must be done after all other error cases are handled,
4651 * but before we disembowel vd (so we can still do I/O to it).
4652 * But if we can't do it, don't treat the error as fatal --
4653 * it may be that the unwritability of the disk is the reason
4654 * it's being detached!
4656 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
4659 * Remove vd from its parent and compact the parent's children.
4661 vdev_remove_child(pvd, vd);
4662 vdev_compact_children(pvd);
4665 * Remember one of the remaining children so we can get tvd below.
4667 cvd = pvd->vdev_child[pvd->vdev_children - 1];
4670 * If we need to remove the remaining child from the list of hot spares,
4671 * do it now, marking the vdev as no longer a spare in the process.
4672 * We must do this before vdev_remove_parent(), because that can
4673 * change the GUID if it creates a new toplevel GUID. For a similar
4674 * reason, we must remove the spare now, in the same txg as the detach;
4675 * otherwise someone could attach a new sibling, change the GUID, and
4676 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4679 ASSERT(cvd->vdev_isspare);
4680 spa_spare_remove(cvd);
4681 unspare_guid = cvd->vdev_guid;
4682 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
4683 cvd->vdev_unspare = B_TRUE;
4687 * If the parent mirror/replacing vdev only has one child,
4688 * the parent is no longer needed. Remove it from the tree.
4690 if (pvd->vdev_children == 1) {
4691 if (pvd->vdev_ops == &vdev_spare_ops)
4692 cvd->vdev_unspare = B_FALSE;
4693 vdev_remove_parent(cvd);
4694 cvd->vdev_resilvering = B_FALSE;
4699 * We don't set tvd until now because the parent we just removed
4700 * may have been the previous top-level vdev.
4702 tvd = cvd->vdev_top;
4703 ASSERT(tvd->vdev_parent == rvd);
4706 * Reevaluate the parent vdev state.
4708 vdev_propagate_state(cvd);
4711 * If the 'autoexpand' property is set on the pool then automatically
4712 * try to expand the size of the pool. For example if the device we
4713 * just detached was smaller than the others, it may be possible to
4714 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4715 * first so that we can obtain the updated sizes of the leaf vdevs.
4717 if (spa->spa_autoexpand) {
4719 vdev_expand(tvd, txg);
4722 vdev_config_dirty(tvd);
4725 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4726 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4727 * But first make sure we're not on any *other* txg's DTL list, to
4728 * prevent vd from being accessed after it's freed.
4730 vdpath = spa_strdup(vd->vdev_path);
4731 for (t = 0; t < TXG_SIZE; t++)
4732 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
4733 vd->vdev_detached = B_TRUE;
4734 vdev_dirty(tvd, VDD_DTL, vd, txg);
4736 spa_event_notify(spa, vd, FM_EREPORT_ZFS_DEVICE_REMOVE);
4738 /* hang on to the spa before we release the lock */
4739 spa_open_ref(spa, FTAG);
4741 error = spa_vdev_exit(spa, vd, txg, 0);
4743 spa_history_log_internal(LOG_POOL_VDEV_DETACH, spa, NULL,
4745 spa_strfree(vdpath);
4748 * If this was the removal of the original device in a hot spare vdev,
4749 * then we want to go through and remove the device from the hot spare
4750 * list of every other pool.
4753 spa_t *altspa = NULL;
4755 mutex_enter(&spa_namespace_lock);
4756 while ((altspa = spa_next(altspa)) != NULL) {
4757 if (altspa->spa_state != POOL_STATE_ACTIVE ||
4761 spa_open_ref(altspa, FTAG);
4762 mutex_exit(&spa_namespace_lock);
4763 (void) spa_vdev_remove(altspa, unspare_guid, B_TRUE);
4764 mutex_enter(&spa_namespace_lock);
4765 spa_close(altspa, FTAG);
4767 mutex_exit(&spa_namespace_lock);
4769 /* search the rest of the vdevs for spares to remove */
4770 spa_vdev_resilver_done(spa);
4773 /* all done with the spa; OK to release */
4774 mutex_enter(&spa_namespace_lock);
4775 spa_close(spa, FTAG);
4776 mutex_exit(&spa_namespace_lock);
4782 * Split a set of devices from their mirrors, and create a new pool from them.
4785 spa_vdev_split_mirror(spa_t *spa, char *newname, nvlist_t *config,
4786 nvlist_t *props, boolean_t exp)
4789 uint64_t txg, *glist;
4791 uint_t c, children, lastlog;
4792 nvlist_t **child, *nvl, *tmp;
4794 char *altroot = NULL;
4795 vdev_t *rvd, **vml = NULL; /* vdev modify list */
4796 boolean_t activate_slog;
4798 ASSERT(spa_writeable(spa));
4800 txg = spa_vdev_enter(spa);
4802 /* clear the log and flush everything up to now */
4803 activate_slog = spa_passivate_log(spa);
4804 (void) spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4805 error = spa_offline_log(spa);
4806 txg = spa_vdev_config_enter(spa);
4809 spa_activate_log(spa);
4812 return (spa_vdev_exit(spa, NULL, txg, error));
4814 /* check new spa name before going any further */
4815 if (spa_lookup(newname) != NULL)
4816 return (spa_vdev_exit(spa, NULL, txg, EEXIST));
4819 * scan through all the children to ensure they're all mirrors
4821 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvl) != 0 ||
4822 nvlist_lookup_nvlist_array(nvl, ZPOOL_CONFIG_CHILDREN, &child,
4824 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4826 /* first, check to ensure we've got the right child count */
4827 rvd = spa->spa_root_vdev;
4829 for (c = 0; c < rvd->vdev_children; c++) {
4830 vdev_t *vd = rvd->vdev_child[c];
4832 /* don't count the holes & logs as children */
4833 if (vd->vdev_islog || vd->vdev_ishole) {
4841 if (children != (lastlog != 0 ? lastlog : rvd->vdev_children))
4842 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4844 /* next, ensure no spare or cache devices are part of the split */
4845 if (nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_SPARES, &tmp) == 0 ||
4846 nvlist_lookup_nvlist(nvl, ZPOOL_CONFIG_L2CACHE, &tmp) == 0)
4847 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
4849 vml = kmem_zalloc(children * sizeof (vdev_t *), KM_PUSHPAGE);
4850 glist = kmem_zalloc(children * sizeof (uint64_t), KM_PUSHPAGE);
4852 /* then, loop over each vdev and validate it */
4853 for (c = 0; c < children; c++) {
4854 uint64_t is_hole = 0;
4856 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
4860 if (spa->spa_root_vdev->vdev_child[c]->vdev_ishole ||
4861 spa->spa_root_vdev->vdev_child[c]->vdev_islog) {
4869 /* which disk is going to be split? */
4870 if (nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_GUID,
4876 /* look it up in the spa */
4877 vml[c] = spa_lookup_by_guid(spa, glist[c], B_FALSE);
4878 if (vml[c] == NULL) {
4883 /* make sure there's nothing stopping the split */
4884 if (vml[c]->vdev_parent->vdev_ops != &vdev_mirror_ops ||
4885 vml[c]->vdev_islog ||
4886 vml[c]->vdev_ishole ||
4887 vml[c]->vdev_isspare ||
4888 vml[c]->vdev_isl2cache ||
4889 !vdev_writeable(vml[c]) ||
4890 vml[c]->vdev_children != 0 ||
4891 vml[c]->vdev_state != VDEV_STATE_HEALTHY ||
4892 c != spa->spa_root_vdev->vdev_child[c]->vdev_id) {
4897 if (vdev_dtl_required(vml[c])) {
4902 /* we need certain info from the top level */
4903 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_ARRAY,
4904 vml[c]->vdev_top->vdev_ms_array) == 0);
4905 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_METASLAB_SHIFT,
4906 vml[c]->vdev_top->vdev_ms_shift) == 0);
4907 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASIZE,
4908 vml[c]->vdev_top->vdev_asize) == 0);
4909 VERIFY(nvlist_add_uint64(child[c], ZPOOL_CONFIG_ASHIFT,
4910 vml[c]->vdev_top->vdev_ashift) == 0);
4914 kmem_free(vml, children * sizeof (vdev_t *));
4915 kmem_free(glist, children * sizeof (uint64_t));
4916 return (spa_vdev_exit(spa, NULL, txg, error));
4919 /* stop writers from using the disks */
4920 for (c = 0; c < children; c++) {
4922 vml[c]->vdev_offline = B_TRUE;
4924 vdev_reopen(spa->spa_root_vdev);
4927 * Temporarily record the splitting vdevs in the spa config. This
4928 * will disappear once the config is regenerated.
4930 VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
4931 VERIFY(nvlist_add_uint64_array(nvl, ZPOOL_CONFIG_SPLIT_LIST,
4932 glist, children) == 0);
4933 kmem_free(glist, children * sizeof (uint64_t));
4935 mutex_enter(&spa->spa_props_lock);
4936 VERIFY(nvlist_add_nvlist(spa->spa_config, ZPOOL_CONFIG_SPLIT,
4938 mutex_exit(&spa->spa_props_lock);
4939 spa->spa_config_splitting = nvl;
4940 vdev_config_dirty(spa->spa_root_vdev);
4942 /* configure and create the new pool */
4943 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME, newname) == 0);
4944 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
4945 exp ? POOL_STATE_EXPORTED : POOL_STATE_ACTIVE) == 0);
4946 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
4947 spa_version(spa)) == 0);
4948 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_TXG,
4949 spa->spa_config_txg) == 0);
4950 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_GUID,
4951 spa_generate_guid(NULL)) == 0);
4952 (void) nvlist_lookup_string(props,
4953 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
4955 /* add the new pool to the namespace */
4956 newspa = spa_add(newname, config, altroot);
4957 newspa->spa_config_txg = spa->spa_config_txg;
4958 spa_set_log_state(newspa, SPA_LOG_CLEAR);
4960 /* release the spa config lock, retaining the namespace lock */
4961 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
4963 if (zio_injection_enabled)
4964 zio_handle_panic_injection(spa, FTAG, 1);
4966 spa_activate(newspa, spa_mode_global);
4967 spa_async_suspend(newspa);
4969 /* create the new pool from the disks of the original pool */
4970 error = spa_load(newspa, SPA_LOAD_IMPORT, SPA_IMPORT_ASSEMBLE, B_TRUE);
4974 /* if that worked, generate a real config for the new pool */
4975 if (newspa->spa_root_vdev != NULL) {
4976 VERIFY(nvlist_alloc(&newspa->spa_config_splitting,
4977 NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
4978 VERIFY(nvlist_add_uint64(newspa->spa_config_splitting,
4979 ZPOOL_CONFIG_SPLIT_GUID, spa_guid(spa)) == 0);
4980 spa_config_set(newspa, spa_config_generate(newspa, NULL, -1ULL,
4985 if (props != NULL) {
4986 spa_configfile_set(newspa, props, B_FALSE);
4987 error = spa_prop_set(newspa, props);
4992 /* flush everything */
4993 txg = spa_vdev_config_enter(newspa);
4994 vdev_config_dirty(newspa->spa_root_vdev);
4995 (void) spa_vdev_config_exit(newspa, NULL, txg, 0, FTAG);
4997 if (zio_injection_enabled)
4998 zio_handle_panic_injection(spa, FTAG, 2);
5000 spa_async_resume(newspa);
5002 /* finally, update the original pool's config */
5003 txg = spa_vdev_config_enter(spa);
5004 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
5005 error = dmu_tx_assign(tx, TXG_WAIT);
5008 for (c = 0; c < children; c++) {
5009 if (vml[c] != NULL) {
5012 spa_history_log_internal(LOG_POOL_VDEV_DETACH,
5018 vdev_config_dirty(spa->spa_root_vdev);
5019 spa->spa_config_splitting = NULL;
5023 (void) spa_vdev_exit(spa, NULL, txg, 0);
5025 if (zio_injection_enabled)
5026 zio_handle_panic_injection(spa, FTAG, 3);
5028 /* split is complete; log a history record */
5029 spa_history_log_internal(LOG_POOL_SPLIT, newspa, NULL,
5030 "split new pool %s from pool %s", newname, spa_name(spa));
5032 kmem_free(vml, children * sizeof (vdev_t *));
5034 /* if we're not going to mount the filesystems in userland, export */
5036 error = spa_export_common(newname, POOL_STATE_EXPORTED, NULL,
5043 spa_deactivate(newspa);
5046 txg = spa_vdev_config_enter(spa);
5048 /* re-online all offlined disks */
5049 for (c = 0; c < children; c++) {
5051 vml[c]->vdev_offline = B_FALSE;
5053 vdev_reopen(spa->spa_root_vdev);
5055 nvlist_free(spa->spa_config_splitting);
5056 spa->spa_config_splitting = NULL;
5057 (void) spa_vdev_exit(spa, NULL, txg, error);
5059 kmem_free(vml, children * sizeof (vdev_t *));
5064 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
5068 for (i = 0; i < count; i++) {
5071 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
5074 if (guid == target_guid)
5082 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
5083 nvlist_t *dev_to_remove)
5085 nvlist_t **newdev = NULL;
5089 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_PUSHPAGE);
5091 for (i = 0, j = 0; i < count; i++) {
5092 if (dev[i] == dev_to_remove)
5094 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_PUSHPAGE) == 0);
5097 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
5098 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
5100 for (i = 0; i < count - 1; i++)
5101 nvlist_free(newdev[i]);
5104 kmem_free(newdev, (count - 1) * sizeof (void *));
5108 * Evacuate the device.
5111 spa_vdev_remove_evacuate(spa_t *spa, vdev_t *vd)
5116 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5117 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5118 ASSERT(vd == vd->vdev_top);
5121 * Evacuate the device. We don't hold the config lock as writer
5122 * since we need to do I/O but we do keep the
5123 * spa_namespace_lock held. Once this completes the device
5124 * should no longer have any blocks allocated on it.
5126 if (vd->vdev_islog) {
5127 if (vd->vdev_stat.vs_alloc != 0)
5128 error = spa_offline_log(spa);
5137 * The evacuation succeeded. Remove any remaining MOS metadata
5138 * associated with this vdev, and wait for these changes to sync.
5140 ASSERT3U(vd->vdev_stat.vs_alloc, ==, 0);
5141 txg = spa_vdev_config_enter(spa);
5142 vd->vdev_removing = B_TRUE;
5143 vdev_dirty(vd, 0, NULL, txg);
5144 vdev_config_dirty(vd);
5145 spa_vdev_config_exit(spa, NULL, txg, 0, FTAG);
5151 * Complete the removal by cleaning up the namespace.
5154 spa_vdev_remove_from_namespace(spa_t *spa, vdev_t *vd)
5156 vdev_t *rvd = spa->spa_root_vdev;
5157 uint64_t id = vd->vdev_id;
5158 boolean_t last_vdev = (id == (rvd->vdev_children - 1));
5160 ASSERT(MUTEX_HELD(&spa_namespace_lock));
5161 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
5162 ASSERT(vd == vd->vdev_top);
5165 * Only remove any devices which are empty.
5167 if (vd->vdev_stat.vs_alloc != 0)
5170 (void) vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
5172 if (list_link_active(&vd->vdev_state_dirty_node))
5173 vdev_state_clean(vd);
5174 if (list_link_active(&vd->vdev_config_dirty_node))
5175 vdev_config_clean(vd);
5180 vdev_compact_children(rvd);
5182 vd = vdev_alloc_common(spa, id, 0, &vdev_hole_ops);
5183 vdev_add_child(rvd, vd);
5185 vdev_config_dirty(rvd);
5188 * Reassess the health of our root vdev.
5194 * Remove a device from the pool -
5196 * Removing a device from the vdev namespace requires several steps
5197 * and can take a significant amount of time. As a result we use
5198 * the spa_vdev_config_[enter/exit] functions which allow us to
5199 * grab and release the spa_config_lock while still holding the namespace
5200 * lock. During each step the configuration is synced out.
5204 * Remove a device from the pool. Currently, this supports removing only hot
5205 * spares, slogs, and level 2 ARC devices.
5208 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
5211 metaslab_group_t *mg;
5212 nvlist_t **spares, **l2cache, *nv;
5214 uint_t nspares, nl2cache;
5216 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
5218 ASSERT(spa_writeable(spa));
5221 txg = spa_vdev_enter(spa);
5223 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
5225 if (spa->spa_spares.sav_vdevs != NULL &&
5226 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
5227 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
5228 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
5230 * Only remove the hot spare if it's not currently in use
5233 if (vd == NULL || unspare) {
5234 spa_vdev_remove_aux(spa->spa_spares.sav_config,
5235 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
5236 spa_load_spares(spa);
5237 spa->spa_spares.sav_sync = B_TRUE;
5241 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
5242 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
5243 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
5244 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
5246 * Cache devices can always be removed.
5248 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
5249 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
5250 spa_load_l2cache(spa);
5251 spa->spa_l2cache.sav_sync = B_TRUE;
5252 } else if (vd != NULL && vd->vdev_islog) {
5254 ASSERT(vd == vd->vdev_top);
5257 * XXX - Once we have bp-rewrite this should
5258 * become the common case.
5264 * Stop allocating from this vdev.
5266 metaslab_group_passivate(mg);
5269 * Wait for the youngest allocations and frees to sync,
5270 * and then wait for the deferral of those frees to finish.
5272 spa_vdev_config_exit(spa, NULL,
5273 txg + TXG_CONCURRENT_STATES + TXG_DEFER_SIZE, 0, FTAG);
5276 * Attempt to evacuate the vdev.
5278 error = spa_vdev_remove_evacuate(spa, vd);
5280 txg = spa_vdev_config_enter(spa);
5283 * If we couldn't evacuate the vdev, unwind.
5286 metaslab_group_activate(mg);
5287 return (spa_vdev_exit(spa, NULL, txg, error));
5291 * Clean up the vdev namespace.
5293 spa_vdev_remove_from_namespace(spa, vd);
5295 } else if (vd != NULL) {
5297 * Normal vdevs cannot be removed (yet).
5302 * There is no vdev of any kind with the specified guid.
5308 return (spa_vdev_exit(spa, NULL, txg, error));
5314 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5315 * current spared, so we can detach it.
5318 spa_vdev_resilver_done_hunt(vdev_t *vd)
5320 vdev_t *newvd, *oldvd;
5323 for (c = 0; c < vd->vdev_children; c++) {
5324 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
5330 * Check for a completed replacement. We always consider the first
5331 * vdev in the list to be the oldest vdev, and the last one to be
5332 * the newest (see spa_vdev_attach() for how that works). In
5333 * the case where the newest vdev is faulted, we will not automatically
5334 * remove it after a resilver completes. This is OK as it will require
5335 * user intervention to determine which disk the admin wishes to keep.
5337 if (vd->vdev_ops == &vdev_replacing_ops) {
5338 ASSERT(vd->vdev_children > 1);
5340 newvd = vd->vdev_child[vd->vdev_children - 1];
5341 oldvd = vd->vdev_child[0];
5343 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
5344 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5345 !vdev_dtl_required(oldvd))
5350 * Check for a completed resilver with the 'unspare' flag set.
5352 if (vd->vdev_ops == &vdev_spare_ops) {
5353 vdev_t *first = vd->vdev_child[0];
5354 vdev_t *last = vd->vdev_child[vd->vdev_children - 1];
5356 if (last->vdev_unspare) {
5359 } else if (first->vdev_unspare) {
5366 if (oldvd != NULL &&
5367 vdev_dtl_empty(newvd, DTL_MISSING) &&
5368 vdev_dtl_empty(newvd, DTL_OUTAGE) &&
5369 !vdev_dtl_required(oldvd))
5373 * If there are more than two spares attached to a disk,
5374 * and those spares are not required, then we want to
5375 * attempt to free them up now so that they can be used
5376 * by other pools. Once we're back down to a single
5377 * disk+spare, we stop removing them.
5379 if (vd->vdev_children > 2) {
5380 newvd = vd->vdev_child[1];
5382 if (newvd->vdev_isspare && last->vdev_isspare &&
5383 vdev_dtl_empty(last, DTL_MISSING) &&
5384 vdev_dtl_empty(last, DTL_OUTAGE) &&
5385 !vdev_dtl_required(newvd))
5394 spa_vdev_resilver_done(spa_t *spa)
5396 vdev_t *vd, *pvd, *ppvd;
5397 uint64_t guid, sguid, pguid, ppguid;
5399 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5401 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
5402 pvd = vd->vdev_parent;
5403 ppvd = pvd->vdev_parent;
5404 guid = vd->vdev_guid;
5405 pguid = pvd->vdev_guid;
5406 ppguid = ppvd->vdev_guid;
5409 * If we have just finished replacing a hot spared device, then
5410 * we need to detach the parent's first child (the original hot
5413 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0 &&
5414 ppvd->vdev_children == 2) {
5415 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
5416 sguid = ppvd->vdev_child[1]->vdev_guid;
5418 spa_config_exit(spa, SCL_ALL, FTAG);
5419 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
5421 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
5423 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
5426 spa_config_exit(spa, SCL_ALL, FTAG);
5430 * Update the stored path or FRU for this vdev.
5433 spa_vdev_set_common(spa_t *spa, uint64_t guid, const char *value,
5437 boolean_t sync = B_FALSE;
5439 ASSERT(spa_writeable(spa));
5441 spa_vdev_state_enter(spa, SCL_ALL);
5443 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL)
5444 return (spa_vdev_state_exit(spa, NULL, ENOENT));
5446 if (!vd->vdev_ops->vdev_op_leaf)
5447 return (spa_vdev_state_exit(spa, NULL, ENOTSUP));
5450 if (strcmp(value, vd->vdev_path) != 0) {
5451 spa_strfree(vd->vdev_path);
5452 vd->vdev_path = spa_strdup(value);
5456 if (vd->vdev_fru == NULL) {
5457 vd->vdev_fru = spa_strdup(value);
5459 } else if (strcmp(value, vd->vdev_fru) != 0) {
5460 spa_strfree(vd->vdev_fru);
5461 vd->vdev_fru = spa_strdup(value);
5466 return (spa_vdev_state_exit(spa, sync ? vd : NULL, 0));
5470 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
5472 return (spa_vdev_set_common(spa, guid, newpath, B_TRUE));
5476 spa_vdev_setfru(spa_t *spa, uint64_t guid, const char *newfru)
5478 return (spa_vdev_set_common(spa, guid, newfru, B_FALSE));
5482 * ==========================================================================
5484 * ==========================================================================
5488 spa_scan_stop(spa_t *spa)
5490 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5491 if (dsl_scan_resilvering(spa->spa_dsl_pool))
5493 return (dsl_scan_cancel(spa->spa_dsl_pool));
5497 spa_scan(spa_t *spa, pool_scan_func_t func)
5499 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
5501 if (func >= POOL_SCAN_FUNCS || func == POOL_SCAN_NONE)
5505 * If a resilver was requested, but there is no DTL on a
5506 * writeable leaf device, we have nothing to do.
5508 if (func == POOL_SCAN_RESILVER &&
5509 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
5510 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
5514 return (dsl_scan(spa->spa_dsl_pool, func));
5518 * ==========================================================================
5519 * SPA async task processing
5520 * ==========================================================================
5524 spa_async_remove(spa_t *spa, vdev_t *vd)
5528 if (vd->vdev_remove_wanted) {
5529 vd->vdev_remove_wanted = B_FALSE;
5530 vd->vdev_delayed_close = B_FALSE;
5531 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
5534 * We want to clear the stats, but we don't want to do a full
5535 * vdev_clear() as that will cause us to throw away
5536 * degraded/faulted state as well as attempt to reopen the
5537 * device, all of which is a waste.
5539 vd->vdev_stat.vs_read_errors = 0;
5540 vd->vdev_stat.vs_write_errors = 0;
5541 vd->vdev_stat.vs_checksum_errors = 0;
5543 vdev_state_dirty(vd->vdev_top);
5546 for (c = 0; c < vd->vdev_children; c++)
5547 spa_async_remove(spa, vd->vdev_child[c]);
5551 spa_async_probe(spa_t *spa, vdev_t *vd)
5555 if (vd->vdev_probe_wanted) {
5556 vd->vdev_probe_wanted = B_FALSE;
5557 vdev_reopen(vd); /* vdev_open() does the actual probe */
5560 for (c = 0; c < vd->vdev_children; c++)
5561 spa_async_probe(spa, vd->vdev_child[c]);
5565 spa_async_autoexpand(spa_t *spa, vdev_t *vd)
5569 if (!spa->spa_autoexpand)
5572 for (c = 0; c < vd->vdev_children; c++) {
5573 vdev_t *cvd = vd->vdev_child[c];
5574 spa_async_autoexpand(spa, cvd);
5577 if (!vd->vdev_ops->vdev_op_leaf || vd->vdev_physpath == NULL)
5580 spa_event_notify(vd->vdev_spa, vd, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND);
5584 spa_async_thread(spa_t *spa)
5588 ASSERT(spa->spa_sync_on);
5590 mutex_enter(&spa->spa_async_lock);
5591 tasks = spa->spa_async_tasks;
5592 spa->spa_async_tasks = 0;
5593 mutex_exit(&spa->spa_async_lock);
5596 * See if the config needs to be updated.
5598 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
5599 uint64_t old_space, new_space;
5601 mutex_enter(&spa_namespace_lock);
5602 old_space = metaslab_class_get_space(spa_normal_class(spa));
5603 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
5604 new_space = metaslab_class_get_space(spa_normal_class(spa));
5605 mutex_exit(&spa_namespace_lock);
5608 * If the pool grew as a result of the config update,
5609 * then log an internal history event.
5611 if (new_space != old_space) {
5612 spa_history_log_internal(LOG_POOL_VDEV_ONLINE,
5614 "pool '%s' size: %llu(+%llu)",
5615 spa_name(spa), new_space, new_space - old_space);
5620 * See if any devices need to be marked REMOVED.
5622 if (tasks & SPA_ASYNC_REMOVE) {
5623 spa_vdev_state_enter(spa, SCL_NONE);
5624 spa_async_remove(spa, spa->spa_root_vdev);
5625 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
5626 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
5627 for (i = 0; i < spa->spa_spares.sav_count; i++)
5628 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
5629 (void) spa_vdev_state_exit(spa, NULL, 0);
5632 if ((tasks & SPA_ASYNC_AUTOEXPAND) && !spa_suspended(spa)) {
5633 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
5634 spa_async_autoexpand(spa, spa->spa_root_vdev);
5635 spa_config_exit(spa, SCL_CONFIG, FTAG);
5639 * See if any devices need to be probed.
5641 if (tasks & SPA_ASYNC_PROBE) {
5642 spa_vdev_state_enter(spa, SCL_NONE);
5643 spa_async_probe(spa, spa->spa_root_vdev);
5644 (void) spa_vdev_state_exit(spa, NULL, 0);
5648 * If any devices are done replacing, detach them.
5650 if (tasks & SPA_ASYNC_RESILVER_DONE)
5651 spa_vdev_resilver_done(spa);
5654 * Kick off a resilver.
5656 if (tasks & SPA_ASYNC_RESILVER)
5657 dsl_resilver_restart(spa->spa_dsl_pool, 0);
5660 * Let the world know that we're done.
5662 mutex_enter(&spa->spa_async_lock);
5663 spa->spa_async_thread = NULL;
5664 cv_broadcast(&spa->spa_async_cv);
5665 mutex_exit(&spa->spa_async_lock);
5670 spa_async_suspend(spa_t *spa)
5672 mutex_enter(&spa->spa_async_lock);
5673 spa->spa_async_suspended++;
5674 while (spa->spa_async_thread != NULL)
5675 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
5676 mutex_exit(&spa->spa_async_lock);
5680 spa_async_resume(spa_t *spa)
5682 mutex_enter(&spa->spa_async_lock);
5683 ASSERT(spa->spa_async_suspended != 0);
5684 spa->spa_async_suspended--;
5685 mutex_exit(&spa->spa_async_lock);
5689 spa_async_dispatch(spa_t *spa)
5691 mutex_enter(&spa->spa_async_lock);
5692 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
5693 spa->spa_async_thread == NULL &&
5694 rootdir != NULL && !vn_is_readonly(rootdir))
5695 spa->spa_async_thread = thread_create(NULL, 0,
5696 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
5697 mutex_exit(&spa->spa_async_lock);
5701 spa_async_request(spa_t *spa, int task)
5703 zfs_dbgmsg("spa=%s async request task=%u", spa->spa_name, task);
5704 mutex_enter(&spa->spa_async_lock);
5705 spa->spa_async_tasks |= task;
5706 mutex_exit(&spa->spa_async_lock);
5710 * ==========================================================================
5711 * SPA syncing routines
5712 * ==========================================================================
5716 bpobj_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5719 bpobj_enqueue(bpo, bp, tx);
5724 spa_free_sync_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
5728 zio_nowait(zio_free_sync(zio, zio->io_spa, dmu_tx_get_txg(tx), bp,
5734 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
5736 char *packed = NULL;
5741 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
5744 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5745 * information. This avoids the dbuf_will_dirty() path and
5746 * saves us a pre-read to get data we don't actually care about.
5748 bufsize = P2ROUNDUP((uint64_t)nvsize, SPA_CONFIG_BLOCKSIZE);
5749 packed = vmem_alloc(bufsize, KM_PUSHPAGE);
5751 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
5753 bzero(packed + nvsize, bufsize - nvsize);
5755 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
5757 vmem_free(packed, bufsize);
5759 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
5760 dmu_buf_will_dirty(db, tx);
5761 *(uint64_t *)db->db_data = nvsize;
5762 dmu_buf_rele(db, FTAG);
5766 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
5767 const char *config, const char *entry)
5777 * Update the MOS nvlist describing the list of available devices.
5778 * spa_validate_aux() will have already made sure this nvlist is
5779 * valid and the vdevs are labeled appropriately.
5781 if (sav->sav_object == 0) {
5782 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
5783 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
5784 sizeof (uint64_t), tx);
5785 VERIFY(zap_update(spa->spa_meta_objset,
5786 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
5787 &sav->sav_object, tx) == 0);
5790 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_PUSHPAGE) == 0);
5791 if (sav->sav_count == 0) {
5792 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
5794 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_PUSHPAGE);
5795 for (i = 0; i < sav->sav_count; i++)
5796 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
5797 B_FALSE, VDEV_CONFIG_L2CACHE);
5798 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
5799 sav->sav_count) == 0);
5800 for (i = 0; i < sav->sav_count; i++)
5801 nvlist_free(list[i]);
5802 kmem_free(list, sav->sav_count * sizeof (void *));
5805 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
5806 nvlist_free(nvroot);
5808 sav->sav_sync = B_FALSE;
5812 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
5816 if (list_is_empty(&spa->spa_config_dirty_list))
5819 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
5821 config = spa_config_generate(spa, spa->spa_root_vdev,
5822 dmu_tx_get_txg(tx), B_FALSE);
5825 * If we're upgrading the spa version then make sure that
5826 * the config object gets updated with the correct version.
5828 if (spa->spa_ubsync.ub_version < spa->spa_uberblock.ub_version)
5829 fnvlist_add_uint64(config, ZPOOL_CONFIG_VERSION,
5830 spa->spa_uberblock.ub_version);
5832 spa_config_exit(spa, SCL_STATE, FTAG);
5834 if (spa->spa_config_syncing)
5835 nvlist_free(spa->spa_config_syncing);
5836 spa->spa_config_syncing = config;
5838 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
5842 spa_sync_version(void *arg1, void *arg2, dmu_tx_t *tx)
5845 uint64_t version = *(uint64_t *)arg2;
5848 * Setting the version is special cased when first creating the pool.
5850 ASSERT(tx->tx_txg != TXG_INITIAL);
5852 ASSERT(SPA_VERSION_IS_SUPPORTED(version));
5853 ASSERT(version >= spa_version(spa));
5855 spa->spa_uberblock.ub_version = version;
5856 vdev_config_dirty(spa->spa_root_vdev);
5860 * Set zpool properties.
5863 spa_sync_props(void *arg1, void *arg2, dmu_tx_t *tx)
5866 objset_t *mos = spa->spa_meta_objset;
5867 nvlist_t *nvp = arg2;
5868 nvpair_t *elem = NULL;
5870 mutex_enter(&spa->spa_props_lock);
5872 while ((elem = nvlist_next_nvpair(nvp, elem))) {
5874 char *strval, *fname;
5876 const char *propname;
5877 zprop_type_t proptype;
5878 zfeature_info_t *feature;
5880 prop = zpool_name_to_prop(nvpair_name(elem));
5881 switch ((int)prop) {
5884 * We checked this earlier in spa_prop_validate().
5886 ASSERT(zpool_prop_feature(nvpair_name(elem)));
5888 fname = strchr(nvpair_name(elem), '@') + 1;
5889 VERIFY3U(0, ==, zfeature_lookup_name(fname, &feature));
5891 spa_feature_enable(spa, feature, tx);
5894 case ZPOOL_PROP_VERSION:
5895 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5897 * The version is synced seperatly before other
5898 * properties and should be correct by now.
5900 ASSERT3U(spa_version(spa), >=, intval);
5903 case ZPOOL_PROP_ALTROOT:
5905 * 'altroot' is a non-persistent property. It should
5906 * have been set temporarily at creation or import time.
5908 ASSERT(spa->spa_root != NULL);
5911 case ZPOOL_PROP_READONLY:
5912 case ZPOOL_PROP_CACHEFILE:
5914 * 'readonly' and 'cachefile' are also non-persisitent
5918 case ZPOOL_PROP_COMMENT:
5919 VERIFY(nvpair_value_string(elem, &strval) == 0);
5920 if (spa->spa_comment != NULL)
5921 spa_strfree(spa->spa_comment);
5922 spa->spa_comment = spa_strdup(strval);
5924 * We need to dirty the configuration on all the vdevs
5925 * so that their labels get updated. It's unnecessary
5926 * to do this for pool creation since the vdev's
5927 * configuratoin has already been dirtied.
5929 if (tx->tx_txg != TXG_INITIAL)
5930 vdev_config_dirty(spa->spa_root_vdev);
5934 * Set pool property values in the poolprops mos object.
5936 if (spa->spa_pool_props_object == 0) {
5937 spa->spa_pool_props_object =
5938 zap_create_link(mos, DMU_OT_POOL_PROPS,
5939 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
5943 /* normalize the property name */
5944 propname = zpool_prop_to_name(prop);
5945 proptype = zpool_prop_get_type(prop);
5947 if (nvpair_type(elem) == DATA_TYPE_STRING) {
5948 ASSERT(proptype == PROP_TYPE_STRING);
5949 VERIFY(nvpair_value_string(elem, &strval) == 0);
5950 VERIFY(zap_update(mos,
5951 spa->spa_pool_props_object, propname,
5952 1, strlen(strval) + 1, strval, tx) == 0);
5954 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
5955 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
5957 if (proptype == PROP_TYPE_INDEX) {
5959 VERIFY(zpool_prop_index_to_string(
5960 prop, intval, &unused) == 0);
5962 VERIFY(zap_update(mos,
5963 spa->spa_pool_props_object, propname,
5964 8, 1, &intval, tx) == 0);
5966 ASSERT(0); /* not allowed */
5970 case ZPOOL_PROP_DELEGATION:
5971 spa->spa_delegation = intval;
5973 case ZPOOL_PROP_BOOTFS:
5974 spa->spa_bootfs = intval;
5976 case ZPOOL_PROP_FAILUREMODE:
5977 spa->spa_failmode = intval;
5979 case ZPOOL_PROP_AUTOEXPAND:
5980 spa->spa_autoexpand = intval;
5981 if (tx->tx_txg != TXG_INITIAL)
5982 spa_async_request(spa,
5983 SPA_ASYNC_AUTOEXPAND);
5985 case ZPOOL_PROP_DEDUPDITTO:
5986 spa->spa_dedup_ditto = intval;
5993 /* log internal history if this is not a zpool create */
5994 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
5995 tx->tx_txg != TXG_INITIAL) {
5996 spa_history_log_internal(LOG_POOL_PROPSET,
5997 spa, tx, "%s %lld %s",
5998 nvpair_name(elem), intval, spa_name(spa));
6002 mutex_exit(&spa->spa_props_lock);
6006 * Perform one-time upgrade on-disk changes. spa_version() does not
6007 * reflect the new version this txg, so there must be no changes this
6008 * txg to anything that the upgrade code depends on after it executes.
6009 * Therefore this must be called after dsl_pool_sync() does the sync
6013 spa_sync_upgrades(spa_t *spa, dmu_tx_t *tx)
6015 dsl_pool_t *dp = spa->spa_dsl_pool;
6017 ASSERT(spa->spa_sync_pass == 1);
6019 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
6020 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
6021 dsl_pool_create_origin(dp, tx);
6023 /* Keeping the origin open increases spa_minref */
6024 spa->spa_minref += 3;
6027 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
6028 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
6029 dsl_pool_upgrade_clones(dp, tx);
6032 if (spa->spa_ubsync.ub_version < SPA_VERSION_DIR_CLONES &&
6033 spa->spa_uberblock.ub_version >= SPA_VERSION_DIR_CLONES) {
6034 dsl_pool_upgrade_dir_clones(dp, tx);
6036 /* Keeping the freedir open increases spa_minref */
6037 spa->spa_minref += 3;
6040 if (spa->spa_ubsync.ub_version < SPA_VERSION_FEATURES &&
6041 spa->spa_uberblock.ub_version >= SPA_VERSION_FEATURES) {
6042 spa_feature_create_zap_objects(spa, tx);
6047 * Sync the specified transaction group. New blocks may be dirtied as
6048 * part of the process, so we iterate until it converges.
6051 spa_sync(spa_t *spa, uint64_t txg)
6053 dsl_pool_t *dp = spa->spa_dsl_pool;
6054 objset_t *mos = spa->spa_meta_objset;
6055 bpobj_t *defer_bpo = &spa->spa_deferred_bpobj;
6056 bplist_t *free_bpl = &spa->spa_free_bplist[txg & TXG_MASK];
6057 vdev_t *rvd = spa->spa_root_vdev;
6063 VERIFY(spa_writeable(spa));
6066 * Lock out configuration changes.
6068 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
6070 spa->spa_syncing_txg = txg;
6071 spa->spa_sync_pass = 0;
6074 * If there are any pending vdev state changes, convert them
6075 * into config changes that go out with this transaction group.
6077 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6078 while (list_head(&spa->spa_state_dirty_list) != NULL) {
6080 * We need the write lock here because, for aux vdevs,
6081 * calling vdev_config_dirty() modifies sav_config.
6082 * This is ugly and will become unnecessary when we
6083 * eliminate the aux vdev wart by integrating all vdevs
6084 * into the root vdev tree.
6086 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6087 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
6088 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
6089 vdev_state_clean(vd);
6090 vdev_config_dirty(vd);
6092 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
6093 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
6095 spa_config_exit(spa, SCL_STATE, FTAG);
6097 tx = dmu_tx_create_assigned(dp, txg);
6099 spa->spa_sync_starttime = gethrtime();
6100 taskq_cancel_id(system_taskq, spa->spa_deadman_tqid);
6101 spa->spa_deadman_tqid = taskq_dispatch_delay(system_taskq,
6102 spa_deadman, spa, TQ_SLEEP, ddi_get_lbolt() +
6103 NSEC_TO_TICK(spa->spa_deadman_synctime));
6106 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6107 * set spa_deflate if we have no raid-z vdevs.
6109 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
6110 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
6113 for (i = 0; i < rvd->vdev_children; i++) {
6114 vd = rvd->vdev_child[i];
6115 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
6118 if (i == rvd->vdev_children) {
6119 spa->spa_deflate = TRUE;
6120 VERIFY(0 == zap_add(spa->spa_meta_objset,
6121 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
6122 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
6127 * If anything has changed in this txg, or if someone is waiting
6128 * for this txg to sync (eg, spa_vdev_remove()), push the
6129 * deferred frees from the previous txg. If not, leave them
6130 * alone so that we don't generate work on an otherwise idle
6133 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
6134 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
6135 !txg_list_empty(&dp->dp_sync_tasks, txg) ||
6136 ((dsl_scan_active(dp->dp_scan) ||
6137 txg_sync_waiting(dp)) && !spa_shutting_down(spa))) {
6138 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6139 VERIFY3U(bpobj_iterate(defer_bpo,
6140 spa_free_sync_cb, zio, tx), ==, 0);
6141 VERIFY3U(zio_wait(zio), ==, 0);
6145 * Iterate to convergence.
6148 int pass = ++spa->spa_sync_pass;
6150 spa_sync_config_object(spa, tx);
6151 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
6152 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
6153 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
6154 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
6155 spa_errlog_sync(spa, txg);
6156 dsl_pool_sync(dp, txg);
6158 if (pass < zfs_sync_pass_deferred_free) {
6159 zio_t *zio = zio_root(spa, NULL, NULL, 0);
6160 bplist_iterate(free_bpl, spa_free_sync_cb,
6162 VERIFY(zio_wait(zio) == 0);
6164 bplist_iterate(free_bpl, bpobj_enqueue_cb,
6169 dsl_scan_sync(dp, tx);
6171 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)))
6175 spa_sync_upgrades(spa, tx);
6177 } while (dmu_objset_is_dirty(mos, txg));
6180 * Rewrite the vdev configuration (which includes the uberblock)
6181 * to commit the transaction group.
6183 * If there are no dirty vdevs, we sync the uberblock to a few
6184 * random top-level vdevs that are known to be visible in the
6185 * config cache (see spa_vdev_add() for a complete description).
6186 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6190 * We hold SCL_STATE to prevent vdev open/close/etc.
6191 * while we're attempting to write the vdev labels.
6193 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
6195 if (list_is_empty(&spa->spa_config_dirty_list)) {
6196 vdev_t *svd[SPA_DVAS_PER_BP];
6198 int children = rvd->vdev_children;
6199 int c0 = spa_get_random(children);
6201 for (c = 0; c < children; c++) {
6202 vd = rvd->vdev_child[(c0 + c) % children];
6203 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
6205 svd[svdcount++] = vd;
6206 if (svdcount == SPA_DVAS_PER_BP)
6209 error = vdev_config_sync(svd, svdcount, txg, B_FALSE);
6211 error = vdev_config_sync(svd, svdcount, txg,
6214 error = vdev_config_sync(rvd->vdev_child,
6215 rvd->vdev_children, txg, B_FALSE);
6217 error = vdev_config_sync(rvd->vdev_child,
6218 rvd->vdev_children, txg, B_TRUE);
6222 spa->spa_last_synced_guid = rvd->vdev_guid;
6224 spa_config_exit(spa, SCL_STATE, FTAG);
6228 zio_suspend(spa, NULL);
6229 zio_resume_wait(spa);
6233 taskq_cancel_id(system_taskq, spa->spa_deadman_tqid);
6234 spa->spa_deadman_tqid = 0;
6237 * Clear the dirty config list.
6239 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
6240 vdev_config_clean(vd);
6243 * Now that the new config has synced transactionally,
6244 * let it become visible to the config cache.
6246 if (spa->spa_config_syncing != NULL) {
6247 spa_config_set(spa, spa->spa_config_syncing);
6248 spa->spa_config_txg = txg;
6249 spa->spa_config_syncing = NULL;
6252 spa->spa_ubsync = spa->spa_uberblock;
6254 dsl_pool_sync_done(dp, txg);
6257 * Update usable space statistics.
6259 while ((vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg))))
6260 vdev_sync_done(vd, txg);
6262 spa_update_dspace(spa);
6265 * It had better be the case that we didn't dirty anything
6266 * since vdev_config_sync().
6268 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
6269 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
6270 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
6272 spa->spa_sync_pass = 0;
6274 spa_config_exit(spa, SCL_CONFIG, FTAG);
6276 spa_handle_ignored_writes(spa);
6279 * If any async tasks have been requested, kick them off.
6281 spa_async_dispatch(spa);
6285 * Sync all pools. We don't want to hold the namespace lock across these
6286 * operations, so we take a reference on the spa_t and drop the lock during the
6290 spa_sync_allpools(void)
6293 mutex_enter(&spa_namespace_lock);
6294 while ((spa = spa_next(spa)) != NULL) {
6295 if (spa_state(spa) != POOL_STATE_ACTIVE ||
6296 !spa_writeable(spa) || spa_suspended(spa))
6298 spa_open_ref(spa, FTAG);
6299 mutex_exit(&spa_namespace_lock);
6300 txg_wait_synced(spa_get_dsl(spa), 0);
6301 mutex_enter(&spa_namespace_lock);
6302 spa_close(spa, FTAG);
6304 mutex_exit(&spa_namespace_lock);
6308 * ==========================================================================
6309 * Miscellaneous routines
6310 * ==========================================================================
6314 * Remove all pools in the system.
6322 * Remove all cached state. All pools should be closed now,
6323 * so every spa in the AVL tree should be unreferenced.
6325 mutex_enter(&spa_namespace_lock);
6326 while ((spa = spa_next(NULL)) != NULL) {
6328 * Stop async tasks. The async thread may need to detach
6329 * a device that's been replaced, which requires grabbing
6330 * spa_namespace_lock, so we must drop it here.
6332 spa_open_ref(spa, FTAG);
6333 mutex_exit(&spa_namespace_lock);
6334 spa_async_suspend(spa);
6335 mutex_enter(&spa_namespace_lock);
6336 spa_close(spa, FTAG);
6338 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
6340 spa_deactivate(spa);
6344 mutex_exit(&spa_namespace_lock);
6348 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t aux)
6353 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
6357 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
6358 vd = spa->spa_l2cache.sav_vdevs[i];
6359 if (vd->vdev_guid == guid)
6363 for (i = 0; i < spa->spa_spares.sav_count; i++) {
6364 vd = spa->spa_spares.sav_vdevs[i];
6365 if (vd->vdev_guid == guid)
6374 spa_upgrade(spa_t *spa, uint64_t version)
6376 ASSERT(spa_writeable(spa));
6378 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
6381 * This should only be called for a non-faulted pool, and since a
6382 * future version would result in an unopenable pool, this shouldn't be
6385 ASSERT(SPA_VERSION_IS_SUPPORTED(spa->spa_uberblock.ub_version));
6386 ASSERT(version >= spa->spa_uberblock.ub_version);
6388 spa->spa_uberblock.ub_version = version;
6389 vdev_config_dirty(spa->spa_root_vdev);
6391 spa_config_exit(spa, SCL_ALL, FTAG);
6393 txg_wait_synced(spa_get_dsl(spa), 0);
6397 spa_has_spare(spa_t *spa, uint64_t guid)
6401 spa_aux_vdev_t *sav = &spa->spa_spares;
6403 for (i = 0; i < sav->sav_count; i++)
6404 if (sav->sav_vdevs[i]->vdev_guid == guid)
6407 for (i = 0; i < sav->sav_npending; i++) {
6408 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
6409 &spareguid) == 0 && spareguid == guid)
6417 * Check if a pool has an active shared spare device.
6418 * Note: reference count of an active spare is 2, as a spare and as a replace
6421 spa_has_active_shared_spare(spa_t *spa)
6425 spa_aux_vdev_t *sav = &spa->spa_spares;
6427 for (i = 0; i < sav->sav_count; i++) {
6428 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
6429 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
6438 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
6439 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6440 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6441 * or zdb as real changes.
6444 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
6447 zfs_ereport_post(name, spa, vd, NULL, 0, 0);
6451 #if defined(_KERNEL) && defined(HAVE_SPL)
6452 /* state manipulation functions */
6453 EXPORT_SYMBOL(spa_open);
6454 EXPORT_SYMBOL(spa_open_rewind);
6455 EXPORT_SYMBOL(spa_get_stats);
6456 EXPORT_SYMBOL(spa_create);
6457 EXPORT_SYMBOL(spa_import_rootpool);
6458 EXPORT_SYMBOL(spa_import);
6459 EXPORT_SYMBOL(spa_tryimport);
6460 EXPORT_SYMBOL(spa_destroy);
6461 EXPORT_SYMBOL(spa_export);
6462 EXPORT_SYMBOL(spa_reset);
6463 EXPORT_SYMBOL(spa_async_request);
6464 EXPORT_SYMBOL(spa_async_suspend);
6465 EXPORT_SYMBOL(spa_async_resume);
6466 EXPORT_SYMBOL(spa_inject_addref);
6467 EXPORT_SYMBOL(spa_inject_delref);
6468 EXPORT_SYMBOL(spa_scan_stat_init);
6469 EXPORT_SYMBOL(spa_scan_get_stats);
6471 /* device maniion */
6472 EXPORT_SYMBOL(spa_vdev_add);
6473 EXPORT_SYMBOL(spa_vdev_attach);
6474 EXPORT_SYMBOL(spa_vdev_detach);
6475 EXPORT_SYMBOL(spa_vdev_remove);
6476 EXPORT_SYMBOL(spa_vdev_setpath);
6477 EXPORT_SYMBOL(spa_vdev_setfru);
6478 EXPORT_SYMBOL(spa_vdev_split_mirror);
6480 /* spare statech is global across all pools) */
6481 EXPORT_SYMBOL(spa_spare_add);
6482 EXPORT_SYMBOL(spa_spare_remove);
6483 EXPORT_SYMBOL(spa_spare_exists);
6484 EXPORT_SYMBOL(spa_spare_activate);
6486 /* L2ARC statech is global across all pools) */
6487 EXPORT_SYMBOL(spa_l2cache_add);
6488 EXPORT_SYMBOL(spa_l2cache_remove);
6489 EXPORT_SYMBOL(spa_l2cache_exists);
6490 EXPORT_SYMBOL(spa_l2cache_activate);
6491 EXPORT_SYMBOL(spa_l2cache_drop);
6494 EXPORT_SYMBOL(spa_scan);
6495 EXPORT_SYMBOL(spa_scan_stop);
6498 EXPORT_SYMBOL(spa_sync); /* only for DMU use */
6499 EXPORT_SYMBOL(spa_sync_allpools);
6502 EXPORT_SYMBOL(spa_prop_set);
6503 EXPORT_SYMBOL(spa_prop_get);
6504 EXPORT_SYMBOL(spa_prop_clear_bootfs);
6506 /* asynchronous event notification */
6507 EXPORT_SYMBOL(spa_event_notify);