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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * This file contains all the routines used when modifying on-disk SPA state.
29 * This includes opening, importing, destroying, exporting a pool, and syncing a
33 #include <sys/zfs_context.h>
34 #include <sys/fm/fs/zfs.h>
35 #include <sys/spa_impl.h>
37 #include <sys/zio_checksum.h>
38 #include <sys/zio_compress.h>
40 #include <sys/dmu_tx.h>
43 #include <sys/vdev_impl.h>
44 #include <sys/metaslab.h>
45 #include <sys/uberblock_impl.h>
48 #include <sys/dmu_traverse.h>
49 #include <sys/dmu_objset.h>
50 #include <sys/unique.h>
51 #include <sys/dsl_pool.h>
52 #include <sys/dsl_dataset.h>
53 #include <sys/dsl_dir.h>
54 #include <sys/dsl_prop.h>
55 #include <sys/dsl_synctask.h>
56 #include <sys/fs/zfs.h>
58 #include <sys/callb.h>
59 #include <sys/systeminfo.h>
60 #include <sys/sunddi.h>
61 #include <sys/spa_boot.h>
64 #include "zfs_comutil.h"
66 int zio_taskq_threads[ZIO_TYPES][ZIO_TASKQ_TYPES] = {
68 { 1, 1 }, /* ZIO_TYPE_NULL */
69 { 1, 8 }, /* ZIO_TYPE_READ */
70 { 8, 1 }, /* ZIO_TYPE_WRITE */
71 { 1, 1 }, /* ZIO_TYPE_FREE */
72 { 1, 1 }, /* ZIO_TYPE_CLAIM */
73 { 1, 1 }, /* ZIO_TYPE_IOCTL */
76 static void spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx);
77 static boolean_t spa_has_active_shared_spare(spa_t *spa);
80 * ==========================================================================
81 * SPA properties routines
82 * ==========================================================================
86 * Add a (source=src, propname=propval) list to an nvlist.
89 spa_prop_add_list(nvlist_t *nvl, zpool_prop_t prop, char *strval,
90 uint64_t intval, zprop_source_t src)
92 const char *propname = zpool_prop_to_name(prop);
95 VERIFY(nvlist_alloc(&propval, NV_UNIQUE_NAME, KM_SLEEP) == 0);
96 VERIFY(nvlist_add_uint64(propval, ZPROP_SOURCE, src) == 0);
99 VERIFY(nvlist_add_string(propval, ZPROP_VALUE, strval) == 0);
101 VERIFY(nvlist_add_uint64(propval, ZPROP_VALUE, intval) == 0);
103 VERIFY(nvlist_add_nvlist(nvl, propname, propval) == 0);
104 nvlist_free(propval);
108 * Get property values from the spa configuration.
111 spa_prop_get_config(spa_t *spa, nvlist_t **nvp)
113 uint64_t size = spa_get_space(spa);
114 uint64_t used = spa_get_alloc(spa);
115 uint64_t cap, version;
116 zprop_source_t src = ZPROP_SRC_NONE;
117 spa_config_dirent_t *dp;
119 ASSERT(MUTEX_HELD(&spa->spa_props_lock));
122 * readonly properties
124 spa_prop_add_list(*nvp, ZPOOL_PROP_NAME, spa_name(spa), 0, src);
125 spa_prop_add_list(*nvp, ZPOOL_PROP_SIZE, NULL, size, src);
126 spa_prop_add_list(*nvp, ZPOOL_PROP_USED, NULL, used, src);
127 spa_prop_add_list(*nvp, ZPOOL_PROP_AVAILABLE, NULL, size - used, src);
129 cap = (size == 0) ? 0 : (used * 100 / size);
130 spa_prop_add_list(*nvp, ZPOOL_PROP_CAPACITY, NULL, cap, src);
132 spa_prop_add_list(*nvp, ZPOOL_PROP_GUID, NULL, spa_guid(spa), src);
133 spa_prop_add_list(*nvp, ZPOOL_PROP_HEALTH, NULL,
134 spa->spa_root_vdev->vdev_state, src);
137 * settable properties that are not stored in the pool property object.
139 version = spa_version(spa);
140 if (version == zpool_prop_default_numeric(ZPOOL_PROP_VERSION))
141 src = ZPROP_SRC_DEFAULT;
143 src = ZPROP_SRC_LOCAL;
144 spa_prop_add_list(*nvp, ZPOOL_PROP_VERSION, NULL, version, src);
146 if (spa->spa_root != NULL)
147 spa_prop_add_list(*nvp, ZPOOL_PROP_ALTROOT, spa->spa_root,
150 if ((dp = list_head(&spa->spa_config_list)) != NULL) {
151 if (dp->scd_path == NULL) {
152 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
153 "none", 0, ZPROP_SRC_LOCAL);
154 } else if (strcmp(dp->scd_path, spa_config_path) != 0) {
155 spa_prop_add_list(*nvp, ZPOOL_PROP_CACHEFILE,
156 dp->scd_path, 0, ZPROP_SRC_LOCAL);
162 * Get zpool property values.
165 spa_prop_get(spa_t *spa, nvlist_t **nvp)
169 objset_t *mos = spa->spa_meta_objset;
172 VERIFY(nvlist_alloc(nvp, NV_UNIQUE_NAME, KM_SLEEP) == 0);
174 mutex_enter(&spa->spa_props_lock);
177 * Get properties from the spa config.
179 spa_prop_get_config(spa, nvp);
181 /* If no pool property object, no more prop to get. */
182 if (spa->spa_pool_props_object == 0) {
183 mutex_exit(&spa->spa_props_lock);
188 * Get properties from the MOS pool property object.
190 for (zap_cursor_init(&zc, mos, spa->spa_pool_props_object);
191 (err = zap_cursor_retrieve(&zc, &za)) == 0;
192 zap_cursor_advance(&zc)) {
195 zprop_source_t src = ZPROP_SRC_DEFAULT;
198 if ((prop = zpool_name_to_prop(za.za_name)) == ZPROP_INVAL)
201 switch (za.za_integer_length) {
203 /* integer property */
204 if (za.za_first_integer !=
205 zpool_prop_default_numeric(prop))
206 src = ZPROP_SRC_LOCAL;
208 if (prop == ZPOOL_PROP_BOOTFS) {
210 dsl_dataset_t *ds = NULL;
212 dp = spa_get_dsl(spa);
213 rw_enter(&dp->dp_config_rwlock, RW_READER);
214 if (err = dsl_dataset_hold_obj(dp,
215 za.za_first_integer, FTAG, &ds)) {
216 rw_exit(&dp->dp_config_rwlock);
221 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1,
223 dsl_dataset_name(ds, strval);
224 dsl_dataset_rele(ds, FTAG);
225 rw_exit(&dp->dp_config_rwlock);
228 intval = za.za_first_integer;
231 spa_prop_add_list(*nvp, prop, strval, intval, src);
235 MAXNAMELEN + strlen(MOS_DIR_NAME) + 1);
240 /* string property */
241 strval = kmem_alloc(za.za_num_integers, KM_SLEEP);
242 err = zap_lookup(mos, spa->spa_pool_props_object,
243 za.za_name, 1, za.za_num_integers, strval);
245 kmem_free(strval, za.za_num_integers);
248 spa_prop_add_list(*nvp, prop, strval, 0, src);
249 kmem_free(strval, za.za_num_integers);
256 zap_cursor_fini(&zc);
257 mutex_exit(&spa->spa_props_lock);
259 if (err && err != ENOENT) {
269 * Validate the given pool properties nvlist and modify the list
270 * for the property values to be set.
273 spa_prop_validate(spa_t *spa, nvlist_t *props)
276 int error = 0, reset_bootfs = 0;
280 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
282 char *propname, *strval;
287 propname = nvpair_name(elem);
289 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL)
293 case ZPOOL_PROP_VERSION:
294 error = nvpair_value_uint64(elem, &intval);
296 (intval < spa_version(spa) || intval > SPA_VERSION))
300 case ZPOOL_PROP_DELEGATION:
301 case ZPOOL_PROP_AUTOREPLACE:
302 case ZPOOL_PROP_LISTSNAPS:
303 error = nvpair_value_uint64(elem, &intval);
304 if (!error && intval > 1)
308 case ZPOOL_PROP_BOOTFS:
309 if (spa_version(spa) < SPA_VERSION_BOOTFS) {
315 * Make sure the vdev config is bootable
317 if (!vdev_is_bootable(spa->spa_root_vdev)) {
324 error = nvpair_value_string(elem, &strval);
329 if (strval == NULL || strval[0] == '\0') {
330 objnum = zpool_prop_default_numeric(
335 if (error = dmu_objset_open(strval, DMU_OST_ZFS,
336 DS_MODE_USER | DS_MODE_READONLY, &os))
339 /* We don't support gzip bootable datasets */
340 if ((error = dsl_prop_get_integer(strval,
341 zfs_prop_to_name(ZFS_PROP_COMPRESSION),
342 &compress, NULL)) == 0 &&
343 !BOOTFS_COMPRESS_VALID(compress)) {
346 objnum = dmu_objset_id(os);
348 dmu_objset_close(os);
352 case ZPOOL_PROP_FAILUREMODE:
353 error = nvpair_value_uint64(elem, &intval);
354 if (!error && (intval < ZIO_FAILURE_MODE_WAIT ||
355 intval > ZIO_FAILURE_MODE_PANIC))
359 * This is a special case which only occurs when
360 * the pool has completely failed. This allows
361 * the user to change the in-core failmode property
362 * without syncing it out to disk (I/Os might
363 * currently be blocked). We do this by returning
364 * EIO to the caller (spa_prop_set) to trick it
365 * into thinking we encountered a property validation
368 if (!error && spa_suspended(spa)) {
369 spa->spa_failmode = intval;
374 case ZPOOL_PROP_CACHEFILE:
375 if ((error = nvpair_value_string(elem, &strval)) != 0)
378 if (strval[0] == '\0')
381 if (strcmp(strval, "none") == 0)
384 if (strval[0] != '/') {
389 slash = strrchr(strval, '/');
390 ASSERT(slash != NULL);
392 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
393 strcmp(slash, "/..") == 0)
402 if (!error && reset_bootfs) {
403 error = nvlist_remove(props,
404 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), DATA_TYPE_STRING);
407 error = nvlist_add_uint64(props,
408 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), objnum);
416 spa_prop_set(spa_t *spa, nvlist_t *nvp)
420 if ((error = spa_prop_validate(spa, nvp)) != 0)
423 return (dsl_sync_task_do(spa_get_dsl(spa), NULL, spa_sync_props,
428 * If the bootfs property value is dsobj, clear it.
431 spa_prop_clear_bootfs(spa_t *spa, uint64_t dsobj, dmu_tx_t *tx)
433 if (spa->spa_bootfs == dsobj && spa->spa_pool_props_object != 0) {
434 VERIFY(zap_remove(spa->spa_meta_objset,
435 spa->spa_pool_props_object,
436 zpool_prop_to_name(ZPOOL_PROP_BOOTFS), tx) == 0);
442 * ==========================================================================
443 * SPA state manipulation (open/create/destroy/import/export)
444 * ==========================================================================
448 spa_error_entry_compare(const void *a, const void *b)
450 spa_error_entry_t *sa = (spa_error_entry_t *)a;
451 spa_error_entry_t *sb = (spa_error_entry_t *)b;
454 ret = bcmp(&sa->se_bookmark, &sb->se_bookmark,
455 sizeof (zbookmark_t));
466 * Utility function which retrieves copies of the current logs and
467 * re-initializes them in the process.
470 spa_get_errlists(spa_t *spa, avl_tree_t *last, avl_tree_t *scrub)
472 ASSERT(MUTEX_HELD(&spa->spa_errlist_lock));
474 bcopy(&spa->spa_errlist_last, last, sizeof (avl_tree_t));
475 bcopy(&spa->spa_errlist_scrub, scrub, sizeof (avl_tree_t));
477 avl_create(&spa->spa_errlist_scrub,
478 spa_error_entry_compare, sizeof (spa_error_entry_t),
479 offsetof(spa_error_entry_t, se_avl));
480 avl_create(&spa->spa_errlist_last,
481 spa_error_entry_compare, sizeof (spa_error_entry_t),
482 offsetof(spa_error_entry_t, se_avl));
486 * Activate an uninitialized pool.
489 spa_activate(spa_t *spa, int mode)
491 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
493 spa->spa_state = POOL_STATE_ACTIVE;
494 spa->spa_mode = mode;
496 spa->spa_normal_class = metaslab_class_create();
497 spa->spa_log_class = metaslab_class_create();
499 for (int t = 0; t < ZIO_TYPES; t++) {
500 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
501 spa->spa_zio_taskq[t][q] = taskq_create("spa_zio",
502 zio_taskq_threads[t][q], maxclsyspri, 50,
503 INT_MAX, TASKQ_PREPOPULATE);
507 list_create(&spa->spa_config_dirty_list, sizeof (vdev_t),
508 offsetof(vdev_t, vdev_config_dirty_node));
509 list_create(&spa->spa_state_dirty_list, sizeof (vdev_t),
510 offsetof(vdev_t, vdev_state_dirty_node));
512 txg_list_create(&spa->spa_vdev_txg_list,
513 offsetof(struct vdev, vdev_txg_node));
515 avl_create(&spa->spa_errlist_scrub,
516 spa_error_entry_compare, sizeof (spa_error_entry_t),
517 offsetof(spa_error_entry_t, se_avl));
518 avl_create(&spa->spa_errlist_last,
519 spa_error_entry_compare, sizeof (spa_error_entry_t),
520 offsetof(spa_error_entry_t, se_avl));
524 * Opposite of spa_activate().
527 spa_deactivate(spa_t *spa)
529 ASSERT(spa->spa_sync_on == B_FALSE);
530 ASSERT(spa->spa_dsl_pool == NULL);
531 ASSERT(spa->spa_root_vdev == NULL);
533 ASSERT(spa->spa_state != POOL_STATE_UNINITIALIZED);
535 txg_list_destroy(&spa->spa_vdev_txg_list);
537 list_destroy(&spa->spa_config_dirty_list);
538 list_destroy(&spa->spa_state_dirty_list);
540 for (int t = 0; t < ZIO_TYPES; t++) {
541 for (int q = 0; q < ZIO_TASKQ_TYPES; q++) {
542 taskq_destroy(spa->spa_zio_taskq[t][q]);
543 spa->spa_zio_taskq[t][q] = NULL;
547 metaslab_class_destroy(spa->spa_normal_class);
548 spa->spa_normal_class = NULL;
550 metaslab_class_destroy(spa->spa_log_class);
551 spa->spa_log_class = NULL;
554 * If this was part of an import or the open otherwise failed, we may
555 * still have errors left in the queues. Empty them just in case.
557 spa_errlog_drain(spa);
559 avl_destroy(&spa->spa_errlist_scrub);
560 avl_destroy(&spa->spa_errlist_last);
562 spa->spa_state = POOL_STATE_UNINITIALIZED;
566 * Verify a pool configuration, and construct the vdev tree appropriately. This
567 * will create all the necessary vdevs in the appropriate layout, with each vdev
568 * in the CLOSED state. This will prep the pool before open/creation/import.
569 * All vdev validation is done by the vdev_alloc() routine.
572 spa_config_parse(spa_t *spa, vdev_t **vdp, nvlist_t *nv, vdev_t *parent,
573 uint_t id, int atype)
579 if ((error = vdev_alloc(spa, vdp, nv, parent, id, atype)) != 0)
582 if ((*vdp)->vdev_ops->vdev_op_leaf)
585 error = nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
597 for (c = 0; c < children; c++) {
599 if ((error = spa_config_parse(spa, &vd, child[c], *vdp, c,
607 ASSERT(*vdp != NULL);
613 * Opposite of spa_load().
616 spa_unload(spa_t *spa)
620 ASSERT(MUTEX_HELD(&spa_namespace_lock));
625 spa_async_suspend(spa);
630 if (spa->spa_sync_on) {
631 txg_sync_stop(spa->spa_dsl_pool);
632 spa->spa_sync_on = B_FALSE;
636 * Wait for any outstanding async I/O to complete.
638 mutex_enter(&spa->spa_async_root_lock);
639 while (spa->spa_async_root_count != 0)
640 cv_wait(&spa->spa_async_root_cv, &spa->spa_async_root_lock);
641 mutex_exit(&spa->spa_async_root_lock);
644 * Close the dsl pool.
646 if (spa->spa_dsl_pool) {
647 dsl_pool_close(spa->spa_dsl_pool);
648 spa->spa_dsl_pool = NULL;
651 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
654 * Drop and purge level 2 cache
656 spa_l2cache_drop(spa);
661 if (spa->spa_root_vdev)
662 vdev_free(spa->spa_root_vdev);
663 ASSERT(spa->spa_root_vdev == NULL);
665 for (i = 0; i < spa->spa_spares.sav_count; i++)
666 vdev_free(spa->spa_spares.sav_vdevs[i]);
667 if (spa->spa_spares.sav_vdevs) {
668 kmem_free(spa->spa_spares.sav_vdevs,
669 spa->spa_spares.sav_count * sizeof (void *));
670 spa->spa_spares.sav_vdevs = NULL;
672 if (spa->spa_spares.sav_config) {
673 nvlist_free(spa->spa_spares.sav_config);
674 spa->spa_spares.sav_config = NULL;
676 spa->spa_spares.sav_count = 0;
678 for (i = 0; i < spa->spa_l2cache.sav_count; i++)
679 vdev_free(spa->spa_l2cache.sav_vdevs[i]);
680 if (spa->spa_l2cache.sav_vdevs) {
681 kmem_free(spa->spa_l2cache.sav_vdevs,
682 spa->spa_l2cache.sav_count * sizeof (void *));
683 spa->spa_l2cache.sav_vdevs = NULL;
685 if (spa->spa_l2cache.sav_config) {
686 nvlist_free(spa->spa_l2cache.sav_config);
687 spa->spa_l2cache.sav_config = NULL;
689 spa->spa_l2cache.sav_count = 0;
691 spa->spa_async_suspended = 0;
693 spa_config_exit(spa, SCL_ALL, FTAG);
697 * Load (or re-load) the current list of vdevs describing the active spares for
698 * this pool. When this is called, we have some form of basic information in
699 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
700 * then re-generate a more complete list including status information.
703 spa_load_spares(spa_t *spa)
710 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
713 * First, close and free any existing spare vdevs.
715 for (i = 0; i < spa->spa_spares.sav_count; i++) {
716 vd = spa->spa_spares.sav_vdevs[i];
718 /* Undo the call to spa_activate() below */
719 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
720 B_FALSE)) != NULL && tvd->vdev_isspare)
721 spa_spare_remove(tvd);
726 if (spa->spa_spares.sav_vdevs)
727 kmem_free(spa->spa_spares.sav_vdevs,
728 spa->spa_spares.sav_count * sizeof (void *));
730 if (spa->spa_spares.sav_config == NULL)
733 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
734 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
736 spa->spa_spares.sav_count = (int)nspares;
737 spa->spa_spares.sav_vdevs = NULL;
743 * Construct the array of vdevs, opening them to get status in the
744 * process. For each spare, there is potentially two different vdev_t
745 * structures associated with it: one in the list of spares (used only
746 * for basic validation purposes) and one in the active vdev
747 * configuration (if it's spared in). During this phase we open and
748 * validate each vdev on the spare list. If the vdev also exists in the
749 * active configuration, then we also mark this vdev as an active spare.
751 spa->spa_spares.sav_vdevs = kmem_alloc(nspares * sizeof (void *),
753 for (i = 0; i < spa->spa_spares.sav_count; i++) {
754 VERIFY(spa_config_parse(spa, &vd, spares[i], NULL, 0,
755 VDEV_ALLOC_SPARE) == 0);
758 spa->spa_spares.sav_vdevs[i] = vd;
760 if ((tvd = spa_lookup_by_guid(spa, vd->vdev_guid,
762 if (!tvd->vdev_isspare)
766 * We only mark the spare active if we were successfully
767 * able to load the vdev. Otherwise, importing a pool
768 * with a bad active spare would result in strange
769 * behavior, because multiple pool would think the spare
770 * is actively in use.
772 * There is a vulnerability here to an equally bizarre
773 * circumstance, where a dead active spare is later
774 * brought back to life (onlined or otherwise). Given
775 * the rarity of this scenario, and the extra complexity
776 * it adds, we ignore the possibility.
778 if (!vdev_is_dead(tvd))
779 spa_spare_activate(tvd);
784 if (vdev_open(vd) != 0)
787 if (vdev_validate_aux(vd) == 0)
792 * Recompute the stashed list of spares, with status information
795 VERIFY(nvlist_remove(spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
796 DATA_TYPE_NVLIST_ARRAY) == 0);
798 spares = kmem_alloc(spa->spa_spares.sav_count * sizeof (void *),
800 for (i = 0; i < spa->spa_spares.sav_count; i++)
801 spares[i] = vdev_config_generate(spa,
802 spa->spa_spares.sav_vdevs[i], B_TRUE, B_TRUE, B_FALSE);
803 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
804 ZPOOL_CONFIG_SPARES, spares, spa->spa_spares.sav_count) == 0);
805 for (i = 0; i < spa->spa_spares.sav_count; i++)
806 nvlist_free(spares[i]);
807 kmem_free(spares, spa->spa_spares.sav_count * sizeof (void *));
811 * Load (or re-load) the current list of vdevs describing the active l2cache for
812 * this pool. When this is called, we have some form of basic information in
813 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
814 * then re-generate a more complete list including status information.
815 * Devices which are already active have their details maintained, and are
819 spa_load_l2cache(spa_t *spa)
825 vdev_t *vd, **oldvdevs, **newvdevs;
826 spa_aux_vdev_t *sav = &spa->spa_l2cache;
828 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
830 if (sav->sav_config != NULL) {
831 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config,
832 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
833 newvdevs = kmem_alloc(nl2cache * sizeof (void *), KM_SLEEP);
838 oldvdevs = sav->sav_vdevs;
839 oldnvdevs = sav->sav_count;
840 sav->sav_vdevs = NULL;
844 * Process new nvlist of vdevs.
846 for (i = 0; i < nl2cache; i++) {
847 VERIFY(nvlist_lookup_uint64(l2cache[i], ZPOOL_CONFIG_GUID,
851 for (j = 0; j < oldnvdevs; j++) {
853 if (vd != NULL && guid == vd->vdev_guid) {
855 * Retain previous vdev for add/remove ops.
863 if (newvdevs[i] == NULL) {
867 VERIFY(spa_config_parse(spa, &vd, l2cache[i], NULL, 0,
868 VDEV_ALLOC_L2CACHE) == 0);
873 * Commit this vdev as an l2cache device,
874 * even if it fails to open.
881 spa_l2cache_activate(vd);
883 if (vdev_open(vd) != 0)
886 (void) vdev_validate_aux(vd);
888 if (!vdev_is_dead(vd)) {
889 size = vdev_get_rsize(vd);
890 l2arc_add_vdev(spa, vd,
891 VDEV_LABEL_START_SIZE,
892 size - VDEV_LABEL_START_SIZE);
898 * Purge vdevs that were dropped
900 for (i = 0; i < oldnvdevs; i++) {
905 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
906 pool != 0ULL && l2arc_vdev_present(vd))
907 l2arc_remove_vdev(vd);
908 (void) vdev_close(vd);
909 spa_l2cache_remove(vd);
914 kmem_free(oldvdevs, oldnvdevs * sizeof (void *));
916 if (sav->sav_config == NULL)
919 sav->sav_vdevs = newvdevs;
920 sav->sav_count = (int)nl2cache;
923 * Recompute the stashed list of l2cache devices, with status
924 * information this time.
926 VERIFY(nvlist_remove(sav->sav_config, ZPOOL_CONFIG_L2CACHE,
927 DATA_TYPE_NVLIST_ARRAY) == 0);
929 l2cache = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
930 for (i = 0; i < sav->sav_count; i++)
931 l2cache[i] = vdev_config_generate(spa,
932 sav->sav_vdevs[i], B_TRUE, B_FALSE, B_TRUE);
933 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
934 ZPOOL_CONFIG_L2CACHE, l2cache, sav->sav_count) == 0);
936 for (i = 0; i < sav->sav_count; i++)
937 nvlist_free(l2cache[i]);
939 kmem_free(l2cache, sav->sav_count * sizeof (void *));
943 load_nvlist(spa_t *spa, uint64_t obj, nvlist_t **value)
951 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
952 nvsize = *(uint64_t *)db->db_data;
953 dmu_buf_rele(db, FTAG);
955 packed = kmem_alloc(nvsize, KM_SLEEP);
956 error = dmu_read(spa->spa_meta_objset, obj, 0, nvsize, packed);
958 error = nvlist_unpack(packed, nvsize, value, 0);
959 kmem_free(packed, nvsize);
965 * Checks to see if the given vdev could not be opened, in which case we post a
966 * sysevent to notify the autoreplace code that the device has been removed.
969 spa_check_removed(vdev_t *vd)
973 for (c = 0; c < vd->vdev_children; c++)
974 spa_check_removed(vd->vdev_child[c]);
976 if (vd->vdev_ops->vdev_op_leaf && vdev_is_dead(vd)) {
977 zfs_post_autoreplace(vd->vdev_spa, vd);
978 spa_event_notify(vd->vdev_spa, vd, ESC_ZFS_VDEV_CHECK);
983 * Check for missing log devices
986 spa_check_logs(spa_t *spa)
988 switch (spa->spa_log_state) {
989 case SPA_LOG_MISSING:
990 /* need to recheck in case slog has been restored */
991 case SPA_LOG_UNKNOWN:
992 if (dmu_objset_find(spa->spa_name, zil_check_log_chain, NULL,
994 spa->spa_log_state = SPA_LOG_MISSING;
1000 (void) dmu_objset_find(spa->spa_name, zil_clear_log_chain, NULL,
1004 spa->spa_log_state = SPA_LOG_GOOD;
1009 * Load an existing storage pool, using the pool's builtin spa_config as a
1010 * source of configuration information.
1013 spa_load(spa_t *spa, nvlist_t *config, spa_load_state_t state, int mosconfig)
1016 nvlist_t *nvroot = NULL;
1018 uberblock_t *ub = &spa->spa_uberblock;
1019 uint64_t config_cache_txg = spa->spa_config_txg;
1022 uint64_t autoreplace = 0;
1023 int orig_mode = spa->spa_mode;
1024 char *ereport = FM_EREPORT_ZFS_POOL;
1027 * If this is an untrusted config, access the pool in read-only mode.
1028 * This prevents things like resilvering recently removed devices.
1031 spa->spa_mode = FREAD;
1033 ASSERT(MUTEX_HELD(&spa_namespace_lock));
1035 spa->spa_load_state = state;
1037 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvroot) ||
1038 nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pool_guid)) {
1044 * Versioning wasn't explicitly added to the label until later, so if
1045 * it's not present treat it as the initial version.
1047 if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) != 0)
1048 version = SPA_VERSION_INITIAL;
1050 (void) nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG,
1051 &spa->spa_config_txg);
1053 if ((state == SPA_LOAD_IMPORT || state == SPA_LOAD_TRYIMPORT) &&
1054 spa_guid_exists(pool_guid, 0)) {
1059 spa->spa_load_guid = pool_guid;
1062 * Parse the configuration into a vdev tree. We explicitly set the
1063 * value that will be returned by spa_version() since parsing the
1064 * configuration requires knowing the version number.
1066 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1067 spa->spa_ubsync.ub_version = version;
1068 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_LOAD);
1069 spa_config_exit(spa, SCL_ALL, FTAG);
1074 ASSERT(spa->spa_root_vdev == rvd);
1075 ASSERT(spa_guid(spa) == pool_guid);
1078 * Try to open all vdevs, loading each label in the process.
1080 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1081 error = vdev_open(rvd);
1082 spa_config_exit(spa, SCL_ALL, FTAG);
1087 * Validate the labels for all leaf vdevs. We need to grab the config
1088 * lock because all label I/O is done with ZIO_FLAG_CONFIG_WRITER.
1091 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1092 error = vdev_validate(rvd);
1093 spa_config_exit(spa, SCL_ALL, FTAG);
1098 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1104 * Find the best uberblock.
1106 vdev_uberblock_load(NULL, rvd, ub);
1109 * If we weren't able to find a single valid uberblock, return failure.
1111 if (ub->ub_txg == 0) {
1112 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1113 VDEV_AUX_CORRUPT_DATA);
1119 * If the pool is newer than the code, we can't open it.
1121 if (ub->ub_version > SPA_VERSION) {
1122 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1123 VDEV_AUX_VERSION_NEWER);
1129 * If the vdev guid sum doesn't match the uberblock, we have an
1130 * incomplete configuration.
1132 if (rvd->vdev_guid_sum != ub->ub_guid_sum && mosconfig) {
1133 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1134 VDEV_AUX_BAD_GUID_SUM);
1140 * Initialize internal SPA structures.
1142 spa->spa_state = POOL_STATE_ACTIVE;
1143 spa->spa_ubsync = spa->spa_uberblock;
1144 spa->spa_first_txg = spa_last_synced_txg(spa) + 1;
1145 error = dsl_pool_open(spa, spa->spa_first_txg, &spa->spa_dsl_pool);
1147 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1148 VDEV_AUX_CORRUPT_DATA);
1151 spa->spa_meta_objset = spa->spa_dsl_pool->dp_meta_objset;
1153 if (zap_lookup(spa->spa_meta_objset,
1154 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
1155 sizeof (uint64_t), 1, &spa->spa_config_object) != 0) {
1156 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1157 VDEV_AUX_CORRUPT_DATA);
1163 nvlist_t *newconfig;
1166 if (load_nvlist(spa, spa->spa_config_object, &newconfig) != 0) {
1167 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1168 VDEV_AUX_CORRUPT_DATA);
1173 if (!spa_is_root(spa) && nvlist_lookup_uint64(newconfig,
1174 ZPOOL_CONFIG_HOSTID, &hostid) == 0) {
1176 unsigned long myhostid = 0;
1178 VERIFY(nvlist_lookup_string(newconfig,
1179 ZPOOL_CONFIG_HOSTNAME, &hostname) == 0);
1181 (void) ddi_strtoul(hw_serial, NULL, 10, &myhostid);
1182 if (hostid != 0 && myhostid != 0 &&
1183 (unsigned long)hostid != myhostid) {
1184 cmn_err(CE_WARN, "pool '%s' could not be "
1185 "loaded as it was last accessed by "
1186 "another system (host: %s hostid: 0x%lx). "
1187 "See: http://www.sun.com/msg/ZFS-8000-EY",
1188 spa_name(spa), hostname,
1189 (unsigned long)hostid);
1195 spa_config_set(spa, newconfig);
1197 spa_deactivate(spa);
1198 spa_activate(spa, orig_mode);
1200 return (spa_load(spa, newconfig, state, B_TRUE));
1203 if (zap_lookup(spa->spa_meta_objset,
1204 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
1205 sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj) != 0) {
1206 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1207 VDEV_AUX_CORRUPT_DATA);
1213 * Load the bit that tells us to use the new accounting function
1214 * (raid-z deflation). If we have an older pool, this will not
1217 error = zap_lookup(spa->spa_meta_objset,
1218 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
1219 sizeof (uint64_t), 1, &spa->spa_deflate);
1220 if (error != 0 && error != ENOENT) {
1221 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1222 VDEV_AUX_CORRUPT_DATA);
1228 * Load the persistent error log. If we have an older pool, this will
1231 error = zap_lookup(spa->spa_meta_objset,
1232 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_LAST,
1233 sizeof (uint64_t), 1, &spa->spa_errlog_last);
1234 if (error != 0 && error != ENOENT) {
1235 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1236 VDEV_AUX_CORRUPT_DATA);
1241 error = zap_lookup(spa->spa_meta_objset,
1242 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_ERRLOG_SCRUB,
1243 sizeof (uint64_t), 1, &spa->spa_errlog_scrub);
1244 if (error != 0 && error != ENOENT) {
1245 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1246 VDEV_AUX_CORRUPT_DATA);
1252 * Load the history object. If we have an older pool, this
1253 * will not be present.
1255 error = zap_lookup(spa->spa_meta_objset,
1256 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_HISTORY,
1257 sizeof (uint64_t), 1, &spa->spa_history);
1258 if (error != 0 && error != ENOENT) {
1259 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1260 VDEV_AUX_CORRUPT_DATA);
1266 * Load any hot spares for this pool.
1268 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1269 DMU_POOL_SPARES, sizeof (uint64_t), 1, &spa->spa_spares.sav_object);
1270 if (error != 0 && error != ENOENT) {
1271 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1272 VDEV_AUX_CORRUPT_DATA);
1277 ASSERT(spa_version(spa) >= SPA_VERSION_SPARES);
1278 if (load_nvlist(spa, spa->spa_spares.sav_object,
1279 &spa->spa_spares.sav_config) != 0) {
1280 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1281 VDEV_AUX_CORRUPT_DATA);
1286 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1287 spa_load_spares(spa);
1288 spa_config_exit(spa, SCL_ALL, FTAG);
1292 * Load any level 2 ARC devices for this pool.
1294 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1295 DMU_POOL_L2CACHE, sizeof (uint64_t), 1,
1296 &spa->spa_l2cache.sav_object);
1297 if (error != 0 && error != ENOENT) {
1298 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1299 VDEV_AUX_CORRUPT_DATA);
1304 ASSERT(spa_version(spa) >= SPA_VERSION_L2CACHE);
1305 if (load_nvlist(spa, spa->spa_l2cache.sav_object,
1306 &spa->spa_l2cache.sav_config) != 0) {
1307 vdev_set_state(rvd, B_TRUE,
1308 VDEV_STATE_CANT_OPEN,
1309 VDEV_AUX_CORRUPT_DATA);
1314 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1315 spa_load_l2cache(spa);
1316 spa_config_exit(spa, SCL_ALL, FTAG);
1319 if (spa_check_logs(spa)) {
1320 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1323 ereport = FM_EREPORT_ZFS_LOG_REPLAY;
1328 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
1330 error = zap_lookup(spa->spa_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
1331 DMU_POOL_PROPS, sizeof (uint64_t), 1, &spa->spa_pool_props_object);
1333 if (error && error != ENOENT) {
1334 vdev_set_state(rvd, B_TRUE, VDEV_STATE_CANT_OPEN,
1335 VDEV_AUX_CORRUPT_DATA);
1341 (void) zap_lookup(spa->spa_meta_objset,
1342 spa->spa_pool_props_object,
1343 zpool_prop_to_name(ZPOOL_PROP_BOOTFS),
1344 sizeof (uint64_t), 1, &spa->spa_bootfs);
1345 (void) zap_lookup(spa->spa_meta_objset,
1346 spa->spa_pool_props_object,
1347 zpool_prop_to_name(ZPOOL_PROP_AUTOREPLACE),
1348 sizeof (uint64_t), 1, &autoreplace);
1349 (void) zap_lookup(spa->spa_meta_objset,
1350 spa->spa_pool_props_object,
1351 zpool_prop_to_name(ZPOOL_PROP_DELEGATION),
1352 sizeof (uint64_t), 1, &spa->spa_delegation);
1353 (void) zap_lookup(spa->spa_meta_objset,
1354 spa->spa_pool_props_object,
1355 zpool_prop_to_name(ZPOOL_PROP_FAILUREMODE),
1356 sizeof (uint64_t), 1, &spa->spa_failmode);
1360 * If the 'autoreplace' property is set, then post a resource notifying
1361 * the ZFS DE that it should not issue any faults for unopenable
1362 * devices. We also iterate over the vdevs, and post a sysevent for any
1363 * unopenable vdevs so that the normal autoreplace handler can take
1366 if (autoreplace && state != SPA_LOAD_TRYIMPORT)
1367 spa_check_removed(spa->spa_root_vdev);
1370 * Load the vdev state for all toplevel vdevs.
1375 * Propagate the leaf DTLs we just loaded all the way up the tree.
1377 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1378 vdev_dtl_reassess(rvd, 0, 0, B_FALSE);
1379 spa_config_exit(spa, SCL_ALL, FTAG);
1382 * Check the state of the root vdev. If it can't be opened, it
1383 * indicates one or more toplevel vdevs are faulted.
1385 if (rvd->vdev_state <= VDEV_STATE_CANT_OPEN) {
1390 if (spa_writeable(spa)) {
1392 int need_update = B_FALSE;
1394 ASSERT(state != SPA_LOAD_TRYIMPORT);
1397 * Claim log blocks that haven't been committed yet.
1398 * This must all happen in a single txg.
1400 tx = dmu_tx_create_assigned(spa_get_dsl(spa),
1401 spa_first_txg(spa));
1402 (void) dmu_objset_find(spa_name(spa),
1403 zil_claim, tx, DS_FIND_CHILDREN);
1406 spa->spa_sync_on = B_TRUE;
1407 txg_sync_start(spa->spa_dsl_pool);
1410 * Wait for all claims to sync.
1412 txg_wait_synced(spa->spa_dsl_pool, 0);
1415 * If the config cache is stale, or we have uninitialized
1416 * metaslabs (see spa_vdev_add()), then update the config.
1418 if (config_cache_txg != spa->spa_config_txg ||
1419 state == SPA_LOAD_IMPORT)
1420 need_update = B_TRUE;
1422 for (int c = 0; c < rvd->vdev_children; c++)
1423 if (rvd->vdev_child[c]->vdev_ms_array == 0)
1424 need_update = B_TRUE;
1427 * Update the config cache asychronously in case we're the
1428 * root pool, in which case the config cache isn't writable yet.
1431 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
1434 * Check all DTLs to see if anything needs resilvering.
1436 if (vdev_resilver_needed(rvd, NULL, NULL))
1437 spa_async_request(spa, SPA_ASYNC_RESILVER);
1442 spa->spa_minref = refcount_count(&spa->spa_refcount);
1443 if (error && error != EBADF)
1444 zfs_ereport_post(ereport, spa, NULL, NULL, 0, 0);
1445 spa->spa_load_state = SPA_LOAD_NONE;
1454 * The import case is identical to an open except that the configuration is sent
1455 * down from userland, instead of grabbed from the configuration cache. For the
1456 * case of an open, the pool configuration will exist in the
1457 * POOL_STATE_UNINITIALIZED state.
1459 * The stats information (gen/count/ustats) is used to gather vdev statistics at
1460 * the same time open the pool, without having to keep around the spa_t in some
1464 spa_open_common(const char *pool, spa_t **spapp, void *tag, nvlist_t **config)
1468 int locked = B_FALSE;
1473 * As disgusting as this is, we need to support recursive calls to this
1474 * function because dsl_dir_open() is called during spa_load(), and ends
1475 * up calling spa_open() again. The real fix is to figure out how to
1476 * avoid dsl_dir_open() calling this in the first place.
1478 if (mutex_owner(&spa_namespace_lock) != curthread) {
1479 mutex_enter(&spa_namespace_lock);
1483 if ((spa = spa_lookup(pool)) == NULL) {
1485 mutex_exit(&spa_namespace_lock);
1488 if (spa->spa_state == POOL_STATE_UNINITIALIZED) {
1490 spa_activate(spa, spa_mode_global);
1492 error = spa_load(spa, spa->spa_config, SPA_LOAD_OPEN, B_FALSE);
1494 if (error == EBADF) {
1496 * If vdev_validate() returns failure (indicated by
1497 * EBADF), it indicates that one of the vdevs indicates
1498 * that the pool has been exported or destroyed. If
1499 * this is the case, the config cache is out of sync and
1500 * we should remove the pool from the namespace.
1503 spa_deactivate(spa);
1504 spa_config_sync(spa, B_TRUE, B_TRUE);
1507 mutex_exit(&spa_namespace_lock);
1513 * We can't open the pool, but we still have useful
1514 * information: the state of each vdev after the
1515 * attempted vdev_open(). Return this to the user.
1517 if (config != NULL && spa->spa_root_vdev != NULL)
1518 *config = spa_config_generate(spa, NULL, -1ULL,
1521 spa_deactivate(spa);
1522 spa->spa_last_open_failed = B_TRUE;
1524 mutex_exit(&spa_namespace_lock);
1528 spa->spa_last_open_failed = B_FALSE;
1532 spa_open_ref(spa, tag);
1535 mutex_exit(&spa_namespace_lock);
1540 *config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
1546 spa_open(const char *name, spa_t **spapp, void *tag)
1548 return (spa_open_common(name, spapp, tag, NULL));
1552 * Lookup the given spa_t, incrementing the inject count in the process,
1553 * preventing it from being exported or destroyed.
1556 spa_inject_addref(char *name)
1560 mutex_enter(&spa_namespace_lock);
1561 if ((spa = spa_lookup(name)) == NULL) {
1562 mutex_exit(&spa_namespace_lock);
1565 spa->spa_inject_ref++;
1566 mutex_exit(&spa_namespace_lock);
1572 spa_inject_delref(spa_t *spa)
1574 mutex_enter(&spa_namespace_lock);
1575 spa->spa_inject_ref--;
1576 mutex_exit(&spa_namespace_lock);
1580 * Add spares device information to the nvlist.
1583 spa_add_spares(spa_t *spa, nvlist_t *config)
1593 if (spa->spa_spares.sav_count == 0)
1596 VERIFY(nvlist_lookup_nvlist(config,
1597 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1598 VERIFY(nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
1599 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1601 VERIFY(nvlist_add_nvlist_array(nvroot,
1602 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
1603 VERIFY(nvlist_lookup_nvlist_array(nvroot,
1604 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0);
1607 * Go through and find any spares which have since been
1608 * repurposed as an active spare. If this is the case, update
1609 * their status appropriately.
1611 for (i = 0; i < nspares; i++) {
1612 VERIFY(nvlist_lookup_uint64(spares[i],
1613 ZPOOL_CONFIG_GUID, &guid) == 0);
1614 if (spa_spare_exists(guid, &pool, NULL) &&
1616 VERIFY(nvlist_lookup_uint64_array(
1617 spares[i], ZPOOL_CONFIG_STATS,
1618 (uint64_t **)&vs, &vsc) == 0);
1619 vs->vs_state = VDEV_STATE_CANT_OPEN;
1620 vs->vs_aux = VDEV_AUX_SPARED;
1627 * Add l2cache device information to the nvlist, including vdev stats.
1630 spa_add_l2cache(spa_t *spa, nvlist_t *config)
1633 uint_t i, j, nl2cache;
1640 if (spa->spa_l2cache.sav_count == 0)
1643 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
1645 VERIFY(nvlist_lookup_nvlist(config,
1646 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1647 VERIFY(nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
1648 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1649 if (nl2cache != 0) {
1650 VERIFY(nvlist_add_nvlist_array(nvroot,
1651 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
1652 VERIFY(nvlist_lookup_nvlist_array(nvroot,
1653 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0);
1656 * Update level 2 cache device stats.
1659 for (i = 0; i < nl2cache; i++) {
1660 VERIFY(nvlist_lookup_uint64(l2cache[i],
1661 ZPOOL_CONFIG_GUID, &guid) == 0);
1664 for (j = 0; j < spa->spa_l2cache.sav_count; j++) {
1666 spa->spa_l2cache.sav_vdevs[j]->vdev_guid) {
1667 vd = spa->spa_l2cache.sav_vdevs[j];
1673 VERIFY(nvlist_lookup_uint64_array(l2cache[i],
1674 ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
1675 vdev_get_stats(vd, vs);
1679 spa_config_exit(spa, SCL_CONFIG, FTAG);
1683 spa_get_stats(const char *name, nvlist_t **config, char *altroot, size_t buflen)
1689 error = spa_open_common(name, &spa, FTAG, config);
1691 if (spa && *config != NULL) {
1692 VERIFY(nvlist_add_uint64(*config, ZPOOL_CONFIG_ERRCOUNT,
1693 spa_get_errlog_size(spa)) == 0);
1695 if (spa_suspended(spa))
1696 VERIFY(nvlist_add_uint64(*config,
1697 ZPOOL_CONFIG_SUSPENDED, spa->spa_failmode) == 0);
1699 spa_add_spares(spa, *config);
1700 spa_add_l2cache(spa, *config);
1704 * We want to get the alternate root even for faulted pools, so we cheat
1705 * and call spa_lookup() directly.
1709 mutex_enter(&spa_namespace_lock);
1710 spa = spa_lookup(name);
1712 spa_altroot(spa, altroot, buflen);
1716 mutex_exit(&spa_namespace_lock);
1718 spa_altroot(spa, altroot, buflen);
1723 spa_close(spa, FTAG);
1729 * Validate that the auxiliary device array is well formed. We must have an
1730 * array of nvlists, each which describes a valid leaf vdev. If this is an
1731 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
1732 * specified, as long as they are well-formed.
1735 spa_validate_aux_devs(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode,
1736 spa_aux_vdev_t *sav, const char *config, uint64_t version,
1737 vdev_labeltype_t label)
1744 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1747 * It's acceptable to have no devs specified.
1749 if (nvlist_lookup_nvlist_array(nvroot, config, &dev, &ndev) != 0)
1756 * Make sure the pool is formatted with a version that supports this
1759 if (spa_version(spa) < version)
1763 * Set the pending device list so we correctly handle device in-use
1766 sav->sav_pending = dev;
1767 sav->sav_npending = ndev;
1769 for (i = 0; i < ndev; i++) {
1770 if ((error = spa_config_parse(spa, &vd, dev[i], NULL, 0,
1774 if (!vd->vdev_ops->vdev_op_leaf) {
1781 * The L2ARC currently only supports disk devices in
1782 * kernel context. For user-level testing, we allow it.
1785 if ((strcmp(config, ZPOOL_CONFIG_L2CACHE) == 0) &&
1786 strcmp(vd->vdev_ops->vdev_op_type, VDEV_TYPE_DISK) != 0) {
1793 if ((error = vdev_open(vd)) == 0 &&
1794 (error = vdev_label_init(vd, crtxg, label)) == 0) {
1795 VERIFY(nvlist_add_uint64(dev[i], ZPOOL_CONFIG_GUID,
1796 vd->vdev_guid) == 0);
1802 (mode != VDEV_ALLOC_SPARE && mode != VDEV_ALLOC_L2CACHE))
1809 sav->sav_pending = NULL;
1810 sav->sav_npending = 0;
1815 spa_validate_aux(spa_t *spa, nvlist_t *nvroot, uint64_t crtxg, int mode)
1819 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == SCL_ALL);
1821 if ((error = spa_validate_aux_devs(spa, nvroot, crtxg, mode,
1822 &spa->spa_spares, ZPOOL_CONFIG_SPARES, SPA_VERSION_SPARES,
1823 VDEV_LABEL_SPARE)) != 0) {
1827 return (spa_validate_aux_devs(spa, nvroot, crtxg, mode,
1828 &spa->spa_l2cache, ZPOOL_CONFIG_L2CACHE, SPA_VERSION_L2CACHE,
1829 VDEV_LABEL_L2CACHE));
1833 spa_set_aux_vdevs(spa_aux_vdev_t *sav, nvlist_t **devs, int ndevs,
1838 if (sav->sav_config != NULL) {
1844 * Generate new dev list by concatentating with the
1847 VERIFY(nvlist_lookup_nvlist_array(sav->sav_config, config,
1848 &olddevs, &oldndevs) == 0);
1850 newdevs = kmem_alloc(sizeof (void *) *
1851 (ndevs + oldndevs), KM_SLEEP);
1852 for (i = 0; i < oldndevs; i++)
1853 VERIFY(nvlist_dup(olddevs[i], &newdevs[i],
1855 for (i = 0; i < ndevs; i++)
1856 VERIFY(nvlist_dup(devs[i], &newdevs[i + oldndevs],
1859 VERIFY(nvlist_remove(sav->sav_config, config,
1860 DATA_TYPE_NVLIST_ARRAY) == 0);
1862 VERIFY(nvlist_add_nvlist_array(sav->sav_config,
1863 config, newdevs, ndevs + oldndevs) == 0);
1864 for (i = 0; i < oldndevs + ndevs; i++)
1865 nvlist_free(newdevs[i]);
1866 kmem_free(newdevs, (oldndevs + ndevs) * sizeof (void *));
1869 * Generate a new dev list.
1871 VERIFY(nvlist_alloc(&sav->sav_config, NV_UNIQUE_NAME,
1873 VERIFY(nvlist_add_nvlist_array(sav->sav_config, config,
1879 * Stop and drop level 2 ARC devices
1882 spa_l2cache_drop(spa_t *spa)
1886 spa_aux_vdev_t *sav = &spa->spa_l2cache;
1888 for (i = 0; i < sav->sav_count; i++) {
1891 vd = sav->sav_vdevs[i];
1894 if (spa_l2cache_exists(vd->vdev_guid, &pool) &&
1895 pool != 0ULL && l2arc_vdev_present(vd))
1896 l2arc_remove_vdev(vd);
1897 if (vd->vdev_isl2cache)
1898 spa_l2cache_remove(vd);
1899 vdev_clear_stats(vd);
1900 (void) vdev_close(vd);
1908 spa_create(const char *pool, nvlist_t *nvroot, nvlist_t *props,
1909 const char *history_str, nvlist_t *zplprops)
1912 char *altroot = NULL;
1917 uint64_t txg = TXG_INITIAL;
1918 nvlist_t **spares, **l2cache;
1919 uint_t nspares, nl2cache;
1923 * If this pool already exists, return failure.
1925 mutex_enter(&spa_namespace_lock);
1926 if (spa_lookup(pool) != NULL) {
1927 mutex_exit(&spa_namespace_lock);
1932 * Allocate a new spa_t structure.
1934 (void) nvlist_lookup_string(props,
1935 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
1936 spa = spa_add(pool, altroot);
1937 spa_activate(spa, spa_mode_global);
1939 spa->spa_uberblock.ub_txg = txg - 1;
1941 if (props && (error = spa_prop_validate(spa, props))) {
1943 spa_deactivate(spa);
1945 mutex_exit(&spa_namespace_lock);
1949 if (nvlist_lookup_uint64(props, zpool_prop_to_name(ZPOOL_PROP_VERSION),
1951 version = SPA_VERSION;
1952 ASSERT(version <= SPA_VERSION);
1953 spa->spa_uberblock.ub_version = version;
1954 spa->spa_ubsync = spa->spa_uberblock;
1957 * Create the root vdev.
1959 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1961 error = spa_config_parse(spa, &rvd, nvroot, NULL, 0, VDEV_ALLOC_ADD);
1963 ASSERT(error != 0 || rvd != NULL);
1964 ASSERT(error != 0 || spa->spa_root_vdev == rvd);
1966 if (error == 0 && !zfs_allocatable_devs(nvroot))
1970 (error = vdev_create(rvd, txg, B_FALSE)) == 0 &&
1971 (error = spa_validate_aux(spa, nvroot, txg,
1972 VDEV_ALLOC_ADD)) == 0) {
1973 for (c = 0; c < rvd->vdev_children; c++)
1974 vdev_init(rvd->vdev_child[c], txg);
1975 vdev_config_dirty(rvd);
1978 spa_config_exit(spa, SCL_ALL, FTAG);
1982 spa_deactivate(spa);
1984 mutex_exit(&spa_namespace_lock);
1989 * Get the list of spares, if specified.
1991 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1992 &spares, &nspares) == 0) {
1993 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config, NV_UNIQUE_NAME,
1995 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
1996 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
1997 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
1998 spa_load_spares(spa);
1999 spa_config_exit(spa, SCL_ALL, FTAG);
2000 spa->spa_spares.sav_sync = B_TRUE;
2004 * Get the list of level 2 cache devices, if specified.
2006 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2007 &l2cache, &nl2cache) == 0) {
2008 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2009 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2010 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2011 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2012 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2013 spa_load_l2cache(spa);
2014 spa_config_exit(spa, SCL_ALL, FTAG);
2015 spa->spa_l2cache.sav_sync = B_TRUE;
2018 spa->spa_dsl_pool = dp = dsl_pool_create(spa, zplprops, txg);
2019 spa->spa_meta_objset = dp->dp_meta_objset;
2021 tx = dmu_tx_create_assigned(dp, txg);
2024 * Create the pool config object.
2026 spa->spa_config_object = dmu_object_alloc(spa->spa_meta_objset,
2027 DMU_OT_PACKED_NVLIST, SPA_CONFIG_BLOCKSIZE,
2028 DMU_OT_PACKED_NVLIST_SIZE, sizeof (uint64_t), tx);
2030 if (zap_add(spa->spa_meta_objset,
2031 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_CONFIG,
2032 sizeof (uint64_t), 1, &spa->spa_config_object, tx) != 0) {
2033 cmn_err(CE_PANIC, "failed to add pool config");
2036 /* Newly created pools with the right version are always deflated. */
2037 if (version >= SPA_VERSION_RAIDZ_DEFLATE) {
2038 spa->spa_deflate = TRUE;
2039 if (zap_add(spa->spa_meta_objset,
2040 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
2041 sizeof (uint64_t), 1, &spa->spa_deflate, tx) != 0) {
2042 cmn_err(CE_PANIC, "failed to add deflate");
2047 * Create the deferred-free bplist object. Turn off compression
2048 * because sync-to-convergence takes longer if the blocksize
2051 spa->spa_sync_bplist_obj = bplist_create(spa->spa_meta_objset,
2053 dmu_object_set_compress(spa->spa_meta_objset, spa->spa_sync_bplist_obj,
2054 ZIO_COMPRESS_OFF, tx);
2056 if (zap_add(spa->spa_meta_objset,
2057 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_SYNC_BPLIST,
2058 sizeof (uint64_t), 1, &spa->spa_sync_bplist_obj, tx) != 0) {
2059 cmn_err(CE_PANIC, "failed to add bplist");
2063 * Create the pool's history object.
2065 if (version >= SPA_VERSION_ZPOOL_HISTORY)
2066 spa_history_create_obj(spa, tx);
2069 * Set pool properties.
2071 spa->spa_bootfs = zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS);
2072 spa->spa_delegation = zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION);
2073 spa->spa_failmode = zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE);
2075 spa_sync_props(spa, props, CRED(), tx);
2079 spa->spa_sync_on = B_TRUE;
2080 txg_sync_start(spa->spa_dsl_pool);
2083 * We explicitly wait for the first transaction to complete so that our
2084 * bean counters are appropriately updated.
2086 txg_wait_synced(spa->spa_dsl_pool, txg);
2088 spa_config_sync(spa, B_FALSE, B_TRUE);
2090 if (version >= SPA_VERSION_ZPOOL_HISTORY && history_str != NULL)
2091 (void) spa_history_log(spa, history_str, LOG_CMD_POOL_CREATE);
2093 mutex_exit(&spa_namespace_lock);
2095 spa->spa_minref = refcount_count(&spa->spa_refcount);
2101 * Import the given pool into the system. We set up the necessary spa_t and
2102 * then call spa_load() to do the dirty work.
2105 spa_import_common(const char *pool, nvlist_t *config, nvlist_t *props,
2106 boolean_t isroot, boolean_t allowfaulted)
2109 char *altroot = NULL;
2112 nvlist_t **spares, **l2cache;
2113 uint_t nspares, nl2cache;
2116 * If a pool with this name exists, return failure.
2118 mutex_enter(&spa_namespace_lock);
2119 if ((spa = spa_lookup(pool)) != NULL) {
2122 * Remove the existing root pool from the
2123 * namespace so that we can replace it with
2124 * the correct config we just read in.
2126 ASSERT(spa->spa_state == POOL_STATE_UNINITIALIZED);
2129 mutex_exit(&spa_namespace_lock);
2135 * Create and initialize the spa structure.
2137 (void) nvlist_lookup_string(props,
2138 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), &altroot);
2139 spa = spa_add(pool, altroot);
2140 spa_activate(spa, spa_mode_global);
2143 spa->spa_import_faulted = B_TRUE;
2144 spa->spa_is_root = isroot;
2147 * Pass off the heavy lifting to spa_load().
2148 * Pass TRUE for mosconfig (unless this is a root pool) because
2149 * the user-supplied config is actually the one to trust when
2152 loaderr = error = spa_load(spa, config, SPA_LOAD_IMPORT, !isroot);
2154 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2156 * Toss any existing sparelist, as it doesn't have any validity anymore,
2157 * and conflicts with spa_has_spare().
2159 if (!isroot && spa->spa_spares.sav_config) {
2160 nvlist_free(spa->spa_spares.sav_config);
2161 spa->spa_spares.sav_config = NULL;
2162 spa_load_spares(spa);
2164 if (!isroot && spa->spa_l2cache.sav_config) {
2165 nvlist_free(spa->spa_l2cache.sav_config);
2166 spa->spa_l2cache.sav_config = NULL;
2167 spa_load_l2cache(spa);
2170 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2173 error = spa_validate_aux(spa, nvroot, -1ULL, VDEV_ALLOC_SPARE);
2175 error = spa_validate_aux(spa, nvroot, -1ULL,
2176 VDEV_ALLOC_L2CACHE);
2177 spa_config_exit(spa, SCL_ALL, FTAG);
2179 if (error != 0 || (props && spa_writeable(spa) &&
2180 (error = spa_prop_set(spa, props)))) {
2181 if (loaderr != 0 && loaderr != EINVAL && allowfaulted) {
2183 * If we failed to load the pool, but 'allowfaulted' is
2184 * set, then manually set the config as if the config
2185 * passed in was specified in the cache file.
2188 spa->spa_import_faulted = B_FALSE;
2189 if (spa->spa_config == NULL)
2190 spa->spa_config = spa_config_generate(spa,
2191 NULL, -1ULL, B_TRUE);
2193 spa_deactivate(spa);
2194 spa_config_sync(spa, B_FALSE, B_TRUE);
2197 spa_deactivate(spa);
2200 mutex_exit(&spa_namespace_lock);
2205 * Override any spares and level 2 cache devices as specified by
2206 * the user, as these may have correct device names/devids, etc.
2208 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
2209 &spares, &nspares) == 0) {
2210 if (spa->spa_spares.sav_config)
2211 VERIFY(nvlist_remove(spa->spa_spares.sav_config,
2212 ZPOOL_CONFIG_SPARES, DATA_TYPE_NVLIST_ARRAY) == 0);
2214 VERIFY(nvlist_alloc(&spa->spa_spares.sav_config,
2215 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2216 VERIFY(nvlist_add_nvlist_array(spa->spa_spares.sav_config,
2217 ZPOOL_CONFIG_SPARES, spares, nspares) == 0);
2218 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2219 spa_load_spares(spa);
2220 spa_config_exit(spa, SCL_ALL, FTAG);
2221 spa->spa_spares.sav_sync = B_TRUE;
2223 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
2224 &l2cache, &nl2cache) == 0) {
2225 if (spa->spa_l2cache.sav_config)
2226 VERIFY(nvlist_remove(spa->spa_l2cache.sav_config,
2227 ZPOOL_CONFIG_L2CACHE, DATA_TYPE_NVLIST_ARRAY) == 0);
2229 VERIFY(nvlist_alloc(&spa->spa_l2cache.sav_config,
2230 NV_UNIQUE_NAME, KM_SLEEP) == 0);
2231 VERIFY(nvlist_add_nvlist_array(spa->spa_l2cache.sav_config,
2232 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache) == 0);
2233 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2234 spa_load_l2cache(spa);
2235 spa_config_exit(spa, SCL_ALL, FTAG);
2236 spa->spa_l2cache.sav_sync = B_TRUE;
2239 if (spa_writeable(spa)) {
2241 * Update the config cache to include the newly-imported pool.
2243 spa_config_update_common(spa, SPA_CONFIG_UPDATE_POOL, isroot);
2246 spa->spa_import_faulted = B_FALSE;
2247 mutex_exit(&spa_namespace_lock);
2254 * Build a "root" vdev for a top level vdev read in from a rootpool
2258 spa_build_rootpool_config(nvlist_t *config)
2260 nvlist_t *nvtop, *nvroot;
2264 * Add this top-level vdev to the child array.
2266 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &nvtop)
2268 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &pgid)
2272 * Put this pool's top-level vdevs into a root vdev.
2274 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2275 VERIFY(nvlist_add_string(nvroot, ZPOOL_CONFIG_TYPE, VDEV_TYPE_ROOT)
2277 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_ID, 0ULL) == 0);
2278 VERIFY(nvlist_add_uint64(nvroot, ZPOOL_CONFIG_GUID, pgid) == 0);
2279 VERIFY(nvlist_add_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2283 * Replace the existing vdev_tree with the new root vdev in
2284 * this pool's configuration (remove the old, add the new).
2286 VERIFY(nvlist_add_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, nvroot) == 0);
2287 nvlist_free(nvroot);
2291 * Get the root pool information from the root disk, then import the root pool
2292 * during the system boot up time.
2294 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t **);
2297 spa_check_rootconf(char *devpath, char *devid, nvlist_t **bestconf,
2304 if (error = vdev_disk_read_rootlabel(devpath, devid, &config))
2307 VERIFY(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_TXG, &txg) == 0);
2309 if (bestconf != NULL)
2312 nvlist_free(config);
2318 spa_rootdev_validate(nvlist_t *nv)
2322 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2323 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2324 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2332 * Given the boot device's physical path or devid, check if the device
2333 * is in a valid state. If so, return the configuration from the vdev
2337 spa_get_rootconf(char *devpath, char *devid, nvlist_t **bestconf)
2339 nvlist_t *conf = NULL;
2341 nvlist_t *nvtop, **child;
2343 char *bootpath = NULL;
2348 if (devpath && ((tmp = strchr(devpath, ' ')) != NULL))
2350 if (error = spa_check_rootconf(devpath, devid, &conf, &txg)) {
2351 cmn_err(CE_NOTE, "error reading device label");
2355 cmn_err(CE_NOTE, "this device is detached");
2360 VERIFY(nvlist_lookup_nvlist(conf, ZPOOL_CONFIG_VDEV_TREE,
2362 VERIFY(nvlist_lookup_string(nvtop, ZPOOL_CONFIG_TYPE, &type) == 0);
2364 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2365 if (spa_rootdev_validate(nvtop)) {
2373 ASSERT(strcmp(type, VDEV_TYPE_MIRROR) == 0);
2375 VERIFY(nvlist_lookup_nvlist_array(nvtop, ZPOOL_CONFIG_CHILDREN,
2376 &child, &children) == 0);
2379 * Go thru vdevs in the mirror to see if the given device
2380 * has the most recent txg. Only the device with the most
2381 * recent txg has valid information and should be booted.
2383 for (c = 0; c < children; c++) {
2384 char *cdevid, *cpath;
2389 if (nvlist_lookup_string(child[c], ZPOOL_CONFIG_PHYS_PATH,
2390 &cpath) != 0 && nvlist_lookup_string(child[c],
2391 ZPOOL_CONFIG_DEVID, &cdevid) != 0)
2393 if ((spa_check_rootconf(cpath, cdevid, NULL,
2394 &tmptxg) == 0) && (tmptxg > txg)) {
2396 VERIFY(nvlist_lookup_string(child[c],
2397 ZPOOL_CONFIG_PATH, &bootpath) == 0);
2401 /* Does the best device match the one we've booted from? */
2403 cmn_err(CE_NOTE, "try booting from '%s'", bootpath);
2412 * Import a root pool.
2414 * For x86. devpath_list will consist of devid and/or physpath name of
2415 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
2416 * The GRUB "findroot" command will return the vdev we should boot.
2418 * For Sparc, devpath_list consists the physpath name of the booting device
2419 * no matter the rootpool is a single device pool or a mirrored pool.
2421 * "/pci@1f,0/ide@d/disk@0,0:a"
2424 spa_import_rootpool(char *devpath, char *devid)
2426 nvlist_t *conf = NULL;
2431 * Get the vdev pathname and configuation from the most
2432 * recently updated vdev (highest txg).
2434 if (error = spa_get_rootconf(devpath, devid, &conf))
2438 * Add type "root" vdev to the config.
2440 spa_build_rootpool_config(conf);
2442 VERIFY(nvlist_lookup_string(conf, ZPOOL_CONFIG_POOL_NAME, &pname) == 0);
2445 * We specify 'allowfaulted' for this to be treated like spa_open()
2446 * instead of spa_import(). This prevents us from marking vdevs as
2447 * persistently unavailable, and generates FMA ereports as if it were a
2448 * pool open, not import.
2450 error = spa_import_common(pname, conf, NULL, B_TRUE, B_TRUE);
2451 ASSERT(error != EEXIST);
2457 cmn_err(CE_NOTE, "\n"
2458 " *************************************************** \n"
2459 " * This device is not bootable! * \n"
2460 " * It is either offlined or detached or faulted. * \n"
2461 " * Please try to boot from a different device. * \n"
2462 " *************************************************** ");
2469 * Import a non-root pool into the system.
2472 spa_import(const char *pool, nvlist_t *config, nvlist_t *props)
2474 return (spa_import_common(pool, config, props, B_FALSE, B_FALSE));
2478 spa_import_faulted(const char *pool, nvlist_t *config, nvlist_t *props)
2480 return (spa_import_common(pool, config, props, B_FALSE, B_TRUE));
2485 * This (illegal) pool name is used when temporarily importing a spa_t in order
2486 * to get the vdev stats associated with the imported devices.
2488 #define TRYIMPORT_NAME "$import"
2491 spa_tryimport(nvlist_t *tryconfig)
2493 nvlist_t *config = NULL;
2498 if (nvlist_lookup_string(tryconfig, ZPOOL_CONFIG_POOL_NAME, &poolname))
2501 if (nvlist_lookup_uint64(tryconfig, ZPOOL_CONFIG_POOL_STATE, &state))
2505 * Create and initialize the spa structure.
2507 mutex_enter(&spa_namespace_lock);
2508 spa = spa_add(TRYIMPORT_NAME, NULL);
2509 spa_activate(spa, FREAD);
2512 * Pass off the heavy lifting to spa_load().
2513 * Pass TRUE for mosconfig because the user-supplied config
2514 * is actually the one to trust when doing an import.
2516 (void) spa_load(spa, tryconfig, SPA_LOAD_TRYIMPORT, B_TRUE);
2519 * If 'tryconfig' was at least parsable, return the current config.
2521 if (spa->spa_root_vdev != NULL) {
2522 config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
2523 VERIFY(nvlist_add_string(config, ZPOOL_CONFIG_POOL_NAME,
2525 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_POOL_STATE,
2527 VERIFY(nvlist_add_uint64(config, ZPOOL_CONFIG_TIMESTAMP,
2528 spa->spa_uberblock.ub_timestamp) == 0);
2531 * If the bootfs property exists on this pool then we
2532 * copy it out so that external consumers can tell which
2533 * pools are bootable.
2535 if (spa->spa_bootfs) {
2536 char *tmpname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2539 * We have to play games with the name since the
2540 * pool was opened as TRYIMPORT_NAME.
2542 if (dsl_dsobj_to_dsname(spa_name(spa),
2543 spa->spa_bootfs, tmpname) == 0) {
2545 char *dsname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2547 cp = strchr(tmpname, '/');
2549 (void) strlcpy(dsname, tmpname,
2552 (void) snprintf(dsname, MAXPATHLEN,
2553 "%s/%s", poolname, ++cp);
2555 VERIFY(nvlist_add_string(config,
2556 ZPOOL_CONFIG_BOOTFS, dsname) == 0);
2557 kmem_free(dsname, MAXPATHLEN);
2559 kmem_free(tmpname, MAXPATHLEN);
2563 * Add the list of hot spares and level 2 cache devices.
2565 spa_add_spares(spa, config);
2566 spa_add_l2cache(spa, config);
2570 spa_deactivate(spa);
2572 mutex_exit(&spa_namespace_lock);
2578 * Pool export/destroy
2580 * The act of destroying or exporting a pool is very simple. We make sure there
2581 * is no more pending I/O and any references to the pool are gone. Then, we
2582 * update the pool state and sync all the labels to disk, removing the
2583 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
2584 * we don't sync the labels or remove the configuration cache.
2587 spa_export_common(char *pool, int new_state, nvlist_t **oldconfig,
2588 boolean_t force, boolean_t hardforce)
2595 if (!(spa_mode_global & FWRITE))
2598 mutex_enter(&spa_namespace_lock);
2599 if ((spa = spa_lookup(pool)) == NULL) {
2600 mutex_exit(&spa_namespace_lock);
2605 * Put a hold on the pool, drop the namespace lock, stop async tasks,
2606 * reacquire the namespace lock, and see if we can export.
2608 spa_open_ref(spa, FTAG);
2609 mutex_exit(&spa_namespace_lock);
2610 spa_async_suspend(spa);
2611 mutex_enter(&spa_namespace_lock);
2612 spa_close(spa, FTAG);
2615 * The pool will be in core if it's openable,
2616 * in which case we can modify its state.
2618 if (spa->spa_state != POOL_STATE_UNINITIALIZED && spa->spa_sync_on) {
2620 * Objsets may be open only because they're dirty, so we
2621 * have to force it to sync before checking spa_refcnt.
2623 txg_wait_synced(spa->spa_dsl_pool, 0);
2626 * A pool cannot be exported or destroyed if there are active
2627 * references. If we are resetting a pool, allow references by
2628 * fault injection handlers.
2630 if (!spa_refcount_zero(spa) ||
2631 (spa->spa_inject_ref != 0 &&
2632 new_state != POOL_STATE_UNINITIALIZED)) {
2633 spa_async_resume(spa);
2634 mutex_exit(&spa_namespace_lock);
2639 * A pool cannot be exported if it has an active shared spare.
2640 * This is to prevent other pools stealing the active spare
2641 * from an exported pool. At user's own will, such pool can
2642 * be forcedly exported.
2644 if (!force && new_state == POOL_STATE_EXPORTED &&
2645 spa_has_active_shared_spare(spa)) {
2646 spa_async_resume(spa);
2647 mutex_exit(&spa_namespace_lock);
2652 * We want this to be reflected on every label,
2653 * so mark them all dirty. spa_unload() will do the
2654 * final sync that pushes these changes out.
2656 if (new_state != POOL_STATE_UNINITIALIZED && !hardforce) {
2657 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
2658 spa->spa_state = new_state;
2659 spa->spa_final_txg = spa_last_synced_txg(spa) + 1;
2660 vdev_config_dirty(spa->spa_root_vdev);
2661 spa_config_exit(spa, SCL_ALL, FTAG);
2665 spa_event_notify(spa, NULL, ESC_ZFS_POOL_DESTROY);
2667 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
2669 spa_deactivate(spa);
2672 if (oldconfig && spa->spa_config)
2673 VERIFY(nvlist_dup(spa->spa_config, oldconfig, 0) == 0);
2675 if (new_state != POOL_STATE_UNINITIALIZED) {
2677 spa_config_sync(spa, B_TRUE, B_TRUE);
2680 mutex_exit(&spa_namespace_lock);
2686 * Destroy a storage pool.
2689 spa_destroy(char *pool)
2691 return (spa_export_common(pool, POOL_STATE_DESTROYED, NULL,
2696 * Export a storage pool.
2699 spa_export(char *pool, nvlist_t **oldconfig, boolean_t force,
2700 boolean_t hardforce)
2702 return (spa_export_common(pool, POOL_STATE_EXPORTED, oldconfig,
2707 * Similar to spa_export(), this unloads the spa_t without actually removing it
2708 * from the namespace in any way.
2711 spa_reset(char *pool)
2713 return (spa_export_common(pool, POOL_STATE_UNINITIALIZED, NULL,
2718 * ==========================================================================
2719 * Device manipulation
2720 * ==========================================================================
2724 * Add a device to a storage pool.
2727 spa_vdev_add(spa_t *spa, nvlist_t *nvroot)
2731 vdev_t *rvd = spa->spa_root_vdev;
2733 nvlist_t **spares, **l2cache;
2734 uint_t nspares, nl2cache;
2736 txg = spa_vdev_enter(spa);
2738 if ((error = spa_config_parse(spa, &vd, nvroot, NULL, 0,
2739 VDEV_ALLOC_ADD)) != 0)
2740 return (spa_vdev_exit(spa, NULL, txg, error));
2742 spa->spa_pending_vdev = vd; /* spa_vdev_exit() will clear this */
2744 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES, &spares,
2748 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE, &l2cache,
2752 if (vd->vdev_children == 0 && nspares == 0 && nl2cache == 0)
2753 return (spa_vdev_exit(spa, vd, txg, EINVAL));
2755 if (vd->vdev_children != 0 &&
2756 (error = vdev_create(vd, txg, B_FALSE)) != 0)
2757 return (spa_vdev_exit(spa, vd, txg, error));
2760 * We must validate the spares and l2cache devices after checking the
2761 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
2763 if ((error = spa_validate_aux(spa, nvroot, txg, VDEV_ALLOC_ADD)) != 0)
2764 return (spa_vdev_exit(spa, vd, txg, error));
2767 * Transfer each new top-level vdev from vd to rvd.
2769 for (int c = 0; c < vd->vdev_children; c++) {
2770 tvd = vd->vdev_child[c];
2771 vdev_remove_child(vd, tvd);
2772 tvd->vdev_id = rvd->vdev_children;
2773 vdev_add_child(rvd, tvd);
2774 vdev_config_dirty(tvd);
2778 spa_set_aux_vdevs(&spa->spa_spares, spares, nspares,
2779 ZPOOL_CONFIG_SPARES);
2780 spa_load_spares(spa);
2781 spa->spa_spares.sav_sync = B_TRUE;
2784 if (nl2cache != 0) {
2785 spa_set_aux_vdevs(&spa->spa_l2cache, l2cache, nl2cache,
2786 ZPOOL_CONFIG_L2CACHE);
2787 spa_load_l2cache(spa);
2788 spa->spa_l2cache.sav_sync = B_TRUE;
2792 * We have to be careful when adding new vdevs to an existing pool.
2793 * If other threads start allocating from these vdevs before we
2794 * sync the config cache, and we lose power, then upon reboot we may
2795 * fail to open the pool because there are DVAs that the config cache
2796 * can't translate. Therefore, we first add the vdevs without
2797 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
2798 * and then let spa_config_update() initialize the new metaslabs.
2800 * spa_load() checks for added-but-not-initialized vdevs, so that
2801 * if we lose power at any point in this sequence, the remaining
2802 * steps will be completed the next time we load the pool.
2804 (void) spa_vdev_exit(spa, vd, txg, 0);
2806 mutex_enter(&spa_namespace_lock);
2807 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
2808 mutex_exit(&spa_namespace_lock);
2814 * Attach a device to a mirror. The arguments are the path to any device
2815 * in the mirror, and the nvroot for the new device. If the path specifies
2816 * a device that is not mirrored, we automatically insert the mirror vdev.
2818 * If 'replacing' is specified, the new device is intended to replace the
2819 * existing device; in this case the two devices are made into their own
2820 * mirror using the 'replacing' vdev, which is functionally identical to
2821 * the mirror vdev (it actually reuses all the same ops) but has a few
2822 * extra rules: you can't attach to it after it's been created, and upon
2823 * completion of resilvering, the first disk (the one being replaced)
2824 * is automatically detached.
2827 spa_vdev_attach(spa_t *spa, uint64_t guid, nvlist_t *nvroot, int replacing)
2829 uint64_t txg, open_txg;
2830 vdev_t *rvd = spa->spa_root_vdev;
2831 vdev_t *oldvd, *newvd, *newrootvd, *pvd, *tvd;
2834 char *oldvdpath, *newvdpath;
2838 txg = spa_vdev_enter(spa);
2840 oldvd = spa_lookup_by_guid(spa, guid, B_FALSE);
2843 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
2845 if (!oldvd->vdev_ops->vdev_op_leaf)
2846 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
2848 pvd = oldvd->vdev_parent;
2850 if ((error = spa_config_parse(spa, &newrootvd, nvroot, NULL, 0,
2851 VDEV_ALLOC_ADD)) != 0)
2852 return (spa_vdev_exit(spa, NULL, txg, EINVAL));
2854 if (newrootvd->vdev_children != 1)
2855 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2857 newvd = newrootvd->vdev_child[0];
2859 if (!newvd->vdev_ops->vdev_op_leaf)
2860 return (spa_vdev_exit(spa, newrootvd, txg, EINVAL));
2862 if ((error = vdev_create(newrootvd, txg, replacing)) != 0)
2863 return (spa_vdev_exit(spa, newrootvd, txg, error));
2866 * Spares can't replace logs
2868 if (oldvd->vdev_top->vdev_islog && newvd->vdev_isspare)
2869 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2873 * For attach, the only allowable parent is a mirror or the root
2876 if (pvd->vdev_ops != &vdev_mirror_ops &&
2877 pvd->vdev_ops != &vdev_root_ops)
2878 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2880 pvops = &vdev_mirror_ops;
2883 * Active hot spares can only be replaced by inactive hot
2886 if (pvd->vdev_ops == &vdev_spare_ops &&
2887 pvd->vdev_child[1] == oldvd &&
2888 !spa_has_spare(spa, newvd->vdev_guid))
2889 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2892 * If the source is a hot spare, and the parent isn't already a
2893 * spare, then we want to create a new hot spare. Otherwise, we
2894 * want to create a replacing vdev. The user is not allowed to
2895 * attach to a spared vdev child unless the 'isspare' state is
2896 * the same (spare replaces spare, non-spare replaces
2899 if (pvd->vdev_ops == &vdev_replacing_ops)
2900 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2901 else if (pvd->vdev_ops == &vdev_spare_ops &&
2902 newvd->vdev_isspare != oldvd->vdev_isspare)
2903 return (spa_vdev_exit(spa, newrootvd, txg, ENOTSUP));
2904 else if (pvd->vdev_ops != &vdev_spare_ops &&
2905 newvd->vdev_isspare)
2906 pvops = &vdev_spare_ops;
2908 pvops = &vdev_replacing_ops;
2912 * Compare the new device size with the replaceable/attachable
2915 if (newvd->vdev_psize < vdev_get_rsize(oldvd))
2916 return (spa_vdev_exit(spa, newrootvd, txg, EOVERFLOW));
2919 * The new device cannot have a higher alignment requirement
2920 * than the top-level vdev.
2922 if (newvd->vdev_ashift > oldvd->vdev_top->vdev_ashift)
2923 return (spa_vdev_exit(spa, newrootvd, txg, EDOM));
2926 * If this is an in-place replacement, update oldvd's path and devid
2927 * to make it distinguishable from newvd, and unopenable from now on.
2929 if (strcmp(oldvd->vdev_path, newvd->vdev_path) == 0) {
2930 spa_strfree(oldvd->vdev_path);
2931 oldvd->vdev_path = kmem_alloc(strlen(newvd->vdev_path) + 5,
2933 (void) sprintf(oldvd->vdev_path, "%s/%s",
2934 newvd->vdev_path, "old");
2935 if (oldvd->vdev_devid != NULL) {
2936 spa_strfree(oldvd->vdev_devid);
2937 oldvd->vdev_devid = NULL;
2942 * If the parent is not a mirror, or if we're replacing, insert the new
2943 * mirror/replacing/spare vdev above oldvd.
2945 if (pvd->vdev_ops != pvops)
2946 pvd = vdev_add_parent(oldvd, pvops);
2948 ASSERT(pvd->vdev_top->vdev_parent == rvd);
2949 ASSERT(pvd->vdev_ops == pvops);
2950 ASSERT(oldvd->vdev_parent == pvd);
2953 * Extract the new device from its root and add it to pvd.
2955 vdev_remove_child(newrootvd, newvd);
2956 newvd->vdev_id = pvd->vdev_children;
2957 vdev_add_child(pvd, newvd);
2960 * If newvd is smaller than oldvd, but larger than its rsize,
2961 * the addition of newvd may have decreased our parent's asize.
2963 pvd->vdev_asize = MIN(pvd->vdev_asize, newvd->vdev_asize);
2965 tvd = newvd->vdev_top;
2966 ASSERT(pvd->vdev_top == tvd);
2967 ASSERT(tvd->vdev_parent == rvd);
2969 vdev_config_dirty(tvd);
2972 * Set newvd's DTL to [TXG_INITIAL, open_txg]. It will propagate
2973 * upward when spa_vdev_exit() calls vdev_dtl_reassess().
2975 open_txg = txg + TXG_CONCURRENT_STATES - 1;
2977 vdev_dtl_dirty(newvd, DTL_MISSING,
2978 TXG_INITIAL, open_txg - TXG_INITIAL + 1);
2980 if (newvd->vdev_isspare)
2981 spa_spare_activate(newvd);
2982 oldvdpath = spa_strdup(oldvd->vdev_path);
2983 newvdpath = spa_strdup(newvd->vdev_path);
2984 newvd_isspare = newvd->vdev_isspare;
2987 * Mark newvd's DTL dirty in this txg.
2989 vdev_dirty(tvd, VDD_DTL, newvd, txg);
2991 (void) spa_vdev_exit(spa, newrootvd, open_txg, 0);
2993 tx = dmu_tx_create_dd(spa_get_dsl(spa)->dp_mos_dir);
2994 if (dmu_tx_assign(tx, TXG_WAIT) == 0) {
2995 spa_history_internal_log(LOG_POOL_VDEV_ATTACH, spa, tx,
2996 CRED(), "%s vdev=%s %s vdev=%s",
2997 replacing && newvd_isspare ? "spare in" :
2998 replacing ? "replace" : "attach", newvdpath,
2999 replacing ? "for" : "to", oldvdpath);
3005 spa_strfree(oldvdpath);
3006 spa_strfree(newvdpath);
3009 * Kick off a resilver to update newvd.
3011 VERIFY3U(spa_scrub(spa, POOL_SCRUB_RESILVER), ==, 0);
3017 * Detach a device from a mirror or replacing vdev.
3018 * If 'replace_done' is specified, only detach if the parent
3019 * is a replacing vdev.
3022 spa_vdev_detach(spa_t *spa, uint64_t guid, uint64_t pguid, int replace_done)
3026 vdev_t *rvd = spa->spa_root_vdev;
3027 vdev_t *vd, *pvd, *cvd, *tvd;
3028 boolean_t unspare = B_FALSE;
3029 uint64_t unspare_guid;
3032 txg = spa_vdev_enter(spa);
3034 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3037 return (spa_vdev_exit(spa, NULL, txg, ENODEV));
3039 if (!vd->vdev_ops->vdev_op_leaf)
3040 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3042 pvd = vd->vdev_parent;
3045 * If the parent/child relationship is not as expected, don't do it.
3046 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
3047 * vdev that's replacing B with C. The user's intent in replacing
3048 * is to go from M(A,B) to M(A,C). If the user decides to cancel
3049 * the replace by detaching C, the expected behavior is to end up
3050 * M(A,B). But suppose that right after deciding to detach C,
3051 * the replacement of B completes. We would have M(A,C), and then
3052 * ask to detach C, which would leave us with just A -- not what
3053 * the user wanted. To prevent this, we make sure that the
3054 * parent/child relationship hasn't changed -- in this example,
3055 * that C's parent is still the replacing vdev R.
3057 if (pvd->vdev_guid != pguid && pguid != 0)
3058 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3061 * If replace_done is specified, only remove this device if it's
3062 * the first child of a replacing vdev. For the 'spare' vdev, either
3063 * disk can be removed.
3066 if (pvd->vdev_ops == &vdev_replacing_ops) {
3067 if (vd->vdev_id != 0)
3068 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3069 } else if (pvd->vdev_ops != &vdev_spare_ops) {
3070 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3074 ASSERT(pvd->vdev_ops != &vdev_spare_ops ||
3075 spa_version(spa) >= SPA_VERSION_SPARES);
3078 * Only mirror, replacing, and spare vdevs support detach.
3080 if (pvd->vdev_ops != &vdev_replacing_ops &&
3081 pvd->vdev_ops != &vdev_mirror_ops &&
3082 pvd->vdev_ops != &vdev_spare_ops)
3083 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3086 * If this device has the only valid copy of some data,
3087 * we cannot safely detach it.
3089 if (vdev_dtl_required(vd))
3090 return (spa_vdev_exit(spa, NULL, txg, EBUSY));
3092 ASSERT(pvd->vdev_children >= 2);
3095 * If we are detaching the second disk from a replacing vdev, then
3096 * check to see if we changed the original vdev's path to have "/old"
3097 * at the end in spa_vdev_attach(). If so, undo that change now.
3099 if (pvd->vdev_ops == &vdev_replacing_ops && vd->vdev_id == 1 &&
3100 pvd->vdev_child[0]->vdev_path != NULL &&
3101 pvd->vdev_child[1]->vdev_path != NULL) {
3102 ASSERT(pvd->vdev_child[1] == vd);
3103 cvd = pvd->vdev_child[0];
3104 len = strlen(vd->vdev_path);
3105 if (strncmp(cvd->vdev_path, vd->vdev_path, len) == 0 &&
3106 strcmp(cvd->vdev_path + len, "/old") == 0) {
3107 spa_strfree(cvd->vdev_path);
3108 cvd->vdev_path = spa_strdup(vd->vdev_path);
3113 * If we are detaching the original disk from a spare, then it implies
3114 * that the spare should become a real disk, and be removed from the
3115 * active spare list for the pool.
3117 if (pvd->vdev_ops == &vdev_spare_ops &&
3118 vd->vdev_id == 0 && pvd->vdev_child[1]->vdev_isspare)
3122 * Erase the disk labels so the disk can be used for other things.
3123 * This must be done after all other error cases are handled,
3124 * but before we disembowel vd (so we can still do I/O to it).
3125 * But if we can't do it, don't treat the error as fatal --
3126 * it may be that the unwritability of the disk is the reason
3127 * it's being detached!
3129 error = vdev_label_init(vd, 0, VDEV_LABEL_REMOVE);
3132 * Remove vd from its parent and compact the parent's children.
3134 vdev_remove_child(pvd, vd);
3135 vdev_compact_children(pvd);
3138 * Remember one of the remaining children so we can get tvd below.
3140 cvd = pvd->vdev_child[0];
3143 * If we need to remove the remaining child from the list of hot spares,
3144 * do it now, marking the vdev as no longer a spare in the process.
3145 * We must do this before vdev_remove_parent(), because that can
3146 * change the GUID if it creates a new toplevel GUID. For a similar
3147 * reason, we must remove the spare now, in the same txg as the detach;
3148 * otherwise someone could attach a new sibling, change the GUID, and
3149 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
3152 ASSERT(cvd->vdev_isspare);
3153 spa_spare_remove(cvd);
3154 unspare_guid = cvd->vdev_guid;
3155 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
3159 * If the parent mirror/replacing vdev only has one child,
3160 * the parent is no longer needed. Remove it from the tree.
3162 if (pvd->vdev_children == 1)
3163 vdev_remove_parent(cvd);
3166 * We don't set tvd until now because the parent we just removed
3167 * may have been the previous top-level vdev.
3169 tvd = cvd->vdev_top;
3170 ASSERT(tvd->vdev_parent == rvd);
3173 * Reevaluate the parent vdev state.
3175 vdev_propagate_state(cvd);
3178 * If the device we just detached was smaller than the others, it may be
3179 * possible to add metaslabs (i.e. grow the pool). vdev_metaslab_init()
3180 * can't fail because the existing metaslabs are already in core, so
3181 * there's nothing to read from disk.
3183 VERIFY(vdev_metaslab_init(tvd, txg) == 0);
3185 vdev_config_dirty(tvd);
3188 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
3189 * vd->vdev_detached is set and free vd's DTL object in syncing context.
3190 * But first make sure we're not on any *other* txg's DTL list, to
3191 * prevent vd from being accessed after it's freed.
3193 for (int t = 0; t < TXG_SIZE; t++)
3194 (void) txg_list_remove_this(&tvd->vdev_dtl_list, vd, t);
3195 vd->vdev_detached = B_TRUE;
3196 vdev_dirty(tvd, VDD_DTL, vd, txg);
3198 spa_event_notify(spa, vd, ESC_ZFS_VDEV_REMOVE);
3200 error = spa_vdev_exit(spa, vd, txg, 0);
3203 * If this was the removal of the original device in a hot spare vdev,
3204 * then we want to go through and remove the device from the hot spare
3205 * list of every other pool.
3210 mutex_enter(&spa_namespace_lock);
3211 while ((spa = spa_next(spa)) != NULL) {
3212 if (spa->spa_state != POOL_STATE_ACTIVE)
3216 spa_open_ref(spa, FTAG);
3217 mutex_exit(&spa_namespace_lock);
3218 (void) spa_vdev_remove(spa, unspare_guid, B_TRUE);
3219 mutex_enter(&spa_namespace_lock);
3220 spa_close(spa, FTAG);
3222 mutex_exit(&spa_namespace_lock);
3229 spa_nvlist_lookup_by_guid(nvlist_t **nvpp, int count, uint64_t target_guid)
3231 for (int i = 0; i < count; i++) {
3234 VERIFY(nvlist_lookup_uint64(nvpp[i], ZPOOL_CONFIG_GUID,
3237 if (guid == target_guid)
3245 spa_vdev_remove_aux(nvlist_t *config, char *name, nvlist_t **dev, int count,
3246 nvlist_t *dev_to_remove)
3248 nvlist_t **newdev = NULL;
3251 newdev = kmem_alloc((count - 1) * sizeof (void *), KM_SLEEP);
3253 for (int i = 0, j = 0; i < count; i++) {
3254 if (dev[i] == dev_to_remove)
3256 VERIFY(nvlist_dup(dev[i], &newdev[j++], KM_SLEEP) == 0);
3259 VERIFY(nvlist_remove(config, name, DATA_TYPE_NVLIST_ARRAY) == 0);
3260 VERIFY(nvlist_add_nvlist_array(config, name, newdev, count - 1) == 0);
3262 for (int i = 0; i < count - 1; i++)
3263 nvlist_free(newdev[i]);
3266 kmem_free(newdev, (count - 1) * sizeof (void *));
3270 * Remove a device from the pool. Currently, this supports removing only hot
3271 * spares and level 2 ARC devices.
3274 spa_vdev_remove(spa_t *spa, uint64_t guid, boolean_t unspare)
3277 nvlist_t **spares, **l2cache, *nv;
3278 uint_t nspares, nl2cache;
3281 boolean_t locked = MUTEX_HELD(&spa_namespace_lock);
3284 txg = spa_vdev_enter(spa);
3286 vd = spa_lookup_by_guid(spa, guid, B_FALSE);
3288 if (spa->spa_spares.sav_vdevs != NULL &&
3289 nvlist_lookup_nvlist_array(spa->spa_spares.sav_config,
3290 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0 &&
3291 (nv = spa_nvlist_lookup_by_guid(spares, nspares, guid)) != NULL) {
3293 * Only remove the hot spare if it's not currently in use
3296 if (vd == NULL || unspare) {
3297 spa_vdev_remove_aux(spa->spa_spares.sav_config,
3298 ZPOOL_CONFIG_SPARES, spares, nspares, nv);
3299 spa_load_spares(spa);
3300 spa->spa_spares.sav_sync = B_TRUE;
3304 } else if (spa->spa_l2cache.sav_vdevs != NULL &&
3305 nvlist_lookup_nvlist_array(spa->spa_l2cache.sav_config,
3306 ZPOOL_CONFIG_L2CACHE, &l2cache, &nl2cache) == 0 &&
3307 (nv = spa_nvlist_lookup_by_guid(l2cache, nl2cache, guid)) != NULL) {
3309 * Cache devices can always be removed.
3311 spa_vdev_remove_aux(spa->spa_l2cache.sav_config,
3312 ZPOOL_CONFIG_L2CACHE, l2cache, nl2cache, nv);
3313 spa_load_l2cache(spa);
3314 spa->spa_l2cache.sav_sync = B_TRUE;
3315 } else if (vd != NULL) {
3317 * Normal vdevs cannot be removed (yet).
3322 * There is no vdev of any kind with the specified guid.
3328 return (spa_vdev_exit(spa, NULL, txg, error));
3334 * Find any device that's done replacing, or a vdev marked 'unspare' that's
3335 * current spared, so we can detach it.
3338 spa_vdev_resilver_done_hunt(vdev_t *vd)
3340 vdev_t *newvd, *oldvd;
3343 for (c = 0; c < vd->vdev_children; c++) {
3344 oldvd = spa_vdev_resilver_done_hunt(vd->vdev_child[c]);
3350 * Check for a completed replacement.
3352 if (vd->vdev_ops == &vdev_replacing_ops && vd->vdev_children == 2) {
3353 oldvd = vd->vdev_child[0];
3354 newvd = vd->vdev_child[1];
3356 if (vdev_dtl_empty(newvd, DTL_MISSING) &&
3357 !vdev_dtl_required(oldvd))
3362 * Check for a completed resilver with the 'unspare' flag set.
3364 if (vd->vdev_ops == &vdev_spare_ops && vd->vdev_children == 2) {
3365 newvd = vd->vdev_child[0];
3366 oldvd = vd->vdev_child[1];
3368 if (newvd->vdev_unspare &&
3369 vdev_dtl_empty(newvd, DTL_MISSING) &&
3370 !vdev_dtl_required(oldvd)) {
3371 newvd->vdev_unspare = 0;
3380 spa_vdev_resilver_done(spa_t *spa)
3382 vdev_t *vd, *pvd, *ppvd;
3383 uint64_t guid, sguid, pguid, ppguid;
3385 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3387 while ((vd = spa_vdev_resilver_done_hunt(spa->spa_root_vdev)) != NULL) {
3388 pvd = vd->vdev_parent;
3389 ppvd = pvd->vdev_parent;
3390 guid = vd->vdev_guid;
3391 pguid = pvd->vdev_guid;
3392 ppguid = ppvd->vdev_guid;
3395 * If we have just finished replacing a hot spared device, then
3396 * we need to detach the parent's first child (the original hot
3399 if (ppvd->vdev_ops == &vdev_spare_ops && pvd->vdev_id == 0) {
3400 ASSERT(pvd->vdev_ops == &vdev_replacing_ops);
3401 ASSERT(ppvd->vdev_children == 2);
3402 sguid = ppvd->vdev_child[1]->vdev_guid;
3404 spa_config_exit(spa, SCL_ALL, FTAG);
3405 if (spa_vdev_detach(spa, guid, pguid, B_TRUE) != 0)
3407 if (sguid && spa_vdev_detach(spa, sguid, ppguid, B_TRUE) != 0)
3409 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
3412 spa_config_exit(spa, SCL_ALL, FTAG);
3416 * Update the stored path for this vdev. Dirty the vdev configuration, relying
3417 * on spa_vdev_enter/exit() to synchronize the labels and cache.
3420 spa_vdev_setpath(spa_t *spa, uint64_t guid, const char *newpath)
3425 txg = spa_vdev_enter(spa);
3427 if ((vd = spa_lookup_by_guid(spa, guid, B_TRUE)) == NULL) {
3429 * Determine if this is a reference to a hot spare device. If
3430 * it is, update the path manually as there is no associated
3431 * vdev_t that can be synced to disk.
3436 if (spa->spa_spares.sav_config != NULL) {
3437 VERIFY(nvlist_lookup_nvlist_array(
3438 spa->spa_spares.sav_config, ZPOOL_CONFIG_SPARES,
3439 &spares, &nspares) == 0);
3440 for (i = 0; i < nspares; i++) {
3442 VERIFY(nvlist_lookup_uint64(spares[i],
3443 ZPOOL_CONFIG_GUID, &theguid) == 0);
3444 if (theguid == guid) {
3445 VERIFY(nvlist_add_string(spares[i],
3446 ZPOOL_CONFIG_PATH, newpath) == 0);
3447 spa_load_spares(spa);
3448 spa->spa_spares.sav_sync = B_TRUE;
3449 return (spa_vdev_exit(spa, NULL, txg,
3455 return (spa_vdev_exit(spa, NULL, txg, ENOENT));
3458 if (!vd->vdev_ops->vdev_op_leaf)
3459 return (spa_vdev_exit(spa, NULL, txg, ENOTSUP));
3461 spa_strfree(vd->vdev_path);
3462 vd->vdev_path = spa_strdup(newpath);
3464 vdev_config_dirty(vd->vdev_top);
3466 return (spa_vdev_exit(spa, NULL, txg, 0));
3470 * ==========================================================================
3472 * ==========================================================================
3476 spa_scrub(spa_t *spa, pool_scrub_type_t type)
3478 ASSERT(spa_config_held(spa, SCL_ALL, RW_WRITER) == 0);
3480 if ((uint_t)type >= POOL_SCRUB_TYPES)
3484 * If a resilver was requested, but there is no DTL on a
3485 * writeable leaf device, we have nothing to do.
3487 if (type == POOL_SCRUB_RESILVER &&
3488 !vdev_resilver_needed(spa->spa_root_vdev, NULL, NULL)) {
3489 spa_async_request(spa, SPA_ASYNC_RESILVER_DONE);
3493 if (type == POOL_SCRUB_EVERYTHING &&
3494 spa->spa_dsl_pool->dp_scrub_func != SCRUB_FUNC_NONE &&
3495 spa->spa_dsl_pool->dp_scrub_isresilver)
3498 if (type == POOL_SCRUB_EVERYTHING || type == POOL_SCRUB_RESILVER) {
3499 return (dsl_pool_scrub_clean(spa->spa_dsl_pool));
3500 } else if (type == POOL_SCRUB_NONE) {
3501 return (dsl_pool_scrub_cancel(spa->spa_dsl_pool));
3508 * ==========================================================================
3509 * SPA async task processing
3510 * ==========================================================================
3514 spa_async_remove(spa_t *spa, vdev_t *vd)
3516 if (vd->vdev_remove_wanted) {
3517 vd->vdev_remove_wanted = 0;
3518 vdev_set_state(vd, B_FALSE, VDEV_STATE_REMOVED, VDEV_AUX_NONE);
3519 vdev_clear(spa, vd);
3520 vdev_state_dirty(vd->vdev_top);
3523 for (int c = 0; c < vd->vdev_children; c++)
3524 spa_async_remove(spa, vd->vdev_child[c]);
3528 spa_async_probe(spa_t *spa, vdev_t *vd)
3530 if (vd->vdev_probe_wanted) {
3531 vd->vdev_probe_wanted = 0;
3532 vdev_reopen(vd); /* vdev_open() does the actual probe */
3535 for (int c = 0; c < vd->vdev_children; c++)
3536 spa_async_probe(spa, vd->vdev_child[c]);
3540 spa_async_thread(spa_t *spa)
3544 ASSERT(spa->spa_sync_on);
3546 mutex_enter(&spa->spa_async_lock);
3547 tasks = spa->spa_async_tasks;
3548 spa->spa_async_tasks = 0;
3549 mutex_exit(&spa->spa_async_lock);
3552 * See if the config needs to be updated.
3554 if (tasks & SPA_ASYNC_CONFIG_UPDATE) {
3555 mutex_enter(&spa_namespace_lock);
3556 spa_config_update(spa, SPA_CONFIG_UPDATE_POOL);
3557 mutex_exit(&spa_namespace_lock);
3561 * See if any devices need to be marked REMOVED.
3563 if (tasks & SPA_ASYNC_REMOVE) {
3564 spa_vdev_state_enter(spa);
3565 spa_async_remove(spa, spa->spa_root_vdev);
3566 for (int i = 0; i < spa->spa_l2cache.sav_count; i++)
3567 spa_async_remove(spa, spa->spa_l2cache.sav_vdevs[i]);
3568 for (int i = 0; i < spa->spa_spares.sav_count; i++)
3569 spa_async_remove(spa, spa->spa_spares.sav_vdevs[i]);
3570 (void) spa_vdev_state_exit(spa, NULL, 0);
3574 * See if any devices need to be probed.
3576 if (tasks & SPA_ASYNC_PROBE) {
3577 spa_vdev_state_enter(spa);
3578 spa_async_probe(spa, spa->spa_root_vdev);
3579 (void) spa_vdev_state_exit(spa, NULL, 0);
3583 * If any devices are done replacing, detach them.
3585 if (tasks & SPA_ASYNC_RESILVER_DONE)
3586 spa_vdev_resilver_done(spa);
3589 * Kick off a resilver.
3591 if (tasks & SPA_ASYNC_RESILVER)
3592 VERIFY(spa_scrub(spa, POOL_SCRUB_RESILVER) == 0);
3595 * Let the world know that we're done.
3597 mutex_enter(&spa->spa_async_lock);
3598 spa->spa_async_thread = NULL;
3599 cv_broadcast(&spa->spa_async_cv);
3600 mutex_exit(&spa->spa_async_lock);
3605 spa_async_suspend(spa_t *spa)
3607 mutex_enter(&spa->spa_async_lock);
3608 spa->spa_async_suspended++;
3609 while (spa->spa_async_thread != NULL)
3610 cv_wait(&spa->spa_async_cv, &spa->spa_async_lock);
3611 mutex_exit(&spa->spa_async_lock);
3615 spa_async_resume(spa_t *spa)
3617 mutex_enter(&spa->spa_async_lock);
3618 ASSERT(spa->spa_async_suspended != 0);
3619 spa->spa_async_suspended--;
3620 mutex_exit(&spa->spa_async_lock);
3624 spa_async_dispatch(spa_t *spa)
3626 mutex_enter(&spa->spa_async_lock);
3627 if (spa->spa_async_tasks && !spa->spa_async_suspended &&
3628 spa->spa_async_thread == NULL &&
3629 rootdir != NULL && !vn_is_readonly(rootdir))
3630 spa->spa_async_thread = thread_create(NULL, 0,
3631 spa_async_thread, spa, 0, &p0, TS_RUN, maxclsyspri);
3632 mutex_exit(&spa->spa_async_lock);
3636 spa_async_request(spa_t *spa, int task)
3638 mutex_enter(&spa->spa_async_lock);
3639 spa->spa_async_tasks |= task;
3640 mutex_exit(&spa->spa_async_lock);
3644 * ==========================================================================
3645 * SPA syncing routines
3646 * ==========================================================================
3650 spa_sync_deferred_frees(spa_t *spa, uint64_t txg)
3652 bplist_t *bpl = &spa->spa_sync_bplist;
3660 zio = zio_root(spa, NULL, NULL, ZIO_FLAG_CANFAIL);
3662 while (bplist_iterate(bpl, &itor, &blk) == 0) {
3663 ASSERT(blk.blk_birth < txg);
3664 zio_nowait(zio_free(zio, spa, txg, &blk, NULL, NULL,
3665 ZIO_FLAG_MUSTSUCCEED));
3668 error = zio_wait(zio);
3669 ASSERT3U(error, ==, 0);
3671 tx = dmu_tx_create_assigned(spa->spa_dsl_pool, txg);
3672 bplist_vacate(bpl, tx);
3675 * Pre-dirty the first block so we sync to convergence faster.
3676 * (Usually only the first block is needed.)
3678 dmu_write(spa->spa_meta_objset, spa->spa_sync_bplist_obj, 0, 1, &c, tx);
3683 spa_sync_nvlist(spa_t *spa, uint64_t obj, nvlist_t *nv, dmu_tx_t *tx)
3685 char *packed = NULL;
3690 VERIFY(nvlist_size(nv, &nvsize, NV_ENCODE_XDR) == 0);
3693 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
3694 * information. This avoids the dbuf_will_dirty() path and
3695 * saves us a pre-read to get data we don't actually care about.
3697 bufsize = P2ROUNDUP(nvsize, SPA_CONFIG_BLOCKSIZE);
3698 packed = kmem_alloc(bufsize, KM_SLEEP);
3700 VERIFY(nvlist_pack(nv, &packed, &nvsize, NV_ENCODE_XDR,
3702 bzero(packed + nvsize, bufsize - nvsize);
3704 dmu_write(spa->spa_meta_objset, obj, 0, bufsize, packed, tx);
3706 kmem_free(packed, bufsize);
3708 VERIFY(0 == dmu_bonus_hold(spa->spa_meta_objset, obj, FTAG, &db));
3709 dmu_buf_will_dirty(db, tx);
3710 *(uint64_t *)db->db_data = nvsize;
3711 dmu_buf_rele(db, FTAG);
3715 spa_sync_aux_dev(spa_t *spa, spa_aux_vdev_t *sav, dmu_tx_t *tx,
3716 const char *config, const char *entry)
3726 * Update the MOS nvlist describing the list of available devices.
3727 * spa_validate_aux() will have already made sure this nvlist is
3728 * valid and the vdevs are labeled appropriately.
3730 if (sav->sav_object == 0) {
3731 sav->sav_object = dmu_object_alloc(spa->spa_meta_objset,
3732 DMU_OT_PACKED_NVLIST, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE,
3733 sizeof (uint64_t), tx);
3734 VERIFY(zap_update(spa->spa_meta_objset,
3735 DMU_POOL_DIRECTORY_OBJECT, entry, sizeof (uint64_t), 1,
3736 &sav->sav_object, tx) == 0);
3739 VERIFY(nvlist_alloc(&nvroot, NV_UNIQUE_NAME, KM_SLEEP) == 0);
3740 if (sav->sav_count == 0) {
3741 VERIFY(nvlist_add_nvlist_array(nvroot, config, NULL, 0) == 0);
3743 list = kmem_alloc(sav->sav_count * sizeof (void *), KM_SLEEP);
3744 for (i = 0; i < sav->sav_count; i++)
3745 list[i] = vdev_config_generate(spa, sav->sav_vdevs[i],
3746 B_FALSE, B_FALSE, B_TRUE);
3747 VERIFY(nvlist_add_nvlist_array(nvroot, config, list,
3748 sav->sav_count) == 0);
3749 for (i = 0; i < sav->sav_count; i++)
3750 nvlist_free(list[i]);
3751 kmem_free(list, sav->sav_count * sizeof (void *));
3754 spa_sync_nvlist(spa, sav->sav_object, nvroot, tx);
3755 nvlist_free(nvroot);
3757 sav->sav_sync = B_FALSE;
3761 spa_sync_config_object(spa_t *spa, dmu_tx_t *tx)
3765 if (list_is_empty(&spa->spa_config_dirty_list))
3768 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3770 config = spa_config_generate(spa, spa->spa_root_vdev,
3771 dmu_tx_get_txg(tx), B_FALSE);
3773 spa_config_exit(spa, SCL_STATE, FTAG);
3775 if (spa->spa_config_syncing)
3776 nvlist_free(spa->spa_config_syncing);
3777 spa->spa_config_syncing = config;
3779 spa_sync_nvlist(spa, spa->spa_config_object, config, tx);
3783 * Set zpool properties.
3786 spa_sync_props(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
3789 objset_t *mos = spa->spa_meta_objset;
3790 nvlist_t *nvp = arg2;
3795 const char *propname;
3796 zprop_type_t proptype;
3797 spa_config_dirent_t *dp;
3799 mutex_enter(&spa->spa_props_lock);
3802 while ((elem = nvlist_next_nvpair(nvp, elem))) {
3803 switch (prop = zpool_name_to_prop(nvpair_name(elem))) {
3804 case ZPOOL_PROP_VERSION:
3806 * Only set version for non-zpool-creation cases
3807 * (set/import). spa_create() needs special care
3808 * for version setting.
3810 if (tx->tx_txg != TXG_INITIAL) {
3811 VERIFY(nvpair_value_uint64(elem,
3813 ASSERT(intval <= SPA_VERSION);
3814 ASSERT(intval >= spa_version(spa));
3815 spa->spa_uberblock.ub_version = intval;
3816 vdev_config_dirty(spa->spa_root_vdev);
3820 case ZPOOL_PROP_ALTROOT:
3822 * 'altroot' is a non-persistent property. It should
3823 * have been set temporarily at creation or import time.
3825 ASSERT(spa->spa_root != NULL);
3828 case ZPOOL_PROP_CACHEFILE:
3830 * 'cachefile' is a non-persistent property, but note
3831 * an async request that the config cache needs to be
3834 VERIFY(nvpair_value_string(elem, &strval) == 0);
3836 dp = kmem_alloc(sizeof (spa_config_dirent_t), KM_SLEEP);
3838 if (strval[0] == '\0')
3839 dp->scd_path = spa_strdup(spa_config_path);
3840 else if (strcmp(strval, "none") == 0)
3841 dp->scd_path = NULL;
3843 dp->scd_path = spa_strdup(strval);
3845 list_insert_head(&spa->spa_config_list, dp);
3846 spa_async_request(spa, SPA_ASYNC_CONFIG_UPDATE);
3850 * Set pool property values in the poolprops mos object.
3852 if (spa->spa_pool_props_object == 0) {
3853 objset_t *mos = spa->spa_meta_objset;
3855 VERIFY((spa->spa_pool_props_object =
3856 zap_create(mos, DMU_OT_POOL_PROPS,
3857 DMU_OT_NONE, 0, tx)) > 0);
3859 VERIFY(zap_update(mos,
3860 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_PROPS,
3861 8, 1, &spa->spa_pool_props_object, tx)
3865 /* normalize the property name */
3866 propname = zpool_prop_to_name(prop);
3867 proptype = zpool_prop_get_type(prop);
3869 if (nvpair_type(elem) == DATA_TYPE_STRING) {
3870 ASSERT(proptype == PROP_TYPE_STRING);
3871 VERIFY(nvpair_value_string(elem, &strval) == 0);
3872 VERIFY(zap_update(mos,
3873 spa->spa_pool_props_object, propname,
3874 1, strlen(strval) + 1, strval, tx) == 0);
3876 } else if (nvpair_type(elem) == DATA_TYPE_UINT64) {
3877 VERIFY(nvpair_value_uint64(elem, &intval) == 0);
3879 if (proptype == PROP_TYPE_INDEX) {
3881 VERIFY(zpool_prop_index_to_string(
3882 prop, intval, &unused) == 0);
3884 VERIFY(zap_update(mos,
3885 spa->spa_pool_props_object, propname,
3886 8, 1, &intval, tx) == 0);
3888 ASSERT(0); /* not allowed */
3892 case ZPOOL_PROP_DELEGATION:
3893 spa->spa_delegation = intval;
3895 case ZPOOL_PROP_BOOTFS:
3896 spa->spa_bootfs = intval;
3898 case ZPOOL_PROP_FAILUREMODE:
3899 spa->spa_failmode = intval;
3906 /* log internal history if this is not a zpool create */
3907 if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY &&
3908 tx->tx_txg != TXG_INITIAL) {
3909 spa_history_internal_log(LOG_POOL_PROPSET,
3910 spa, tx, cr, "%s %lld %s",
3911 nvpair_name(elem), intval, spa_name(spa));
3915 mutex_exit(&spa->spa_props_lock);
3919 * Sync the specified transaction group. New blocks may be dirtied as
3920 * part of the process, so we iterate until it converges.
3923 spa_sync(spa_t *spa, uint64_t txg)
3925 dsl_pool_t *dp = spa->spa_dsl_pool;
3926 objset_t *mos = spa->spa_meta_objset;
3927 bplist_t *bpl = &spa->spa_sync_bplist;
3928 vdev_t *rvd = spa->spa_root_vdev;
3935 * Lock out configuration changes.
3937 spa_config_enter(spa, SCL_CONFIG, FTAG, RW_READER);
3939 spa->spa_syncing_txg = txg;
3940 spa->spa_sync_pass = 0;
3943 * If there are any pending vdev state changes, convert them
3944 * into config changes that go out with this transaction group.
3946 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
3947 while (list_head(&spa->spa_state_dirty_list) != NULL) {
3949 * We need the write lock here because, for aux vdevs,
3950 * calling vdev_config_dirty() modifies sav_config.
3951 * This is ugly and will become unnecessary when we
3952 * eliminate the aux vdev wart by integrating all vdevs
3953 * into the root vdev tree.
3955 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
3956 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_WRITER);
3957 while ((vd = list_head(&spa->spa_state_dirty_list)) != NULL) {
3958 vdev_state_clean(vd);
3959 vdev_config_dirty(vd);
3961 spa_config_exit(spa, SCL_CONFIG | SCL_STATE, FTAG);
3962 spa_config_enter(spa, SCL_CONFIG | SCL_STATE, FTAG, RW_READER);
3964 spa_config_exit(spa, SCL_STATE, FTAG);
3966 VERIFY(0 == bplist_open(bpl, mos, spa->spa_sync_bplist_obj));
3968 tx = dmu_tx_create_assigned(dp, txg);
3971 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
3972 * set spa_deflate if we have no raid-z vdevs.
3974 if (spa->spa_ubsync.ub_version < SPA_VERSION_RAIDZ_DEFLATE &&
3975 spa->spa_uberblock.ub_version >= SPA_VERSION_RAIDZ_DEFLATE) {
3978 for (i = 0; i < rvd->vdev_children; i++) {
3979 vd = rvd->vdev_child[i];
3980 if (vd->vdev_deflate_ratio != SPA_MINBLOCKSIZE)
3983 if (i == rvd->vdev_children) {
3984 spa->spa_deflate = TRUE;
3985 VERIFY(0 == zap_add(spa->spa_meta_objset,
3986 DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_DEFLATE,
3987 sizeof (uint64_t), 1, &spa->spa_deflate, tx));
3991 if (spa->spa_ubsync.ub_version < SPA_VERSION_ORIGIN &&
3992 spa->spa_uberblock.ub_version >= SPA_VERSION_ORIGIN) {
3993 dsl_pool_create_origin(dp, tx);
3995 /* Keeping the origin open increases spa_minref */
3996 spa->spa_minref += 3;
3999 if (spa->spa_ubsync.ub_version < SPA_VERSION_NEXT_CLONES &&
4000 spa->spa_uberblock.ub_version >= SPA_VERSION_NEXT_CLONES) {
4001 dsl_pool_upgrade_clones(dp, tx);
4005 * If anything has changed in this txg, push the deferred frees
4006 * from the previous txg. If not, leave them alone so that we
4007 * don't generate work on an otherwise idle system.
4009 if (!txg_list_empty(&dp->dp_dirty_datasets, txg) ||
4010 !txg_list_empty(&dp->dp_dirty_dirs, txg) ||
4011 !txg_list_empty(&dp->dp_sync_tasks, txg))
4012 spa_sync_deferred_frees(spa, txg);
4015 * Iterate to convergence.
4018 spa->spa_sync_pass++;
4020 spa_sync_config_object(spa, tx);
4021 spa_sync_aux_dev(spa, &spa->spa_spares, tx,
4022 ZPOOL_CONFIG_SPARES, DMU_POOL_SPARES);
4023 spa_sync_aux_dev(spa, &spa->spa_l2cache, tx,
4024 ZPOOL_CONFIG_L2CACHE, DMU_POOL_L2CACHE);
4025 spa_errlog_sync(spa, txg);
4026 dsl_pool_sync(dp, txg);
4029 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, txg)) {
4034 bplist_sync(bpl, tx);
4035 } while (dirty_vdevs);
4039 dprintf("txg %llu passes %d\n", txg, spa->spa_sync_pass);
4042 * Rewrite the vdev configuration (which includes the uberblock)
4043 * to commit the transaction group.
4045 * If there are no dirty vdevs, we sync the uberblock to a few
4046 * random top-level vdevs that are known to be visible in the
4047 * config cache (see spa_vdev_add() for a complete description).
4048 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
4052 * We hold SCL_STATE to prevent vdev open/close/etc.
4053 * while we're attempting to write the vdev labels.
4055 spa_config_enter(spa, SCL_STATE, FTAG, RW_READER);
4057 if (list_is_empty(&spa->spa_config_dirty_list)) {
4058 vdev_t *svd[SPA_DVAS_PER_BP];
4060 int children = rvd->vdev_children;
4061 int c0 = spa_get_random(children);
4064 for (c = 0; c < children; c++) {
4065 vd = rvd->vdev_child[(c0 + c) % children];
4066 if (vd->vdev_ms_array == 0 || vd->vdev_islog)
4068 svd[svdcount++] = vd;
4069 if (svdcount == SPA_DVAS_PER_BP)
4072 error = vdev_config_sync(svd, svdcount, txg);
4074 error = vdev_config_sync(rvd->vdev_child,
4075 rvd->vdev_children, txg);
4078 spa_config_exit(spa, SCL_STATE, FTAG);
4082 zio_suspend(spa, NULL);
4083 zio_resume_wait(spa);
4088 * Clear the dirty config list.
4090 while ((vd = list_head(&spa->spa_config_dirty_list)) != NULL)
4091 vdev_config_clean(vd);
4094 * Now that the new config has synced transactionally,
4095 * let it become visible to the config cache.
4097 if (spa->spa_config_syncing != NULL) {
4098 spa_config_set(spa, spa->spa_config_syncing);
4099 spa->spa_config_txg = txg;
4100 spa->spa_config_syncing = NULL;
4103 spa->spa_ubsync = spa->spa_uberblock;
4106 * Clean up the ZIL records for the synced txg.
4108 dsl_pool_zil_clean(dp);
4111 * Update usable space statistics.
4113 while (vd = txg_list_remove(&spa->spa_vdev_txg_list, TXG_CLEAN(txg)))
4114 vdev_sync_done(vd, txg);
4117 * It had better be the case that we didn't dirty anything
4118 * since vdev_config_sync().
4120 ASSERT(txg_list_empty(&dp->dp_dirty_datasets, txg));
4121 ASSERT(txg_list_empty(&dp->dp_dirty_dirs, txg));
4122 ASSERT(txg_list_empty(&spa->spa_vdev_txg_list, txg));
4123 ASSERT(bpl->bpl_queue == NULL);
4125 spa_config_exit(spa, SCL_CONFIG, FTAG);
4128 * If any async tasks have been requested, kick them off.
4130 spa_async_dispatch(spa);
4134 * Sync all pools. We don't want to hold the namespace lock across these
4135 * operations, so we take a reference on the spa_t and drop the lock during the
4139 spa_sync_allpools(void)
4142 mutex_enter(&spa_namespace_lock);
4143 while ((spa = spa_next(spa)) != NULL) {
4144 if (spa_state(spa) != POOL_STATE_ACTIVE || spa_suspended(spa))
4146 spa_open_ref(spa, FTAG);
4147 mutex_exit(&spa_namespace_lock);
4148 txg_wait_synced(spa_get_dsl(spa), 0);
4149 mutex_enter(&spa_namespace_lock);
4150 spa_close(spa, FTAG);
4152 mutex_exit(&spa_namespace_lock);
4156 * ==========================================================================
4157 * Miscellaneous routines
4158 * ==========================================================================
4162 * Remove all pools in the system.
4170 * Remove all cached state. All pools should be closed now,
4171 * so every spa in the AVL tree should be unreferenced.
4173 mutex_enter(&spa_namespace_lock);
4174 while ((spa = spa_next(NULL)) != NULL) {
4176 * Stop async tasks. The async thread may need to detach
4177 * a device that's been replaced, which requires grabbing
4178 * spa_namespace_lock, so we must drop it here.
4180 spa_open_ref(spa, FTAG);
4181 mutex_exit(&spa_namespace_lock);
4182 spa_async_suspend(spa);
4183 mutex_enter(&spa_namespace_lock);
4184 spa_close(spa, FTAG);
4186 if (spa->spa_state != POOL_STATE_UNINITIALIZED) {
4188 spa_deactivate(spa);
4192 mutex_exit(&spa_namespace_lock);
4196 spa_lookup_by_guid(spa_t *spa, uint64_t guid, boolean_t l2cache)
4201 if ((vd = vdev_lookup_by_guid(spa->spa_root_vdev, guid)) != NULL)
4205 for (i = 0; i < spa->spa_l2cache.sav_count; i++) {
4206 vd = spa->spa_l2cache.sav_vdevs[i];
4207 if (vd->vdev_guid == guid)
4216 spa_upgrade(spa_t *spa, uint64_t version)
4218 spa_config_enter(spa, SCL_ALL, FTAG, RW_WRITER);
4221 * This should only be called for a non-faulted pool, and since a
4222 * future version would result in an unopenable pool, this shouldn't be
4225 ASSERT(spa->spa_uberblock.ub_version <= SPA_VERSION);
4226 ASSERT(version >= spa->spa_uberblock.ub_version);
4228 spa->spa_uberblock.ub_version = version;
4229 vdev_config_dirty(spa->spa_root_vdev);
4231 spa_config_exit(spa, SCL_ALL, FTAG);
4233 txg_wait_synced(spa_get_dsl(spa), 0);
4237 spa_has_spare(spa_t *spa, uint64_t guid)
4241 spa_aux_vdev_t *sav = &spa->spa_spares;
4243 for (i = 0; i < sav->sav_count; i++)
4244 if (sav->sav_vdevs[i]->vdev_guid == guid)
4247 for (i = 0; i < sav->sav_npending; i++) {
4248 if (nvlist_lookup_uint64(sav->sav_pending[i], ZPOOL_CONFIG_GUID,
4249 &spareguid) == 0 && spareguid == guid)
4257 * Check if a pool has an active shared spare device.
4258 * Note: reference count of an active spare is 2, as a spare and as a replace
4261 spa_has_active_shared_spare(spa_t *spa)
4265 spa_aux_vdev_t *sav = &spa->spa_spares;
4267 for (i = 0; i < sav->sav_count; i++) {
4268 if (spa_spare_exists(sav->sav_vdevs[i]->vdev_guid, &pool,
4269 &refcnt) && pool != 0ULL && pool == spa_guid(spa) &&
4278 * Post a sysevent corresponding to the given event. The 'name' must be one of
4279 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
4280 * filled in from the spa and (optionally) the vdev. This doesn't do anything
4281 * in the userland libzpool, as we don't want consumers to misinterpret ztest
4282 * or zdb as real changes.
4285 spa_event_notify(spa_t *spa, vdev_t *vd, const char *name)
4289 sysevent_attr_list_t *attr = NULL;
4290 sysevent_value_t value;
4293 ev = sysevent_alloc(EC_ZFS, (char *)name, SUNW_KERN_PUB "zfs",
4296 value.value_type = SE_DATA_TYPE_STRING;
4297 value.value.sv_string = spa_name(spa);
4298 if (sysevent_add_attr(&attr, ZFS_EV_POOL_NAME, &value, SE_SLEEP) != 0)
4301 value.value_type = SE_DATA_TYPE_UINT64;
4302 value.value.sv_uint64 = spa_guid(spa);
4303 if (sysevent_add_attr(&attr, ZFS_EV_POOL_GUID, &value, SE_SLEEP) != 0)
4307 value.value_type = SE_DATA_TYPE_UINT64;
4308 value.value.sv_uint64 = vd->vdev_guid;
4309 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_GUID, &value,
4313 if (vd->vdev_path) {
4314 value.value_type = SE_DATA_TYPE_STRING;
4315 value.value.sv_string = vd->vdev_path;
4316 if (sysevent_add_attr(&attr, ZFS_EV_VDEV_PATH,
4317 &value, SE_SLEEP) != 0)
4322 if (sysevent_attach_attributes(ev, attr) != 0)
4326 (void) log_sysevent(ev, SE_SLEEP, &eid);
4330 sysevent_free_attr(attr);