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 https://opensource.org/licenses/CDDL-1.0.
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, 2020 by Delphix. All rights reserved.
25 * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 * Copyright 2018 Joyent, Inc.
32 * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
33 * Copyright 2017 Joyent, Inc.
34 * Copyright (c) 2017, Intel Corporation.
35 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
36 * Copyright (c) 2023 Hewlett Packard Enterprise Development LP.
40 * SPA: Storage Pool Allocator
42 * This file contains all the routines used when modifying on-disk SPA state.
43 * This includes opening, importing, destroying, exporting a pool, and syncing a
47 #include <sys/zfs_context.h>
48 #include <sys/fm/fs/zfs.h>
49 #include <sys/spa_impl.h>
51 #include <sys/zio_checksum.h>
53 #include <sys/dmu_tx.h>
58 #include <sys/vdev_impl.h>
59 #include <sys/vdev_removal.h>
60 #include <sys/vdev_indirect_mapping.h>
61 #include <sys/vdev_indirect_births.h>
62 #include <sys/vdev_initialize.h>
63 #include <sys/vdev_rebuild.h>
64 #include <sys/vdev_trim.h>
65 #include <sys/vdev_disk.h>
66 #include <sys/vdev_draid.h>
67 #include <sys/metaslab.h>
68 #include <sys/metaslab_impl.h>
70 #include <sys/uberblock_impl.h>
73 #include <sys/bpobj.h>
74 #include <sys/dmu_traverse.h>
75 #include <sys/dmu_objset.h>
76 #include <sys/unique.h>
77 #include <sys/dsl_pool.h>
78 #include <sys/dsl_dataset.h>
79 #include <sys/dsl_dir.h>
80 #include <sys/dsl_prop.h>
81 #include <sys/dsl_synctask.h>
82 #include <sys/fs/zfs.h>
84 #include <sys/callb.h>
85 #include <sys/systeminfo.h>
86 #include <sys/zfs_ioctl.h>
87 #include <sys/dsl_scan.h>
88 #include <sys/zfeature.h>
89 #include <sys/dsl_destroy.h>
93 #include <sys/fm/protocol.h>
94 #include <sys/fm/util.h>
95 #include <sys/callb.h>
97 #include <sys/vmsystm.h>
100 #include "zfs_prop.h"
101 #include "zfs_comutil.h"
104 * The interval, in seconds, at which failed configuration cache file writes
107 int zfs_ccw_retry_interval
= 300;
109 typedef enum zti_modes
{
110 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
111 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
112 ZTI_MODE_SCALE
, /* Taskqs scale with CPUs. */
113 ZTI_MODE_NULL
, /* don't create a taskq */
117 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
118 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
119 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
120 #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 }
121 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
123 #define ZTI_N(n) ZTI_P(n, 1)
124 #define ZTI_ONE ZTI_N(1)
126 typedef struct zio_taskq_info
{
127 zti_modes_t zti_mode
;
132 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
133 "iss", "iss_h", "int", "int_h"
137 * This table defines the taskq settings for each ZFS I/O type. When
138 * initializing a pool, we use this table to create an appropriately sized
139 * taskq. Some operations are low volume and therefore have a small, static
140 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
141 * macros. Other operations process a large amount of data; the ZTI_BATCH
142 * macro causes us to create a taskq oriented for throughput. Some operations
143 * are so high frequency and short-lived that the taskq itself can become a
144 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
145 * additional degree of parallelism specified by the number of threads per-
146 * taskq and the number of taskqs; when dispatching an event in this case, the
147 * particular taskq is chosen at random. ZTI_SCALE is similar to ZTI_BATCH,
148 * but with number of taskqs also scaling with number of CPUs.
150 * The different taskq priorities are to handle the different contexts (issue
151 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
152 * need to be handled with minimum delay.
154 static const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
155 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
156 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
157 { ZTI_N(8), ZTI_NULL
, ZTI_SCALE
, ZTI_NULL
}, /* READ */
158 { ZTI_BATCH
, ZTI_N(5), ZTI_SCALE
, ZTI_N(5) }, /* WRITE */
159 { ZTI_SCALE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
160 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
161 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
162 { ZTI_N(4), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* TRIM */
165 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
166 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
167 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
168 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
,
169 const char **ereport
);
170 static void spa_vdev_resilver_done(spa_t
*spa
);
172 static uint_t zio_taskq_batch_pct
= 80; /* 1 thread per cpu in pset */
173 static uint_t zio_taskq_batch_tpq
; /* threads per taskq */
174 static const boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
175 static const uint_t zio_taskq_basedc
= 80; /* base duty cycle */
177 static const boolean_t spa_create_process
= B_TRUE
; /* no process => no sysdc */
180 * Report any spa_load_verify errors found, but do not fail spa_load.
181 * This is used by zdb to analyze non-idle pools.
183 boolean_t spa_load_verify_dryrun
= B_FALSE
;
186 * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ).
187 * This is used by zdb for spacemaps verification.
189 boolean_t spa_mode_readable_spacemaps
= B_FALSE
;
192 * This (illegal) pool name is used when temporarily importing a spa_t in order
193 * to get the vdev stats associated with the imported devices.
195 #define TRYIMPORT_NAME "$import"
198 * For debugging purposes: print out vdev tree during pool import.
200 static int spa_load_print_vdev_tree
= B_FALSE
;
203 * A non-zero value for zfs_max_missing_tvds means that we allow importing
204 * pools with missing top-level vdevs. This is strictly intended for advanced
205 * pool recovery cases since missing data is almost inevitable. Pools with
206 * missing devices can only be imported read-only for safety reasons, and their
207 * fail-mode will be automatically set to "continue".
209 * With 1 missing vdev we should be able to import the pool and mount all
210 * datasets. User data that was not modified after the missing device has been
211 * added should be recoverable. This means that snapshots created prior to the
212 * addition of that device should be completely intact.
214 * With 2 missing vdevs, some datasets may fail to mount since there are
215 * dataset statistics that are stored as regular metadata. Some data might be
216 * recoverable if those vdevs were added recently.
218 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
219 * may be missing entirely. Chances of data recovery are very low. Note that
220 * there are also risks of performing an inadvertent rewind as we might be
221 * missing all the vdevs with the latest uberblocks.
223 uint64_t zfs_max_missing_tvds
= 0;
226 * The parameters below are similar to zfs_max_missing_tvds but are only
227 * intended for a preliminary open of the pool with an untrusted config which
228 * might be incomplete or out-dated.
230 * We are more tolerant for pools opened from a cachefile since we could have
231 * an out-dated cachefile where a device removal was not registered.
232 * We could have set the limit arbitrarily high but in the case where devices
233 * are really missing we would want to return the proper error codes; we chose
234 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
235 * and we get a chance to retrieve the trusted config.
237 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
240 * In the case where config was assembled by scanning device paths (/dev/dsks
241 * by default) we are less tolerant since all the existing devices should have
242 * been detected and we want spa_load to return the right error codes.
244 uint64_t zfs_max_missing_tvds_scan
= 0;
247 * Debugging aid that pauses spa_sync() towards the end.
249 static const boolean_t zfs_pause_spa_sync
= B_FALSE
;
252 * Variables to indicate the livelist condense zthr func should wait at certain
253 * points for the livelist to be removed - used to test condense/destroy races
255 static int zfs_livelist_condense_zthr_pause
= 0;
256 static int zfs_livelist_condense_sync_pause
= 0;
259 * Variables to track whether or not condense cancellation has been
260 * triggered in testing.
262 static int zfs_livelist_condense_sync_cancel
= 0;
263 static int zfs_livelist_condense_zthr_cancel
= 0;
266 * Variable to track whether or not extra ALLOC blkptrs were added to a
267 * livelist entry while it was being condensed (caused by the way we track
268 * remapped blkptrs in dbuf_remap_impl)
270 static int zfs_livelist_condense_new_alloc
= 0;
273 * ==========================================================================
274 * SPA properties routines
275 * ==========================================================================
279 * Add a (source=src, propname=propval) list to an nvlist.
282 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, const char *strval
,
283 uint64_t intval
, zprop_source_t src
)
285 const char *propname
= zpool_prop_to_name(prop
);
288 propval
= fnvlist_alloc();
289 fnvlist_add_uint64(propval
, ZPROP_SOURCE
, src
);
292 fnvlist_add_string(propval
, ZPROP_VALUE
, strval
);
294 fnvlist_add_uint64(propval
, ZPROP_VALUE
, intval
);
296 fnvlist_add_nvlist(nvl
, propname
, propval
);
297 nvlist_free(propval
);
301 * Add a user property (source=src, propname=propval) to an nvlist.
304 spa_prop_add_user(nvlist_t
*nvl
, const char *propname
, char *strval
,
309 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
310 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
311 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
312 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
313 nvlist_free(propval
);
317 * Get property values from the spa configuration.
320 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
322 vdev_t
*rvd
= spa
->spa_root_vdev
;
323 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
324 uint64_t size
, alloc
, cap
, version
;
325 const zprop_source_t src
= ZPROP_SRC_NONE
;
326 spa_config_dirent_t
*dp
;
327 metaslab_class_t
*mc
= spa_normal_class(spa
);
329 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
332 alloc
= metaslab_class_get_alloc(mc
);
333 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
334 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
335 alloc
+= metaslab_class_get_alloc(spa_embedded_log_class(spa
));
337 size
= metaslab_class_get_space(mc
);
338 size
+= metaslab_class_get_space(spa_special_class(spa
));
339 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
340 size
+= metaslab_class_get_space(spa_embedded_log_class(spa
));
342 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
343 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
344 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
345 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
347 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
348 spa
->spa_checkpoint_info
.sci_dspace
, src
);
350 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
351 metaslab_class_fragmentation(mc
), src
);
352 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
353 metaslab_class_expandable_space(mc
), src
);
354 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
355 (spa_mode(spa
) == SPA_MODE_READ
), src
);
357 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
358 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
360 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
361 ddt_get_pool_dedup_ratio(spa
), src
);
362 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONEUSED
, NULL
,
363 brt_get_used(spa
), src
);
364 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONESAVED
, NULL
,
365 brt_get_saved(spa
), src
);
366 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONERATIO
, NULL
,
367 brt_get_ratio(spa
), src
);
369 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
370 rvd
->vdev_state
, src
);
372 version
= spa_version(spa
);
373 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
374 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
375 version
, ZPROP_SRC_DEFAULT
);
377 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
378 version
, ZPROP_SRC_LOCAL
);
380 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
381 NULL
, spa_load_guid(spa
), src
);
386 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
387 * when opening pools before this version freedir will be NULL.
389 if (pool
->dp_free_dir
!= NULL
) {
390 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
391 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
394 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
398 if (pool
->dp_leak_dir
!= NULL
) {
399 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
400 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
403 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
408 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
410 if (spa
->spa_comment
!= NULL
) {
411 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
415 if (spa
->spa_compatibility
!= NULL
) {
416 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMPATIBILITY
,
417 spa
->spa_compatibility
, 0, ZPROP_SRC_LOCAL
);
420 if (spa
->spa_root
!= NULL
)
421 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
424 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
425 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
426 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
428 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
429 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
432 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
433 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
434 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
436 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
437 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
440 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
441 if (dp
->scd_path
== NULL
) {
442 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
443 "none", 0, ZPROP_SRC_LOCAL
);
444 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
445 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
446 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
452 * Get zpool property values.
455 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
457 objset_t
*mos
= spa
->spa_meta_objset
;
463 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
467 dp
= spa_get_dsl(spa
);
468 dsl_pool_config_enter(dp
, FTAG
);
469 mutex_enter(&spa
->spa_props_lock
);
472 * Get properties from the spa config.
474 spa_prop_get_config(spa
, nvp
);
476 /* If no pool property object, no more prop to get. */
477 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0)
481 * Get properties from the MOS pool property object.
483 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
484 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
485 zap_cursor_advance(&zc
)) {
488 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
491 if ((prop
= zpool_name_to_prop(za
.za_name
)) ==
492 ZPOOL_PROP_INVAL
&& !zfs_prop_user(za
.za_name
))
495 switch (za
.za_integer_length
) {
497 /* integer property */
498 if (za
.za_first_integer
!=
499 zpool_prop_default_numeric(prop
))
500 src
= ZPROP_SRC_LOCAL
;
502 if (prop
== ZPOOL_PROP_BOOTFS
) {
503 dsl_dataset_t
*ds
= NULL
;
505 err
= dsl_dataset_hold_obj(dp
,
506 za
.za_first_integer
, FTAG
, &ds
);
510 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
512 dsl_dataset_name(ds
, strval
);
513 dsl_dataset_rele(ds
, FTAG
);
516 intval
= za
.za_first_integer
;
519 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
522 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
527 /* string property */
528 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
529 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
530 za
.za_name
, 1, za
.za_num_integers
, strval
);
532 kmem_free(strval
, za
.za_num_integers
);
535 if (prop
!= ZPOOL_PROP_INVAL
) {
536 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
538 src
= ZPROP_SRC_LOCAL
;
539 spa_prop_add_user(*nvp
, za
.za_name
, strval
,
542 kmem_free(strval
, za
.za_num_integers
);
549 zap_cursor_fini(&zc
);
551 mutex_exit(&spa
->spa_props_lock
);
552 dsl_pool_config_exit(dp
, FTAG
);
553 if (err
&& err
!= ENOENT
) {
563 * Validate the given pool properties nvlist and modify the list
564 * for the property values to be set.
567 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
570 int error
= 0, reset_bootfs
= 0;
572 boolean_t has_feature
= B_FALSE
;
575 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
577 const char *strval
, *slash
, *check
, *fname
;
578 const char *propname
= nvpair_name(elem
);
579 zpool_prop_t prop
= zpool_name_to_prop(propname
);
582 case ZPOOL_PROP_INVAL
:
584 * Sanitize the input.
586 if (zfs_prop_user(propname
)) {
587 if (strlen(propname
) >= ZAP_MAXNAMELEN
) {
588 error
= SET_ERROR(ENAMETOOLONG
);
592 if (strlen(fnvpair_value_string(elem
)) >=
594 error
= SET_ERROR(E2BIG
);
597 } else if (zpool_prop_feature(propname
)) {
598 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
599 error
= SET_ERROR(EINVAL
);
603 if (nvpair_value_uint64(elem
, &intval
) != 0) {
604 error
= SET_ERROR(EINVAL
);
609 error
= SET_ERROR(EINVAL
);
613 fname
= strchr(propname
, '@') + 1;
614 if (zfeature_lookup_name(fname
, NULL
) != 0) {
615 error
= SET_ERROR(EINVAL
);
619 has_feature
= B_TRUE
;
621 error
= SET_ERROR(EINVAL
);
626 case ZPOOL_PROP_VERSION
:
627 error
= nvpair_value_uint64(elem
, &intval
);
629 (intval
< spa_version(spa
) ||
630 intval
> SPA_VERSION_BEFORE_FEATURES
||
632 error
= SET_ERROR(EINVAL
);
635 case ZPOOL_PROP_DELEGATION
:
636 case ZPOOL_PROP_AUTOREPLACE
:
637 case ZPOOL_PROP_LISTSNAPS
:
638 case ZPOOL_PROP_AUTOEXPAND
:
639 case ZPOOL_PROP_AUTOTRIM
:
640 error
= nvpair_value_uint64(elem
, &intval
);
641 if (!error
&& intval
> 1)
642 error
= SET_ERROR(EINVAL
);
645 case ZPOOL_PROP_MULTIHOST
:
646 error
= nvpair_value_uint64(elem
, &intval
);
647 if (!error
&& intval
> 1)
648 error
= SET_ERROR(EINVAL
);
651 uint32_t hostid
= zone_get_hostid(NULL
);
653 spa
->spa_hostid
= hostid
;
655 error
= SET_ERROR(ENOTSUP
);
660 case ZPOOL_PROP_BOOTFS
:
662 * If the pool version is less than SPA_VERSION_BOOTFS,
663 * or the pool is still being created (version == 0),
664 * the bootfs property cannot be set.
666 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
667 error
= SET_ERROR(ENOTSUP
);
672 * Make sure the vdev config is bootable
674 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
675 error
= SET_ERROR(ENOTSUP
);
681 error
= nvpair_value_string(elem
, &strval
);
686 if (strval
== NULL
|| strval
[0] == '\0') {
687 objnum
= zpool_prop_default_numeric(
692 error
= dmu_objset_hold(strval
, FTAG
, &os
);
697 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
698 error
= SET_ERROR(ENOTSUP
);
700 objnum
= dmu_objset_id(os
);
702 dmu_objset_rele(os
, FTAG
);
706 case ZPOOL_PROP_FAILUREMODE
:
707 error
= nvpair_value_uint64(elem
, &intval
);
708 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
709 error
= SET_ERROR(EINVAL
);
712 * This is a special case which only occurs when
713 * the pool has completely failed. This allows
714 * the user to change the in-core failmode property
715 * without syncing it out to disk (I/Os might
716 * currently be blocked). We do this by returning
717 * EIO to the caller (spa_prop_set) to trick it
718 * into thinking we encountered a property validation
721 if (!error
&& spa_suspended(spa
)) {
722 spa
->spa_failmode
= intval
;
723 error
= SET_ERROR(EIO
);
727 case ZPOOL_PROP_CACHEFILE
:
728 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
731 if (strval
[0] == '\0')
734 if (strcmp(strval
, "none") == 0)
737 if (strval
[0] != '/') {
738 error
= SET_ERROR(EINVAL
);
742 slash
= strrchr(strval
, '/');
743 ASSERT(slash
!= NULL
);
745 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
746 strcmp(slash
, "/..") == 0)
747 error
= SET_ERROR(EINVAL
);
750 case ZPOOL_PROP_COMMENT
:
751 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
753 for (check
= strval
; *check
!= '\0'; check
++) {
754 if (!isprint(*check
)) {
755 error
= SET_ERROR(EINVAL
);
759 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
760 error
= SET_ERROR(E2BIG
);
771 (void) nvlist_remove_all(props
,
772 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO
));
774 if (!error
&& reset_bootfs
) {
775 error
= nvlist_remove(props
,
776 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
779 error
= nvlist_add_uint64(props
,
780 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
788 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
790 const char *cachefile
;
791 spa_config_dirent_t
*dp
;
793 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
797 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
800 if (cachefile
[0] == '\0')
801 dp
->scd_path
= spa_strdup(spa_config_path
);
802 else if (strcmp(cachefile
, "none") == 0)
805 dp
->scd_path
= spa_strdup(cachefile
);
807 list_insert_head(&spa
->spa_config_list
, dp
);
809 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
813 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
816 nvpair_t
*elem
= NULL
;
817 boolean_t need_sync
= B_FALSE
;
819 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
822 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
823 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
825 if (prop
== ZPOOL_PROP_CACHEFILE
||
826 prop
== ZPOOL_PROP_ALTROOT
||
827 prop
== ZPOOL_PROP_READONLY
)
830 if (prop
== ZPOOL_PROP_INVAL
&&
831 zfs_prop_user(nvpair_name(elem
))) {
836 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
839 if (prop
== ZPOOL_PROP_VERSION
) {
840 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
842 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
843 ver
= SPA_VERSION_FEATURES
;
847 /* Save time if the version is already set. */
848 if (ver
== spa_version(spa
))
852 * In addition to the pool directory object, we might
853 * create the pool properties object, the features for
854 * read object, the features for write object, or the
855 * feature descriptions object.
857 error
= dsl_sync_task(spa
->spa_name
, NULL
,
858 spa_sync_version
, &ver
,
859 6, ZFS_SPACE_CHECK_RESERVED
);
870 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
871 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
878 * If the bootfs property value is dsobj, clear it.
881 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
883 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
884 VERIFY(zap_remove(spa
->spa_meta_objset
,
885 spa
->spa_pool_props_object
,
886 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
892 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
894 uint64_t *newguid __maybe_unused
= arg
;
895 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
896 vdev_t
*rvd
= spa
->spa_root_vdev
;
899 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
900 int error
= (spa_has_checkpoint(spa
)) ?
901 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
902 return (SET_ERROR(error
));
905 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
906 vdev_state
= rvd
->vdev_state
;
907 spa_config_exit(spa
, SCL_STATE
, FTAG
);
909 if (vdev_state
!= VDEV_STATE_HEALTHY
)
910 return (SET_ERROR(ENXIO
));
912 ASSERT3U(spa_guid(spa
), !=, *newguid
);
918 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
920 uint64_t *newguid
= arg
;
921 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
923 vdev_t
*rvd
= spa
->spa_root_vdev
;
925 oldguid
= spa_guid(spa
);
927 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
928 rvd
->vdev_guid
= *newguid
;
929 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
930 vdev_config_dirty(rvd
);
931 spa_config_exit(spa
, SCL_STATE
, FTAG
);
933 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
934 (u_longlong_t
)oldguid
, (u_longlong_t
)*newguid
);
938 * Change the GUID for the pool. This is done so that we can later
939 * re-import a pool built from a clone of our own vdevs. We will modify
940 * the root vdev's guid, our own pool guid, and then mark all of our
941 * vdevs dirty. Note that we must make sure that all our vdevs are
942 * online when we do this, or else any vdevs that weren't present
943 * would be orphaned from our pool. We are also going to issue a
944 * sysevent to update any watchers.
947 spa_change_guid(spa_t
*spa
)
952 mutex_enter(&spa
->spa_vdev_top_lock
);
953 mutex_enter(&spa_namespace_lock
);
954 guid
= spa_generate_guid(NULL
);
956 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
957 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
961 * Clear the kobj flag from all the vdevs to allow
962 * vdev_cache_process_kobj_evt() to post events to all the
963 * vdevs since GUID is updated.
965 vdev_clear_kobj_evt(spa
->spa_root_vdev
);
966 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
967 vdev_clear_kobj_evt(spa
->spa_l2cache
.sav_vdevs
[i
]);
969 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
970 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
973 mutex_exit(&spa_namespace_lock
);
974 mutex_exit(&spa
->spa_vdev_top_lock
);
980 * ==========================================================================
981 * SPA state manipulation (open/create/destroy/import/export)
982 * ==========================================================================
986 spa_error_entry_compare(const void *a
, const void *b
)
988 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
989 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
992 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
993 sizeof (zbookmark_phys_t
));
995 return (TREE_ISIGN(ret
));
999 * Utility function which retrieves copies of the current logs and
1000 * re-initializes them in the process.
1003 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
1005 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
1007 memcpy(last
, &spa
->spa_errlist_last
, sizeof (avl_tree_t
));
1008 memcpy(scrub
, &spa
->spa_errlist_scrub
, sizeof (avl_tree_t
));
1010 avl_create(&spa
->spa_errlist_scrub
,
1011 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1012 offsetof(spa_error_entry_t
, se_avl
));
1013 avl_create(&spa
->spa_errlist_last
,
1014 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1015 offsetof(spa_error_entry_t
, se_avl
));
1019 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1021 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
1022 enum zti_modes mode
= ztip
->zti_mode
;
1023 uint_t value
= ztip
->zti_value
;
1024 uint_t count
= ztip
->zti_count
;
1025 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1026 uint_t cpus
, flags
= TASKQ_DYNAMIC
;
1027 boolean_t batch
= B_FALSE
;
1030 case ZTI_MODE_FIXED
:
1031 ASSERT3U(value
, >, 0);
1034 case ZTI_MODE_BATCH
:
1036 flags
|= TASKQ_THREADS_CPU_PCT
;
1037 value
= MIN(zio_taskq_batch_pct
, 100);
1040 case ZTI_MODE_SCALE
:
1041 flags
|= TASKQ_THREADS_CPU_PCT
;
1043 * We want more taskqs to reduce lock contention, but we want
1044 * less for better request ordering and CPU utilization.
1046 cpus
= MAX(1, boot_ncpus
* zio_taskq_batch_pct
/ 100);
1047 if (zio_taskq_batch_tpq
> 0) {
1048 count
= MAX(1, (cpus
+ zio_taskq_batch_tpq
/ 2) /
1049 zio_taskq_batch_tpq
);
1052 * Prefer 6 threads per taskq, but no more taskqs
1053 * than threads in them on large systems. For 80%:
1056 * cpus taskqs percent threads threads
1057 * ------- ------- ------- ------- -------
1068 count
= 1 + cpus
/ 6;
1069 while (count
* count
> cpus
)
1072 /* Limit each taskq within 100% to not trigger assertion. */
1073 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1074 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1078 tqs
->stqs_count
= 0;
1079 tqs
->stqs_taskq
= NULL
;
1083 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
1085 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
1089 ASSERT3U(count
, >, 0);
1090 tqs
->stqs_count
= count
;
1091 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
1093 for (uint_t i
= 0; i
< count
; i
++) {
1098 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
1099 zio_type_name
[t
], zio_taskq_types
[q
], i
);
1101 (void) snprintf(name
, sizeof (name
), "%s_%s",
1102 zio_type_name
[t
], zio_taskq_types
[q
]);
1104 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
1106 flags
|= TASKQ_DC_BATCH
;
1108 (void) zio_taskq_basedc
;
1109 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
1110 spa
->spa_proc
, zio_taskq_basedc
, flags
);
1112 pri_t pri
= maxclsyspri
;
1114 * The write issue taskq can be extremely CPU
1115 * intensive. Run it at slightly less important
1116 * priority than the other taskqs.
1118 * Under Linux and FreeBSD this means incrementing
1119 * the priority value as opposed to platforms like
1120 * illumos where it should be decremented.
1122 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1123 * are equal then a difference between them is
1126 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
) {
1127 #if defined(__linux__)
1129 #elif defined(__FreeBSD__)
1135 tq
= taskq_create_proc(name
, value
, pri
, 50,
1136 INT_MAX
, spa
->spa_proc
, flags
);
1139 tqs
->stqs_taskq
[i
] = tq
;
1144 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1146 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1148 if (tqs
->stqs_taskq
== NULL
) {
1149 ASSERT3U(tqs
->stqs_count
, ==, 0);
1153 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1154 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1155 taskq_destroy(tqs
->stqs_taskq
[i
]);
1158 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1159 tqs
->stqs_taskq
= NULL
;
1163 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1164 * Note that a type may have multiple discrete taskqs to avoid lock contention
1165 * on the taskq itself. In that case we choose which taskq at random by using
1166 * the low bits of gethrtime().
1169 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1170 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1172 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1175 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1176 ASSERT3U(tqs
->stqs_count
, !=, 0);
1178 if (tqs
->stqs_count
== 1) {
1179 tq
= tqs
->stqs_taskq
[0];
1181 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1184 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1188 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1191 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1192 task_func_t
*func
, void *arg
, uint_t flags
)
1194 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1198 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1199 ASSERT3U(tqs
->stqs_count
, !=, 0);
1201 if (tqs
->stqs_count
== 1) {
1202 tq
= tqs
->stqs_taskq
[0];
1204 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1207 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1209 taskq_wait_id(tq
, id
);
1213 spa_create_zio_taskqs(spa_t
*spa
)
1215 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1216 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1217 spa_taskqs_init(spa
, t
, q
);
1223 * Disabled until spa_thread() can be adapted for Linux.
1225 #undef HAVE_SPA_THREAD
1227 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1229 spa_thread(void *arg
)
1231 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1232 callb_cpr_t cprinfo
;
1235 user_t
*pu
= PTOU(curproc
);
1237 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1240 ASSERT(curproc
!= &p0
);
1241 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1242 "zpool-%s", spa
->spa_name
);
1243 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1245 /* bind this thread to the requested psrset */
1246 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1248 mutex_enter(&cpu_lock
);
1249 mutex_enter(&pidlock
);
1250 mutex_enter(&curproc
->p_lock
);
1252 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1253 0, NULL
, NULL
) == 0) {
1254 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1257 "Couldn't bind process for zfs pool \"%s\" to "
1258 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1261 mutex_exit(&curproc
->p_lock
);
1262 mutex_exit(&pidlock
);
1263 mutex_exit(&cpu_lock
);
1267 if (zio_taskq_sysdc
) {
1268 sysdc_thread_enter(curthread
, 100, 0);
1271 spa
->spa_proc
= curproc
;
1272 spa
->spa_did
= curthread
->t_did
;
1274 spa_create_zio_taskqs(spa
);
1276 mutex_enter(&spa
->spa_proc_lock
);
1277 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1279 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1280 cv_broadcast(&spa
->spa_proc_cv
);
1282 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1283 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1284 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1285 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1287 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1288 spa
->spa_proc_state
= SPA_PROC_GONE
;
1289 spa
->spa_proc
= &p0
;
1290 cv_broadcast(&spa
->spa_proc_cv
);
1291 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1293 mutex_enter(&curproc
->p_lock
);
1299 * Activate an uninitialized pool.
1302 spa_activate(spa_t
*spa
, spa_mode_t mode
)
1304 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1306 spa
->spa_state
= POOL_STATE_ACTIVE
;
1307 spa
->spa_mode
= mode
;
1308 spa
->spa_read_spacemaps
= spa_mode_readable_spacemaps
;
1310 spa
->spa_normal_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1311 spa
->spa_log_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1312 spa
->spa_embedded_log_class
=
1313 metaslab_class_create(spa
, &zfs_metaslab_ops
);
1314 spa
->spa_special_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1315 spa
->spa_dedup_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1317 /* Try to create a covering process */
1318 mutex_enter(&spa
->spa_proc_lock
);
1319 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1320 ASSERT(spa
->spa_proc
== &p0
);
1323 (void) spa_create_process
;
1324 #ifdef HAVE_SPA_THREAD
1325 /* Only create a process if we're going to be around a while. */
1326 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1327 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1329 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1330 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1331 cv_wait(&spa
->spa_proc_cv
,
1332 &spa
->spa_proc_lock
);
1334 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1335 ASSERT(spa
->spa_proc
!= &p0
);
1336 ASSERT(spa
->spa_did
!= 0);
1340 "Couldn't create process for zfs pool \"%s\"\n",
1345 #endif /* HAVE_SPA_THREAD */
1346 mutex_exit(&spa
->spa_proc_lock
);
1348 /* If we didn't create a process, we need to create our taskqs. */
1349 if (spa
->spa_proc
== &p0
) {
1350 spa_create_zio_taskqs(spa
);
1353 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1354 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1358 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1359 offsetof(vdev_t
, vdev_config_dirty_node
));
1360 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1361 offsetof(objset_t
, os_evicting_node
));
1362 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1363 offsetof(vdev_t
, vdev_state_dirty_node
));
1365 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1366 offsetof(struct vdev
, vdev_txg_node
));
1368 avl_create(&spa
->spa_errlist_scrub
,
1369 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1370 offsetof(spa_error_entry_t
, se_avl
));
1371 avl_create(&spa
->spa_errlist_last
,
1372 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1373 offsetof(spa_error_entry_t
, se_avl
));
1374 avl_create(&spa
->spa_errlist_healed
,
1375 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1376 offsetof(spa_error_entry_t
, se_avl
));
1378 spa_activate_os(spa
);
1380 spa_keystore_init(&spa
->spa_keystore
);
1383 * This taskq is used to perform zvol-minor-related tasks
1384 * asynchronously. This has several advantages, including easy
1385 * resolution of various deadlocks.
1387 * The taskq must be single threaded to ensure tasks are always
1388 * processed in the order in which they were dispatched.
1390 * A taskq per pool allows one to keep the pools independent.
1391 * This way if one pool is suspended, it will not impact another.
1393 * The preferred location to dispatch a zvol minor task is a sync
1394 * task. In this context, there is easy access to the spa_t and minimal
1395 * error handling is required because the sync task must succeed.
1397 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1401 * Taskq dedicated to prefetcher threads: this is used to prevent the
1402 * pool traverse code from monopolizing the global (and limited)
1403 * system_taskq by inappropriately scheduling long running tasks on it.
1405 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", 100,
1406 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1409 * The taskq to upgrade datasets in this pool. Currently used by
1410 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1412 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", 100,
1413 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1417 * Opposite of spa_activate().
1420 spa_deactivate(spa_t
*spa
)
1422 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1423 ASSERT(spa
->spa_dsl_pool
== NULL
);
1424 ASSERT(spa
->spa_root_vdev
== NULL
);
1425 ASSERT(spa
->spa_async_zio_root
== NULL
);
1426 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1428 spa_evicting_os_wait(spa
);
1430 if (spa
->spa_zvol_taskq
) {
1431 taskq_destroy(spa
->spa_zvol_taskq
);
1432 spa
->spa_zvol_taskq
= NULL
;
1435 if (spa
->spa_prefetch_taskq
) {
1436 taskq_destroy(spa
->spa_prefetch_taskq
);
1437 spa
->spa_prefetch_taskq
= NULL
;
1440 if (spa
->spa_upgrade_taskq
) {
1441 taskq_destroy(spa
->spa_upgrade_taskq
);
1442 spa
->spa_upgrade_taskq
= NULL
;
1445 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1447 list_destroy(&spa
->spa_config_dirty_list
);
1448 list_destroy(&spa
->spa_evicting_os_list
);
1449 list_destroy(&spa
->spa_state_dirty_list
);
1451 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1453 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1454 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1455 spa_taskqs_fini(spa
, t
, q
);
1459 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1460 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1461 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1462 spa
->spa_txg_zio
[i
] = NULL
;
1465 metaslab_class_destroy(spa
->spa_normal_class
);
1466 spa
->spa_normal_class
= NULL
;
1468 metaslab_class_destroy(spa
->spa_log_class
);
1469 spa
->spa_log_class
= NULL
;
1471 metaslab_class_destroy(spa
->spa_embedded_log_class
);
1472 spa
->spa_embedded_log_class
= NULL
;
1474 metaslab_class_destroy(spa
->spa_special_class
);
1475 spa
->spa_special_class
= NULL
;
1477 metaslab_class_destroy(spa
->spa_dedup_class
);
1478 spa
->spa_dedup_class
= NULL
;
1481 * If this was part of an import or the open otherwise failed, we may
1482 * still have errors left in the queues. Empty them just in case.
1484 spa_errlog_drain(spa
);
1485 avl_destroy(&spa
->spa_errlist_scrub
);
1486 avl_destroy(&spa
->spa_errlist_last
);
1487 avl_destroy(&spa
->spa_errlist_healed
);
1489 spa_keystore_fini(&spa
->spa_keystore
);
1491 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1493 mutex_enter(&spa
->spa_proc_lock
);
1494 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1495 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1496 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1497 cv_broadcast(&spa
->spa_proc_cv
);
1498 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1499 ASSERT(spa
->spa_proc
!= &p0
);
1500 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1502 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1503 spa
->spa_proc_state
= SPA_PROC_NONE
;
1505 ASSERT(spa
->spa_proc
== &p0
);
1506 mutex_exit(&spa
->spa_proc_lock
);
1509 * We want to make sure spa_thread() has actually exited the ZFS
1510 * module, so that the module can't be unloaded out from underneath
1513 if (spa
->spa_did
!= 0) {
1514 thread_join(spa
->spa_did
);
1518 spa_deactivate_os(spa
);
1523 * Verify a pool configuration, and construct the vdev tree appropriately. This
1524 * will create all the necessary vdevs in the appropriate layout, with each vdev
1525 * in the CLOSED state. This will prep the pool before open/creation/import.
1526 * All vdev validation is done by the vdev_alloc() routine.
1529 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1530 uint_t id
, int atype
)
1536 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1539 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1542 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1545 if (error
== ENOENT
)
1551 return (SET_ERROR(EINVAL
));
1554 for (int c
= 0; c
< children
; c
++) {
1556 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1564 ASSERT(*vdp
!= NULL
);
1570 spa_should_flush_logs_on_unload(spa_t
*spa
)
1572 if (!spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
))
1575 if (!spa_writeable(spa
))
1578 if (!spa
->spa_sync_on
)
1581 if (spa_state(spa
) != POOL_STATE_EXPORTED
)
1584 if (zfs_keep_log_spacemaps_at_export
)
1591 * Opens a transaction that will set the flag that will instruct
1592 * spa_sync to attempt to flush all the metaslabs for that txg.
1595 spa_unload_log_sm_flush_all(spa_t
*spa
)
1597 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
1598 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
1600 ASSERT3U(spa
->spa_log_flushall_txg
, ==, 0);
1601 spa
->spa_log_flushall_txg
= dmu_tx_get_txg(tx
);
1604 txg_wait_synced(spa_get_dsl(spa
), spa
->spa_log_flushall_txg
);
1608 spa_unload_log_sm_metadata(spa_t
*spa
)
1610 void *cookie
= NULL
;
1612 log_summary_entry_t
*e
;
1614 while ((sls
= avl_destroy_nodes(&spa
->spa_sm_logs_by_txg
,
1615 &cookie
)) != NULL
) {
1616 VERIFY0(sls
->sls_mscount
);
1617 kmem_free(sls
, sizeof (spa_log_sm_t
));
1620 while ((e
= list_remove_head(&spa
->spa_log_summary
)) != NULL
) {
1621 VERIFY0(e
->lse_mscount
);
1622 kmem_free(e
, sizeof (log_summary_entry_t
));
1625 spa
->spa_unflushed_stats
.sus_nblocks
= 0;
1626 spa
->spa_unflushed_stats
.sus_memused
= 0;
1627 spa
->spa_unflushed_stats
.sus_blocklimit
= 0;
1631 spa_destroy_aux_threads(spa_t
*spa
)
1633 if (spa
->spa_condense_zthr
!= NULL
) {
1634 zthr_destroy(spa
->spa_condense_zthr
);
1635 spa
->spa_condense_zthr
= NULL
;
1637 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1638 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1639 spa
->spa_checkpoint_discard_zthr
= NULL
;
1641 if (spa
->spa_livelist_delete_zthr
!= NULL
) {
1642 zthr_destroy(spa
->spa_livelist_delete_zthr
);
1643 spa
->spa_livelist_delete_zthr
= NULL
;
1645 if (spa
->spa_livelist_condense_zthr
!= NULL
) {
1646 zthr_destroy(spa
->spa_livelist_condense_zthr
);
1647 spa
->spa_livelist_condense_zthr
= NULL
;
1652 * Opposite of spa_load().
1655 spa_unload(spa_t
*spa
)
1657 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1658 ASSERT(spa_state(spa
) != POOL_STATE_UNINITIALIZED
);
1660 spa_import_progress_remove(spa_guid(spa
));
1661 spa_load_note(spa
, "UNLOADING");
1663 spa_wake_waiters(spa
);
1666 * If we have set the spa_final_txg, we have already performed the
1667 * tasks below in spa_export_common(). We should not redo it here since
1668 * we delay the final TXGs beyond what spa_final_txg is set at.
1670 if (spa
->spa_final_txg
== UINT64_MAX
) {
1672 * If the log space map feature is enabled and the pool is
1673 * getting exported (but not destroyed), we want to spend some
1674 * time flushing as many metaslabs as we can in an attempt to
1675 * destroy log space maps and save import time.
1677 if (spa_should_flush_logs_on_unload(spa
))
1678 spa_unload_log_sm_flush_all(spa
);
1683 spa_async_suspend(spa
);
1685 if (spa
->spa_root_vdev
) {
1686 vdev_t
*root_vdev
= spa
->spa_root_vdev
;
1687 vdev_initialize_stop_all(root_vdev
,
1688 VDEV_INITIALIZE_ACTIVE
);
1689 vdev_trim_stop_all(root_vdev
, VDEV_TRIM_ACTIVE
);
1690 vdev_autotrim_stop_all(spa
);
1691 vdev_rebuild_stop_all(spa
);
1698 if (spa
->spa_sync_on
) {
1699 txg_sync_stop(spa
->spa_dsl_pool
);
1700 spa
->spa_sync_on
= B_FALSE
;
1704 * This ensures that there is no async metaslab prefetching
1705 * while we attempt to unload the spa.
1707 if (spa
->spa_root_vdev
!= NULL
) {
1708 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++) {
1709 vdev_t
*vc
= spa
->spa_root_vdev
->vdev_child
[c
];
1710 if (vc
->vdev_mg
!= NULL
)
1711 taskq_wait(vc
->vdev_mg
->mg_taskq
);
1715 if (spa
->spa_mmp
.mmp_thread
)
1716 mmp_thread_stop(spa
);
1719 * Wait for any outstanding async I/O to complete.
1721 if (spa
->spa_async_zio_root
!= NULL
) {
1722 for (int i
= 0; i
< max_ncpus
; i
++)
1723 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1724 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1725 spa
->spa_async_zio_root
= NULL
;
1728 if (spa
->spa_vdev_removal
!= NULL
) {
1729 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1730 spa
->spa_vdev_removal
= NULL
;
1733 spa_destroy_aux_threads(spa
);
1735 spa_condense_fini(spa
);
1737 bpobj_close(&spa
->spa_deferred_bpobj
);
1739 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1744 if (spa
->spa_root_vdev
)
1745 vdev_free(spa
->spa_root_vdev
);
1746 ASSERT(spa
->spa_root_vdev
== NULL
);
1749 * Close the dsl pool.
1751 if (spa
->spa_dsl_pool
) {
1752 dsl_pool_close(spa
->spa_dsl_pool
);
1753 spa
->spa_dsl_pool
= NULL
;
1754 spa
->spa_meta_objset
= NULL
;
1759 spa_unload_log_sm_metadata(spa
);
1762 * Drop and purge level 2 cache
1764 spa_l2cache_drop(spa
);
1766 if (spa
->spa_spares
.sav_vdevs
) {
1767 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1768 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1769 kmem_free(spa
->spa_spares
.sav_vdevs
,
1770 spa
->spa_spares
.sav_count
* sizeof (void *));
1771 spa
->spa_spares
.sav_vdevs
= NULL
;
1773 if (spa
->spa_spares
.sav_config
) {
1774 nvlist_free(spa
->spa_spares
.sav_config
);
1775 spa
->spa_spares
.sav_config
= NULL
;
1777 spa
->spa_spares
.sav_count
= 0;
1779 if (spa
->spa_l2cache
.sav_vdevs
) {
1780 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1781 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1782 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1784 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1785 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1786 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1788 if (spa
->spa_l2cache
.sav_config
) {
1789 nvlist_free(spa
->spa_l2cache
.sav_config
);
1790 spa
->spa_l2cache
.sav_config
= NULL
;
1792 spa
->spa_l2cache
.sav_count
= 0;
1794 spa
->spa_async_suspended
= 0;
1796 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1798 if (spa
->spa_comment
!= NULL
) {
1799 spa_strfree(spa
->spa_comment
);
1800 spa
->spa_comment
= NULL
;
1802 if (spa
->spa_compatibility
!= NULL
) {
1803 spa_strfree(spa
->spa_compatibility
);
1804 spa
->spa_compatibility
= NULL
;
1807 spa_config_exit(spa
, SCL_ALL
, spa
);
1811 * Load (or re-load) the current list of vdevs describing the active spares for
1812 * this pool. When this is called, we have some form of basic information in
1813 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1814 * then re-generate a more complete list including status information.
1817 spa_load_spares(spa_t
*spa
)
1826 * zdb opens both the current state of the pool and the
1827 * checkpointed state (if present), with a different spa_t.
1829 * As spare vdevs are shared among open pools, we skip loading
1830 * them when we load the checkpointed state of the pool.
1832 if (!spa_writeable(spa
))
1836 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1839 * First, close and free any existing spare vdevs.
1841 if (spa
->spa_spares
.sav_vdevs
) {
1842 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1843 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1845 /* Undo the call to spa_activate() below */
1846 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1847 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1848 spa_spare_remove(tvd
);
1853 kmem_free(spa
->spa_spares
.sav_vdevs
,
1854 spa
->spa_spares
.sav_count
* sizeof (void *));
1857 if (spa
->spa_spares
.sav_config
== NULL
)
1860 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1861 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
1863 spa
->spa_spares
.sav_count
= (int)nspares
;
1864 spa
->spa_spares
.sav_vdevs
= NULL
;
1870 * Construct the array of vdevs, opening them to get status in the
1871 * process. For each spare, there is potentially two different vdev_t
1872 * structures associated with it: one in the list of spares (used only
1873 * for basic validation purposes) and one in the active vdev
1874 * configuration (if it's spared in). During this phase we open and
1875 * validate each vdev on the spare list. If the vdev also exists in the
1876 * active configuration, then we also mark this vdev as an active spare.
1878 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1880 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1881 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1882 VDEV_ALLOC_SPARE
) == 0);
1885 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1887 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1888 B_FALSE
)) != NULL
) {
1889 if (!tvd
->vdev_isspare
)
1893 * We only mark the spare active if we were successfully
1894 * able to load the vdev. Otherwise, importing a pool
1895 * with a bad active spare would result in strange
1896 * behavior, because multiple pool would think the spare
1897 * is actively in use.
1899 * There is a vulnerability here to an equally bizarre
1900 * circumstance, where a dead active spare is later
1901 * brought back to life (onlined or otherwise). Given
1902 * the rarity of this scenario, and the extra complexity
1903 * it adds, we ignore the possibility.
1905 if (!vdev_is_dead(tvd
))
1906 spa_spare_activate(tvd
);
1910 vd
->vdev_aux
= &spa
->spa_spares
;
1912 if (vdev_open(vd
) != 0)
1915 if (vdev_validate_aux(vd
) == 0)
1920 * Recompute the stashed list of spares, with status information
1923 fnvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
);
1925 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1927 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1928 spares
[i
] = vdev_config_generate(spa
,
1929 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1930 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1931 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
1932 spa
->spa_spares
.sav_count
);
1933 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1934 nvlist_free(spares
[i
]);
1935 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1939 * Load (or re-load) the current list of vdevs describing the active l2cache for
1940 * this pool. When this is called, we have some form of basic information in
1941 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1942 * then re-generate a more complete list including status information.
1943 * Devices which are already active have their details maintained, and are
1947 spa_load_l2cache(spa_t
*spa
)
1949 nvlist_t
**l2cache
= NULL
;
1951 int i
, j
, oldnvdevs
;
1953 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1954 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1958 * zdb opens both the current state of the pool and the
1959 * checkpointed state (if present), with a different spa_t.
1961 * As L2 caches are part of the ARC which is shared among open
1962 * pools, we skip loading them when we load the checkpointed
1963 * state of the pool.
1965 if (!spa_writeable(spa
))
1969 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1971 oldvdevs
= sav
->sav_vdevs
;
1972 oldnvdevs
= sav
->sav_count
;
1973 sav
->sav_vdevs
= NULL
;
1976 if (sav
->sav_config
== NULL
) {
1982 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
,
1983 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
1984 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1987 * Process new nvlist of vdevs.
1989 for (i
= 0; i
< nl2cache
; i
++) {
1990 guid
= fnvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
);
1993 for (j
= 0; j
< oldnvdevs
; j
++) {
1995 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1997 * Retain previous vdev for add/remove ops.
2005 if (newvdevs
[i
] == NULL
) {
2009 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
2010 VDEV_ALLOC_L2CACHE
) == 0);
2015 * Commit this vdev as an l2cache device,
2016 * even if it fails to open.
2018 spa_l2cache_add(vd
);
2023 spa_l2cache_activate(vd
);
2025 if (vdev_open(vd
) != 0)
2028 (void) vdev_validate_aux(vd
);
2030 if (!vdev_is_dead(vd
))
2031 l2arc_add_vdev(spa
, vd
);
2034 * Upon cache device addition to a pool or pool
2035 * creation with a cache device or if the header
2036 * of the device is invalid we issue an async
2037 * TRIM command for the whole device which will
2038 * execute if l2arc_trim_ahead > 0.
2040 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2044 sav
->sav_vdevs
= newvdevs
;
2045 sav
->sav_count
= (int)nl2cache
;
2048 * Recompute the stashed list of l2cache devices, with status
2049 * information this time.
2051 fnvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
);
2053 if (sav
->sav_count
> 0)
2054 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
2056 for (i
= 0; i
< sav
->sav_count
; i
++)
2057 l2cache
[i
] = vdev_config_generate(spa
,
2058 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
2059 fnvlist_add_nvlist_array(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
2060 (const nvlist_t
* const *)l2cache
, sav
->sav_count
);
2064 * Purge vdevs that were dropped
2067 for (i
= 0; i
< oldnvdevs
; i
++) {
2072 ASSERT(vd
->vdev_isl2cache
);
2074 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
2075 pool
!= 0ULL && l2arc_vdev_present(vd
))
2076 l2arc_remove_vdev(vd
);
2077 vdev_clear_stats(vd
);
2082 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
2085 for (i
= 0; i
< sav
->sav_count
; i
++)
2086 nvlist_free(l2cache
[i
]);
2088 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
2092 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
2095 char *packed
= NULL
;
2100 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
2104 nvsize
= *(uint64_t *)db
->db_data
;
2105 dmu_buf_rele(db
, FTAG
);
2107 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
2108 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
2111 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
2112 vmem_free(packed
, nvsize
);
2118 * Concrete top-level vdevs that are not missing and are not logs. At every
2119 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
2122 spa_healthy_core_tvds(spa_t
*spa
)
2124 vdev_t
*rvd
= spa
->spa_root_vdev
;
2127 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
2128 vdev_t
*vd
= rvd
->vdev_child
[i
];
2131 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
2139 * Checks to see if the given vdev could not be opened, in which case we post a
2140 * sysevent to notify the autoreplace code that the device has been removed.
2143 spa_check_removed(vdev_t
*vd
)
2145 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2146 spa_check_removed(vd
->vdev_child
[c
]);
2148 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
2149 vdev_is_concrete(vd
)) {
2150 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
2151 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
2156 spa_check_for_missing_logs(spa_t
*spa
)
2158 vdev_t
*rvd
= spa
->spa_root_vdev
;
2161 * If we're doing a normal import, then build up any additional
2162 * diagnostic information about missing log devices.
2163 * We'll pass this up to the user for further processing.
2165 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
2166 nvlist_t
**child
, *nv
;
2169 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
2171 nv
= fnvlist_alloc();
2173 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2174 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2177 * We consider a device as missing only if it failed
2178 * to open (i.e. offline or faulted is not considered
2181 if (tvd
->vdev_islog
&&
2182 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2183 child
[idx
++] = vdev_config_generate(spa
, tvd
,
2184 B_FALSE
, VDEV_CONFIG_MISSING
);
2189 fnvlist_add_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2190 (const nvlist_t
* const *)child
, idx
);
2191 fnvlist_add_nvlist(spa
->spa_load_info
,
2192 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
2194 for (uint64_t i
= 0; i
< idx
; i
++)
2195 nvlist_free(child
[i
]);
2198 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
2201 spa_load_failed(spa
, "some log devices are missing");
2202 vdev_dbgmsg_print_tree(rvd
, 2);
2203 return (SET_ERROR(ENXIO
));
2206 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2207 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2209 if (tvd
->vdev_islog
&&
2210 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2211 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2212 spa_load_note(spa
, "some log devices are "
2213 "missing, ZIL is dropped.");
2214 vdev_dbgmsg_print_tree(rvd
, 2);
2224 * Check for missing log devices
2227 spa_check_logs(spa_t
*spa
)
2229 boolean_t rv
= B_FALSE
;
2230 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2232 switch (spa
->spa_log_state
) {
2235 case SPA_LOG_MISSING
:
2236 /* need to recheck in case slog has been restored */
2237 case SPA_LOG_UNKNOWN
:
2238 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2239 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
2241 spa_set_log_state(spa
, SPA_LOG_MISSING
);
2248 * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2251 spa_passivate_log(spa_t
*spa
)
2253 vdev_t
*rvd
= spa
->spa_root_vdev
;
2254 boolean_t slog_found
= B_FALSE
;
2256 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2258 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2259 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2261 if (tvd
->vdev_islog
) {
2262 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2263 metaslab_group_passivate(tvd
->vdev_mg
);
2264 slog_found
= B_TRUE
;
2268 return (slog_found
);
2272 * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2275 spa_activate_log(spa_t
*spa
)
2277 vdev_t
*rvd
= spa
->spa_root_vdev
;
2279 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2281 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2282 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2284 if (tvd
->vdev_islog
) {
2285 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2286 metaslab_group_activate(tvd
->vdev_mg
);
2292 spa_reset_logs(spa_t
*spa
)
2296 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2297 NULL
, DS_FIND_CHILDREN
);
2300 * We successfully offlined the log device, sync out the
2301 * current txg so that the "stubby" block can be removed
2304 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2310 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2312 for (int i
= 0; i
< sav
->sav_count
; i
++)
2313 spa_check_removed(sav
->sav_vdevs
[i
]);
2317 spa_claim_notify(zio_t
*zio
)
2319 spa_t
*spa
= zio
->io_spa
;
2324 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2325 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2326 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2327 mutex_exit(&spa
->spa_props_lock
);
2330 typedef struct spa_load_error
{
2331 boolean_t sle_verify_data
;
2332 uint64_t sle_meta_count
;
2333 uint64_t sle_data_count
;
2337 spa_load_verify_done(zio_t
*zio
)
2339 blkptr_t
*bp
= zio
->io_bp
;
2340 spa_load_error_t
*sle
= zio
->io_private
;
2341 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2342 int error
= zio
->io_error
;
2343 spa_t
*spa
= zio
->io_spa
;
2345 abd_free(zio
->io_abd
);
2347 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2348 type
!= DMU_OT_INTENT_LOG
)
2349 atomic_inc_64(&sle
->sle_meta_count
);
2351 atomic_inc_64(&sle
->sle_data_count
);
2354 mutex_enter(&spa
->spa_scrub_lock
);
2355 spa
->spa_load_verify_bytes
-= BP_GET_PSIZE(bp
);
2356 cv_broadcast(&spa
->spa_scrub_io_cv
);
2357 mutex_exit(&spa
->spa_scrub_lock
);
2361 * Maximum number of inflight bytes is the log2 fraction of the arc size.
2362 * By default, we set it to 1/16th of the arc.
2364 static uint_t spa_load_verify_shift
= 4;
2365 static int spa_load_verify_metadata
= B_TRUE
;
2366 static int spa_load_verify_data
= B_TRUE
;
2369 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2370 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2373 spa_load_error_t
*sle
= rio
->io_private
;
2375 (void) zilog
, (void) dnp
;
2378 * Note: normally this routine will not be called if
2379 * spa_load_verify_metadata is not set. However, it may be useful
2380 * to manually set the flag after the traversal has begun.
2382 if (!spa_load_verify_metadata
)
2386 * Sanity check the block pointer in order to detect obvious damage
2387 * before using the contents in subsequent checks or in zio_read().
2388 * When damaged consider it to be a metadata error since we cannot
2389 * trust the BP_GET_TYPE and BP_GET_LEVEL values.
2391 if (!zfs_blkptr_verify(spa
, bp
, BLK_CONFIG_NEEDED
, BLK_VERIFY_LOG
)) {
2392 atomic_inc_64(&sle
->sle_meta_count
);
2396 if (zb
->zb_level
== ZB_DNODE_LEVEL
|| BP_IS_HOLE(bp
) ||
2397 BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
2400 if (!BP_IS_METADATA(bp
) &&
2401 (!spa_load_verify_data
|| !sle
->sle_verify_data
))
2404 uint64_t maxinflight_bytes
=
2405 arc_target_bytes() >> spa_load_verify_shift
;
2406 size_t size
= BP_GET_PSIZE(bp
);
2408 mutex_enter(&spa
->spa_scrub_lock
);
2409 while (spa
->spa_load_verify_bytes
>= maxinflight_bytes
)
2410 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2411 spa
->spa_load_verify_bytes
+= size
;
2412 mutex_exit(&spa
->spa_scrub_lock
);
2414 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2415 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2416 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2417 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2422 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2424 (void) dp
, (void) arg
;
2426 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2427 return (SET_ERROR(ENAMETOOLONG
));
2433 spa_load_verify(spa_t
*spa
)
2436 spa_load_error_t sle
= { 0 };
2437 zpool_load_policy_t policy
;
2438 boolean_t verify_ok
= B_FALSE
;
2441 zpool_get_load_policy(spa
->spa_config
, &policy
);
2443 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
||
2444 policy
.zlp_maxmeta
== UINT64_MAX
)
2447 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2448 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2449 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2451 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2456 * Verify data only if we are rewinding or error limit was set.
2457 * Otherwise nothing except dbgmsg care about it to waste time.
2459 sle
.sle_verify_data
= (policy
.zlp_rewind
& ZPOOL_REWIND_MASK
) ||
2460 (policy
.zlp_maxdata
< UINT64_MAX
);
2462 rio
= zio_root(spa
, NULL
, &sle
,
2463 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2465 if (spa_load_verify_metadata
) {
2466 if (spa
->spa_extreme_rewind
) {
2467 spa_load_note(spa
, "performing a complete scan of the "
2468 "pool since extreme rewind is on. This may take "
2469 "a very long time.\n (spa_load_verify_data=%u, "
2470 "spa_load_verify_metadata=%u)",
2471 spa_load_verify_data
, spa_load_verify_metadata
);
2474 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2475 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2476 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2479 (void) zio_wait(rio
);
2480 ASSERT0(spa
->spa_load_verify_bytes
);
2482 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2483 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2485 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2486 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2487 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2488 (u_longlong_t
)sle
.sle_data_count
);
2491 if (spa_load_verify_dryrun
||
2492 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2493 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2497 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2498 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2500 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2501 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_LOAD_TIME
,
2502 spa
->spa_load_txg_ts
);
2503 fnvlist_add_int64(spa
->spa_load_info
, ZPOOL_CONFIG_REWIND_TIME
,
2505 fnvlist_add_uint64(spa
->spa_load_info
,
2506 ZPOOL_CONFIG_LOAD_META_ERRORS
, sle
.sle_meta_count
);
2507 fnvlist_add_uint64(spa
->spa_load_info
,
2508 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
);
2510 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2513 if (spa_load_verify_dryrun
)
2517 if (error
!= ENXIO
&& error
!= EIO
)
2518 error
= SET_ERROR(EIO
);
2522 return (verify_ok
? 0 : EIO
);
2526 * Find a value in the pool props object.
2529 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2531 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2532 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2536 * Find a value in the pool directory object.
2539 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2541 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2542 name
, sizeof (uint64_t), 1, val
);
2544 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2545 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2546 "[error=%d]", name
, error
);
2553 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2555 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2556 return (SET_ERROR(err
));
2560 spa_livelist_delete_check(spa_t
*spa
)
2562 return (spa
->spa_livelists_to_delete
!= 0);
2566 spa_livelist_delete_cb_check(void *arg
, zthr_t
*z
)
2570 return (spa_livelist_delete_check(spa
));
2574 delete_blkptr_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2577 zio_free(spa
, tx
->tx_txg
, bp
);
2578 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
2579 -bp_get_dsize_sync(spa
, bp
),
2580 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
2585 dsl_get_next_livelist_obj(objset_t
*os
, uint64_t zap_obj
, uint64_t *llp
)
2590 zap_cursor_init(&zc
, os
, zap_obj
);
2591 err
= zap_cursor_retrieve(&zc
, &za
);
2592 zap_cursor_fini(&zc
);
2594 *llp
= za
.za_first_integer
;
2599 * Components of livelist deletion that must be performed in syncing
2600 * context: freeing block pointers and updating the pool-wide data
2601 * structures to indicate how much work is left to do
2603 typedef struct sublist_delete_arg
{
2608 } sublist_delete_arg_t
;
2611 sublist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2613 sublist_delete_arg_t
*sda
= arg
;
2614 spa_t
*spa
= sda
->spa
;
2615 dsl_deadlist_t
*ll
= sda
->ll
;
2616 uint64_t key
= sda
->key
;
2617 bplist_t
*to_free
= sda
->to_free
;
2619 bplist_iterate(to_free
, delete_blkptr_cb
, spa
, tx
);
2620 dsl_deadlist_remove_entry(ll
, key
, tx
);
2623 typedef struct livelist_delete_arg
{
2627 } livelist_delete_arg_t
;
2630 livelist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2632 livelist_delete_arg_t
*lda
= arg
;
2633 spa_t
*spa
= lda
->spa
;
2634 uint64_t ll_obj
= lda
->ll_obj
;
2635 uint64_t zap_obj
= lda
->zap_obj
;
2636 objset_t
*mos
= spa
->spa_meta_objset
;
2639 /* free the livelist and decrement the feature count */
2640 VERIFY0(zap_remove_int(mos
, zap_obj
, ll_obj
, tx
));
2641 dsl_deadlist_free(mos
, ll_obj
, tx
);
2642 spa_feature_decr(spa
, SPA_FEATURE_LIVELIST
, tx
);
2643 VERIFY0(zap_count(mos
, zap_obj
, &count
));
2645 /* no more livelists to delete */
2646 VERIFY0(zap_remove(mos
, DMU_POOL_DIRECTORY_OBJECT
,
2647 DMU_POOL_DELETED_CLONES
, tx
));
2648 VERIFY0(zap_destroy(mos
, zap_obj
, tx
));
2649 spa
->spa_livelists_to_delete
= 0;
2650 spa_notify_waiters(spa
);
2655 * Load in the value for the livelist to be removed and open it. Then,
2656 * load its first sublist and determine which block pointers should actually
2657 * be freed. Then, call a synctask which performs the actual frees and updates
2658 * the pool-wide livelist data.
2661 spa_livelist_delete_cb(void *arg
, zthr_t
*z
)
2664 uint64_t ll_obj
= 0, count
;
2665 objset_t
*mos
= spa
->spa_meta_objset
;
2666 uint64_t zap_obj
= spa
->spa_livelists_to_delete
;
2668 * Determine the next livelist to delete. This function should only
2669 * be called if there is at least one deleted clone.
2671 VERIFY0(dsl_get_next_livelist_obj(mos
, zap_obj
, &ll_obj
));
2672 VERIFY0(zap_count(mos
, ll_obj
, &count
));
2675 dsl_deadlist_entry_t
*dle
;
2677 ll
= kmem_zalloc(sizeof (dsl_deadlist_t
), KM_SLEEP
);
2678 dsl_deadlist_open(ll
, mos
, ll_obj
);
2679 dle
= dsl_deadlist_first(ll
);
2680 ASSERT3P(dle
, !=, NULL
);
2681 bplist_create(&to_free
);
2682 int err
= dsl_process_sub_livelist(&dle
->dle_bpobj
, &to_free
,
2685 sublist_delete_arg_t sync_arg
= {
2688 .key
= dle
->dle_mintxg
,
2691 zfs_dbgmsg("deleting sublist (id %llu) from"
2692 " livelist %llu, %lld remaining",
2693 (u_longlong_t
)dle
->dle_bpobj
.bpo_object
,
2694 (u_longlong_t
)ll_obj
, (longlong_t
)count
- 1);
2695 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
2696 sublist_delete_sync
, &sync_arg
, 0,
2697 ZFS_SPACE_CHECK_DESTROY
));
2699 VERIFY3U(err
, ==, EINTR
);
2701 bplist_clear(&to_free
);
2702 bplist_destroy(&to_free
);
2703 dsl_deadlist_close(ll
);
2704 kmem_free(ll
, sizeof (dsl_deadlist_t
));
2706 livelist_delete_arg_t sync_arg
= {
2711 zfs_dbgmsg("deletion of livelist %llu completed",
2712 (u_longlong_t
)ll_obj
);
2713 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
, livelist_delete_sync
,
2714 &sync_arg
, 0, ZFS_SPACE_CHECK_DESTROY
));
2719 spa_start_livelist_destroy_thread(spa_t
*spa
)
2721 ASSERT3P(spa
->spa_livelist_delete_zthr
, ==, NULL
);
2722 spa
->spa_livelist_delete_zthr
=
2723 zthr_create("z_livelist_destroy",
2724 spa_livelist_delete_cb_check
, spa_livelist_delete_cb
, spa
,
2728 typedef struct livelist_new_arg
{
2731 } livelist_new_arg_t
;
2734 livelist_track_new_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
2738 livelist_new_arg_t
*lna
= arg
;
2740 bplist_append(lna
->frees
, bp
);
2742 bplist_append(lna
->allocs
, bp
);
2743 zfs_livelist_condense_new_alloc
++;
2748 typedef struct livelist_condense_arg
{
2751 uint64_t first_size
;
2753 } livelist_condense_arg_t
;
2756 spa_livelist_condense_sync(void *arg
, dmu_tx_t
*tx
)
2758 livelist_condense_arg_t
*lca
= arg
;
2759 spa_t
*spa
= lca
->spa
;
2761 dsl_dataset_t
*ds
= spa
->spa_to_condense
.ds
;
2763 /* Have we been cancelled? */
2764 if (spa
->spa_to_condense
.cancelled
) {
2765 zfs_livelist_condense_sync_cancel
++;
2769 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2770 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2771 dsl_deadlist_t
*ll
= &ds
->ds_dir
->dd_livelist
;
2774 * It's possible that the livelist was changed while the zthr was
2775 * running. Therefore, we need to check for new blkptrs in the two
2776 * entries being condensed and continue to track them in the livelist.
2777 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
2778 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
2779 * we need to sort them into two different bplists.
2781 uint64_t first_obj
= first
->dle_bpobj
.bpo_object
;
2782 uint64_t next_obj
= next
->dle_bpobj
.bpo_object
;
2783 uint64_t cur_first_size
= first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2784 uint64_t cur_next_size
= next
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2786 bplist_create(&new_frees
);
2787 livelist_new_arg_t new_bps
= {
2788 .allocs
= &lca
->to_keep
,
2789 .frees
= &new_frees
,
2792 if (cur_first_size
> lca
->first_size
) {
2793 VERIFY0(livelist_bpobj_iterate_from_nofree(&first
->dle_bpobj
,
2794 livelist_track_new_cb
, &new_bps
, lca
->first_size
));
2796 if (cur_next_size
> lca
->next_size
) {
2797 VERIFY0(livelist_bpobj_iterate_from_nofree(&next
->dle_bpobj
,
2798 livelist_track_new_cb
, &new_bps
, lca
->next_size
));
2801 dsl_deadlist_clear_entry(first
, ll
, tx
);
2802 ASSERT(bpobj_is_empty(&first
->dle_bpobj
));
2803 dsl_deadlist_remove_entry(ll
, next
->dle_mintxg
, tx
);
2805 bplist_iterate(&lca
->to_keep
, dsl_deadlist_insert_alloc_cb
, ll
, tx
);
2806 bplist_iterate(&new_frees
, dsl_deadlist_insert_free_cb
, ll
, tx
);
2807 bplist_destroy(&new_frees
);
2809 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
2810 dsl_dataset_name(ds
, dsname
);
2811 zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
2812 "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
2813 "(%llu blkptrs)", (u_longlong_t
)tx
->tx_txg
, dsname
,
2814 (u_longlong_t
)ds
->ds_object
, (u_longlong_t
)first_obj
,
2815 (u_longlong_t
)cur_first_size
, (u_longlong_t
)next_obj
,
2816 (u_longlong_t
)cur_next_size
,
2817 (u_longlong_t
)first
->dle_bpobj
.bpo_object
,
2818 (u_longlong_t
)first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
);
2820 dmu_buf_rele(ds
->ds_dbuf
, spa
);
2821 spa
->spa_to_condense
.ds
= NULL
;
2822 bplist_clear(&lca
->to_keep
);
2823 bplist_destroy(&lca
->to_keep
);
2824 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2825 spa
->spa_to_condense
.syncing
= B_FALSE
;
2829 spa_livelist_condense_cb(void *arg
, zthr_t
*t
)
2831 while (zfs_livelist_condense_zthr_pause
&&
2832 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2836 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2837 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2838 uint64_t first_size
, next_size
;
2840 livelist_condense_arg_t
*lca
=
2841 kmem_alloc(sizeof (livelist_condense_arg_t
), KM_SLEEP
);
2842 bplist_create(&lca
->to_keep
);
2845 * Process the livelists (matching FREEs and ALLOCs) in open context
2846 * so we have minimal work in syncing context to condense.
2848 * We save bpobj sizes (first_size and next_size) to use later in
2849 * syncing context to determine if entries were added to these sublists
2850 * while in open context. This is possible because the clone is still
2851 * active and open for normal writes and we want to make sure the new,
2852 * unprocessed blockpointers are inserted into the livelist normally.
2854 * Note that dsl_process_sub_livelist() both stores the size number of
2855 * blockpointers and iterates over them while the bpobj's lock held, so
2856 * the sizes returned to us are consistent which what was actually
2859 int err
= dsl_process_sub_livelist(&first
->dle_bpobj
, &lca
->to_keep
, t
,
2862 err
= dsl_process_sub_livelist(&next
->dle_bpobj
, &lca
->to_keep
,
2866 while (zfs_livelist_condense_sync_pause
&&
2867 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2870 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
2871 dmu_tx_mark_netfree(tx
);
2872 dmu_tx_hold_space(tx
, 1);
2873 err
= dmu_tx_assign(tx
, TXG_NOWAIT
| TXG_NOTHROTTLE
);
2876 * Prevent the condense zthr restarting before
2877 * the synctask completes.
2879 spa
->spa_to_condense
.syncing
= B_TRUE
;
2881 lca
->first_size
= first_size
;
2882 lca
->next_size
= next_size
;
2883 dsl_sync_task_nowait(spa_get_dsl(spa
),
2884 spa_livelist_condense_sync
, lca
, tx
);
2890 * Condensing can not continue: either it was externally stopped or
2891 * we were unable to assign to a tx because the pool has run out of
2892 * space. In the second case, we'll just end up trying to condense
2893 * again in a later txg.
2896 bplist_clear(&lca
->to_keep
);
2897 bplist_destroy(&lca
->to_keep
);
2898 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2899 dmu_buf_rele(spa
->spa_to_condense
.ds
->ds_dbuf
, spa
);
2900 spa
->spa_to_condense
.ds
= NULL
;
2902 zfs_livelist_condense_zthr_cancel
++;
2906 * Check that there is something to condense but that a condense is not
2907 * already in progress and that condensing has not been cancelled.
2910 spa_livelist_condense_cb_check(void *arg
, zthr_t
*z
)
2914 if ((spa
->spa_to_condense
.ds
!= NULL
) &&
2915 (spa
->spa_to_condense
.syncing
== B_FALSE
) &&
2916 (spa
->spa_to_condense
.cancelled
== B_FALSE
)) {
2923 spa_start_livelist_condensing_thread(spa_t
*spa
)
2925 spa
->spa_to_condense
.ds
= NULL
;
2926 spa
->spa_to_condense
.first
= NULL
;
2927 spa
->spa_to_condense
.next
= NULL
;
2928 spa
->spa_to_condense
.syncing
= B_FALSE
;
2929 spa
->spa_to_condense
.cancelled
= B_FALSE
;
2931 ASSERT3P(spa
->spa_livelist_condense_zthr
, ==, NULL
);
2932 spa
->spa_livelist_condense_zthr
=
2933 zthr_create("z_livelist_condense",
2934 spa_livelist_condense_cb_check
,
2935 spa_livelist_condense_cb
, spa
, minclsyspri
);
2939 spa_spawn_aux_threads(spa_t
*spa
)
2941 ASSERT(spa_writeable(spa
));
2943 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2945 spa_start_indirect_condensing_thread(spa
);
2946 spa_start_livelist_destroy_thread(spa
);
2947 spa_start_livelist_condensing_thread(spa
);
2949 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
2950 spa
->spa_checkpoint_discard_zthr
=
2951 zthr_create("z_checkpoint_discard",
2952 spa_checkpoint_discard_thread_check
,
2953 spa_checkpoint_discard_thread
, spa
, minclsyspri
);
2957 * Fix up config after a partly-completed split. This is done with the
2958 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2959 * pool have that entry in their config, but only the splitting one contains
2960 * a list of all the guids of the vdevs that are being split off.
2962 * This function determines what to do with that list: either rejoin
2963 * all the disks to the pool, or complete the splitting process. To attempt
2964 * the rejoin, each disk that is offlined is marked online again, and
2965 * we do a reopen() call. If the vdev label for every disk that was
2966 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2967 * then we call vdev_split() on each disk, and complete the split.
2969 * Otherwise we leave the config alone, with all the vdevs in place in
2970 * the original pool.
2973 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2980 boolean_t attempt_reopen
;
2982 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2985 /* check that the config is complete */
2986 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2987 &glist
, &gcount
) != 0)
2990 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2992 /* attempt to online all the vdevs & validate */
2993 attempt_reopen
= B_TRUE
;
2994 for (i
= 0; i
< gcount
; i
++) {
2995 if (glist
[i
] == 0) /* vdev is hole */
2998 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2999 if (vd
[i
] == NULL
) {
3001 * Don't bother attempting to reopen the disks;
3002 * just do the split.
3004 attempt_reopen
= B_FALSE
;
3006 /* attempt to re-online it */
3007 vd
[i
]->vdev_offline
= B_FALSE
;
3011 if (attempt_reopen
) {
3012 vdev_reopen(spa
->spa_root_vdev
);
3014 /* check each device to see what state it's in */
3015 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
3016 if (vd
[i
] != NULL
&&
3017 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
3024 * If every disk has been moved to the new pool, or if we never
3025 * even attempted to look at them, then we split them off for
3028 if (!attempt_reopen
|| gcount
== extracted
) {
3029 for (i
= 0; i
< gcount
; i
++)
3032 vdev_reopen(spa
->spa_root_vdev
);
3035 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
3039 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
3041 const char *ereport
= FM_EREPORT_ZFS_POOL
;
3044 spa
->spa_load_state
= state
;
3045 (void) spa_import_progress_set_state(spa_guid(spa
),
3046 spa_load_state(spa
));
3048 gethrestime(&spa
->spa_loaded_ts
);
3049 error
= spa_load_impl(spa
, type
, &ereport
);
3052 * Don't count references from objsets that are already closed
3053 * and are making their way through the eviction process.
3055 spa_evicting_os_wait(spa
);
3056 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
3058 if (error
!= EEXIST
) {
3059 spa
->spa_loaded_ts
.tv_sec
= 0;
3060 spa
->spa_loaded_ts
.tv_nsec
= 0;
3062 if (error
!= EBADF
) {
3063 (void) zfs_ereport_post(ereport
, spa
,
3064 NULL
, NULL
, NULL
, 0);
3067 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
3070 (void) spa_import_progress_set_state(spa_guid(spa
),
3071 spa_load_state(spa
));
3078 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
3079 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
3080 * spa's per-vdev ZAP list.
3083 vdev_count_verify_zaps(vdev_t
*vd
)
3085 spa_t
*spa
= vd
->vdev_spa
;
3088 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
) &&
3089 vd
->vdev_root_zap
!= 0) {
3091 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3092 spa
->spa_all_vdev_zaps
, vd
->vdev_root_zap
));
3094 if (vd
->vdev_top_zap
!= 0) {
3096 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3097 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
3099 if (vd
->vdev_leaf_zap
!= 0) {
3101 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3102 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
3105 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3106 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
3112 #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0)
3116 * Determine whether the activity check is required.
3119 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
3123 uint64_t hostid
= 0;
3124 uint64_t tryconfig_txg
= 0;
3125 uint64_t tryconfig_timestamp
= 0;
3126 uint16_t tryconfig_mmp_seq
= 0;
3129 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3130 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3131 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
3133 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
3134 &tryconfig_timestamp
);
3135 (void) nvlist_lookup_uint16(nvinfo
, ZPOOL_CONFIG_MMP_SEQ
,
3136 &tryconfig_mmp_seq
);
3139 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
3142 * Disable the MMP activity check - This is used by zdb which
3143 * is intended to be used on potentially active pools.
3145 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
3149 * Skip the activity check when the MMP feature is disabled.
3151 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
3155 * If the tryconfig_ values are nonzero, they are the results of an
3156 * earlier tryimport. If they all match the uberblock we just found,
3157 * then the pool has not changed and we return false so we do not test
3160 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
3161 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
&&
3162 tryconfig_mmp_seq
&& tryconfig_mmp_seq
==
3163 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0))
3167 * Allow the activity check to be skipped when importing the pool
3168 * on the same host which last imported it. Since the hostid from
3169 * configuration may be stale use the one read from the label.
3171 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
3172 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
3174 if (hostid
== spa_get_hostid(spa
))
3178 * Skip the activity test when the pool was cleanly exported.
3180 if (state
!= POOL_STATE_ACTIVE
)
3187 * Nanoseconds the activity check must watch for changes on-disk.
3190 spa_activity_check_duration(spa_t
*spa
, uberblock_t
*ub
)
3192 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
3193 uint64_t multihost_interval
= MSEC2NSEC(
3194 MMP_INTERVAL_OK(zfs_multihost_interval
));
3195 uint64_t import_delay
= MAX(NANOSEC
, import_intervals
*
3196 multihost_interval
);
3199 * Local tunables determine a minimum duration except for the case
3200 * where we know when the remote host will suspend the pool if MMP
3201 * writes do not land.
3203 * See Big Theory comment at the top of mmp.c for the reasoning behind
3204 * these cases and times.
3207 ASSERT(MMP_IMPORT_SAFETY_FACTOR
>= 100);
3209 if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3210 MMP_FAIL_INT(ub
) > 0) {
3212 /* MMP on remote host will suspend pool after failed writes */
3213 import_delay
= MMP_FAIL_INT(ub
) * MSEC2NSEC(MMP_INTERVAL(ub
)) *
3214 MMP_IMPORT_SAFETY_FACTOR
/ 100;
3216 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3217 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
3218 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3219 (u_longlong_t
)MMP_FAIL_INT(ub
),
3220 (u_longlong_t
)MMP_INTERVAL(ub
),
3221 (u_longlong_t
)import_intervals
);
3223 } else if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3224 MMP_FAIL_INT(ub
) == 0) {
3226 /* MMP on remote host will never suspend pool */
3227 import_delay
= MAX(import_delay
, (MSEC2NSEC(MMP_INTERVAL(ub
)) +
3228 ub
->ub_mmp_delay
) * import_intervals
);
3230 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3231 "mmp_interval=%llu ub_mmp_delay=%llu "
3232 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3233 (u_longlong_t
)MMP_INTERVAL(ub
),
3234 (u_longlong_t
)ub
->ub_mmp_delay
,
3235 (u_longlong_t
)import_intervals
);
3237 } else if (MMP_VALID(ub
)) {
3239 * zfs-0.7 compatibility case
3242 import_delay
= MAX(import_delay
, (multihost_interval
+
3243 ub
->ub_mmp_delay
) * import_intervals
);
3245 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
3246 "import_intervals=%llu leaves=%u",
3247 (u_longlong_t
)import_delay
,
3248 (u_longlong_t
)ub
->ub_mmp_delay
,
3249 (u_longlong_t
)import_intervals
,
3250 vdev_count_leaves(spa
));
3252 /* Using local tunings is the only reasonable option */
3253 zfs_dbgmsg("pool last imported on non-MMP aware "
3254 "host using import_delay=%llu multihost_interval=%llu "
3255 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3256 (u_longlong_t
)multihost_interval
,
3257 (u_longlong_t
)import_intervals
);
3260 return (import_delay
);
3264 * Perform the import activity check. If the user canceled the import or
3265 * we detected activity then fail.
3268 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
3270 uint64_t txg
= ub
->ub_txg
;
3271 uint64_t timestamp
= ub
->ub_timestamp
;
3272 uint64_t mmp_config
= ub
->ub_mmp_config
;
3273 uint16_t mmp_seq
= MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0;
3274 uint64_t import_delay
;
3275 hrtime_t import_expire
;
3276 nvlist_t
*mmp_label
= NULL
;
3277 vdev_t
*rvd
= spa
->spa_root_vdev
;
3282 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
3283 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3287 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3288 * during the earlier tryimport. If the txg recorded there is 0 then
3289 * the pool is known to be active on another host.
3291 * Otherwise, the pool might be in use on another host. Check for
3292 * changes in the uberblocks on disk if necessary.
3294 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3295 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
3296 ZPOOL_CONFIG_LOAD_INFO
);
3298 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
3299 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
3300 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3301 error
= SET_ERROR(EREMOTEIO
);
3306 import_delay
= spa_activity_check_duration(spa
, ub
);
3308 /* Add a small random factor in case of simultaneous imports (0-25%) */
3309 import_delay
+= import_delay
* random_in_range(250) / 1000;
3311 import_expire
= gethrtime() + import_delay
;
3313 while (gethrtime() < import_expire
) {
3314 (void) spa_import_progress_set_mmp_check(spa_guid(spa
),
3315 NSEC2SEC(import_expire
- gethrtime()));
3317 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3319 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
||
3320 mmp_seq
!= (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0)) {
3321 zfs_dbgmsg("multihost activity detected "
3322 "txg %llu ub_txg %llu "
3323 "timestamp %llu ub_timestamp %llu "
3324 "mmp_config %#llx ub_mmp_config %#llx",
3325 (u_longlong_t
)txg
, (u_longlong_t
)ub
->ub_txg
,
3326 (u_longlong_t
)timestamp
,
3327 (u_longlong_t
)ub
->ub_timestamp
,
3328 (u_longlong_t
)mmp_config
,
3329 (u_longlong_t
)ub
->ub_mmp_config
);
3331 error
= SET_ERROR(EREMOTEIO
);
3336 nvlist_free(mmp_label
);
3340 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
3342 error
= SET_ERROR(EINTR
);
3350 mutex_destroy(&mtx
);
3354 * If the pool is determined to be active store the status in the
3355 * spa->spa_load_info nvlist. If the remote hostname or hostid are
3356 * available from configuration read from disk store them as well.
3357 * This allows 'zpool import' to generate a more useful message.
3359 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
3360 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3361 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
3363 if (error
== EREMOTEIO
) {
3364 const char *hostname
= "<unknown>";
3365 uint64_t hostid
= 0;
3368 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
3369 hostname
= fnvlist_lookup_string(mmp_label
,
3370 ZPOOL_CONFIG_HOSTNAME
);
3371 fnvlist_add_string(spa
->spa_load_info
,
3372 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
3375 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
3376 hostid
= fnvlist_lookup_uint64(mmp_label
,
3377 ZPOOL_CONFIG_HOSTID
);
3378 fnvlist_add_uint64(spa
->spa_load_info
,
3379 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
3383 fnvlist_add_uint64(spa
->spa_load_info
,
3384 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
3385 fnvlist_add_uint64(spa
->spa_load_info
,
3386 ZPOOL_CONFIG_MMP_TXG
, 0);
3388 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
3392 nvlist_free(mmp_label
);
3398 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
3401 const char *hostname
;
3402 uint64_t myhostid
= 0;
3404 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
3405 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
3406 hostname
= fnvlist_lookup_string(mos_config
,
3407 ZPOOL_CONFIG_HOSTNAME
);
3409 myhostid
= zone_get_hostid(NULL
);
3411 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
3412 cmn_err(CE_WARN
, "pool '%s' could not be "
3413 "loaded as it was last accessed by "
3414 "another system (host: %s hostid: 0x%llx). "
3415 "See: https://openzfs.github.io/openzfs-docs/msg/"
3417 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
3418 spa_load_failed(spa
, "hostid verification failed: pool "
3419 "last accessed by host: %s (hostid: 0x%llx)",
3420 hostname
, (u_longlong_t
)hostid
);
3421 return (SET_ERROR(EBADF
));
3429 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
3432 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
3436 const char *comment
;
3437 const char *compatibility
;
3440 * Versioning wasn't explicitly added to the label until later, so if
3441 * it's not present treat it as the initial version.
3443 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
3444 &spa
->spa_ubsync
.ub_version
) != 0)
3445 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
3447 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
3448 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3449 ZPOOL_CONFIG_POOL_GUID
);
3450 return (SET_ERROR(EINVAL
));
3454 * If we are doing an import, ensure that the pool is not already
3455 * imported by checking if its pool guid already exists in the
3458 * The only case that we allow an already imported pool to be
3459 * imported again, is when the pool is checkpointed and we want to
3460 * look at its checkpointed state from userland tools like zdb.
3463 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3464 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3465 spa_guid_exists(pool_guid
, 0)) {
3467 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3468 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3469 spa_guid_exists(pool_guid
, 0) &&
3470 !spa_importing_readonly_checkpoint(spa
)) {
3472 spa_load_failed(spa
, "a pool with guid %llu is already open",
3473 (u_longlong_t
)pool_guid
);
3474 return (SET_ERROR(EEXIST
));
3477 spa
->spa_config_guid
= pool_guid
;
3479 nvlist_free(spa
->spa_load_info
);
3480 spa
->spa_load_info
= fnvlist_alloc();
3482 ASSERT(spa
->spa_comment
== NULL
);
3483 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3484 spa
->spa_comment
= spa_strdup(comment
);
3486 ASSERT(spa
->spa_compatibility
== NULL
);
3487 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMPATIBILITY
,
3488 &compatibility
) == 0)
3489 spa
->spa_compatibility
= spa_strdup(compatibility
);
3491 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
3492 &spa
->spa_config_txg
);
3494 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
3495 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
3497 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
3498 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3499 ZPOOL_CONFIG_VDEV_TREE
);
3500 return (SET_ERROR(EINVAL
));
3504 * Create "The Godfather" zio to hold all async IOs
3506 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3508 for (int i
= 0; i
< max_ncpus
; i
++) {
3509 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3510 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3511 ZIO_FLAG_GODFATHER
);
3515 * Parse the configuration into a vdev tree. We explicitly set the
3516 * value that will be returned by spa_version() since parsing the
3517 * configuration requires knowing the version number.
3519 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3520 parse
= (type
== SPA_IMPORT_EXISTING
?
3521 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
3522 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
3523 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3526 spa_load_failed(spa
, "unable to parse config [error=%d]",
3531 ASSERT(spa
->spa_root_vdev
== rvd
);
3532 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
3533 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
3535 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3536 ASSERT(spa_guid(spa
) == pool_guid
);
3543 * Recursively open all vdevs in the vdev tree. This function is called twice:
3544 * first with the untrusted config, then with the trusted config.
3547 spa_ld_open_vdevs(spa_t
*spa
)
3552 * spa_missing_tvds_allowed defines how many top-level vdevs can be
3553 * missing/unopenable for the root vdev to be still considered openable.
3555 if (spa
->spa_trust_config
) {
3556 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
3557 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
3558 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
3559 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
3560 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
3562 spa
->spa_missing_tvds_allowed
= 0;
3565 spa
->spa_missing_tvds_allowed
=
3566 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
3568 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3569 error
= vdev_open(spa
->spa_root_vdev
);
3570 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3572 if (spa
->spa_missing_tvds
!= 0) {
3573 spa_load_note(spa
, "vdev tree has %lld missing top-level "
3574 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
3575 if (spa
->spa_trust_config
&& (spa
->spa_mode
& SPA_MODE_WRITE
)) {
3577 * Although theoretically we could allow users to open
3578 * incomplete pools in RW mode, we'd need to add a lot
3579 * of extra logic (e.g. adjust pool space to account
3580 * for missing vdevs).
3581 * This limitation also prevents users from accidentally
3582 * opening the pool in RW mode during data recovery and
3583 * damaging it further.
3585 spa_load_note(spa
, "pools with missing top-level "
3586 "vdevs can only be opened in read-only mode.");
3587 error
= SET_ERROR(ENXIO
);
3589 spa_load_note(spa
, "current settings allow for maximum "
3590 "%lld missing top-level vdevs at this stage.",
3591 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
3595 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
3598 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
3599 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
3605 * We need to validate the vdev labels against the configuration that
3606 * we have in hand. This function is called twice: first with an untrusted
3607 * config, then with a trusted config. The validation is more strict when the
3608 * config is trusted.
3611 spa_ld_validate_vdevs(spa_t
*spa
)
3614 vdev_t
*rvd
= spa
->spa_root_vdev
;
3616 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3617 error
= vdev_validate(rvd
);
3618 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3621 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
3625 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
3626 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
3628 vdev_dbgmsg_print_tree(rvd
, 2);
3629 return (SET_ERROR(ENXIO
));
3636 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
3638 spa
->spa_state
= POOL_STATE_ACTIVE
;
3639 spa
->spa_ubsync
= spa
->spa_uberblock
;
3640 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
3641 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
3642 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
3643 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
3644 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
3645 spa
->spa_prev_software_version
= ub
->ub_software_version
;
3649 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
3651 vdev_t
*rvd
= spa
->spa_root_vdev
;
3653 uberblock_t
*ub
= &spa
->spa_uberblock
;
3654 boolean_t activity_check
= B_FALSE
;
3657 * If we are opening the checkpointed state of the pool by
3658 * rewinding to it, at this point we will have written the
3659 * checkpointed uberblock to the vdev labels, so searching
3660 * the labels will find the right uberblock. However, if
3661 * we are opening the checkpointed state read-only, we have
3662 * not modified the labels. Therefore, we must ignore the
3663 * labels and continue using the spa_uberblock that was set
3664 * by spa_ld_checkpoint_rewind.
3666 * Note that it would be fine to ignore the labels when
3667 * rewinding (opening writeable) as well. However, if we
3668 * crash just after writing the labels, we will end up
3669 * searching the labels. Doing so in the common case means
3670 * that this code path gets exercised normally, rather than
3671 * just in the edge case.
3673 if (ub
->ub_checkpoint_txg
!= 0 &&
3674 spa_importing_readonly_checkpoint(spa
)) {
3675 spa_ld_select_uberblock_done(spa
, ub
);
3680 * Find the best uberblock.
3682 vdev_uberblock_load(rvd
, ub
, &label
);
3685 * If we weren't able to find a single valid uberblock, return failure.
3687 if (ub
->ub_txg
== 0) {
3689 spa_load_failed(spa
, "no valid uberblock found");
3690 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
3693 if (spa
->spa_load_max_txg
!= UINT64_MAX
) {
3694 (void) spa_import_progress_set_max_txg(spa_guid(spa
),
3695 (u_longlong_t
)spa
->spa_load_max_txg
);
3697 spa_load_note(spa
, "using uberblock with txg=%llu",
3698 (u_longlong_t
)ub
->ub_txg
);
3702 * For pools which have the multihost property on determine if the
3703 * pool is truly inactive and can be safely imported. Prevent
3704 * hosts which don't have a hostid set from importing the pool.
3706 activity_check
= spa_activity_check_required(spa
, ub
, label
,
3708 if (activity_check
) {
3709 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
3710 spa_get_hostid(spa
) == 0) {
3712 fnvlist_add_uint64(spa
->spa_load_info
,
3713 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3714 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3717 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
3723 fnvlist_add_uint64(spa
->spa_load_info
,
3724 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
3725 fnvlist_add_uint64(spa
->spa_load_info
,
3726 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
3727 fnvlist_add_uint16(spa
->spa_load_info
,
3728 ZPOOL_CONFIG_MMP_SEQ
,
3729 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0));
3733 * If the pool has an unsupported version we can't open it.
3735 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
3737 spa_load_failed(spa
, "version %llu is not supported",
3738 (u_longlong_t
)ub
->ub_version
);
3739 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
3742 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3746 * If we weren't able to find what's necessary for reading the
3747 * MOS in the label, return failure.
3749 if (label
== NULL
) {
3750 spa_load_failed(spa
, "label config unavailable");
3751 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3755 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
3758 spa_load_failed(spa
, "invalid label: '%s' missing",
3759 ZPOOL_CONFIG_FEATURES_FOR_READ
);
3760 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3765 * Update our in-core representation with the definitive values
3768 nvlist_free(spa
->spa_label_features
);
3769 spa
->spa_label_features
= fnvlist_dup(features
);
3775 * Look through entries in the label nvlist's features_for_read. If
3776 * there is a feature listed there which we don't understand then we
3777 * cannot open a pool.
3779 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3780 nvlist_t
*unsup_feat
;
3782 unsup_feat
= fnvlist_alloc();
3784 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
3786 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
3787 if (!zfeature_is_supported(nvpair_name(nvp
))) {
3788 fnvlist_add_string(unsup_feat
,
3789 nvpair_name(nvp
), "");
3793 if (!nvlist_empty(unsup_feat
)) {
3794 fnvlist_add_nvlist(spa
->spa_load_info
,
3795 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3796 nvlist_free(unsup_feat
);
3797 spa_load_failed(spa
, "some features are unsupported");
3798 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3802 nvlist_free(unsup_feat
);
3805 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
3806 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3807 spa_try_repair(spa
, spa
->spa_config
);
3808 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3809 nvlist_free(spa
->spa_config_splitting
);
3810 spa
->spa_config_splitting
= NULL
;
3814 * Initialize internal SPA structures.
3816 spa_ld_select_uberblock_done(spa
, ub
);
3822 spa_ld_open_rootbp(spa_t
*spa
)
3825 vdev_t
*rvd
= spa
->spa_root_vdev
;
3827 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
3829 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
3830 "[error=%d]", error
);
3831 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3833 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
3839 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3840 boolean_t reloading
)
3842 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
3843 nvlist_t
*nv
, *mos_config
, *policy
;
3844 int error
= 0, copy_error
;
3845 uint64_t healthy_tvds
, healthy_tvds_mos
;
3846 uint64_t mos_config_txg
;
3848 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
3850 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3853 * If we're assembling a pool from a split, the config provided is
3854 * already trusted so there is nothing to do.
3856 if (type
== SPA_IMPORT_ASSEMBLE
)
3859 healthy_tvds
= spa_healthy_core_tvds(spa
);
3861 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
3863 spa_load_failed(spa
, "unable to retrieve MOS config");
3864 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3868 * If we are doing an open, pool owner wasn't verified yet, thus do
3869 * the verification here.
3871 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
3872 error
= spa_verify_host(spa
, mos_config
);
3874 nvlist_free(mos_config
);
3879 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
3881 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3884 * Build a new vdev tree from the trusted config
3886 error
= spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
);
3888 nvlist_free(mos_config
);
3889 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3890 spa_load_failed(spa
, "spa_config_parse failed [error=%d]",
3892 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3896 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3897 * obtained by scanning /dev/dsk, then it will have the right vdev
3898 * paths. We update the trusted MOS config with this information.
3899 * We first try to copy the paths with vdev_copy_path_strict, which
3900 * succeeds only when both configs have exactly the same vdev tree.
3901 * If that fails, we fall back to a more flexible method that has a
3902 * best effort policy.
3904 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3905 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3906 spa_load_note(spa
, "provided vdev tree:");
3907 vdev_dbgmsg_print_tree(rvd
, 2);
3908 spa_load_note(spa
, "MOS vdev tree:");
3909 vdev_dbgmsg_print_tree(mrvd
, 2);
3911 if (copy_error
!= 0) {
3912 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3913 "back to vdev_copy_path_relaxed");
3914 vdev_copy_path_relaxed(rvd
, mrvd
);
3919 spa
->spa_root_vdev
= mrvd
;
3921 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3924 * We will use spa_config if we decide to reload the spa or if spa_load
3925 * fails and we rewind. We must thus regenerate the config using the
3926 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3927 * pass settings on how to load the pool and is not stored in the MOS.
3928 * We copy it over to our new, trusted config.
3930 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3931 ZPOOL_CONFIG_POOL_TXG
);
3932 nvlist_free(mos_config
);
3933 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3934 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
3936 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
3937 spa_config_set(spa
, mos_config
);
3938 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3941 * Now that we got the config from the MOS, we should be more strict
3942 * in checking blkptrs and can make assumptions about the consistency
3943 * of the vdev tree. spa_trust_config must be set to true before opening
3944 * vdevs in order for them to be writeable.
3946 spa
->spa_trust_config
= B_TRUE
;
3949 * Open and validate the new vdev tree
3951 error
= spa_ld_open_vdevs(spa
);
3955 error
= spa_ld_validate_vdevs(spa
);
3959 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3960 spa_load_note(spa
, "final vdev tree:");
3961 vdev_dbgmsg_print_tree(rvd
, 2);
3964 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3965 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3967 * Sanity check to make sure that we are indeed loading the
3968 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3969 * in the config provided and they happened to be the only ones
3970 * to have the latest uberblock, we could involuntarily perform
3971 * an extreme rewind.
3973 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
3974 if (healthy_tvds_mos
- healthy_tvds
>=
3975 SPA_SYNC_MIN_VDEVS
) {
3976 spa_load_note(spa
, "config provided misses too many "
3977 "top-level vdevs compared to MOS (%lld vs %lld). ",
3978 (u_longlong_t
)healthy_tvds
,
3979 (u_longlong_t
)healthy_tvds_mos
);
3980 spa_load_note(spa
, "vdev tree:");
3981 vdev_dbgmsg_print_tree(rvd
, 2);
3983 spa_load_failed(spa
, "config was already "
3984 "provided from MOS. Aborting.");
3985 return (spa_vdev_err(rvd
,
3986 VDEV_AUX_CORRUPT_DATA
, EIO
));
3988 spa_load_note(spa
, "spa must be reloaded using MOS "
3990 return (SET_ERROR(EAGAIN
));
3994 error
= spa_check_for_missing_logs(spa
);
3996 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
3998 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
3999 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
4000 "guid sum (%llu != %llu)",
4001 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
4002 (u_longlong_t
)rvd
->vdev_guid_sum
);
4003 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
4011 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
4014 vdev_t
*rvd
= spa
->spa_root_vdev
;
4017 * Everything that we read before spa_remove_init() must be stored
4018 * on concreted vdevs. Therefore we do this as early as possible.
4020 error
= spa_remove_init(spa
);
4022 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
4024 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4028 * Retrieve information needed to condense indirect vdev mappings.
4030 error
= spa_condense_init(spa
);
4032 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
4034 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4041 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
4044 vdev_t
*rvd
= spa
->spa_root_vdev
;
4046 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
4047 boolean_t missing_feat_read
= B_FALSE
;
4048 nvlist_t
*unsup_feat
, *enabled_feat
;
4050 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
4051 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
4052 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4055 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
4056 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
4057 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4060 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
4061 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
4062 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4065 enabled_feat
= fnvlist_alloc();
4066 unsup_feat
= fnvlist_alloc();
4068 if (!spa_features_check(spa
, B_FALSE
,
4069 unsup_feat
, enabled_feat
))
4070 missing_feat_read
= B_TRUE
;
4072 if (spa_writeable(spa
) ||
4073 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
4074 if (!spa_features_check(spa
, B_TRUE
,
4075 unsup_feat
, enabled_feat
)) {
4076 *missing_feat_writep
= B_TRUE
;
4080 fnvlist_add_nvlist(spa
->spa_load_info
,
4081 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
4083 if (!nvlist_empty(unsup_feat
)) {
4084 fnvlist_add_nvlist(spa
->spa_load_info
,
4085 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4088 fnvlist_free(enabled_feat
);
4089 fnvlist_free(unsup_feat
);
4091 if (!missing_feat_read
) {
4092 fnvlist_add_boolean(spa
->spa_load_info
,
4093 ZPOOL_CONFIG_CAN_RDONLY
);
4097 * If the state is SPA_LOAD_TRYIMPORT, our objective is
4098 * twofold: to determine whether the pool is available for
4099 * import in read-write mode and (if it is not) whether the
4100 * pool is available for import in read-only mode. If the pool
4101 * is available for import in read-write mode, it is displayed
4102 * as available in userland; if it is not available for import
4103 * in read-only mode, it is displayed as unavailable in
4104 * userland. If the pool is available for import in read-only
4105 * mode but not read-write mode, it is displayed as unavailable
4106 * in userland with a special note that the pool is actually
4107 * available for open in read-only mode.
4109 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
4110 * missing a feature for write, we must first determine whether
4111 * the pool can be opened read-only before returning to
4112 * userland in order to know whether to display the
4113 * abovementioned note.
4115 if (missing_feat_read
|| (*missing_feat_writep
&&
4116 spa_writeable(spa
))) {
4117 spa_load_failed(spa
, "pool uses unsupported features");
4118 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4123 * Load refcounts for ZFS features from disk into an in-memory
4124 * cache during SPA initialization.
4126 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
4129 error
= feature_get_refcount_from_disk(spa
,
4130 &spa_feature_table
[i
], &refcount
);
4132 spa
->spa_feat_refcount_cache
[i
] = refcount
;
4133 } else if (error
== ENOTSUP
) {
4134 spa
->spa_feat_refcount_cache
[i
] =
4135 SPA_FEATURE_DISABLED
;
4137 spa_load_failed(spa
, "error getting refcount "
4138 "for feature %s [error=%d]",
4139 spa_feature_table
[i
].fi_guid
, error
);
4140 return (spa_vdev_err(rvd
,
4141 VDEV_AUX_CORRUPT_DATA
, EIO
));
4146 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
4147 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
4148 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
4149 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4153 * Encryption was added before bookmark_v2, even though bookmark_v2
4154 * is now a dependency. If this pool has encryption enabled without
4155 * bookmark_v2, trigger an errata message.
4157 if (spa_feature_is_enabled(spa
, SPA_FEATURE_ENCRYPTION
) &&
4158 !spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
4159 spa
->spa_errata
= ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
4166 spa_ld_load_special_directories(spa_t
*spa
)
4169 vdev_t
*rvd
= spa
->spa_root_vdev
;
4171 spa
->spa_is_initializing
= B_TRUE
;
4172 error
= dsl_pool_open(spa
->spa_dsl_pool
);
4173 spa
->spa_is_initializing
= B_FALSE
;
4175 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
4176 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4183 spa_ld_get_props(spa_t
*spa
)
4187 vdev_t
*rvd
= spa
->spa_root_vdev
;
4189 /* Grab the checksum salt from the MOS. */
4190 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4191 DMU_POOL_CHECKSUM_SALT
, 1,
4192 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
4193 spa
->spa_cksum_salt
.zcs_bytes
);
4194 if (error
== ENOENT
) {
4195 /* Generate a new salt for subsequent use */
4196 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4197 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4198 } else if (error
!= 0) {
4199 spa_load_failed(spa
, "unable to retrieve checksum salt from "
4200 "MOS [error=%d]", error
);
4201 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4204 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
4205 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4206 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
4208 spa_load_failed(spa
, "error opening deferred-frees bpobj "
4209 "[error=%d]", error
);
4210 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4214 * Load the bit that tells us to use the new accounting function
4215 * (raid-z deflation). If we have an older pool, this will not
4218 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
4219 if (error
!= 0 && error
!= ENOENT
)
4220 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4222 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
4223 &spa
->spa_creation_version
, B_FALSE
);
4224 if (error
!= 0 && error
!= ENOENT
)
4225 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4228 * Load the persistent error log. If we have an older pool, this will
4231 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
4233 if (error
!= 0 && error
!= ENOENT
)
4234 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4236 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
4237 &spa
->spa_errlog_scrub
, B_FALSE
);
4238 if (error
!= 0 && error
!= ENOENT
)
4239 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4242 * Load the livelist deletion field. If a livelist is queued for
4243 * deletion, indicate that in the spa
4245 error
= spa_dir_prop(spa
, DMU_POOL_DELETED_CLONES
,
4246 &spa
->spa_livelists_to_delete
, B_FALSE
);
4247 if (error
!= 0 && error
!= ENOENT
)
4248 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4251 * Load the history object. If we have an older pool, this
4252 * will not be present.
4254 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
4255 if (error
!= 0 && error
!= ENOENT
)
4256 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4259 * Load the per-vdev ZAP map. If we have an older pool, this will not
4260 * be present; in this case, defer its creation to a later time to
4261 * avoid dirtying the MOS this early / out of sync context. See
4262 * spa_sync_config_object.
4265 /* The sentinel is only available in the MOS config. */
4266 nvlist_t
*mos_config
;
4267 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
4268 spa_load_failed(spa
, "unable to retrieve MOS config");
4269 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4272 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
4273 &spa
->spa_all_vdev_zaps
, B_FALSE
);
4275 if (error
== ENOENT
) {
4276 VERIFY(!nvlist_exists(mos_config
,
4277 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
4278 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
4279 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4280 } else if (error
!= 0) {
4281 nvlist_free(mos_config
);
4282 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4283 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
4285 * An older version of ZFS overwrote the sentinel value, so
4286 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4287 * destruction to later; see spa_sync_config_object.
4289 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
4291 * We're assuming that no vdevs have had their ZAPs created
4292 * before this. Better be sure of it.
4294 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4296 nvlist_free(mos_config
);
4298 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4300 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
4302 if (error
&& error
!= ENOENT
)
4303 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4306 uint64_t autoreplace
= 0;
4308 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
4309 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
4310 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
4311 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
4312 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
4313 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
4314 spa_prop_find(spa
, ZPOOL_PROP_AUTOTRIM
, &spa
->spa_autotrim
);
4315 spa
->spa_autoreplace
= (autoreplace
!= 0);
4319 * If we are importing a pool with missing top-level vdevs,
4320 * we enforce that the pool doesn't panic or get suspended on
4321 * error since the likelihood of missing data is extremely high.
4323 if (spa
->spa_missing_tvds
> 0 &&
4324 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
4325 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4326 spa_load_note(spa
, "forcing failmode to 'continue' "
4327 "as some top level vdevs are missing");
4328 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
4335 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
4338 vdev_t
*rvd
= spa
->spa_root_vdev
;
4341 * If we're assembling the pool from the split-off vdevs of
4342 * an existing pool, we don't want to attach the spares & cache
4347 * Load any hot spares for this pool.
4349 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
4351 if (error
!= 0 && error
!= ENOENT
)
4352 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4353 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4354 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
4355 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
4356 &spa
->spa_spares
.sav_config
) != 0) {
4357 spa_load_failed(spa
, "error loading spares nvlist");
4358 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4361 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4362 spa_load_spares(spa
);
4363 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4364 } else if (error
== 0) {
4365 spa
->spa_spares
.sav_sync
= B_TRUE
;
4369 * Load any level 2 ARC devices for this pool.
4371 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
4372 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
4373 if (error
!= 0 && error
!= ENOENT
)
4374 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4375 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4376 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
4377 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
4378 &spa
->spa_l2cache
.sav_config
) != 0) {
4379 spa_load_failed(spa
, "error loading l2cache nvlist");
4380 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4383 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4384 spa_load_l2cache(spa
);
4385 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4386 } else if (error
== 0) {
4387 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4394 spa_ld_load_vdev_metadata(spa_t
*spa
)
4397 vdev_t
*rvd
= spa
->spa_root_vdev
;
4400 * If the 'multihost' property is set, then never allow a pool to
4401 * be imported when the system hostid is zero. The exception to
4402 * this rule is zdb which is always allowed to access pools.
4404 if (spa_multihost(spa
) && spa_get_hostid(spa
) == 0 &&
4405 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
4406 fnvlist_add_uint64(spa
->spa_load_info
,
4407 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4408 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4412 * If the 'autoreplace' property is set, then post a resource notifying
4413 * the ZFS DE that it should not issue any faults for unopenable
4414 * devices. We also iterate over the vdevs, and post a sysevent for any
4415 * unopenable vdevs so that the normal autoreplace handler can take
4418 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4419 spa_check_removed(spa
->spa_root_vdev
);
4421 * For the import case, this is done in spa_import(), because
4422 * at this point we're using the spare definitions from
4423 * the MOS config, not necessarily from the userland config.
4425 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
4426 spa_aux_check_removed(&spa
->spa_spares
);
4427 spa_aux_check_removed(&spa
->spa_l2cache
);
4432 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
4434 error
= vdev_load(rvd
);
4436 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
4437 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4440 error
= spa_ld_log_spacemaps(spa
);
4442 spa_load_failed(spa
, "spa_ld_log_spacemaps failed [error=%d]",
4444 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4448 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
4450 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4451 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
, B_FALSE
);
4452 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4458 spa_ld_load_dedup_tables(spa_t
*spa
)
4461 vdev_t
*rvd
= spa
->spa_root_vdev
;
4463 error
= ddt_load(spa
);
4465 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
4466 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4473 spa_ld_load_brt(spa_t
*spa
)
4476 vdev_t
*rvd
= spa
->spa_root_vdev
;
4478 error
= brt_load(spa
);
4480 spa_load_failed(spa
, "brt_load failed [error=%d]", error
);
4481 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4488 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4490 vdev_t
*rvd
= spa
->spa_root_vdev
;
4492 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
4493 boolean_t missing
= spa_check_logs(spa
);
4495 if (spa
->spa_missing_tvds
!= 0) {
4496 spa_load_note(spa
, "spa_check_logs failed "
4497 "so dropping the logs");
4499 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
4500 spa_load_failed(spa
, "spa_check_logs failed");
4501 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
4511 spa_ld_verify_pool_data(spa_t
*spa
)
4514 vdev_t
*rvd
= spa
->spa_root_vdev
;
4517 * We've successfully opened the pool, verify that we're ready
4518 * to start pushing transactions.
4520 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4521 error
= spa_load_verify(spa
);
4523 spa_load_failed(spa
, "spa_load_verify failed "
4524 "[error=%d]", error
);
4525 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4534 spa_ld_claim_log_blocks(spa_t
*spa
)
4537 dsl_pool_t
*dp
= spa_get_dsl(spa
);
4540 * Claim log blocks that haven't been committed yet.
4541 * This must all happen in a single txg.
4542 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
4543 * invoked from zil_claim_log_block()'s i/o done callback.
4544 * Price of rollback is that we abandon the log.
4546 spa
->spa_claiming
= B_TRUE
;
4548 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
4549 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
4550 zil_claim
, tx
, DS_FIND_CHILDREN
);
4553 spa
->spa_claiming
= B_FALSE
;
4555 spa_set_log_state(spa
, SPA_LOG_GOOD
);
4559 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
4560 boolean_t update_config_cache
)
4562 vdev_t
*rvd
= spa
->spa_root_vdev
;
4563 int need_update
= B_FALSE
;
4566 * If the config cache is stale, or we have uninitialized
4567 * metaslabs (see spa_vdev_add()), then update the config.
4569 * If this is a verbatim import, trust the current
4570 * in-core spa_config and update the disk labels.
4572 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
4573 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
4574 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4575 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
4576 need_update
= B_TRUE
;
4578 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
4579 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
4580 need_update
= B_TRUE
;
4583 * Update the config cache asynchronously in case we're the
4584 * root pool, in which case the config cache isn't writable yet.
4587 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
4591 spa_ld_prepare_for_reload(spa_t
*spa
)
4593 spa_mode_t mode
= spa
->spa_mode
;
4594 int async_suspended
= spa
->spa_async_suspended
;
4597 spa_deactivate(spa
);
4598 spa_activate(spa
, mode
);
4601 * We save the value of spa_async_suspended as it gets reset to 0 by
4602 * spa_unload(). We want to restore it back to the original value before
4603 * returning as we might be calling spa_async_resume() later.
4605 spa
->spa_async_suspended
= async_suspended
;
4609 spa_ld_read_checkpoint_txg(spa_t
*spa
)
4611 uberblock_t checkpoint
;
4614 ASSERT0(spa
->spa_checkpoint_txg
);
4615 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4617 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4618 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4619 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4621 if (error
== ENOENT
)
4627 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
4628 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
4629 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
4630 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
4631 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
4637 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
4641 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4642 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4645 * Never trust the config that is provided unless we are assembling
4646 * a pool following a split.
4647 * This means don't trust blkptrs and the vdev tree in general. This
4648 * also effectively puts the spa in read-only mode since
4649 * spa_writeable() checks for spa_trust_config to be true.
4650 * We will later load a trusted config from the MOS.
4652 if (type
!= SPA_IMPORT_ASSEMBLE
)
4653 spa
->spa_trust_config
= B_FALSE
;
4656 * Parse the config provided to create a vdev tree.
4658 error
= spa_ld_parse_config(spa
, type
);
4662 spa_import_progress_add(spa
);
4665 * Now that we have the vdev tree, try to open each vdev. This involves
4666 * opening the underlying physical device, retrieving its geometry and
4667 * probing the vdev with a dummy I/O. The state of each vdev will be set
4668 * based on the success of those operations. After this we'll be ready
4669 * to read from the vdevs.
4671 error
= spa_ld_open_vdevs(spa
);
4676 * Read the label of each vdev and make sure that the GUIDs stored
4677 * there match the GUIDs in the config provided.
4678 * If we're assembling a new pool that's been split off from an
4679 * existing pool, the labels haven't yet been updated so we skip
4680 * validation for now.
4682 if (type
!= SPA_IMPORT_ASSEMBLE
) {
4683 error
= spa_ld_validate_vdevs(spa
);
4689 * Read all vdev labels to find the best uberblock (i.e. latest,
4690 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
4691 * get the list of features required to read blkptrs in the MOS from
4692 * the vdev label with the best uberblock and verify that our version
4693 * of zfs supports them all.
4695 error
= spa_ld_select_uberblock(spa
, type
);
4700 * Pass that uberblock to the dsl_pool layer which will open the root
4701 * blkptr. This blkptr points to the latest version of the MOS and will
4702 * allow us to read its contents.
4704 error
= spa_ld_open_rootbp(spa
);
4712 spa_ld_checkpoint_rewind(spa_t
*spa
)
4714 uberblock_t checkpoint
;
4717 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4718 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4720 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4721 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4722 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4725 spa_load_failed(spa
, "unable to retrieve checkpointed "
4726 "uberblock from the MOS config [error=%d]", error
);
4728 if (error
== ENOENT
)
4729 error
= ZFS_ERR_NO_CHECKPOINT
;
4734 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
4735 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
4738 * We need to update the txg and timestamp of the checkpointed
4739 * uberblock to be higher than the latest one. This ensures that
4740 * the checkpointed uberblock is selected if we were to close and
4741 * reopen the pool right after we've written it in the vdev labels.
4742 * (also see block comment in vdev_uberblock_compare)
4744 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
4745 checkpoint
.ub_timestamp
= gethrestime_sec();
4748 * Set current uberblock to be the checkpointed uberblock.
4750 spa
->spa_uberblock
= checkpoint
;
4753 * If we are doing a normal rewind, then the pool is open for
4754 * writing and we sync the "updated" checkpointed uberblock to
4755 * disk. Once this is done, we've basically rewound the whole
4756 * pool and there is no way back.
4758 * There are cases when we don't want to attempt and sync the
4759 * checkpointed uberblock to disk because we are opening a
4760 * pool as read-only. Specifically, verifying the checkpointed
4761 * state with zdb, and importing the checkpointed state to get
4762 * a "preview" of its content.
4764 if (spa_writeable(spa
)) {
4765 vdev_t
*rvd
= spa
->spa_root_vdev
;
4767 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4768 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
4770 int children
= rvd
->vdev_children
;
4771 int c0
= random_in_range(children
);
4773 for (int c
= 0; c
< children
; c
++) {
4774 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
4776 /* Stop when revisiting the first vdev */
4777 if (c
> 0 && svd
[0] == vd
)
4780 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
4781 !vdev_is_concrete(vd
))
4784 svd
[svdcount
++] = vd
;
4785 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
4788 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
4790 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
4791 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4794 spa_load_failed(spa
, "failed to write checkpointed "
4795 "uberblock to the vdev labels [error=%d]", error
);
4804 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
4805 boolean_t
*update_config_cache
)
4810 * Parse the config for pool, open and validate vdevs,
4811 * select an uberblock, and use that uberblock to open
4814 error
= spa_ld_mos_init(spa
, type
);
4819 * Retrieve the trusted config stored in the MOS and use it to create
4820 * a new, exact version of the vdev tree, then reopen all vdevs.
4822 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
4823 if (error
== EAGAIN
) {
4824 if (update_config_cache
!= NULL
)
4825 *update_config_cache
= B_TRUE
;
4828 * Redo the loading process with the trusted config if it is
4829 * too different from the untrusted config.
4831 spa_ld_prepare_for_reload(spa
);
4832 spa_load_note(spa
, "RELOADING");
4833 error
= spa_ld_mos_init(spa
, type
);
4837 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
4841 } else if (error
!= 0) {
4849 * Load an existing storage pool, using the config provided. This config
4850 * describes which vdevs are part of the pool and is later validated against
4851 * partial configs present in each vdev's label and an entire copy of the
4852 * config stored in the MOS.
4855 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4858 boolean_t missing_feat_write
= B_FALSE
;
4859 boolean_t checkpoint_rewind
=
4860 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4861 boolean_t update_config_cache
= B_FALSE
;
4863 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4864 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4866 spa_load_note(spa
, "LOADING");
4868 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
4873 * If we are rewinding to the checkpoint then we need to repeat
4874 * everything we've done so far in this function but this time
4875 * selecting the checkpointed uberblock and using that to open
4878 if (checkpoint_rewind
) {
4880 * If we are rewinding to the checkpoint update config cache
4883 update_config_cache
= B_TRUE
;
4886 * Extract the checkpointed uberblock from the current MOS
4887 * and use this as the pool's uberblock from now on. If the
4888 * pool is imported as writeable we also write the checkpoint
4889 * uberblock to the labels, making the rewind permanent.
4891 error
= spa_ld_checkpoint_rewind(spa
);
4896 * Redo the loading process again with the
4897 * checkpointed uberblock.
4899 spa_ld_prepare_for_reload(spa
);
4900 spa_load_note(spa
, "LOADING checkpointed uberblock");
4901 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
4907 * Retrieve the checkpoint txg if the pool has a checkpoint.
4909 error
= spa_ld_read_checkpoint_txg(spa
);
4914 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
4915 * from the pool and their contents were re-mapped to other vdevs. Note
4916 * that everything that we read before this step must have been
4917 * rewritten on concrete vdevs after the last device removal was
4918 * initiated. Otherwise we could be reading from indirect vdevs before
4919 * we have loaded their mappings.
4921 error
= spa_ld_open_indirect_vdev_metadata(spa
);
4926 * Retrieve the full list of active features from the MOS and check if
4927 * they are all supported.
4929 error
= spa_ld_check_features(spa
, &missing_feat_write
);
4934 * Load several special directories from the MOS needed by the dsl_pool
4937 error
= spa_ld_load_special_directories(spa
);
4942 * Retrieve pool properties from the MOS.
4944 error
= spa_ld_get_props(spa
);
4949 * Retrieve the list of auxiliary devices - cache devices and spares -
4952 error
= spa_ld_open_aux_vdevs(spa
, type
);
4957 * Load the metadata for all vdevs. Also check if unopenable devices
4958 * should be autoreplaced.
4960 error
= spa_ld_load_vdev_metadata(spa
);
4964 error
= spa_ld_load_dedup_tables(spa
);
4968 error
= spa_ld_load_brt(spa
);
4973 * Verify the logs now to make sure we don't have any unexpected errors
4974 * when we claim log blocks later.
4976 error
= spa_ld_verify_logs(spa
, type
, ereport
);
4980 if (missing_feat_write
) {
4981 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
4984 * At this point, we know that we can open the pool in
4985 * read-only mode but not read-write mode. We now have enough
4986 * information and can return to userland.
4988 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
4993 * Traverse the last txgs to make sure the pool was left off in a safe
4994 * state. When performing an extreme rewind, we verify the whole pool,
4995 * which can take a very long time.
4997 error
= spa_ld_verify_pool_data(spa
);
5002 * Calculate the deflated space for the pool. This must be done before
5003 * we write anything to the pool because we'd need to update the space
5004 * accounting using the deflated sizes.
5006 spa_update_dspace(spa
);
5009 * We have now retrieved all the information we needed to open the
5010 * pool. If we are importing the pool in read-write mode, a few
5011 * additional steps must be performed to finish the import.
5013 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
5014 spa
->spa_load_max_txg
== UINT64_MAX
)) {
5015 uint64_t config_cache_txg
= spa
->spa_config_txg
;
5017 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
5020 * In case of a checkpoint rewind, log the original txg
5021 * of the checkpointed uberblock.
5023 if (checkpoint_rewind
) {
5024 spa_history_log_internal(spa
, "checkpoint rewind",
5025 NULL
, "rewound state to txg=%llu",
5026 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
5030 * Traverse the ZIL and claim all blocks.
5032 spa_ld_claim_log_blocks(spa
);
5035 * Kick-off the syncing thread.
5037 spa
->spa_sync_on
= B_TRUE
;
5038 txg_sync_start(spa
->spa_dsl_pool
);
5039 mmp_thread_start(spa
);
5042 * Wait for all claims to sync. We sync up to the highest
5043 * claimed log block birth time so that claimed log blocks
5044 * don't appear to be from the future. spa_claim_max_txg
5045 * will have been set for us by ZIL traversal operations
5048 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
5051 * Check if we need to request an update of the config. On the
5052 * next sync, we would update the config stored in vdev labels
5053 * and the cachefile (by default /etc/zfs/zpool.cache).
5055 spa_ld_check_for_config_update(spa
, config_cache_txg
,
5056 update_config_cache
);
5059 * Check if a rebuild was in progress and if so resume it.
5060 * Then check all DTLs to see if anything needs resilvering.
5061 * The resilver will be deferred if a rebuild was started.
5063 if (vdev_rebuild_active(spa
->spa_root_vdev
)) {
5064 vdev_rebuild_restart(spa
);
5065 } else if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
5066 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5067 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
5071 * Log the fact that we booted up (so that we can detect if
5072 * we rebooted in the middle of an operation).
5074 spa_history_log_version(spa
, "open", NULL
);
5076 spa_restart_removal(spa
);
5077 spa_spawn_aux_threads(spa
);
5080 * Delete any inconsistent datasets.
5083 * Since we may be issuing deletes for clones here,
5084 * we make sure to do so after we've spawned all the
5085 * auxiliary threads above (from which the livelist
5086 * deletion zthr is part of).
5088 (void) dmu_objset_find(spa_name(spa
),
5089 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
5092 * Clean up any stale temporary dataset userrefs.
5094 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
5096 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5097 vdev_initialize_restart(spa
->spa_root_vdev
);
5098 vdev_trim_restart(spa
->spa_root_vdev
);
5099 vdev_autotrim_restart(spa
);
5100 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5103 spa_import_progress_remove(spa_guid(spa
));
5104 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
5106 spa_load_note(spa
, "LOADED");
5112 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
5114 spa_mode_t mode
= spa
->spa_mode
;
5117 spa_deactivate(spa
);
5119 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
5121 spa_activate(spa
, mode
);
5122 spa_async_suspend(spa
);
5124 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
5125 (u_longlong_t
)spa
->spa_load_max_txg
);
5127 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
5131 * If spa_load() fails this function will try loading prior txg's. If
5132 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
5133 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
5134 * function will not rewind the pool and will return the same error as
5138 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
5141 nvlist_t
*loadinfo
= NULL
;
5142 nvlist_t
*config
= NULL
;
5143 int load_error
, rewind_error
;
5144 uint64_t safe_rewind_txg
;
5147 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
5148 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
5149 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5151 spa
->spa_load_max_txg
= max_request
;
5152 if (max_request
!= UINT64_MAX
)
5153 spa
->spa_extreme_rewind
= B_TRUE
;
5156 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
5157 if (load_error
== 0)
5159 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
5161 * When attempting checkpoint-rewind on a pool with no
5162 * checkpoint, we should not attempt to load uberblocks
5163 * from previous txgs when spa_load fails.
5165 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5166 spa_import_progress_remove(spa_guid(spa
));
5167 return (load_error
);
5170 if (spa
->spa_root_vdev
!= NULL
)
5171 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5173 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
5174 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
5176 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
5177 nvlist_free(config
);
5178 spa_import_progress_remove(spa_guid(spa
));
5179 return (load_error
);
5182 if (state
== SPA_LOAD_RECOVER
) {
5183 /* Price of rolling back is discarding txgs, including log */
5184 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5187 * If we aren't rolling back save the load info from our first
5188 * import attempt so that we can restore it after attempting
5191 loadinfo
= spa
->spa_load_info
;
5192 spa
->spa_load_info
= fnvlist_alloc();
5195 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
5196 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
5197 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
5198 TXG_INITIAL
: safe_rewind_txg
;
5201 * Continue as long as we're finding errors, we're still within
5202 * the acceptable rewind range, and we're still finding uberblocks
5204 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
5205 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
5206 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
5207 spa
->spa_extreme_rewind
= B_TRUE
;
5208 rewind_error
= spa_load_retry(spa
, state
);
5211 spa
->spa_extreme_rewind
= B_FALSE
;
5212 spa
->spa_load_max_txg
= UINT64_MAX
;
5214 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
5215 spa_config_set(spa
, config
);
5217 nvlist_free(config
);
5219 if (state
== SPA_LOAD_RECOVER
) {
5220 ASSERT3P(loadinfo
, ==, NULL
);
5221 spa_import_progress_remove(spa_guid(spa
));
5222 return (rewind_error
);
5224 /* Store the rewind info as part of the initial load info */
5225 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
5226 spa
->spa_load_info
);
5228 /* Restore the initial load info */
5229 fnvlist_free(spa
->spa_load_info
);
5230 spa
->spa_load_info
= loadinfo
;
5232 spa_import_progress_remove(spa_guid(spa
));
5233 return (load_error
);
5240 * The import case is identical to an open except that the configuration is sent
5241 * down from userland, instead of grabbed from the configuration cache. For the
5242 * case of an open, the pool configuration will exist in the
5243 * POOL_STATE_UNINITIALIZED state.
5245 * The stats information (gen/count/ustats) is used to gather vdev statistics at
5246 * the same time open the pool, without having to keep around the spa_t in some
5250 spa_open_common(const char *pool
, spa_t
**spapp
, const void *tag
,
5251 nvlist_t
*nvpolicy
, nvlist_t
**config
)
5254 spa_load_state_t state
= SPA_LOAD_OPEN
;
5256 int locked
= B_FALSE
;
5257 int firstopen
= B_FALSE
;
5262 * As disgusting as this is, we need to support recursive calls to this
5263 * function because dsl_dir_open() is called during spa_load(), and ends
5264 * up calling spa_open() again. The real fix is to figure out how to
5265 * avoid dsl_dir_open() calling this in the first place.
5267 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
5268 mutex_enter(&spa_namespace_lock
);
5272 if ((spa
= spa_lookup(pool
)) == NULL
) {
5274 mutex_exit(&spa_namespace_lock
);
5275 return (SET_ERROR(ENOENT
));
5278 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
5279 zpool_load_policy_t policy
;
5283 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
5285 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5286 state
= SPA_LOAD_RECOVER
;
5288 spa_activate(spa
, spa_mode_global
);
5290 if (state
!= SPA_LOAD_RECOVER
)
5291 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5292 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5294 zfs_dbgmsg("spa_open_common: opening %s", pool
);
5295 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
5298 if (error
== EBADF
) {
5300 * If vdev_validate() returns failure (indicated by
5301 * EBADF), it indicates that one of the vdevs indicates
5302 * that the pool has been exported or destroyed. If
5303 * this is the case, the config cache is out of sync and
5304 * we should remove the pool from the namespace.
5307 spa_deactivate(spa
);
5308 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
5311 mutex_exit(&spa_namespace_lock
);
5312 return (SET_ERROR(ENOENT
));
5317 * We can't open the pool, but we still have useful
5318 * information: the state of each vdev after the
5319 * attempted vdev_open(). Return this to the user.
5321 if (config
!= NULL
&& spa
->spa_config
) {
5322 *config
= fnvlist_dup(spa
->spa_config
);
5323 fnvlist_add_nvlist(*config
,
5324 ZPOOL_CONFIG_LOAD_INFO
,
5325 spa
->spa_load_info
);
5328 spa_deactivate(spa
);
5329 spa
->spa_last_open_failed
= error
;
5331 mutex_exit(&spa_namespace_lock
);
5337 spa_open_ref(spa
, tag
);
5340 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5343 * If we've recovered the pool, pass back any information we
5344 * gathered while doing the load.
5346 if (state
== SPA_LOAD_RECOVER
&& config
!= NULL
) {
5347 fnvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
5348 spa
->spa_load_info
);
5352 spa
->spa_last_open_failed
= 0;
5353 spa
->spa_last_ubsync_txg
= 0;
5354 spa
->spa_load_txg
= 0;
5355 mutex_exit(&spa_namespace_lock
);
5359 zvol_create_minors_recursive(spa_name(spa
));
5367 spa_open_rewind(const char *name
, spa_t
**spapp
, const void *tag
,
5368 nvlist_t
*policy
, nvlist_t
**config
)
5370 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
5374 spa_open(const char *name
, spa_t
**spapp
, const void *tag
)
5376 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
5380 * Lookup the given spa_t, incrementing the inject count in the process,
5381 * preventing it from being exported or destroyed.
5384 spa_inject_addref(char *name
)
5388 mutex_enter(&spa_namespace_lock
);
5389 if ((spa
= spa_lookup(name
)) == NULL
) {
5390 mutex_exit(&spa_namespace_lock
);
5393 spa
->spa_inject_ref
++;
5394 mutex_exit(&spa_namespace_lock
);
5400 spa_inject_delref(spa_t
*spa
)
5402 mutex_enter(&spa_namespace_lock
);
5403 spa
->spa_inject_ref
--;
5404 mutex_exit(&spa_namespace_lock
);
5408 * Add spares device information to the nvlist.
5411 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
5421 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5423 if (spa
->spa_spares
.sav_count
== 0)
5426 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5427 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5428 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
5430 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5431 (const nvlist_t
* const *)spares
, nspares
);
5432 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5433 &spares
, &nspares
));
5436 * Go through and find any spares which have since been
5437 * repurposed as an active spare. If this is the case, update
5438 * their status appropriately.
5440 for (i
= 0; i
< nspares
; i
++) {
5441 guid
= fnvlist_lookup_uint64(spares
[i
],
5443 VERIFY0(nvlist_lookup_uint64_array(spares
[i
],
5444 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5445 if (spa_spare_exists(guid
, &pool
, NULL
) &&
5447 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
5448 vs
->vs_aux
= VDEV_AUX_SPARED
;
5451 spa
->spa_spares
.sav_vdevs
[i
]->vdev_state
;
5458 * Add l2cache device information to the nvlist, including vdev stats.
5461 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
5464 uint_t i
, j
, nl2cache
;
5471 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5473 if (spa
->spa_l2cache
.sav_count
== 0)
5476 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5477 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5478 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
5479 if (nl2cache
!= 0) {
5480 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5481 (const nvlist_t
* const *)l2cache
, nl2cache
);
5482 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5483 &l2cache
, &nl2cache
));
5486 * Update level 2 cache device stats.
5489 for (i
= 0; i
< nl2cache
; i
++) {
5490 guid
= fnvlist_lookup_uint64(l2cache
[i
],
5494 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
5496 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
5497 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
5503 VERIFY0(nvlist_lookup_uint64_array(l2cache
[i
],
5504 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5505 vdev_get_stats(vd
, vs
);
5506 vdev_config_generate_stats(vd
, l2cache
[i
]);
5513 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
5518 if (spa
->spa_feat_for_read_obj
!= 0) {
5519 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5520 spa
->spa_feat_for_read_obj
);
5521 zap_cursor_retrieve(&zc
, &za
) == 0;
5522 zap_cursor_advance(&zc
)) {
5523 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5524 za
.za_num_integers
== 1);
5525 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5526 za
.za_first_integer
));
5528 zap_cursor_fini(&zc
);
5531 if (spa
->spa_feat_for_write_obj
!= 0) {
5532 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5533 spa
->spa_feat_for_write_obj
);
5534 zap_cursor_retrieve(&zc
, &za
) == 0;
5535 zap_cursor_advance(&zc
)) {
5536 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5537 za
.za_num_integers
== 1);
5538 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5539 za
.za_first_integer
));
5541 zap_cursor_fini(&zc
);
5546 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
5550 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5551 zfeature_info_t feature
= spa_feature_table
[i
];
5554 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
5557 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
5562 * Store a list of pool features and their reference counts in the
5565 * The first time this is called on a spa, allocate a new nvlist, fetch
5566 * the pool features and reference counts from disk, then save the list
5567 * in the spa. In subsequent calls on the same spa use the saved nvlist
5568 * and refresh its values from the cached reference counts. This
5569 * ensures we don't block here on I/O on a suspended pool so 'zpool
5570 * clear' can resume the pool.
5573 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
5577 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5579 mutex_enter(&spa
->spa_feat_stats_lock
);
5580 features
= spa
->spa_feat_stats
;
5582 if (features
!= NULL
) {
5583 spa_feature_stats_from_cache(spa
, features
);
5585 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
5586 spa
->spa_feat_stats
= features
;
5587 spa_feature_stats_from_disk(spa
, features
);
5590 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
5593 mutex_exit(&spa
->spa_feat_stats_lock
);
5597 spa_get_stats(const char *name
, nvlist_t
**config
,
5598 char *altroot
, size_t buflen
)
5604 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
5608 * This still leaves a window of inconsistency where the spares
5609 * or l2cache devices could change and the config would be
5610 * self-inconsistent.
5612 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5614 if (*config
!= NULL
) {
5615 uint64_t loadtimes
[2];
5617 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
5618 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
5619 fnvlist_add_uint64_array(*config
,
5620 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2);
5622 fnvlist_add_uint64(*config
,
5623 ZPOOL_CONFIG_ERRCOUNT
,
5624 spa_approx_errlog_size(spa
));
5626 if (spa_suspended(spa
)) {
5627 fnvlist_add_uint64(*config
,
5628 ZPOOL_CONFIG_SUSPENDED
,
5630 fnvlist_add_uint64(*config
,
5631 ZPOOL_CONFIG_SUSPENDED_REASON
,
5632 spa
->spa_suspended
);
5635 spa_add_spares(spa
, *config
);
5636 spa_add_l2cache(spa
, *config
);
5637 spa_add_feature_stats(spa
, *config
);
5642 * We want to get the alternate root even for faulted pools, so we cheat
5643 * and call spa_lookup() directly.
5647 mutex_enter(&spa_namespace_lock
);
5648 spa
= spa_lookup(name
);
5650 spa_altroot(spa
, altroot
, buflen
);
5654 mutex_exit(&spa_namespace_lock
);
5656 spa_altroot(spa
, altroot
, buflen
);
5661 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5662 spa_close(spa
, FTAG
);
5669 * Validate that the auxiliary device array is well formed. We must have an
5670 * array of nvlists, each which describes a valid leaf vdev. If this is an
5671 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
5672 * specified, as long as they are well-formed.
5675 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
5676 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
5677 vdev_labeltype_t label
)
5684 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5687 * It's acceptable to have no devs specified.
5689 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
5693 return (SET_ERROR(EINVAL
));
5696 * Make sure the pool is formatted with a version that supports this
5699 if (spa_version(spa
) < version
)
5700 return (SET_ERROR(ENOTSUP
));
5703 * Set the pending device list so we correctly handle device in-use
5706 sav
->sav_pending
= dev
;
5707 sav
->sav_npending
= ndev
;
5709 for (i
= 0; i
< ndev
; i
++) {
5710 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
5714 if (!vd
->vdev_ops
->vdev_op_leaf
) {
5716 error
= SET_ERROR(EINVAL
);
5722 if ((error
= vdev_open(vd
)) == 0 &&
5723 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
5724 fnvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
5731 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
5738 sav
->sav_pending
= NULL
;
5739 sav
->sav_npending
= 0;
5744 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
5748 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5750 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5751 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
5752 VDEV_LABEL_SPARE
)) != 0) {
5756 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5757 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
5758 VDEV_LABEL_L2CACHE
));
5762 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
5767 if (sav
->sav_config
!= NULL
) {
5773 * Generate new dev list by concatenating with the
5776 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
5777 &olddevs
, &oldndevs
));
5779 newdevs
= kmem_alloc(sizeof (void *) *
5780 (ndevs
+ oldndevs
), KM_SLEEP
);
5781 for (i
= 0; i
< oldndevs
; i
++)
5782 newdevs
[i
] = fnvlist_dup(olddevs
[i
]);
5783 for (i
= 0; i
< ndevs
; i
++)
5784 newdevs
[i
+ oldndevs
] = fnvlist_dup(devs
[i
]);
5786 fnvlist_remove(sav
->sav_config
, config
);
5788 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
5789 (const nvlist_t
* const *)newdevs
, ndevs
+ oldndevs
);
5790 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
5791 nvlist_free(newdevs
[i
]);
5792 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
5795 * Generate a new dev list.
5797 sav
->sav_config
= fnvlist_alloc();
5798 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
5799 (const nvlist_t
* const *)devs
, ndevs
);
5804 * Stop and drop level 2 ARC devices
5807 spa_l2cache_drop(spa_t
*spa
)
5811 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5813 for (i
= 0; i
< sav
->sav_count
; i
++) {
5816 vd
= sav
->sav_vdevs
[i
];
5819 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
5820 pool
!= 0ULL && l2arc_vdev_present(vd
))
5821 l2arc_remove_vdev(vd
);
5826 * Verify encryption parameters for spa creation. If we are encrypting, we must
5827 * have the encryption feature flag enabled.
5830 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
5831 boolean_t has_encryption
)
5833 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
5834 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
5836 return (SET_ERROR(ENOTSUP
));
5838 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
5845 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
5846 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
5849 const char *altroot
= NULL
;
5854 uint64_t txg
= TXG_INITIAL
;
5855 nvlist_t
**spares
, **l2cache
;
5856 uint_t nspares
, nl2cache
;
5857 uint64_t version
, obj
, ndraid
= 0;
5858 boolean_t has_features
;
5859 boolean_t has_encryption
;
5860 boolean_t has_allocclass
;
5862 const char *feat_name
;
5863 const char *poolname
;
5866 if (props
== NULL
||
5867 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
5868 poolname
= (char *)pool
;
5871 * If this pool already exists, return failure.
5873 mutex_enter(&spa_namespace_lock
);
5874 if (spa_lookup(poolname
) != NULL
) {
5875 mutex_exit(&spa_namespace_lock
);
5876 return (SET_ERROR(EEXIST
));
5880 * Allocate a new spa_t structure.
5882 nvl
= fnvlist_alloc();
5883 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
5884 (void) nvlist_lookup_string(props
,
5885 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5886 spa
= spa_add(poolname
, nvl
, altroot
);
5888 spa_activate(spa
, spa_mode_global
);
5890 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
5891 spa_deactivate(spa
);
5893 mutex_exit(&spa_namespace_lock
);
5898 * Temporary pool names should never be written to disk.
5900 if (poolname
!= pool
)
5901 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
5903 has_features
= B_FALSE
;
5904 has_encryption
= B_FALSE
;
5905 has_allocclass
= B_FALSE
;
5906 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
5907 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
5908 if (zpool_prop_feature(nvpair_name(elem
))) {
5909 has_features
= B_TRUE
;
5911 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
5912 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
5913 if (feat
== SPA_FEATURE_ENCRYPTION
)
5914 has_encryption
= B_TRUE
;
5915 if (feat
== SPA_FEATURE_ALLOCATION_CLASSES
)
5916 has_allocclass
= B_TRUE
;
5920 /* verify encryption params, if they were provided */
5922 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
5924 spa_deactivate(spa
);
5926 mutex_exit(&spa_namespace_lock
);
5930 if (!has_allocclass
&& zfs_special_devs(nvroot
, NULL
)) {
5931 spa_deactivate(spa
);
5933 mutex_exit(&spa_namespace_lock
);
5937 if (has_features
|| nvlist_lookup_uint64(props
,
5938 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
5939 version
= SPA_VERSION
;
5941 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5943 spa
->spa_first_txg
= txg
;
5944 spa
->spa_uberblock
.ub_txg
= txg
- 1;
5945 spa
->spa_uberblock
.ub_version
= version
;
5946 spa
->spa_ubsync
= spa
->spa_uberblock
;
5947 spa
->spa_load_state
= SPA_LOAD_CREATE
;
5948 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
5949 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
5950 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
5951 spa
->spa_indirect_vdevs_loaded
= B_TRUE
;
5954 * Create "The Godfather" zio to hold all async IOs
5956 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
5958 for (int i
= 0; i
< max_ncpus
; i
++) {
5959 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
5960 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
5961 ZIO_FLAG_GODFATHER
);
5965 * Create the root vdev.
5967 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5969 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
5971 ASSERT(error
!= 0 || rvd
!= NULL
);
5972 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
5974 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
5975 error
= SET_ERROR(EINVAL
);
5978 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
5979 (error
= vdev_draid_spare_create(nvroot
, rvd
, &ndraid
, 0)) == 0 &&
5980 (error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) == 0) {
5982 * instantiate the metaslab groups (this will dirty the vdevs)
5983 * we can no longer error exit past this point
5985 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
5986 vdev_t
*vd
= rvd
->vdev_child
[c
];
5988 vdev_metaslab_set_size(vd
);
5989 vdev_expand(vd
, txg
);
5993 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5997 spa_deactivate(spa
);
5999 mutex_exit(&spa_namespace_lock
);
6004 * Get the list of spares, if specified.
6006 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6007 &spares
, &nspares
) == 0) {
6008 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6009 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6010 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6012 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6013 spa_load_spares(spa
);
6014 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6015 spa
->spa_spares
.sav_sync
= B_TRUE
;
6019 * Get the list of level 2 cache devices, if specified.
6021 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6022 &l2cache
, &nl2cache
) == 0) {
6023 VERIFY0(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
6024 NV_UNIQUE_NAME
, KM_SLEEP
));
6025 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6026 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6028 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6029 spa_load_l2cache(spa
);
6030 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6031 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6034 spa
->spa_is_initializing
= B_TRUE
;
6035 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
6036 spa
->spa_is_initializing
= B_FALSE
;
6039 * Create DDTs (dedup tables).
6043 * Create BRT table and BRT table object.
6047 spa_update_dspace(spa
);
6049 tx
= dmu_tx_create_assigned(dp
, txg
);
6052 * Create the pool's history object.
6054 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
6055 spa_history_create_obj(spa
, tx
);
6057 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
6058 spa_history_log_version(spa
, "create", tx
);
6061 * Create the pool config object.
6063 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6064 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
6065 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
6067 if (zap_add(spa
->spa_meta_objset
,
6068 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
6069 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
6070 cmn_err(CE_PANIC
, "failed to add pool config");
6073 if (zap_add(spa
->spa_meta_objset
,
6074 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
6075 sizeof (uint64_t), 1, &version
, tx
) != 0) {
6076 cmn_err(CE_PANIC
, "failed to add pool version");
6079 /* Newly created pools with the right version are always deflated. */
6080 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6081 spa
->spa_deflate
= TRUE
;
6082 if (zap_add(spa
->spa_meta_objset
,
6083 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6084 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
6085 cmn_err(CE_PANIC
, "failed to add deflate");
6090 * Create the deferred-free bpobj. Turn off compression
6091 * because sync-to-convergence takes longer if the blocksize
6094 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
6095 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
6096 ZIO_COMPRESS_OFF
, tx
);
6097 if (zap_add(spa
->spa_meta_objset
,
6098 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
6099 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
6100 cmn_err(CE_PANIC
, "failed to add bpobj");
6102 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
6103 spa
->spa_meta_objset
, obj
));
6106 * Generate some random noise for salted checksums to operate on.
6108 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
6109 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
6112 * Set pool properties.
6114 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
6115 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
6116 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
6117 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
6118 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
6119 spa
->spa_autotrim
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM
);
6121 if (props
!= NULL
) {
6122 spa_configfile_set(spa
, props
, B_FALSE
);
6123 spa_sync_props(props
, tx
);
6126 for (int i
= 0; i
< ndraid
; i
++)
6127 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6131 spa
->spa_sync_on
= B_TRUE
;
6133 mmp_thread_start(spa
);
6134 txg_wait_synced(dp
, txg
);
6136 spa_spawn_aux_threads(spa
);
6138 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
6141 * Don't count references from objsets that are already closed
6142 * and are making their way through the eviction process.
6144 spa_evicting_os_wait(spa
);
6145 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
6146 spa
->spa_load_state
= SPA_LOAD_NONE
;
6150 mutex_exit(&spa_namespace_lock
);
6156 * Import a non-root pool into the system.
6159 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
6162 const char *altroot
= NULL
;
6163 spa_load_state_t state
= SPA_LOAD_IMPORT
;
6164 zpool_load_policy_t policy
;
6165 spa_mode_t mode
= spa_mode_global
;
6166 uint64_t readonly
= B_FALSE
;
6169 nvlist_t
**spares
, **l2cache
;
6170 uint_t nspares
, nl2cache
;
6173 * If a pool with this name exists, return failure.
6175 mutex_enter(&spa_namespace_lock
);
6176 if (spa_lookup(pool
) != NULL
) {
6177 mutex_exit(&spa_namespace_lock
);
6178 return (SET_ERROR(EEXIST
));
6182 * Create and initialize the spa structure.
6184 (void) nvlist_lookup_string(props
,
6185 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6186 (void) nvlist_lookup_uint64(props
,
6187 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
6189 mode
= SPA_MODE_READ
;
6190 spa
= spa_add(pool
, config
, altroot
);
6191 spa
->spa_import_flags
= flags
;
6194 * Verbatim import - Take a pool and insert it into the namespace
6195 * as if it had been loaded at boot.
6197 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
6199 spa_configfile_set(spa
, props
, B_FALSE
);
6201 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_FALSE
);
6202 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6203 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
6204 mutex_exit(&spa_namespace_lock
);
6208 spa_activate(spa
, mode
);
6211 * Don't start async tasks until we know everything is healthy.
6213 spa_async_suspend(spa
);
6215 zpool_get_load_policy(config
, &policy
);
6216 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
6217 state
= SPA_LOAD_RECOVER
;
6219 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
6221 if (state
!= SPA_LOAD_RECOVER
) {
6222 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
6223 zfs_dbgmsg("spa_import: importing %s", pool
);
6225 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
6226 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
6228 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
6231 * Propagate anything learned while loading the pool and pass it
6232 * back to caller (i.e. rewind info, missing devices, etc).
6234 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, spa
->spa_load_info
);
6236 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6238 * Toss any existing sparelist, as it doesn't have any validity
6239 * anymore, and conflicts with spa_has_spare().
6241 if (spa
->spa_spares
.sav_config
) {
6242 nvlist_free(spa
->spa_spares
.sav_config
);
6243 spa
->spa_spares
.sav_config
= NULL
;
6244 spa_load_spares(spa
);
6246 if (spa
->spa_l2cache
.sav_config
) {
6247 nvlist_free(spa
->spa_l2cache
.sav_config
);
6248 spa
->spa_l2cache
.sav_config
= NULL
;
6249 spa_load_l2cache(spa
);
6252 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6253 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6256 spa_configfile_set(spa
, props
, B_FALSE
);
6258 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
6259 (error
= spa_prop_set(spa
, props
)))) {
6261 spa_deactivate(spa
);
6263 mutex_exit(&spa_namespace_lock
);
6267 spa_async_resume(spa
);
6270 * Override any spares and level 2 cache devices as specified by
6271 * the user, as these may have correct device names/devids, etc.
6273 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6274 &spares
, &nspares
) == 0) {
6275 if (spa
->spa_spares
.sav_config
)
6276 fnvlist_remove(spa
->spa_spares
.sav_config
,
6277 ZPOOL_CONFIG_SPARES
);
6279 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6280 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6281 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6283 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6284 spa_load_spares(spa
);
6285 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6286 spa
->spa_spares
.sav_sync
= B_TRUE
;
6288 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6289 &l2cache
, &nl2cache
) == 0) {
6290 if (spa
->spa_l2cache
.sav_config
)
6291 fnvlist_remove(spa
->spa_l2cache
.sav_config
,
6292 ZPOOL_CONFIG_L2CACHE
);
6294 spa
->spa_l2cache
.sav_config
= fnvlist_alloc();
6295 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6296 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6298 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6299 spa_load_l2cache(spa
);
6300 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6301 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6305 * Check for any removed devices.
6307 if (spa
->spa_autoreplace
) {
6308 spa_aux_check_removed(&spa
->spa_spares
);
6309 spa_aux_check_removed(&spa
->spa_l2cache
);
6312 if (spa_writeable(spa
)) {
6314 * Update the config cache to include the newly-imported pool.
6316 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6320 * It's possible that the pool was expanded while it was exported.
6321 * We kick off an async task to handle this for us.
6323 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
6325 spa_history_log_version(spa
, "import", NULL
);
6327 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6329 mutex_exit(&spa_namespace_lock
);
6331 zvol_create_minors_recursive(pool
);
6339 spa_tryimport(nvlist_t
*tryconfig
)
6341 nvlist_t
*config
= NULL
;
6342 const char *poolname
, *cachefile
;
6346 zpool_load_policy_t policy
;
6348 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
6351 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
6355 * Create and initialize the spa structure.
6357 mutex_enter(&spa_namespace_lock
);
6358 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
6359 spa_activate(spa
, SPA_MODE_READ
);
6362 * Rewind pool if a max txg was provided.
6364 zpool_get_load_policy(spa
->spa_config
, &policy
);
6365 if (policy
.zlp_txg
!= UINT64_MAX
) {
6366 spa
->spa_load_max_txg
= policy
.zlp_txg
;
6367 spa
->spa_extreme_rewind
= B_TRUE
;
6368 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
6369 poolname
, (longlong_t
)policy
.zlp_txg
);
6371 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
6374 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
6376 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
6377 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
6379 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
6383 * spa_import() relies on a pool config fetched by spa_try_import()
6384 * for spare/cache devices. Import flags are not passed to
6385 * spa_tryimport(), which makes it return early due to a missing log
6386 * device and missing retrieving the cache device and spare eventually.
6387 * Passing ZFS_IMPORT_MISSING_LOG to spa_tryimport() makes it fetch
6388 * the correct configuration regardless of the missing log device.
6390 spa
->spa_import_flags
|= ZFS_IMPORT_MISSING_LOG
;
6392 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
6395 * If 'tryconfig' was at least parsable, return the current config.
6397 if (spa
->spa_root_vdev
!= NULL
) {
6398 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
6399 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, poolname
);
6400 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, state
);
6401 fnvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
6402 spa
->spa_uberblock
.ub_timestamp
);
6403 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
6404 spa
->spa_load_info
);
6405 fnvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
6409 * If the bootfs property exists on this pool then we
6410 * copy it out so that external consumers can tell which
6411 * pools are bootable.
6413 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
6414 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6417 * We have to play games with the name since the
6418 * pool was opened as TRYIMPORT_NAME.
6420 if (dsl_dsobj_to_dsname(spa_name(spa
),
6421 spa
->spa_bootfs
, tmpname
) == 0) {
6425 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6427 cp
= strchr(tmpname
, '/');
6429 (void) strlcpy(dsname
, tmpname
,
6432 (void) snprintf(dsname
, MAXPATHLEN
,
6433 "%s/%s", poolname
, ++cp
);
6435 fnvlist_add_string(config
, ZPOOL_CONFIG_BOOTFS
,
6437 kmem_free(dsname
, MAXPATHLEN
);
6439 kmem_free(tmpname
, MAXPATHLEN
);
6443 * Add the list of hot spares and level 2 cache devices.
6445 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6446 spa_add_spares(spa
, config
);
6447 spa_add_l2cache(spa
, config
);
6448 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6452 spa_deactivate(spa
);
6454 mutex_exit(&spa_namespace_lock
);
6460 * Pool export/destroy
6462 * The act of destroying or exporting a pool is very simple. We make sure there
6463 * is no more pending I/O and any references to the pool are gone. Then, we
6464 * update the pool state and sync all the labels to disk, removing the
6465 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
6466 * we don't sync the labels or remove the configuration cache.
6469 spa_export_common(const char *pool
, int new_state
, nvlist_t
**oldconfig
,
6470 boolean_t force
, boolean_t hardforce
)
6478 if (!(spa_mode_global
& SPA_MODE_WRITE
))
6479 return (SET_ERROR(EROFS
));
6481 mutex_enter(&spa_namespace_lock
);
6482 if ((spa
= spa_lookup(pool
)) == NULL
) {
6483 mutex_exit(&spa_namespace_lock
);
6484 return (SET_ERROR(ENOENT
));
6487 if (spa
->spa_is_exporting
) {
6488 /* the pool is being exported by another thread */
6489 mutex_exit(&spa_namespace_lock
);
6490 return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS
));
6492 spa
->spa_is_exporting
= B_TRUE
;
6495 * Put a hold on the pool, drop the namespace lock, stop async tasks,
6496 * reacquire the namespace lock, and see if we can export.
6498 spa_open_ref(spa
, FTAG
);
6499 mutex_exit(&spa_namespace_lock
);
6500 spa_async_suspend(spa
);
6501 if (spa
->spa_zvol_taskq
) {
6502 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
6503 taskq_wait(spa
->spa_zvol_taskq
);
6505 mutex_enter(&spa_namespace_lock
);
6506 spa_close(spa
, FTAG
);
6508 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
6511 * The pool will be in core if it's openable, in which case we can
6512 * modify its state. Objsets may be open only because they're dirty,
6513 * so we have to force it to sync before checking spa_refcnt.
6515 if (spa
->spa_sync_on
) {
6516 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6517 spa_evicting_os_wait(spa
);
6521 * A pool cannot be exported or destroyed if there are active
6522 * references. If we are resetting a pool, allow references by
6523 * fault injection handlers.
6525 if (!spa_refcount_zero(spa
) || (spa
->spa_inject_ref
!= 0)) {
6526 error
= SET_ERROR(EBUSY
);
6530 if (spa
->spa_sync_on
) {
6531 vdev_t
*rvd
= spa
->spa_root_vdev
;
6533 * A pool cannot be exported if it has an active shared spare.
6534 * This is to prevent other pools stealing the active spare
6535 * from an exported pool. At user's own will, such pool can
6536 * be forcedly exported.
6538 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
6539 spa_has_active_shared_spare(spa
)) {
6540 error
= SET_ERROR(EXDEV
);
6545 * We're about to export or destroy this pool. Make sure
6546 * we stop all initialization and trim activity here before
6547 * we set the spa_final_txg. This will ensure that all
6548 * dirty data resulting from the initialization is
6549 * committed to disk before we unload the pool.
6551 vdev_initialize_stop_all(rvd
, VDEV_INITIALIZE_ACTIVE
);
6552 vdev_trim_stop_all(rvd
, VDEV_TRIM_ACTIVE
);
6553 vdev_autotrim_stop_all(spa
);
6554 vdev_rebuild_stop_all(spa
);
6557 * We want this to be reflected on every label,
6558 * so mark them all dirty. spa_unload() will do the
6559 * final sync that pushes these changes out.
6561 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6562 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6563 spa
->spa_state
= new_state
;
6564 vdev_config_dirty(rvd
);
6565 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6569 * If the log space map feature is enabled and the pool is
6570 * getting exported (but not destroyed), we want to spend some
6571 * time flushing as many metaslabs as we can in an attempt to
6572 * destroy log space maps and save import time. This has to be
6573 * done before we set the spa_final_txg, otherwise
6574 * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs.
6575 * spa_should_flush_logs_on_unload() should be called after
6576 * spa_state has been set to the new_state.
6578 if (spa_should_flush_logs_on_unload(spa
))
6579 spa_unload_log_sm_flush_all(spa
);
6581 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6582 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6583 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
6585 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6592 if (new_state
== POOL_STATE_DESTROYED
)
6593 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
6594 else if (new_state
== POOL_STATE_EXPORTED
)
6595 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
6597 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6599 spa_deactivate(spa
);
6602 if (oldconfig
&& spa
->spa_config
)
6603 *oldconfig
= fnvlist_dup(spa
->spa_config
);
6605 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
6607 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
6611 * If spa_remove() is not called for this spa_t and
6612 * there is any possibility that it can be reused,
6613 * we make sure to reset the exporting flag.
6615 spa
->spa_is_exporting
= B_FALSE
;
6618 mutex_exit(&spa_namespace_lock
);
6622 spa
->spa_is_exporting
= B_FALSE
;
6623 spa_async_resume(spa
);
6624 mutex_exit(&spa_namespace_lock
);
6629 * Destroy a storage pool.
6632 spa_destroy(const char *pool
)
6634 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
6639 * Export a storage pool.
6642 spa_export(const char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
6643 boolean_t hardforce
)
6645 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
6650 * Similar to spa_export(), this unloads the spa_t without actually removing it
6651 * from the namespace in any way.
6654 spa_reset(const char *pool
)
6656 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
6661 * ==========================================================================
6662 * Device manipulation
6663 * ==========================================================================
6667 * This is called as a synctask to increment the draid feature flag
6670 spa_draid_feature_incr(void *arg
, dmu_tx_t
*tx
)
6672 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6673 int draid
= (int)(uintptr_t)arg
;
6675 for (int c
= 0; c
< draid
; c
++)
6676 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6680 * Add a device to a storage pool.
6683 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
6685 uint64_t txg
, ndraid
= 0;
6687 vdev_t
*rvd
= spa
->spa_root_vdev
;
6689 nvlist_t
**spares
, **l2cache
;
6690 uint_t nspares
, nl2cache
;
6692 ASSERT(spa_writeable(spa
));
6694 txg
= spa_vdev_enter(spa
);
6696 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
6697 VDEV_ALLOC_ADD
)) != 0)
6698 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6700 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
6702 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
6706 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
6710 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
6711 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6713 if (vd
->vdev_children
!= 0 &&
6714 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0) {
6715 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6719 * The virtual dRAID spares must be added after vdev tree is created
6720 * and the vdev guids are generated. The guid of their associated
6721 * dRAID is stored in the config and used when opening the spare.
6723 if ((error
= vdev_draid_spare_create(nvroot
, vd
, &ndraid
,
6724 rvd
->vdev_children
)) == 0) {
6725 if (ndraid
> 0 && nvlist_lookup_nvlist_array(nvroot
,
6726 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) != 0)
6729 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6733 * We must validate the spares and l2cache devices after checking the
6734 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
6736 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
6737 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6740 * If we are in the middle of a device removal, we can only add
6741 * devices which match the existing devices in the pool.
6742 * If we are in the middle of a removal, or have some indirect
6743 * vdevs, we can not add raidz or dRAID top levels.
6745 if (spa
->spa_vdev_removal
!= NULL
||
6746 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
6747 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6748 tvd
= vd
->vdev_child
[c
];
6749 if (spa
->spa_vdev_removal
!= NULL
&&
6750 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
6751 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6753 /* Fail if top level vdev is raidz or a dRAID */
6754 if (vdev_get_nparity(tvd
) != 0)
6755 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6758 * Need the top level mirror to be
6759 * a mirror of leaf vdevs only
6761 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
6762 for (uint64_t cid
= 0;
6763 cid
< tvd
->vdev_children
; cid
++) {
6764 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
6765 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
6766 return (spa_vdev_exit(spa
, vd
,
6774 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6775 tvd
= vd
->vdev_child
[c
];
6776 vdev_remove_child(vd
, tvd
);
6777 tvd
->vdev_id
= rvd
->vdev_children
;
6778 vdev_add_child(rvd
, tvd
);
6779 vdev_config_dirty(tvd
);
6783 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
6784 ZPOOL_CONFIG_SPARES
);
6785 spa_load_spares(spa
);
6786 spa
->spa_spares
.sav_sync
= B_TRUE
;
6789 if (nl2cache
!= 0) {
6790 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
6791 ZPOOL_CONFIG_L2CACHE
);
6792 spa_load_l2cache(spa
);
6793 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6797 * We can't increment a feature while holding spa_vdev so we
6798 * have to do it in a synctask.
6803 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
6804 dsl_sync_task_nowait(spa
->spa_dsl_pool
, spa_draid_feature_incr
,
6805 (void *)(uintptr_t)ndraid
, tx
);
6810 * We have to be careful when adding new vdevs to an existing pool.
6811 * If other threads start allocating from these vdevs before we
6812 * sync the config cache, and we lose power, then upon reboot we may
6813 * fail to open the pool because there are DVAs that the config cache
6814 * can't translate. Therefore, we first add the vdevs without
6815 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
6816 * and then let spa_config_update() initialize the new metaslabs.
6818 * spa_load() checks for added-but-not-initialized vdevs, so that
6819 * if we lose power at any point in this sequence, the remaining
6820 * steps will be completed the next time we load the pool.
6822 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
6824 mutex_enter(&spa_namespace_lock
);
6825 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6826 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
6827 mutex_exit(&spa_namespace_lock
);
6833 * Attach a device to a mirror. The arguments are the path to any device
6834 * in the mirror, and the nvroot for the new device. If the path specifies
6835 * a device that is not mirrored, we automatically insert the mirror vdev.
6837 * If 'replacing' is specified, the new device is intended to replace the
6838 * existing device; in this case the two devices are made into their own
6839 * mirror using the 'replacing' vdev, which is functionally identical to
6840 * the mirror vdev (it actually reuses all the same ops) but has a few
6841 * extra rules: you can't attach to it after it's been created, and upon
6842 * completion of resilvering, the first disk (the one being replaced)
6843 * is automatically detached.
6845 * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
6846 * should be performed instead of traditional healing reconstruction. From
6847 * an administrators perspective these are both resilver operations.
6850 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
,
6853 uint64_t txg
, dtl_max_txg
;
6854 vdev_t
*rvd
= spa
->spa_root_vdev
;
6855 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
6857 char *oldvdpath
, *newvdpath
;
6861 ASSERT(spa_writeable(spa
));
6863 txg
= spa_vdev_enter(spa
);
6865 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6867 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6868 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6869 error
= (spa_has_checkpoint(spa
)) ?
6870 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6871 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6875 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_DEVICE_REBUILD
))
6876 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6878 if (dsl_scan_resilvering(spa_get_dsl(spa
)) ||
6879 dsl_scan_resilver_scheduled(spa_get_dsl(spa
))) {
6880 return (spa_vdev_exit(spa
, NULL
, txg
,
6881 ZFS_ERR_RESILVER_IN_PROGRESS
));
6884 if (vdev_rebuild_active(rvd
))
6885 return (spa_vdev_exit(spa
, NULL
, txg
,
6886 ZFS_ERR_REBUILD_IN_PROGRESS
));
6889 if (spa
->spa_vdev_removal
!= NULL
)
6890 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6893 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6895 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
6896 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6898 pvd
= oldvd
->vdev_parent
;
6900 if (spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
6901 VDEV_ALLOC_ATTACH
) != 0)
6902 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6904 if (newrootvd
->vdev_children
!= 1)
6905 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6907 newvd
= newrootvd
->vdev_child
[0];
6909 if (!newvd
->vdev_ops
->vdev_op_leaf
)
6910 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6912 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
6913 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
6916 * log, dedup and special vdevs should not be replaced by spares.
6918 if ((oldvd
->vdev_top
->vdev_alloc_bias
!= VDEV_BIAS_NONE
||
6919 oldvd
->vdev_top
->vdev_islog
) && newvd
->vdev_isspare
) {
6920 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6924 * A dRAID spare can only replace a child of its parent dRAID vdev.
6926 if (newvd
->vdev_ops
== &vdev_draid_spare_ops
&&
6927 oldvd
->vdev_top
!= vdev_draid_spare_get_parent(newvd
)) {
6928 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6933 * For rebuilds, the top vdev must support reconstruction
6934 * using only space maps. This means the only allowable
6935 * vdevs types are the root vdev, a mirror, or dRAID.
6938 if (pvd
->vdev_top
!= NULL
)
6939 tvd
= pvd
->vdev_top
;
6941 if (tvd
->vdev_ops
!= &vdev_mirror_ops
&&
6942 tvd
->vdev_ops
!= &vdev_root_ops
&&
6943 tvd
->vdev_ops
!= &vdev_draid_ops
) {
6944 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6950 * For attach, the only allowable parent is a mirror or the root
6953 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6954 pvd
->vdev_ops
!= &vdev_root_ops
)
6955 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6957 pvops
= &vdev_mirror_ops
;
6960 * Active hot spares can only be replaced by inactive hot
6963 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6964 oldvd
->vdev_isspare
&&
6965 !spa_has_spare(spa
, newvd
->vdev_guid
))
6966 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6969 * If the source is a hot spare, and the parent isn't already a
6970 * spare, then we want to create a new hot spare. Otherwise, we
6971 * want to create a replacing vdev. The user is not allowed to
6972 * attach to a spared vdev child unless the 'isspare' state is
6973 * the same (spare replaces spare, non-spare replaces
6976 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
6977 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
6978 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6979 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6980 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
6981 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6984 if (newvd
->vdev_isspare
)
6985 pvops
= &vdev_spare_ops
;
6987 pvops
= &vdev_replacing_ops
;
6991 * Make sure the new device is big enough.
6993 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
6994 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
6997 * The new device cannot have a higher alignment requirement
6998 * than the top-level vdev.
7000 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
7001 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7004 * If this is an in-place replacement, update oldvd's path and devid
7005 * to make it distinguishable from newvd, and unopenable from now on.
7007 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
7008 spa_strfree(oldvd
->vdev_path
);
7009 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
7011 (void) snprintf(oldvd
->vdev_path
, strlen(newvd
->vdev_path
) + 5,
7012 "%s/%s", newvd
->vdev_path
, "old");
7013 if (oldvd
->vdev_devid
!= NULL
) {
7014 spa_strfree(oldvd
->vdev_devid
);
7015 oldvd
->vdev_devid
= NULL
;
7020 * If the parent is not a mirror, or if we're replacing, insert the new
7021 * mirror/replacing/spare vdev above oldvd.
7023 if (pvd
->vdev_ops
!= pvops
)
7024 pvd
= vdev_add_parent(oldvd
, pvops
);
7026 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
7027 ASSERT(pvd
->vdev_ops
== pvops
);
7028 ASSERT(oldvd
->vdev_parent
== pvd
);
7031 * Extract the new device from its root and add it to pvd.
7033 vdev_remove_child(newrootvd
, newvd
);
7034 newvd
->vdev_id
= pvd
->vdev_children
;
7035 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
7036 vdev_add_child(pvd
, newvd
);
7039 * Reevaluate the parent vdev state.
7041 vdev_propagate_state(pvd
);
7043 tvd
= newvd
->vdev_top
;
7044 ASSERT(pvd
->vdev_top
== tvd
);
7045 ASSERT(tvd
->vdev_parent
== rvd
);
7047 vdev_config_dirty(tvd
);
7050 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
7051 * for any dmu_sync-ed blocks. It will propagate upward when
7052 * spa_vdev_exit() calls vdev_dtl_reassess().
7054 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
7056 vdev_dtl_dirty(newvd
, DTL_MISSING
,
7057 TXG_INITIAL
, dtl_max_txg
- TXG_INITIAL
);
7059 if (newvd
->vdev_isspare
) {
7060 spa_spare_activate(newvd
);
7061 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
7064 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
7065 newvdpath
= spa_strdup(newvd
->vdev_path
);
7066 newvd_isspare
= newvd
->vdev_isspare
;
7069 * Mark newvd's DTL dirty in this txg.
7071 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
7074 * Schedule the resilver or rebuild to restart in the future. We do
7075 * this to ensure that dmu_sync-ed blocks have been stitched into the
7076 * respective datasets.
7079 newvd
->vdev_rebuild_txg
= txg
;
7083 newvd
->vdev_resilver_txg
= txg
;
7085 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
7086 spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
)) {
7087 vdev_defer_resilver(newvd
);
7089 dsl_scan_restart_resilver(spa
->spa_dsl_pool
,
7094 if (spa
->spa_bootfs
)
7095 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
7097 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
7102 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
7104 spa_history_log_internal(spa
, "vdev attach", NULL
,
7105 "%s vdev=%s %s vdev=%s",
7106 replacing
&& newvd_isspare
? "spare in" :
7107 replacing
? "replace" : "attach", newvdpath
,
7108 replacing
? "for" : "to", oldvdpath
);
7110 spa_strfree(oldvdpath
);
7111 spa_strfree(newvdpath
);
7117 * Detach a device from a mirror or replacing vdev.
7119 * If 'replace_done' is specified, only detach if the parent
7120 * is a replacing or a spare vdev.
7123 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
7127 vdev_t
*rvd __maybe_unused
= spa
->spa_root_vdev
;
7128 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
7129 boolean_t unspare
= B_FALSE
;
7130 uint64_t unspare_guid
= 0;
7133 ASSERT(spa_writeable(spa
));
7135 txg
= spa_vdev_detach_enter(spa
, guid
);
7137 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7140 * Besides being called directly from the userland through the
7141 * ioctl interface, spa_vdev_detach() can be potentially called
7142 * at the end of spa_vdev_resilver_done().
7144 * In the regular case, when we have a checkpoint this shouldn't
7145 * happen as we never empty the DTLs of a vdev during the scrub
7146 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
7147 * should never get here when we have a checkpoint.
7149 * That said, even in a case when we checkpoint the pool exactly
7150 * as spa_vdev_resilver_done() calls this function everything
7151 * should be fine as the resilver will return right away.
7153 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7154 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7155 error
= (spa_has_checkpoint(spa
)) ?
7156 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7157 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7161 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7163 if (!vd
->vdev_ops
->vdev_op_leaf
)
7164 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7166 pvd
= vd
->vdev_parent
;
7169 * If the parent/child relationship is not as expected, don't do it.
7170 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
7171 * vdev that's replacing B with C. The user's intent in replacing
7172 * is to go from M(A,B) to M(A,C). If the user decides to cancel
7173 * the replace by detaching C, the expected behavior is to end up
7174 * M(A,B). But suppose that right after deciding to detach C,
7175 * the replacement of B completes. We would have M(A,C), and then
7176 * ask to detach C, which would leave us with just A -- not what
7177 * the user wanted. To prevent this, we make sure that the
7178 * parent/child relationship hasn't changed -- in this example,
7179 * that C's parent is still the replacing vdev R.
7181 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
7182 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7185 * Only 'replacing' or 'spare' vdevs can be replaced.
7187 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7188 pvd
->vdev_ops
!= &vdev_spare_ops
)
7189 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7191 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
7192 spa_version(spa
) >= SPA_VERSION_SPARES
);
7195 * Only mirror, replacing, and spare vdevs support detach.
7197 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7198 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7199 pvd
->vdev_ops
!= &vdev_spare_ops
)
7200 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7203 * If this device has the only valid copy of some data,
7204 * we cannot safely detach it.
7206 if (vdev_dtl_required(vd
))
7207 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7209 ASSERT(pvd
->vdev_children
>= 2);
7212 * If we are detaching the second disk from a replacing vdev, then
7213 * check to see if we changed the original vdev's path to have "/old"
7214 * at the end in spa_vdev_attach(). If so, undo that change now.
7216 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
7217 vd
->vdev_path
!= NULL
) {
7218 size_t len
= strlen(vd
->vdev_path
);
7220 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
7221 cvd
= pvd
->vdev_child
[c
];
7223 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
7226 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
7227 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
7228 spa_strfree(cvd
->vdev_path
);
7229 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
7236 * If we are detaching the original disk from a normal spare, then it
7237 * implies that the spare should become a real disk, and be removed
7238 * from the active spare list for the pool. dRAID spares on the
7239 * other hand are coupled to the pool and thus should never be removed
7240 * from the spares list.
7242 if (pvd
->vdev_ops
== &vdev_spare_ops
&& vd
->vdev_id
== 0) {
7243 vdev_t
*last_cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7245 if (last_cvd
->vdev_isspare
&&
7246 last_cvd
->vdev_ops
!= &vdev_draid_spare_ops
) {
7252 * Erase the disk labels so the disk can be used for other things.
7253 * This must be done after all other error cases are handled,
7254 * but before we disembowel vd (so we can still do I/O to it).
7255 * But if we can't do it, don't treat the error as fatal --
7256 * it may be that the unwritability of the disk is the reason
7257 * it's being detached!
7259 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
7262 * Remove vd from its parent and compact the parent's children.
7264 vdev_remove_child(pvd
, vd
);
7265 vdev_compact_children(pvd
);
7268 * Remember one of the remaining children so we can get tvd below.
7270 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7273 * If we need to remove the remaining child from the list of hot spares,
7274 * do it now, marking the vdev as no longer a spare in the process.
7275 * We must do this before vdev_remove_parent(), because that can
7276 * change the GUID if it creates a new toplevel GUID. For a similar
7277 * reason, we must remove the spare now, in the same txg as the detach;
7278 * otherwise someone could attach a new sibling, change the GUID, and
7279 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
7282 ASSERT(cvd
->vdev_isspare
);
7283 spa_spare_remove(cvd
);
7284 unspare_guid
= cvd
->vdev_guid
;
7285 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
7286 cvd
->vdev_unspare
= B_TRUE
;
7290 * If the parent mirror/replacing vdev only has one child,
7291 * the parent is no longer needed. Remove it from the tree.
7293 if (pvd
->vdev_children
== 1) {
7294 if (pvd
->vdev_ops
== &vdev_spare_ops
)
7295 cvd
->vdev_unspare
= B_FALSE
;
7296 vdev_remove_parent(cvd
);
7300 * We don't set tvd until now because the parent we just removed
7301 * may have been the previous top-level vdev.
7303 tvd
= cvd
->vdev_top
;
7304 ASSERT(tvd
->vdev_parent
== rvd
);
7307 * Reevaluate the parent vdev state.
7309 vdev_propagate_state(cvd
);
7312 * If the 'autoexpand' property is set on the pool then automatically
7313 * try to expand the size of the pool. For example if the device we
7314 * just detached was smaller than the others, it may be possible to
7315 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
7316 * first so that we can obtain the updated sizes of the leaf vdevs.
7318 if (spa
->spa_autoexpand
) {
7320 vdev_expand(tvd
, txg
);
7323 vdev_config_dirty(tvd
);
7326 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
7327 * vd->vdev_detached is set and free vd's DTL object in syncing context.
7328 * But first make sure we're not on any *other* txg's DTL list, to
7329 * prevent vd from being accessed after it's freed.
7331 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
7332 for (int t
= 0; t
< TXG_SIZE
; t
++)
7333 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
7334 vd
->vdev_detached
= B_TRUE
;
7335 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
7337 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
7338 spa_notify_waiters(spa
);
7340 /* hang on to the spa before we release the lock */
7341 spa_open_ref(spa
, FTAG
);
7343 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
7345 spa_history_log_internal(spa
, "detach", NULL
,
7347 spa_strfree(vdpath
);
7350 * If this was the removal of the original device in a hot spare vdev,
7351 * then we want to go through and remove the device from the hot spare
7352 * list of every other pool.
7355 spa_t
*altspa
= NULL
;
7357 mutex_enter(&spa_namespace_lock
);
7358 while ((altspa
= spa_next(altspa
)) != NULL
) {
7359 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
7363 spa_open_ref(altspa
, FTAG
);
7364 mutex_exit(&spa_namespace_lock
);
7365 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
7366 mutex_enter(&spa_namespace_lock
);
7367 spa_close(altspa
, FTAG
);
7369 mutex_exit(&spa_namespace_lock
);
7371 /* search the rest of the vdevs for spares to remove */
7372 spa_vdev_resilver_done(spa
);
7375 /* all done with the spa; OK to release */
7376 mutex_enter(&spa_namespace_lock
);
7377 spa_close(spa
, FTAG
);
7378 mutex_exit(&spa_namespace_lock
);
7384 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7387 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7389 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7391 /* Look up vdev and ensure it's a leaf. */
7392 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7393 if (vd
== NULL
|| vd
->vdev_detached
) {
7394 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7395 return (SET_ERROR(ENODEV
));
7396 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7397 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7398 return (SET_ERROR(EINVAL
));
7399 } else if (!vdev_writeable(vd
)) {
7400 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7401 return (SET_ERROR(EROFS
));
7403 mutex_enter(&vd
->vdev_initialize_lock
);
7404 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7407 * When we activate an initialize action we check to see
7408 * if the vdev_initialize_thread is NULL. We do this instead
7409 * of using the vdev_initialize_state since there might be
7410 * a previous initialization process which has completed but
7411 * the thread is not exited.
7413 if (cmd_type
== POOL_INITIALIZE_START
&&
7414 (vd
->vdev_initialize_thread
!= NULL
||
7415 vd
->vdev_top
->vdev_removing
)) {
7416 mutex_exit(&vd
->vdev_initialize_lock
);
7417 return (SET_ERROR(EBUSY
));
7418 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
7419 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
7420 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
7421 mutex_exit(&vd
->vdev_initialize_lock
);
7422 return (SET_ERROR(ESRCH
));
7423 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
7424 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
7425 mutex_exit(&vd
->vdev_initialize_lock
);
7426 return (SET_ERROR(ESRCH
));
7427 } else if (cmd_type
== POOL_INITIALIZE_UNINIT
&&
7428 vd
->vdev_initialize_thread
!= NULL
) {
7429 mutex_exit(&vd
->vdev_initialize_lock
);
7430 return (SET_ERROR(EBUSY
));
7434 case POOL_INITIALIZE_START
:
7435 vdev_initialize(vd
);
7437 case POOL_INITIALIZE_CANCEL
:
7438 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
7440 case POOL_INITIALIZE_SUSPEND
:
7441 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
7443 case POOL_INITIALIZE_UNINIT
:
7444 vdev_uninitialize(vd
);
7447 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7449 mutex_exit(&vd
->vdev_initialize_lock
);
7455 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
7456 nvlist_t
*vdev_errlist
)
7458 int total_errors
= 0;
7461 list_create(&vd_list
, sizeof (vdev_t
),
7462 offsetof(vdev_t
, vdev_initialize_node
));
7465 * We hold the namespace lock through the whole function
7466 * to prevent any changes to the pool while we're starting or
7467 * stopping initialization. The config and state locks are held so that
7468 * we can properly assess the vdev state before we commit to
7469 * the initializing operation.
7471 mutex_enter(&spa_namespace_lock
);
7473 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7474 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7475 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7477 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
7480 char guid_as_str
[MAXNAMELEN
];
7482 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7483 "%llu", (unsigned long long)vdev_guid
);
7484 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7489 /* Wait for all initialize threads to stop. */
7490 vdev_initialize_stop_wait(spa
, &vd_list
);
7492 /* Sync out the initializing state */
7493 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7494 mutex_exit(&spa_namespace_lock
);
7496 list_destroy(&vd_list
);
7498 return (total_errors
);
7502 spa_vdev_trim_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7503 uint64_t rate
, boolean_t partial
, boolean_t secure
, list_t
*vd_list
)
7505 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7507 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7509 /* Look up vdev and ensure it's a leaf. */
7510 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7511 if (vd
== NULL
|| vd
->vdev_detached
) {
7512 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7513 return (SET_ERROR(ENODEV
));
7514 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7515 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7516 return (SET_ERROR(EINVAL
));
7517 } else if (!vdev_writeable(vd
)) {
7518 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7519 return (SET_ERROR(EROFS
));
7520 } else if (!vd
->vdev_has_trim
) {
7521 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7522 return (SET_ERROR(EOPNOTSUPP
));
7523 } else if (secure
&& !vd
->vdev_has_securetrim
) {
7524 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7525 return (SET_ERROR(EOPNOTSUPP
));
7527 mutex_enter(&vd
->vdev_trim_lock
);
7528 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7531 * When we activate a TRIM action we check to see if the
7532 * vdev_trim_thread is NULL. We do this instead of using the
7533 * vdev_trim_state since there might be a previous TRIM process
7534 * which has completed but the thread is not exited.
7536 if (cmd_type
== POOL_TRIM_START
&&
7537 (vd
->vdev_trim_thread
!= NULL
|| vd
->vdev_top
->vdev_removing
)) {
7538 mutex_exit(&vd
->vdev_trim_lock
);
7539 return (SET_ERROR(EBUSY
));
7540 } else if (cmd_type
== POOL_TRIM_CANCEL
&&
7541 (vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
&&
7542 vd
->vdev_trim_state
!= VDEV_TRIM_SUSPENDED
)) {
7543 mutex_exit(&vd
->vdev_trim_lock
);
7544 return (SET_ERROR(ESRCH
));
7545 } else if (cmd_type
== POOL_TRIM_SUSPEND
&&
7546 vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
) {
7547 mutex_exit(&vd
->vdev_trim_lock
);
7548 return (SET_ERROR(ESRCH
));
7552 case POOL_TRIM_START
:
7553 vdev_trim(vd
, rate
, partial
, secure
);
7555 case POOL_TRIM_CANCEL
:
7556 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, vd_list
);
7558 case POOL_TRIM_SUSPEND
:
7559 vdev_trim_stop(vd
, VDEV_TRIM_SUSPENDED
, vd_list
);
7562 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7564 mutex_exit(&vd
->vdev_trim_lock
);
7570 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
7571 * TRIM threads for each child vdev. These threads pass over all of the free
7572 * space in the vdev's metaslabs and issues TRIM commands for that space.
7575 spa_vdev_trim(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
, uint64_t rate
,
7576 boolean_t partial
, boolean_t secure
, nvlist_t
*vdev_errlist
)
7578 int total_errors
= 0;
7581 list_create(&vd_list
, sizeof (vdev_t
),
7582 offsetof(vdev_t
, vdev_trim_node
));
7585 * We hold the namespace lock through the whole function
7586 * to prevent any changes to the pool while we're starting or
7587 * stopping TRIM. The config and state locks are held so that
7588 * we can properly assess the vdev state before we commit to
7589 * the TRIM operation.
7591 mutex_enter(&spa_namespace_lock
);
7593 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7594 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7595 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7597 int error
= spa_vdev_trim_impl(spa
, vdev_guid
, cmd_type
,
7598 rate
, partial
, secure
, &vd_list
);
7600 char guid_as_str
[MAXNAMELEN
];
7602 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7603 "%llu", (unsigned long long)vdev_guid
);
7604 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7609 /* Wait for all TRIM threads to stop. */
7610 vdev_trim_stop_wait(spa
, &vd_list
);
7612 /* Sync out the TRIM state */
7613 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7614 mutex_exit(&spa_namespace_lock
);
7616 list_destroy(&vd_list
);
7618 return (total_errors
);
7622 * Split a set of devices from their mirrors, and create a new pool from them.
7625 spa_vdev_split_mirror(spa_t
*spa
, const char *newname
, nvlist_t
*config
,
7626 nvlist_t
*props
, boolean_t exp
)
7629 uint64_t txg
, *glist
;
7631 uint_t c
, children
, lastlog
;
7632 nvlist_t
**child
, *nvl
, *tmp
;
7634 const char *altroot
= NULL
;
7635 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
7636 boolean_t activate_slog
;
7638 ASSERT(spa_writeable(spa
));
7640 txg
= spa_vdev_enter(spa
);
7642 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7643 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7644 error
= (spa_has_checkpoint(spa
)) ?
7645 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7646 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7649 /* clear the log and flush everything up to now */
7650 activate_slog
= spa_passivate_log(spa
);
7651 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7652 error
= spa_reset_logs(spa
);
7653 txg
= spa_vdev_config_enter(spa
);
7656 spa_activate_log(spa
);
7659 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7661 /* check new spa name before going any further */
7662 if (spa_lookup(newname
) != NULL
)
7663 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
7666 * scan through all the children to ensure they're all mirrors
7668 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
7669 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
7671 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7673 /* first, check to ensure we've got the right child count */
7674 rvd
= spa
->spa_root_vdev
;
7676 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
7677 vdev_t
*vd
= rvd
->vdev_child
[c
];
7679 /* don't count the holes & logs as children */
7680 if (vd
->vdev_islog
|| (vd
->vdev_ops
!= &vdev_indirect_ops
&&
7681 !vdev_is_concrete(vd
))) {
7689 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
7690 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7692 /* next, ensure no spare or cache devices are part of the split */
7693 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
7694 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
7695 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7697 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
7698 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
7700 /* then, loop over each vdev and validate it */
7701 for (c
= 0; c
< children
; c
++) {
7702 uint64_t is_hole
= 0;
7704 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
7708 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
7709 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
7712 error
= SET_ERROR(EINVAL
);
7717 /* deal with indirect vdevs */
7718 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ops
==
7722 /* which disk is going to be split? */
7723 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
7725 error
= SET_ERROR(EINVAL
);
7729 /* look it up in the spa */
7730 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
7731 if (vml
[c
] == NULL
) {
7732 error
= SET_ERROR(ENODEV
);
7736 /* make sure there's nothing stopping the split */
7737 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
7738 vml
[c
]->vdev_islog
||
7739 !vdev_is_concrete(vml
[c
]) ||
7740 vml
[c
]->vdev_isspare
||
7741 vml
[c
]->vdev_isl2cache
||
7742 !vdev_writeable(vml
[c
]) ||
7743 vml
[c
]->vdev_children
!= 0 ||
7744 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
7745 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
7746 error
= SET_ERROR(EINVAL
);
7750 if (vdev_dtl_required(vml
[c
]) ||
7751 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
7752 error
= SET_ERROR(EBUSY
);
7756 /* we need certain info from the top level */
7757 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
7758 vml
[c
]->vdev_top
->vdev_ms_array
);
7759 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
7760 vml
[c
]->vdev_top
->vdev_ms_shift
);
7761 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
7762 vml
[c
]->vdev_top
->vdev_asize
);
7763 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
7764 vml
[c
]->vdev_top
->vdev_ashift
);
7766 /* transfer per-vdev ZAPs */
7767 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
7768 VERIFY0(nvlist_add_uint64(child
[c
],
7769 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
7771 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
7772 VERIFY0(nvlist_add_uint64(child
[c
],
7773 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
7774 vml
[c
]->vdev_parent
->vdev_top_zap
));
7778 kmem_free(vml
, children
* sizeof (vdev_t
*));
7779 kmem_free(glist
, children
* sizeof (uint64_t));
7780 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7783 /* stop writers from using the disks */
7784 for (c
= 0; c
< children
; c
++) {
7786 vml
[c
]->vdev_offline
= B_TRUE
;
7788 vdev_reopen(spa
->spa_root_vdev
);
7791 * Temporarily record the splitting vdevs in the spa config. This
7792 * will disappear once the config is regenerated.
7794 nvl
= fnvlist_alloc();
7795 fnvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
, glist
, children
);
7796 kmem_free(glist
, children
* sizeof (uint64_t));
7798 mutex_enter(&spa
->spa_props_lock
);
7799 fnvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
, nvl
);
7800 mutex_exit(&spa
->spa_props_lock
);
7801 spa
->spa_config_splitting
= nvl
;
7802 vdev_config_dirty(spa
->spa_root_vdev
);
7804 /* configure and create the new pool */
7805 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
);
7806 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
7807 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
);
7808 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
, spa_version(spa
));
7809 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, spa
->spa_config_txg
);
7810 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
7811 spa_generate_guid(NULL
));
7812 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
7813 (void) nvlist_lookup_string(props
,
7814 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
7816 /* add the new pool to the namespace */
7817 newspa
= spa_add(newname
, config
, altroot
);
7818 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7819 newspa
->spa_config_txg
= spa
->spa_config_txg
;
7820 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
7822 /* release the spa config lock, retaining the namespace lock */
7823 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7825 if (zio_injection_enabled
)
7826 zio_handle_panic_injection(spa
, FTAG
, 1);
7828 spa_activate(newspa
, spa_mode_global
);
7829 spa_async_suspend(newspa
);
7832 * Temporarily stop the initializing and TRIM activity. We set the
7833 * state to ACTIVE so that we know to resume initializing or TRIM
7834 * once the split has completed.
7836 list_t vd_initialize_list
;
7837 list_create(&vd_initialize_list
, sizeof (vdev_t
),
7838 offsetof(vdev_t
, vdev_initialize_node
));
7840 list_t vd_trim_list
;
7841 list_create(&vd_trim_list
, sizeof (vdev_t
),
7842 offsetof(vdev_t
, vdev_trim_node
));
7844 for (c
= 0; c
< children
; c
++) {
7845 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7846 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
7847 vdev_initialize_stop(vml
[c
],
7848 VDEV_INITIALIZE_ACTIVE
, &vd_initialize_list
);
7849 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
7851 mutex_enter(&vml
[c
]->vdev_trim_lock
);
7852 vdev_trim_stop(vml
[c
], VDEV_TRIM_ACTIVE
, &vd_trim_list
);
7853 mutex_exit(&vml
[c
]->vdev_trim_lock
);
7857 vdev_initialize_stop_wait(spa
, &vd_initialize_list
);
7858 vdev_trim_stop_wait(spa
, &vd_trim_list
);
7860 list_destroy(&vd_initialize_list
);
7861 list_destroy(&vd_trim_list
);
7863 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
7864 newspa
->spa_is_splitting
= B_TRUE
;
7866 /* create the new pool from the disks of the original pool */
7867 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
7871 /* if that worked, generate a real config for the new pool */
7872 if (newspa
->spa_root_vdev
!= NULL
) {
7873 newspa
->spa_config_splitting
= fnvlist_alloc();
7874 fnvlist_add_uint64(newspa
->spa_config_splitting
,
7875 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
));
7876 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
7881 if (props
!= NULL
) {
7882 spa_configfile_set(newspa
, props
, B_FALSE
);
7883 error
= spa_prop_set(newspa
, props
);
7888 /* flush everything */
7889 txg
= spa_vdev_config_enter(newspa
);
7890 vdev_config_dirty(newspa
->spa_root_vdev
);
7891 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
7893 if (zio_injection_enabled
)
7894 zio_handle_panic_injection(spa
, FTAG
, 2);
7896 spa_async_resume(newspa
);
7898 /* finally, update the original pool's config */
7899 txg
= spa_vdev_config_enter(spa
);
7900 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
7901 error
= dmu_tx_assign(tx
, TXG_WAIT
);
7904 for (c
= 0; c
< children
; c
++) {
7905 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7906 vdev_t
*tvd
= vml
[c
]->vdev_top
;
7909 * Need to be sure the detachable VDEV is not
7910 * on any *other* txg's DTL list to prevent it
7911 * from being accessed after it's freed.
7913 for (int t
= 0; t
< TXG_SIZE
; t
++) {
7914 (void) txg_list_remove_this(
7915 &tvd
->vdev_dtl_list
, vml
[c
], t
);
7920 spa_history_log_internal(spa
, "detach", tx
,
7921 "vdev=%s", vml
[c
]->vdev_path
);
7926 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7927 vdev_config_dirty(spa
->spa_root_vdev
);
7928 spa
->spa_config_splitting
= NULL
;
7932 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
7934 if (zio_injection_enabled
)
7935 zio_handle_panic_injection(spa
, FTAG
, 3);
7937 /* split is complete; log a history record */
7938 spa_history_log_internal(newspa
, "split", NULL
,
7939 "from pool %s", spa_name(spa
));
7941 newspa
->spa_is_splitting
= B_FALSE
;
7942 kmem_free(vml
, children
* sizeof (vdev_t
*));
7944 /* if we're not going to mount the filesystems in userland, export */
7946 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
7953 spa_deactivate(newspa
);
7956 txg
= spa_vdev_config_enter(spa
);
7958 /* re-online all offlined disks */
7959 for (c
= 0; c
< children
; c
++) {
7961 vml
[c
]->vdev_offline
= B_FALSE
;
7964 /* restart initializing or trimming disks as necessary */
7965 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
7966 spa_async_request(spa
, SPA_ASYNC_TRIM_RESTART
);
7967 spa_async_request(spa
, SPA_ASYNC_AUTOTRIM_RESTART
);
7969 vdev_reopen(spa
->spa_root_vdev
);
7971 nvlist_free(spa
->spa_config_splitting
);
7972 spa
->spa_config_splitting
= NULL
;
7973 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
7975 kmem_free(vml
, children
* sizeof (vdev_t
*));
7980 * Find any device that's done replacing, or a vdev marked 'unspare' that's
7981 * currently spared, so we can detach it.
7984 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
7986 vdev_t
*newvd
, *oldvd
;
7988 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7989 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
7995 * Check for a completed replacement. We always consider the first
7996 * vdev in the list to be the oldest vdev, and the last one to be
7997 * the newest (see spa_vdev_attach() for how that works). In
7998 * the case where the newest vdev is faulted, we will not automatically
7999 * remove it after a resilver completes. This is OK as it will require
8000 * user intervention to determine which disk the admin wishes to keep.
8002 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
8003 ASSERT(vd
->vdev_children
> 1);
8005 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
8006 oldvd
= vd
->vdev_child
[0];
8008 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8009 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8010 !vdev_dtl_required(oldvd
))
8015 * Check for a completed resilver with the 'unspare' flag set.
8016 * Also potentially update faulted state.
8018 if (vd
->vdev_ops
== &vdev_spare_ops
) {
8019 vdev_t
*first
= vd
->vdev_child
[0];
8020 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
8022 if (last
->vdev_unspare
) {
8025 } else if (first
->vdev_unspare
) {
8032 if (oldvd
!= NULL
&&
8033 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8034 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8035 !vdev_dtl_required(oldvd
))
8038 vdev_propagate_state(vd
);
8041 * If there are more than two spares attached to a disk,
8042 * and those spares are not required, then we want to
8043 * attempt to free them up now so that they can be used
8044 * by other pools. Once we're back down to a single
8045 * disk+spare, we stop removing them.
8047 if (vd
->vdev_children
> 2) {
8048 newvd
= vd
->vdev_child
[1];
8050 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
8051 vdev_dtl_empty(last
, DTL_MISSING
) &&
8052 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
8053 !vdev_dtl_required(newvd
))
8062 spa_vdev_resilver_done(spa_t
*spa
)
8064 vdev_t
*vd
, *pvd
, *ppvd
;
8065 uint64_t guid
, sguid
, pguid
, ppguid
;
8067 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8069 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
8070 pvd
= vd
->vdev_parent
;
8071 ppvd
= pvd
->vdev_parent
;
8072 guid
= vd
->vdev_guid
;
8073 pguid
= pvd
->vdev_guid
;
8074 ppguid
= ppvd
->vdev_guid
;
8077 * If we have just finished replacing a hot spared device, then
8078 * we need to detach the parent's first child (the original hot
8081 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
8082 ppvd
->vdev_children
== 2) {
8083 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
8084 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
8086 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
8088 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8089 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
8091 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
8093 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8096 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8099 * If a detach was not performed above replace waiters will not have
8100 * been notified. In which case we must do so now.
8102 spa_notify_waiters(spa
);
8106 * Update the stored path or FRU for this vdev.
8109 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
8113 boolean_t sync
= B_FALSE
;
8115 ASSERT(spa_writeable(spa
));
8117 spa_vdev_state_enter(spa
, SCL_ALL
);
8119 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
8120 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
8122 if (!vd
->vdev_ops
->vdev_op_leaf
)
8123 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
8126 if (strcmp(value
, vd
->vdev_path
) != 0) {
8127 spa_strfree(vd
->vdev_path
);
8128 vd
->vdev_path
= spa_strdup(value
);
8132 if (vd
->vdev_fru
== NULL
) {
8133 vd
->vdev_fru
= spa_strdup(value
);
8135 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
8136 spa_strfree(vd
->vdev_fru
);
8137 vd
->vdev_fru
= spa_strdup(value
);
8142 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
8146 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
8148 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
8152 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
8154 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
8158 * ==========================================================================
8160 * ==========================================================================
8163 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
8165 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8167 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8168 return (SET_ERROR(EBUSY
));
8170 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
8174 spa_scan_stop(spa_t
*spa
)
8176 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8177 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8178 return (SET_ERROR(EBUSY
));
8180 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
8184 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
8186 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8188 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
8189 return (SET_ERROR(ENOTSUP
));
8191 if (func
== POOL_SCAN_RESILVER
&&
8192 !spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
8193 return (SET_ERROR(ENOTSUP
));
8196 * If a resilver was requested, but there is no DTL on a
8197 * writeable leaf device, we have nothing to do.
8199 if (func
== POOL_SCAN_RESILVER
&&
8200 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
8201 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
8205 if (func
== POOL_SCAN_ERRORSCRUB
&&
8206 !spa_feature_is_enabled(spa
, SPA_FEATURE_HEAD_ERRLOG
))
8207 return (SET_ERROR(ENOTSUP
));
8209 return (dsl_scan(spa
->spa_dsl_pool
, func
));
8213 * ==========================================================================
8214 * SPA async task processing
8215 * ==========================================================================
8219 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
8221 if (vd
->vdev_remove_wanted
) {
8222 vd
->vdev_remove_wanted
= B_FALSE
;
8223 vd
->vdev_delayed_close
= B_FALSE
;
8224 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
8227 * We want to clear the stats, but we don't want to do a full
8228 * vdev_clear() as that will cause us to throw away
8229 * degraded/faulted state as well as attempt to reopen the
8230 * device, all of which is a waste.
8232 vd
->vdev_stat
.vs_read_errors
= 0;
8233 vd
->vdev_stat
.vs_write_errors
= 0;
8234 vd
->vdev_stat
.vs_checksum_errors
= 0;
8236 vdev_state_dirty(vd
->vdev_top
);
8238 /* Tell userspace that the vdev is gone. */
8239 zfs_post_remove(spa
, vd
);
8242 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8243 spa_async_remove(spa
, vd
->vdev_child
[c
]);
8247 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
8249 if (vd
->vdev_probe_wanted
) {
8250 vd
->vdev_probe_wanted
= B_FALSE
;
8251 vdev_reopen(vd
); /* vdev_open() does the actual probe */
8254 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8255 spa_async_probe(spa
, vd
->vdev_child
[c
]);
8259 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
8261 if (!spa
->spa_autoexpand
)
8264 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8265 vdev_t
*cvd
= vd
->vdev_child
[c
];
8266 spa_async_autoexpand(spa
, cvd
);
8269 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
8272 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
8275 static __attribute__((noreturn
)) void
8276 spa_async_thread(void *arg
)
8278 spa_t
*spa
= (spa_t
*)arg
;
8279 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8282 ASSERT(spa
->spa_sync_on
);
8284 mutex_enter(&spa
->spa_async_lock
);
8285 tasks
= spa
->spa_async_tasks
;
8286 spa
->spa_async_tasks
= 0;
8287 mutex_exit(&spa
->spa_async_lock
);
8290 * See if the config needs to be updated.
8292 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
8293 uint64_t old_space
, new_space
;
8295 mutex_enter(&spa_namespace_lock
);
8296 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
8297 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
8298 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8299 old_space
+= metaslab_class_get_space(
8300 spa_embedded_log_class(spa
));
8302 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
8304 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
8305 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
8306 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8307 new_space
+= metaslab_class_get_space(
8308 spa_embedded_log_class(spa
));
8309 mutex_exit(&spa_namespace_lock
);
8312 * If the pool grew as a result of the config update,
8313 * then log an internal history event.
8315 if (new_space
!= old_space
) {
8316 spa_history_log_internal(spa
, "vdev online", NULL
,
8317 "pool '%s' size: %llu(+%llu)",
8318 spa_name(spa
), (u_longlong_t
)new_space
,
8319 (u_longlong_t
)(new_space
- old_space
));
8324 * See if any devices need to be marked REMOVED.
8326 if (tasks
& SPA_ASYNC_REMOVE
) {
8327 spa_vdev_state_enter(spa
, SCL_NONE
);
8328 spa_async_remove(spa
, spa
->spa_root_vdev
);
8329 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
8330 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
8331 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
8332 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
8333 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8336 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
8337 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8338 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
8339 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8343 * See if any devices need to be probed.
8345 if (tasks
& SPA_ASYNC_PROBE
) {
8346 spa_vdev_state_enter(spa
, SCL_NONE
);
8347 spa_async_probe(spa
, spa
->spa_root_vdev
);
8348 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8352 * If any devices are done replacing, detach them.
8354 if (tasks
& SPA_ASYNC_RESILVER_DONE
||
8355 tasks
& SPA_ASYNC_REBUILD_DONE
||
8356 tasks
& SPA_ASYNC_DETACH_SPARE
) {
8357 spa_vdev_resilver_done(spa
);
8361 * Kick off a resilver.
8363 if (tasks
& SPA_ASYNC_RESILVER
&&
8364 !vdev_rebuild_active(spa
->spa_root_vdev
) &&
8365 (!dsl_scan_resilvering(dp
) ||
8366 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
8367 dsl_scan_restart_resilver(dp
, 0);
8369 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
8370 mutex_enter(&spa_namespace_lock
);
8371 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8372 vdev_initialize_restart(spa
->spa_root_vdev
);
8373 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8374 mutex_exit(&spa_namespace_lock
);
8377 if (tasks
& SPA_ASYNC_TRIM_RESTART
) {
8378 mutex_enter(&spa_namespace_lock
);
8379 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8380 vdev_trim_restart(spa
->spa_root_vdev
);
8381 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8382 mutex_exit(&spa_namespace_lock
);
8385 if (tasks
& SPA_ASYNC_AUTOTRIM_RESTART
) {
8386 mutex_enter(&spa_namespace_lock
);
8387 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8388 vdev_autotrim_restart(spa
);
8389 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8390 mutex_exit(&spa_namespace_lock
);
8394 * Kick off L2 cache whole device TRIM.
8396 if (tasks
& SPA_ASYNC_L2CACHE_TRIM
) {
8397 mutex_enter(&spa_namespace_lock
);
8398 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8399 vdev_trim_l2arc(spa
);
8400 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8401 mutex_exit(&spa_namespace_lock
);
8405 * Kick off L2 cache rebuilding.
8407 if (tasks
& SPA_ASYNC_L2CACHE_REBUILD
) {
8408 mutex_enter(&spa_namespace_lock
);
8409 spa_config_enter(spa
, SCL_L2ARC
, FTAG
, RW_READER
);
8410 l2arc_spa_rebuild_start(spa
);
8411 spa_config_exit(spa
, SCL_L2ARC
, FTAG
);
8412 mutex_exit(&spa_namespace_lock
);
8416 * Let the world know that we're done.
8418 mutex_enter(&spa
->spa_async_lock
);
8419 spa
->spa_async_thread
= NULL
;
8420 cv_broadcast(&spa
->spa_async_cv
);
8421 mutex_exit(&spa
->spa_async_lock
);
8426 spa_async_suspend(spa_t
*spa
)
8428 mutex_enter(&spa
->spa_async_lock
);
8429 spa
->spa_async_suspended
++;
8430 while (spa
->spa_async_thread
!= NULL
)
8431 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
8432 mutex_exit(&spa
->spa_async_lock
);
8434 spa_vdev_remove_suspend(spa
);
8436 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8437 if (condense_thread
!= NULL
)
8438 zthr_cancel(condense_thread
);
8440 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8441 if (discard_thread
!= NULL
)
8442 zthr_cancel(discard_thread
);
8444 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8445 if (ll_delete_thread
!= NULL
)
8446 zthr_cancel(ll_delete_thread
);
8448 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8449 if (ll_condense_thread
!= NULL
)
8450 zthr_cancel(ll_condense_thread
);
8454 spa_async_resume(spa_t
*spa
)
8456 mutex_enter(&spa
->spa_async_lock
);
8457 ASSERT(spa
->spa_async_suspended
!= 0);
8458 spa
->spa_async_suspended
--;
8459 mutex_exit(&spa
->spa_async_lock
);
8460 spa_restart_removal(spa
);
8462 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8463 if (condense_thread
!= NULL
)
8464 zthr_resume(condense_thread
);
8466 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8467 if (discard_thread
!= NULL
)
8468 zthr_resume(discard_thread
);
8470 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8471 if (ll_delete_thread
!= NULL
)
8472 zthr_resume(ll_delete_thread
);
8474 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8475 if (ll_condense_thread
!= NULL
)
8476 zthr_resume(ll_condense_thread
);
8480 spa_async_tasks_pending(spa_t
*spa
)
8482 uint_t non_config_tasks
;
8484 boolean_t config_task_suspended
;
8486 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
8487 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
8488 if (spa
->spa_ccw_fail_time
== 0) {
8489 config_task_suspended
= B_FALSE
;
8491 config_task_suspended
=
8492 (gethrtime() - spa
->spa_ccw_fail_time
) <
8493 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
8496 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
8500 spa_async_dispatch(spa_t
*spa
)
8502 mutex_enter(&spa
->spa_async_lock
);
8503 if (spa_async_tasks_pending(spa
) &&
8504 !spa
->spa_async_suspended
&&
8505 spa
->spa_async_thread
== NULL
)
8506 spa
->spa_async_thread
= thread_create(NULL
, 0,
8507 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
8508 mutex_exit(&spa
->spa_async_lock
);
8512 spa_async_request(spa_t
*spa
, int task
)
8514 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
8515 mutex_enter(&spa
->spa_async_lock
);
8516 spa
->spa_async_tasks
|= task
;
8517 mutex_exit(&spa
->spa_async_lock
);
8521 spa_async_tasks(spa_t
*spa
)
8523 return (spa
->spa_async_tasks
);
8527 * ==========================================================================
8528 * SPA syncing routines
8529 * ==========================================================================
8534 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8538 bpobj_enqueue(bpo
, bp
, bp_freed
, tx
);
8543 bpobj_enqueue_alloc_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8545 return (bpobj_enqueue_cb(arg
, bp
, B_FALSE
, tx
));
8549 bpobj_enqueue_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8551 return (bpobj_enqueue_cb(arg
, bp
, B_TRUE
, tx
));
8555 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8559 zio_nowait(zio_free_sync(pio
, pio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
8565 bpobj_spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8569 return (spa_free_sync_cb(arg
, bp
, tx
));
8573 * Note: this simple function is not inlined to make it easier to dtrace the
8574 * amount of time spent syncing frees.
8577 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
8579 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8580 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
8581 VERIFY(zio_wait(zio
) == 0);
8585 * Note: this simple function is not inlined to make it easier to dtrace the
8586 * amount of time spent syncing deferred frees.
8589 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
8591 if (spa_sync_pass(spa
) != 1)
8596 * If the log space map feature is active, we stop deferring
8597 * frees to the next TXG and therefore running this function
8598 * would be considered a no-op as spa_deferred_bpobj should
8599 * not have any entries.
8601 * That said we run this function anyway (instead of returning
8602 * immediately) for the edge-case scenario where we just
8603 * activated the log space map feature in this TXG but we have
8604 * deferred frees from the previous TXG.
8606 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8607 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
8608 bpobj_spa_free_sync_cb
, zio
, tx
), ==, 0);
8609 VERIFY0(zio_wait(zio
));
8613 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
8615 char *packed
= NULL
;
8620 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
8623 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
8624 * information. This avoids the dmu_buf_will_dirty() path and
8625 * saves us a pre-read to get data we don't actually care about.
8627 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
8628 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
8630 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
8632 memset(packed
+ nvsize
, 0, bufsize
- nvsize
);
8634 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
8636 vmem_free(packed
, bufsize
);
8638 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
8639 dmu_buf_will_dirty(db
, tx
);
8640 *(uint64_t *)db
->db_data
= nvsize
;
8641 dmu_buf_rele(db
, FTAG
);
8645 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
8646 const char *config
, const char *entry
)
8656 * Update the MOS nvlist describing the list of available devices.
8657 * spa_validate_aux() will have already made sure this nvlist is
8658 * valid and the vdevs are labeled appropriately.
8660 if (sav
->sav_object
== 0) {
8661 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
8662 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
8663 sizeof (uint64_t), tx
);
8664 VERIFY(zap_update(spa
->spa_meta_objset
,
8665 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
8666 &sav
->sav_object
, tx
) == 0);
8669 nvroot
= fnvlist_alloc();
8670 if (sav
->sav_count
== 0) {
8671 fnvlist_add_nvlist_array(nvroot
, config
,
8672 (const nvlist_t
* const *)NULL
, 0);
8674 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
8675 for (i
= 0; i
< sav
->sav_count
; i
++)
8676 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
8677 B_FALSE
, VDEV_CONFIG_L2CACHE
);
8678 fnvlist_add_nvlist_array(nvroot
, config
,
8679 (const nvlist_t
* const *)list
, sav
->sav_count
);
8680 for (i
= 0; i
< sav
->sav_count
; i
++)
8681 nvlist_free(list
[i
]);
8682 kmem_free(list
, sav
->sav_count
* sizeof (void *));
8685 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
8686 nvlist_free(nvroot
);
8688 sav
->sav_sync
= B_FALSE
;
8692 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
8693 * The all-vdev ZAP must be empty.
8696 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
8698 spa_t
*spa
= vd
->vdev_spa
;
8700 if (vd
->vdev_root_zap
!= 0 &&
8701 spa_feature_is_active(spa
, SPA_FEATURE_AVZ_V2
)) {
8702 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8703 vd
->vdev_root_zap
, tx
));
8705 if (vd
->vdev_top_zap
!= 0) {
8706 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8707 vd
->vdev_top_zap
, tx
));
8709 if (vd
->vdev_leaf_zap
!= 0) {
8710 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8711 vd
->vdev_leaf_zap
, tx
));
8713 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
8714 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
8719 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
8724 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
8725 * its config may not be dirty but we still need to build per-vdev ZAPs.
8726 * Similarly, if the pool is being assembled (e.g. after a split), we
8727 * need to rebuild the AVZ although the config may not be dirty.
8729 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
8730 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
8733 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
8735 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
8736 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
8737 spa
->spa_all_vdev_zaps
!= 0);
8739 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
8740 /* Make and build the new AVZ */
8741 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
8742 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
8743 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
8745 /* Diff old AVZ with new one */
8749 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8750 spa
->spa_all_vdev_zaps
);
8751 zap_cursor_retrieve(&zc
, &za
) == 0;
8752 zap_cursor_advance(&zc
)) {
8753 uint64_t vdzap
= za
.za_first_integer
;
8754 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
8757 * ZAP is listed in old AVZ but not in new one;
8760 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
8765 zap_cursor_fini(&zc
);
8767 /* Destroy the old AVZ */
8768 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8769 spa
->spa_all_vdev_zaps
, tx
));
8771 /* Replace the old AVZ in the dir obj with the new one */
8772 VERIFY0(zap_update(spa
->spa_meta_objset
,
8773 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
8774 sizeof (new_avz
), 1, &new_avz
, tx
));
8776 spa
->spa_all_vdev_zaps
= new_avz
;
8777 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
8781 /* Walk through the AVZ and destroy all listed ZAPs */
8782 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8783 spa
->spa_all_vdev_zaps
);
8784 zap_cursor_retrieve(&zc
, &za
) == 0;
8785 zap_cursor_advance(&zc
)) {
8786 uint64_t zap
= za
.za_first_integer
;
8787 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
8790 zap_cursor_fini(&zc
);
8792 /* Destroy and unlink the AVZ itself */
8793 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8794 spa
->spa_all_vdev_zaps
, tx
));
8795 VERIFY0(zap_remove(spa
->spa_meta_objset
,
8796 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
8797 spa
->spa_all_vdev_zaps
= 0;
8800 if (spa
->spa_all_vdev_zaps
== 0) {
8801 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
8802 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
8803 DMU_POOL_VDEV_ZAP_MAP
, tx
);
8805 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
8807 /* Create ZAPs for vdevs that don't have them. */
8808 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
8810 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
8811 dmu_tx_get_txg(tx
), B_FALSE
);
8814 * If we're upgrading the spa version then make sure that
8815 * the config object gets updated with the correct version.
8817 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
8818 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
8819 spa
->spa_uberblock
.ub_version
);
8821 spa_config_exit(spa
, SCL_STATE
, FTAG
);
8823 nvlist_free(spa
->spa_config_syncing
);
8824 spa
->spa_config_syncing
= config
;
8826 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
8830 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
8832 uint64_t *versionp
= arg
;
8833 uint64_t version
= *versionp
;
8834 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8837 * Setting the version is special cased when first creating the pool.
8839 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
8841 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
8842 ASSERT(version
>= spa_version(spa
));
8844 spa
->spa_uberblock
.ub_version
= version
;
8845 vdev_config_dirty(spa
->spa_root_vdev
);
8846 spa_history_log_internal(spa
, "set", tx
, "version=%lld",
8847 (longlong_t
)version
);
8851 * Set zpool properties.
8854 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
8856 nvlist_t
*nvp
= arg
;
8857 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8858 objset_t
*mos
= spa
->spa_meta_objset
;
8859 nvpair_t
*elem
= NULL
;
8861 mutex_enter(&spa
->spa_props_lock
);
8863 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
8865 const char *strval
, *fname
;
8867 const char *propname
;
8868 const char *elemname
= nvpair_name(elem
);
8869 zprop_type_t proptype
;
8872 switch (prop
= zpool_name_to_prop(elemname
)) {
8873 case ZPOOL_PROP_VERSION
:
8874 intval
= fnvpair_value_uint64(elem
);
8876 * The version is synced separately before other
8877 * properties and should be correct by now.
8879 ASSERT3U(spa_version(spa
), >=, intval
);
8882 case ZPOOL_PROP_ALTROOT
:
8884 * 'altroot' is a non-persistent property. It should
8885 * have been set temporarily at creation or import time.
8887 ASSERT(spa
->spa_root
!= NULL
);
8890 case ZPOOL_PROP_READONLY
:
8891 case ZPOOL_PROP_CACHEFILE
:
8893 * 'readonly' and 'cachefile' are also non-persistent
8897 case ZPOOL_PROP_COMMENT
:
8898 strval
= fnvpair_value_string(elem
);
8899 if (spa
->spa_comment
!= NULL
)
8900 spa_strfree(spa
->spa_comment
);
8901 spa
->spa_comment
= spa_strdup(strval
);
8903 * We need to dirty the configuration on all the vdevs
8904 * so that their labels get updated. We also need to
8905 * update the cache file to keep it in sync with the
8906 * MOS version. It's unnecessary to do this for pool
8907 * creation since the vdev's configuration has already
8910 if (tx
->tx_txg
!= TXG_INITIAL
) {
8911 vdev_config_dirty(spa
->spa_root_vdev
);
8912 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
8914 spa_history_log_internal(spa
, "set", tx
,
8915 "%s=%s", elemname
, strval
);
8917 case ZPOOL_PROP_COMPATIBILITY
:
8918 strval
= fnvpair_value_string(elem
);
8919 if (spa
->spa_compatibility
!= NULL
)
8920 spa_strfree(spa
->spa_compatibility
);
8921 spa
->spa_compatibility
= spa_strdup(strval
);
8923 * Dirty the configuration on vdevs as above.
8925 if (tx
->tx_txg
!= TXG_INITIAL
) {
8926 vdev_config_dirty(spa
->spa_root_vdev
);
8927 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
8930 spa_history_log_internal(spa
, "set", tx
,
8931 "%s=%s", nvpair_name(elem
), strval
);
8934 case ZPOOL_PROP_INVAL
:
8935 if (zpool_prop_feature(elemname
)) {
8936 fname
= strchr(elemname
, '@') + 1;
8937 VERIFY0(zfeature_lookup_name(fname
, &fid
));
8939 spa_feature_enable(spa
, fid
, tx
);
8940 spa_history_log_internal(spa
, "set", tx
,
8941 "%s=enabled", elemname
);
8943 } else if (!zfs_prop_user(elemname
)) {
8944 ASSERT(zpool_prop_feature(elemname
));
8950 * Set pool property values in the poolprops mos object.
8952 if (spa
->spa_pool_props_object
== 0) {
8953 spa
->spa_pool_props_object
=
8954 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
8955 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
8959 /* normalize the property name */
8960 if (prop
== ZPOOL_PROP_INVAL
) {
8961 propname
= elemname
;
8962 proptype
= PROP_TYPE_STRING
;
8964 propname
= zpool_prop_to_name(prop
);
8965 proptype
= zpool_prop_get_type(prop
);
8968 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
8969 ASSERT(proptype
== PROP_TYPE_STRING
);
8970 strval
= fnvpair_value_string(elem
);
8971 VERIFY0(zap_update(mos
,
8972 spa
->spa_pool_props_object
, propname
,
8973 1, strlen(strval
) + 1, strval
, tx
));
8974 spa_history_log_internal(spa
, "set", tx
,
8975 "%s=%s", elemname
, strval
);
8976 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
8977 intval
= fnvpair_value_uint64(elem
);
8979 if (proptype
== PROP_TYPE_INDEX
) {
8981 VERIFY0(zpool_prop_index_to_string(
8982 prop
, intval
, &unused
));
8984 VERIFY0(zap_update(mos
,
8985 spa
->spa_pool_props_object
, propname
,
8986 8, 1, &intval
, tx
));
8987 spa_history_log_internal(spa
, "set", tx
,
8988 "%s=%lld", elemname
,
8989 (longlong_t
)intval
);
8992 case ZPOOL_PROP_DELEGATION
:
8993 spa
->spa_delegation
= intval
;
8995 case ZPOOL_PROP_BOOTFS
:
8996 spa
->spa_bootfs
= intval
;
8998 case ZPOOL_PROP_FAILUREMODE
:
8999 spa
->spa_failmode
= intval
;
9001 case ZPOOL_PROP_AUTOTRIM
:
9002 spa
->spa_autotrim
= intval
;
9003 spa_async_request(spa
,
9004 SPA_ASYNC_AUTOTRIM_RESTART
);
9006 case ZPOOL_PROP_AUTOEXPAND
:
9007 spa
->spa_autoexpand
= intval
;
9008 if (tx
->tx_txg
!= TXG_INITIAL
)
9009 spa_async_request(spa
,
9010 SPA_ASYNC_AUTOEXPAND
);
9012 case ZPOOL_PROP_MULTIHOST
:
9013 spa
->spa_multihost
= intval
;
9019 ASSERT(0); /* not allowed */
9025 mutex_exit(&spa
->spa_props_lock
);
9029 * Perform one-time upgrade on-disk changes. spa_version() does not
9030 * reflect the new version this txg, so there must be no changes this
9031 * txg to anything that the upgrade code depends on after it executes.
9032 * Therefore this must be called after dsl_pool_sync() does the sync
9036 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
9038 if (spa_sync_pass(spa
) != 1)
9041 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9042 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
9044 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
9045 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
9046 dsl_pool_create_origin(dp
, tx
);
9048 /* Keeping the origin open increases spa_minref */
9049 spa
->spa_minref
+= 3;
9052 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
9053 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
9054 dsl_pool_upgrade_clones(dp
, tx
);
9057 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
9058 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
9059 dsl_pool_upgrade_dir_clones(dp
, tx
);
9061 /* Keeping the freedir open increases spa_minref */
9062 spa
->spa_minref
+= 3;
9065 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
9066 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9067 spa_feature_create_zap_objects(spa
, tx
);
9071 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
9072 * when possibility to use lz4 compression for metadata was added
9073 * Old pools that have this feature enabled must be upgraded to have
9074 * this feature active
9076 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9077 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
9078 SPA_FEATURE_LZ4_COMPRESS
);
9079 boolean_t lz4_ac
= spa_feature_is_active(spa
,
9080 SPA_FEATURE_LZ4_COMPRESS
);
9082 if (lz4_en
&& !lz4_ac
)
9083 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
9087 * If we haven't written the salt, do so now. Note that the
9088 * feature may not be activated yet, but that's fine since
9089 * the presence of this ZAP entry is backwards compatible.
9091 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
9092 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
9093 VERIFY0(zap_add(spa
->spa_meta_objset
,
9094 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
9095 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
9096 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
9099 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
9103 vdev_indirect_state_sync_verify(vdev_t
*vd
)
9105 vdev_indirect_mapping_t
*vim __maybe_unused
= vd
->vdev_indirect_mapping
;
9106 vdev_indirect_births_t
*vib __maybe_unused
= vd
->vdev_indirect_births
;
9108 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
9109 ASSERT(vim
!= NULL
);
9110 ASSERT(vib
!= NULL
);
9113 uint64_t obsolete_sm_object
= 0;
9114 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
9115 if (obsolete_sm_object
!= 0) {
9116 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
9117 ASSERT(vd
->vdev_removing
||
9118 vd
->vdev_ops
== &vdev_indirect_ops
);
9119 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
9120 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
9121 ASSERT3U(obsolete_sm_object
, ==,
9122 space_map_object(vd
->vdev_obsolete_sm
));
9123 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
9124 space_map_allocated(vd
->vdev_obsolete_sm
));
9126 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
9129 * Since frees / remaps to an indirect vdev can only
9130 * happen in syncing context, the obsolete segments
9131 * tree must be empty when we start syncing.
9133 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
9137 * Set the top-level vdev's max queue depth. Evaluate each top-level's
9138 * async write queue depth in case it changed. The max queue depth will
9139 * not change in the middle of syncing out this txg.
9142 spa_sync_adjust_vdev_max_queue_depth(spa_t
*spa
)
9144 ASSERT(spa_writeable(spa
));
9146 vdev_t
*rvd
= spa
->spa_root_vdev
;
9147 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
9148 zfs_vdev_queue_depth_pct
/ 100;
9149 metaslab_class_t
*normal
= spa_normal_class(spa
);
9150 metaslab_class_t
*special
= spa_special_class(spa
);
9151 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
9153 uint64_t slots_per_allocator
= 0;
9154 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9155 vdev_t
*tvd
= rvd
->vdev_child
[c
];
9157 metaslab_group_t
*mg
= tvd
->vdev_mg
;
9158 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
9161 metaslab_class_t
*mc
= mg
->mg_class
;
9162 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
9166 * It is safe to do a lock-free check here because only async
9167 * allocations look at mg_max_alloc_queue_depth, and async
9168 * allocations all happen from spa_sync().
9170 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9171 ASSERT0(zfs_refcount_count(
9172 &(mg
->mg_allocator
[i
].mga_alloc_queue_depth
)));
9174 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
9176 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9177 mg
->mg_allocator
[i
].mga_cur_max_alloc_queue_depth
=
9178 zfs_vdev_def_queue_depth
;
9180 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
9183 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9184 ASSERT0(zfs_refcount_count(&normal
->mc_allocator
[i
].
9186 ASSERT0(zfs_refcount_count(&special
->mc_allocator
[i
].
9188 ASSERT0(zfs_refcount_count(&dedup
->mc_allocator
[i
].
9190 normal
->mc_allocator
[i
].mca_alloc_max_slots
=
9191 slots_per_allocator
;
9192 special
->mc_allocator
[i
].mca_alloc_max_slots
=
9193 slots_per_allocator
;
9194 dedup
->mc_allocator
[i
].mca_alloc_max_slots
=
9195 slots_per_allocator
;
9197 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9198 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9199 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9203 spa_sync_condense_indirect(spa_t
*spa
, dmu_tx_t
*tx
)
9205 ASSERT(spa_writeable(spa
));
9207 vdev_t
*rvd
= spa
->spa_root_vdev
;
9208 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9209 vdev_t
*vd
= rvd
->vdev_child
[c
];
9210 vdev_indirect_state_sync_verify(vd
);
9212 if (vdev_indirect_should_condense(vd
)) {
9213 spa_condense_indirect_start_sync(vd
, tx
);
9220 spa_sync_iterate_to_convergence(spa_t
*spa
, dmu_tx_t
*tx
)
9222 objset_t
*mos
= spa
->spa_meta_objset
;
9223 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9224 uint64_t txg
= tx
->tx_txg
;
9225 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
9228 int pass
= ++spa
->spa_sync_pass
;
9230 spa_sync_config_object(spa
, tx
);
9231 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
9232 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
9233 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
9234 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
9235 spa_errlog_sync(spa
, txg
);
9236 dsl_pool_sync(dp
, txg
);
9238 if (pass
< zfs_sync_pass_deferred_free
||
9239 spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
)) {
9241 * If the log space map feature is active we don't
9242 * care about deferred frees and the deferred bpobj
9243 * as the log space map should effectively have the
9244 * same results (i.e. appending only to one object).
9246 spa_sync_frees(spa
, free_bpl
, tx
);
9249 * We can not defer frees in pass 1, because
9250 * we sync the deferred frees later in pass 1.
9252 ASSERT3U(pass
, >, 1);
9253 bplist_iterate(free_bpl
, bpobj_enqueue_alloc_cb
,
9254 &spa
->spa_deferred_bpobj
, tx
);
9259 dsl_scan_sync(dp
, tx
);
9260 dsl_errorscrub_sync(dp
, tx
);
9262 spa_sync_upgrades(spa
, tx
);
9264 spa_flush_metaslabs(spa
, tx
);
9267 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
9272 * Note: We need to check if the MOS is dirty because we could
9273 * have marked the MOS dirty without updating the uberblock
9274 * (e.g. if we have sync tasks but no dirty user data). We need
9275 * to check the uberblock's rootbp because it is updated if we
9276 * have synced out dirty data (though in this case the MOS will
9277 * most likely also be dirty due to second order effects, we
9278 * don't want to rely on that here).
9281 spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
9282 !dmu_objset_is_dirty(mos
, txg
)) {
9284 * Nothing changed on the first pass, therefore this
9285 * TXG is a no-op. Avoid syncing deferred frees, so
9286 * that we can keep this TXG as a no-op.
9288 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9289 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9290 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
9291 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
, txg
));
9295 spa_sync_deferred_frees(spa
, tx
);
9296 } while (dmu_objset_is_dirty(mos
, txg
));
9300 * Rewrite the vdev configuration (which includes the uberblock) to
9301 * commit the transaction group.
9303 * If there are no dirty vdevs, we sync the uberblock to a few random
9304 * top-level vdevs that are known to be visible in the config cache
9305 * (see spa_vdev_add() for a complete description). If there *are* dirty
9306 * vdevs, sync the uberblock to all vdevs.
9309 spa_sync_rewrite_vdev_config(spa_t
*spa
, dmu_tx_t
*tx
)
9311 vdev_t
*rvd
= spa
->spa_root_vdev
;
9312 uint64_t txg
= tx
->tx_txg
;
9318 * We hold SCL_STATE to prevent vdev open/close/etc.
9319 * while we're attempting to write the vdev labels.
9321 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9323 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
9324 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
9326 int children
= rvd
->vdev_children
;
9327 int c0
= random_in_range(children
);
9329 for (int c
= 0; c
< children
; c
++) {
9331 rvd
->vdev_child
[(c0
+ c
) % children
];
9333 /* Stop when revisiting the first vdev */
9334 if (c
> 0 && svd
[0] == vd
)
9337 if (vd
->vdev_ms_array
== 0 ||
9339 !vdev_is_concrete(vd
))
9342 svd
[svdcount
++] = vd
;
9343 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
9346 error
= vdev_config_sync(svd
, svdcount
, txg
);
9348 error
= vdev_config_sync(rvd
->vdev_child
,
9349 rvd
->vdev_children
, txg
);
9353 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
9355 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9359 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
9360 zio_resume_wait(spa
);
9365 * Sync the specified transaction group. New blocks may be dirtied as
9366 * part of the process, so we iterate until it converges.
9369 spa_sync(spa_t
*spa
, uint64_t txg
)
9373 VERIFY(spa_writeable(spa
));
9376 * Wait for i/os issued in open context that need to complete
9377 * before this txg syncs.
9379 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
9380 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
9384 * Now that there can be no more cloning in this transaction group,
9385 * but we are still before issuing frees, we can process pending BRT
9388 brt_pending_apply(spa
, txg
);
9391 * Lock out configuration changes.
9393 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9395 spa
->spa_syncing_txg
= txg
;
9396 spa
->spa_sync_pass
= 0;
9398 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9399 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
9400 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
9401 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
9405 * If there are any pending vdev state changes, convert them
9406 * into config changes that go out with this transaction group.
9408 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9409 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9410 /* Avoid holding the write lock unless actually necessary */
9411 if (vd
->vdev_aux
== NULL
) {
9412 vdev_state_clean(vd
);
9413 vdev_config_dirty(vd
);
9417 * We need the write lock here because, for aux vdevs,
9418 * calling vdev_config_dirty() modifies sav_config.
9419 * This is ugly and will become unnecessary when we
9420 * eliminate the aux vdev wart by integrating all vdevs
9421 * into the root vdev tree.
9423 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9424 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
9425 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9426 vdev_state_clean(vd
);
9427 vdev_config_dirty(vd
);
9429 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9430 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9432 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9434 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9435 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
9437 spa
->spa_sync_starttime
= gethrtime();
9438 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9439 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
9440 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
9441 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
9444 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
9445 * set spa_deflate if we have no raid-z vdevs.
9447 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
9448 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
9449 vdev_t
*rvd
= spa
->spa_root_vdev
;
9452 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
9453 vd
= rvd
->vdev_child
[i
];
9454 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
9457 if (i
== rvd
->vdev_children
) {
9458 spa
->spa_deflate
= TRUE
;
9459 VERIFY0(zap_add(spa
->spa_meta_objset
,
9460 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
9461 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
9465 spa_sync_adjust_vdev_max_queue_depth(spa
);
9467 spa_sync_condense_indirect(spa
, tx
);
9469 spa_sync_iterate_to_convergence(spa
, tx
);
9472 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
9474 * Make sure that the number of ZAPs for all the vdevs matches
9475 * the number of ZAPs in the per-vdev ZAP list. This only gets
9476 * called if the config is dirty; otherwise there may be
9477 * outstanding AVZ operations that weren't completed in
9478 * spa_sync_config_object.
9480 uint64_t all_vdev_zap_entry_count
;
9481 ASSERT0(zap_count(spa
->spa_meta_objset
,
9482 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
9483 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
9484 all_vdev_zap_entry_count
);
9488 if (spa
->spa_vdev_removal
!= NULL
) {
9489 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
9492 spa_sync_rewrite_vdev_config(spa
, tx
);
9495 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9496 spa
->spa_deadman_tqid
= 0;
9499 * Clear the dirty config list.
9501 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
9502 vdev_config_clean(vd
);
9505 * Now that the new config has synced transactionally,
9506 * let it become visible to the config cache.
9508 if (spa
->spa_config_syncing
!= NULL
) {
9509 spa_config_set(spa
, spa
->spa_config_syncing
);
9510 spa
->spa_config_txg
= txg
;
9511 spa
->spa_config_syncing
= NULL
;
9514 dsl_pool_sync_done(dp
, txg
);
9516 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9517 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
9518 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
9519 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
9523 * Update usable space statistics.
9525 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
9527 vdev_sync_done(vd
, txg
);
9529 metaslab_class_evict_old(spa
->spa_normal_class
, txg
);
9530 metaslab_class_evict_old(spa
->spa_log_class
, txg
);
9532 spa_sync_close_syncing_log_sm(spa
);
9534 spa_update_dspace(spa
);
9536 if (spa_get_autotrim(spa
) == SPA_AUTOTRIM_ON
)
9537 vdev_autotrim_kick(spa
);
9540 * It had better be the case that we didn't dirty anything
9541 * since vdev_config_sync().
9543 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9544 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9545 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
9547 while (zfs_pause_spa_sync
)
9550 spa
->spa_sync_pass
= 0;
9553 * Update the last synced uberblock here. We want to do this at
9554 * the end of spa_sync() so that consumers of spa_last_synced_txg()
9555 * will be guaranteed that all the processing associated with
9556 * that txg has been completed.
9558 spa
->spa_ubsync
= spa
->spa_uberblock
;
9559 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9561 spa_handle_ignored_writes(spa
);
9564 * If any async tasks have been requested, kick them off.
9566 spa_async_dispatch(spa
);
9570 * Sync all pools. We don't want to hold the namespace lock across these
9571 * operations, so we take a reference on the spa_t and drop the lock during the
9575 spa_sync_allpools(void)
9578 mutex_enter(&spa_namespace_lock
);
9579 while ((spa
= spa_next(spa
)) != NULL
) {
9580 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
9581 !spa_writeable(spa
) || spa_suspended(spa
))
9583 spa_open_ref(spa
, FTAG
);
9584 mutex_exit(&spa_namespace_lock
);
9585 txg_wait_synced(spa_get_dsl(spa
), 0);
9586 mutex_enter(&spa_namespace_lock
);
9587 spa_close(spa
, FTAG
);
9589 mutex_exit(&spa_namespace_lock
);
9593 * ==========================================================================
9594 * Miscellaneous routines
9595 * ==========================================================================
9599 * Remove all pools in the system.
9607 * Remove all cached state. All pools should be closed now,
9608 * so every spa in the AVL tree should be unreferenced.
9610 mutex_enter(&spa_namespace_lock
);
9611 while ((spa
= spa_next(NULL
)) != NULL
) {
9613 * Stop async tasks. The async thread may need to detach
9614 * a device that's been replaced, which requires grabbing
9615 * spa_namespace_lock, so we must drop it here.
9617 spa_open_ref(spa
, FTAG
);
9618 mutex_exit(&spa_namespace_lock
);
9619 spa_async_suspend(spa
);
9620 mutex_enter(&spa_namespace_lock
);
9621 spa_close(spa
, FTAG
);
9623 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
9625 spa_deactivate(spa
);
9629 mutex_exit(&spa_namespace_lock
);
9633 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
9638 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
9642 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
9643 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
9644 if (vd
->vdev_guid
== guid
)
9648 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
9649 vd
= spa
->spa_spares
.sav_vdevs
[i
];
9650 if (vd
->vdev_guid
== guid
)
9659 spa_upgrade(spa_t
*spa
, uint64_t version
)
9661 ASSERT(spa_writeable(spa
));
9663 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
9666 * This should only be called for a non-faulted pool, and since a
9667 * future version would result in an unopenable pool, this shouldn't be
9670 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
9671 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
9673 spa
->spa_uberblock
.ub_version
= version
;
9674 vdev_config_dirty(spa
->spa_root_vdev
);
9676 spa_config_exit(spa
, SCL_ALL
, FTAG
);
9678 txg_wait_synced(spa_get_dsl(spa
), 0);
9682 spa_has_aux_vdev(spa_t
*spa
, uint64_t guid
, spa_aux_vdev_t
*sav
)
9688 for (i
= 0; i
< sav
->sav_count
; i
++)
9689 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
9692 for (i
= 0; i
< sav
->sav_npending
; i
++) {
9693 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
9694 &vdev_guid
) == 0 && vdev_guid
== guid
)
9702 spa_has_l2cache(spa_t
*spa
, uint64_t guid
)
9704 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_l2cache
));
9708 spa_has_spare(spa_t
*spa
, uint64_t guid
)
9710 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_spares
));
9714 * Check if a pool has an active shared spare device.
9715 * Note: reference count of an active spare is 2, as a spare and as a replace
9718 spa_has_active_shared_spare(spa_t
*spa
)
9722 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
9724 for (i
= 0; i
< sav
->sav_count
; i
++) {
9725 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
9726 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
9735 spa_total_metaslabs(spa_t
*spa
)
9737 vdev_t
*rvd
= spa
->spa_root_vdev
;
9740 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
9741 vdev_t
*vd
= rvd
->vdev_child
[c
];
9742 if (!vdev_is_concrete(vd
))
9744 m
+= vd
->vdev_ms_count
;
9750 * Notify any waiting threads that some activity has switched from being in-
9751 * progress to not-in-progress so that the thread can wake up and determine
9752 * whether it is finished waiting.
9755 spa_notify_waiters(spa_t
*spa
)
9758 * Acquiring spa_activities_lock here prevents the cv_broadcast from
9759 * happening between the waiting thread's check and cv_wait.
9761 mutex_enter(&spa
->spa_activities_lock
);
9762 cv_broadcast(&spa
->spa_activities_cv
);
9763 mutex_exit(&spa
->spa_activities_lock
);
9767 * Notify any waiting threads that the pool is exporting, and then block until
9768 * they are finished using the spa_t.
9771 spa_wake_waiters(spa_t
*spa
)
9773 mutex_enter(&spa
->spa_activities_lock
);
9774 spa
->spa_waiters_cancel
= B_TRUE
;
9775 cv_broadcast(&spa
->spa_activities_cv
);
9776 while (spa
->spa_waiters
!= 0)
9777 cv_wait(&spa
->spa_waiters_cv
, &spa
->spa_activities_lock
);
9778 spa
->spa_waiters_cancel
= B_FALSE
;
9779 mutex_exit(&spa
->spa_activities_lock
);
9782 /* Whether the vdev or any of its descendants are being initialized/trimmed. */
9784 spa_vdev_activity_in_progress_impl(vdev_t
*vd
, zpool_wait_activity_t activity
)
9786 spa_t
*spa
= vd
->vdev_spa
;
9788 ASSERT(spa_config_held(spa
, SCL_CONFIG
| SCL_STATE
, RW_READER
));
9789 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9790 ASSERT(activity
== ZPOOL_WAIT_INITIALIZE
||
9791 activity
== ZPOOL_WAIT_TRIM
);
9793 kmutex_t
*lock
= activity
== ZPOOL_WAIT_INITIALIZE
?
9794 &vd
->vdev_initialize_lock
: &vd
->vdev_trim_lock
;
9796 mutex_exit(&spa
->spa_activities_lock
);
9798 mutex_enter(&spa
->spa_activities_lock
);
9800 boolean_t in_progress
= (activity
== ZPOOL_WAIT_INITIALIZE
) ?
9801 (vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) :
9802 (vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
);
9808 for (int i
= 0; i
< vd
->vdev_children
; i
++) {
9809 if (spa_vdev_activity_in_progress_impl(vd
->vdev_child
[i
],
9818 * If use_guid is true, this checks whether the vdev specified by guid is
9819 * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
9820 * is being initialized/trimmed. The caller must hold the config lock and
9821 * spa_activities_lock.
9824 spa_vdev_activity_in_progress(spa_t
*spa
, boolean_t use_guid
, uint64_t guid
,
9825 zpool_wait_activity_t activity
, boolean_t
*in_progress
)
9827 mutex_exit(&spa
->spa_activities_lock
);
9828 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9829 mutex_enter(&spa
->spa_activities_lock
);
9833 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
9834 if (vd
== NULL
|| !vd
->vdev_ops
->vdev_op_leaf
) {
9835 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9839 vd
= spa
->spa_root_vdev
;
9842 *in_progress
= spa_vdev_activity_in_progress_impl(vd
, activity
);
9844 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9849 * Locking for waiting threads
9850 * ---------------------------
9852 * Waiting threads need a way to check whether a given activity is in progress,
9853 * and then, if it is, wait for it to complete. Each activity will have some
9854 * in-memory representation of the relevant on-disk state which can be used to
9855 * determine whether or not the activity is in progress. The in-memory state and
9856 * the locking used to protect it will be different for each activity, and may
9857 * not be suitable for use with a cvar (e.g., some state is protected by the
9858 * config lock). To allow waiting threads to wait without any races, another
9859 * lock, spa_activities_lock, is used.
9861 * When the state is checked, both the activity-specific lock (if there is one)
9862 * and spa_activities_lock are held. In some cases, the activity-specific lock
9863 * is acquired explicitly (e.g. the config lock). In others, the locking is
9864 * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
9865 * thread releases the activity-specific lock and, if the activity is in
9866 * progress, then cv_waits using spa_activities_lock.
9868 * The waiting thread is woken when another thread, one completing some
9869 * activity, updates the state of the activity and then calls
9870 * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
9871 * needs to hold its activity-specific lock when updating the state, and this
9872 * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
9874 * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
9875 * and because it is held when the waiting thread checks the state of the
9876 * activity, it can never be the case that the completing thread both updates
9877 * the activity state and cv_broadcasts in between the waiting thread's check
9878 * and cv_wait. Thus, a waiting thread can never miss a wakeup.
9880 * In order to prevent deadlock, when the waiting thread does its check, in some
9881 * cases it will temporarily drop spa_activities_lock in order to acquire the
9882 * activity-specific lock. The order in which spa_activities_lock and the
9883 * activity specific lock are acquired in the waiting thread is determined by
9884 * the order in which they are acquired in the completing thread; if the
9885 * completing thread calls spa_notify_waiters with the activity-specific lock
9886 * held, then the waiting thread must also acquire the activity-specific lock
9891 spa_activity_in_progress(spa_t
*spa
, zpool_wait_activity_t activity
,
9892 boolean_t use_tag
, uint64_t tag
, boolean_t
*in_progress
)
9896 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9899 case ZPOOL_WAIT_CKPT_DISCARD
:
9901 (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
) &&
9902 zap_contains(spa_meta_objset(spa
),
9903 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_ZPOOL_CHECKPOINT
) ==
9906 case ZPOOL_WAIT_FREE
:
9907 *in_progress
= ((spa_version(spa
) >= SPA_VERSION_DEADLISTS
&&
9908 !bpobj_is_empty(&spa
->spa_dsl_pool
->dp_free_bpobj
)) ||
9909 spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
) ||
9910 spa_livelist_delete_check(spa
));
9912 case ZPOOL_WAIT_INITIALIZE
:
9913 case ZPOOL_WAIT_TRIM
:
9914 error
= spa_vdev_activity_in_progress(spa
, use_tag
, tag
,
9915 activity
, in_progress
);
9917 case ZPOOL_WAIT_REPLACE
:
9918 mutex_exit(&spa
->spa_activities_lock
);
9919 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9920 mutex_enter(&spa
->spa_activities_lock
);
9922 *in_progress
= vdev_replace_in_progress(spa
->spa_root_vdev
);
9923 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9925 case ZPOOL_WAIT_REMOVE
:
9926 *in_progress
= (spa
->spa_removing_phys
.sr_state
==
9929 case ZPOOL_WAIT_RESILVER
:
9930 if ((*in_progress
= vdev_rebuild_active(spa
->spa_root_vdev
)))
9933 case ZPOOL_WAIT_SCRUB
:
9935 boolean_t scanning
, paused
, is_scrub
;
9936 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
9938 is_scrub
= (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
);
9939 scanning
= (scn
->scn_phys
.scn_state
== DSS_SCANNING
);
9940 paused
= dsl_scan_is_paused_scrub(scn
);
9941 *in_progress
= (scanning
&& !paused
&&
9942 is_scrub
== (activity
== ZPOOL_WAIT_SCRUB
));
9946 panic("unrecognized value for activity %d", activity
);
9953 spa_wait_common(const char *pool
, zpool_wait_activity_t activity
,
9954 boolean_t use_tag
, uint64_t tag
, boolean_t
*waited
)
9957 * The tag is used to distinguish between instances of an activity.
9958 * 'initialize' and 'trim' are the only activities that we use this for.
9959 * The other activities can only have a single instance in progress in a
9960 * pool at one time, making the tag unnecessary.
9962 * There can be multiple devices being replaced at once, but since they
9963 * all finish once resilvering finishes, we don't bother keeping track
9964 * of them individually, we just wait for them all to finish.
9966 if (use_tag
&& activity
!= ZPOOL_WAIT_INITIALIZE
&&
9967 activity
!= ZPOOL_WAIT_TRIM
)
9970 if (activity
< 0 || activity
>= ZPOOL_WAIT_NUM_ACTIVITIES
)
9974 int error
= spa_open(pool
, &spa
, FTAG
);
9979 * Increment the spa's waiter count so that we can call spa_close and
9980 * still ensure that the spa_t doesn't get freed before this thread is
9981 * finished with it when the pool is exported. We want to call spa_close
9982 * before we start waiting because otherwise the additional ref would
9983 * prevent the pool from being exported or destroyed throughout the
9984 * potentially long wait.
9986 mutex_enter(&spa
->spa_activities_lock
);
9988 spa_close(spa
, FTAG
);
9992 boolean_t in_progress
;
9993 error
= spa_activity_in_progress(spa
, activity
, use_tag
, tag
,
9996 if (error
|| !in_progress
|| spa
->spa_waiters_cancel
)
10001 if (cv_wait_sig(&spa
->spa_activities_cv
,
10002 &spa
->spa_activities_lock
) == 0) {
10008 spa
->spa_waiters
--;
10009 cv_signal(&spa
->spa_waiters_cv
);
10010 mutex_exit(&spa
->spa_activities_lock
);
10016 * Wait for a particular instance of the specified activity to complete, where
10017 * the instance is identified by 'tag'
10020 spa_wait_tag(const char *pool
, zpool_wait_activity_t activity
, uint64_t tag
,
10023 return (spa_wait_common(pool
, activity
, B_TRUE
, tag
, waited
));
10027 * Wait for all instances of the specified activity complete
10030 spa_wait(const char *pool
, zpool_wait_activity_t activity
, boolean_t
*waited
)
10033 return (spa_wait_common(pool
, activity
, B_FALSE
, 0, waited
));
10037 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10039 sysevent_t
*ev
= NULL
;
10041 nvlist_t
*resource
;
10043 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
10045 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
10046 ev
->resource
= resource
;
10049 (void) spa
, (void) vd
, (void) hist_nvl
, (void) name
;
10055 spa_event_post(sysevent_t
*ev
)
10059 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
10060 kmem_free(ev
, sizeof (*ev
));
10068 * Post a zevent corresponding to the given sysevent. The 'name' must be one
10069 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
10070 * filled in from the spa and (optionally) the vdev. This doesn't do anything
10071 * in the userland libzpool, as we don't want consumers to misinterpret ztest
10072 * or zdb as real changes.
10075 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10077 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
10080 /* state manipulation functions */
10081 EXPORT_SYMBOL(spa_open
);
10082 EXPORT_SYMBOL(spa_open_rewind
);
10083 EXPORT_SYMBOL(spa_get_stats
);
10084 EXPORT_SYMBOL(spa_create
);
10085 EXPORT_SYMBOL(spa_import
);
10086 EXPORT_SYMBOL(spa_tryimport
);
10087 EXPORT_SYMBOL(spa_destroy
);
10088 EXPORT_SYMBOL(spa_export
);
10089 EXPORT_SYMBOL(spa_reset
);
10090 EXPORT_SYMBOL(spa_async_request
);
10091 EXPORT_SYMBOL(spa_async_suspend
);
10092 EXPORT_SYMBOL(spa_async_resume
);
10093 EXPORT_SYMBOL(spa_inject_addref
);
10094 EXPORT_SYMBOL(spa_inject_delref
);
10095 EXPORT_SYMBOL(spa_scan_stat_init
);
10096 EXPORT_SYMBOL(spa_scan_get_stats
);
10098 /* device manipulation */
10099 EXPORT_SYMBOL(spa_vdev_add
);
10100 EXPORT_SYMBOL(spa_vdev_attach
);
10101 EXPORT_SYMBOL(spa_vdev_detach
);
10102 EXPORT_SYMBOL(spa_vdev_setpath
);
10103 EXPORT_SYMBOL(spa_vdev_setfru
);
10104 EXPORT_SYMBOL(spa_vdev_split_mirror
);
10106 /* spare statech is global across all pools) */
10107 EXPORT_SYMBOL(spa_spare_add
);
10108 EXPORT_SYMBOL(spa_spare_remove
);
10109 EXPORT_SYMBOL(spa_spare_exists
);
10110 EXPORT_SYMBOL(spa_spare_activate
);
10112 /* L2ARC statech is global across all pools) */
10113 EXPORT_SYMBOL(spa_l2cache_add
);
10114 EXPORT_SYMBOL(spa_l2cache_remove
);
10115 EXPORT_SYMBOL(spa_l2cache_exists
);
10116 EXPORT_SYMBOL(spa_l2cache_activate
);
10117 EXPORT_SYMBOL(spa_l2cache_drop
);
10120 EXPORT_SYMBOL(spa_scan
);
10121 EXPORT_SYMBOL(spa_scan_stop
);
10124 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
10125 EXPORT_SYMBOL(spa_sync_allpools
);
10128 EXPORT_SYMBOL(spa_prop_set
);
10129 EXPORT_SYMBOL(spa_prop_get
);
10130 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
10132 /* asynchronous event notification */
10133 EXPORT_SYMBOL(spa_event_notify
);
10135 /* BEGIN CSTYLED */
10136 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_shift
, UINT
, ZMOD_RW
,
10137 "log2 fraction of arc that can be used by inflight I/Os when "
10138 "verifying pool during import");
10141 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_metadata
, INT
, ZMOD_RW
,
10142 "Set to traverse metadata on pool import");
10144 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_data
, INT
, ZMOD_RW
,
10145 "Set to traverse data on pool import");
10147 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_print_vdev_tree
, INT
, ZMOD_RW
,
10148 "Print vdev tree to zfs_dbgmsg during pool import");
10150 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_pct
, UINT
, ZMOD_RD
,
10151 "Percentage of CPUs to run an IO worker thread");
10153 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_tpq
, UINT
, ZMOD_RD
,
10154 "Number of threads per IO worker taskqueue");
10156 /* BEGIN CSTYLED */
10157 ZFS_MODULE_PARAM(zfs
, zfs_
, max_missing_tvds
, U64
, ZMOD_RW
,
10158 "Allow importing pool with up to this number of missing top-level "
10159 "vdevs (in read-only mode)");
10162 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_pause
, INT
,
10163 ZMOD_RW
, "Set the livelist condense zthr to pause");
10165 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_pause
, INT
,
10166 ZMOD_RW
, "Set the livelist condense synctask to pause");
10168 /* BEGIN CSTYLED */
10169 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_cancel
,
10171 "Whether livelist condensing was canceled in the synctask");
10173 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_cancel
,
10175 "Whether livelist condensing was canceled in the zthr function");
10177 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, new_alloc
, INT
,
10179 "Whether extra ALLOC blkptrs were added to a livelist entry while it "
10180 "was being condensed");