4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
25 * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * Copyright 2017 Joyent, Inc.
31 * Copyright (c) 2017 Datto Inc.
35 * SPA: Storage Pool Allocator
37 * This file contains all the routines used when modifying on-disk SPA state.
38 * This includes opening, importing, destroying, exporting a pool, and syncing a
42 #include <sys/zfs_context.h>
43 #include <sys/fm/fs/zfs.h>
44 #include <sys/spa_impl.h>
46 #include <sys/zio_checksum.h>
48 #include <sys/dmu_tx.h>
52 #include <sys/vdev_impl.h>
53 #include <sys/vdev_removal.h>
54 #include <sys/vdev_indirect_mapping.h>
55 #include <sys/vdev_indirect_births.h>
56 #include <sys/metaslab.h>
57 #include <sys/metaslab_impl.h>
58 #include <sys/uberblock_impl.h>
61 #include <sys/bpobj.h>
62 #include <sys/dmu_traverse.h>
63 #include <sys/dmu_objset.h>
64 #include <sys/unique.h>
65 #include <sys/dsl_pool.h>
66 #include <sys/dsl_dataset.h>
67 #include <sys/dsl_dir.h>
68 #include <sys/dsl_prop.h>
69 #include <sys/dsl_synctask.h>
70 #include <sys/fs/zfs.h>
72 #include <sys/callb.h>
73 #include <sys/systeminfo.h>
74 #include <sys/spa_boot.h>
75 #include <sys/zfs_ioctl.h>
76 #include <sys/dsl_scan.h>
77 #include <sys/zfeature.h>
78 #include <sys/dsl_destroy.h>
82 #include <sys/bootprops.h>
83 #include <sys/callb.h>
84 #include <sys/cpupart.h>
86 #include <sys/sysdc.h>
91 #include "zfs_comutil.h"
94 * The interval, in seconds, at which failed configuration cache file writes
97 int zfs_ccw_retry_interval
= 300;
99 typedef enum zti_modes
{
100 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
101 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
102 ZTI_MODE_NULL
, /* don't create a taskq */
106 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
107 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
108 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
110 #define ZTI_N(n) ZTI_P(n, 1)
111 #define ZTI_ONE ZTI_N(1)
113 typedef struct zio_taskq_info
{
114 zti_modes_t zti_mode
;
119 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
120 "issue", "issue_high", "intr", "intr_high"
124 * This table defines the taskq settings for each ZFS I/O type. When
125 * initializing a pool, we use this table to create an appropriately sized
126 * taskq. Some operations are low volume and therefore have a small, static
127 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
128 * macros. Other operations process a large amount of data; the ZTI_BATCH
129 * macro causes us to create a taskq oriented for throughput. Some operations
130 * are so high frequency and short-lived that the taskq itself can become a a
131 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
132 * additional degree of parallelism specified by the number of threads per-
133 * taskq and the number of taskqs; when dispatching an event in this case, the
134 * particular taskq is chosen at random.
136 * The different taskq priorities are to handle the different contexts (issue
137 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
138 * need to be handled with minimum delay.
140 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
141 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
142 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
143 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
144 { ZTI_BATCH
, ZTI_N(5), ZTI_N(8), ZTI_N(5) }, /* WRITE */
145 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
146 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
147 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
150 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
151 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
152 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
153 static int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
154 spa_load_state_t state
, spa_import_type_t type
, boolean_t trust_config
,
156 static void spa_vdev_resilver_done(spa_t
*spa
);
158 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
159 id_t zio_taskq_psrset_bind
= PS_NONE
;
160 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
161 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
163 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
164 extern int zfs_sync_pass_deferred_free
;
167 * This (illegal) pool name is used when temporarily importing a spa_t in order
168 * to get the vdev stats associated with the imported devices.
170 #define TRYIMPORT_NAME "$import"
173 * ==========================================================================
174 * SPA properties routines
175 * ==========================================================================
179 * Add a (source=src, propname=propval) list to an nvlist.
182 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
183 uint64_t intval
, zprop_source_t src
)
185 const char *propname
= zpool_prop_to_name(prop
);
188 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
189 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
192 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
194 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
196 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
197 nvlist_free(propval
);
201 * Get property values from the spa configuration.
204 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
206 vdev_t
*rvd
= spa
->spa_root_vdev
;
207 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
208 uint64_t size
, alloc
, cap
, version
;
209 zprop_source_t src
= ZPROP_SRC_NONE
;
210 spa_config_dirent_t
*dp
;
211 metaslab_class_t
*mc
= spa_normal_class(spa
);
213 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
216 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
217 size
= metaslab_class_get_space(spa_normal_class(spa
));
218 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
219 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
220 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
221 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
224 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
225 metaslab_class_fragmentation(mc
), src
);
226 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
227 metaslab_class_expandable_space(mc
), src
);
228 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
229 (spa_mode(spa
) == FREAD
), src
);
231 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
232 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
234 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
235 ddt_get_pool_dedup_ratio(spa
), src
);
237 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
238 rvd
->vdev_state
, src
);
240 version
= spa_version(spa
);
241 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
242 src
= ZPROP_SRC_DEFAULT
;
244 src
= ZPROP_SRC_LOCAL
;
245 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
250 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
251 * when opening pools before this version freedir will be NULL.
253 if (pool
->dp_free_dir
!= NULL
) {
254 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
255 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
258 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
262 if (pool
->dp_leak_dir
!= NULL
) {
263 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
264 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
267 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
272 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
274 if (spa
->spa_comment
!= NULL
) {
275 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
279 if (spa
->spa_root
!= NULL
)
280 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
283 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
284 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
285 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
287 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
288 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
291 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
292 if (dp
->scd_path
== NULL
) {
293 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
294 "none", 0, ZPROP_SRC_LOCAL
);
295 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
296 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
297 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
303 * Get zpool property values.
306 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
308 objset_t
*mos
= spa
->spa_meta_objset
;
313 VERIFY(nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
315 mutex_enter(&spa
->spa_props_lock
);
318 * Get properties from the spa config.
320 spa_prop_get_config(spa
, nvp
);
322 /* If no pool property object, no more prop to get. */
323 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
324 mutex_exit(&spa
->spa_props_lock
);
329 * Get properties from the MOS pool property object.
331 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
332 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
333 zap_cursor_advance(&zc
)) {
336 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
339 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
342 switch (za
.za_integer_length
) {
344 /* integer property */
345 if (za
.za_first_integer
!=
346 zpool_prop_default_numeric(prop
))
347 src
= ZPROP_SRC_LOCAL
;
349 if (prop
== ZPOOL_PROP_BOOTFS
) {
351 dsl_dataset_t
*ds
= NULL
;
353 dp
= spa_get_dsl(spa
);
354 dsl_pool_config_enter(dp
, FTAG
);
355 if (err
= dsl_dataset_hold_obj(dp
,
356 za
.za_first_integer
, FTAG
, &ds
)) {
357 dsl_pool_config_exit(dp
, FTAG
);
361 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
363 dsl_dataset_name(ds
, strval
);
364 dsl_dataset_rele(ds
, FTAG
);
365 dsl_pool_config_exit(dp
, FTAG
);
368 intval
= za
.za_first_integer
;
371 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
374 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
379 /* string property */
380 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
381 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
382 za
.za_name
, 1, za
.za_num_integers
, strval
);
384 kmem_free(strval
, za
.za_num_integers
);
387 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
388 kmem_free(strval
, za
.za_num_integers
);
395 zap_cursor_fini(&zc
);
396 mutex_exit(&spa
->spa_props_lock
);
398 if (err
&& err
!= ENOENT
) {
408 * Validate the given pool properties nvlist and modify the list
409 * for the property values to be set.
412 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
415 int error
= 0, reset_bootfs
= 0;
417 boolean_t has_feature
= B_FALSE
;
420 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
422 char *strval
, *slash
, *check
, *fname
;
423 const char *propname
= nvpair_name(elem
);
424 zpool_prop_t prop
= zpool_name_to_prop(propname
);
428 if (!zpool_prop_feature(propname
)) {
429 error
= SET_ERROR(EINVAL
);
434 * Sanitize the input.
436 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
437 error
= SET_ERROR(EINVAL
);
441 if (nvpair_value_uint64(elem
, &intval
) != 0) {
442 error
= SET_ERROR(EINVAL
);
447 error
= SET_ERROR(EINVAL
);
451 fname
= strchr(propname
, '@') + 1;
452 if (zfeature_lookup_name(fname
, NULL
) != 0) {
453 error
= SET_ERROR(EINVAL
);
457 has_feature
= B_TRUE
;
460 case ZPOOL_PROP_VERSION
:
461 error
= nvpair_value_uint64(elem
, &intval
);
463 (intval
< spa_version(spa
) ||
464 intval
> SPA_VERSION_BEFORE_FEATURES
||
466 error
= SET_ERROR(EINVAL
);
469 case ZPOOL_PROP_DELEGATION
:
470 case ZPOOL_PROP_AUTOREPLACE
:
471 case ZPOOL_PROP_LISTSNAPS
:
472 case ZPOOL_PROP_AUTOEXPAND
:
473 error
= nvpair_value_uint64(elem
, &intval
);
474 if (!error
&& intval
> 1)
475 error
= SET_ERROR(EINVAL
);
478 case ZPOOL_PROP_BOOTFS
:
480 * If the pool version is less than SPA_VERSION_BOOTFS,
481 * or the pool is still being created (version == 0),
482 * the bootfs property cannot be set.
484 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
485 error
= SET_ERROR(ENOTSUP
);
490 * Make sure the vdev config is bootable
492 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
493 error
= SET_ERROR(ENOTSUP
);
499 error
= nvpair_value_string(elem
, &strval
);
505 if (strval
== NULL
|| strval
[0] == '\0') {
506 objnum
= zpool_prop_default_numeric(
511 if (error
= dmu_objset_hold(strval
, FTAG
, &os
))
515 * Must be ZPL, and its property settings
516 * must be supported by GRUB (compression
517 * is not gzip, and large blocks are not used).
520 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
521 error
= SET_ERROR(ENOTSUP
);
523 dsl_prop_get_int_ds(dmu_objset_ds(os
),
524 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
526 !BOOTFS_COMPRESS_VALID(propval
)) {
527 error
= SET_ERROR(ENOTSUP
);
529 objnum
= dmu_objset_id(os
);
531 dmu_objset_rele(os
, FTAG
);
535 case ZPOOL_PROP_FAILUREMODE
:
536 error
= nvpair_value_uint64(elem
, &intval
);
537 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
538 intval
> ZIO_FAILURE_MODE_PANIC
))
539 error
= SET_ERROR(EINVAL
);
542 * This is a special case which only occurs when
543 * the pool has completely failed. This allows
544 * the user to change the in-core failmode property
545 * without syncing it out to disk (I/Os might
546 * currently be blocked). We do this by returning
547 * EIO to the caller (spa_prop_set) to trick it
548 * into thinking we encountered a property validation
551 if (!error
&& spa_suspended(spa
)) {
552 spa
->spa_failmode
= intval
;
553 error
= SET_ERROR(EIO
);
557 case ZPOOL_PROP_CACHEFILE
:
558 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
561 if (strval
[0] == '\0')
564 if (strcmp(strval
, "none") == 0)
567 if (strval
[0] != '/') {
568 error
= SET_ERROR(EINVAL
);
572 slash
= strrchr(strval
, '/');
573 ASSERT(slash
!= NULL
);
575 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
576 strcmp(slash
, "/..") == 0)
577 error
= SET_ERROR(EINVAL
);
580 case ZPOOL_PROP_COMMENT
:
581 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
583 for (check
= strval
; *check
!= '\0'; check
++) {
585 * The kernel doesn't have an easy isprint()
586 * check. For this kernel check, we merely
587 * check ASCII apart from DEL. Fix this if
588 * there is an easy-to-use kernel isprint().
590 if (*check
>= 0x7f) {
591 error
= SET_ERROR(EINVAL
);
595 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
599 case ZPOOL_PROP_DEDUPDITTO
:
600 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
601 error
= SET_ERROR(ENOTSUP
);
603 error
= nvpair_value_uint64(elem
, &intval
);
605 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
606 error
= SET_ERROR(EINVAL
);
614 if (!error
&& reset_bootfs
) {
615 error
= nvlist_remove(props
,
616 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
619 error
= nvlist_add_uint64(props
,
620 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
628 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
631 spa_config_dirent_t
*dp
;
633 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
637 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
640 if (cachefile
[0] == '\0')
641 dp
->scd_path
= spa_strdup(spa_config_path
);
642 else if (strcmp(cachefile
, "none") == 0)
645 dp
->scd_path
= spa_strdup(cachefile
);
647 list_insert_head(&spa
->spa_config_list
, dp
);
649 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
653 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
656 nvpair_t
*elem
= NULL
;
657 boolean_t need_sync
= B_FALSE
;
659 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
662 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
663 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
665 if (prop
== ZPOOL_PROP_CACHEFILE
||
666 prop
== ZPOOL_PROP_ALTROOT
||
667 prop
== ZPOOL_PROP_READONLY
)
670 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
673 if (prop
== ZPOOL_PROP_VERSION
) {
674 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
676 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
677 ver
= SPA_VERSION_FEATURES
;
681 /* Save time if the version is already set. */
682 if (ver
== spa_version(spa
))
686 * In addition to the pool directory object, we might
687 * create the pool properties object, the features for
688 * read object, the features for write object, or the
689 * feature descriptions object.
691 error
= dsl_sync_task(spa
->spa_name
, NULL
,
692 spa_sync_version
, &ver
,
693 6, ZFS_SPACE_CHECK_RESERVED
);
704 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
705 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
712 * If the bootfs property value is dsobj, clear it.
715 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
717 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
718 VERIFY(zap_remove(spa
->spa_meta_objset
,
719 spa
->spa_pool_props_object
,
720 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
727 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
729 uint64_t *newguid
= arg
;
730 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
731 vdev_t
*rvd
= spa
->spa_root_vdev
;
734 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
735 vdev_state
= rvd
->vdev_state
;
736 spa_config_exit(spa
, SCL_STATE
, FTAG
);
738 if (vdev_state
!= VDEV_STATE_HEALTHY
)
739 return (SET_ERROR(ENXIO
));
741 ASSERT3U(spa_guid(spa
), !=, *newguid
);
747 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
749 uint64_t *newguid
= arg
;
750 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
752 vdev_t
*rvd
= spa
->spa_root_vdev
;
754 oldguid
= spa_guid(spa
);
756 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
757 rvd
->vdev_guid
= *newguid
;
758 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
759 vdev_config_dirty(rvd
);
760 spa_config_exit(spa
, SCL_STATE
, FTAG
);
762 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
767 * Change the GUID for the pool. This is done so that we can later
768 * re-import a pool built from a clone of our own vdevs. We will modify
769 * the root vdev's guid, our own pool guid, and then mark all of our
770 * vdevs dirty. Note that we must make sure that all our vdevs are
771 * online when we do this, or else any vdevs that weren't present
772 * would be orphaned from our pool. We are also going to issue a
773 * sysevent to update any watchers.
776 spa_change_guid(spa_t
*spa
)
781 mutex_enter(&spa
->spa_vdev_top_lock
);
782 mutex_enter(&spa_namespace_lock
);
783 guid
= spa_generate_guid(NULL
);
785 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
786 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
789 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
790 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
793 mutex_exit(&spa_namespace_lock
);
794 mutex_exit(&spa
->spa_vdev_top_lock
);
800 * ==========================================================================
801 * SPA state manipulation (open/create/destroy/import/export)
802 * ==========================================================================
806 spa_error_entry_compare(const void *a
, const void *b
)
808 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
809 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
812 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
813 sizeof (zbookmark_phys_t
));
824 * Utility function which retrieves copies of the current logs and
825 * re-initializes them in the process.
828 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
830 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
832 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
833 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
835 avl_create(&spa
->spa_errlist_scrub
,
836 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
837 offsetof(spa_error_entry_t
, se_avl
));
838 avl_create(&spa
->spa_errlist_last
,
839 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
840 offsetof(spa_error_entry_t
, se_avl
));
844 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
846 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
847 enum zti_modes mode
= ztip
->zti_mode
;
848 uint_t value
= ztip
->zti_value
;
849 uint_t count
= ztip
->zti_count
;
850 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
853 boolean_t batch
= B_FALSE
;
855 if (mode
== ZTI_MODE_NULL
) {
857 tqs
->stqs_taskq
= NULL
;
861 ASSERT3U(count
, >, 0);
863 tqs
->stqs_count
= count
;
864 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
868 ASSERT3U(value
, >=, 1);
869 value
= MAX(value
, 1);
874 flags
|= TASKQ_THREADS_CPU_PCT
;
875 value
= zio_taskq_batch_pct
;
879 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
881 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
885 for (uint_t i
= 0; i
< count
; i
++) {
889 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
890 zio_type_name
[t
], zio_taskq_types
[q
], i
);
892 (void) snprintf(name
, sizeof (name
), "%s_%s",
893 zio_type_name
[t
], zio_taskq_types
[q
]);
896 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
898 flags
|= TASKQ_DC_BATCH
;
900 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
901 spa
->spa_proc
, zio_taskq_basedc
, flags
);
903 pri_t pri
= maxclsyspri
;
905 * The write issue taskq can be extremely CPU
906 * intensive. Run it at slightly lower priority
907 * than the other taskqs.
909 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
912 tq
= taskq_create_proc(name
, value
, pri
, 50,
913 INT_MAX
, spa
->spa_proc
, flags
);
916 tqs
->stqs_taskq
[i
] = tq
;
921 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
923 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
925 if (tqs
->stqs_taskq
== NULL
) {
926 ASSERT0(tqs
->stqs_count
);
930 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
931 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
932 taskq_destroy(tqs
->stqs_taskq
[i
]);
935 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
936 tqs
->stqs_taskq
= NULL
;
940 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
941 * Note that a type may have multiple discrete taskqs to avoid lock contention
942 * on the taskq itself. In that case we choose which taskq at random by using
943 * the low bits of gethrtime().
946 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
947 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
949 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
952 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
953 ASSERT3U(tqs
->stqs_count
, !=, 0);
955 if (tqs
->stqs_count
== 1) {
956 tq
= tqs
->stqs_taskq
[0];
958 tq
= tqs
->stqs_taskq
[gethrtime() % tqs
->stqs_count
];
961 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
965 spa_create_zio_taskqs(spa_t
*spa
)
967 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
968 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
969 spa_taskqs_init(spa
, t
, q
);
976 spa_thread(void *arg
)
981 user_t
*pu
= PTOU(curproc
);
983 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
986 ASSERT(curproc
!= &p0
);
987 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
988 "zpool-%s", spa
->spa_name
);
989 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
991 /* bind this thread to the requested psrset */
992 if (zio_taskq_psrset_bind
!= PS_NONE
) {
994 mutex_enter(&cpu_lock
);
995 mutex_enter(&pidlock
);
996 mutex_enter(&curproc
->p_lock
);
998 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
999 0, NULL
, NULL
) == 0) {
1000 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1003 "Couldn't bind process for zfs pool \"%s\" to "
1004 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1007 mutex_exit(&curproc
->p_lock
);
1008 mutex_exit(&pidlock
);
1009 mutex_exit(&cpu_lock
);
1013 if (zio_taskq_sysdc
) {
1014 sysdc_thread_enter(curthread
, 100, 0);
1017 spa
->spa_proc
= curproc
;
1018 spa
->spa_did
= curthread
->t_did
;
1020 spa_create_zio_taskqs(spa
);
1022 mutex_enter(&spa
->spa_proc_lock
);
1023 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1025 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1026 cv_broadcast(&spa
->spa_proc_cv
);
1028 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1029 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1030 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1031 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1033 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1034 spa
->spa_proc_state
= SPA_PROC_GONE
;
1035 spa
->spa_proc
= &p0
;
1036 cv_broadcast(&spa
->spa_proc_cv
);
1037 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1039 mutex_enter(&curproc
->p_lock
);
1045 * Activate an uninitialized pool.
1048 spa_activate(spa_t
*spa
, int mode
)
1050 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1052 spa
->spa_state
= POOL_STATE_ACTIVE
;
1053 spa
->spa_mode
= mode
;
1055 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1056 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1058 /* Try to create a covering process */
1059 mutex_enter(&spa
->spa_proc_lock
);
1060 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1061 ASSERT(spa
->spa_proc
== &p0
);
1064 /* Only create a process if we're going to be around a while. */
1065 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1066 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1068 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1069 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1070 cv_wait(&spa
->spa_proc_cv
,
1071 &spa
->spa_proc_lock
);
1073 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1074 ASSERT(spa
->spa_proc
!= &p0
);
1075 ASSERT(spa
->spa_did
!= 0);
1079 "Couldn't create process for zfs pool \"%s\"\n",
1084 mutex_exit(&spa
->spa_proc_lock
);
1086 /* If we didn't create a process, we need to create our taskqs. */
1087 if (spa
->spa_proc
== &p0
) {
1088 spa_create_zio_taskqs(spa
);
1091 for (size_t i
= 0; i
< TXG_SIZE
; i
++)
1092 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
, 0);
1094 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1095 offsetof(vdev_t
, vdev_config_dirty_node
));
1096 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1097 offsetof(objset_t
, os_evicting_node
));
1098 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1099 offsetof(vdev_t
, vdev_state_dirty_node
));
1101 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1102 offsetof(struct vdev
, vdev_txg_node
));
1104 avl_create(&spa
->spa_errlist_scrub
,
1105 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1106 offsetof(spa_error_entry_t
, se_avl
));
1107 avl_create(&spa
->spa_errlist_last
,
1108 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1109 offsetof(spa_error_entry_t
, se_avl
));
1113 * Opposite of spa_activate().
1116 spa_deactivate(spa_t
*spa
)
1118 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1119 ASSERT(spa
->spa_dsl_pool
== NULL
);
1120 ASSERT(spa
->spa_root_vdev
== NULL
);
1121 ASSERT(spa
->spa_async_zio_root
== NULL
);
1122 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1124 spa_evicting_os_wait(spa
);
1126 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1128 list_destroy(&spa
->spa_config_dirty_list
);
1129 list_destroy(&spa
->spa_evicting_os_list
);
1130 list_destroy(&spa
->spa_state_dirty_list
);
1132 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1133 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1134 spa_taskqs_fini(spa
, t
, q
);
1138 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1139 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1140 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1141 spa
->spa_txg_zio
[i
] = NULL
;
1144 metaslab_class_destroy(spa
->spa_normal_class
);
1145 spa
->spa_normal_class
= NULL
;
1147 metaslab_class_destroy(spa
->spa_log_class
);
1148 spa
->spa_log_class
= NULL
;
1151 * If this was part of an import or the open otherwise failed, we may
1152 * still have errors left in the queues. Empty them just in case.
1154 spa_errlog_drain(spa
);
1156 avl_destroy(&spa
->spa_errlist_scrub
);
1157 avl_destroy(&spa
->spa_errlist_last
);
1159 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1161 mutex_enter(&spa
->spa_proc_lock
);
1162 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1163 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1164 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1165 cv_broadcast(&spa
->spa_proc_cv
);
1166 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1167 ASSERT(spa
->spa_proc
!= &p0
);
1168 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1170 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1171 spa
->spa_proc_state
= SPA_PROC_NONE
;
1173 ASSERT(spa
->spa_proc
== &p0
);
1174 mutex_exit(&spa
->spa_proc_lock
);
1177 * We want to make sure spa_thread() has actually exited the ZFS
1178 * module, so that the module can't be unloaded out from underneath
1181 if (spa
->spa_did
!= 0) {
1182 thread_join(spa
->spa_did
);
1188 * Verify a pool configuration, and construct the vdev tree appropriately. This
1189 * will create all the necessary vdevs in the appropriate layout, with each vdev
1190 * in the CLOSED state. This will prep the pool before open/creation/import.
1191 * All vdev validation is done by the vdev_alloc() routine.
1194 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1195 uint_t id
, int atype
)
1201 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1204 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1207 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1210 if (error
== ENOENT
)
1216 return (SET_ERROR(EINVAL
));
1219 for (int c
= 0; c
< children
; c
++) {
1221 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1229 ASSERT(*vdp
!= NULL
);
1235 * Opposite of spa_load().
1238 spa_unload(spa_t
*spa
)
1242 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1247 spa_async_suspend(spa
);
1252 if (spa
->spa_sync_on
) {
1253 txg_sync_stop(spa
->spa_dsl_pool
);
1254 spa
->spa_sync_on
= B_FALSE
;
1258 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1259 * to call it earlier, before we wait for async i/o to complete.
1260 * This ensures that there is no async metaslab prefetching, by
1261 * calling taskq_wait(mg_taskq).
1263 if (spa
->spa_root_vdev
!= NULL
) {
1264 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1265 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1266 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1267 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1271 * Wait for any outstanding async I/O to complete.
1273 if (spa
->spa_async_zio_root
!= NULL
) {
1274 for (int i
= 0; i
< max_ncpus
; i
++)
1275 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1276 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1277 spa
->spa_async_zio_root
= NULL
;
1280 if (spa
->spa_vdev_removal
!= NULL
) {
1281 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1282 spa
->spa_vdev_removal
= NULL
;
1285 spa_condense_fini(spa
);
1287 bpobj_close(&spa
->spa_deferred_bpobj
);
1289 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1294 if (spa
->spa_root_vdev
)
1295 vdev_free(spa
->spa_root_vdev
);
1296 ASSERT(spa
->spa_root_vdev
== NULL
);
1299 * Close the dsl pool.
1301 if (spa
->spa_dsl_pool
) {
1302 dsl_pool_close(spa
->spa_dsl_pool
);
1303 spa
->spa_dsl_pool
= NULL
;
1304 spa
->spa_meta_objset
= NULL
;
1310 * Drop and purge level 2 cache
1312 spa_l2cache_drop(spa
);
1314 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1315 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1316 if (spa
->spa_spares
.sav_vdevs
) {
1317 kmem_free(spa
->spa_spares
.sav_vdevs
,
1318 spa
->spa_spares
.sav_count
* sizeof (void *));
1319 spa
->spa_spares
.sav_vdevs
= NULL
;
1321 if (spa
->spa_spares
.sav_config
) {
1322 nvlist_free(spa
->spa_spares
.sav_config
);
1323 spa
->spa_spares
.sav_config
= NULL
;
1325 spa
->spa_spares
.sav_count
= 0;
1327 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1328 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1329 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1331 if (spa
->spa_l2cache
.sav_vdevs
) {
1332 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1333 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1334 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1336 if (spa
->spa_l2cache
.sav_config
) {
1337 nvlist_free(spa
->spa_l2cache
.sav_config
);
1338 spa
->spa_l2cache
.sav_config
= NULL
;
1340 spa
->spa_l2cache
.sav_count
= 0;
1342 spa
->spa_async_suspended
= 0;
1344 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1346 if (spa
->spa_comment
!= NULL
) {
1347 spa_strfree(spa
->spa_comment
);
1348 spa
->spa_comment
= NULL
;
1351 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1355 * Load (or re-load) the current list of vdevs describing the active spares for
1356 * this pool. When this is called, we have some form of basic information in
1357 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1358 * then re-generate a more complete list including status information.
1361 spa_load_spares(spa_t
*spa
)
1368 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1371 * First, close and free any existing spare vdevs.
1373 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1374 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1376 /* Undo the call to spa_activate() below */
1377 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1378 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1379 spa_spare_remove(tvd
);
1384 if (spa
->spa_spares
.sav_vdevs
)
1385 kmem_free(spa
->spa_spares
.sav_vdevs
,
1386 spa
->spa_spares
.sav_count
* sizeof (void *));
1388 if (spa
->spa_spares
.sav_config
== NULL
)
1391 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1392 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1394 spa
->spa_spares
.sav_count
= (int)nspares
;
1395 spa
->spa_spares
.sav_vdevs
= NULL
;
1401 * Construct the array of vdevs, opening them to get status in the
1402 * process. For each spare, there is potentially two different vdev_t
1403 * structures associated with it: one in the list of spares (used only
1404 * for basic validation purposes) and one in the active vdev
1405 * configuration (if it's spared in). During this phase we open and
1406 * validate each vdev on the spare list. If the vdev also exists in the
1407 * active configuration, then we also mark this vdev as an active spare.
1409 spa
->spa_spares
.sav_vdevs
= kmem_alloc(nspares
* sizeof (void *),
1411 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1412 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1413 VDEV_ALLOC_SPARE
) == 0);
1416 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1418 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1419 B_FALSE
)) != NULL
) {
1420 if (!tvd
->vdev_isspare
)
1424 * We only mark the spare active if we were successfully
1425 * able to load the vdev. Otherwise, importing a pool
1426 * with a bad active spare would result in strange
1427 * behavior, because multiple pool would think the spare
1428 * is actively in use.
1430 * There is a vulnerability here to an equally bizarre
1431 * circumstance, where a dead active spare is later
1432 * brought back to life (onlined or otherwise). Given
1433 * the rarity of this scenario, and the extra complexity
1434 * it adds, we ignore the possibility.
1436 if (!vdev_is_dead(tvd
))
1437 spa_spare_activate(tvd
);
1441 vd
->vdev_aux
= &spa
->spa_spares
;
1443 if (vdev_open(vd
) != 0)
1446 if (vdev_validate_aux(vd
) == 0)
1451 * Recompute the stashed list of spares, with status information
1454 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1455 DATA_TYPE_NVLIST_ARRAY
) == 0);
1457 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1459 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1460 spares
[i
] = vdev_config_generate(spa
,
1461 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1462 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1463 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1464 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1465 nvlist_free(spares
[i
]);
1466 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1470 * Load (or re-load) the current list of vdevs describing the active l2cache for
1471 * this pool. When this is called, we have some form of basic information in
1472 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1473 * then re-generate a more complete list including status information.
1474 * Devices which are already active have their details maintained, and are
1478 spa_load_l2cache(spa_t
*spa
)
1482 int i
, j
, oldnvdevs
;
1484 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1485 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1487 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1489 if (sav
->sav_config
!= NULL
) {
1490 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1491 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1492 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1498 oldvdevs
= sav
->sav_vdevs
;
1499 oldnvdevs
= sav
->sav_count
;
1500 sav
->sav_vdevs
= NULL
;
1504 * Process new nvlist of vdevs.
1506 for (i
= 0; i
< nl2cache
; i
++) {
1507 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1511 for (j
= 0; j
< oldnvdevs
; j
++) {
1513 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1515 * Retain previous vdev for add/remove ops.
1523 if (newvdevs
[i
] == NULL
) {
1527 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1528 VDEV_ALLOC_L2CACHE
) == 0);
1533 * Commit this vdev as an l2cache device,
1534 * even if it fails to open.
1536 spa_l2cache_add(vd
);
1541 spa_l2cache_activate(vd
);
1543 if (vdev_open(vd
) != 0)
1546 (void) vdev_validate_aux(vd
);
1548 if (!vdev_is_dead(vd
))
1549 l2arc_add_vdev(spa
, vd
);
1554 * Purge vdevs that were dropped
1556 for (i
= 0; i
< oldnvdevs
; i
++) {
1561 ASSERT(vd
->vdev_isl2cache
);
1563 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1564 pool
!= 0ULL && l2arc_vdev_present(vd
))
1565 l2arc_remove_vdev(vd
);
1566 vdev_clear_stats(vd
);
1572 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1574 if (sav
->sav_config
== NULL
)
1577 sav
->sav_vdevs
= newvdevs
;
1578 sav
->sav_count
= (int)nl2cache
;
1581 * Recompute the stashed list of l2cache devices, with status
1582 * information this time.
1584 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1585 DATA_TYPE_NVLIST_ARRAY
) == 0);
1587 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1588 for (i
= 0; i
< sav
->sav_count
; i
++)
1589 l2cache
[i
] = vdev_config_generate(spa
,
1590 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1591 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1592 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1594 for (i
= 0; i
< sav
->sav_count
; i
++)
1595 nvlist_free(l2cache
[i
]);
1597 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1601 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1604 char *packed
= NULL
;
1609 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1613 nvsize
= *(uint64_t *)db
->db_data
;
1614 dmu_buf_rele(db
, FTAG
);
1616 packed
= kmem_alloc(nvsize
, KM_SLEEP
);
1617 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1620 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1621 kmem_free(packed
, nvsize
);
1627 * Checks to see if the given vdev could not be opened, in which case we post a
1628 * sysevent to notify the autoreplace code that the device has been removed.
1631 spa_check_removed(vdev_t
*vd
)
1633 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1634 spa_check_removed(vd
->vdev_child
[c
]);
1636 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1637 vdev_is_concrete(vd
)) {
1638 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1639 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1644 spa_config_valid_zaps(vdev_t
*vd
, vdev_t
*mvd
)
1646 ASSERT3U(vd
->vdev_children
, ==, mvd
->vdev_children
);
1648 vd
->vdev_top_zap
= mvd
->vdev_top_zap
;
1649 vd
->vdev_leaf_zap
= mvd
->vdev_leaf_zap
;
1651 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
1652 spa_config_valid_zaps(vd
->vdev_child
[i
], mvd
->vdev_child
[i
]);
1657 * Validate the current config against the MOS config
1660 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1662 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1665 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1667 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1668 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1670 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1673 * If we're doing a normal import, then build up any additional
1674 * diagnostic information about missing devices in this config.
1675 * We'll pass this up to the user for further processing.
1677 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1678 nvlist_t
**child
, *nv
;
1681 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1683 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1685 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1686 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1687 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1689 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1690 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1692 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1697 VERIFY(nvlist_add_nvlist_array(nv
,
1698 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1699 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1700 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1702 for (int i
= 0; i
< idx
; i
++)
1703 nvlist_free(child
[i
]);
1706 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1710 * Compare the root vdev tree with the information we have
1711 * from the MOS config (mrvd). Check each top-level vdev
1712 * with the corresponding MOS config top-level (mtvd).
1714 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1715 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1716 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1719 * Resolve any "missing" vdevs in the current configuration.
1720 * Also trust the MOS config about any "indirect" vdevs.
1721 * If we find that the MOS config has more accurate information
1722 * about the top-level vdev then use that vdev instead.
1724 if ((tvd
->vdev_ops
== &vdev_missing_ops
&&
1725 mtvd
->vdev_ops
!= &vdev_missing_ops
) ||
1726 (mtvd
->vdev_ops
== &vdev_indirect_ops
&&
1727 tvd
->vdev_ops
!= &vdev_indirect_ops
)) {
1730 * Device specific actions.
1732 if (mtvd
->vdev_islog
) {
1733 if (!(spa
->spa_import_flags
&
1734 ZFS_IMPORT_MISSING_LOG
)) {
1738 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1739 } else if (mtvd
->vdev_ops
!= &vdev_indirect_ops
) {
1744 * Swap the missing vdev with the data we were
1745 * able to obtain from the MOS config.
1747 vdev_remove_child(rvd
, tvd
);
1748 vdev_remove_child(mrvd
, mtvd
);
1750 vdev_add_child(rvd
, mtvd
);
1751 vdev_add_child(mrvd
, tvd
);
1755 if (mtvd
->vdev_islog
) {
1757 * Load the slog device's state from the MOS
1758 * config since it's possible that the label
1759 * does not contain the most up-to-date
1762 vdev_load_log_state(tvd
, mtvd
);
1767 * Per-vdev ZAP info is stored exclusively in the MOS.
1769 spa_config_valid_zaps(tvd
, mtvd
);
1773 * Never trust this info from userland; always use what's
1774 * in the MOS. This prevents it from getting out of sync
1775 * with the rest of the info in the MOS.
1777 tvd
->vdev_removing
= mtvd
->vdev_removing
;
1778 tvd
->vdev_indirect_config
= mtvd
->vdev_indirect_config
;
1782 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1785 * Ensure we were able to validate the config.
1787 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1791 * Check for missing log devices
1794 spa_check_logs(spa_t
*spa
)
1796 boolean_t rv
= B_FALSE
;
1797 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1799 switch (spa
->spa_log_state
) {
1800 case SPA_LOG_MISSING
:
1801 /* need to recheck in case slog has been restored */
1802 case SPA_LOG_UNKNOWN
:
1803 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1804 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1806 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1813 spa_passivate_log(spa_t
*spa
)
1815 vdev_t
*rvd
= spa
->spa_root_vdev
;
1816 boolean_t slog_found
= B_FALSE
;
1818 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1820 if (!spa_has_slogs(spa
))
1823 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1824 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1825 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1827 if (tvd
->vdev_islog
) {
1828 metaslab_group_passivate(mg
);
1829 slog_found
= B_TRUE
;
1833 return (slog_found
);
1837 spa_activate_log(spa_t
*spa
)
1839 vdev_t
*rvd
= spa
->spa_root_vdev
;
1841 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1843 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1844 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1845 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1847 if (tvd
->vdev_islog
)
1848 metaslab_group_activate(mg
);
1853 spa_reset_logs(spa_t
*spa
)
1857 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
1858 NULL
, DS_FIND_CHILDREN
);
1861 * We successfully offlined the log device, sync out the
1862 * current txg so that the "stubby" block can be removed
1865 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1871 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1873 for (int i
= 0; i
< sav
->sav_count
; i
++)
1874 spa_check_removed(sav
->sav_vdevs
[i
]);
1878 spa_claim_notify(zio_t
*zio
)
1880 spa_t
*spa
= zio
->io_spa
;
1885 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1886 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1887 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1888 mutex_exit(&spa
->spa_props_lock
);
1891 typedef struct spa_load_error
{
1892 uint64_t sle_meta_count
;
1893 uint64_t sle_data_count
;
1897 spa_load_verify_done(zio_t
*zio
)
1899 blkptr_t
*bp
= zio
->io_bp
;
1900 spa_load_error_t
*sle
= zio
->io_private
;
1901 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1902 int error
= zio
->io_error
;
1903 spa_t
*spa
= zio
->io_spa
;
1905 abd_free(zio
->io_abd
);
1907 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1908 type
!= DMU_OT_INTENT_LOG
)
1909 atomic_inc_64(&sle
->sle_meta_count
);
1911 atomic_inc_64(&sle
->sle_data_count
);
1914 mutex_enter(&spa
->spa_scrub_lock
);
1915 spa
->spa_scrub_inflight
--;
1916 cv_broadcast(&spa
->spa_scrub_io_cv
);
1917 mutex_exit(&spa
->spa_scrub_lock
);
1921 * Maximum number of concurrent scrub i/os to create while verifying
1922 * a pool while importing it.
1924 int spa_load_verify_maxinflight
= 10000;
1925 boolean_t spa_load_verify_metadata
= B_TRUE
;
1926 boolean_t spa_load_verify_data
= B_TRUE
;
1930 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1931 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1933 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
1936 * Note: normally this routine will not be called if
1937 * spa_load_verify_metadata is not set. However, it may be useful
1938 * to manually set the flag after the traversal has begun.
1940 if (!spa_load_verify_metadata
)
1942 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
1946 size_t size
= BP_GET_PSIZE(bp
);
1948 mutex_enter(&spa
->spa_scrub_lock
);
1949 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
1950 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1951 spa
->spa_scrub_inflight
++;
1952 mutex_exit(&spa
->spa_scrub_lock
);
1954 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
1955 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1956 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1957 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1963 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
1965 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
1966 return (SET_ERROR(ENAMETOOLONG
));
1972 spa_load_verify(spa_t
*spa
)
1975 spa_load_error_t sle
= { 0 };
1976 zpool_rewind_policy_t policy
;
1977 boolean_t verify_ok
= B_FALSE
;
1980 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1982 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1985 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
1986 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
1987 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
1989 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
1993 rio
= zio_root(spa
, NULL
, &sle
,
1994 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1996 if (spa_load_verify_metadata
) {
1997 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1998 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
1999 spa_load_verify_cb
, rio
);
2002 (void) zio_wait(rio
);
2004 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2005 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2007 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
2008 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
2012 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2013 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2015 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2016 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2017 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2018 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2019 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2020 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2021 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2023 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2027 if (error
!= ENXIO
&& error
!= EIO
)
2028 error
= SET_ERROR(EIO
);
2032 return (verify_ok
? 0 : EIO
);
2036 * Find a value in the pool props object.
2039 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2041 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2042 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2046 * Find a value in the pool directory object.
2049 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2051 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2052 name
, sizeof (uint64_t), 1, val
));
2056 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2058 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2059 return (SET_ERROR(err
));
2063 * Fix up config after a partly-completed split. This is done with the
2064 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2065 * pool have that entry in their config, but only the splitting one contains
2066 * a list of all the guids of the vdevs that are being split off.
2068 * This function determines what to do with that list: either rejoin
2069 * all the disks to the pool, or complete the splitting process. To attempt
2070 * the rejoin, each disk that is offlined is marked online again, and
2071 * we do a reopen() call. If the vdev label for every disk that was
2072 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2073 * then we call vdev_split() on each disk, and complete the split.
2075 * Otherwise we leave the config alone, with all the vdevs in place in
2076 * the original pool.
2079 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2086 boolean_t attempt_reopen
;
2088 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2091 /* check that the config is complete */
2092 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2093 &glist
, &gcount
) != 0)
2096 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2098 /* attempt to online all the vdevs & validate */
2099 attempt_reopen
= B_TRUE
;
2100 for (i
= 0; i
< gcount
; i
++) {
2101 if (glist
[i
] == 0) /* vdev is hole */
2104 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2105 if (vd
[i
] == NULL
) {
2107 * Don't bother attempting to reopen the disks;
2108 * just do the split.
2110 attempt_reopen
= B_FALSE
;
2112 /* attempt to re-online it */
2113 vd
[i
]->vdev_offline
= B_FALSE
;
2117 if (attempt_reopen
) {
2118 vdev_reopen(spa
->spa_root_vdev
);
2120 /* check each device to see what state it's in */
2121 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2122 if (vd
[i
] != NULL
&&
2123 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2130 * If every disk has been moved to the new pool, or if we never
2131 * even attempted to look at them, then we split them off for
2134 if (!attempt_reopen
|| gcount
== extracted
) {
2135 for (i
= 0; i
< gcount
; i
++)
2138 vdev_reopen(spa
->spa_root_vdev
);
2141 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2145 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2146 boolean_t mosconfig
)
2148 nvlist_t
*config
= spa
->spa_config
;
2149 char *ereport
= FM_EREPORT_ZFS_POOL
;
2155 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2156 return (SET_ERROR(EINVAL
));
2158 ASSERT(spa
->spa_comment
== NULL
);
2159 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2160 spa
->spa_comment
= spa_strdup(comment
);
2163 * Versioning wasn't explicitly added to the label until later, so if
2164 * it's not present treat it as the initial version.
2166 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2167 &spa
->spa_ubsync
.ub_version
) != 0)
2168 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2170 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2171 &spa
->spa_config_txg
);
2173 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2174 spa_guid_exists(pool_guid
, 0)) {
2175 error
= SET_ERROR(EEXIST
);
2177 spa
->spa_config_guid
= pool_guid
;
2179 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2181 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2185 nvlist_free(spa
->spa_load_info
);
2186 spa
->spa_load_info
= fnvlist_alloc();
2188 gethrestime(&spa
->spa_loaded_ts
);
2189 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2190 mosconfig
, &ereport
);
2194 * Don't count references from objsets that are already closed
2195 * and are making their way through the eviction process.
2197 spa_evicting_os_wait(spa
);
2198 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2200 if (error
!= EEXIST
) {
2201 spa
->spa_loaded_ts
.tv_sec
= 0;
2202 spa
->spa_loaded_ts
.tv_nsec
= 0;
2204 if (error
!= EBADF
) {
2205 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2208 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2215 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2216 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2217 * spa's per-vdev ZAP list.
2220 vdev_count_verify_zaps(vdev_t
*vd
)
2222 spa_t
*spa
= vd
->vdev_spa
;
2224 if (vd
->vdev_top_zap
!= 0) {
2226 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2227 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2229 if (vd
->vdev_leaf_zap
!= 0) {
2231 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2232 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2235 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2236 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2243 * Load an existing storage pool, using the pool's builtin spa_config as a
2244 * source of configuration information.
2247 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2248 spa_load_state_t state
, spa_import_type_t type
, boolean_t trust_config
,
2252 nvlist_t
*nvroot
= NULL
;
2255 uberblock_t
*ub
= &spa
->spa_uberblock
;
2256 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2257 int orig_mode
= spa
->spa_mode
;
2260 boolean_t missing_feat_write
= B_FALSE
;
2263 * If this is an untrusted config, access the pool in read-only mode.
2264 * This prevents things like resilvering recently removed devices.
2267 spa
->spa_mode
= FREAD
;
2269 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2271 spa
->spa_load_state
= state
;
2273 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2274 return (SET_ERROR(EINVAL
));
2276 parse
= (type
== SPA_IMPORT_EXISTING
?
2277 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2280 * Create "The Godfather" zio to hold all async IOs
2282 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2284 for (int i
= 0; i
< max_ncpus
; i
++) {
2285 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2286 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2287 ZIO_FLAG_GODFATHER
);
2291 * Parse the configuration into a vdev tree. We explicitly set the
2292 * value that will be returned by spa_version() since parsing the
2293 * configuration requires knowing the version number.
2295 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2296 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2297 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2302 ASSERT(spa
->spa_root_vdev
== rvd
);
2303 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2304 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2306 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2307 ASSERT(spa_guid(spa
) == pool_guid
);
2311 * Try to open all vdevs, loading each label in the process.
2313 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2314 error
= vdev_open(rvd
);
2315 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2320 * We need to validate the vdev labels against the configuration that
2321 * we have in hand, which is dependent on the setting of mosconfig. If
2322 * mosconfig is true then we're validating the vdev labels based on
2323 * that config. Otherwise, we're validating against the cached config
2324 * (zpool.cache) that was read when we loaded the zfs module, and then
2325 * later we will recursively call spa_load() and validate against
2328 * If we're assembling a new pool that's been split off from an
2329 * existing pool, the labels haven't yet been updated so we skip
2330 * validation for now.
2332 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2333 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2334 error
= vdev_validate(rvd
, trust_config
);
2335 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2340 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2341 return (SET_ERROR(ENXIO
));
2345 * Find the best uberblock.
2347 vdev_uberblock_load(rvd
, ub
, &label
);
2350 * If we weren't able to find a single valid uberblock, return failure.
2352 if (ub
->ub_txg
== 0) {
2354 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2358 * If the pool has an unsupported version we can't open it.
2360 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2362 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2365 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2369 * If we weren't able to find what's necessary for reading the
2370 * MOS in the label, return failure.
2372 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2373 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2375 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2380 * Update our in-core representation with the definitive values
2383 nvlist_free(spa
->spa_label_features
);
2384 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2390 * Look through entries in the label nvlist's features_for_read. If
2391 * there is a feature listed there which we don't understand then we
2392 * cannot open a pool.
2394 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2395 nvlist_t
*unsup_feat
;
2397 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2400 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
2402 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2403 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2404 VERIFY(nvlist_add_string(unsup_feat
,
2405 nvpair_name(nvp
), "") == 0);
2409 if (!nvlist_empty(unsup_feat
)) {
2410 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2411 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2412 nvlist_free(unsup_feat
);
2413 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2417 nvlist_free(unsup_feat
);
2421 * If the vdev guid sum doesn't match the uberblock, we have an
2422 * incomplete configuration. We first check to see if the pool
2423 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2424 * If it is, defer the vdev_guid_sum check till later so we
2425 * can handle missing vdevs.
2427 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2428 &children
) != 0 && trust_config
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2429 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2430 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2432 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2433 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2434 spa_try_repair(spa
, config
);
2435 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2436 nvlist_free(spa
->spa_config_splitting
);
2437 spa
->spa_config_splitting
= NULL
;
2441 * Initialize internal SPA structures.
2443 spa
->spa_state
= POOL_STATE_ACTIVE
;
2444 spa
->spa_ubsync
= spa
->spa_uberblock
;
2445 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2446 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2447 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2448 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2449 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2450 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2453 * Everything that we read before we do spa_remove_init() must
2454 * have been rewritten after the last device removal was initiated.
2455 * Otherwise we could be reading from indirect vdevs before
2456 * we have loaded their mappings.
2459 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2461 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2462 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2464 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2465 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2468 * Validate the config, using the MOS config to fill in any
2469 * information which might be missing. If we fail to validate
2470 * the config then declare the pool unfit for use. If we're
2471 * assembling a pool from a split, the log is not transferred
2474 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2475 nvlist_t
*mos_config
;
2476 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2477 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2479 if (!spa_config_valid(spa
, mos_config
)) {
2480 nvlist_free(mos_config
);
2481 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2484 nvlist_free(mos_config
);
2487 * Now that we've validated the config, check the state of the
2488 * root vdev. If it can't be opened, it indicates one or
2489 * more toplevel vdevs are faulted.
2491 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2492 return (SET_ERROR(ENXIO
));
2496 * Everything that we read before spa_remove_init() must be stored
2497 * on concreted vdevs. Therefore we do this as early as possible.
2499 if (spa_remove_init(spa
) != 0)
2500 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2502 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2503 boolean_t missing_feat_read
= B_FALSE
;
2504 nvlist_t
*unsup_feat
, *enabled_feat
;
2506 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2507 &spa
->spa_feat_for_read_obj
) != 0) {
2508 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2511 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2512 &spa
->spa_feat_for_write_obj
) != 0) {
2513 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2516 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2517 &spa
->spa_feat_desc_obj
) != 0) {
2518 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2521 enabled_feat
= fnvlist_alloc();
2522 unsup_feat
= fnvlist_alloc();
2524 if (!spa_features_check(spa
, B_FALSE
,
2525 unsup_feat
, enabled_feat
))
2526 missing_feat_read
= B_TRUE
;
2528 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2529 if (!spa_features_check(spa
, B_TRUE
,
2530 unsup_feat
, enabled_feat
)) {
2531 missing_feat_write
= B_TRUE
;
2535 fnvlist_add_nvlist(spa
->spa_load_info
,
2536 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2538 if (!nvlist_empty(unsup_feat
)) {
2539 fnvlist_add_nvlist(spa
->spa_load_info
,
2540 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2543 fnvlist_free(enabled_feat
);
2544 fnvlist_free(unsup_feat
);
2546 if (!missing_feat_read
) {
2547 fnvlist_add_boolean(spa
->spa_load_info
,
2548 ZPOOL_CONFIG_CAN_RDONLY
);
2552 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2553 * twofold: to determine whether the pool is available for
2554 * import in read-write mode and (if it is not) whether the
2555 * pool is available for import in read-only mode. If the pool
2556 * is available for import in read-write mode, it is displayed
2557 * as available in userland; if it is not available for import
2558 * in read-only mode, it is displayed as unavailable in
2559 * userland. If the pool is available for import in read-only
2560 * mode but not read-write mode, it is displayed as unavailable
2561 * in userland with a special note that the pool is actually
2562 * available for open in read-only mode.
2564 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2565 * missing a feature for write, we must first determine whether
2566 * the pool can be opened read-only before returning to
2567 * userland in order to know whether to display the
2568 * abovementioned note.
2570 if (missing_feat_read
|| (missing_feat_write
&&
2571 spa_writeable(spa
))) {
2572 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2577 * Load refcounts for ZFS features from disk into an in-memory
2578 * cache during SPA initialization.
2580 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
2583 error
= feature_get_refcount_from_disk(spa
,
2584 &spa_feature_table
[i
], &refcount
);
2586 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2587 } else if (error
== ENOTSUP
) {
2588 spa
->spa_feat_refcount_cache
[i
] =
2589 SPA_FEATURE_DISABLED
;
2591 return (spa_vdev_err(rvd
,
2592 VDEV_AUX_CORRUPT_DATA
, EIO
));
2597 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2598 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2599 &spa
->spa_feat_enabled_txg_obj
) != 0)
2600 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2603 spa
->spa_is_initializing
= B_TRUE
;
2604 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2605 spa
->spa_is_initializing
= B_FALSE
;
2607 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2609 if (!trust_config
) {
2611 nvlist_t
*policy
= NULL
;
2612 nvlist_t
*mos_config
;
2614 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2615 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2617 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
2618 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2620 unsigned long myhostid
= 0;
2622 VERIFY(nvlist_lookup_string(mos_config
,
2623 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2626 myhostid
= zone_get_hostid(NULL
);
2629 * We're emulating the system's hostid in userland, so
2630 * we can't use zone_get_hostid().
2632 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2633 #endif /* _KERNEL */
2634 if (hostid
!= 0 && myhostid
!= 0 &&
2635 hostid
!= myhostid
) {
2636 nvlist_free(mos_config
);
2637 cmn_err(CE_WARN
, "pool '%s' could not be "
2638 "loaded as it was last accessed by "
2639 "another system (host: %s hostid: 0x%lx). "
2640 "See: http://illumos.org/msg/ZFS-8000-EY",
2641 spa_name(spa
), hostname
,
2642 (unsigned long)hostid
);
2643 return (SET_ERROR(EBADF
));
2646 if (nvlist_lookup_nvlist(spa
->spa_config
,
2647 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2648 VERIFY(nvlist_add_nvlist(mos_config
,
2649 ZPOOL_REWIND_POLICY
, policy
) == 0);
2651 spa_config_set(spa
, mos_config
);
2653 spa_deactivate(spa
);
2654 spa_activate(spa
, orig_mode
);
2656 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2659 /* Grab the secret checksum salt from the MOS. */
2660 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2661 DMU_POOL_CHECKSUM_SALT
, 1,
2662 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
2663 spa
->spa_cksum_salt
.zcs_bytes
);
2664 if (error
== ENOENT
) {
2665 /* Generate a new salt for subsequent use */
2666 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
2667 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
2668 } else if (error
!= 0) {
2669 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2672 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2673 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2674 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2676 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2679 * Load the bit that tells us to use the new accounting function
2680 * (raid-z deflation). If we have an older pool, this will not
2683 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2684 if (error
!= 0 && error
!= ENOENT
)
2685 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2687 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2688 &spa
->spa_creation_version
);
2689 if (error
!= 0 && error
!= ENOENT
)
2690 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2693 * Load the persistent error log. If we have an older pool, this will
2696 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2697 if (error
!= 0 && error
!= ENOENT
)
2698 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2700 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2701 &spa
->spa_errlog_scrub
);
2702 if (error
!= 0 && error
!= ENOENT
)
2703 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2706 * Load the history object. If we have an older pool, this
2707 * will not be present.
2709 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2710 if (error
!= 0 && error
!= ENOENT
)
2711 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2714 * Load the per-vdev ZAP map. If we have an older pool, this will not
2715 * be present; in this case, defer its creation to a later time to
2716 * avoid dirtying the MOS this early / out of sync context. See
2717 * spa_sync_config_object.
2720 /* The sentinel is only available in the MOS config. */
2721 nvlist_t
*mos_config
;
2722 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2723 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2725 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
2726 &spa
->spa_all_vdev_zaps
);
2728 if (error
== ENOENT
) {
2729 VERIFY(!nvlist_exists(mos_config
,
2730 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
2731 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
2732 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2733 } else if (error
!= 0) {
2734 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2735 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
2737 * An older version of ZFS overwrote the sentinel value, so
2738 * we have orphaned per-vdev ZAPs in the MOS. Defer their
2739 * destruction to later; see spa_sync_config_object.
2741 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
2743 * We're assuming that no vdevs have had their ZAPs created
2744 * before this. Better be sure of it.
2746 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2748 nvlist_free(mos_config
);
2751 * If we're assembling the pool from the split-off vdevs of
2752 * an existing pool, we don't want to attach the spares & cache
2757 * Load any hot spares for this pool.
2759 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2760 if (error
!= 0 && error
!= ENOENT
)
2761 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2762 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2763 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2764 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2765 &spa
->spa_spares
.sav_config
) != 0)
2766 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2768 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2769 spa_load_spares(spa
);
2770 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2771 } else if (error
== 0) {
2772 spa
->spa_spares
.sav_sync
= B_TRUE
;
2776 * Load any level 2 ARC devices for this pool.
2778 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2779 &spa
->spa_l2cache
.sav_object
);
2780 if (error
!= 0 && error
!= ENOENT
)
2781 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2782 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2783 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2784 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2785 &spa
->spa_l2cache
.sav_config
) != 0)
2786 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2788 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2789 spa_load_l2cache(spa
);
2790 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2791 } else if (error
== 0) {
2792 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2795 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2797 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2798 if (error
&& error
!= ENOENT
)
2799 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2802 uint64_t autoreplace
;
2804 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2805 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2806 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2807 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2808 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2809 spa_prop_find(spa
, ZPOOL_PROP_BOOTSIZE
, &spa
->spa_bootsize
);
2810 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2811 &spa
->spa_dedup_ditto
);
2813 spa
->spa_autoreplace
= (autoreplace
!= 0);
2817 * If the 'autoreplace' property is set, then post a resource notifying
2818 * the ZFS DE that it should not issue any faults for unopenable
2819 * devices. We also iterate over the vdevs, and post a sysevent for any
2820 * unopenable vdevs so that the normal autoreplace handler can take
2823 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2824 spa_check_removed(spa
->spa_root_vdev
);
2826 * For the import case, this is done in spa_import(), because
2827 * at this point we're using the spare definitions from
2828 * the MOS config, not necessarily from the userland config.
2830 if (state
!= SPA_LOAD_IMPORT
) {
2831 spa_aux_check_removed(&spa
->spa_spares
);
2832 spa_aux_check_removed(&spa
->spa_l2cache
);
2837 * Load the vdev state for all toplevel vdevs.
2839 error
= vdev_load(rvd
);
2841 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
2844 error
= spa_condense_init(spa
);
2846 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
2850 * Propagate the leaf DTLs we just loaded all the way up the tree.
2852 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2853 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2854 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2857 * Load the DDTs (dedup tables).
2859 error
= ddt_load(spa
);
2861 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2863 spa_update_dspace(spa
);
2865 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
) &&
2866 spa_check_logs(spa
)) {
2867 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2868 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2871 if (missing_feat_write
) {
2872 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2875 * At this point, we know that we can open the pool in
2876 * read-only mode but not read-write mode. We now have enough
2877 * information and can return to userland.
2879 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2883 * We've successfully opened the pool, verify that we're ready
2884 * to start pushing transactions.
2886 if (state
!= SPA_LOAD_TRYIMPORT
) {
2887 if (error
= spa_load_verify(spa
))
2888 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2892 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2893 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2895 int need_update
= B_FALSE
;
2896 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2899 * We must check this before we start the sync thread, because
2900 * we only want to start a condense thread for condense
2901 * operations that were in progress when the pool was
2902 * imported. Once we start syncing, spa_sync() could
2903 * initiate a condense (and start a thread for it). In
2904 * that case it would be wrong to start a second
2907 boolean_t condense_in_progress
=
2908 (spa
->spa_condensing_indirect
!= NULL
);
2910 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2913 * Claim log blocks that haven't been committed yet.
2914 * This must all happen in a single txg.
2915 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2916 * invoked from zil_claim_log_block()'s i/o done callback.
2917 * Price of rollback is that we abandon the log.
2919 spa
->spa_claiming
= B_TRUE
;
2921 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
2922 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2923 zil_claim
, tx
, DS_FIND_CHILDREN
);
2926 spa
->spa_claiming
= B_FALSE
;
2928 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2929 spa
->spa_sync_on
= B_TRUE
;
2930 txg_sync_start(spa
->spa_dsl_pool
);
2933 * Wait for all claims to sync. We sync up to the highest
2934 * claimed log block birth time so that claimed log blocks
2935 * don't appear to be from the future. spa_claim_max_txg
2936 * will have been set for us by either zil_check_log_chain()
2937 * (invoked from spa_check_logs()) or zil_claim() above.
2939 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2942 * If the config cache is stale, or we have uninitialized
2943 * metaslabs (see spa_vdev_add()), then update the config.
2945 * If this is a verbatim import, trust the current
2946 * in-core spa_config and update the disk labels.
2948 if (config_cache_txg
!= spa
->spa_config_txg
||
2949 state
== SPA_LOAD_IMPORT
||
2950 state
== SPA_LOAD_RECOVER
||
2951 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2952 need_update
= B_TRUE
;
2954 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
2955 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2956 need_update
= B_TRUE
;
2959 * Update the config cache asychronously in case we're the
2960 * root pool, in which case the config cache isn't writable yet.
2963 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2966 * Check all DTLs to see if anything needs resilvering.
2968 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2969 vdev_resilver_needed(rvd
, NULL
, NULL
))
2970 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2973 * Log the fact that we booted up (so that we can detect if
2974 * we rebooted in the middle of an operation).
2976 spa_history_log_version(spa
, "open");
2979 * Delete any inconsistent datasets.
2981 (void) dmu_objset_find(spa_name(spa
),
2982 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2985 * Clean up any stale temporary dataset userrefs.
2987 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2990 * Note: unlike condensing, we don't need an analogous
2991 * "removal_in_progress" dance because no other thread
2992 * can start a removal while we hold the spa_namespace_lock.
2994 spa_restart_removal(spa
);
2996 if (condense_in_progress
)
2997 spa_condense_indirect_restart(spa
);
3004 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
3006 int mode
= spa
->spa_mode
;
3009 spa_deactivate(spa
);
3011 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
3013 spa_activate(spa
, mode
);
3014 spa_async_suspend(spa
);
3016 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
3020 * If spa_load() fails this function will try loading prior txg's. If
3021 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3022 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3023 * function will not rewind the pool and will return the same error as
3027 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
3028 uint64_t max_request
, int rewind_flags
)
3030 nvlist_t
*loadinfo
= NULL
;
3031 nvlist_t
*config
= NULL
;
3032 int load_error
, rewind_error
;
3033 uint64_t safe_rewind_txg
;
3036 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
3037 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
3038 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3040 spa
->spa_load_max_txg
= max_request
;
3041 if (max_request
!= UINT64_MAX
)
3042 spa
->spa_extreme_rewind
= B_TRUE
;
3045 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
3047 if (load_error
== 0)
3050 if (spa
->spa_root_vdev
!= NULL
)
3051 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3053 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
3054 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
3056 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
3057 nvlist_free(config
);
3058 return (load_error
);
3061 if (state
== SPA_LOAD_RECOVER
) {
3062 /* Price of rolling back is discarding txgs, including log */
3063 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3066 * If we aren't rolling back save the load info from our first
3067 * import attempt so that we can restore it after attempting
3070 loadinfo
= spa
->spa_load_info
;
3071 spa
->spa_load_info
= fnvlist_alloc();
3074 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
3075 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
3076 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
3077 TXG_INITIAL
: safe_rewind_txg
;
3080 * Continue as long as we're finding errors, we're still within
3081 * the acceptable rewind range, and we're still finding uberblocks
3083 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
3084 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
3085 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
3086 spa
->spa_extreme_rewind
= B_TRUE
;
3087 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
3090 spa
->spa_extreme_rewind
= B_FALSE
;
3091 spa
->spa_load_max_txg
= UINT64_MAX
;
3093 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
3094 spa_config_set(spa
, config
);
3096 nvlist_free(config
);
3098 if (state
== SPA_LOAD_RECOVER
) {
3099 ASSERT3P(loadinfo
, ==, NULL
);
3100 return (rewind_error
);
3102 /* Store the rewind info as part of the initial load info */
3103 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
3104 spa
->spa_load_info
);
3106 /* Restore the initial load info */
3107 fnvlist_free(spa
->spa_load_info
);
3108 spa
->spa_load_info
= loadinfo
;
3110 return (load_error
);
3117 * The import case is identical to an open except that the configuration is sent
3118 * down from userland, instead of grabbed from the configuration cache. For the
3119 * case of an open, the pool configuration will exist in the
3120 * POOL_STATE_UNINITIALIZED state.
3122 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3123 * the same time open the pool, without having to keep around the spa_t in some
3127 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
3131 spa_load_state_t state
= SPA_LOAD_OPEN
;
3133 int locked
= B_FALSE
;
3138 * As disgusting as this is, we need to support recursive calls to this
3139 * function because dsl_dir_open() is called during spa_load(), and ends
3140 * up calling spa_open() again. The real fix is to figure out how to
3141 * avoid dsl_dir_open() calling this in the first place.
3143 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
3144 mutex_enter(&spa_namespace_lock
);
3148 if ((spa
= spa_lookup(pool
)) == NULL
) {
3150 mutex_exit(&spa_namespace_lock
);
3151 return (SET_ERROR(ENOENT
));
3154 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3155 zpool_rewind_policy_t policy
;
3157 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3159 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3160 state
= SPA_LOAD_RECOVER
;
3162 spa_activate(spa
, spa_mode_global
);
3164 if (state
!= SPA_LOAD_RECOVER
)
3165 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3167 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3168 policy
.zrp_request
);
3170 if (error
== EBADF
) {
3172 * If vdev_validate() returns failure (indicated by
3173 * EBADF), it indicates that one of the vdevs indicates
3174 * that the pool has been exported or destroyed. If
3175 * this is the case, the config cache is out of sync and
3176 * we should remove the pool from the namespace.
3179 spa_deactivate(spa
);
3180 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
3183 mutex_exit(&spa_namespace_lock
);
3184 return (SET_ERROR(ENOENT
));
3189 * We can't open the pool, but we still have useful
3190 * information: the state of each vdev after the
3191 * attempted vdev_open(). Return this to the user.
3193 if (config
!= NULL
&& spa
->spa_config
) {
3194 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3196 VERIFY(nvlist_add_nvlist(*config
,
3197 ZPOOL_CONFIG_LOAD_INFO
,
3198 spa
->spa_load_info
) == 0);
3201 spa_deactivate(spa
);
3202 spa
->spa_last_open_failed
= error
;
3204 mutex_exit(&spa_namespace_lock
);
3210 spa_open_ref(spa
, tag
);
3213 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3216 * If we've recovered the pool, pass back any information we
3217 * gathered while doing the load.
3219 if (state
== SPA_LOAD_RECOVER
) {
3220 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3221 spa
->spa_load_info
) == 0);
3225 spa
->spa_last_open_failed
= 0;
3226 spa
->spa_last_ubsync_txg
= 0;
3227 spa
->spa_load_txg
= 0;
3228 mutex_exit(&spa_namespace_lock
);
3237 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3240 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3244 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3246 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3250 * Lookup the given spa_t, incrementing the inject count in the process,
3251 * preventing it from being exported or destroyed.
3254 spa_inject_addref(char *name
)
3258 mutex_enter(&spa_namespace_lock
);
3259 if ((spa
= spa_lookup(name
)) == NULL
) {
3260 mutex_exit(&spa_namespace_lock
);
3263 spa
->spa_inject_ref
++;
3264 mutex_exit(&spa_namespace_lock
);
3270 spa_inject_delref(spa_t
*spa
)
3272 mutex_enter(&spa_namespace_lock
);
3273 spa
->spa_inject_ref
--;
3274 mutex_exit(&spa_namespace_lock
);
3278 * Add spares device information to the nvlist.
3281 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3291 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3293 if (spa
->spa_spares
.sav_count
== 0)
3296 VERIFY(nvlist_lookup_nvlist(config
,
3297 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3298 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3299 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3301 VERIFY(nvlist_add_nvlist_array(nvroot
,
3302 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3303 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3304 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3307 * Go through and find any spares which have since been
3308 * repurposed as an active spare. If this is the case, update
3309 * their status appropriately.
3311 for (i
= 0; i
< nspares
; i
++) {
3312 VERIFY(nvlist_lookup_uint64(spares
[i
],
3313 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3314 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3316 VERIFY(nvlist_lookup_uint64_array(
3317 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3318 (uint64_t **)&vs
, &vsc
) == 0);
3319 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3320 vs
->vs_aux
= VDEV_AUX_SPARED
;
3327 * Add l2cache device information to the nvlist, including vdev stats.
3330 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3333 uint_t i
, j
, nl2cache
;
3340 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3342 if (spa
->spa_l2cache
.sav_count
== 0)
3345 VERIFY(nvlist_lookup_nvlist(config
,
3346 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3347 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3348 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3349 if (nl2cache
!= 0) {
3350 VERIFY(nvlist_add_nvlist_array(nvroot
,
3351 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3352 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3353 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3356 * Update level 2 cache device stats.
3359 for (i
= 0; i
< nl2cache
; i
++) {
3360 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3361 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3364 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3366 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3367 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3373 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3374 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3376 vdev_get_stats(vd
, vs
);
3382 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3388 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3389 VERIFY(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3391 if (spa
->spa_feat_for_read_obj
!= 0) {
3392 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3393 spa
->spa_feat_for_read_obj
);
3394 zap_cursor_retrieve(&zc
, &za
) == 0;
3395 zap_cursor_advance(&zc
)) {
3396 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3397 za
.za_num_integers
== 1);
3398 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3399 za
.za_first_integer
));
3401 zap_cursor_fini(&zc
);
3404 if (spa
->spa_feat_for_write_obj
!= 0) {
3405 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3406 spa
->spa_feat_for_write_obj
);
3407 zap_cursor_retrieve(&zc
, &za
) == 0;
3408 zap_cursor_advance(&zc
)) {
3409 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3410 za
.za_num_integers
== 1);
3411 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3412 za
.za_first_integer
));
3414 zap_cursor_fini(&zc
);
3417 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3419 nvlist_free(features
);
3423 spa_get_stats(const char *name
, nvlist_t
**config
,
3424 char *altroot
, size_t buflen
)
3430 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3434 * This still leaves a window of inconsistency where the spares
3435 * or l2cache devices could change and the config would be
3436 * self-inconsistent.
3438 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3440 if (*config
!= NULL
) {
3441 uint64_t loadtimes
[2];
3443 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3444 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3445 VERIFY(nvlist_add_uint64_array(*config
,
3446 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3448 VERIFY(nvlist_add_uint64(*config
,
3449 ZPOOL_CONFIG_ERRCOUNT
,
3450 spa_get_errlog_size(spa
)) == 0);
3452 if (spa_suspended(spa
))
3453 VERIFY(nvlist_add_uint64(*config
,
3454 ZPOOL_CONFIG_SUSPENDED
,
3455 spa
->spa_failmode
) == 0);
3457 spa_add_spares(spa
, *config
);
3458 spa_add_l2cache(spa
, *config
);
3459 spa_add_feature_stats(spa
, *config
);
3464 * We want to get the alternate root even for faulted pools, so we cheat
3465 * and call spa_lookup() directly.
3469 mutex_enter(&spa_namespace_lock
);
3470 spa
= spa_lookup(name
);
3472 spa_altroot(spa
, altroot
, buflen
);
3476 mutex_exit(&spa_namespace_lock
);
3478 spa_altroot(spa
, altroot
, buflen
);
3483 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3484 spa_close(spa
, FTAG
);
3491 * Validate that the auxiliary device array is well formed. We must have an
3492 * array of nvlists, each which describes a valid leaf vdev. If this is an
3493 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3494 * specified, as long as they are well-formed.
3497 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3498 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3499 vdev_labeltype_t label
)
3506 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3509 * It's acceptable to have no devs specified.
3511 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3515 return (SET_ERROR(EINVAL
));
3518 * Make sure the pool is formatted with a version that supports this
3521 if (spa_version(spa
) < version
)
3522 return (SET_ERROR(ENOTSUP
));
3525 * Set the pending device list so we correctly handle device in-use
3528 sav
->sav_pending
= dev
;
3529 sav
->sav_npending
= ndev
;
3531 for (i
= 0; i
< ndev
; i
++) {
3532 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3536 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3538 error
= SET_ERROR(EINVAL
);
3543 * The L2ARC currently only supports disk devices in
3544 * kernel context. For user-level testing, we allow it.
3547 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3548 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3549 error
= SET_ERROR(ENOTBLK
);
3556 if ((error
= vdev_open(vd
)) == 0 &&
3557 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3558 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3559 vd
->vdev_guid
) == 0);
3565 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3572 sav
->sav_pending
= NULL
;
3573 sav
->sav_npending
= 0;
3578 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3582 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3584 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3585 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3586 VDEV_LABEL_SPARE
)) != 0) {
3590 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3591 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3592 VDEV_LABEL_L2CACHE
));
3596 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3601 if (sav
->sav_config
!= NULL
) {
3607 * Generate new dev list by concatentating with the
3610 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3611 &olddevs
, &oldndevs
) == 0);
3613 newdevs
= kmem_alloc(sizeof (void *) *
3614 (ndevs
+ oldndevs
), KM_SLEEP
);
3615 for (i
= 0; i
< oldndevs
; i
++)
3616 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3618 for (i
= 0; i
< ndevs
; i
++)
3619 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3622 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3623 DATA_TYPE_NVLIST_ARRAY
) == 0);
3625 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3626 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3627 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3628 nvlist_free(newdevs
[i
]);
3629 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3632 * Generate a new dev list.
3634 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3636 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3642 * Stop and drop level 2 ARC devices
3645 spa_l2cache_drop(spa_t
*spa
)
3649 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3651 for (i
= 0; i
< sav
->sav_count
; i
++) {
3654 vd
= sav
->sav_vdevs
[i
];
3657 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3658 pool
!= 0ULL && l2arc_vdev_present(vd
))
3659 l2arc_remove_vdev(vd
);
3667 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3671 char *altroot
= NULL
;
3676 uint64_t txg
= TXG_INITIAL
;
3677 nvlist_t
**spares
, **l2cache
;
3678 uint_t nspares
, nl2cache
;
3679 uint64_t version
, obj
;
3680 boolean_t has_features
;
3683 * If this pool already exists, return failure.
3685 mutex_enter(&spa_namespace_lock
);
3686 if (spa_lookup(pool
) != NULL
) {
3687 mutex_exit(&spa_namespace_lock
);
3688 return (SET_ERROR(EEXIST
));
3692 * Allocate a new spa_t structure.
3694 (void) nvlist_lookup_string(props
,
3695 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3696 spa
= spa_add(pool
, NULL
, altroot
);
3697 spa_activate(spa
, spa_mode_global
);
3699 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3700 spa_deactivate(spa
);
3702 mutex_exit(&spa_namespace_lock
);
3706 has_features
= B_FALSE
;
3707 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
3708 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3709 if (zpool_prop_feature(nvpair_name(elem
)))
3710 has_features
= B_TRUE
;
3713 if (has_features
|| nvlist_lookup_uint64(props
,
3714 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3715 version
= SPA_VERSION
;
3717 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3719 spa
->spa_first_txg
= txg
;
3720 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3721 spa
->spa_uberblock
.ub_version
= version
;
3722 spa
->spa_ubsync
= spa
->spa_uberblock
;
3723 spa
->spa_load_state
= SPA_LOAD_CREATE
;
3724 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
3725 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
3726 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
3729 * Create "The Godfather" zio to hold all async IOs
3731 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3733 for (int i
= 0; i
< max_ncpus
; i
++) {
3734 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3735 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3736 ZIO_FLAG_GODFATHER
);
3740 * Create the root vdev.
3742 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3744 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3746 ASSERT(error
!= 0 || rvd
!= NULL
);
3747 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3749 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3750 error
= SET_ERROR(EINVAL
);
3753 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3754 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3755 VDEV_ALLOC_ADD
)) == 0) {
3756 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
3757 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3758 vdev_expand(rvd
->vdev_child
[c
], txg
);
3762 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3766 spa_deactivate(spa
);
3768 mutex_exit(&spa_namespace_lock
);
3773 * Get the list of spares, if specified.
3775 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3776 &spares
, &nspares
) == 0) {
3777 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3779 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3780 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3781 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3782 spa_load_spares(spa
);
3783 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3784 spa
->spa_spares
.sav_sync
= B_TRUE
;
3788 * Get the list of level 2 cache devices, if specified.
3790 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3791 &l2cache
, &nl2cache
) == 0) {
3792 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3793 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3794 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3795 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3796 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3797 spa_load_l2cache(spa
);
3798 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3799 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3802 spa
->spa_is_initializing
= B_TRUE
;
3803 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3804 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3805 spa
->spa_is_initializing
= B_FALSE
;
3808 * Create DDTs (dedup tables).
3812 spa_update_dspace(spa
);
3814 tx
= dmu_tx_create_assigned(dp
, txg
);
3817 * Create the pool config object.
3819 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3820 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3821 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3823 if (zap_add(spa
->spa_meta_objset
,
3824 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3825 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3826 cmn_err(CE_PANIC
, "failed to add pool config");
3829 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3830 spa_feature_create_zap_objects(spa
, tx
);
3832 if (zap_add(spa
->spa_meta_objset
,
3833 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3834 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3835 cmn_err(CE_PANIC
, "failed to add pool version");
3838 /* Newly created pools with the right version are always deflated. */
3839 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3840 spa
->spa_deflate
= TRUE
;
3841 if (zap_add(spa
->spa_meta_objset
,
3842 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3843 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3844 cmn_err(CE_PANIC
, "failed to add deflate");
3849 * Create the deferred-free bpobj. Turn off compression
3850 * because sync-to-convergence takes longer if the blocksize
3853 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3854 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3855 ZIO_COMPRESS_OFF
, tx
);
3856 if (zap_add(spa
->spa_meta_objset
,
3857 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3858 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3859 cmn_err(CE_PANIC
, "failed to add bpobj");
3861 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3862 spa
->spa_meta_objset
, obj
));
3865 * Create the pool's history object.
3867 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3868 spa_history_create_obj(spa
, tx
);
3871 * Generate some random noise for salted checksums to operate on.
3873 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3874 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3877 * Set pool properties.
3879 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3880 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3881 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3882 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3884 if (props
!= NULL
) {
3885 spa_configfile_set(spa
, props
, B_FALSE
);
3886 spa_sync_props(props
, tx
);
3891 spa
->spa_sync_on
= B_TRUE
;
3892 txg_sync_start(spa
->spa_dsl_pool
);
3895 * We explicitly wait for the first transaction to complete so that our
3896 * bean counters are appropriately updated.
3898 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3900 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
3901 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
3903 spa_history_log_version(spa
, "create");
3906 * Don't count references from objsets that are already closed
3907 * and are making their way through the eviction process.
3909 spa_evicting_os_wait(spa
);
3910 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3911 spa
->spa_load_state
= SPA_LOAD_NONE
;
3913 mutex_exit(&spa_namespace_lock
);
3920 * Get the root pool information from the root disk, then import the root pool
3921 * during the system boot up time.
3923 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3926 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3929 nvlist_t
*nvtop
, *nvroot
;
3932 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3936 * Add this top-level vdev to the child array.
3938 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3940 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3942 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3945 * Put this pool's top-level vdevs into a root vdev.
3947 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3948 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3949 VDEV_TYPE_ROOT
) == 0);
3950 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3951 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3952 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3956 * Replace the existing vdev_tree with the new root vdev in
3957 * this pool's configuration (remove the old, add the new).
3959 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3960 nvlist_free(nvroot
);
3965 * Walk the vdev tree and see if we can find a device with "better"
3966 * configuration. A configuration is "better" if the label on that
3967 * device has a more recent txg.
3970 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3972 for (int c
= 0; c
< vd
->vdev_children
; c
++)
3973 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3975 if (vd
->vdev_ops
->vdev_op_leaf
) {
3979 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3983 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3987 * Do we have a better boot device?
3989 if (label_txg
> *txg
) {
3998 * Import a root pool.
4000 * For x86. devpath_list will consist of devid and/or physpath name of
4001 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
4002 * The GRUB "findroot" command will return the vdev we should boot.
4004 * For Sparc, devpath_list consists the physpath name of the booting device
4005 * no matter the rootpool is a single device pool or a mirrored pool.
4007 * "/pci@1f,0/ide@d/disk@0,0:a"
4010 spa_import_rootpool(char *devpath
, char *devid
)
4013 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
4014 nvlist_t
*config
, *nvtop
;
4020 * Read the label from the boot device and generate a configuration.
4022 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
4023 #if defined(_OBP) && defined(_KERNEL)
4024 if (config
== NULL
) {
4025 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
4027 get_iscsi_bootpath_phy(devpath
);
4028 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
4032 if (config
== NULL
) {
4033 cmn_err(CE_NOTE
, "Cannot read the pool label from '%s'",
4035 return (SET_ERROR(EIO
));
4038 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4040 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
4042 mutex_enter(&spa_namespace_lock
);
4043 if ((spa
= spa_lookup(pname
)) != NULL
) {
4045 * Remove the existing root pool from the namespace so that we
4046 * can replace it with the correct config we just read in.
4051 spa
= spa_add(pname
, config
, NULL
);
4052 spa
->spa_is_root
= B_TRUE
;
4053 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
4056 * Build up a vdev tree based on the boot device's label config.
4058 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4060 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4061 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
4062 VDEV_ALLOC_ROOTPOOL
);
4063 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4065 mutex_exit(&spa_namespace_lock
);
4066 nvlist_free(config
);
4067 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
4073 * Get the boot vdev.
4075 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
4076 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
4077 (u_longlong_t
)guid
);
4078 error
= SET_ERROR(ENOENT
);
4083 * Determine if there is a better boot device.
4086 spa_alt_rootvdev(rvd
, &avd
, &txg
);
4088 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
4089 "try booting from '%s'", avd
->vdev_path
);
4090 error
= SET_ERROR(EINVAL
);
4095 * If the boot device is part of a spare vdev then ensure that
4096 * we're booting off the active spare.
4098 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4099 !bvd
->vdev_isspare
) {
4100 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
4101 "try booting from '%s'",
4103 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
4104 error
= SET_ERROR(EINVAL
);
4110 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4112 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4113 mutex_exit(&spa_namespace_lock
);
4115 nvlist_free(config
);
4122 * Import a non-root pool into the system.
4125 spa_import(const char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4128 char *altroot
= NULL
;
4129 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4130 zpool_rewind_policy_t policy
;
4131 uint64_t mode
= spa_mode_global
;
4132 uint64_t readonly
= B_FALSE
;
4135 nvlist_t
**spares
, **l2cache
;
4136 uint_t nspares
, nl2cache
;
4139 * If a pool with this name exists, return failure.
4141 mutex_enter(&spa_namespace_lock
);
4142 if (spa_lookup(pool
) != NULL
) {
4143 mutex_exit(&spa_namespace_lock
);
4144 return (SET_ERROR(EEXIST
));
4148 * Create and initialize the spa structure.
4150 (void) nvlist_lookup_string(props
,
4151 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4152 (void) nvlist_lookup_uint64(props
,
4153 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4156 spa
= spa_add(pool
, config
, altroot
);
4157 spa
->spa_import_flags
= flags
;
4160 * Verbatim import - Take a pool and insert it into the namespace
4161 * as if it had been loaded at boot.
4163 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4165 spa_configfile_set(spa
, props
, B_FALSE
);
4167 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
4168 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4170 mutex_exit(&spa_namespace_lock
);
4174 spa_activate(spa
, mode
);
4177 * Don't start async tasks until we know everything is healthy.
4179 spa_async_suspend(spa
);
4181 zpool_get_rewind_policy(config
, &policy
);
4182 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4183 state
= SPA_LOAD_RECOVER
;
4186 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4187 * because the user-supplied config is actually the one to trust when
4190 if (state
!= SPA_LOAD_RECOVER
)
4191 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4193 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4194 policy
.zrp_request
);
4197 * Propagate anything learned while loading the pool and pass it
4198 * back to caller (i.e. rewind info, missing devices, etc).
4200 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4201 spa
->spa_load_info
) == 0);
4203 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4205 * Toss any existing sparelist, as it doesn't have any validity
4206 * anymore, and conflicts with spa_has_spare().
4208 if (spa
->spa_spares
.sav_config
) {
4209 nvlist_free(spa
->spa_spares
.sav_config
);
4210 spa
->spa_spares
.sav_config
= NULL
;
4211 spa_load_spares(spa
);
4213 if (spa
->spa_l2cache
.sav_config
) {
4214 nvlist_free(spa
->spa_l2cache
.sav_config
);
4215 spa
->spa_l2cache
.sav_config
= NULL
;
4216 spa_load_l2cache(spa
);
4219 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4222 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4225 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4226 VDEV_ALLOC_L2CACHE
);
4227 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4230 spa_configfile_set(spa
, props
, B_FALSE
);
4232 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4233 (error
= spa_prop_set(spa
, props
)))) {
4235 spa_deactivate(spa
);
4237 mutex_exit(&spa_namespace_lock
);
4241 spa_async_resume(spa
);
4244 * Override any spares and level 2 cache devices as specified by
4245 * the user, as these may have correct device names/devids, etc.
4247 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4248 &spares
, &nspares
) == 0) {
4249 if (spa
->spa_spares
.sav_config
)
4250 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4251 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4253 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4254 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4255 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4256 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4257 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4258 spa_load_spares(spa
);
4259 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4260 spa
->spa_spares
.sav_sync
= B_TRUE
;
4262 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4263 &l2cache
, &nl2cache
) == 0) {
4264 if (spa
->spa_l2cache
.sav_config
)
4265 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4266 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4268 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4269 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4270 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4271 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4272 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4273 spa_load_l2cache(spa
);
4274 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4275 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4279 * Check for any removed devices.
4281 if (spa
->spa_autoreplace
) {
4282 spa_aux_check_removed(&spa
->spa_spares
);
4283 spa_aux_check_removed(&spa
->spa_l2cache
);
4286 if (spa_writeable(spa
)) {
4288 * Update the config cache to include the newly-imported pool.
4290 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4294 * It's possible that the pool was expanded while it was exported.
4295 * We kick off an async task to handle this for us.
4297 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4299 spa_history_log_version(spa
, "import");
4301 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4303 mutex_exit(&spa_namespace_lock
);
4309 spa_tryimport(nvlist_t
*tryconfig
)
4311 nvlist_t
*config
= NULL
;
4317 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4320 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4324 * Create and initialize the spa structure.
4326 mutex_enter(&spa_namespace_lock
);
4327 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4328 spa_activate(spa
, FREAD
);
4331 * Pass off the heavy lifting to spa_load().
4332 * Pass TRUE for mosconfig because the user-supplied config
4333 * is actually the one to trust when doing an import.
4335 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4338 * If 'tryconfig' was at least parsable, return the current config.
4340 if (spa
->spa_root_vdev
!= NULL
) {
4341 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4342 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4344 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4346 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4347 spa
->spa_uberblock
.ub_timestamp
) == 0);
4348 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4349 spa
->spa_load_info
) == 0);
4352 * If the bootfs property exists on this pool then we
4353 * copy it out so that external consumers can tell which
4354 * pools are bootable.
4356 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4357 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4360 * We have to play games with the name since the
4361 * pool was opened as TRYIMPORT_NAME.
4363 if (dsl_dsobj_to_dsname(spa_name(spa
),
4364 spa
->spa_bootfs
, tmpname
) == 0) {
4366 char *dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4368 cp
= strchr(tmpname
, '/');
4370 (void) strlcpy(dsname
, tmpname
,
4373 (void) snprintf(dsname
, MAXPATHLEN
,
4374 "%s/%s", poolname
, ++cp
);
4376 VERIFY(nvlist_add_string(config
,
4377 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4378 kmem_free(dsname
, MAXPATHLEN
);
4380 kmem_free(tmpname
, MAXPATHLEN
);
4384 * Add the list of hot spares and level 2 cache devices.
4386 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4387 spa_add_spares(spa
, config
);
4388 spa_add_l2cache(spa
, config
);
4389 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4393 spa_deactivate(spa
);
4395 mutex_exit(&spa_namespace_lock
);
4401 * Pool export/destroy
4403 * The act of destroying or exporting a pool is very simple. We make sure there
4404 * is no more pending I/O and any references to the pool are gone. Then, we
4405 * update the pool state and sync all the labels to disk, removing the
4406 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4407 * we don't sync the labels or remove the configuration cache.
4410 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4411 boolean_t force
, boolean_t hardforce
)
4418 if (!(spa_mode_global
& FWRITE
))
4419 return (SET_ERROR(EROFS
));
4421 mutex_enter(&spa_namespace_lock
);
4422 if ((spa
= spa_lookup(pool
)) == NULL
) {
4423 mutex_exit(&spa_namespace_lock
);
4424 return (SET_ERROR(ENOENT
));
4428 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4429 * reacquire the namespace lock, and see if we can export.
4431 spa_open_ref(spa
, FTAG
);
4432 mutex_exit(&spa_namespace_lock
);
4433 spa_async_suspend(spa
);
4434 mutex_enter(&spa_namespace_lock
);
4435 spa_close(spa
, FTAG
);
4438 * The pool will be in core if it's openable,
4439 * in which case we can modify its state.
4441 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
&& spa
->spa_sync_on
) {
4443 * Objsets may be open only because they're dirty, so we
4444 * have to force it to sync before checking spa_refcnt.
4446 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4447 spa_evicting_os_wait(spa
);
4450 * A pool cannot be exported or destroyed if there are active
4451 * references. If we are resetting a pool, allow references by
4452 * fault injection handlers.
4454 if (!spa_refcount_zero(spa
) ||
4455 (spa
->spa_inject_ref
!= 0 &&
4456 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4457 spa_async_resume(spa
);
4458 mutex_exit(&spa_namespace_lock
);
4459 return (SET_ERROR(EBUSY
));
4463 * A pool cannot be exported if it has an active shared spare.
4464 * This is to prevent other pools stealing the active spare
4465 * from an exported pool. At user's own will, such pool can
4466 * be forcedly exported.
4468 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4469 spa_has_active_shared_spare(spa
)) {
4470 spa_async_resume(spa
);
4471 mutex_exit(&spa_namespace_lock
);
4472 return (SET_ERROR(EXDEV
));
4476 * We want this to be reflected on every label,
4477 * so mark them all dirty. spa_unload() will do the
4478 * final sync that pushes these changes out.
4480 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4481 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4482 spa
->spa_state
= new_state
;
4483 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4485 vdev_config_dirty(spa
->spa_root_vdev
);
4486 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4490 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
4492 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4494 spa_deactivate(spa
);
4497 if (oldconfig
&& spa
->spa_config
)
4498 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4500 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4502 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
4505 mutex_exit(&spa_namespace_lock
);
4511 * Destroy a storage pool.
4514 spa_destroy(char *pool
)
4516 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4521 * Export a storage pool.
4524 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4525 boolean_t hardforce
)
4527 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4532 * Similar to spa_export(), this unloads the spa_t without actually removing it
4533 * from the namespace in any way.
4536 spa_reset(char *pool
)
4538 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4543 * ==========================================================================
4544 * Device manipulation
4545 * ==========================================================================
4549 * Add a device to a storage pool.
4552 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4556 vdev_t
*rvd
= spa
->spa_root_vdev
;
4558 nvlist_t
**spares
, **l2cache
;
4559 uint_t nspares
, nl2cache
;
4561 ASSERT(spa_writeable(spa
));
4563 txg
= spa_vdev_enter(spa
);
4565 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4566 VDEV_ALLOC_ADD
)) != 0)
4567 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4569 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4571 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4575 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4579 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4580 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4582 if (vd
->vdev_children
!= 0 &&
4583 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4584 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4587 * We must validate the spares and l2cache devices after checking the
4588 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4590 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4591 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4594 * If we are in the middle of a device removal, we can only add
4595 * devices which match the existing devices in the pool.
4596 * If we are in the middle of a removal, or have some indirect
4597 * vdevs, we can not add raidz toplevels.
4599 if (spa
->spa_vdev_removal
!= NULL
||
4600 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
4601 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4602 tvd
= vd
->vdev_child
[c
];
4603 if (spa
->spa_vdev_removal
!= NULL
&&
4605 spa
->spa_vdev_removal
->svr_vdev
->vdev_ashift
) {
4606 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4608 /* Fail if top level vdev is raidz */
4609 if (tvd
->vdev_ops
== &vdev_raidz_ops
) {
4610 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4613 * Need the top level mirror to be
4614 * a mirror of leaf vdevs only
4616 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
4617 for (uint64_t cid
= 0;
4618 cid
< tvd
->vdev_children
; cid
++) {
4619 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
4620 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
4621 return (spa_vdev_exit(spa
, vd
,
4629 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4632 * Set the vdev id to the first hole, if one exists.
4634 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4635 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4636 vdev_free(rvd
->vdev_child
[id
]);
4640 tvd
= vd
->vdev_child
[c
];
4641 vdev_remove_child(vd
, tvd
);
4643 vdev_add_child(rvd
, tvd
);
4644 vdev_config_dirty(tvd
);
4648 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4649 ZPOOL_CONFIG_SPARES
);
4650 spa_load_spares(spa
);
4651 spa
->spa_spares
.sav_sync
= B_TRUE
;
4654 if (nl2cache
!= 0) {
4655 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4656 ZPOOL_CONFIG_L2CACHE
);
4657 spa_load_l2cache(spa
);
4658 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4662 * We have to be careful when adding new vdevs to an existing pool.
4663 * If other threads start allocating from these vdevs before we
4664 * sync the config cache, and we lose power, then upon reboot we may
4665 * fail to open the pool because there are DVAs that the config cache
4666 * can't translate. Therefore, we first add the vdevs without
4667 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4668 * and then let spa_config_update() initialize the new metaslabs.
4670 * spa_load() checks for added-but-not-initialized vdevs, so that
4671 * if we lose power at any point in this sequence, the remaining
4672 * steps will be completed the next time we load the pool.
4674 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4676 mutex_enter(&spa_namespace_lock
);
4677 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4678 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
4679 mutex_exit(&spa_namespace_lock
);
4685 * Attach a device to a mirror. The arguments are the path to any device
4686 * in the mirror, and the nvroot for the new device. If the path specifies
4687 * a device that is not mirrored, we automatically insert the mirror vdev.
4689 * If 'replacing' is specified, the new device is intended to replace the
4690 * existing device; in this case the two devices are made into their own
4691 * mirror using the 'replacing' vdev, which is functionally identical to
4692 * the mirror vdev (it actually reuses all the same ops) but has a few
4693 * extra rules: you can't attach to it after it's been created, and upon
4694 * completion of resilvering, the first disk (the one being replaced)
4695 * is automatically detached.
4698 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4700 uint64_t txg
, dtl_max_txg
;
4701 vdev_t
*rvd
= spa
->spa_root_vdev
;
4702 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4704 char *oldvdpath
, *newvdpath
;
4708 ASSERT(spa_writeable(spa
));
4710 txg
= spa_vdev_enter(spa
);
4712 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4714 if (spa
->spa_vdev_removal
!= NULL
||
4715 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
4716 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4720 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4722 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4723 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4725 pvd
= oldvd
->vdev_parent
;
4727 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4728 VDEV_ALLOC_ATTACH
)) != 0)
4729 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4731 if (newrootvd
->vdev_children
!= 1)
4732 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4734 newvd
= newrootvd
->vdev_child
[0];
4736 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4737 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4739 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4740 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4743 * Spares can't replace logs
4745 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4746 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4750 * For attach, the only allowable parent is a mirror or the root
4753 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4754 pvd
->vdev_ops
!= &vdev_root_ops
)
4755 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4757 pvops
= &vdev_mirror_ops
;
4760 * Active hot spares can only be replaced by inactive hot
4763 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4764 oldvd
->vdev_isspare
&&
4765 !spa_has_spare(spa
, newvd
->vdev_guid
))
4766 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4769 * If the source is a hot spare, and the parent isn't already a
4770 * spare, then we want to create a new hot spare. Otherwise, we
4771 * want to create a replacing vdev. The user is not allowed to
4772 * attach to a spared vdev child unless the 'isspare' state is
4773 * the same (spare replaces spare, non-spare replaces
4776 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4777 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4778 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4779 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4780 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4781 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4784 if (newvd
->vdev_isspare
)
4785 pvops
= &vdev_spare_ops
;
4787 pvops
= &vdev_replacing_ops
;
4791 * Make sure the new device is big enough.
4793 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4794 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4797 * The new device cannot have a higher alignment requirement
4798 * than the top-level vdev.
4800 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4801 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4804 * If this is an in-place replacement, update oldvd's path and devid
4805 * to make it distinguishable from newvd, and unopenable from now on.
4807 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4808 spa_strfree(oldvd
->vdev_path
);
4809 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4811 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4812 newvd
->vdev_path
, "old");
4813 if (oldvd
->vdev_devid
!= NULL
) {
4814 spa_strfree(oldvd
->vdev_devid
);
4815 oldvd
->vdev_devid
= NULL
;
4819 /* mark the device being resilvered */
4820 newvd
->vdev_resilver_txg
= txg
;
4823 * If the parent is not a mirror, or if we're replacing, insert the new
4824 * mirror/replacing/spare vdev above oldvd.
4826 if (pvd
->vdev_ops
!= pvops
)
4827 pvd
= vdev_add_parent(oldvd
, pvops
);
4829 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4830 ASSERT(pvd
->vdev_ops
== pvops
);
4831 ASSERT(oldvd
->vdev_parent
== pvd
);
4834 * Extract the new device from its root and add it to pvd.
4836 vdev_remove_child(newrootvd
, newvd
);
4837 newvd
->vdev_id
= pvd
->vdev_children
;
4838 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4839 vdev_add_child(pvd
, newvd
);
4841 tvd
= newvd
->vdev_top
;
4842 ASSERT(pvd
->vdev_top
== tvd
);
4843 ASSERT(tvd
->vdev_parent
== rvd
);
4845 vdev_config_dirty(tvd
);
4848 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4849 * for any dmu_sync-ed blocks. It will propagate upward when
4850 * spa_vdev_exit() calls vdev_dtl_reassess().
4852 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4854 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4855 dtl_max_txg
- TXG_INITIAL
);
4857 if (newvd
->vdev_isspare
) {
4858 spa_spare_activate(newvd
);
4859 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
4862 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4863 newvdpath
= spa_strdup(newvd
->vdev_path
);
4864 newvd_isspare
= newvd
->vdev_isspare
;
4867 * Mark newvd's DTL dirty in this txg.
4869 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4872 * Schedule the resilver to restart in the future. We do this to
4873 * ensure that dmu_sync-ed blocks have been stitched into the
4874 * respective datasets.
4876 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4878 if (spa
->spa_bootfs
)
4879 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
4881 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
4886 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4888 spa_history_log_internal(spa
, "vdev attach", NULL
,
4889 "%s vdev=%s %s vdev=%s",
4890 replacing
&& newvd_isspare
? "spare in" :
4891 replacing
? "replace" : "attach", newvdpath
,
4892 replacing
? "for" : "to", oldvdpath
);
4894 spa_strfree(oldvdpath
);
4895 spa_strfree(newvdpath
);
4901 * Detach a device from a mirror or replacing vdev.
4903 * If 'replace_done' is specified, only detach if the parent
4904 * is a replacing vdev.
4907 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4911 vdev_t
*rvd
= spa
->spa_root_vdev
;
4912 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4913 boolean_t unspare
= B_FALSE
;
4914 uint64_t unspare_guid
= 0;
4917 ASSERT(spa_writeable(spa
));
4919 txg
= spa_vdev_enter(spa
);
4921 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4924 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4926 if (!vd
->vdev_ops
->vdev_op_leaf
)
4927 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4929 pvd
= vd
->vdev_parent
;
4932 * If the parent/child relationship is not as expected, don't do it.
4933 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4934 * vdev that's replacing B with C. The user's intent in replacing
4935 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4936 * the replace by detaching C, the expected behavior is to end up
4937 * M(A,B). But suppose that right after deciding to detach C,
4938 * the replacement of B completes. We would have M(A,C), and then
4939 * ask to detach C, which would leave us with just A -- not what
4940 * the user wanted. To prevent this, we make sure that the
4941 * parent/child relationship hasn't changed -- in this example,
4942 * that C's parent is still the replacing vdev R.
4944 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4945 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4948 * Only 'replacing' or 'spare' vdevs can be replaced.
4950 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4951 pvd
->vdev_ops
!= &vdev_spare_ops
)
4952 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4954 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4955 spa_version(spa
) >= SPA_VERSION_SPARES
);
4958 * Only mirror, replacing, and spare vdevs support detach.
4960 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4961 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4962 pvd
->vdev_ops
!= &vdev_spare_ops
)
4963 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4966 * If this device has the only valid copy of some data,
4967 * we cannot safely detach it.
4969 if (vdev_dtl_required(vd
))
4970 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4972 ASSERT(pvd
->vdev_children
>= 2);
4975 * If we are detaching the second disk from a replacing vdev, then
4976 * check to see if we changed the original vdev's path to have "/old"
4977 * at the end in spa_vdev_attach(). If so, undo that change now.
4979 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4980 vd
->vdev_path
!= NULL
) {
4981 size_t len
= strlen(vd
->vdev_path
);
4983 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
4984 cvd
= pvd
->vdev_child
[c
];
4986 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4989 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4990 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4991 spa_strfree(cvd
->vdev_path
);
4992 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4999 * If we are detaching the original disk from a spare, then it implies
5000 * that the spare should become a real disk, and be removed from the
5001 * active spare list for the pool.
5003 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5005 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
5009 * Erase the disk labels so the disk can be used for other things.
5010 * This must be done after all other error cases are handled,
5011 * but before we disembowel vd (so we can still do I/O to it).
5012 * But if we can't do it, don't treat the error as fatal --
5013 * it may be that the unwritability of the disk is the reason
5014 * it's being detached!
5016 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5019 * Remove vd from its parent and compact the parent's children.
5021 vdev_remove_child(pvd
, vd
);
5022 vdev_compact_children(pvd
);
5025 * Remember one of the remaining children so we can get tvd below.
5027 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
5030 * If we need to remove the remaining child from the list of hot spares,
5031 * do it now, marking the vdev as no longer a spare in the process.
5032 * We must do this before vdev_remove_parent(), because that can
5033 * change the GUID if it creates a new toplevel GUID. For a similar
5034 * reason, we must remove the spare now, in the same txg as the detach;
5035 * otherwise someone could attach a new sibling, change the GUID, and
5036 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5039 ASSERT(cvd
->vdev_isspare
);
5040 spa_spare_remove(cvd
);
5041 unspare_guid
= cvd
->vdev_guid
;
5042 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
5043 cvd
->vdev_unspare
= B_TRUE
;
5047 * If the parent mirror/replacing vdev only has one child,
5048 * the parent is no longer needed. Remove it from the tree.
5050 if (pvd
->vdev_children
== 1) {
5051 if (pvd
->vdev_ops
== &vdev_spare_ops
)
5052 cvd
->vdev_unspare
= B_FALSE
;
5053 vdev_remove_parent(cvd
);
5058 * We don't set tvd until now because the parent we just removed
5059 * may have been the previous top-level vdev.
5061 tvd
= cvd
->vdev_top
;
5062 ASSERT(tvd
->vdev_parent
== rvd
);
5065 * Reevaluate the parent vdev state.
5067 vdev_propagate_state(cvd
);
5070 * If the 'autoexpand' property is set on the pool then automatically
5071 * try to expand the size of the pool. For example if the device we
5072 * just detached was smaller than the others, it may be possible to
5073 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5074 * first so that we can obtain the updated sizes of the leaf vdevs.
5076 if (spa
->spa_autoexpand
) {
5078 vdev_expand(tvd
, txg
);
5081 vdev_config_dirty(tvd
);
5084 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5085 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5086 * But first make sure we're not on any *other* txg's DTL list, to
5087 * prevent vd from being accessed after it's freed.
5089 vdpath
= spa_strdup(vd
->vdev_path
);
5090 for (int t
= 0; t
< TXG_SIZE
; t
++)
5091 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
5092 vd
->vdev_detached
= B_TRUE
;
5093 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
5095 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
5097 /* hang on to the spa before we release the lock */
5098 spa_open_ref(spa
, FTAG
);
5100 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
5102 spa_history_log_internal(spa
, "detach", NULL
,
5104 spa_strfree(vdpath
);
5107 * If this was the removal of the original device in a hot spare vdev,
5108 * then we want to go through and remove the device from the hot spare
5109 * list of every other pool.
5112 spa_t
*altspa
= NULL
;
5114 mutex_enter(&spa_namespace_lock
);
5115 while ((altspa
= spa_next(altspa
)) != NULL
) {
5116 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
5120 spa_open_ref(altspa
, FTAG
);
5121 mutex_exit(&spa_namespace_lock
);
5122 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
5123 mutex_enter(&spa_namespace_lock
);
5124 spa_close(altspa
, FTAG
);
5126 mutex_exit(&spa_namespace_lock
);
5128 /* search the rest of the vdevs for spares to remove */
5129 spa_vdev_resilver_done(spa
);
5132 /* all done with the spa; OK to release */
5133 mutex_enter(&spa_namespace_lock
);
5134 spa_close(spa
, FTAG
);
5135 mutex_exit(&spa_namespace_lock
);
5141 * Split a set of devices from their mirrors, and create a new pool from them.
5144 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5145 nvlist_t
*props
, boolean_t exp
)
5148 uint64_t txg
, *glist
;
5150 uint_t c
, children
, lastlog
;
5151 nvlist_t
**child
, *nvl
, *tmp
;
5153 char *altroot
= NULL
;
5154 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5155 boolean_t activate_slog
;
5157 ASSERT(spa_writeable(spa
));
5159 txg
= spa_vdev_enter(spa
);
5161 /* clear the log and flush everything up to now */
5162 activate_slog
= spa_passivate_log(spa
);
5163 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5164 error
= spa_reset_logs(spa
);
5165 txg
= spa_vdev_config_enter(spa
);
5168 spa_activate_log(spa
);
5171 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5173 /* check new spa name before going any further */
5174 if (spa_lookup(newname
) != NULL
)
5175 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5178 * scan through all the children to ensure they're all mirrors
5180 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5181 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5183 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5185 /* first, check to ensure we've got the right child count */
5186 rvd
= spa
->spa_root_vdev
;
5188 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5189 vdev_t
*vd
= rvd
->vdev_child
[c
];
5191 /* don't count the holes & logs as children */
5192 if (vd
->vdev_islog
|| !vdev_is_concrete(vd
)) {
5200 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5201 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5203 /* next, ensure no spare or cache devices are part of the split */
5204 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5205 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5206 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5208 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5209 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5211 /* then, loop over each vdev and validate it */
5212 for (c
= 0; c
< children
; c
++) {
5213 uint64_t is_hole
= 0;
5215 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5219 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5220 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5223 error
= SET_ERROR(EINVAL
);
5228 /* which disk is going to be split? */
5229 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5231 error
= SET_ERROR(EINVAL
);
5235 /* look it up in the spa */
5236 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5237 if (vml
[c
] == NULL
) {
5238 error
= SET_ERROR(ENODEV
);
5242 /* make sure there's nothing stopping the split */
5243 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5244 vml
[c
]->vdev_islog
||
5245 !vdev_is_concrete(vml
[c
]) ||
5246 vml
[c
]->vdev_isspare
||
5247 vml
[c
]->vdev_isl2cache
||
5248 !vdev_writeable(vml
[c
]) ||
5249 vml
[c
]->vdev_children
!= 0 ||
5250 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5251 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5252 error
= SET_ERROR(EINVAL
);
5256 if (vdev_dtl_required(vml
[c
])) {
5257 error
= SET_ERROR(EBUSY
);
5261 /* we need certain info from the top level */
5262 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5263 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5264 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5265 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5266 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5267 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5268 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5269 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5271 /* transfer per-vdev ZAPs */
5272 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
5273 VERIFY0(nvlist_add_uint64(child
[c
],
5274 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
5276 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
5277 VERIFY0(nvlist_add_uint64(child
[c
],
5278 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
5279 vml
[c
]->vdev_parent
->vdev_top_zap
));
5283 kmem_free(vml
, children
* sizeof (vdev_t
*));
5284 kmem_free(glist
, children
* sizeof (uint64_t));
5285 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5288 /* stop writers from using the disks */
5289 for (c
= 0; c
< children
; c
++) {
5291 vml
[c
]->vdev_offline
= B_TRUE
;
5293 vdev_reopen(spa
->spa_root_vdev
);
5296 * Temporarily record the splitting vdevs in the spa config. This
5297 * will disappear once the config is regenerated.
5299 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5300 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5301 glist
, children
) == 0);
5302 kmem_free(glist
, children
* sizeof (uint64_t));
5304 mutex_enter(&spa
->spa_props_lock
);
5305 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5307 mutex_exit(&spa
->spa_props_lock
);
5308 spa
->spa_config_splitting
= nvl
;
5309 vdev_config_dirty(spa
->spa_root_vdev
);
5311 /* configure and create the new pool */
5312 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5313 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5314 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5315 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5316 spa_version(spa
)) == 0);
5317 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5318 spa
->spa_config_txg
) == 0);
5319 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5320 spa_generate_guid(NULL
)) == 0);
5321 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
5322 (void) nvlist_lookup_string(props
,
5323 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5325 /* add the new pool to the namespace */
5326 newspa
= spa_add(newname
, config
, altroot
);
5327 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5328 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5329 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5331 /* release the spa config lock, retaining the namespace lock */
5332 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5334 if (zio_injection_enabled
)
5335 zio_handle_panic_injection(spa
, FTAG
, 1);
5337 spa_activate(newspa
, spa_mode_global
);
5338 spa_async_suspend(newspa
);
5340 /* create the new pool from the disks of the original pool */
5341 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5345 /* if that worked, generate a real config for the new pool */
5346 if (newspa
->spa_root_vdev
!= NULL
) {
5347 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5348 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5349 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5350 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5351 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5356 if (props
!= NULL
) {
5357 spa_configfile_set(newspa
, props
, B_FALSE
);
5358 error
= spa_prop_set(newspa
, props
);
5363 /* flush everything */
5364 txg
= spa_vdev_config_enter(newspa
);
5365 vdev_config_dirty(newspa
->spa_root_vdev
);
5366 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5368 if (zio_injection_enabled
)
5369 zio_handle_panic_injection(spa
, FTAG
, 2);
5371 spa_async_resume(newspa
);
5373 /* finally, update the original pool's config */
5374 txg
= spa_vdev_config_enter(spa
);
5375 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5376 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5379 for (c
= 0; c
< children
; c
++) {
5380 if (vml
[c
] != NULL
) {
5383 spa_history_log_internal(spa
, "detach", tx
,
5384 "vdev=%s", vml
[c
]->vdev_path
);
5389 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5390 vdev_config_dirty(spa
->spa_root_vdev
);
5391 spa
->spa_config_splitting
= NULL
;
5395 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5397 if (zio_injection_enabled
)
5398 zio_handle_panic_injection(spa
, FTAG
, 3);
5400 /* split is complete; log a history record */
5401 spa_history_log_internal(newspa
, "split", NULL
,
5402 "from pool %s", spa_name(spa
));
5404 kmem_free(vml
, children
* sizeof (vdev_t
*));
5406 /* if we're not going to mount the filesystems in userland, export */
5408 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5415 spa_deactivate(newspa
);
5418 txg
= spa_vdev_config_enter(spa
);
5420 /* re-online all offlined disks */
5421 for (c
= 0; c
< children
; c
++) {
5423 vml
[c
]->vdev_offline
= B_FALSE
;
5425 vdev_reopen(spa
->spa_root_vdev
);
5427 nvlist_free(spa
->spa_config_splitting
);
5428 spa
->spa_config_splitting
= NULL
;
5429 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5431 kmem_free(vml
, children
* sizeof (vdev_t
*));
5436 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5437 * currently spared, so we can detach it.
5440 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5442 vdev_t
*newvd
, *oldvd
;
5444 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5445 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5451 * Check for a completed replacement. We always consider the first
5452 * vdev in the list to be the oldest vdev, and the last one to be
5453 * the newest (see spa_vdev_attach() for how that works). In
5454 * the case where the newest vdev is faulted, we will not automatically
5455 * remove it after a resilver completes. This is OK as it will require
5456 * user intervention to determine which disk the admin wishes to keep.
5458 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5459 ASSERT(vd
->vdev_children
> 1);
5461 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5462 oldvd
= vd
->vdev_child
[0];
5464 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5465 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5466 !vdev_dtl_required(oldvd
))
5471 * Check for a completed resilver with the 'unspare' flag set.
5473 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5474 vdev_t
*first
= vd
->vdev_child
[0];
5475 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5477 if (last
->vdev_unspare
) {
5480 } else if (first
->vdev_unspare
) {
5487 if (oldvd
!= NULL
&&
5488 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5489 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5490 !vdev_dtl_required(oldvd
))
5494 * If there are more than two spares attached to a disk,
5495 * and those spares are not required, then we want to
5496 * attempt to free them up now so that they can be used
5497 * by other pools. Once we're back down to a single
5498 * disk+spare, we stop removing them.
5500 if (vd
->vdev_children
> 2) {
5501 newvd
= vd
->vdev_child
[1];
5503 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5504 vdev_dtl_empty(last
, DTL_MISSING
) &&
5505 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5506 !vdev_dtl_required(newvd
))
5515 spa_vdev_resilver_done(spa_t
*spa
)
5517 vdev_t
*vd
, *pvd
, *ppvd
;
5518 uint64_t guid
, sguid
, pguid
, ppguid
;
5520 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5522 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5523 pvd
= vd
->vdev_parent
;
5524 ppvd
= pvd
->vdev_parent
;
5525 guid
= vd
->vdev_guid
;
5526 pguid
= pvd
->vdev_guid
;
5527 ppguid
= ppvd
->vdev_guid
;
5530 * If we have just finished replacing a hot spared device, then
5531 * we need to detach the parent's first child (the original hot
5534 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5535 ppvd
->vdev_children
== 2) {
5536 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5537 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5539 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5541 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5542 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5544 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5546 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5549 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5553 * Update the stored path or FRU for this vdev.
5556 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5560 boolean_t sync
= B_FALSE
;
5562 ASSERT(spa_writeable(spa
));
5564 spa_vdev_state_enter(spa
, SCL_ALL
);
5566 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5567 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5569 if (!vd
->vdev_ops
->vdev_op_leaf
)
5570 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5573 if (strcmp(value
, vd
->vdev_path
) != 0) {
5574 spa_strfree(vd
->vdev_path
);
5575 vd
->vdev_path
= spa_strdup(value
);
5579 if (vd
->vdev_fru
== NULL
) {
5580 vd
->vdev_fru
= spa_strdup(value
);
5582 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5583 spa_strfree(vd
->vdev_fru
);
5584 vd
->vdev_fru
= spa_strdup(value
);
5589 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5593 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5595 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5599 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5601 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5605 * ==========================================================================
5607 * ==========================================================================
5610 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
5612 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5614 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5615 return (SET_ERROR(EBUSY
));
5617 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
5621 spa_scan_stop(spa_t
*spa
)
5623 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5624 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5625 return (SET_ERROR(EBUSY
));
5626 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5630 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5632 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5634 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5635 return (SET_ERROR(ENOTSUP
));
5638 * If a resilver was requested, but there is no DTL on a
5639 * writeable leaf device, we have nothing to do.
5641 if (func
== POOL_SCAN_RESILVER
&&
5642 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5643 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5647 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5651 * ==========================================================================
5652 * SPA async task processing
5653 * ==========================================================================
5657 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5659 if (vd
->vdev_remove_wanted
) {
5660 vd
->vdev_remove_wanted
= B_FALSE
;
5661 vd
->vdev_delayed_close
= B_FALSE
;
5662 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5665 * We want to clear the stats, but we don't want to do a full
5666 * vdev_clear() as that will cause us to throw away
5667 * degraded/faulted state as well as attempt to reopen the
5668 * device, all of which is a waste.
5670 vd
->vdev_stat
.vs_read_errors
= 0;
5671 vd
->vdev_stat
.vs_write_errors
= 0;
5672 vd
->vdev_stat
.vs_checksum_errors
= 0;
5674 vdev_state_dirty(vd
->vdev_top
);
5677 for (int c
= 0; c
< vd
->vdev_children
; c
++)
5678 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5682 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5684 if (vd
->vdev_probe_wanted
) {
5685 vd
->vdev_probe_wanted
= B_FALSE
;
5686 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5689 for (int c
= 0; c
< vd
->vdev_children
; c
++)
5690 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5694 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5700 if (!spa
->spa_autoexpand
)
5703 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5704 vdev_t
*cvd
= vd
->vdev_child
[c
];
5705 spa_async_autoexpand(spa
, cvd
);
5708 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5711 physpath
= kmem_zalloc(MAXPATHLEN
, KM_SLEEP
);
5712 (void) snprintf(physpath
, MAXPATHLEN
, "/devices%s", vd
->vdev_physpath
);
5714 VERIFY(nvlist_alloc(&attr
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5715 VERIFY(nvlist_add_string(attr
, DEV_PHYS_PATH
, physpath
) == 0);
5717 (void) ddi_log_sysevent(zfs_dip
, SUNW_VENDOR
, EC_DEV_STATUS
,
5718 ESC_DEV_DLE
, attr
, &eid
, DDI_SLEEP
);
5721 kmem_free(physpath
, MAXPATHLEN
);
5725 spa_async_thread(void *arg
)
5727 spa_t
*spa
= (spa_t
*)arg
;
5730 ASSERT(spa
->spa_sync_on
);
5732 mutex_enter(&spa
->spa_async_lock
);
5733 tasks
= spa
->spa_async_tasks
;
5734 spa
->spa_async_tasks
= 0;
5735 mutex_exit(&spa
->spa_async_lock
);
5738 * See if the config needs to be updated.
5740 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5741 uint64_t old_space
, new_space
;
5743 mutex_enter(&spa_namespace_lock
);
5744 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5745 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5746 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5747 mutex_exit(&spa_namespace_lock
);
5750 * If the pool grew as a result of the config update,
5751 * then log an internal history event.
5753 if (new_space
!= old_space
) {
5754 spa_history_log_internal(spa
, "vdev online", NULL
,
5755 "pool '%s' size: %llu(+%llu)",
5756 spa_name(spa
), new_space
, new_space
- old_space
);
5761 * See if any devices need to be marked REMOVED.
5763 if (tasks
& SPA_ASYNC_REMOVE
) {
5764 spa_vdev_state_enter(spa
, SCL_NONE
);
5765 spa_async_remove(spa
, spa
->spa_root_vdev
);
5766 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5767 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5768 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5769 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5770 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5773 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5774 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5775 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5776 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5780 * See if any devices need to be probed.
5782 if (tasks
& SPA_ASYNC_PROBE
) {
5783 spa_vdev_state_enter(spa
, SCL_NONE
);
5784 spa_async_probe(spa
, spa
->spa_root_vdev
);
5785 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5789 * If any devices are done replacing, detach them.
5791 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5792 spa_vdev_resilver_done(spa
);
5795 * Kick off a resilver.
5797 if (tasks
& SPA_ASYNC_RESILVER
)
5798 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5801 * Let the world know that we're done.
5803 mutex_enter(&spa
->spa_async_lock
);
5804 spa
->spa_async_thread
= NULL
;
5805 cv_broadcast(&spa
->spa_async_cv
);
5806 mutex_exit(&spa
->spa_async_lock
);
5811 spa_async_suspend(spa_t
*spa
)
5813 mutex_enter(&spa
->spa_async_lock
);
5814 spa
->spa_async_suspended
++;
5815 while (spa
->spa_async_thread
!= NULL
||
5816 spa
->spa_condense_thread
!= NULL
)
5817 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5818 mutex_exit(&spa
->spa_async_lock
);
5820 spa_vdev_remove_suspend(spa
);
5824 spa_async_resume(spa_t
*spa
)
5826 mutex_enter(&spa
->spa_async_lock
);
5827 ASSERT(spa
->spa_async_suspended
!= 0);
5828 spa
->spa_async_suspended
--;
5829 mutex_exit(&spa
->spa_async_lock
);
5830 spa_restart_removal(spa
);
5834 spa_async_tasks_pending(spa_t
*spa
)
5836 uint_t non_config_tasks
;
5838 boolean_t config_task_suspended
;
5840 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
5841 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
5842 if (spa
->spa_ccw_fail_time
== 0) {
5843 config_task_suspended
= B_FALSE
;
5845 config_task_suspended
=
5846 (gethrtime() - spa
->spa_ccw_fail_time
) <
5847 (zfs_ccw_retry_interval
* NANOSEC
);
5850 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
5854 spa_async_dispatch(spa_t
*spa
)
5856 mutex_enter(&spa
->spa_async_lock
);
5857 if (spa_async_tasks_pending(spa
) &&
5858 !spa
->spa_async_suspended
&&
5859 spa
->spa_async_thread
== NULL
&&
5861 spa
->spa_async_thread
= thread_create(NULL
, 0,
5862 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5863 mutex_exit(&spa
->spa_async_lock
);
5867 spa_async_request(spa_t
*spa
, int task
)
5869 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5870 mutex_enter(&spa
->spa_async_lock
);
5871 spa
->spa_async_tasks
|= task
;
5872 mutex_exit(&spa
->spa_async_lock
);
5876 * ==========================================================================
5877 * SPA syncing routines
5878 * ==========================================================================
5882 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5885 bpobj_enqueue(bpo
, bp
, tx
);
5890 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5894 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5900 * Note: this simple function is not inlined to make it easier to dtrace the
5901 * amount of time spent syncing frees.
5904 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
5906 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5907 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
5908 VERIFY(zio_wait(zio
) == 0);
5912 * Note: this simple function is not inlined to make it easier to dtrace the
5913 * amount of time spent syncing deferred frees.
5916 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
5918 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5919 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
5920 spa_free_sync_cb
, zio
, tx
), ==, 0);
5921 VERIFY0(zio_wait(zio
));
5926 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
5928 char *packed
= NULL
;
5933 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
5936 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5937 * information. This avoids the dmu_buf_will_dirty() path and
5938 * saves us a pre-read to get data we don't actually care about.
5940 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
5941 packed
= kmem_alloc(bufsize
, KM_SLEEP
);
5943 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
5945 bzero(packed
+ nvsize
, bufsize
- nvsize
);
5947 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
5949 kmem_free(packed
, bufsize
);
5951 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
5952 dmu_buf_will_dirty(db
, tx
);
5953 *(uint64_t *)db
->db_data
= nvsize
;
5954 dmu_buf_rele(db
, FTAG
);
5958 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
5959 const char *config
, const char *entry
)
5969 * Update the MOS nvlist describing the list of available devices.
5970 * spa_validate_aux() will have already made sure this nvlist is
5971 * valid and the vdevs are labeled appropriately.
5973 if (sav
->sav_object
== 0) {
5974 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5975 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
5976 sizeof (uint64_t), tx
);
5977 VERIFY(zap_update(spa
->spa_meta_objset
,
5978 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
5979 &sav
->sav_object
, tx
) == 0);
5982 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5983 if (sav
->sav_count
== 0) {
5984 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
5986 list
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
5987 for (i
= 0; i
< sav
->sav_count
; i
++)
5988 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
5989 B_FALSE
, VDEV_CONFIG_L2CACHE
);
5990 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
5991 sav
->sav_count
) == 0);
5992 for (i
= 0; i
< sav
->sav_count
; i
++)
5993 nvlist_free(list
[i
]);
5994 kmem_free(list
, sav
->sav_count
* sizeof (void *));
5997 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
5998 nvlist_free(nvroot
);
6000 sav
->sav_sync
= B_FALSE
;
6004 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6005 * The all-vdev ZAP must be empty.
6008 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6010 spa_t
*spa
= vd
->vdev_spa
;
6011 if (vd
->vdev_top_zap
!= 0) {
6012 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6013 vd
->vdev_top_zap
, tx
));
6015 if (vd
->vdev_leaf_zap
!= 0) {
6016 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6017 vd
->vdev_leaf_zap
, tx
));
6019 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
6020 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6025 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6030 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6031 * its config may not be dirty but we still need to build per-vdev ZAPs.
6032 * Similarly, if the pool is being assembled (e.g. after a split), we
6033 * need to rebuild the AVZ although the config may not be dirty.
6035 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6036 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6039 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6041 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6042 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
6043 spa
->spa_all_vdev_zaps
!= 0);
6045 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6046 /* Make and build the new AVZ */
6047 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6048 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6049 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6051 /* Diff old AVZ with new one */
6055 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6056 spa
->spa_all_vdev_zaps
);
6057 zap_cursor_retrieve(&zc
, &za
) == 0;
6058 zap_cursor_advance(&zc
)) {
6059 uint64_t vdzap
= za
.za_first_integer
;
6060 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6063 * ZAP is listed in old AVZ but not in new one;
6066 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6071 zap_cursor_fini(&zc
);
6073 /* Destroy the old AVZ */
6074 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6075 spa
->spa_all_vdev_zaps
, tx
));
6077 /* Replace the old AVZ in the dir obj with the new one */
6078 VERIFY0(zap_update(spa
->spa_meta_objset
,
6079 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6080 sizeof (new_avz
), 1, &new_avz
, tx
));
6082 spa
->spa_all_vdev_zaps
= new_avz
;
6083 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6087 /* Walk through the AVZ and destroy all listed ZAPs */
6088 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6089 spa
->spa_all_vdev_zaps
);
6090 zap_cursor_retrieve(&zc
, &za
) == 0;
6091 zap_cursor_advance(&zc
)) {
6092 uint64_t zap
= za
.za_first_integer
;
6093 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6096 zap_cursor_fini(&zc
);
6098 /* Destroy and unlink the AVZ itself */
6099 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6100 spa
->spa_all_vdev_zaps
, tx
));
6101 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6102 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6103 spa
->spa_all_vdev_zaps
= 0;
6106 if (spa
->spa_all_vdev_zaps
== 0) {
6107 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6108 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6109 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6111 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6113 /* Create ZAPs for vdevs that don't have them. */
6114 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6116 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6117 dmu_tx_get_txg(tx
), B_FALSE
);
6120 * If we're upgrading the spa version then make sure that
6121 * the config object gets updated with the correct version.
6123 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6124 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6125 spa
->spa_uberblock
.ub_version
);
6127 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6129 nvlist_free(spa
->spa_config_syncing
);
6130 spa
->spa_config_syncing
= config
;
6132 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6136 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6138 uint64_t *versionp
= arg
;
6139 uint64_t version
= *versionp
;
6140 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6143 * Setting the version is special cased when first creating the pool.
6145 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6147 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6148 ASSERT(version
>= spa_version(spa
));
6150 spa
->spa_uberblock
.ub_version
= version
;
6151 vdev_config_dirty(spa
->spa_root_vdev
);
6152 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6156 * Set zpool properties.
6159 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6161 nvlist_t
*nvp
= arg
;
6162 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6163 objset_t
*mos
= spa
->spa_meta_objset
;
6164 nvpair_t
*elem
= NULL
;
6166 mutex_enter(&spa
->spa_props_lock
);
6168 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6170 char *strval
, *fname
;
6172 const char *propname
;
6173 zprop_type_t proptype
;
6176 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
6179 * We checked this earlier in spa_prop_validate().
6181 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6183 fname
= strchr(nvpair_name(elem
), '@') + 1;
6184 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6186 spa_feature_enable(spa
, fid
, tx
);
6187 spa_history_log_internal(spa
, "set", tx
,
6188 "%s=enabled", nvpair_name(elem
));
6191 case ZPOOL_PROP_VERSION
:
6192 intval
= fnvpair_value_uint64(elem
);
6194 * The version is synced seperatly before other
6195 * properties and should be correct by now.
6197 ASSERT3U(spa_version(spa
), >=, intval
);
6200 case ZPOOL_PROP_ALTROOT
:
6202 * 'altroot' is a non-persistent property. It should
6203 * have been set temporarily at creation or import time.
6205 ASSERT(spa
->spa_root
!= NULL
);
6208 case ZPOOL_PROP_READONLY
:
6209 case ZPOOL_PROP_CACHEFILE
:
6211 * 'readonly' and 'cachefile' are also non-persisitent
6215 case ZPOOL_PROP_COMMENT
:
6216 strval
= fnvpair_value_string(elem
);
6217 if (spa
->spa_comment
!= NULL
)
6218 spa_strfree(spa
->spa_comment
);
6219 spa
->spa_comment
= spa_strdup(strval
);
6221 * We need to dirty the configuration on all the vdevs
6222 * so that their labels get updated. It's unnecessary
6223 * to do this for pool creation since the vdev's
6224 * configuratoin has already been dirtied.
6226 if (tx
->tx_txg
!= TXG_INITIAL
)
6227 vdev_config_dirty(spa
->spa_root_vdev
);
6228 spa_history_log_internal(spa
, "set", tx
,
6229 "%s=%s", nvpair_name(elem
), strval
);
6233 * Set pool property values in the poolprops mos object.
6235 if (spa
->spa_pool_props_object
== 0) {
6236 spa
->spa_pool_props_object
=
6237 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6238 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6242 /* normalize the property name */
6243 propname
= zpool_prop_to_name(prop
);
6244 proptype
= zpool_prop_get_type(prop
);
6246 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6247 ASSERT(proptype
== PROP_TYPE_STRING
);
6248 strval
= fnvpair_value_string(elem
);
6249 VERIFY0(zap_update(mos
,
6250 spa
->spa_pool_props_object
, propname
,
6251 1, strlen(strval
) + 1, strval
, tx
));
6252 spa_history_log_internal(spa
, "set", tx
,
6253 "%s=%s", nvpair_name(elem
), strval
);
6254 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6255 intval
= fnvpair_value_uint64(elem
);
6257 if (proptype
== PROP_TYPE_INDEX
) {
6259 VERIFY0(zpool_prop_index_to_string(
6260 prop
, intval
, &unused
));
6262 VERIFY0(zap_update(mos
,
6263 spa
->spa_pool_props_object
, propname
,
6264 8, 1, &intval
, tx
));
6265 spa_history_log_internal(spa
, "set", tx
,
6266 "%s=%lld", nvpair_name(elem
), intval
);
6268 ASSERT(0); /* not allowed */
6272 case ZPOOL_PROP_DELEGATION
:
6273 spa
->spa_delegation
= intval
;
6275 case ZPOOL_PROP_BOOTFS
:
6276 spa
->spa_bootfs
= intval
;
6278 case ZPOOL_PROP_FAILUREMODE
:
6279 spa
->spa_failmode
= intval
;
6281 case ZPOOL_PROP_AUTOEXPAND
:
6282 spa
->spa_autoexpand
= intval
;
6283 if (tx
->tx_txg
!= TXG_INITIAL
)
6284 spa_async_request(spa
,
6285 SPA_ASYNC_AUTOEXPAND
);
6287 case ZPOOL_PROP_DEDUPDITTO
:
6288 spa
->spa_dedup_ditto
= intval
;
6297 mutex_exit(&spa
->spa_props_lock
);
6301 * Perform one-time upgrade on-disk changes. spa_version() does not
6302 * reflect the new version this txg, so there must be no changes this
6303 * txg to anything that the upgrade code depends on after it executes.
6304 * Therefore this must be called after dsl_pool_sync() does the sync
6308 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6310 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6312 ASSERT(spa
->spa_sync_pass
== 1);
6314 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6316 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6317 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6318 dsl_pool_create_origin(dp
, tx
);
6320 /* Keeping the origin open increases spa_minref */
6321 spa
->spa_minref
+= 3;
6324 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6325 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6326 dsl_pool_upgrade_clones(dp
, tx
);
6329 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6330 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6331 dsl_pool_upgrade_dir_clones(dp
, tx
);
6333 /* Keeping the freedir open increases spa_minref */
6334 spa
->spa_minref
+= 3;
6337 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6338 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6339 spa_feature_create_zap_objects(spa
, tx
);
6343 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6344 * when possibility to use lz4 compression for metadata was added
6345 * Old pools that have this feature enabled must be upgraded to have
6346 * this feature active
6348 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6349 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6350 SPA_FEATURE_LZ4_COMPRESS
);
6351 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6352 SPA_FEATURE_LZ4_COMPRESS
);
6354 if (lz4_en
&& !lz4_ac
)
6355 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6359 * If we haven't written the salt, do so now. Note that the
6360 * feature may not be activated yet, but that's fine since
6361 * the presence of this ZAP entry is backwards compatible.
6363 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
6364 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
6365 VERIFY0(zap_add(spa
->spa_meta_objset
,
6366 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
6367 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
6368 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
6371 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6375 vdev_indirect_state_sync_verify(vdev_t
*vd
)
6377 vdev_indirect_mapping_t
*vim
= vd
->vdev_indirect_mapping
;
6378 vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
;
6380 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
6381 ASSERT(vim
!= NULL
);
6382 ASSERT(vib
!= NULL
);
6385 if (vdev_obsolete_sm_object(vd
) != 0) {
6386 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
6387 ASSERT(vd
->vdev_removing
||
6388 vd
->vdev_ops
== &vdev_indirect_ops
);
6389 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
6390 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
6392 ASSERT3U(vdev_obsolete_sm_object(vd
), ==,
6393 space_map_object(vd
->vdev_obsolete_sm
));
6394 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
6395 space_map_allocated(vd
->vdev_obsolete_sm
));
6397 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
6400 * Since frees / remaps to an indirect vdev can only
6401 * happen in syncing context, the obsolete segments
6402 * tree must be empty when we start syncing.
6404 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
6408 * Sync the specified transaction group. New blocks may be dirtied as
6409 * part of the process, so we iterate until it converges.
6412 spa_sync(spa_t
*spa
, uint64_t txg
)
6414 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6415 objset_t
*mos
= spa
->spa_meta_objset
;
6416 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6417 vdev_t
*rvd
= spa
->spa_root_vdev
;
6421 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
6422 zfs_vdev_queue_depth_pct
/ 100;
6424 VERIFY(spa_writeable(spa
));
6427 * Wait for i/os issued in open context that need to complete
6428 * before this txg syncs.
6430 VERIFY0(zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]));
6431 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
, 0);
6434 * Lock out configuration changes.
6436 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6438 spa
->spa_syncing_txg
= txg
;
6439 spa
->spa_sync_pass
= 0;
6441 mutex_enter(&spa
->spa_alloc_lock
);
6442 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6443 mutex_exit(&spa
->spa_alloc_lock
);
6446 * If there are any pending vdev state changes, convert them
6447 * into config changes that go out with this transaction group.
6449 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6450 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6452 * We need the write lock here because, for aux vdevs,
6453 * calling vdev_config_dirty() modifies sav_config.
6454 * This is ugly and will become unnecessary when we
6455 * eliminate the aux vdev wart by integrating all vdevs
6456 * into the root vdev tree.
6458 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6459 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6460 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6461 vdev_state_clean(vd
);
6462 vdev_config_dirty(vd
);
6464 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6465 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6467 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6469 tx
= dmu_tx_create_assigned(dp
, txg
);
6471 spa
->spa_sync_starttime
= gethrtime();
6472 VERIFY(cyclic_reprogram(spa
->spa_deadman_cycid
,
6473 spa
->spa_sync_starttime
+ spa
->spa_deadman_synctime
));
6476 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6477 * set spa_deflate if we have no raid-z vdevs.
6479 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6480 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6483 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6484 vd
= rvd
->vdev_child
[i
];
6485 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6488 if (i
== rvd
->vdev_children
) {
6489 spa
->spa_deflate
= TRUE
;
6490 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6491 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6492 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6497 * Set the top-level vdev's max queue depth. Evaluate each
6498 * top-level's async write queue depth in case it changed.
6499 * The max queue depth will not change in the middle of syncing
6502 uint64_t queue_depth_total
= 0;
6503 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
6504 vdev_t
*tvd
= rvd
->vdev_child
[c
];
6505 metaslab_group_t
*mg
= tvd
->vdev_mg
;
6507 if (mg
== NULL
|| mg
->mg_class
!= spa_normal_class(spa
) ||
6508 !metaslab_group_initialized(mg
))
6512 * It is safe to do a lock-free check here because only async
6513 * allocations look at mg_max_alloc_queue_depth, and async
6514 * allocations all happen from spa_sync().
6516 ASSERT0(refcount_count(&mg
->mg_alloc_queue_depth
));
6517 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
6518 queue_depth_total
+= mg
->mg_max_alloc_queue_depth
;
6520 metaslab_class_t
*mc
= spa_normal_class(spa
);
6521 ASSERT0(refcount_count(&mc
->mc_alloc_slots
));
6522 mc
->mc_alloc_max_slots
= queue_depth_total
;
6523 mc
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
6525 ASSERT3U(mc
->mc_alloc_max_slots
, <=,
6526 max_queue_depth
* rvd
->vdev_children
);
6528 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
6529 vdev_t
*vd
= rvd
->vdev_child
[c
];
6530 vdev_indirect_state_sync_verify(vd
);
6532 if (vdev_indirect_should_condense(vd
)) {
6533 spa_condense_indirect_start_sync(vd
, tx
);
6539 * Iterate to convergence.
6542 int pass
= ++spa
->spa_sync_pass
;
6544 spa_sync_config_object(spa
, tx
);
6545 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6546 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6547 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6548 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6549 spa_errlog_sync(spa
, txg
);
6550 dsl_pool_sync(dp
, txg
);
6552 if (pass
< zfs_sync_pass_deferred_free
) {
6553 spa_sync_frees(spa
, free_bpl
, tx
);
6556 * We can not defer frees in pass 1, because
6557 * we sync the deferred frees later in pass 1.
6559 ASSERT3U(pass
, >, 1);
6560 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6561 &spa
->spa_deferred_bpobj
, tx
);
6565 dsl_scan_sync(dp
, tx
);
6567 if (spa
->spa_vdev_removal
!= NULL
)
6570 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
6575 spa_sync_upgrades(spa
, tx
);
6577 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6579 * Note: We need to check if the MOS is dirty
6580 * because we could have marked the MOS dirty
6581 * without updating the uberblock (e.g. if we
6582 * have sync tasks but no dirty user data). We
6583 * need to check the uberblock's rootbp because
6584 * it is updated if we have synced out dirty
6585 * data (though in this case the MOS will most
6586 * likely also be dirty due to second order
6587 * effects, we don't want to rely on that here).
6589 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6590 !dmu_objset_is_dirty(mos
, txg
)) {
6592 * Nothing changed on the first pass,
6593 * therefore this TXG is a no-op. Avoid
6594 * syncing deferred frees, so that we
6595 * can keep this TXG as a no-op.
6597 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6599 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6600 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6603 spa_sync_deferred_frees(spa
, tx
);
6606 } while (dmu_objset_is_dirty(mos
, txg
));
6608 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
6610 * Make sure that the number of ZAPs for all the vdevs matches
6611 * the number of ZAPs in the per-vdev ZAP list. This only gets
6612 * called if the config is dirty; otherwise there may be
6613 * outstanding AVZ operations that weren't completed in
6614 * spa_sync_config_object.
6616 uint64_t all_vdev_zap_entry_count
;
6617 ASSERT0(zap_count(spa
->spa_meta_objset
,
6618 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
6619 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
6620 all_vdev_zap_entry_count
);
6623 if (spa
->spa_vdev_removal
!= NULL
) {
6624 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
6628 * Rewrite the vdev configuration (which includes the uberblock)
6629 * to commit the transaction group.
6631 * If there are no dirty vdevs, we sync the uberblock to a few
6632 * random top-level vdevs that are known to be visible in the
6633 * config cache (see spa_vdev_add() for a complete description).
6634 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6638 * We hold SCL_STATE to prevent vdev open/close/etc.
6639 * while we're attempting to write the vdev labels.
6641 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6643 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6644 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6646 int children
= rvd
->vdev_children
;
6647 int c0
= spa_get_random(children
);
6649 for (int c
= 0; c
< children
; c
++) {
6650 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6651 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
6652 !vdev_is_concrete(vd
))
6654 svd
[svdcount
++] = vd
;
6655 if (svdcount
== SPA_DVAS_PER_BP
)
6658 error
= vdev_config_sync(svd
, svdcount
, txg
);
6660 error
= vdev_config_sync(rvd
->vdev_child
,
6661 rvd
->vdev_children
, txg
);
6665 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6667 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6671 zio_suspend(spa
, NULL
);
6672 zio_resume_wait(spa
);
6676 VERIFY(cyclic_reprogram(spa
->spa_deadman_cycid
, CY_INFINITY
));
6679 * Clear the dirty config list.
6681 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6682 vdev_config_clean(vd
);
6685 * Now that the new config has synced transactionally,
6686 * let it become visible to the config cache.
6688 if (spa
->spa_config_syncing
!= NULL
) {
6689 spa_config_set(spa
, spa
->spa_config_syncing
);
6690 spa
->spa_config_txg
= txg
;
6691 spa
->spa_config_syncing
= NULL
;
6694 dsl_pool_sync_done(dp
, txg
);
6696 mutex_enter(&spa
->spa_alloc_lock
);
6697 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6698 mutex_exit(&spa
->spa_alloc_lock
);
6701 * Update usable space statistics.
6703 while (vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
6704 vdev_sync_done(vd
, txg
);
6706 spa_update_dspace(spa
);
6709 * It had better be the case that we didn't dirty anything
6710 * since vdev_config_sync().
6712 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6713 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6714 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6716 spa
->spa_sync_pass
= 0;
6719 * Update the last synced uberblock here. We want to do this at
6720 * the end of spa_sync() so that consumers of spa_last_synced_txg()
6721 * will be guaranteed that all the processing associated with
6722 * that txg has been completed.
6724 spa
->spa_ubsync
= spa
->spa_uberblock
;
6725 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6727 spa_handle_ignored_writes(spa
);
6730 * If any async tasks have been requested, kick them off.
6732 spa_async_dispatch(spa
);
6736 * Sync all pools. We don't want to hold the namespace lock across these
6737 * operations, so we take a reference on the spa_t and drop the lock during the
6741 spa_sync_allpools(void)
6744 mutex_enter(&spa_namespace_lock
);
6745 while ((spa
= spa_next(spa
)) != NULL
) {
6746 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6747 !spa_writeable(spa
) || spa_suspended(spa
))
6749 spa_open_ref(spa
, FTAG
);
6750 mutex_exit(&spa_namespace_lock
);
6751 txg_wait_synced(spa_get_dsl(spa
), 0);
6752 mutex_enter(&spa_namespace_lock
);
6753 spa_close(spa
, FTAG
);
6755 mutex_exit(&spa_namespace_lock
);
6759 * ==========================================================================
6760 * Miscellaneous routines
6761 * ==========================================================================
6765 * Remove all pools in the system.
6773 * Remove all cached state. All pools should be closed now,
6774 * so every spa in the AVL tree should be unreferenced.
6776 mutex_enter(&spa_namespace_lock
);
6777 while ((spa
= spa_next(NULL
)) != NULL
) {
6779 * Stop async tasks. The async thread may need to detach
6780 * a device that's been replaced, which requires grabbing
6781 * spa_namespace_lock, so we must drop it here.
6783 spa_open_ref(spa
, FTAG
);
6784 mutex_exit(&spa_namespace_lock
);
6785 spa_async_suspend(spa
);
6786 mutex_enter(&spa_namespace_lock
);
6787 spa_close(spa
, FTAG
);
6789 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6791 spa_deactivate(spa
);
6795 mutex_exit(&spa_namespace_lock
);
6799 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6804 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6808 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6809 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6810 if (vd
->vdev_guid
== guid
)
6814 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6815 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6816 if (vd
->vdev_guid
== guid
)
6825 spa_upgrade(spa_t
*spa
, uint64_t version
)
6827 ASSERT(spa_writeable(spa
));
6829 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6832 * This should only be called for a non-faulted pool, and since a
6833 * future version would result in an unopenable pool, this shouldn't be
6836 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6837 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6839 spa
->spa_uberblock
.ub_version
= version
;
6840 vdev_config_dirty(spa
->spa_root_vdev
);
6842 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6844 txg_wait_synced(spa_get_dsl(spa
), 0);
6848 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6852 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6854 for (i
= 0; i
< sav
->sav_count
; i
++)
6855 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6858 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6859 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6860 &spareguid
) == 0 && spareguid
== guid
)
6868 * Check if a pool has an active shared spare device.
6869 * Note: reference count of an active spare is 2, as a spare and as a replace
6872 spa_has_active_shared_spare(spa_t
*spa
)
6876 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6878 for (i
= 0; i
< sav
->sav_count
; i
++) {
6879 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6880 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6889 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
6891 sysevent_t
*ev
= NULL
;
6893 sysevent_attr_list_t
*attr
= NULL
;
6894 sysevent_value_t value
;
6896 ev
= sysevent_alloc(EC_ZFS
, (char *)name
, SUNW_KERN_PUB
"zfs",
6900 value
.value_type
= SE_DATA_TYPE_STRING
;
6901 value
.value
.sv_string
= spa_name(spa
);
6902 if (sysevent_add_attr(&attr
, ZFS_EV_POOL_NAME
, &value
, SE_SLEEP
) != 0)
6905 value
.value_type
= SE_DATA_TYPE_UINT64
;
6906 value
.value
.sv_uint64
= spa_guid(spa
);
6907 if (sysevent_add_attr(&attr
, ZFS_EV_POOL_GUID
, &value
, SE_SLEEP
) != 0)
6911 value
.value_type
= SE_DATA_TYPE_UINT64
;
6912 value
.value
.sv_uint64
= vd
->vdev_guid
;
6913 if (sysevent_add_attr(&attr
, ZFS_EV_VDEV_GUID
, &value
,
6917 if (vd
->vdev_path
) {
6918 value
.value_type
= SE_DATA_TYPE_STRING
;
6919 value
.value
.sv_string
= vd
->vdev_path
;
6920 if (sysevent_add_attr(&attr
, ZFS_EV_VDEV_PATH
,
6921 &value
, SE_SLEEP
) != 0)
6926 if (hist_nvl
!= NULL
) {
6927 fnvlist_merge((nvlist_t
*)attr
, hist_nvl
);
6930 if (sysevent_attach_attributes(ev
, attr
) != 0)
6936 sysevent_free_attr(attr
);
6943 spa_event_post(sysevent_t
*ev
)
6948 (void) log_sysevent(ev
, SE_SLEEP
, &eid
);
6954 spa_event_discard(sysevent_t
*ev
)
6962 * Post a sysevent corresponding to the given event. The 'name' must be one of
6963 * the event definitions in sys/sysevent/eventdefs.h. The payload will be
6964 * filled in from the spa and (optionally) the vdev and history nvl. This
6965 * doesn't do anything in the userland libzpool, as we don't want consumers to
6966 * misinterpret ztest or zdb as real changes.
6969 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
6971 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));