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, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright 2013 Saso Kiselkov. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Toomas Soome <tsoome@me.com>
31 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
32 * Copyright (c) 2017 Datto Inc.
33 * Copyright 2017 Joyent, Inc.
37 * SPA: Storage Pool Allocator
39 * This file contains all the routines used when modifying on-disk SPA state.
40 * This includes opening, importing, destroying, exporting a pool, and syncing a
44 #include <sys/zfs_context.h>
45 #include <sys/fm/fs/zfs.h>
46 #include <sys/spa_impl.h>
48 #include <sys/zio_checksum.h>
50 #include <sys/dmu_tx.h>
54 #include <sys/vdev_impl.h>
55 #include <sys/vdev_removal.h>
56 #include <sys/vdev_indirect_mapping.h>
57 #include <sys/vdev_indirect_births.h>
58 #include <sys/vdev_disk.h>
59 #include <sys/metaslab.h>
60 #include <sys/metaslab_impl.h>
62 #include <sys/uberblock_impl.h>
65 #include <sys/bpobj.h>
66 #include <sys/dmu_traverse.h>
67 #include <sys/dmu_objset.h>
68 #include <sys/unique.h>
69 #include <sys/dsl_pool.h>
70 #include <sys/dsl_dataset.h>
71 #include <sys/dsl_dir.h>
72 #include <sys/dsl_prop.h>
73 #include <sys/dsl_synctask.h>
74 #include <sys/fs/zfs.h>
76 #include <sys/callb.h>
77 #include <sys/systeminfo.h>
78 #include <sys/spa_boot.h>
79 #include <sys/zfs_ioctl.h>
80 #include <sys/dsl_scan.h>
81 #include <sys/zfeature.h>
82 #include <sys/dsl_destroy.h>
86 #include <sys/fm/protocol.h>
87 #include <sys/fm/util.h>
88 #include <sys/bootprops.h>
89 #include <sys/callb.h>
90 #include <sys/cpupart.h>
92 #include <sys/sysdc.h>
97 #include "zfs_comutil.h"
100 * The interval, in seconds, at which failed configuration cache file writes
103 int zfs_ccw_retry_interval
= 300;
105 typedef enum zti_modes
{
106 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
107 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
108 ZTI_MODE_NULL
, /* don't create a taskq */
112 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
113 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
114 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
115 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
117 #define ZTI_N(n) ZTI_P(n, 1)
118 #define ZTI_ONE ZTI_N(1)
120 typedef struct zio_taskq_info
{
121 zti_modes_t zti_mode
;
126 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
127 "iss", "iss_h", "int", "int_h"
131 * This table defines the taskq settings for each ZFS I/O type. When
132 * initializing a pool, we use this table to create an appropriately sized
133 * taskq. Some operations are low volume and therefore have a small, static
134 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
135 * macros. Other operations process a large amount of data; the ZTI_BATCH
136 * macro causes us to create a taskq oriented for throughput. Some operations
137 * are so high frequency and short-lived that the taskq itself can become a a
138 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
139 * additional degree of parallelism specified by the number of threads per-
140 * taskq and the number of taskqs; when dispatching an event in this case, the
141 * particular taskq is chosen at random.
143 * The different taskq priorities are to handle the different contexts (issue
144 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
145 * need to be handled with minimum delay.
147 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
148 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
149 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
150 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
151 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
152 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
153 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
154 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
157 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
158 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
159 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
160 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
161 spa_load_state_t state
, spa_import_type_t type
, boolean_t trust_config
,
163 static void spa_vdev_resilver_done(spa_t
*spa
);
165 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
166 id_t zio_taskq_psrset_bind
= PS_NONE
;
167 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
168 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
170 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
173 * This (illegal) pool name is used when temporarily importing a spa_t in order
174 * to get the vdev stats associated with the imported devices.
176 #define TRYIMPORT_NAME "$import"
179 * ==========================================================================
180 * SPA properties routines
181 * ==========================================================================
185 * Add a (source=src, propname=propval) list to an nvlist.
188 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
189 uint64_t intval
, zprop_source_t src
)
191 const char *propname
= zpool_prop_to_name(prop
);
194 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
195 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
198 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
200 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
202 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
203 nvlist_free(propval
);
207 * Get property values from the spa configuration.
210 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
212 vdev_t
*rvd
= spa
->spa_root_vdev
;
213 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
214 uint64_t size
, alloc
, cap
, version
;
215 const zprop_source_t src
= ZPROP_SRC_NONE
;
216 spa_config_dirent_t
*dp
;
217 metaslab_class_t
*mc
= spa_normal_class(spa
);
219 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
222 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
223 size
= metaslab_class_get_space(spa_normal_class(spa
));
224 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
225 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
226 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
227 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
230 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
231 metaslab_class_fragmentation(mc
), src
);
232 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
233 metaslab_class_expandable_space(mc
), src
);
234 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
235 (spa_mode(spa
) == FREAD
), src
);
237 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
238 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
240 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
241 ddt_get_pool_dedup_ratio(spa
), src
);
243 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
244 rvd
->vdev_state
, src
);
246 version
= spa_version(spa
);
247 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
248 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
249 version
, ZPROP_SRC_DEFAULT
);
251 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
252 version
, ZPROP_SRC_LOCAL
);
258 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
259 * when opening pools before this version freedir will be NULL.
261 if (pool
->dp_free_dir
!= NULL
) {
262 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
263 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
266 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
270 if (pool
->dp_leak_dir
!= NULL
) {
271 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
272 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
275 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
280 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
282 if (spa
->spa_comment
!= NULL
) {
283 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
287 if (spa
->spa_root
!= NULL
)
288 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
291 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
292 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
293 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
295 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
296 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
299 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
300 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
301 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
303 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
304 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
307 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
308 if (dp
->scd_path
== NULL
) {
309 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
310 "none", 0, ZPROP_SRC_LOCAL
);
311 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
312 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
313 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
319 * Get zpool property values.
322 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
324 objset_t
*mos
= spa
->spa_meta_objset
;
329 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
333 mutex_enter(&spa
->spa_props_lock
);
336 * Get properties from the spa config.
338 spa_prop_get_config(spa
, nvp
);
340 /* If no pool property object, no more prop to get. */
341 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
342 mutex_exit(&spa
->spa_props_lock
);
347 * Get properties from the MOS pool property object.
349 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
350 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
351 zap_cursor_advance(&zc
)) {
354 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
357 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPOOL_PROP_INVAL
)
360 switch (za
.za_integer_length
) {
362 /* integer property */
363 if (za
.za_first_integer
!=
364 zpool_prop_default_numeric(prop
))
365 src
= ZPROP_SRC_LOCAL
;
367 if (prop
== ZPOOL_PROP_BOOTFS
) {
369 dsl_dataset_t
*ds
= NULL
;
371 dp
= spa_get_dsl(spa
);
372 dsl_pool_config_enter(dp
, FTAG
);
373 if ((err
= dsl_dataset_hold_obj(dp
,
374 za
.za_first_integer
, FTAG
, &ds
))) {
375 dsl_pool_config_exit(dp
, FTAG
);
379 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
381 dsl_dataset_name(ds
, strval
);
382 dsl_dataset_rele(ds
, FTAG
);
383 dsl_pool_config_exit(dp
, FTAG
);
386 intval
= za
.za_first_integer
;
389 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
392 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
397 /* string property */
398 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
399 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
400 za
.za_name
, 1, za
.za_num_integers
, strval
);
402 kmem_free(strval
, za
.za_num_integers
);
405 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
406 kmem_free(strval
, za
.za_num_integers
);
413 zap_cursor_fini(&zc
);
414 mutex_exit(&spa
->spa_props_lock
);
416 if (err
&& err
!= ENOENT
) {
426 * Validate the given pool properties nvlist and modify the list
427 * for the property values to be set.
430 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
433 int error
= 0, reset_bootfs
= 0;
435 boolean_t has_feature
= B_FALSE
;
438 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
440 char *strval
, *slash
, *check
, *fname
;
441 const char *propname
= nvpair_name(elem
);
442 zpool_prop_t prop
= zpool_name_to_prop(propname
);
445 case ZPOOL_PROP_INVAL
:
446 if (!zpool_prop_feature(propname
)) {
447 error
= SET_ERROR(EINVAL
);
452 * Sanitize the input.
454 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
455 error
= SET_ERROR(EINVAL
);
459 if (nvpair_value_uint64(elem
, &intval
) != 0) {
460 error
= SET_ERROR(EINVAL
);
465 error
= SET_ERROR(EINVAL
);
469 fname
= strchr(propname
, '@') + 1;
470 if (zfeature_lookup_name(fname
, NULL
) != 0) {
471 error
= SET_ERROR(EINVAL
);
475 has_feature
= B_TRUE
;
478 case ZPOOL_PROP_VERSION
:
479 error
= nvpair_value_uint64(elem
, &intval
);
481 (intval
< spa_version(spa
) ||
482 intval
> SPA_VERSION_BEFORE_FEATURES
||
484 error
= SET_ERROR(EINVAL
);
487 case ZPOOL_PROP_DELEGATION
:
488 case ZPOOL_PROP_AUTOREPLACE
:
489 case ZPOOL_PROP_LISTSNAPS
:
490 case ZPOOL_PROP_AUTOEXPAND
:
491 error
= nvpair_value_uint64(elem
, &intval
);
492 if (!error
&& intval
> 1)
493 error
= SET_ERROR(EINVAL
);
496 case ZPOOL_PROP_MULTIHOST
:
497 error
= nvpair_value_uint64(elem
, &intval
);
498 if (!error
&& intval
> 1)
499 error
= SET_ERROR(EINVAL
);
501 if (!error
&& !spa_get_hostid())
502 error
= SET_ERROR(ENOTSUP
);
506 case ZPOOL_PROP_BOOTFS
:
508 * If the pool version is less than SPA_VERSION_BOOTFS,
509 * or the pool is still being created (version == 0),
510 * the bootfs property cannot be set.
512 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
513 error
= SET_ERROR(ENOTSUP
);
518 * Make sure the vdev config is bootable
520 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
521 error
= SET_ERROR(ENOTSUP
);
527 error
= nvpair_value_string(elem
, &strval
);
533 if (strval
== NULL
|| strval
[0] == '\0') {
534 objnum
= zpool_prop_default_numeric(
539 error
= dmu_objset_hold(strval
, FTAG
, &os
);
544 * Must be ZPL, and its property settings
545 * must be supported by GRUB (compression
546 * is not gzip, and large blocks or large
547 * dnodes are not used).
550 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
551 error
= SET_ERROR(ENOTSUP
);
553 dsl_prop_get_int_ds(dmu_objset_ds(os
),
554 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
556 !BOOTFS_COMPRESS_VALID(propval
)) {
557 error
= SET_ERROR(ENOTSUP
);
559 dsl_prop_get_int_ds(dmu_objset_ds(os
),
560 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
562 propval
!= ZFS_DNSIZE_LEGACY
) {
563 error
= SET_ERROR(ENOTSUP
);
565 objnum
= dmu_objset_id(os
);
567 dmu_objset_rele(os
, FTAG
);
571 case ZPOOL_PROP_FAILUREMODE
:
572 error
= nvpair_value_uint64(elem
, &intval
);
573 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
574 error
= SET_ERROR(EINVAL
);
577 * This is a special case which only occurs when
578 * the pool has completely failed. This allows
579 * the user to change the in-core failmode property
580 * without syncing it out to disk (I/Os might
581 * currently be blocked). We do this by returning
582 * EIO to the caller (spa_prop_set) to trick it
583 * into thinking we encountered a property validation
586 if (!error
&& spa_suspended(spa
)) {
587 spa
->spa_failmode
= intval
;
588 error
= SET_ERROR(EIO
);
592 case ZPOOL_PROP_CACHEFILE
:
593 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
596 if (strval
[0] == '\0')
599 if (strcmp(strval
, "none") == 0)
602 if (strval
[0] != '/') {
603 error
= SET_ERROR(EINVAL
);
607 slash
= strrchr(strval
, '/');
608 ASSERT(slash
!= NULL
);
610 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
611 strcmp(slash
, "/..") == 0)
612 error
= SET_ERROR(EINVAL
);
615 case ZPOOL_PROP_COMMENT
:
616 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
618 for (check
= strval
; *check
!= '\0'; check
++) {
619 if (!isprint(*check
)) {
620 error
= SET_ERROR(EINVAL
);
624 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
625 error
= SET_ERROR(E2BIG
);
628 case ZPOOL_PROP_DEDUPDITTO
:
629 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
630 error
= SET_ERROR(ENOTSUP
);
632 error
= nvpair_value_uint64(elem
, &intval
);
634 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
635 error
= SET_ERROR(EINVAL
);
646 if (!error
&& reset_bootfs
) {
647 error
= nvlist_remove(props
,
648 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
651 error
= nvlist_add_uint64(props
,
652 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
660 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
663 spa_config_dirent_t
*dp
;
665 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
669 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
672 if (cachefile
[0] == '\0')
673 dp
->scd_path
= spa_strdup(spa_config_path
);
674 else if (strcmp(cachefile
, "none") == 0)
677 dp
->scd_path
= spa_strdup(cachefile
);
679 list_insert_head(&spa
->spa_config_list
, dp
);
681 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
685 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
688 nvpair_t
*elem
= NULL
;
689 boolean_t need_sync
= B_FALSE
;
691 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
694 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
695 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
697 if (prop
== ZPOOL_PROP_CACHEFILE
||
698 prop
== ZPOOL_PROP_ALTROOT
||
699 prop
== ZPOOL_PROP_READONLY
)
702 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
705 if (prop
== ZPOOL_PROP_VERSION
) {
706 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
708 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
709 ver
= SPA_VERSION_FEATURES
;
713 /* Save time if the version is already set. */
714 if (ver
== spa_version(spa
))
718 * In addition to the pool directory object, we might
719 * create the pool properties object, the features for
720 * read object, the features for write object, or the
721 * feature descriptions object.
723 error
= dsl_sync_task(spa
->spa_name
, NULL
,
724 spa_sync_version
, &ver
,
725 6, ZFS_SPACE_CHECK_RESERVED
);
736 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
737 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
744 * If the bootfs property value is dsobj, clear it.
747 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
749 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
750 VERIFY(zap_remove(spa
->spa_meta_objset
,
751 spa
->spa_pool_props_object
,
752 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
759 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
761 ASSERTV(uint64_t *newguid
= arg
);
762 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
763 vdev_t
*rvd
= spa
->spa_root_vdev
;
766 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
767 vdev_state
= rvd
->vdev_state
;
768 spa_config_exit(spa
, SCL_STATE
, FTAG
);
770 if (vdev_state
!= VDEV_STATE_HEALTHY
)
771 return (SET_ERROR(ENXIO
));
773 ASSERT3U(spa_guid(spa
), !=, *newguid
);
779 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
781 uint64_t *newguid
= arg
;
782 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
784 vdev_t
*rvd
= spa
->spa_root_vdev
;
786 oldguid
= spa_guid(spa
);
788 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
789 rvd
->vdev_guid
= *newguid
;
790 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
791 vdev_config_dirty(rvd
);
792 spa_config_exit(spa
, SCL_STATE
, FTAG
);
794 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
799 * Change the GUID for the pool. This is done so that we can later
800 * re-import a pool built from a clone of our own vdevs. We will modify
801 * the root vdev's guid, our own pool guid, and then mark all of our
802 * vdevs dirty. Note that we must make sure that all our vdevs are
803 * online when we do this, or else any vdevs that weren't present
804 * would be orphaned from our pool. We are also going to issue a
805 * sysevent to update any watchers.
808 spa_change_guid(spa_t
*spa
)
813 mutex_enter(&spa
->spa_vdev_top_lock
);
814 mutex_enter(&spa_namespace_lock
);
815 guid
= spa_generate_guid(NULL
);
817 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
818 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
821 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
822 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
825 mutex_exit(&spa_namespace_lock
);
826 mutex_exit(&spa
->spa_vdev_top_lock
);
832 * ==========================================================================
833 * SPA state manipulation (open/create/destroy/import/export)
834 * ==========================================================================
838 spa_error_entry_compare(const void *a
, const void *b
)
840 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
841 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
844 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
845 sizeof (zbookmark_phys_t
));
847 return (AVL_ISIGN(ret
));
851 * Utility function which retrieves copies of the current logs and
852 * re-initializes them in the process.
855 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
857 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
859 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
860 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
862 avl_create(&spa
->spa_errlist_scrub
,
863 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
864 offsetof(spa_error_entry_t
, se_avl
));
865 avl_create(&spa
->spa_errlist_last
,
866 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
867 offsetof(spa_error_entry_t
, se_avl
));
871 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
873 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
874 enum zti_modes mode
= ztip
->zti_mode
;
875 uint_t value
= ztip
->zti_value
;
876 uint_t count
= ztip
->zti_count
;
877 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
880 boolean_t batch
= B_FALSE
;
882 if (mode
== ZTI_MODE_NULL
) {
884 tqs
->stqs_taskq
= NULL
;
888 ASSERT3U(count
, >, 0);
890 tqs
->stqs_count
= count
;
891 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
895 ASSERT3U(value
, >=, 1);
896 value
= MAX(value
, 1);
897 flags
|= TASKQ_DYNAMIC
;
902 flags
|= TASKQ_THREADS_CPU_PCT
;
903 value
= MIN(zio_taskq_batch_pct
, 100);
907 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
909 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
913 for (uint_t i
= 0; i
< count
; i
++) {
917 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
918 zio_type_name
[t
], zio_taskq_types
[q
], i
);
920 (void) snprintf(name
, sizeof (name
), "%s_%s",
921 zio_type_name
[t
], zio_taskq_types
[q
]);
924 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
926 flags
|= TASKQ_DC_BATCH
;
928 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
929 spa
->spa_proc
, zio_taskq_basedc
, flags
);
931 pri_t pri
= maxclsyspri
;
933 * The write issue taskq can be extremely CPU
934 * intensive. Run it at slightly less important
935 * priority than the other taskqs. Under Linux this
936 * means incrementing the priority value on platforms
937 * like illumos it should be decremented.
939 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
942 tq
= taskq_create_proc(name
, value
, pri
, 50,
943 INT_MAX
, spa
->spa_proc
, flags
);
946 tqs
->stqs_taskq
[i
] = tq
;
951 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
953 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
955 if (tqs
->stqs_taskq
== NULL
) {
956 ASSERT3U(tqs
->stqs_count
, ==, 0);
960 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
961 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
962 taskq_destroy(tqs
->stqs_taskq
[i
]);
965 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
966 tqs
->stqs_taskq
= NULL
;
970 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
971 * Note that a type may have multiple discrete taskqs to avoid lock contention
972 * on the taskq itself. In that case we choose which taskq at random by using
973 * the low bits of gethrtime().
976 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
977 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
979 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
982 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
983 ASSERT3U(tqs
->stqs_count
, !=, 0);
985 if (tqs
->stqs_count
== 1) {
986 tq
= tqs
->stqs_taskq
[0];
988 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
991 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
995 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
998 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
999 task_func_t
*func
, void *arg
, uint_t flags
)
1001 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1005 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1006 ASSERT3U(tqs
->stqs_count
, !=, 0);
1008 if (tqs
->stqs_count
== 1) {
1009 tq
= tqs
->stqs_taskq
[0];
1011 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1014 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1016 taskq_wait_id(tq
, id
);
1020 spa_create_zio_taskqs(spa_t
*spa
)
1022 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1023 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1024 spa_taskqs_init(spa
, t
, q
);
1030 * Disabled until spa_thread() can be adapted for Linux.
1032 #undef HAVE_SPA_THREAD
1034 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1036 spa_thread(void *arg
)
1038 callb_cpr_t cprinfo
;
1041 user_t
*pu
= PTOU(curproc
);
1043 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1046 ASSERT(curproc
!= &p0
);
1047 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1048 "zpool-%s", spa
->spa_name
);
1049 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1051 /* bind this thread to the requested psrset */
1052 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1054 mutex_enter(&cpu_lock
);
1055 mutex_enter(&pidlock
);
1056 mutex_enter(&curproc
->p_lock
);
1058 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1059 0, NULL
, NULL
) == 0) {
1060 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1063 "Couldn't bind process for zfs pool \"%s\" to "
1064 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1067 mutex_exit(&curproc
->p_lock
);
1068 mutex_exit(&pidlock
);
1069 mutex_exit(&cpu_lock
);
1073 if (zio_taskq_sysdc
) {
1074 sysdc_thread_enter(curthread
, 100, 0);
1077 spa
->spa_proc
= curproc
;
1078 spa
->spa_did
= curthread
->t_did
;
1080 spa_create_zio_taskqs(spa
);
1082 mutex_enter(&spa
->spa_proc_lock
);
1083 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1085 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1086 cv_broadcast(&spa
->spa_proc_cv
);
1088 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1089 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1090 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1091 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1093 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1094 spa
->spa_proc_state
= SPA_PROC_GONE
;
1095 spa
->spa_proc
= &p0
;
1096 cv_broadcast(&spa
->spa_proc_cv
);
1097 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1099 mutex_enter(&curproc
->p_lock
);
1105 * Activate an uninitialized pool.
1108 spa_activate(spa_t
*spa
, int mode
)
1110 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1112 spa
->spa_state
= POOL_STATE_ACTIVE
;
1113 spa
->spa_mode
= mode
;
1115 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1116 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1118 /* Try to create a covering process */
1119 mutex_enter(&spa
->spa_proc_lock
);
1120 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1121 ASSERT(spa
->spa_proc
== &p0
);
1124 #ifdef HAVE_SPA_THREAD
1125 /* Only create a process if we're going to be around a while. */
1126 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1127 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1129 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1130 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1131 cv_wait(&spa
->spa_proc_cv
,
1132 &spa
->spa_proc_lock
);
1134 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1135 ASSERT(spa
->spa_proc
!= &p0
);
1136 ASSERT(spa
->spa_did
!= 0);
1140 "Couldn't create process for zfs pool \"%s\"\n",
1145 #endif /* HAVE_SPA_THREAD */
1146 mutex_exit(&spa
->spa_proc_lock
);
1148 /* If we didn't create a process, we need to create our taskqs. */
1149 if (spa
->spa_proc
== &p0
) {
1150 spa_create_zio_taskqs(spa
);
1153 for (size_t i
= 0; i
< TXG_SIZE
; i
++)
1154 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
, 0);
1156 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1157 offsetof(vdev_t
, vdev_config_dirty_node
));
1158 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1159 offsetof(objset_t
, os_evicting_node
));
1160 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1161 offsetof(vdev_t
, vdev_state_dirty_node
));
1163 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1164 offsetof(struct vdev
, vdev_txg_node
));
1166 avl_create(&spa
->spa_errlist_scrub
,
1167 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1168 offsetof(spa_error_entry_t
, se_avl
));
1169 avl_create(&spa
->spa_errlist_last
,
1170 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1171 offsetof(spa_error_entry_t
, se_avl
));
1173 spa_keystore_init(&spa
->spa_keystore
);
1176 * This taskq is used to perform zvol-minor-related tasks
1177 * asynchronously. This has several advantages, including easy
1178 * resolution of various deadlocks (zfsonlinux bug #3681).
1180 * The taskq must be single threaded to ensure tasks are always
1181 * processed in the order in which they were dispatched.
1183 * A taskq per pool allows one to keep the pools independent.
1184 * This way if one pool is suspended, it will not impact another.
1186 * The preferred location to dispatch a zvol minor task is a sync
1187 * task. In this context, there is easy access to the spa_t and minimal
1188 * error handling is required because the sync task must succeed.
1190 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1194 * Taskq dedicated to prefetcher threads: this is used to prevent the
1195 * pool traverse code from monopolizing the global (and limited)
1196 * system_taskq by inappropriately scheduling long running tasks on it.
1198 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", boot_ncpus
,
1199 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1202 * The taskq to upgrade datasets in this pool. Currently used by
1203 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1205 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", boot_ncpus
,
1206 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
);
1210 * Opposite of spa_activate().
1213 spa_deactivate(spa_t
*spa
)
1215 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1216 ASSERT(spa
->spa_dsl_pool
== NULL
);
1217 ASSERT(spa
->spa_root_vdev
== NULL
);
1218 ASSERT(spa
->spa_async_zio_root
== NULL
);
1219 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1221 spa_evicting_os_wait(spa
);
1223 if (spa
->spa_zvol_taskq
) {
1224 taskq_destroy(spa
->spa_zvol_taskq
);
1225 spa
->spa_zvol_taskq
= NULL
;
1228 if (spa
->spa_prefetch_taskq
) {
1229 taskq_destroy(spa
->spa_prefetch_taskq
);
1230 spa
->spa_prefetch_taskq
= NULL
;
1233 if (spa
->spa_upgrade_taskq
) {
1234 taskq_destroy(spa
->spa_upgrade_taskq
);
1235 spa
->spa_upgrade_taskq
= NULL
;
1238 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1240 list_destroy(&spa
->spa_config_dirty_list
);
1241 list_destroy(&spa
->spa_evicting_os_list
);
1242 list_destroy(&spa
->spa_state_dirty_list
);
1244 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1246 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1247 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1248 spa_taskqs_fini(spa
, t
, q
);
1252 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1253 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1254 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1255 spa
->spa_txg_zio
[i
] = NULL
;
1258 metaslab_class_destroy(spa
->spa_normal_class
);
1259 spa
->spa_normal_class
= NULL
;
1261 metaslab_class_destroy(spa
->spa_log_class
);
1262 spa
->spa_log_class
= NULL
;
1265 * If this was part of an import or the open otherwise failed, we may
1266 * still have errors left in the queues. Empty them just in case.
1268 spa_errlog_drain(spa
);
1269 avl_destroy(&spa
->spa_errlist_scrub
);
1270 avl_destroy(&spa
->spa_errlist_last
);
1272 spa_keystore_fini(&spa
->spa_keystore
);
1274 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1276 mutex_enter(&spa
->spa_proc_lock
);
1277 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1278 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1279 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1280 cv_broadcast(&spa
->spa_proc_cv
);
1281 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1282 ASSERT(spa
->spa_proc
!= &p0
);
1283 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1285 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1286 spa
->spa_proc_state
= SPA_PROC_NONE
;
1288 ASSERT(spa
->spa_proc
== &p0
);
1289 mutex_exit(&spa
->spa_proc_lock
);
1292 * We want to make sure spa_thread() has actually exited the ZFS
1293 * module, so that the module can't be unloaded out from underneath
1296 if (spa
->spa_did
!= 0) {
1297 thread_join(spa
->spa_did
);
1303 * Verify a pool configuration, and construct the vdev tree appropriately. This
1304 * will create all the necessary vdevs in the appropriate layout, with each vdev
1305 * in the CLOSED state. This will prep the pool before open/creation/import.
1306 * All vdev validation is done by the vdev_alloc() routine.
1309 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1310 uint_t id
, int atype
)
1316 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1319 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1322 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1325 if (error
== ENOENT
)
1331 return (SET_ERROR(EINVAL
));
1334 for (int c
= 0; c
< children
; c
++) {
1336 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1344 ASSERT(*vdp
!= NULL
);
1350 * Opposite of spa_load().
1353 spa_unload(spa_t
*spa
)
1357 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1362 spa_async_suspend(spa
);
1367 if (spa
->spa_sync_on
) {
1368 txg_sync_stop(spa
->spa_dsl_pool
);
1369 spa
->spa_sync_on
= B_FALSE
;
1373 * Even though vdev_free() also calls vdev_metaslab_fini, we need
1374 * to call it earlier, before we wait for async i/o to complete.
1375 * This ensures that there is no async metaslab prefetching, by
1376 * calling taskq_wait(mg_taskq).
1378 if (spa
->spa_root_vdev
!= NULL
) {
1379 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1380 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++)
1381 vdev_metaslab_fini(spa
->spa_root_vdev
->vdev_child
[c
]);
1382 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1385 if (spa
->spa_mmp
.mmp_thread
)
1386 mmp_thread_stop(spa
);
1389 * Wait for any outstanding async I/O to complete.
1391 if (spa
->spa_async_zio_root
!= NULL
) {
1392 for (int i
= 0; i
< max_ncpus
; i
++)
1393 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1394 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1395 spa
->spa_async_zio_root
= NULL
;
1398 if (spa
->spa_vdev_removal
!= NULL
) {
1399 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1400 spa
->spa_vdev_removal
= NULL
;
1403 spa_condense_fini(spa
);
1405 bpobj_close(&spa
->spa_deferred_bpobj
);
1407 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1412 if (spa
->spa_root_vdev
)
1413 vdev_free(spa
->spa_root_vdev
);
1414 ASSERT(spa
->spa_root_vdev
== NULL
);
1417 * Close the dsl pool.
1419 if (spa
->spa_dsl_pool
) {
1420 dsl_pool_close(spa
->spa_dsl_pool
);
1421 spa
->spa_dsl_pool
= NULL
;
1422 spa
->spa_meta_objset
= NULL
;
1428 * Drop and purge level 2 cache
1430 spa_l2cache_drop(spa
);
1432 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1433 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1434 if (spa
->spa_spares
.sav_vdevs
) {
1435 kmem_free(spa
->spa_spares
.sav_vdevs
,
1436 spa
->spa_spares
.sav_count
* sizeof (void *));
1437 spa
->spa_spares
.sav_vdevs
= NULL
;
1439 if (spa
->spa_spares
.sav_config
) {
1440 nvlist_free(spa
->spa_spares
.sav_config
);
1441 spa
->spa_spares
.sav_config
= NULL
;
1443 spa
->spa_spares
.sav_count
= 0;
1445 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1446 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1447 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1449 if (spa
->spa_l2cache
.sav_vdevs
) {
1450 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1451 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1452 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1454 if (spa
->spa_l2cache
.sav_config
) {
1455 nvlist_free(spa
->spa_l2cache
.sav_config
);
1456 spa
->spa_l2cache
.sav_config
= NULL
;
1458 spa
->spa_l2cache
.sav_count
= 0;
1460 spa
->spa_async_suspended
= 0;
1462 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1464 if (spa
->spa_comment
!= NULL
) {
1465 spa_strfree(spa
->spa_comment
);
1466 spa
->spa_comment
= NULL
;
1469 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1473 * Load (or re-load) the current list of vdevs describing the active spares for
1474 * this pool. When this is called, we have some form of basic information in
1475 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1476 * then re-generate a more complete list including status information.
1479 spa_load_spares(spa_t
*spa
)
1486 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1489 * First, close and free any existing spare vdevs.
1491 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1492 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1494 /* Undo the call to spa_activate() below */
1495 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1496 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1497 spa_spare_remove(tvd
);
1502 if (spa
->spa_spares
.sav_vdevs
)
1503 kmem_free(spa
->spa_spares
.sav_vdevs
,
1504 spa
->spa_spares
.sav_count
* sizeof (void *));
1506 if (spa
->spa_spares
.sav_config
== NULL
)
1509 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1510 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1512 spa
->spa_spares
.sav_count
= (int)nspares
;
1513 spa
->spa_spares
.sav_vdevs
= NULL
;
1519 * Construct the array of vdevs, opening them to get status in the
1520 * process. For each spare, there is potentially two different vdev_t
1521 * structures associated with it: one in the list of spares (used only
1522 * for basic validation purposes) and one in the active vdev
1523 * configuration (if it's spared in). During this phase we open and
1524 * validate each vdev on the spare list. If the vdev also exists in the
1525 * active configuration, then we also mark this vdev as an active spare.
1527 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1529 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1530 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1531 VDEV_ALLOC_SPARE
) == 0);
1534 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1536 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1537 B_FALSE
)) != NULL
) {
1538 if (!tvd
->vdev_isspare
)
1542 * We only mark the spare active if we were successfully
1543 * able to load the vdev. Otherwise, importing a pool
1544 * with a bad active spare would result in strange
1545 * behavior, because multiple pool would think the spare
1546 * is actively in use.
1548 * There is a vulnerability here to an equally bizarre
1549 * circumstance, where a dead active spare is later
1550 * brought back to life (onlined or otherwise). Given
1551 * the rarity of this scenario, and the extra complexity
1552 * it adds, we ignore the possibility.
1554 if (!vdev_is_dead(tvd
))
1555 spa_spare_activate(tvd
);
1559 vd
->vdev_aux
= &spa
->spa_spares
;
1561 if (vdev_open(vd
) != 0)
1564 if (vdev_validate_aux(vd
) == 0)
1569 * Recompute the stashed list of spares, with status information
1572 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1573 DATA_TYPE_NVLIST_ARRAY
) == 0);
1575 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1577 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1578 spares
[i
] = vdev_config_generate(spa
,
1579 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1580 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1581 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1582 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1583 nvlist_free(spares
[i
]);
1584 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1588 * Load (or re-load) the current list of vdevs describing the active l2cache for
1589 * this pool. When this is called, we have some form of basic information in
1590 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1591 * then re-generate a more complete list including status information.
1592 * Devices which are already active have their details maintained, and are
1596 spa_load_l2cache(spa_t
*spa
)
1598 nvlist_t
**l2cache
= NULL
;
1600 int i
, j
, oldnvdevs
;
1602 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1603 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1605 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1607 oldvdevs
= sav
->sav_vdevs
;
1608 oldnvdevs
= sav
->sav_count
;
1609 sav
->sav_vdevs
= NULL
;
1612 if (sav
->sav_config
== NULL
) {
1618 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1619 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1620 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1623 * Process new nvlist of vdevs.
1625 for (i
= 0; i
< nl2cache
; i
++) {
1626 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1630 for (j
= 0; j
< oldnvdevs
; j
++) {
1632 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1634 * Retain previous vdev for add/remove ops.
1642 if (newvdevs
[i
] == NULL
) {
1646 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1647 VDEV_ALLOC_L2CACHE
) == 0);
1652 * Commit this vdev as an l2cache device,
1653 * even if it fails to open.
1655 spa_l2cache_add(vd
);
1660 spa_l2cache_activate(vd
);
1662 if (vdev_open(vd
) != 0)
1665 (void) vdev_validate_aux(vd
);
1667 if (!vdev_is_dead(vd
))
1668 l2arc_add_vdev(spa
, vd
);
1672 sav
->sav_vdevs
= newvdevs
;
1673 sav
->sav_count
= (int)nl2cache
;
1676 * Recompute the stashed list of l2cache devices, with status
1677 * information this time.
1679 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1680 DATA_TYPE_NVLIST_ARRAY
) == 0);
1682 if (sav
->sav_count
> 0)
1683 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
1685 for (i
= 0; i
< sav
->sav_count
; i
++)
1686 l2cache
[i
] = vdev_config_generate(spa
,
1687 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1688 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1689 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1693 * Purge vdevs that were dropped
1695 for (i
= 0; i
< oldnvdevs
; i
++) {
1700 ASSERT(vd
->vdev_isl2cache
);
1702 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1703 pool
!= 0ULL && l2arc_vdev_present(vd
))
1704 l2arc_remove_vdev(vd
);
1705 vdev_clear_stats(vd
);
1711 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1713 for (i
= 0; i
< sav
->sav_count
; i
++)
1714 nvlist_free(l2cache
[i
]);
1716 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1720 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1723 char *packed
= NULL
;
1728 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1732 nvsize
= *(uint64_t *)db
->db_data
;
1733 dmu_buf_rele(db
, FTAG
);
1735 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1736 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1739 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1740 vmem_free(packed
, nvsize
);
1746 * Checks to see if the given vdev could not be opened, in which case we post a
1747 * sysevent to notify the autoreplace code that the device has been removed.
1750 spa_check_removed(vdev_t
*vd
)
1752 for (int c
= 0; c
< vd
->vdev_children
; c
++)
1753 spa_check_removed(vd
->vdev_child
[c
]);
1755 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1756 vdev_is_concrete(vd
)) {
1757 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1758 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
1763 spa_config_valid_zaps(vdev_t
*vd
, vdev_t
*mvd
)
1765 ASSERT3U(vd
->vdev_children
, ==, mvd
->vdev_children
);
1767 vd
->vdev_top_zap
= mvd
->vdev_top_zap
;
1768 vd
->vdev_leaf_zap
= mvd
->vdev_leaf_zap
;
1770 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
1771 spa_config_valid_zaps(vd
->vdev_child
[i
], mvd
->vdev_child
[i
]);
1776 * Validate the current config against the MOS config
1779 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1781 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1784 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1786 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1787 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1789 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1792 * If we're doing a normal import, then build up any additional
1793 * diagnostic information about missing devices in this config.
1794 * We'll pass this up to the user for further processing.
1796 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1797 nvlist_t
**child
, *nv
;
1800 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
1802 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1804 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1805 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1806 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1808 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1809 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1811 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1816 VERIFY(nvlist_add_nvlist_array(nv
,
1817 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1818 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1819 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1821 for (int i
= 0; i
< idx
; i
++)
1822 nvlist_free(child
[i
]);
1825 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1829 * Compare the root vdev tree with the information we have
1830 * from the MOS config (mrvd). Check each top-level vdev
1831 * with the corresponding MOS config top-level (mtvd).
1833 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1834 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1835 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1838 * Resolve any "missing" vdevs in the current configuration.
1839 * Also trust the MOS config about any "indirect" vdevs.
1840 * If we find that the MOS config has more accurate information
1841 * about the top-level vdev then use that vdev instead.
1843 if ((tvd
->vdev_ops
== &vdev_missing_ops
&&
1844 mtvd
->vdev_ops
!= &vdev_missing_ops
) ||
1845 (mtvd
->vdev_ops
== &vdev_indirect_ops
&&
1846 tvd
->vdev_ops
!= &vdev_indirect_ops
)) {
1849 * Device specific actions.
1851 if (mtvd
->vdev_islog
) {
1852 if (!(spa
->spa_import_flags
&
1853 ZFS_IMPORT_MISSING_LOG
)) {
1857 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1858 } else if (mtvd
->vdev_ops
!= &vdev_indirect_ops
) {
1863 * Swap the missing vdev with the data we were
1864 * able to obtain from the MOS config.
1866 vdev_remove_child(rvd
, tvd
);
1867 vdev_remove_child(mrvd
, mtvd
);
1869 vdev_add_child(rvd
, mtvd
);
1870 vdev_add_child(mrvd
, tvd
);
1874 if (mtvd
->vdev_islog
) {
1876 * Load the slog device's state from the MOS
1877 * config since it's possible that the label
1878 * does not contain the most up-to-date
1881 vdev_load_log_state(tvd
, mtvd
);
1886 * Per-vdev ZAP info is stored exclusively in the MOS.
1888 spa_config_valid_zaps(tvd
, mtvd
);
1892 * Never trust this info from userland; always use what's
1893 * in the MOS. This prevents it from getting out of sync
1894 * with the rest of the info in the MOS.
1896 tvd
->vdev_removing
= mtvd
->vdev_removing
;
1897 tvd
->vdev_indirect_config
= mtvd
->vdev_indirect_config
;
1901 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1904 * Ensure we were able to validate the config.
1906 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1910 * Check for missing log devices
1913 spa_check_logs(spa_t
*spa
)
1915 boolean_t rv
= B_FALSE
;
1916 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1918 switch (spa
->spa_log_state
) {
1921 case SPA_LOG_MISSING
:
1922 /* need to recheck in case slog has been restored */
1923 case SPA_LOG_UNKNOWN
:
1924 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1925 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1927 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1934 spa_passivate_log(spa_t
*spa
)
1936 vdev_t
*rvd
= spa
->spa_root_vdev
;
1937 boolean_t slog_found
= B_FALSE
;
1939 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1941 if (!spa_has_slogs(spa
))
1944 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1945 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1946 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1948 if (tvd
->vdev_islog
) {
1949 metaslab_group_passivate(mg
);
1950 slog_found
= B_TRUE
;
1954 return (slog_found
);
1958 spa_activate_log(spa_t
*spa
)
1960 vdev_t
*rvd
= spa
->spa_root_vdev
;
1962 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1964 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
1965 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1966 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1968 if (tvd
->vdev_islog
)
1969 metaslab_group_activate(mg
);
1974 spa_reset_logs(spa_t
*spa
)
1978 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
1979 NULL
, DS_FIND_CHILDREN
);
1982 * We successfully offlined the log device, sync out the
1983 * current txg so that the "stubby" block can be removed
1986 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1992 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1994 for (int i
= 0; i
< sav
->sav_count
; i
++)
1995 spa_check_removed(sav
->sav_vdevs
[i
]);
1999 spa_claim_notify(zio_t
*zio
)
2001 spa_t
*spa
= zio
->io_spa
;
2006 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2007 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2008 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2009 mutex_exit(&spa
->spa_props_lock
);
2012 typedef struct spa_load_error
{
2013 uint64_t sle_meta_count
;
2014 uint64_t sle_data_count
;
2018 spa_load_verify_done(zio_t
*zio
)
2020 blkptr_t
*bp
= zio
->io_bp
;
2021 spa_load_error_t
*sle
= zio
->io_private
;
2022 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2023 int error
= zio
->io_error
;
2024 spa_t
*spa
= zio
->io_spa
;
2026 abd_free(zio
->io_abd
);
2028 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2029 type
!= DMU_OT_INTENT_LOG
)
2030 atomic_inc_64(&sle
->sle_meta_count
);
2032 atomic_inc_64(&sle
->sle_data_count
);
2035 mutex_enter(&spa
->spa_scrub_lock
);
2036 spa
->spa_load_verify_ios
--;
2037 cv_broadcast(&spa
->spa_scrub_io_cv
);
2038 mutex_exit(&spa
->spa_scrub_lock
);
2042 * Maximum number of concurrent scrub i/os to create while verifying
2043 * a pool while importing it.
2045 int spa_load_verify_maxinflight
= 10000;
2046 int spa_load_verify_metadata
= B_TRUE
;
2047 int spa_load_verify_data
= B_TRUE
;
2051 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2052 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2054 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
2057 * Note: normally this routine will not be called if
2058 * spa_load_verify_metadata is not set. However, it may be useful
2059 * to manually set the flag after the traversal has begun.
2061 if (!spa_load_verify_metadata
)
2063 if (!BP_IS_METADATA(bp
) && !spa_load_verify_data
)
2067 size_t size
= BP_GET_PSIZE(bp
);
2069 mutex_enter(&spa
->spa_scrub_lock
);
2070 while (spa
->spa_load_verify_ios
>= spa_load_verify_maxinflight
)
2071 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2072 spa
->spa_load_verify_ios
++;
2073 mutex_exit(&spa
->spa_scrub_lock
);
2075 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2076 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2077 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2078 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2084 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2086 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2087 return (SET_ERROR(ENAMETOOLONG
));
2093 spa_load_verify(spa_t
*spa
)
2096 spa_load_error_t sle
= { 0 };
2097 zpool_rewind_policy_t policy
;
2098 boolean_t verify_ok
= B_FALSE
;
2101 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
2103 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
2106 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2107 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2108 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2110 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2114 rio
= zio_root(spa
, NULL
, &sle
,
2115 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2117 if (spa_load_verify_metadata
) {
2118 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2119 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2120 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2123 (void) zio_wait(rio
);
2125 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2126 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2128 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
2129 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
2133 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2134 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2136 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2137 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2138 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2139 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2140 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2141 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2142 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2144 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2148 if (error
!= ENXIO
&& error
!= EIO
)
2149 error
= SET_ERROR(EIO
);
2153 return (verify_ok
? 0 : EIO
);
2157 * Find a value in the pool props object.
2160 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2162 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2163 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2167 * Find a value in the pool directory object.
2170 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2172 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2173 name
, sizeof (uint64_t), 1, val
));
2177 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2179 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2180 return (SET_ERROR(err
));
2184 * Fix up config after a partly-completed split. This is done with the
2185 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2186 * pool have that entry in their config, but only the splitting one contains
2187 * a list of all the guids of the vdevs that are being split off.
2189 * This function determines what to do with that list: either rejoin
2190 * all the disks to the pool, or complete the splitting process. To attempt
2191 * the rejoin, each disk that is offlined is marked online again, and
2192 * we do a reopen() call. If the vdev label for every disk that was
2193 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2194 * then we call vdev_split() on each disk, and complete the split.
2196 * Otherwise we leave the config alone, with all the vdevs in place in
2197 * the original pool.
2200 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2207 boolean_t attempt_reopen
;
2209 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2212 /* check that the config is complete */
2213 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2214 &glist
, &gcount
) != 0)
2217 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2219 /* attempt to online all the vdevs & validate */
2220 attempt_reopen
= B_TRUE
;
2221 for (i
= 0; i
< gcount
; i
++) {
2222 if (glist
[i
] == 0) /* vdev is hole */
2225 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2226 if (vd
[i
] == NULL
) {
2228 * Don't bother attempting to reopen the disks;
2229 * just do the split.
2231 attempt_reopen
= B_FALSE
;
2233 /* attempt to re-online it */
2234 vd
[i
]->vdev_offline
= B_FALSE
;
2238 if (attempt_reopen
) {
2239 vdev_reopen(spa
->spa_root_vdev
);
2241 /* check each device to see what state it's in */
2242 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2243 if (vd
[i
] != NULL
&&
2244 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2251 * If every disk has been moved to the new pool, or if we never
2252 * even attempted to look at them, then we split them off for
2255 if (!attempt_reopen
|| gcount
== extracted
) {
2256 for (i
= 0; i
< gcount
; i
++)
2259 vdev_reopen(spa
->spa_root_vdev
);
2262 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2266 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2267 boolean_t mosconfig
)
2269 nvlist_t
*config
= spa
->spa_config
;
2270 char *ereport
= FM_EREPORT_ZFS_POOL
;
2276 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2277 return (SET_ERROR(EINVAL
));
2279 ASSERT(spa
->spa_comment
== NULL
);
2280 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2281 spa
->spa_comment
= spa_strdup(comment
);
2284 * Versioning wasn't explicitly added to the label until later, so if
2285 * it's not present treat it as the initial version.
2287 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2288 &spa
->spa_ubsync
.ub_version
) != 0)
2289 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2291 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2292 &spa
->spa_config_txg
);
2294 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2295 spa_guid_exists(pool_guid
, 0)) {
2296 error
= SET_ERROR(EEXIST
);
2298 spa
->spa_config_guid
= pool_guid
;
2300 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2302 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2306 nvlist_free(spa
->spa_load_info
);
2307 spa
->spa_load_info
= fnvlist_alloc();
2309 gethrestime(&spa
->spa_loaded_ts
);
2310 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2311 mosconfig
, &ereport
);
2315 * Don't count references from objsets that are already closed
2316 * and are making their way through the eviction process.
2318 spa_evicting_os_wait(spa
);
2319 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2321 if (error
!= EEXIST
) {
2322 spa
->spa_loaded_ts
.tv_sec
= 0;
2323 spa
->spa_loaded_ts
.tv_nsec
= 0;
2325 if (error
!= EBADF
) {
2326 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, NULL
, 0, 0);
2329 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2337 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2338 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2339 * spa's per-vdev ZAP list.
2342 vdev_count_verify_zaps(vdev_t
*vd
)
2344 spa_t
*spa
= vd
->vdev_spa
;
2347 if (vd
->vdev_top_zap
!= 0) {
2349 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2350 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2352 if (vd
->vdev_leaf_zap
!= 0) {
2354 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2355 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2358 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
2359 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2367 * Determine whether the activity check is required.
2370 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
2374 uint64_t hostid
= 0;
2375 uint64_t tryconfig_txg
= 0;
2376 uint64_t tryconfig_timestamp
= 0;
2379 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2380 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2381 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
2383 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
2384 &tryconfig_timestamp
);
2387 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
2390 * Disable the MMP activity check - This is used by zdb which
2391 * is intended to be used on potentially active pools.
2393 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
2397 * Skip the activity check when the MMP feature is disabled.
2399 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
2402 * If the tryconfig_* values are nonzero, they are the results of an
2403 * earlier tryimport. If they match the uberblock we just found, then
2404 * the pool has not changed and we return false so we do not test a
2407 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
2408 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
)
2412 * Allow the activity check to be skipped when importing the pool
2413 * on the same host which last imported it. Since the hostid from
2414 * configuration may be stale use the one read from the label.
2416 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
2417 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
2419 if (hostid
== spa_get_hostid())
2423 * Skip the activity test when the pool was cleanly exported.
2425 if (state
!= POOL_STATE_ACTIVE
)
2432 * Perform the import activity check. If the user canceled the import or
2433 * we detected activity then fail.
2436 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
2438 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
2439 uint64_t txg
= ub
->ub_txg
;
2440 uint64_t timestamp
= ub
->ub_timestamp
;
2441 uint64_t import_delay
= NANOSEC
;
2442 hrtime_t import_expire
;
2443 nvlist_t
*mmp_label
= NULL
;
2444 vdev_t
*rvd
= spa
->spa_root_vdev
;
2449 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
2450 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
2454 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
2455 * during the earlier tryimport. If the txg recorded there is 0 then
2456 * the pool is known to be active on another host.
2458 * Otherwise, the pool might be in use on another node. Check for
2459 * changes in the uberblocks on disk if necessary.
2461 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
2462 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
2463 ZPOOL_CONFIG_LOAD_INFO
);
2465 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
2466 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
2467 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2468 error
= SET_ERROR(EREMOTEIO
);
2474 * Preferentially use the zfs_multihost_interval from the node which
2475 * last imported the pool. This value is stored in an MMP uberblock as.
2477 * ub_mmp_delay * vdev_count_leaves() == zfs_multihost_interval
2479 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
)
2480 import_delay
= MAX(import_delay
, import_intervals
*
2481 ub
->ub_mmp_delay
* MAX(vdev_count_leaves(spa
), 1));
2483 /* Apply a floor using the local default values. */
2484 import_delay
= MAX(import_delay
, import_intervals
*
2485 MSEC2NSEC(MAX(zfs_multihost_interval
, MMP_MIN_INTERVAL
)));
2487 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu import_intervals=%u "
2488 "leaves=%u", import_delay
, ub
->ub_mmp_delay
, import_intervals
,
2489 vdev_count_leaves(spa
));
2491 /* Add a small random factor in case of simultaneous imports (0-25%) */
2492 import_expire
= gethrtime() + import_delay
+
2493 (import_delay
* spa_get_random(250) / 1000);
2495 while (gethrtime() < import_expire
) {
2496 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
2498 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
) {
2499 error
= SET_ERROR(EREMOTEIO
);
2504 nvlist_free(mmp_label
);
2508 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
2510 error
= SET_ERROR(EINTR
);
2518 mutex_destroy(&mtx
);
2522 * If the pool is determined to be active store the status in the
2523 * spa->spa_load_info nvlist. If the remote hostname or hostid are
2524 * available from configuration read from disk store them as well.
2525 * This allows 'zpool import' to generate a more useful message.
2527 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
2528 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
2529 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
2531 if (error
== EREMOTEIO
) {
2532 char *hostname
= "<unknown>";
2533 uint64_t hostid
= 0;
2536 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
2537 hostname
= fnvlist_lookup_string(mmp_label
,
2538 ZPOOL_CONFIG_HOSTNAME
);
2539 fnvlist_add_string(spa
->spa_load_info
,
2540 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
2543 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
2544 hostid
= fnvlist_lookup_uint64(mmp_label
,
2545 ZPOOL_CONFIG_HOSTID
);
2546 fnvlist_add_uint64(spa
->spa_load_info
,
2547 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
2551 fnvlist_add_uint64(spa
->spa_load_info
,
2552 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
2553 fnvlist_add_uint64(spa
->spa_load_info
,
2554 ZPOOL_CONFIG_MMP_TXG
, 0);
2556 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
2560 nvlist_free(mmp_label
);
2566 * Load an existing storage pool, using the pool's builtin spa_config as a
2567 * source of configuration information.
2569 __attribute__((always_inline
))
2571 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2572 spa_load_state_t state
, spa_import_type_t type
, boolean_t trust_config
,
2576 nvlist_t
*nvroot
= NULL
;
2579 uberblock_t
*ub
= &spa
->spa_uberblock
;
2580 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2581 int orig_mode
= spa
->spa_mode
;
2584 boolean_t missing_feat_write
= B_FALSE
;
2585 boolean_t activity_check
= B_FALSE
;
2588 * If this is an untrusted config, access the pool in read-only mode.
2589 * This prevents things like resilvering recently removed devices.
2592 spa
->spa_mode
= FREAD
;
2594 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2596 spa
->spa_load_state
= state
;
2598 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2599 return (SET_ERROR(EINVAL
));
2601 parse
= (type
== SPA_IMPORT_EXISTING
?
2602 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2605 * Create "The Godfather" zio to hold all async IOs
2607 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2609 for (int i
= 0; i
< max_ncpus
; i
++) {
2610 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2611 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2612 ZIO_FLAG_GODFATHER
);
2616 * Parse the configuration into a vdev tree. We explicitly set the
2617 * value that will be returned by spa_version() since parsing the
2618 * configuration requires knowing the version number.
2620 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2621 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2622 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2627 ASSERT(spa
->spa_root_vdev
== rvd
);
2628 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2629 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2631 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2632 ASSERT(spa_guid(spa
) == pool_guid
);
2636 * Try to open all vdevs, loading each label in the process.
2638 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2639 error
= vdev_open(rvd
);
2640 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2645 * We need to validate the vdev labels against the configuration that
2646 * we have in hand, which is dependent on the setting of mosconfig. If
2647 * mosconfig is true then we're validating the vdev labels based on
2648 * that config. Otherwise, we're validating against the cached config
2649 * (zpool.cache) that was read when we loaded the zfs module, and then
2650 * later we will recursively call spa_load() and validate against
2653 * If we're assembling a new pool that's been split off from an
2654 * existing pool, the labels haven't yet been updated so we skip
2655 * validation for now.
2657 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2658 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2659 error
= vdev_validate(rvd
, trust_config
);
2660 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2665 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2666 return (SET_ERROR(ENXIO
));
2670 * Find the best uberblock.
2672 vdev_uberblock_load(rvd
, ub
, &label
);
2675 * If we weren't able to find a single valid uberblock, return failure.
2677 if (ub
->ub_txg
== 0) {
2679 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2683 * For pools which have the multihost property on determine if the
2684 * pool is truly inactive and can be safely imported. Prevent
2685 * hosts which don't have a hostid set from importing the pool.
2687 activity_check
= spa_activity_check_required(spa
, ub
, label
, config
);
2688 if (activity_check
) {
2689 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
2690 spa_get_hostid() == 0) {
2692 fnvlist_add_uint64(spa
->spa_load_info
,
2693 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
2694 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
2697 error
= spa_activity_check(spa
, ub
, config
);
2703 fnvlist_add_uint64(spa
->spa_load_info
,
2704 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
2705 fnvlist_add_uint64(spa
->spa_load_info
,
2706 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
2710 * If the pool has an unsupported version we can't open it.
2712 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2714 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2717 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2721 * If we weren't able to find what's necessary for reading the
2722 * MOS in the label, return failure.
2724 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2725 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2727 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2732 * Update our in-core representation with the definitive values
2735 nvlist_free(spa
->spa_label_features
);
2736 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2742 * Look through entries in the label nvlist's features_for_read. If
2743 * there is a feature listed there which we don't understand then we
2744 * cannot open a pool.
2746 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2747 nvlist_t
*unsup_feat
;
2749 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2752 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
2754 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2755 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2756 VERIFY(nvlist_add_string(unsup_feat
,
2757 nvpair_name(nvp
), "") == 0);
2761 if (!nvlist_empty(unsup_feat
)) {
2762 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2763 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2764 nvlist_free(unsup_feat
);
2765 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2769 nvlist_free(unsup_feat
);
2773 * If the vdev guid sum doesn't match the uberblock, we have an
2774 * incomplete configuration. We first check to see if the pool
2775 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2776 * If it is, defer the vdev_guid_sum check till later so we
2777 * can handle missing vdevs.
2779 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2780 &children
) != 0 && trust_config
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2781 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2782 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2784 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2785 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2786 spa_try_repair(spa
, config
);
2787 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2788 nvlist_free(spa
->spa_config_splitting
);
2789 spa
->spa_config_splitting
= NULL
;
2793 * Initialize internal SPA structures.
2795 spa
->spa_state
= POOL_STATE_ACTIVE
;
2796 spa
->spa_ubsync
= spa
->spa_uberblock
;
2797 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2798 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2799 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2800 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2801 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2802 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2805 * Everything that we read before we do spa_remove_init() must
2806 * have been rewritten after the last device removal was initiated.
2807 * Otherwise we could be reading from indirect vdevs before
2808 * we have loaded their mappings.
2811 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2813 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2814 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2816 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2817 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2820 * Validate the config, using the MOS config to fill in any
2821 * information which might be missing. If we fail to validate
2822 * the config then declare the pool unfit for use. If we're
2823 * assembling a pool from a split, the log is not transferred
2826 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2827 nvlist_t
*mos_config
;
2828 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2829 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2831 if (!spa_config_valid(spa
, mos_config
)) {
2832 nvlist_free(mos_config
);
2833 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2836 nvlist_free(mos_config
);
2839 * Now that we've validated the config, check the state of the
2840 * root vdev. If it can't be opened, it indicates one or
2841 * more toplevel vdevs are faulted.
2843 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2844 return (SET_ERROR(ENXIO
));
2848 * Everything that we read before spa_remove_init() must be stored
2849 * on concreted vdevs. Therefore we do this as early as possible.
2851 if (spa_remove_init(spa
) != 0)
2852 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2854 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2855 boolean_t missing_feat_read
= B_FALSE
;
2856 nvlist_t
*unsup_feat
, *enabled_feat
;
2858 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2859 &spa
->spa_feat_for_read_obj
) != 0) {
2860 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2863 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2864 &spa
->spa_feat_for_write_obj
) != 0) {
2865 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2868 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2869 &spa
->spa_feat_desc_obj
) != 0) {
2870 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2873 enabled_feat
= fnvlist_alloc();
2874 unsup_feat
= fnvlist_alloc();
2876 if (!spa_features_check(spa
, B_FALSE
,
2877 unsup_feat
, enabled_feat
))
2878 missing_feat_read
= B_TRUE
;
2880 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2881 if (!spa_features_check(spa
, B_TRUE
,
2882 unsup_feat
, enabled_feat
)) {
2883 missing_feat_write
= B_TRUE
;
2887 fnvlist_add_nvlist(spa
->spa_load_info
,
2888 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2890 if (!nvlist_empty(unsup_feat
)) {
2891 fnvlist_add_nvlist(spa
->spa_load_info
,
2892 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2895 fnvlist_free(enabled_feat
);
2896 fnvlist_free(unsup_feat
);
2898 if (!missing_feat_read
) {
2899 fnvlist_add_boolean(spa
->spa_load_info
,
2900 ZPOOL_CONFIG_CAN_RDONLY
);
2904 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2905 * twofold: to determine whether the pool is available for
2906 * import in read-write mode and (if it is not) whether the
2907 * pool is available for import in read-only mode. If the pool
2908 * is available for import in read-write mode, it is displayed
2909 * as available in userland; if it is not available for import
2910 * in read-only mode, it is displayed as unavailable in
2911 * userland. If the pool is available for import in read-only
2912 * mode but not read-write mode, it is displayed as unavailable
2913 * in userland with a special note that the pool is actually
2914 * available for open in read-only mode.
2916 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2917 * missing a feature for write, we must first determine whether
2918 * the pool can be opened read-only before returning to
2919 * userland in order to know whether to display the
2920 * abovementioned note.
2922 if (missing_feat_read
|| (missing_feat_write
&&
2923 spa_writeable(spa
))) {
2924 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2929 * Load refcounts for ZFS features from disk into an in-memory
2930 * cache during SPA initialization.
2932 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
2935 error
= feature_get_refcount_from_disk(spa
,
2936 &spa_feature_table
[i
], &refcount
);
2938 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2939 } else if (error
== ENOTSUP
) {
2940 spa
->spa_feat_refcount_cache
[i
] =
2941 SPA_FEATURE_DISABLED
;
2943 return (spa_vdev_err(rvd
,
2944 VDEV_AUX_CORRUPT_DATA
, EIO
));
2949 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2950 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2951 &spa
->spa_feat_enabled_txg_obj
) != 0)
2952 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2955 spa
->spa_is_initializing
= B_TRUE
;
2956 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2957 spa
->spa_is_initializing
= B_FALSE
;
2959 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2961 if (!trust_config
) {
2963 nvlist_t
*policy
= NULL
;
2964 nvlist_t
*mos_config
;
2966 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2967 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2969 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
2970 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2972 unsigned long myhostid
= 0;
2974 VERIFY(nvlist_lookup_string(mos_config
,
2975 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2977 myhostid
= spa_get_hostid();
2978 if (hostid
&& myhostid
&& hostid
!= myhostid
) {
2979 nvlist_free(mos_config
);
2980 return (SET_ERROR(EBADF
));
2983 if (nvlist_lookup_nvlist(spa
->spa_config
,
2984 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2985 VERIFY(nvlist_add_nvlist(mos_config
,
2986 ZPOOL_REWIND_POLICY
, policy
) == 0);
2988 spa_config_set(spa
, mos_config
);
2990 spa_deactivate(spa
);
2991 spa_activate(spa
, orig_mode
);
2993 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2996 /* Grab the checksum salt from the MOS. */
2997 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2998 DMU_POOL_CHECKSUM_SALT
, 1,
2999 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
3000 spa
->spa_cksum_salt
.zcs_bytes
);
3001 if (error
== ENOENT
) {
3002 /* Generate a new salt for subsequent use */
3003 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
3004 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
3005 } else if (error
!= 0) {
3006 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3009 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
3010 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3011 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
3013 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3016 * Load the bit that tells us to use the new accounting function
3017 * (raid-z deflation). If we have an older pool, this will not
3020 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
3021 if (error
!= 0 && error
!= ENOENT
)
3022 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3024 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
3025 &spa
->spa_creation_version
);
3026 if (error
!= 0 && error
!= ENOENT
)
3027 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3030 * Load the persistent error log. If we have an older pool, this will
3033 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
3034 if (error
!= 0 && error
!= ENOENT
)
3035 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3037 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
3038 &spa
->spa_errlog_scrub
);
3039 if (error
!= 0 && error
!= ENOENT
)
3040 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3043 * Load the history object. If we have an older pool, this
3044 * will not be present.
3046 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
3047 if (error
!= 0 && error
!= ENOENT
)
3048 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3051 * Load the per-vdev ZAP map. If we have an older pool, this will not
3052 * be present; in this case, defer its creation to a later time to
3053 * avoid dirtying the MOS this early / out of sync context. See
3054 * spa_sync_config_object.
3057 /* The sentinel is only available in the MOS config. */
3058 nvlist_t
*mos_config
;
3059 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
3060 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3062 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
3063 &spa
->spa_all_vdev_zaps
);
3065 if (error
== ENOENT
) {
3066 VERIFY(!nvlist_exists(mos_config
,
3067 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
3068 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
3069 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3070 } else if (error
!= 0) {
3071 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3072 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
3074 * An older version of ZFS overwrote the sentinel value, so
3075 * we have orphaned per-vdev ZAPs in the MOS. Defer their
3076 * destruction to later; see spa_sync_config_object.
3078 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
3080 * We're assuming that no vdevs have had their ZAPs created
3081 * before this. Better be sure of it.
3083 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
3085 nvlist_free(mos_config
);
3088 * If we're assembling the pool from the split-off vdevs of
3089 * an existing pool, we don't want to attach the spares & cache
3094 * Load any hot spares for this pool.
3096 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
3097 if (error
!= 0 && error
!= ENOENT
)
3098 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3099 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3100 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
3101 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
3102 &spa
->spa_spares
.sav_config
) != 0)
3103 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3105 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3106 spa_load_spares(spa
);
3107 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3108 } else if (error
== 0) {
3109 spa
->spa_spares
.sav_sync
= B_TRUE
;
3113 * Load any level 2 ARC devices for this pool.
3115 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
3116 &spa
->spa_l2cache
.sav_object
);
3117 if (error
!= 0 && error
!= ENOENT
)
3118 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3119 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
3120 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
3121 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
3122 &spa
->spa_l2cache
.sav_config
) != 0)
3123 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3125 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3126 spa_load_l2cache(spa
);
3127 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3128 } else if (error
== 0) {
3129 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3132 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3134 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
3135 if (error
&& error
!= ENOENT
)
3136 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3139 uint64_t autoreplace
= 0;
3141 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
3142 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
3143 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
3144 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
3145 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
3146 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
3147 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
3148 &spa
->spa_dedup_ditto
);
3150 spa
->spa_autoreplace
= (autoreplace
!= 0);
3154 * If the 'multihost' property is set, then never allow a pool to
3155 * be imported when the system hostid is zero. The exception to
3156 * this rule is zdb which is always allowed to access pools.
3158 if (spa_multihost(spa
) && spa_get_hostid() == 0 &&
3159 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
3160 fnvlist_add_uint64(spa
->spa_load_info
,
3161 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3162 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3166 * If the 'autoreplace' property is set, then post a resource notifying
3167 * the ZFS DE that it should not issue any faults for unopenable
3168 * devices. We also iterate over the vdevs, and post a sysevent for any
3169 * unopenable vdevs so that the normal autoreplace handler can take
3172 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
3173 spa_check_removed(spa
->spa_root_vdev
);
3175 * For the import case, this is done in spa_import(), because
3176 * at this point we're using the spare definitions from
3177 * the MOS config, not necessarily from the userland config.
3179 if (state
!= SPA_LOAD_IMPORT
) {
3180 spa_aux_check_removed(&spa
->spa_spares
);
3181 spa_aux_check_removed(&spa
->spa_l2cache
);
3186 * Load the vdev state for all toplevel vdevs.
3188 error
= vdev_load(rvd
);
3190 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3193 error
= spa_condense_init(spa
);
3195 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3199 * Propagate the leaf DTLs we just loaded all the way up the tree.
3201 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3202 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
3203 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3206 * Load the DDTs (dedup tables).
3208 error
= ddt_load(spa
);
3210 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3212 spa_update_dspace(spa
);
3214 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
) &&
3215 spa_check_logs(spa
)) {
3216 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
3217 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
3220 if (missing_feat_write
) {
3221 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
3224 * At this point, we know that we can open the pool in
3225 * read-only mode but not read-write mode. We now have enough
3226 * information and can return to userland.
3228 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
3232 * We've successfully opened the pool, verify that we're ready
3233 * to start pushing transactions.
3235 if (state
!= SPA_LOAD_TRYIMPORT
) {
3236 if ((error
= spa_load_verify(spa
)))
3237 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3241 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
3242 spa
->spa_load_max_txg
== UINT64_MAX
)) {
3244 int need_update
= B_FALSE
;
3245 dsl_pool_t
*dp
= spa_get_dsl(spa
);
3248 * We must check this before we start the sync thread, because
3249 * we only want to start a condense thread for condense
3250 * operations that were in progress when the pool was
3251 * imported. Once we start syncing, spa_sync() could
3252 * initiate a condense (and start a thread for it). In
3253 * that case it would be wrong to start a second
3256 boolean_t condense_in_progress
=
3257 (spa
->spa_condensing_indirect
!= NULL
);
3259 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
3262 * Claim log blocks that haven't been committed yet.
3263 * This must all happen in a single txg.
3264 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
3265 * invoked from zil_claim_log_block()'s i/o done callback.
3266 * Price of rollback is that we abandon the log.
3268 spa
->spa_claiming
= B_TRUE
;
3270 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
3271 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
3272 zil_claim
, tx
, DS_FIND_CHILDREN
);
3275 spa
->spa_claiming
= B_FALSE
;
3277 spa_set_log_state(spa
, SPA_LOG_GOOD
);
3278 spa
->spa_sync_on
= B_TRUE
;
3279 txg_sync_start(spa
->spa_dsl_pool
);
3280 mmp_thread_start(spa
);
3283 * Wait for all claims to sync. We sync up to the highest
3284 * claimed log block birth time so that claimed log blocks
3285 * don't appear to be from the future. spa_claim_max_txg
3286 * will have been set for us by either zil_check_log_chain()
3287 * (invoked from spa_check_logs()) or zil_claim() above.
3289 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
3292 * If the config cache is stale, or we have uninitialized
3293 * metaslabs (see spa_vdev_add()), then update the config.
3295 * If this is a verbatim import, trust the current
3296 * in-core spa_config and update the disk labels.
3298 if (config_cache_txg
!= spa
->spa_config_txg
||
3299 state
== SPA_LOAD_IMPORT
||
3300 state
== SPA_LOAD_RECOVER
||
3301 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
3302 need_update
= B_TRUE
;
3304 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
3305 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
3306 need_update
= B_TRUE
;
3309 * Update the config cache asychronously in case we're the
3310 * root pool, in which case the config cache isn't writable yet.
3313 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
3316 * Check all DTLs to see if anything needs resilvering.
3318 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
3319 vdev_resilver_needed(rvd
, NULL
, NULL
))
3320 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
3323 * Log the fact that we booted up (so that we can detect if
3324 * we rebooted in the middle of an operation).
3326 spa_history_log_version(spa
, "open", NULL
);
3329 * Delete any inconsistent datasets.
3331 (void) dmu_objset_find(spa_name(spa
),
3332 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
3335 * Clean up any stale temporary dataset userrefs.
3337 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
3340 * Note: unlike condensing, we don't need an analogous
3341 * "removal_in_progress" dance because no other thread
3342 * can start a removal while we hold the spa_namespace_lock.
3344 spa_restart_removal(spa
);
3346 if (condense_in_progress
)
3347 spa_condense_indirect_restart(spa
);
3354 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
3356 int mode
= spa
->spa_mode
;
3359 spa_deactivate(spa
);
3361 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
3363 spa_activate(spa
, mode
);
3364 spa_async_suspend(spa
);
3366 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
3370 * If spa_load() fails this function will try loading prior txg's. If
3371 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3372 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3373 * function will not rewind the pool and will return the same error as
3377 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
3378 uint64_t max_request
, int rewind_flags
)
3380 nvlist_t
*loadinfo
= NULL
;
3381 nvlist_t
*config
= NULL
;
3382 int load_error
, rewind_error
;
3383 uint64_t safe_rewind_txg
;
3386 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
3387 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
3388 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3390 spa
->spa_load_max_txg
= max_request
;
3391 if (max_request
!= UINT64_MAX
)
3392 spa
->spa_extreme_rewind
= B_TRUE
;
3395 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
3397 if (load_error
== 0)
3400 if (spa
->spa_root_vdev
!= NULL
)
3401 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3403 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
3404 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
3406 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
3407 nvlist_free(config
);
3408 return (load_error
);
3411 if (state
== SPA_LOAD_RECOVER
) {
3412 /* Price of rolling back is discarding txgs, including log */
3413 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3416 * If we aren't rolling back save the load info from our first
3417 * import attempt so that we can restore it after attempting
3420 loadinfo
= spa
->spa_load_info
;
3421 spa
->spa_load_info
= fnvlist_alloc();
3424 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
3425 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
3426 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
3427 TXG_INITIAL
: safe_rewind_txg
;
3430 * Continue as long as we're finding errors, we're still within
3431 * the acceptable rewind range, and we're still finding uberblocks
3433 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
3434 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
3435 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
3436 spa
->spa_extreme_rewind
= B_TRUE
;
3437 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
3440 spa
->spa_extreme_rewind
= B_FALSE
;
3441 spa
->spa_load_max_txg
= UINT64_MAX
;
3443 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
3444 spa_config_set(spa
, config
);
3446 nvlist_free(config
);
3448 if (state
== SPA_LOAD_RECOVER
) {
3449 ASSERT3P(loadinfo
, ==, NULL
);
3450 return (rewind_error
);
3452 /* Store the rewind info as part of the initial load info */
3453 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
3454 spa
->spa_load_info
);
3456 /* Restore the initial load info */
3457 fnvlist_free(spa
->spa_load_info
);
3458 spa
->spa_load_info
= loadinfo
;
3460 return (load_error
);
3467 * The import case is identical to an open except that the configuration is sent
3468 * down from userland, instead of grabbed from the configuration cache. For the
3469 * case of an open, the pool configuration will exist in the
3470 * POOL_STATE_UNINITIALIZED state.
3472 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3473 * the same time open the pool, without having to keep around the spa_t in some
3477 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
3481 spa_load_state_t state
= SPA_LOAD_OPEN
;
3483 int locked
= B_FALSE
;
3484 int firstopen
= B_FALSE
;
3489 * As disgusting as this is, we need to support recursive calls to this
3490 * function because dsl_dir_open() is called during spa_load(), and ends
3491 * up calling spa_open() again. The real fix is to figure out how to
3492 * avoid dsl_dir_open() calling this in the first place.
3494 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
3495 mutex_enter(&spa_namespace_lock
);
3499 if ((spa
= spa_lookup(pool
)) == NULL
) {
3501 mutex_exit(&spa_namespace_lock
);
3502 return (SET_ERROR(ENOENT
));
3505 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3506 zpool_rewind_policy_t policy
;
3510 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3512 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3513 state
= SPA_LOAD_RECOVER
;
3515 spa_activate(spa
, spa_mode_global
);
3517 if (state
!= SPA_LOAD_RECOVER
)
3518 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3520 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3521 policy
.zrp_request
);
3523 if (error
== EBADF
) {
3525 * If vdev_validate() returns failure (indicated by
3526 * EBADF), it indicates that one of the vdevs indicates
3527 * that the pool has been exported or destroyed. If
3528 * this is the case, the config cache is out of sync and
3529 * we should remove the pool from the namespace.
3532 spa_deactivate(spa
);
3533 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
3536 mutex_exit(&spa_namespace_lock
);
3537 return (SET_ERROR(ENOENT
));
3542 * We can't open the pool, but we still have useful
3543 * information: the state of each vdev after the
3544 * attempted vdev_open(). Return this to the user.
3546 if (config
!= NULL
&& spa
->spa_config
) {
3547 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3549 VERIFY(nvlist_add_nvlist(*config
,
3550 ZPOOL_CONFIG_LOAD_INFO
,
3551 spa
->spa_load_info
) == 0);
3554 spa_deactivate(spa
);
3555 spa
->spa_last_open_failed
= error
;
3557 mutex_exit(&spa_namespace_lock
);
3563 spa_open_ref(spa
, tag
);
3566 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3569 * If we've recovered the pool, pass back any information we
3570 * gathered while doing the load.
3572 if (state
== SPA_LOAD_RECOVER
) {
3573 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3574 spa
->spa_load_info
) == 0);
3578 spa
->spa_last_open_failed
= 0;
3579 spa
->spa_last_ubsync_txg
= 0;
3580 spa
->spa_load_txg
= 0;
3581 mutex_exit(&spa_namespace_lock
);
3585 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
3593 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3596 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3600 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3602 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3606 * Lookup the given spa_t, incrementing the inject count in the process,
3607 * preventing it from being exported or destroyed.
3610 spa_inject_addref(char *name
)
3614 mutex_enter(&spa_namespace_lock
);
3615 if ((spa
= spa_lookup(name
)) == NULL
) {
3616 mutex_exit(&spa_namespace_lock
);
3619 spa
->spa_inject_ref
++;
3620 mutex_exit(&spa_namespace_lock
);
3626 spa_inject_delref(spa_t
*spa
)
3628 mutex_enter(&spa_namespace_lock
);
3629 spa
->spa_inject_ref
--;
3630 mutex_exit(&spa_namespace_lock
);
3634 * Add spares device information to the nvlist.
3637 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3647 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3649 if (spa
->spa_spares
.sav_count
== 0)
3652 VERIFY(nvlist_lookup_nvlist(config
,
3653 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3654 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3655 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3657 VERIFY(nvlist_add_nvlist_array(nvroot
,
3658 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3659 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3660 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3663 * Go through and find any spares which have since been
3664 * repurposed as an active spare. If this is the case, update
3665 * their status appropriately.
3667 for (i
= 0; i
< nspares
; i
++) {
3668 VERIFY(nvlist_lookup_uint64(spares
[i
],
3669 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3670 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3672 VERIFY(nvlist_lookup_uint64_array(
3673 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3674 (uint64_t **)&vs
, &vsc
) == 0);
3675 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3676 vs
->vs_aux
= VDEV_AUX_SPARED
;
3683 * Add l2cache device information to the nvlist, including vdev stats.
3686 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3689 uint_t i
, j
, nl2cache
;
3696 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3698 if (spa
->spa_l2cache
.sav_count
== 0)
3701 VERIFY(nvlist_lookup_nvlist(config
,
3702 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3703 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3704 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3705 if (nl2cache
!= 0) {
3706 VERIFY(nvlist_add_nvlist_array(nvroot
,
3707 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3708 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3709 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3712 * Update level 2 cache device stats.
3715 for (i
= 0; i
< nl2cache
; i
++) {
3716 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3717 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3720 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3722 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3723 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3729 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3730 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3732 vdev_get_stats(vd
, vs
);
3733 vdev_config_generate_stats(vd
, l2cache
[i
]);
3740 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3745 if (spa
->spa_feat_for_read_obj
!= 0) {
3746 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3747 spa
->spa_feat_for_read_obj
);
3748 zap_cursor_retrieve(&zc
, &za
) == 0;
3749 zap_cursor_advance(&zc
)) {
3750 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3751 za
.za_num_integers
== 1);
3752 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3753 za
.za_first_integer
));
3755 zap_cursor_fini(&zc
);
3758 if (spa
->spa_feat_for_write_obj
!= 0) {
3759 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3760 spa
->spa_feat_for_write_obj
);
3761 zap_cursor_retrieve(&zc
, &za
) == 0;
3762 zap_cursor_advance(&zc
)) {
3763 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3764 za
.za_num_integers
== 1);
3765 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3766 za
.za_first_integer
));
3768 zap_cursor_fini(&zc
);
3773 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3777 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3778 zfeature_info_t feature
= spa_feature_table
[i
];
3781 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3784 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3789 * Store a list of pool features and their reference counts in the
3792 * The first time this is called on a spa, allocate a new nvlist, fetch
3793 * the pool features and reference counts from disk, then save the list
3794 * in the spa. In subsequent calls on the same spa use the saved nvlist
3795 * and refresh its values from the cached reference counts. This
3796 * ensures we don't block here on I/O on a suspended pool so 'zpool
3797 * clear' can resume the pool.
3800 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3804 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3806 mutex_enter(&spa
->spa_feat_stats_lock
);
3807 features
= spa
->spa_feat_stats
;
3809 if (features
!= NULL
) {
3810 spa_feature_stats_from_cache(spa
, features
);
3812 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3813 spa
->spa_feat_stats
= features
;
3814 spa_feature_stats_from_disk(spa
, features
);
3817 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3820 mutex_exit(&spa
->spa_feat_stats_lock
);
3824 spa_get_stats(const char *name
, nvlist_t
**config
,
3825 char *altroot
, size_t buflen
)
3831 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3835 * This still leaves a window of inconsistency where the spares
3836 * or l2cache devices could change and the config would be
3837 * self-inconsistent.
3839 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3841 if (*config
!= NULL
) {
3842 uint64_t loadtimes
[2];
3844 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3845 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3846 VERIFY(nvlist_add_uint64_array(*config
,
3847 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3849 VERIFY(nvlist_add_uint64(*config
,
3850 ZPOOL_CONFIG_ERRCOUNT
,
3851 spa_get_errlog_size(spa
)) == 0);
3853 if (spa_suspended(spa
)) {
3854 VERIFY(nvlist_add_uint64(*config
,
3855 ZPOOL_CONFIG_SUSPENDED
,
3856 spa
->spa_failmode
) == 0);
3857 VERIFY(nvlist_add_uint64(*config
,
3858 ZPOOL_CONFIG_SUSPENDED_REASON
,
3859 spa
->spa_suspended
) == 0);
3862 spa_add_spares(spa
, *config
);
3863 spa_add_l2cache(spa
, *config
);
3864 spa_add_feature_stats(spa
, *config
);
3869 * We want to get the alternate root even for faulted pools, so we cheat
3870 * and call spa_lookup() directly.
3874 mutex_enter(&spa_namespace_lock
);
3875 spa
= spa_lookup(name
);
3877 spa_altroot(spa
, altroot
, buflen
);
3881 mutex_exit(&spa_namespace_lock
);
3883 spa_altroot(spa
, altroot
, buflen
);
3888 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3889 spa_close(spa
, FTAG
);
3896 * Validate that the auxiliary device array is well formed. We must have an
3897 * array of nvlists, each which describes a valid leaf vdev. If this is an
3898 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3899 * specified, as long as they are well-formed.
3902 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3903 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3904 vdev_labeltype_t label
)
3911 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3914 * It's acceptable to have no devs specified.
3916 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3920 return (SET_ERROR(EINVAL
));
3923 * Make sure the pool is formatted with a version that supports this
3926 if (spa_version(spa
) < version
)
3927 return (SET_ERROR(ENOTSUP
));
3930 * Set the pending device list so we correctly handle device in-use
3933 sav
->sav_pending
= dev
;
3934 sav
->sav_npending
= ndev
;
3936 for (i
= 0; i
< ndev
; i
++) {
3937 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3941 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3943 error
= SET_ERROR(EINVAL
);
3949 if ((error
= vdev_open(vd
)) == 0 &&
3950 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3951 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3952 vd
->vdev_guid
) == 0);
3958 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3965 sav
->sav_pending
= NULL
;
3966 sav
->sav_npending
= 0;
3971 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3975 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3977 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3978 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3979 VDEV_LABEL_SPARE
)) != 0) {
3983 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3984 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3985 VDEV_LABEL_L2CACHE
));
3989 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3994 if (sav
->sav_config
!= NULL
) {
4000 * Generate new dev list by concatenating with the
4003 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
4004 &olddevs
, &oldndevs
) == 0);
4006 newdevs
= kmem_alloc(sizeof (void *) *
4007 (ndevs
+ oldndevs
), KM_SLEEP
);
4008 for (i
= 0; i
< oldndevs
; i
++)
4009 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
4011 for (i
= 0; i
< ndevs
; i
++)
4012 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
4015 VERIFY(nvlist_remove(sav
->sav_config
, config
,
4016 DATA_TYPE_NVLIST_ARRAY
) == 0);
4018 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
4019 config
, newdevs
, ndevs
+ oldndevs
) == 0);
4020 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
4021 nvlist_free(newdevs
[i
]);
4022 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
4025 * Generate a new dev list.
4027 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
4029 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
4035 * Stop and drop level 2 ARC devices
4038 spa_l2cache_drop(spa_t
*spa
)
4042 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
4044 for (i
= 0; i
< sav
->sav_count
; i
++) {
4047 vd
= sav
->sav_vdevs
[i
];
4050 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
4051 pool
!= 0ULL && l2arc_vdev_present(vd
))
4052 l2arc_remove_vdev(vd
);
4057 * Verify encryption parameters for spa creation. If we are encrypting, we must
4058 * have the encryption feature flag enabled.
4061 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
4062 boolean_t has_encryption
)
4064 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
4065 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
4067 return (SET_ERROR(ENOTSUP
));
4069 return (dmu_objset_create_crypt_check(NULL
, dcp
));
4076 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
4077 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
4080 char *altroot
= NULL
;
4085 uint64_t txg
= TXG_INITIAL
;
4086 nvlist_t
**spares
, **l2cache
;
4087 uint_t nspares
, nl2cache
;
4088 uint64_t version
, obj
, root_dsobj
= 0;
4089 boolean_t has_features
;
4090 boolean_t has_encryption
;
4096 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
4097 poolname
= (char *)pool
;
4100 * If this pool already exists, return failure.
4102 mutex_enter(&spa_namespace_lock
);
4103 if (spa_lookup(poolname
) != NULL
) {
4104 mutex_exit(&spa_namespace_lock
);
4105 return (SET_ERROR(EEXIST
));
4109 * Allocate a new spa_t structure.
4111 nvl
= fnvlist_alloc();
4112 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
4113 (void) nvlist_lookup_string(props
,
4114 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4115 spa
= spa_add(poolname
, nvl
, altroot
);
4117 spa_activate(spa
, spa_mode_global
);
4119 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
4120 spa_deactivate(spa
);
4122 mutex_exit(&spa_namespace_lock
);
4127 * Temporary pool names should never be written to disk.
4129 if (poolname
!= pool
)
4130 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
4132 has_features
= B_FALSE
;
4133 has_encryption
= B_FALSE
;
4134 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
4135 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
4136 if (zpool_prop_feature(nvpair_name(elem
))) {
4137 has_features
= B_TRUE
;
4139 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
4140 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
4141 if (feat
== SPA_FEATURE_ENCRYPTION
)
4142 has_encryption
= B_TRUE
;
4146 /* verify encryption params, if they were provided */
4148 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
4150 spa_deactivate(spa
);
4152 mutex_exit(&spa_namespace_lock
);
4157 if (has_features
|| nvlist_lookup_uint64(props
,
4158 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
4159 version
= SPA_VERSION
;
4161 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
4163 spa
->spa_first_txg
= txg
;
4164 spa
->spa_uberblock
.ub_txg
= txg
- 1;
4165 spa
->spa_uberblock
.ub_version
= version
;
4166 spa
->spa_ubsync
= spa
->spa_uberblock
;
4167 spa
->spa_load_state
= SPA_LOAD_CREATE
;
4168 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
4169 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
4170 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
4173 * Create "The Godfather" zio to hold all async IOs
4175 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
4177 for (int i
= 0; i
< max_ncpus
; i
++) {
4178 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
4179 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
4180 ZIO_FLAG_GODFATHER
);
4184 * Create the root vdev.
4186 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4188 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
4190 ASSERT(error
!= 0 || rvd
!= NULL
);
4191 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
4193 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
4194 error
= SET_ERROR(EINVAL
);
4197 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
4198 (error
= spa_validate_aux(spa
, nvroot
, txg
,
4199 VDEV_ALLOC_ADD
)) == 0) {
4200 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
4201 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
4202 vdev_expand(rvd
->vdev_child
[c
], txg
);
4206 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4210 spa_deactivate(spa
);
4212 mutex_exit(&spa_namespace_lock
);
4217 * Get the list of spares, if specified.
4219 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4220 &spares
, &nspares
) == 0) {
4221 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
4223 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4224 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4225 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4226 spa_load_spares(spa
);
4227 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4228 spa
->spa_spares
.sav_sync
= B_TRUE
;
4232 * Get the list of level 2 cache devices, if specified.
4234 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4235 &l2cache
, &nl2cache
) == 0) {
4236 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4237 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4238 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4239 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4240 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4241 spa_load_l2cache(spa
);
4242 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4243 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4246 spa
->spa_is_initializing
= B_TRUE
;
4247 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
4248 spa
->spa_is_initializing
= B_FALSE
;
4251 * Create DDTs (dedup tables).
4255 spa_update_dspace(spa
);
4257 tx
= dmu_tx_create_assigned(dp
, txg
);
4260 * Create the pool's history object.
4262 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
4263 spa_history_create_obj(spa
, tx
);
4265 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
4266 spa_history_log_version(spa
, "create", tx
);
4269 * Create the pool config object.
4271 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
4272 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
4273 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
4275 if (zap_add(spa
->spa_meta_objset
,
4276 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
4277 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
4278 cmn_err(CE_PANIC
, "failed to add pool config");
4281 if (zap_add(spa
->spa_meta_objset
,
4282 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
4283 sizeof (uint64_t), 1, &version
, tx
) != 0) {
4284 cmn_err(CE_PANIC
, "failed to add pool version");
4287 /* Newly created pools with the right version are always deflated. */
4288 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
4289 spa
->spa_deflate
= TRUE
;
4290 if (zap_add(spa
->spa_meta_objset
,
4291 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
4292 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
4293 cmn_err(CE_PANIC
, "failed to add deflate");
4298 * Create the deferred-free bpobj. Turn off compression
4299 * because sync-to-convergence takes longer if the blocksize
4302 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
4303 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
4304 ZIO_COMPRESS_OFF
, tx
);
4305 if (zap_add(spa
->spa_meta_objset
,
4306 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
4307 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
4308 cmn_err(CE_PANIC
, "failed to add bpobj");
4310 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
4311 spa
->spa_meta_objset
, obj
));
4314 * Generate some random noise for salted checksums to operate on.
4316 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4317 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4320 * Set pool properties.
4322 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
4323 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4324 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
4325 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
4326 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
4328 if (props
!= NULL
) {
4329 spa_configfile_set(spa
, props
, B_FALSE
);
4330 spa_sync_props(props
, tx
);
4336 * If the root dataset is encrypted we will need to create key mappings
4337 * for the zio layer before we start to write any data to disk and hold
4338 * them until after the first txg has been synced. Waiting for the first
4339 * transaction to complete also ensures that our bean counters are
4340 * appropriately updated.
4342 if (dp
->dp_root_dir
->dd_crypto_obj
!= 0) {
4343 root_dsobj
= dsl_dir_phys(dp
->dp_root_dir
)->dd_head_dataset_obj
;
4344 VERIFY0(spa_keystore_create_mapping_impl(spa
, root_dsobj
,
4345 dp
->dp_root_dir
, FTAG
));
4348 spa
->spa_sync_on
= B_TRUE
;
4350 mmp_thread_start(spa
);
4351 txg_wait_synced(dp
, txg
);
4353 if (dp
->dp_root_dir
->dd_crypto_obj
!= 0)
4354 VERIFY0(spa_keystore_remove_mapping(spa
, root_dsobj
, FTAG
));
4356 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
4359 * Don't count references from objsets that are already closed
4360 * and are making their way through the eviction process.
4362 spa_evicting_os_wait(spa
);
4363 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
4364 spa
->spa_load_state
= SPA_LOAD_NONE
;
4366 mutex_exit(&spa_namespace_lock
);
4372 * Import a non-root pool into the system.
4375 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4378 char *altroot
= NULL
;
4379 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4380 zpool_rewind_policy_t policy
;
4381 uint64_t mode
= spa_mode_global
;
4382 uint64_t readonly
= B_FALSE
;
4385 nvlist_t
**spares
, **l2cache
;
4386 uint_t nspares
, nl2cache
;
4389 * If a pool with this name exists, return failure.
4391 mutex_enter(&spa_namespace_lock
);
4392 if (spa_lookup(pool
) != NULL
) {
4393 mutex_exit(&spa_namespace_lock
);
4394 return (SET_ERROR(EEXIST
));
4398 * Create and initialize the spa structure.
4400 (void) nvlist_lookup_string(props
,
4401 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4402 (void) nvlist_lookup_uint64(props
,
4403 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4406 spa
= spa_add(pool
, config
, altroot
);
4407 spa
->spa_import_flags
= flags
;
4410 * Verbatim import - Take a pool and insert it into the namespace
4411 * as if it had been loaded at boot.
4413 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4415 spa_configfile_set(spa
, props
, B_FALSE
);
4417 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
);
4418 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4420 mutex_exit(&spa_namespace_lock
);
4424 spa_activate(spa
, mode
);
4427 * Don't start async tasks until we know everything is healthy.
4429 spa_async_suspend(spa
);
4431 zpool_get_rewind_policy(config
, &policy
);
4432 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4433 state
= SPA_LOAD_RECOVER
;
4436 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4437 * because the user-supplied config is actually the one to trust when
4440 if (state
!= SPA_LOAD_RECOVER
)
4441 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4443 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4444 policy
.zrp_request
);
4447 * Propagate anything learned while loading the pool and pass it
4448 * back to caller (i.e. rewind info, missing devices, etc).
4450 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4451 spa
->spa_load_info
) == 0);
4453 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4455 * Toss any existing sparelist, as it doesn't have any validity
4456 * anymore, and conflicts with spa_has_spare().
4458 if (spa
->spa_spares
.sav_config
) {
4459 nvlist_free(spa
->spa_spares
.sav_config
);
4460 spa
->spa_spares
.sav_config
= NULL
;
4461 spa_load_spares(spa
);
4463 if (spa
->spa_l2cache
.sav_config
) {
4464 nvlist_free(spa
->spa_l2cache
.sav_config
);
4465 spa
->spa_l2cache
.sav_config
= NULL
;
4466 spa_load_l2cache(spa
);
4469 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4471 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4474 spa_configfile_set(spa
, props
, B_FALSE
);
4476 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4477 (error
= spa_prop_set(spa
, props
)))) {
4479 spa_deactivate(spa
);
4481 mutex_exit(&spa_namespace_lock
);
4485 spa_async_resume(spa
);
4488 * Override any spares and level 2 cache devices as specified by
4489 * the user, as these may have correct device names/devids, etc.
4491 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4492 &spares
, &nspares
) == 0) {
4493 if (spa
->spa_spares
.sav_config
)
4494 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4495 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4497 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4498 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4499 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4500 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4501 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4502 spa_load_spares(spa
);
4503 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4504 spa
->spa_spares
.sav_sync
= B_TRUE
;
4506 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4507 &l2cache
, &nl2cache
) == 0) {
4508 if (spa
->spa_l2cache
.sav_config
)
4509 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4510 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4512 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4513 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4514 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4515 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4516 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4517 spa_load_l2cache(spa
);
4518 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4519 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4523 * Check for any removed devices.
4525 if (spa
->spa_autoreplace
) {
4526 spa_aux_check_removed(&spa
->spa_spares
);
4527 spa_aux_check_removed(&spa
->spa_l2cache
);
4530 if (spa_writeable(spa
)) {
4532 * Update the config cache to include the newly-imported pool.
4534 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4538 * It's possible that the pool was expanded while it was exported.
4539 * We kick off an async task to handle this for us.
4541 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4543 spa_history_log_version(spa
, "import", NULL
);
4545 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
4547 zvol_create_minors(spa
, pool
, B_TRUE
);
4549 mutex_exit(&spa_namespace_lock
);
4555 spa_tryimport(nvlist_t
*tryconfig
)
4557 nvlist_t
*config
= NULL
;
4563 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4566 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4570 * Create and initialize the spa structure.
4572 mutex_enter(&spa_namespace_lock
);
4573 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4574 spa_activate(spa
, FREAD
);
4577 * Pass off the heavy lifting to spa_load().
4578 * Pass TRUE for mosconfig because the user-supplied config
4579 * is actually the one to trust when doing an import.
4581 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4584 * If 'tryconfig' was at least parsable, return the current config.
4586 if (spa
->spa_root_vdev
!= NULL
) {
4587 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4588 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4590 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4592 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4593 spa
->spa_uberblock
.ub_timestamp
) == 0);
4594 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4595 spa
->spa_load_info
) == 0);
4596 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4597 spa
->spa_errata
) == 0);
4600 * If the bootfs property exists on this pool then we
4601 * copy it out so that external consumers can tell which
4602 * pools are bootable.
4604 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4605 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4608 * We have to play games with the name since the
4609 * pool was opened as TRYIMPORT_NAME.
4611 if (dsl_dsobj_to_dsname(spa_name(spa
),
4612 spa
->spa_bootfs
, tmpname
) == 0) {
4616 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4618 cp
= strchr(tmpname
, '/');
4620 (void) strlcpy(dsname
, tmpname
,
4623 (void) snprintf(dsname
, MAXPATHLEN
,
4624 "%s/%s", poolname
, ++cp
);
4626 VERIFY(nvlist_add_string(config
,
4627 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4628 kmem_free(dsname
, MAXPATHLEN
);
4630 kmem_free(tmpname
, MAXPATHLEN
);
4634 * Add the list of hot spares and level 2 cache devices.
4636 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4637 spa_add_spares(spa
, config
);
4638 spa_add_l2cache(spa
, config
);
4639 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4643 spa_deactivate(spa
);
4645 mutex_exit(&spa_namespace_lock
);
4651 * Pool export/destroy
4653 * The act of destroying or exporting a pool is very simple. We make sure there
4654 * is no more pending I/O and any references to the pool are gone. Then, we
4655 * update the pool state and sync all the labels to disk, removing the
4656 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4657 * we don't sync the labels or remove the configuration cache.
4660 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4661 boolean_t force
, boolean_t hardforce
)
4668 if (!(spa_mode_global
& FWRITE
))
4669 return (SET_ERROR(EROFS
));
4671 mutex_enter(&spa_namespace_lock
);
4672 if ((spa
= spa_lookup(pool
)) == NULL
) {
4673 mutex_exit(&spa_namespace_lock
);
4674 return (SET_ERROR(ENOENT
));
4678 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4679 * reacquire the namespace lock, and see if we can export.
4681 spa_open_ref(spa
, FTAG
);
4682 mutex_exit(&spa_namespace_lock
);
4683 spa_async_suspend(spa
);
4684 if (spa
->spa_zvol_taskq
) {
4685 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
4686 taskq_wait(spa
->spa_zvol_taskq
);
4688 mutex_enter(&spa_namespace_lock
);
4689 spa_close(spa
, FTAG
);
4691 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4694 * The pool will be in core if it's openable, in which case we can
4695 * modify its state. Objsets may be open only because they're dirty,
4696 * so we have to force it to sync before checking spa_refcnt.
4698 if (spa
->spa_sync_on
) {
4699 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4700 spa_evicting_os_wait(spa
);
4704 * A pool cannot be exported or destroyed if there are active
4705 * references. If we are resetting a pool, allow references by
4706 * fault injection handlers.
4708 if (!spa_refcount_zero(spa
) ||
4709 (spa
->spa_inject_ref
!= 0 &&
4710 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4711 spa_async_resume(spa
);
4712 mutex_exit(&spa_namespace_lock
);
4713 return (SET_ERROR(EBUSY
));
4716 if (spa
->spa_sync_on
) {
4718 * A pool cannot be exported if it has an active shared spare.
4719 * This is to prevent other pools stealing the active spare
4720 * from an exported pool. At user's own will, such pool can
4721 * be forcedly exported.
4723 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4724 spa_has_active_shared_spare(spa
)) {
4725 spa_async_resume(spa
);
4726 mutex_exit(&spa_namespace_lock
);
4727 return (SET_ERROR(EXDEV
));
4731 * We want this to be reflected on every label,
4732 * so mark them all dirty. spa_unload() will do the
4733 * final sync that pushes these changes out.
4735 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4736 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4737 spa
->spa_state
= new_state
;
4738 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4740 vdev_config_dirty(spa
->spa_root_vdev
);
4741 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4746 if (new_state
== POOL_STATE_DESTROYED
)
4747 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
4748 else if (new_state
== POOL_STATE_EXPORTED
)
4749 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
4751 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4753 spa_deactivate(spa
);
4756 if (oldconfig
&& spa
->spa_config
)
4757 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4759 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4761 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
);
4764 mutex_exit(&spa_namespace_lock
);
4770 * Destroy a storage pool.
4773 spa_destroy(char *pool
)
4775 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4780 * Export a storage pool.
4783 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4784 boolean_t hardforce
)
4786 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4791 * Similar to spa_export(), this unloads the spa_t without actually removing it
4792 * from the namespace in any way.
4795 spa_reset(char *pool
)
4797 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4802 * ==========================================================================
4803 * Device manipulation
4804 * ==========================================================================
4808 * Add a device to a storage pool.
4811 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4815 vdev_t
*rvd
= spa
->spa_root_vdev
;
4817 nvlist_t
**spares
, **l2cache
;
4818 uint_t nspares
, nl2cache
;
4820 ASSERT(spa_writeable(spa
));
4822 txg
= spa_vdev_enter(spa
);
4824 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4825 VDEV_ALLOC_ADD
)) != 0)
4826 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4828 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4830 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4834 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4838 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4839 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4841 if (vd
->vdev_children
!= 0 &&
4842 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4843 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4846 * We must validate the spares and l2cache devices after checking the
4847 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4849 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4850 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4853 * If we are in the middle of a device removal, we can only add
4854 * devices which match the existing devices in the pool.
4855 * If we are in the middle of a removal, or have some indirect
4856 * vdevs, we can not add raidz toplevels.
4858 if (spa
->spa_vdev_removal
!= NULL
||
4859 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
4860 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4861 tvd
= vd
->vdev_child
[c
];
4862 if (spa
->spa_vdev_removal
!= NULL
&&
4864 spa
->spa_vdev_removal
->svr_vdev
->vdev_ashift
) {
4865 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4867 /* Fail if top level vdev is raidz */
4868 if (tvd
->vdev_ops
== &vdev_raidz_ops
) {
4869 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4872 * Need the top level mirror to be
4873 * a mirror of leaf vdevs only
4875 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
4876 for (uint64_t cid
= 0;
4877 cid
< tvd
->vdev_children
; cid
++) {
4878 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
4879 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
4880 return (spa_vdev_exit(spa
, vd
,
4888 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
4891 * Set the vdev id to the first hole, if one exists.
4893 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4894 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4895 vdev_free(rvd
->vdev_child
[id
]);
4899 tvd
= vd
->vdev_child
[c
];
4900 vdev_remove_child(vd
, tvd
);
4902 vdev_add_child(rvd
, tvd
);
4903 vdev_config_dirty(tvd
);
4907 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4908 ZPOOL_CONFIG_SPARES
);
4909 spa_load_spares(spa
);
4910 spa
->spa_spares
.sav_sync
= B_TRUE
;
4913 if (nl2cache
!= 0) {
4914 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4915 ZPOOL_CONFIG_L2CACHE
);
4916 spa_load_l2cache(spa
);
4917 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4921 * We have to be careful when adding new vdevs to an existing pool.
4922 * If other threads start allocating from these vdevs before we
4923 * sync the config cache, and we lose power, then upon reboot we may
4924 * fail to open the pool because there are DVAs that the config cache
4925 * can't translate. Therefore, we first add the vdevs without
4926 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4927 * and then let spa_config_update() initialize the new metaslabs.
4929 * spa_load() checks for added-but-not-initialized vdevs, so that
4930 * if we lose power at any point in this sequence, the remaining
4931 * steps will be completed the next time we load the pool.
4933 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4935 mutex_enter(&spa_namespace_lock
);
4936 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4937 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
4938 mutex_exit(&spa_namespace_lock
);
4944 * Attach a device to a mirror. The arguments are the path to any device
4945 * in the mirror, and the nvroot for the new device. If the path specifies
4946 * a device that is not mirrored, we automatically insert the mirror vdev.
4948 * If 'replacing' is specified, the new device is intended to replace the
4949 * existing device; in this case the two devices are made into their own
4950 * mirror using the 'replacing' vdev, which is functionally identical to
4951 * the mirror vdev (it actually reuses all the same ops) but has a few
4952 * extra rules: you can't attach to it after it's been created, and upon
4953 * completion of resilvering, the first disk (the one being replaced)
4954 * is automatically detached.
4957 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4959 uint64_t txg
, dtl_max_txg
;
4960 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4961 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4963 char *oldvdpath
, *newvdpath
;
4967 ASSERT(spa_writeable(spa
));
4969 txg
= spa_vdev_enter(spa
);
4971 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4973 if (spa
->spa_vdev_removal
!= NULL
||
4974 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
4975 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4979 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4981 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4982 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4984 pvd
= oldvd
->vdev_parent
;
4986 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4987 VDEV_ALLOC_ATTACH
)) != 0)
4988 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4990 if (newrootvd
->vdev_children
!= 1)
4991 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4993 newvd
= newrootvd
->vdev_child
[0];
4995 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4996 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4998 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4999 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
5002 * Spares can't replace logs
5004 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
5005 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5009 * For attach, the only allowable parent is a mirror or the root
5012 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
5013 pvd
->vdev_ops
!= &vdev_root_ops
)
5014 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5016 pvops
= &vdev_mirror_ops
;
5019 * Active hot spares can only be replaced by inactive hot
5022 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5023 oldvd
->vdev_isspare
&&
5024 !spa_has_spare(spa
, newvd
->vdev_guid
))
5025 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5028 * If the source is a hot spare, and the parent isn't already a
5029 * spare, then we want to create a new hot spare. Otherwise, we
5030 * want to create a replacing vdev. The user is not allowed to
5031 * attach to a spared vdev child unless the 'isspare' state is
5032 * the same (spare replaces spare, non-spare replaces
5035 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
5036 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
5037 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5038 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5039 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
5040 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
5043 if (newvd
->vdev_isspare
)
5044 pvops
= &vdev_spare_ops
;
5046 pvops
= &vdev_replacing_ops
;
5050 * Make sure the new device is big enough.
5052 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
5053 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
5056 * The new device cannot have a higher alignment requirement
5057 * than the top-level vdev.
5059 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
5060 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
5063 * If this is an in-place replacement, update oldvd's path and devid
5064 * to make it distinguishable from newvd, and unopenable from now on.
5066 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
5067 spa_strfree(oldvd
->vdev_path
);
5068 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
5070 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
5071 newvd
->vdev_path
, "old");
5072 if (oldvd
->vdev_devid
!= NULL
) {
5073 spa_strfree(oldvd
->vdev_devid
);
5074 oldvd
->vdev_devid
= NULL
;
5078 /* mark the device being resilvered */
5079 newvd
->vdev_resilver_txg
= txg
;
5082 * If the parent is not a mirror, or if we're replacing, insert the new
5083 * mirror/replacing/spare vdev above oldvd.
5085 if (pvd
->vdev_ops
!= pvops
)
5086 pvd
= vdev_add_parent(oldvd
, pvops
);
5088 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
5089 ASSERT(pvd
->vdev_ops
== pvops
);
5090 ASSERT(oldvd
->vdev_parent
== pvd
);
5093 * Extract the new device from its root and add it to pvd.
5095 vdev_remove_child(newrootvd
, newvd
);
5096 newvd
->vdev_id
= pvd
->vdev_children
;
5097 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
5098 vdev_add_child(pvd
, newvd
);
5101 * Reevaluate the parent vdev state.
5103 vdev_propagate_state(pvd
);
5105 tvd
= newvd
->vdev_top
;
5106 ASSERT(pvd
->vdev_top
== tvd
);
5107 ASSERT(tvd
->vdev_parent
== rvd
);
5109 vdev_config_dirty(tvd
);
5112 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
5113 * for any dmu_sync-ed blocks. It will propagate upward when
5114 * spa_vdev_exit() calls vdev_dtl_reassess().
5116 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
5118 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
5119 dtl_max_txg
- TXG_INITIAL
);
5121 if (newvd
->vdev_isspare
) {
5122 spa_spare_activate(newvd
);
5123 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
5126 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
5127 newvdpath
= spa_strdup(newvd
->vdev_path
);
5128 newvd_isspare
= newvd
->vdev_isspare
;
5131 * Mark newvd's DTL dirty in this txg.
5133 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
5136 * Schedule the resilver to restart in the future. We do this to
5137 * ensure that dmu_sync-ed blocks have been stitched into the
5138 * respective datasets.
5140 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
5142 if (spa
->spa_bootfs
)
5143 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
5145 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
5150 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
5152 spa_history_log_internal(spa
, "vdev attach", NULL
,
5153 "%s vdev=%s %s vdev=%s",
5154 replacing
&& newvd_isspare
? "spare in" :
5155 replacing
? "replace" : "attach", newvdpath
,
5156 replacing
? "for" : "to", oldvdpath
);
5158 spa_strfree(oldvdpath
);
5159 spa_strfree(newvdpath
);
5165 * Detach a device from a mirror or replacing vdev.
5167 * If 'replace_done' is specified, only detach if the parent
5168 * is a replacing vdev.
5171 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
5175 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
5176 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
5177 boolean_t unspare
= B_FALSE
;
5178 uint64_t unspare_guid
= 0;
5181 ASSERT(spa_writeable(spa
));
5183 txg
= spa_vdev_enter(spa
);
5185 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5188 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
5190 if (!vd
->vdev_ops
->vdev_op_leaf
)
5191 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5193 pvd
= vd
->vdev_parent
;
5196 * If the parent/child relationship is not as expected, don't do it.
5197 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
5198 * vdev that's replacing B with C. The user's intent in replacing
5199 * is to go from M(A,B) to M(A,C). If the user decides to cancel
5200 * the replace by detaching C, the expected behavior is to end up
5201 * M(A,B). But suppose that right after deciding to detach C,
5202 * the replacement of B completes. We would have M(A,C), and then
5203 * ask to detach C, which would leave us with just A -- not what
5204 * the user wanted. To prevent this, we make sure that the
5205 * parent/child relationship hasn't changed -- in this example,
5206 * that C's parent is still the replacing vdev R.
5208 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
5209 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5212 * Only 'replacing' or 'spare' vdevs can be replaced.
5214 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
5215 pvd
->vdev_ops
!= &vdev_spare_ops
)
5216 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5218 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
5219 spa_version(spa
) >= SPA_VERSION_SPARES
);
5222 * Only mirror, replacing, and spare vdevs support detach.
5224 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
5225 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
5226 pvd
->vdev_ops
!= &vdev_spare_ops
)
5227 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
5230 * If this device has the only valid copy of some data,
5231 * we cannot safely detach it.
5233 if (vdev_dtl_required(vd
))
5234 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
5236 ASSERT(pvd
->vdev_children
>= 2);
5239 * If we are detaching the second disk from a replacing vdev, then
5240 * check to see if we changed the original vdev's path to have "/old"
5241 * at the end in spa_vdev_attach(). If so, undo that change now.
5243 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
5244 vd
->vdev_path
!= NULL
) {
5245 size_t len
= strlen(vd
->vdev_path
);
5247 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
5248 cvd
= pvd
->vdev_child
[c
];
5250 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
5253 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
5254 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
5255 spa_strfree(cvd
->vdev_path
);
5256 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
5263 * If we are detaching the original disk from a spare, then it implies
5264 * that the spare should become a real disk, and be removed from the
5265 * active spare list for the pool.
5267 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
5269 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
5273 * Erase the disk labels so the disk can be used for other things.
5274 * This must be done after all other error cases are handled,
5275 * but before we disembowel vd (so we can still do I/O to it).
5276 * But if we can't do it, don't treat the error as fatal --
5277 * it may be that the unwritability of the disk is the reason
5278 * it's being detached!
5280 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5283 * Remove vd from its parent and compact the parent's children.
5285 vdev_remove_child(pvd
, vd
);
5286 vdev_compact_children(pvd
);
5289 * Remember one of the remaining children so we can get tvd below.
5291 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
5294 * If we need to remove the remaining child from the list of hot spares,
5295 * do it now, marking the vdev as no longer a spare in the process.
5296 * We must do this before vdev_remove_parent(), because that can
5297 * change the GUID if it creates a new toplevel GUID. For a similar
5298 * reason, we must remove the spare now, in the same txg as the detach;
5299 * otherwise someone could attach a new sibling, change the GUID, and
5300 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
5303 ASSERT(cvd
->vdev_isspare
);
5304 spa_spare_remove(cvd
);
5305 unspare_guid
= cvd
->vdev_guid
;
5306 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
5307 cvd
->vdev_unspare
= B_TRUE
;
5311 * If the parent mirror/replacing vdev only has one child,
5312 * the parent is no longer needed. Remove it from the tree.
5314 if (pvd
->vdev_children
== 1) {
5315 if (pvd
->vdev_ops
== &vdev_spare_ops
)
5316 cvd
->vdev_unspare
= B_FALSE
;
5317 vdev_remove_parent(cvd
);
5322 * We don't set tvd until now because the parent we just removed
5323 * may have been the previous top-level vdev.
5325 tvd
= cvd
->vdev_top
;
5326 ASSERT(tvd
->vdev_parent
== rvd
);
5329 * Reevaluate the parent vdev state.
5331 vdev_propagate_state(cvd
);
5334 * If the 'autoexpand' property is set on the pool then automatically
5335 * try to expand the size of the pool. For example if the device we
5336 * just detached was smaller than the others, it may be possible to
5337 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
5338 * first so that we can obtain the updated sizes of the leaf vdevs.
5340 if (spa
->spa_autoexpand
) {
5342 vdev_expand(tvd
, txg
);
5345 vdev_config_dirty(tvd
);
5348 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
5349 * vd->vdev_detached is set and free vd's DTL object in syncing context.
5350 * But first make sure we're not on any *other* txg's DTL list, to
5351 * prevent vd from being accessed after it's freed.
5353 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
5354 for (int t
= 0; t
< TXG_SIZE
; t
++)
5355 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
5356 vd
->vdev_detached
= B_TRUE
;
5357 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
5359 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
5361 /* hang on to the spa before we release the lock */
5362 spa_open_ref(spa
, FTAG
);
5364 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
5366 spa_history_log_internal(spa
, "detach", NULL
,
5368 spa_strfree(vdpath
);
5371 * If this was the removal of the original device in a hot spare vdev,
5372 * then we want to go through and remove the device from the hot spare
5373 * list of every other pool.
5376 spa_t
*altspa
= NULL
;
5378 mutex_enter(&spa_namespace_lock
);
5379 while ((altspa
= spa_next(altspa
)) != NULL
) {
5380 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
5384 spa_open_ref(altspa
, FTAG
);
5385 mutex_exit(&spa_namespace_lock
);
5386 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
5387 mutex_enter(&spa_namespace_lock
);
5388 spa_close(altspa
, FTAG
);
5390 mutex_exit(&spa_namespace_lock
);
5392 /* search the rest of the vdevs for spares to remove */
5393 spa_vdev_resilver_done(spa
);
5396 /* all done with the spa; OK to release */
5397 mutex_enter(&spa_namespace_lock
);
5398 spa_close(spa
, FTAG
);
5399 mutex_exit(&spa_namespace_lock
);
5405 * Split a set of devices from their mirrors, and create a new pool from them.
5408 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5409 nvlist_t
*props
, boolean_t exp
)
5412 uint64_t txg
, *glist
;
5414 uint_t c
, children
, lastlog
;
5415 nvlist_t
**child
, *nvl
, *tmp
;
5417 char *altroot
= NULL
;
5418 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5419 boolean_t activate_slog
;
5421 ASSERT(spa_writeable(spa
));
5423 txg
= spa_vdev_enter(spa
);
5425 /* clear the log and flush everything up to now */
5426 activate_slog
= spa_passivate_log(spa
);
5427 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5428 error
= spa_reset_logs(spa
);
5429 txg
= spa_vdev_config_enter(spa
);
5432 spa_activate_log(spa
);
5435 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5437 /* check new spa name before going any further */
5438 if (spa_lookup(newname
) != NULL
)
5439 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5442 * scan through all the children to ensure they're all mirrors
5444 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5445 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5447 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5449 /* first, check to ensure we've got the right child count */
5450 rvd
= spa
->spa_root_vdev
;
5452 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5453 vdev_t
*vd
= rvd
->vdev_child
[c
];
5455 /* don't count the holes & logs as children */
5456 if (vd
->vdev_islog
|| !vdev_is_concrete(vd
)) {
5464 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5465 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5467 /* next, ensure no spare or cache devices are part of the split */
5468 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5469 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5470 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5472 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5473 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5475 /* then, loop over each vdev and validate it */
5476 for (c
= 0; c
< children
; c
++) {
5477 uint64_t is_hole
= 0;
5479 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5483 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5484 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5487 error
= SET_ERROR(EINVAL
);
5492 /* which disk is going to be split? */
5493 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5495 error
= SET_ERROR(EINVAL
);
5499 /* look it up in the spa */
5500 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5501 if (vml
[c
] == NULL
) {
5502 error
= SET_ERROR(ENODEV
);
5506 /* make sure there's nothing stopping the split */
5507 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5508 vml
[c
]->vdev_islog
||
5509 !vdev_is_concrete(vml
[c
]) ||
5510 vml
[c
]->vdev_isspare
||
5511 vml
[c
]->vdev_isl2cache
||
5512 !vdev_writeable(vml
[c
]) ||
5513 vml
[c
]->vdev_children
!= 0 ||
5514 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5515 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5516 error
= SET_ERROR(EINVAL
);
5520 if (vdev_dtl_required(vml
[c
])) {
5521 error
= SET_ERROR(EBUSY
);
5525 /* we need certain info from the top level */
5526 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5527 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5528 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5529 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5530 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5531 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5532 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5533 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5535 /* transfer per-vdev ZAPs */
5536 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
5537 VERIFY0(nvlist_add_uint64(child
[c
],
5538 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
5540 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
5541 VERIFY0(nvlist_add_uint64(child
[c
],
5542 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
5543 vml
[c
]->vdev_parent
->vdev_top_zap
));
5547 kmem_free(vml
, children
* sizeof (vdev_t
*));
5548 kmem_free(glist
, children
* sizeof (uint64_t));
5549 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5552 /* stop writers from using the disks */
5553 for (c
= 0; c
< children
; c
++) {
5555 vml
[c
]->vdev_offline
= B_TRUE
;
5557 vdev_reopen(spa
->spa_root_vdev
);
5560 * Temporarily record the splitting vdevs in the spa config. This
5561 * will disappear once the config is regenerated.
5563 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5564 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5565 glist
, children
) == 0);
5566 kmem_free(glist
, children
* sizeof (uint64_t));
5568 mutex_enter(&spa
->spa_props_lock
);
5569 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5571 mutex_exit(&spa
->spa_props_lock
);
5572 spa
->spa_config_splitting
= nvl
;
5573 vdev_config_dirty(spa
->spa_root_vdev
);
5575 /* configure and create the new pool */
5576 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5577 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5578 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5579 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5580 spa_version(spa
)) == 0);
5581 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5582 spa
->spa_config_txg
) == 0);
5583 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5584 spa_generate_guid(NULL
)) == 0);
5585 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
5586 (void) nvlist_lookup_string(props
,
5587 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5589 /* add the new pool to the namespace */
5590 newspa
= spa_add(newname
, config
, altroot
);
5591 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5592 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5593 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5595 /* release the spa config lock, retaining the namespace lock */
5596 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5598 if (zio_injection_enabled
)
5599 zio_handle_panic_injection(spa
, FTAG
, 1);
5601 spa_activate(newspa
, spa_mode_global
);
5602 spa_async_suspend(newspa
);
5604 /* create the new pool from the disks of the original pool */
5605 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5609 /* if that worked, generate a real config for the new pool */
5610 if (newspa
->spa_root_vdev
!= NULL
) {
5611 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5612 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5613 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5614 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5615 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5620 if (props
!= NULL
) {
5621 spa_configfile_set(newspa
, props
, B_FALSE
);
5622 error
= spa_prop_set(newspa
, props
);
5627 /* flush everything */
5628 txg
= spa_vdev_config_enter(newspa
);
5629 vdev_config_dirty(newspa
->spa_root_vdev
);
5630 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5632 if (zio_injection_enabled
)
5633 zio_handle_panic_injection(spa
, FTAG
, 2);
5635 spa_async_resume(newspa
);
5637 /* finally, update the original pool's config */
5638 txg
= spa_vdev_config_enter(spa
);
5639 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5640 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5643 for (c
= 0; c
< children
; c
++) {
5644 if (vml
[c
] != NULL
) {
5647 spa_history_log_internal(spa
, "detach", tx
,
5648 "vdev=%s", vml
[c
]->vdev_path
);
5653 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5654 vdev_config_dirty(spa
->spa_root_vdev
);
5655 spa
->spa_config_splitting
= NULL
;
5659 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5661 if (zio_injection_enabled
)
5662 zio_handle_panic_injection(spa
, FTAG
, 3);
5664 /* split is complete; log a history record */
5665 spa_history_log_internal(newspa
, "split", NULL
,
5666 "from pool %s", spa_name(spa
));
5668 kmem_free(vml
, children
* sizeof (vdev_t
*));
5670 /* if we're not going to mount the filesystems in userland, export */
5672 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5679 spa_deactivate(newspa
);
5682 txg
= spa_vdev_config_enter(spa
);
5684 /* re-online all offlined disks */
5685 for (c
= 0; c
< children
; c
++) {
5687 vml
[c
]->vdev_offline
= B_FALSE
;
5689 vdev_reopen(spa
->spa_root_vdev
);
5691 nvlist_free(spa
->spa_config_splitting
);
5692 spa
->spa_config_splitting
= NULL
;
5693 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5695 kmem_free(vml
, children
* sizeof (vdev_t
*));
5700 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5701 * currently spared, so we can detach it.
5704 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5706 vdev_t
*newvd
, *oldvd
;
5708 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5709 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5715 * Check for a completed replacement. We always consider the first
5716 * vdev in the list to be the oldest vdev, and the last one to be
5717 * the newest (see spa_vdev_attach() for how that works). In
5718 * the case where the newest vdev is faulted, we will not automatically
5719 * remove it after a resilver completes. This is OK as it will require
5720 * user intervention to determine which disk the admin wishes to keep.
5722 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5723 ASSERT(vd
->vdev_children
> 1);
5725 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5726 oldvd
= vd
->vdev_child
[0];
5728 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5729 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5730 !vdev_dtl_required(oldvd
))
5735 * Check for a completed resilver with the 'unspare' flag set.
5737 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5738 vdev_t
*first
= vd
->vdev_child
[0];
5739 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5741 if (last
->vdev_unspare
) {
5744 } else if (first
->vdev_unspare
) {
5751 if (oldvd
!= NULL
&&
5752 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5753 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5754 !vdev_dtl_required(oldvd
))
5758 * If there are more than two spares attached to a disk,
5759 * and those spares are not required, then we want to
5760 * attempt to free them up now so that they can be used
5761 * by other pools. Once we're back down to a single
5762 * disk+spare, we stop removing them.
5764 if (vd
->vdev_children
> 2) {
5765 newvd
= vd
->vdev_child
[1];
5767 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5768 vdev_dtl_empty(last
, DTL_MISSING
) &&
5769 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5770 !vdev_dtl_required(newvd
))
5779 spa_vdev_resilver_done(spa_t
*spa
)
5781 vdev_t
*vd
, *pvd
, *ppvd
;
5782 uint64_t guid
, sguid
, pguid
, ppguid
;
5784 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5786 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5787 pvd
= vd
->vdev_parent
;
5788 ppvd
= pvd
->vdev_parent
;
5789 guid
= vd
->vdev_guid
;
5790 pguid
= pvd
->vdev_guid
;
5791 ppguid
= ppvd
->vdev_guid
;
5794 * If we have just finished replacing a hot spared device, then
5795 * we need to detach the parent's first child (the original hot
5798 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5799 ppvd
->vdev_children
== 2) {
5800 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5801 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5803 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5805 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5806 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5808 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5810 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5813 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5817 * Update the stored path or FRU for this vdev.
5820 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5824 boolean_t sync
= B_FALSE
;
5826 ASSERT(spa_writeable(spa
));
5828 spa_vdev_state_enter(spa
, SCL_ALL
);
5830 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5831 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5833 if (!vd
->vdev_ops
->vdev_op_leaf
)
5834 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5837 if (strcmp(value
, vd
->vdev_path
) != 0) {
5838 spa_strfree(vd
->vdev_path
);
5839 vd
->vdev_path
= spa_strdup(value
);
5843 if (vd
->vdev_fru
== NULL
) {
5844 vd
->vdev_fru
= spa_strdup(value
);
5846 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5847 spa_strfree(vd
->vdev_fru
);
5848 vd
->vdev_fru
= spa_strdup(value
);
5853 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5857 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5859 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5863 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5865 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5869 * ==========================================================================
5871 * ==========================================================================
5874 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
5876 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5878 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5879 return (SET_ERROR(EBUSY
));
5881 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
5885 spa_scan_stop(spa_t
*spa
)
5887 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5888 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5889 return (SET_ERROR(EBUSY
));
5890 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5894 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5896 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5898 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5899 return (SET_ERROR(ENOTSUP
));
5902 * If a resilver was requested, but there is no DTL on a
5903 * writeable leaf device, we have nothing to do.
5905 if (func
== POOL_SCAN_RESILVER
&&
5906 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5907 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5911 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5915 * ==========================================================================
5916 * SPA async task processing
5917 * ==========================================================================
5921 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5923 if (vd
->vdev_remove_wanted
) {
5924 vd
->vdev_remove_wanted
= B_FALSE
;
5925 vd
->vdev_delayed_close
= B_FALSE
;
5926 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5929 * We want to clear the stats, but we don't want to do a full
5930 * vdev_clear() as that will cause us to throw away
5931 * degraded/faulted state as well as attempt to reopen the
5932 * device, all of which is a waste.
5934 vd
->vdev_stat
.vs_read_errors
= 0;
5935 vd
->vdev_stat
.vs_write_errors
= 0;
5936 vd
->vdev_stat
.vs_checksum_errors
= 0;
5938 vdev_state_dirty(vd
->vdev_top
);
5941 for (int c
= 0; c
< vd
->vdev_children
; c
++)
5942 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5946 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5948 if (vd
->vdev_probe_wanted
) {
5949 vd
->vdev_probe_wanted
= B_FALSE
;
5950 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5953 for (int c
= 0; c
< vd
->vdev_children
; c
++)
5954 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5958 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5960 if (!spa
->spa_autoexpand
)
5963 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
5964 vdev_t
*cvd
= vd
->vdev_child
[c
];
5965 spa_async_autoexpand(spa
, cvd
);
5968 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5971 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
5975 spa_async_thread(void *arg
)
5977 spa_t
*spa
= (spa_t
*)arg
;
5980 ASSERT(spa
->spa_sync_on
);
5982 mutex_enter(&spa
->spa_async_lock
);
5983 tasks
= spa
->spa_async_tasks
;
5984 spa
->spa_async_tasks
= 0;
5985 mutex_exit(&spa
->spa_async_lock
);
5988 * See if the config needs to be updated.
5990 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5991 uint64_t old_space
, new_space
;
5993 mutex_enter(&spa_namespace_lock
);
5994 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5995 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5996 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5997 mutex_exit(&spa_namespace_lock
);
6000 * If the pool grew as a result of the config update,
6001 * then log an internal history event.
6003 if (new_space
!= old_space
) {
6004 spa_history_log_internal(spa
, "vdev online", NULL
,
6005 "pool '%s' size: %llu(+%llu)",
6006 spa_name(spa
), new_space
, new_space
- old_space
);
6011 * See if any devices need to be marked REMOVED.
6013 if (tasks
& SPA_ASYNC_REMOVE
) {
6014 spa_vdev_state_enter(spa
, SCL_NONE
);
6015 spa_async_remove(spa
, spa
->spa_root_vdev
);
6016 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
6017 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
6018 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
6019 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
6020 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6023 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
6024 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6025 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
6026 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6030 * See if any devices need to be probed.
6032 if (tasks
& SPA_ASYNC_PROBE
) {
6033 spa_vdev_state_enter(spa
, SCL_NONE
);
6034 spa_async_probe(spa
, spa
->spa_root_vdev
);
6035 (void) spa_vdev_state_exit(spa
, NULL
, 0);
6039 * If any devices are done replacing, detach them.
6041 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
6042 spa_vdev_resilver_done(spa
);
6045 * Kick off a resilver.
6047 if (tasks
& SPA_ASYNC_RESILVER
)
6048 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
6051 * Let the world know that we're done.
6053 mutex_enter(&spa
->spa_async_lock
);
6054 spa
->spa_async_thread
= NULL
;
6055 cv_broadcast(&spa
->spa_async_cv
);
6056 mutex_exit(&spa
->spa_async_lock
);
6061 spa_async_suspend(spa_t
*spa
)
6063 mutex_enter(&spa
->spa_async_lock
);
6064 spa
->spa_async_suspended
++;
6065 while (spa
->spa_async_thread
!= NULL
||
6066 spa
->spa_condense_thread
!= NULL
)
6067 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
6068 mutex_exit(&spa
->spa_async_lock
);
6070 spa_vdev_remove_suspend(spa
);
6074 spa_async_resume(spa_t
*spa
)
6076 mutex_enter(&spa
->spa_async_lock
);
6077 ASSERT(spa
->spa_async_suspended
!= 0);
6078 spa
->spa_async_suspended
--;
6079 mutex_exit(&spa
->spa_async_lock
);
6080 spa_restart_removal(spa
);
6084 spa_async_tasks_pending(spa_t
*spa
)
6086 uint_t non_config_tasks
;
6088 boolean_t config_task_suspended
;
6090 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
6091 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
6092 if (spa
->spa_ccw_fail_time
== 0) {
6093 config_task_suspended
= B_FALSE
;
6095 config_task_suspended
=
6096 (gethrtime() - spa
->spa_ccw_fail_time
) <
6097 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
6100 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
6104 spa_async_dispatch(spa_t
*spa
)
6106 mutex_enter(&spa
->spa_async_lock
);
6107 if (spa_async_tasks_pending(spa
) &&
6108 !spa
->spa_async_suspended
&&
6109 spa
->spa_async_thread
== NULL
&&
6111 spa
->spa_async_thread
= thread_create(NULL
, 0,
6112 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
6113 mutex_exit(&spa
->spa_async_lock
);
6117 spa_async_request(spa_t
*spa
, int task
)
6119 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
6120 mutex_enter(&spa
->spa_async_lock
);
6121 spa
->spa_async_tasks
|= task
;
6122 mutex_exit(&spa
->spa_async_lock
);
6126 * ==========================================================================
6127 * SPA syncing routines
6128 * ==========================================================================
6132 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6135 bpobj_enqueue(bpo
, bp
, tx
);
6140 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
6144 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
6150 * Note: this simple function is not inlined to make it easier to dtrace the
6151 * amount of time spent syncing frees.
6154 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
6156 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6157 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
6158 VERIFY(zio_wait(zio
) == 0);
6162 * Note: this simple function is not inlined to make it easier to dtrace the
6163 * amount of time spent syncing deferred frees.
6166 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
6168 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6169 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
6170 spa_free_sync_cb
, zio
, tx
), ==, 0);
6171 VERIFY0(zio_wait(zio
));
6175 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
6177 char *packed
= NULL
;
6182 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
6185 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
6186 * information. This avoids the dmu_buf_will_dirty() path and
6187 * saves us a pre-read to get data we don't actually care about.
6189 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
6190 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
6192 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6194 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6196 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6198 vmem_free(packed
, bufsize
);
6200 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6201 dmu_buf_will_dirty(db
, tx
);
6202 *(uint64_t *)db
->db_data
= nvsize
;
6203 dmu_buf_rele(db
, FTAG
);
6207 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6208 const char *config
, const char *entry
)
6218 * Update the MOS nvlist describing the list of available devices.
6219 * spa_validate_aux() will have already made sure this nvlist is
6220 * valid and the vdevs are labeled appropriately.
6222 if (sav
->sav_object
== 0) {
6223 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6224 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6225 sizeof (uint64_t), tx
);
6226 VERIFY(zap_update(spa
->spa_meta_objset
,
6227 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6228 &sav
->sav_object
, tx
) == 0);
6231 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6232 if (sav
->sav_count
== 0) {
6233 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6235 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6236 for (i
= 0; i
< sav
->sav_count
; i
++)
6237 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6238 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6239 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6240 sav
->sav_count
) == 0);
6241 for (i
= 0; i
< sav
->sav_count
; i
++)
6242 nvlist_free(list
[i
]);
6243 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6246 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6247 nvlist_free(nvroot
);
6249 sav
->sav_sync
= B_FALSE
;
6253 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6254 * The all-vdev ZAP must be empty.
6257 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6259 spa_t
*spa
= vd
->vdev_spa
;
6261 if (vd
->vdev_top_zap
!= 0) {
6262 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6263 vd
->vdev_top_zap
, tx
));
6265 if (vd
->vdev_leaf_zap
!= 0) {
6266 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6267 vd
->vdev_leaf_zap
, tx
));
6269 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
6270 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6275 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6280 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6281 * its config may not be dirty but we still need to build per-vdev ZAPs.
6282 * Similarly, if the pool is being assembled (e.g. after a split), we
6283 * need to rebuild the AVZ although the config may not be dirty.
6285 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6286 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6289 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6291 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6292 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
6293 spa
->spa_all_vdev_zaps
!= 0);
6295 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6296 /* Make and build the new AVZ */
6297 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6298 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6299 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6301 /* Diff old AVZ with new one */
6305 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6306 spa
->spa_all_vdev_zaps
);
6307 zap_cursor_retrieve(&zc
, &za
) == 0;
6308 zap_cursor_advance(&zc
)) {
6309 uint64_t vdzap
= za
.za_first_integer
;
6310 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6313 * ZAP is listed in old AVZ but not in new one;
6316 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6321 zap_cursor_fini(&zc
);
6323 /* Destroy the old AVZ */
6324 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6325 spa
->spa_all_vdev_zaps
, tx
));
6327 /* Replace the old AVZ in the dir obj with the new one */
6328 VERIFY0(zap_update(spa
->spa_meta_objset
,
6329 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6330 sizeof (new_avz
), 1, &new_avz
, tx
));
6332 spa
->spa_all_vdev_zaps
= new_avz
;
6333 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6337 /* Walk through the AVZ and destroy all listed ZAPs */
6338 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6339 spa
->spa_all_vdev_zaps
);
6340 zap_cursor_retrieve(&zc
, &za
) == 0;
6341 zap_cursor_advance(&zc
)) {
6342 uint64_t zap
= za
.za_first_integer
;
6343 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6346 zap_cursor_fini(&zc
);
6348 /* Destroy and unlink the AVZ itself */
6349 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6350 spa
->spa_all_vdev_zaps
, tx
));
6351 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6352 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6353 spa
->spa_all_vdev_zaps
= 0;
6356 if (spa
->spa_all_vdev_zaps
== 0) {
6357 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6358 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6359 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6361 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6363 /* Create ZAPs for vdevs that don't have them. */
6364 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6366 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6367 dmu_tx_get_txg(tx
), B_FALSE
);
6370 * If we're upgrading the spa version then make sure that
6371 * the config object gets updated with the correct version.
6373 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6374 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6375 spa
->spa_uberblock
.ub_version
);
6377 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6379 nvlist_free(spa
->spa_config_syncing
);
6380 spa
->spa_config_syncing
= config
;
6382 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6386 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6388 uint64_t *versionp
= arg
;
6389 uint64_t version
= *versionp
;
6390 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6393 * Setting the version is special cased when first creating the pool.
6395 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6397 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6398 ASSERT(version
>= spa_version(spa
));
6400 spa
->spa_uberblock
.ub_version
= version
;
6401 vdev_config_dirty(spa
->spa_root_vdev
);
6402 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6406 * Set zpool properties.
6409 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6411 nvlist_t
*nvp
= arg
;
6412 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6413 objset_t
*mos
= spa
->spa_meta_objset
;
6414 nvpair_t
*elem
= NULL
;
6416 mutex_enter(&spa
->spa_props_lock
);
6418 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6420 char *strval
, *fname
;
6422 const char *propname
;
6423 zprop_type_t proptype
;
6426 switch (prop
= zpool_name_to_prop(nvpair_name(elem
))) {
6427 case ZPOOL_PROP_INVAL
:
6429 * We checked this earlier in spa_prop_validate().
6431 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6433 fname
= strchr(nvpair_name(elem
), '@') + 1;
6434 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6436 spa_feature_enable(spa
, fid
, tx
);
6437 spa_history_log_internal(spa
, "set", tx
,
6438 "%s=enabled", nvpair_name(elem
));
6441 case ZPOOL_PROP_VERSION
:
6442 intval
= fnvpair_value_uint64(elem
);
6444 * The version is synced separately before other
6445 * properties and should be correct by now.
6447 ASSERT3U(spa_version(spa
), >=, intval
);
6450 case ZPOOL_PROP_ALTROOT
:
6452 * 'altroot' is a non-persistent property. It should
6453 * have been set temporarily at creation or import time.
6455 ASSERT(spa
->spa_root
!= NULL
);
6458 case ZPOOL_PROP_READONLY
:
6459 case ZPOOL_PROP_CACHEFILE
:
6461 * 'readonly' and 'cachefile' are also non-persisitent
6465 case ZPOOL_PROP_COMMENT
:
6466 strval
= fnvpair_value_string(elem
);
6467 if (spa
->spa_comment
!= NULL
)
6468 spa_strfree(spa
->spa_comment
);
6469 spa
->spa_comment
= spa_strdup(strval
);
6471 * We need to dirty the configuration on all the vdevs
6472 * so that their labels get updated. It's unnecessary
6473 * to do this for pool creation since the vdev's
6474 * configuration has already been dirtied.
6476 if (tx
->tx_txg
!= TXG_INITIAL
)
6477 vdev_config_dirty(spa
->spa_root_vdev
);
6478 spa_history_log_internal(spa
, "set", tx
,
6479 "%s=%s", nvpair_name(elem
), strval
);
6483 * Set pool property values in the poolprops mos object.
6485 if (spa
->spa_pool_props_object
== 0) {
6486 spa
->spa_pool_props_object
=
6487 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6488 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6492 /* normalize the property name */
6493 propname
= zpool_prop_to_name(prop
);
6494 proptype
= zpool_prop_get_type(prop
);
6496 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6497 ASSERT(proptype
== PROP_TYPE_STRING
);
6498 strval
= fnvpair_value_string(elem
);
6499 VERIFY0(zap_update(mos
,
6500 spa
->spa_pool_props_object
, propname
,
6501 1, strlen(strval
) + 1, strval
, tx
));
6502 spa_history_log_internal(spa
, "set", tx
,
6503 "%s=%s", nvpair_name(elem
), strval
);
6504 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6505 intval
= fnvpair_value_uint64(elem
);
6507 if (proptype
== PROP_TYPE_INDEX
) {
6509 VERIFY0(zpool_prop_index_to_string(
6510 prop
, intval
, &unused
));
6512 VERIFY0(zap_update(mos
,
6513 spa
->spa_pool_props_object
, propname
,
6514 8, 1, &intval
, tx
));
6515 spa_history_log_internal(spa
, "set", tx
,
6516 "%s=%lld", nvpair_name(elem
), intval
);
6518 ASSERT(0); /* not allowed */
6522 case ZPOOL_PROP_DELEGATION
:
6523 spa
->spa_delegation
= intval
;
6525 case ZPOOL_PROP_BOOTFS
:
6526 spa
->spa_bootfs
= intval
;
6528 case ZPOOL_PROP_FAILUREMODE
:
6529 spa
->spa_failmode
= intval
;
6531 case ZPOOL_PROP_AUTOEXPAND
:
6532 spa
->spa_autoexpand
= intval
;
6533 if (tx
->tx_txg
!= TXG_INITIAL
)
6534 spa_async_request(spa
,
6535 SPA_ASYNC_AUTOEXPAND
);
6537 case ZPOOL_PROP_MULTIHOST
:
6538 spa
->spa_multihost
= intval
;
6540 case ZPOOL_PROP_DEDUPDITTO
:
6541 spa
->spa_dedup_ditto
= intval
;
6550 mutex_exit(&spa
->spa_props_lock
);
6554 * Perform one-time upgrade on-disk changes. spa_version() does not
6555 * reflect the new version this txg, so there must be no changes this
6556 * txg to anything that the upgrade code depends on after it executes.
6557 * Therefore this must be called after dsl_pool_sync() does the sync
6561 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6563 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6565 ASSERT(spa
->spa_sync_pass
== 1);
6567 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6569 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6570 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6571 dsl_pool_create_origin(dp
, tx
);
6573 /* Keeping the origin open increases spa_minref */
6574 spa
->spa_minref
+= 3;
6577 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6578 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6579 dsl_pool_upgrade_clones(dp
, tx
);
6582 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6583 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6584 dsl_pool_upgrade_dir_clones(dp
, tx
);
6586 /* Keeping the freedir open increases spa_minref */
6587 spa
->spa_minref
+= 3;
6590 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6591 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6592 spa_feature_create_zap_objects(spa
, tx
);
6596 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6597 * when possibility to use lz4 compression for metadata was added
6598 * Old pools that have this feature enabled must be upgraded to have
6599 * this feature active
6601 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6602 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6603 SPA_FEATURE_LZ4_COMPRESS
);
6604 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6605 SPA_FEATURE_LZ4_COMPRESS
);
6607 if (lz4_en
&& !lz4_ac
)
6608 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6612 * If we haven't written the salt, do so now. Note that the
6613 * feature may not be activated yet, but that's fine since
6614 * the presence of this ZAP entry is backwards compatible.
6616 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
6617 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
6618 VERIFY0(zap_add(spa
->spa_meta_objset
,
6619 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
6620 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
6621 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
6624 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6628 vdev_indirect_state_sync_verify(vdev_t
*vd
)
6630 ASSERTV(vdev_indirect_mapping_t
*vim
= vd
->vdev_indirect_mapping
);
6631 ASSERTV(vdev_indirect_births_t
*vib
= vd
->vdev_indirect_births
);
6633 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
6634 ASSERT(vim
!= NULL
);
6635 ASSERT(vib
!= NULL
);
6638 if (vdev_obsolete_sm_object(vd
) != 0) {
6639 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
6640 ASSERT(vd
->vdev_removing
||
6641 vd
->vdev_ops
== &vdev_indirect_ops
);
6642 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
6643 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
6645 ASSERT3U(vdev_obsolete_sm_object(vd
), ==,
6646 space_map_object(vd
->vdev_obsolete_sm
));
6647 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
6648 space_map_allocated(vd
->vdev_obsolete_sm
));
6650 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
6653 * Since frees / remaps to an indirect vdev can only
6654 * happen in syncing context, the obsolete segments
6655 * tree must be empty when we start syncing.
6657 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
6661 * Sync the specified transaction group. New blocks may be dirtied as
6662 * part of the process, so we iterate until it converges.
6665 spa_sync(spa_t
*spa
, uint64_t txg
)
6667 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6668 objset_t
*mos
= spa
->spa_meta_objset
;
6669 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6670 vdev_t
*rvd
= spa
->spa_root_vdev
;
6674 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
6675 zfs_vdev_queue_depth_pct
/ 100;
6677 VERIFY(spa_writeable(spa
));
6680 * Wait for i/os issued in open context that need to complete
6681 * before this txg syncs.
6683 VERIFY0(zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]));
6684 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
, 0);
6687 * Lock out configuration changes.
6689 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6691 spa
->spa_syncing_txg
= txg
;
6692 spa
->spa_sync_pass
= 0;
6694 mutex_enter(&spa
->spa_alloc_lock
);
6695 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6696 mutex_exit(&spa
->spa_alloc_lock
);
6699 * If there are any pending vdev state changes, convert them
6700 * into config changes that go out with this transaction group.
6702 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6703 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6705 * We need the write lock here because, for aux vdevs,
6706 * calling vdev_config_dirty() modifies sav_config.
6707 * This is ugly and will become unnecessary when we
6708 * eliminate the aux vdev wart by integrating all vdevs
6709 * into the root vdev tree.
6711 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6712 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6713 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6714 vdev_state_clean(vd
);
6715 vdev_config_dirty(vd
);
6717 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6718 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6720 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6722 tx
= dmu_tx_create_assigned(dp
, txg
);
6724 spa
->spa_sync_starttime
= gethrtime();
6725 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
6726 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
6727 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6728 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6731 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6732 * set spa_deflate if we have no raid-z vdevs.
6734 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6735 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6738 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6739 vd
= rvd
->vdev_child
[i
];
6740 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6743 if (i
== rvd
->vdev_children
) {
6744 spa
->spa_deflate
= TRUE
;
6745 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6746 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6747 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6752 * Set the top-level vdev's max queue depth. Evaluate each
6753 * top-level's async write queue depth in case it changed.
6754 * The max queue depth will not change in the middle of syncing
6757 uint64_t queue_depth_total
= 0;
6758 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
6759 vdev_t
*tvd
= rvd
->vdev_child
[c
];
6760 metaslab_group_t
*mg
= tvd
->vdev_mg
;
6762 if (mg
== NULL
|| mg
->mg_class
!= spa_normal_class(spa
) ||
6763 !metaslab_group_initialized(mg
))
6767 * It is safe to do a lock-free check here because only async
6768 * allocations look at mg_max_alloc_queue_depth, and async
6769 * allocations all happen from spa_sync().
6771 ASSERT0(refcount_count(&mg
->mg_alloc_queue_depth
));
6772 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
6773 queue_depth_total
+= mg
->mg_max_alloc_queue_depth
;
6775 metaslab_class_t
*mc
= spa_normal_class(spa
);
6776 ASSERT0(refcount_count(&mc
->mc_alloc_slots
));
6777 mc
->mc_alloc_max_slots
= queue_depth_total
;
6778 mc
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
6780 ASSERT3U(mc
->mc_alloc_max_slots
, <=,
6781 max_queue_depth
* rvd
->vdev_children
);
6783 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
6784 vdev_t
*vd
= rvd
->vdev_child
[c
];
6785 vdev_indirect_state_sync_verify(vd
);
6787 if (vdev_indirect_should_condense(vd
)) {
6788 spa_condense_indirect_start_sync(vd
, tx
);
6794 * Iterate to convergence.
6797 int pass
= ++spa
->spa_sync_pass
;
6799 spa_sync_config_object(spa
, tx
);
6800 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6801 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6802 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6803 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6804 spa_errlog_sync(spa
, txg
);
6805 dsl_pool_sync(dp
, txg
);
6807 if (pass
< zfs_sync_pass_deferred_free
) {
6808 spa_sync_frees(spa
, free_bpl
, tx
);
6811 * We can not defer frees in pass 1, because
6812 * we sync the deferred frees later in pass 1.
6814 ASSERT3U(pass
, >, 1);
6815 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6816 &spa
->spa_deferred_bpobj
, tx
);
6820 dsl_scan_sync(dp
, tx
);
6822 if (spa
->spa_vdev_removal
!= NULL
)
6825 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
6830 spa_sync_upgrades(spa
, tx
);
6832 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6834 * Note: We need to check if the MOS is dirty
6835 * because we could have marked the MOS dirty
6836 * without updating the uberblock (e.g. if we
6837 * have sync tasks but no dirty user data). We
6838 * need to check the uberblock's rootbp because
6839 * it is updated if we have synced out dirty
6840 * data (though in this case the MOS will most
6841 * likely also be dirty due to second order
6842 * effects, we don't want to rely on that here).
6844 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6845 !dmu_objset_is_dirty(mos
, txg
)) {
6847 * Nothing changed on the first pass,
6848 * therefore this TXG is a no-op. Avoid
6849 * syncing deferred frees, so that we
6850 * can keep this TXG as a no-op.
6852 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6854 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6855 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6858 spa_sync_deferred_frees(spa
, tx
);
6861 } while (dmu_objset_is_dirty(mos
, txg
));
6864 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
6866 * Make sure that the number of ZAPs for all the vdevs matches
6867 * the number of ZAPs in the per-vdev ZAP list. This only gets
6868 * called if the config is dirty; otherwise there may be
6869 * outstanding AVZ operations that weren't completed in
6870 * spa_sync_config_object.
6872 uint64_t all_vdev_zap_entry_count
;
6873 ASSERT0(zap_count(spa
->spa_meta_objset
,
6874 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
6875 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
6876 all_vdev_zap_entry_count
);
6880 if (spa
->spa_vdev_removal
!= NULL
) {
6881 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
6885 * Rewrite the vdev configuration (which includes the uberblock)
6886 * to commit the transaction group.
6888 * If there are no dirty vdevs, we sync the uberblock to a few
6889 * random top-level vdevs that are known to be visible in the
6890 * config cache (see spa_vdev_add() for a complete description).
6891 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6895 * We hold SCL_STATE to prevent vdev open/close/etc.
6896 * while we're attempting to write the vdev labels.
6898 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6900 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6901 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6903 int children
= rvd
->vdev_children
;
6904 int c0
= spa_get_random(children
);
6906 for (int c
= 0; c
< children
; c
++) {
6907 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6908 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
6909 !vdev_is_concrete(vd
))
6911 svd
[svdcount
++] = vd
;
6912 if (svdcount
== SPA_DVAS_PER_BP
)
6915 error
= vdev_config_sync(svd
, svdcount
, txg
);
6917 error
= vdev_config_sync(rvd
->vdev_child
,
6918 rvd
->vdev_children
, txg
);
6922 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6924 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6928 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
6929 zio_resume_wait(spa
);
6933 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
6934 spa
->spa_deadman_tqid
= 0;
6937 * Clear the dirty config list.
6939 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6940 vdev_config_clean(vd
);
6943 * Now that the new config has synced transactionally,
6944 * let it become visible to the config cache.
6946 if (spa
->spa_config_syncing
!= NULL
) {
6947 spa_config_set(spa
, spa
->spa_config_syncing
);
6948 spa
->spa_config_txg
= txg
;
6949 spa
->spa_config_syncing
= NULL
;
6952 dsl_pool_sync_done(dp
, txg
);
6954 mutex_enter(&spa
->spa_alloc_lock
);
6955 VERIFY0(avl_numnodes(&spa
->spa_alloc_tree
));
6956 mutex_exit(&spa
->spa_alloc_lock
);
6959 * Update usable space statistics.
6961 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6962 vdev_sync_done(vd
, txg
);
6964 spa_update_dspace(spa
);
6967 * It had better be the case that we didn't dirty anything
6968 * since vdev_config_sync().
6970 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6971 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6972 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6974 spa
->spa_sync_pass
= 0;
6977 * Update the last synced uberblock here. We want to do this at
6978 * the end of spa_sync() so that consumers of spa_last_synced_txg()
6979 * will be guaranteed that all the processing associated with
6980 * that txg has been completed.
6982 spa
->spa_ubsync
= spa
->spa_uberblock
;
6983 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6985 spa_handle_ignored_writes(spa
);
6988 * If any async tasks have been requested, kick them off.
6990 spa_async_dispatch(spa
);
6994 * Sync all pools. We don't want to hold the namespace lock across these
6995 * operations, so we take a reference on the spa_t and drop the lock during the
6999 spa_sync_allpools(void)
7002 mutex_enter(&spa_namespace_lock
);
7003 while ((spa
= spa_next(spa
)) != NULL
) {
7004 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
7005 !spa_writeable(spa
) || spa_suspended(spa
))
7007 spa_open_ref(spa
, FTAG
);
7008 mutex_exit(&spa_namespace_lock
);
7009 txg_wait_synced(spa_get_dsl(spa
), 0);
7010 mutex_enter(&spa_namespace_lock
);
7011 spa_close(spa
, FTAG
);
7013 mutex_exit(&spa_namespace_lock
);
7017 * ==========================================================================
7018 * Miscellaneous routines
7019 * ==========================================================================
7023 * Remove all pools in the system.
7031 * Remove all cached state. All pools should be closed now,
7032 * so every spa in the AVL tree should be unreferenced.
7034 mutex_enter(&spa_namespace_lock
);
7035 while ((spa
= spa_next(NULL
)) != NULL
) {
7037 * Stop async tasks. The async thread may need to detach
7038 * a device that's been replaced, which requires grabbing
7039 * spa_namespace_lock, so we must drop it here.
7041 spa_open_ref(spa
, FTAG
);
7042 mutex_exit(&spa_namespace_lock
);
7043 spa_async_suspend(spa
);
7044 mutex_enter(&spa_namespace_lock
);
7045 spa_close(spa
, FTAG
);
7047 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
7049 spa_deactivate(spa
);
7053 mutex_exit(&spa_namespace_lock
);
7057 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
7062 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
7066 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
7067 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
7068 if (vd
->vdev_guid
== guid
)
7072 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
7073 vd
= spa
->spa_spares
.sav_vdevs
[i
];
7074 if (vd
->vdev_guid
== guid
)
7083 spa_upgrade(spa_t
*spa
, uint64_t version
)
7085 ASSERT(spa_writeable(spa
));
7087 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7090 * This should only be called for a non-faulted pool, and since a
7091 * future version would result in an unopenable pool, this shouldn't be
7094 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
7095 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
7097 spa
->spa_uberblock
.ub_version
= version
;
7098 vdev_config_dirty(spa
->spa_root_vdev
);
7100 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7102 txg_wait_synced(spa_get_dsl(spa
), 0);
7106 spa_has_spare(spa_t
*spa
, uint64_t guid
)
7110 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
7112 for (i
= 0; i
< sav
->sav_count
; i
++)
7113 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
7116 for (i
= 0; i
< sav
->sav_npending
; i
++) {
7117 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
7118 &spareguid
) == 0 && spareguid
== guid
)
7126 * Check if a pool has an active shared spare device.
7127 * Note: reference count of an active spare is 2, as a spare and as a replace
7130 spa_has_active_shared_spare(spa_t
*spa
)
7134 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
7136 for (i
= 0; i
< sav
->sav_count
; i
++) {
7137 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
7138 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
7147 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
7149 sysevent_t
*ev
= NULL
;
7153 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
7155 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
7156 ev
->resource
= resource
;
7163 spa_event_post(sysevent_t
*ev
)
7167 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
7168 kmem_free(ev
, sizeof (*ev
));
7174 * Post a zevent corresponding to the given sysevent. The 'name' must be one
7175 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
7176 * filled in from the spa and (optionally) the vdev. This doesn't do anything
7177 * in the userland libzpool, as we don't want consumers to misinterpret ztest
7178 * or zdb as real changes.
7181 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
7183 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
7186 #if defined(_KERNEL) && defined(HAVE_SPL)
7187 /* state manipulation functions */
7188 EXPORT_SYMBOL(spa_open
);
7189 EXPORT_SYMBOL(spa_open_rewind
);
7190 EXPORT_SYMBOL(spa_get_stats
);
7191 EXPORT_SYMBOL(spa_create
);
7192 EXPORT_SYMBOL(spa_import
);
7193 EXPORT_SYMBOL(spa_tryimport
);
7194 EXPORT_SYMBOL(spa_destroy
);
7195 EXPORT_SYMBOL(spa_export
);
7196 EXPORT_SYMBOL(spa_reset
);
7197 EXPORT_SYMBOL(spa_async_request
);
7198 EXPORT_SYMBOL(spa_async_suspend
);
7199 EXPORT_SYMBOL(spa_async_resume
);
7200 EXPORT_SYMBOL(spa_inject_addref
);
7201 EXPORT_SYMBOL(spa_inject_delref
);
7202 EXPORT_SYMBOL(spa_scan_stat_init
);
7203 EXPORT_SYMBOL(spa_scan_get_stats
);
7205 /* device maniion */
7206 EXPORT_SYMBOL(spa_vdev_add
);
7207 EXPORT_SYMBOL(spa_vdev_attach
);
7208 EXPORT_SYMBOL(spa_vdev_detach
);
7209 EXPORT_SYMBOL(spa_vdev_setpath
);
7210 EXPORT_SYMBOL(spa_vdev_setfru
);
7211 EXPORT_SYMBOL(spa_vdev_split_mirror
);
7213 /* spare statech is global across all pools) */
7214 EXPORT_SYMBOL(spa_spare_add
);
7215 EXPORT_SYMBOL(spa_spare_remove
);
7216 EXPORT_SYMBOL(spa_spare_exists
);
7217 EXPORT_SYMBOL(spa_spare_activate
);
7219 /* L2ARC statech is global across all pools) */
7220 EXPORT_SYMBOL(spa_l2cache_add
);
7221 EXPORT_SYMBOL(spa_l2cache_remove
);
7222 EXPORT_SYMBOL(spa_l2cache_exists
);
7223 EXPORT_SYMBOL(spa_l2cache_activate
);
7224 EXPORT_SYMBOL(spa_l2cache_drop
);
7227 EXPORT_SYMBOL(spa_scan
);
7228 EXPORT_SYMBOL(spa_scan_stop
);
7231 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
7232 EXPORT_SYMBOL(spa_sync_allpools
);
7235 EXPORT_SYMBOL(spa_prop_set
);
7236 EXPORT_SYMBOL(spa_prop_get
);
7237 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
7239 /* asynchronous event notification */
7240 EXPORT_SYMBOL(spa_event_notify
);
7243 #if defined(_KERNEL) && defined(HAVE_SPL)
7244 module_param(spa_load_verify_maxinflight
, int, 0644);
7245 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
7246 "Max concurrent traversal I/Os while verifying pool during import -X");
7248 module_param(spa_load_verify_metadata
, int, 0644);
7249 MODULE_PARM_DESC(spa_load_verify_metadata
,
7250 "Set to traverse metadata on pool import");
7252 module_param(spa_load_verify_data
, int, 0644);
7253 MODULE_PARM_DESC(spa_load_verify_data
,
7254 "Set to traverse data on pool import");
7257 module_param(zio_taskq_batch_pct
, uint
, 0444);
7258 MODULE_PARM_DESC(zio_taskq_batch_pct
,
7259 "Percentage of CPUs to run an IO worker thread");