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>
39 * SPA: Storage Pool Allocator
41 * This file contains all the routines used when modifying on-disk SPA state.
42 * This includes opening, importing, destroying, exporting a pool, and syncing a
46 #include <sys/zfs_context.h>
47 #include <sys/fm/fs/zfs.h>
48 #include <sys/spa_impl.h>
50 #include <sys/zio_checksum.h>
52 #include <sys/dmu_tx.h>
57 #include <sys/vdev_impl.h>
58 #include <sys/vdev_removal.h>
59 #include <sys/vdev_indirect_mapping.h>
60 #include <sys/vdev_indirect_births.h>
61 #include <sys/vdev_initialize.h>
62 #include <sys/vdev_rebuild.h>
63 #include <sys/vdev_trim.h>
64 #include <sys/vdev_disk.h>
65 #include <sys/vdev_draid.h>
66 #include <sys/metaslab.h>
67 #include <sys/metaslab_impl.h>
69 #include <sys/uberblock_impl.h>
72 #include <sys/bpobj.h>
73 #include <sys/dmu_traverse.h>
74 #include <sys/dmu_objset.h>
75 #include <sys/unique.h>
76 #include <sys/dsl_pool.h>
77 #include <sys/dsl_dataset.h>
78 #include <sys/dsl_dir.h>
79 #include <sys/dsl_prop.h>
80 #include <sys/dsl_synctask.h>
81 #include <sys/fs/zfs.h>
83 #include <sys/callb.h>
84 #include <sys/systeminfo.h>
85 #include <sys/zfs_ioctl.h>
86 #include <sys/dsl_scan.h>
87 #include <sys/zfeature.h>
88 #include <sys/dsl_destroy.h>
92 #include <sys/fm/protocol.h>
93 #include <sys/fm/util.h>
94 #include <sys/callb.h>
96 #include <sys/vmsystm.h>
100 #include "zfs_comutil.h"
103 * The interval, in seconds, at which failed configuration cache file writes
106 int zfs_ccw_retry_interval
= 300;
108 typedef enum zti_modes
{
109 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
110 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
111 ZTI_MODE_SCALE
, /* Taskqs scale with CPUs. */
112 ZTI_MODE_NULL
, /* don't create a taskq */
116 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
117 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
118 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
119 #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 }
120 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
122 #define ZTI_N(n) ZTI_P(n, 1)
123 #define ZTI_ONE ZTI_N(1)
125 typedef struct zio_taskq_info
{
126 zti_modes_t zti_mode
;
131 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
132 "iss", "iss_h", "int", "int_h"
136 * This table defines the taskq settings for each ZFS I/O type. When
137 * initializing a pool, we use this table to create an appropriately sized
138 * taskq. Some operations are low volume and therefore have a small, static
139 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
140 * macros. Other operations process a large amount of data; the ZTI_BATCH
141 * macro causes us to create a taskq oriented for throughput. Some operations
142 * are so high frequency and short-lived that the taskq itself can become a
143 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
144 * additional degree of parallelism specified by the number of threads per-
145 * taskq and the number of taskqs; when dispatching an event in this case, the
146 * particular taskq is chosen at random. ZTI_SCALE is similar to ZTI_BATCH,
147 * but with number of taskqs also scaling with number of CPUs.
149 * The different taskq priorities are to handle the different contexts (issue
150 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
151 * need to be handled with minimum delay.
153 static const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
154 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
155 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
156 { ZTI_N(8), ZTI_NULL
, ZTI_SCALE
, ZTI_NULL
}, /* READ */
157 { ZTI_BATCH
, ZTI_N(5), ZTI_SCALE
, ZTI_N(5) }, /* WRITE */
158 { ZTI_SCALE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
159 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
160 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
161 { ZTI_N(4), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* TRIM */
164 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
165 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
166 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
167 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
,
168 const char **ereport
);
169 static void spa_vdev_resilver_done(spa_t
*spa
);
171 static uint_t zio_taskq_batch_pct
= 80; /* 1 thread per cpu in pset */
172 static uint_t zio_taskq_batch_tpq
; /* threads per taskq */
173 static const boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
174 static const uint_t zio_taskq_basedc
= 80; /* base duty cycle */
176 static const boolean_t spa_create_process
= B_TRUE
; /* no process => no sysdc */
179 * Report any spa_load_verify errors found, but do not fail spa_load.
180 * This is used by zdb to analyze non-idle pools.
182 boolean_t spa_load_verify_dryrun
= B_FALSE
;
185 * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ).
186 * This is used by zdb for spacemaps verification.
188 boolean_t spa_mode_readable_spacemaps
= B_FALSE
;
191 * This (illegal) pool name is used when temporarily importing a spa_t in order
192 * to get the vdev stats associated with the imported devices.
194 #define TRYIMPORT_NAME "$import"
197 * For debugging purposes: print out vdev tree during pool import.
199 static int spa_load_print_vdev_tree
= B_FALSE
;
202 * A non-zero value for zfs_max_missing_tvds means that we allow importing
203 * pools with missing top-level vdevs. This is strictly intended for advanced
204 * pool recovery cases since missing data is almost inevitable. Pools with
205 * missing devices can only be imported read-only for safety reasons, and their
206 * fail-mode will be automatically set to "continue".
208 * With 1 missing vdev we should be able to import the pool and mount all
209 * datasets. User data that was not modified after the missing device has been
210 * added should be recoverable. This means that snapshots created prior to the
211 * addition of that device should be completely intact.
213 * With 2 missing vdevs, some datasets may fail to mount since there are
214 * dataset statistics that are stored as regular metadata. Some data might be
215 * recoverable if those vdevs were added recently.
217 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
218 * may be missing entirely. Chances of data recovery are very low. Note that
219 * there are also risks of performing an inadvertent rewind as we might be
220 * missing all the vdevs with the latest uberblocks.
222 uint64_t zfs_max_missing_tvds
= 0;
225 * The parameters below are similar to zfs_max_missing_tvds but are only
226 * intended for a preliminary open of the pool with an untrusted config which
227 * might be incomplete or out-dated.
229 * We are more tolerant for pools opened from a cachefile since we could have
230 * an out-dated cachefile where a device removal was not registered.
231 * We could have set the limit arbitrarily high but in the case where devices
232 * are really missing we would want to return the proper error codes; we chose
233 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
234 * and we get a chance to retrieve the trusted config.
236 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
239 * In the case where config was assembled by scanning device paths (/dev/dsks
240 * by default) we are less tolerant since all the existing devices should have
241 * been detected and we want spa_load to return the right error codes.
243 uint64_t zfs_max_missing_tvds_scan
= 0;
246 * Debugging aid that pauses spa_sync() towards the end.
248 static const boolean_t zfs_pause_spa_sync
= B_FALSE
;
251 * Variables to indicate the livelist condense zthr func should wait at certain
252 * points for the livelist to be removed - used to test condense/destroy races
254 static int zfs_livelist_condense_zthr_pause
= 0;
255 static int zfs_livelist_condense_sync_pause
= 0;
258 * Variables to track whether or not condense cancellation has been
259 * triggered in testing.
261 static int zfs_livelist_condense_sync_cancel
= 0;
262 static int zfs_livelist_condense_zthr_cancel
= 0;
265 * Variable to track whether or not extra ALLOC blkptrs were added to a
266 * livelist entry while it was being condensed (caused by the way we track
267 * remapped blkptrs in dbuf_remap_impl)
269 static int zfs_livelist_condense_new_alloc
= 0;
272 * ==========================================================================
273 * SPA properties routines
274 * ==========================================================================
278 * Add a (source=src, propname=propval) list to an nvlist.
281 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, const char *strval
,
282 uint64_t intval
, zprop_source_t src
)
284 const char *propname
= zpool_prop_to_name(prop
);
287 propval
= fnvlist_alloc();
288 fnvlist_add_uint64(propval
, ZPROP_SOURCE
, src
);
291 fnvlist_add_string(propval
, ZPROP_VALUE
, strval
);
293 fnvlist_add_uint64(propval
, ZPROP_VALUE
, intval
);
295 fnvlist_add_nvlist(nvl
, propname
, propval
);
296 nvlist_free(propval
);
300 * Add a user property (source=src, propname=propval) to an nvlist.
303 spa_prop_add_user(nvlist_t
*nvl
, const char *propname
, char *strval
,
308 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
309 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
310 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
311 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
312 nvlist_free(propval
);
316 * Get property values from the spa configuration.
319 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
321 vdev_t
*rvd
= spa
->spa_root_vdev
;
322 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
323 uint64_t size
, alloc
, cap
, version
;
324 const zprop_source_t src
= ZPROP_SRC_NONE
;
325 spa_config_dirent_t
*dp
;
326 metaslab_class_t
*mc
= spa_normal_class(spa
);
328 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
331 alloc
= metaslab_class_get_alloc(mc
);
332 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
333 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
334 alloc
+= metaslab_class_get_alloc(spa_embedded_log_class(spa
));
336 size
= metaslab_class_get_space(mc
);
337 size
+= metaslab_class_get_space(spa_special_class(spa
));
338 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
339 size
+= metaslab_class_get_space(spa_embedded_log_class(spa
));
341 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
342 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
343 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
344 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
346 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
347 spa
->spa_checkpoint_info
.sci_dspace
, src
);
349 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
350 metaslab_class_fragmentation(mc
), src
);
351 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
352 metaslab_class_expandable_space(mc
), src
);
353 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
354 (spa_mode(spa
) == SPA_MODE_READ
), src
);
356 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
357 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
359 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
360 ddt_get_pool_dedup_ratio(spa
), src
);
361 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONEUSED
, NULL
,
362 brt_get_used(spa
), src
);
363 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONESAVED
, NULL
,
364 brt_get_saved(spa
), src
);
365 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONERATIO
, NULL
,
366 brt_get_ratio(spa
), src
);
368 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
369 rvd
->vdev_state
, src
);
371 version
= spa_version(spa
);
372 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
373 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
374 version
, ZPROP_SRC_DEFAULT
);
376 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
377 version
, ZPROP_SRC_LOCAL
);
379 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
380 NULL
, spa_load_guid(spa
), src
);
385 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
386 * when opening pools before this version freedir will be NULL.
388 if (pool
->dp_free_dir
!= NULL
) {
389 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
390 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
393 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
397 if (pool
->dp_leak_dir
!= NULL
) {
398 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
399 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
402 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
407 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
409 if (spa
->spa_comment
!= NULL
) {
410 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
414 if (spa
->spa_compatibility
!= NULL
) {
415 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMPATIBILITY
,
416 spa
->spa_compatibility
, 0, ZPROP_SRC_LOCAL
);
419 if (spa
->spa_root
!= NULL
)
420 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
423 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
424 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
425 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
427 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
428 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
431 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
432 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
433 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
435 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
436 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
439 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
440 if (dp
->scd_path
== NULL
) {
441 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
442 "none", 0, ZPROP_SRC_LOCAL
);
443 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
444 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
445 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
451 * Get zpool property values.
454 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
456 objset_t
*mos
= spa
->spa_meta_objset
;
462 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
466 dp
= spa_get_dsl(spa
);
467 dsl_pool_config_enter(dp
, FTAG
);
468 mutex_enter(&spa
->spa_props_lock
);
471 * Get properties from the spa config.
473 spa_prop_get_config(spa
, nvp
);
475 /* If no pool property object, no more prop to get. */
476 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0)
480 * Get properties from the MOS pool property object.
482 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
483 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
484 zap_cursor_advance(&zc
)) {
487 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
490 if ((prop
= zpool_name_to_prop(za
.za_name
)) ==
491 ZPOOL_PROP_INVAL
&& !zfs_prop_user(za
.za_name
))
494 switch (za
.za_integer_length
) {
496 /* integer property */
497 if (za
.za_first_integer
!=
498 zpool_prop_default_numeric(prop
))
499 src
= ZPROP_SRC_LOCAL
;
501 if (prop
== ZPOOL_PROP_BOOTFS
) {
502 dsl_dataset_t
*ds
= NULL
;
504 err
= dsl_dataset_hold_obj(dp
,
505 za
.za_first_integer
, FTAG
, &ds
);
509 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
511 dsl_dataset_name(ds
, strval
);
512 dsl_dataset_rele(ds
, FTAG
);
515 intval
= za
.za_first_integer
;
518 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
521 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
526 /* string property */
527 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
528 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
529 za
.za_name
, 1, za
.za_num_integers
, strval
);
531 kmem_free(strval
, za
.za_num_integers
);
534 if (prop
!= ZPOOL_PROP_INVAL
) {
535 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
537 src
= ZPROP_SRC_LOCAL
;
538 spa_prop_add_user(*nvp
, za
.za_name
, strval
,
541 kmem_free(strval
, za
.za_num_integers
);
548 zap_cursor_fini(&zc
);
550 mutex_exit(&spa
->spa_props_lock
);
551 dsl_pool_config_exit(dp
, FTAG
);
552 if (err
&& err
!= ENOENT
) {
562 * Validate the given pool properties nvlist and modify the list
563 * for the property values to be set.
566 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
569 int error
= 0, reset_bootfs
= 0;
571 boolean_t has_feature
= B_FALSE
;
574 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
576 const char *strval
, *slash
, *check
, *fname
;
577 const char *propname
= nvpair_name(elem
);
578 zpool_prop_t prop
= zpool_name_to_prop(propname
);
581 case ZPOOL_PROP_INVAL
:
583 * Sanitize the input.
585 if (zfs_prop_user(propname
)) {
586 if (strlen(propname
) >= ZAP_MAXNAMELEN
) {
587 error
= SET_ERROR(ENAMETOOLONG
);
591 if (strlen(fnvpair_value_string(elem
)) >=
593 error
= SET_ERROR(E2BIG
);
596 } else if (zpool_prop_feature(propname
)) {
597 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
598 error
= SET_ERROR(EINVAL
);
602 if (nvpair_value_uint64(elem
, &intval
) != 0) {
603 error
= SET_ERROR(EINVAL
);
608 error
= SET_ERROR(EINVAL
);
612 fname
= strchr(propname
, '@') + 1;
613 if (zfeature_lookup_name(fname
, NULL
) != 0) {
614 error
= SET_ERROR(EINVAL
);
618 has_feature
= B_TRUE
;
620 error
= SET_ERROR(EINVAL
);
625 case ZPOOL_PROP_VERSION
:
626 error
= nvpair_value_uint64(elem
, &intval
);
628 (intval
< spa_version(spa
) ||
629 intval
> SPA_VERSION_BEFORE_FEATURES
||
631 error
= SET_ERROR(EINVAL
);
634 case ZPOOL_PROP_DELEGATION
:
635 case ZPOOL_PROP_AUTOREPLACE
:
636 case ZPOOL_PROP_LISTSNAPS
:
637 case ZPOOL_PROP_AUTOEXPAND
:
638 case ZPOOL_PROP_AUTOTRIM
:
639 error
= nvpair_value_uint64(elem
, &intval
);
640 if (!error
&& intval
> 1)
641 error
= SET_ERROR(EINVAL
);
644 case ZPOOL_PROP_MULTIHOST
:
645 error
= nvpair_value_uint64(elem
, &intval
);
646 if (!error
&& intval
> 1)
647 error
= SET_ERROR(EINVAL
);
650 uint32_t hostid
= zone_get_hostid(NULL
);
652 spa
->spa_hostid
= hostid
;
654 error
= SET_ERROR(ENOTSUP
);
659 case ZPOOL_PROP_BOOTFS
:
661 * If the pool version is less than SPA_VERSION_BOOTFS,
662 * or the pool is still being created (version == 0),
663 * the bootfs property cannot be set.
665 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
666 error
= SET_ERROR(ENOTSUP
);
671 * Make sure the vdev config is bootable
673 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
674 error
= SET_ERROR(ENOTSUP
);
680 error
= nvpair_value_string(elem
, &strval
);
685 if (strval
== NULL
|| strval
[0] == '\0') {
686 objnum
= zpool_prop_default_numeric(
691 error
= dmu_objset_hold(strval
, FTAG
, &os
);
696 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
697 error
= SET_ERROR(ENOTSUP
);
699 objnum
= dmu_objset_id(os
);
701 dmu_objset_rele(os
, FTAG
);
705 case ZPOOL_PROP_FAILUREMODE
:
706 error
= nvpair_value_uint64(elem
, &intval
);
707 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
708 error
= SET_ERROR(EINVAL
);
711 * This is a special case which only occurs when
712 * the pool has completely failed. This allows
713 * the user to change the in-core failmode property
714 * without syncing it out to disk (I/Os might
715 * currently be blocked). We do this by returning
716 * EIO to the caller (spa_prop_set) to trick it
717 * into thinking we encountered a property validation
720 if (!error
&& spa_suspended(spa
)) {
721 spa
->spa_failmode
= intval
;
722 error
= SET_ERROR(EIO
);
726 case ZPOOL_PROP_CACHEFILE
:
727 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
730 if (strval
[0] == '\0')
733 if (strcmp(strval
, "none") == 0)
736 if (strval
[0] != '/') {
737 error
= SET_ERROR(EINVAL
);
741 slash
= strrchr(strval
, '/');
742 ASSERT(slash
!= NULL
);
744 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
745 strcmp(slash
, "/..") == 0)
746 error
= SET_ERROR(EINVAL
);
749 case ZPOOL_PROP_COMMENT
:
750 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
752 for (check
= strval
; *check
!= '\0'; check
++) {
753 if (!isprint(*check
)) {
754 error
= SET_ERROR(EINVAL
);
758 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
759 error
= SET_ERROR(E2BIG
);
770 (void) nvlist_remove_all(props
,
771 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO
));
773 if (!error
&& reset_bootfs
) {
774 error
= nvlist_remove(props
,
775 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
778 error
= nvlist_add_uint64(props
,
779 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
787 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
789 const char *cachefile
;
790 spa_config_dirent_t
*dp
;
792 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
796 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
799 if (cachefile
[0] == '\0')
800 dp
->scd_path
= spa_strdup(spa_config_path
);
801 else if (strcmp(cachefile
, "none") == 0)
804 dp
->scd_path
= spa_strdup(cachefile
);
806 list_insert_head(&spa
->spa_config_list
, dp
);
808 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
812 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
815 nvpair_t
*elem
= NULL
;
816 boolean_t need_sync
= B_FALSE
;
818 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
821 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
822 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
824 if (prop
== ZPOOL_PROP_CACHEFILE
||
825 prop
== ZPOOL_PROP_ALTROOT
||
826 prop
== ZPOOL_PROP_READONLY
)
829 if (prop
== ZPOOL_PROP_INVAL
&&
830 zfs_prop_user(nvpair_name(elem
))) {
835 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
838 if (prop
== ZPOOL_PROP_VERSION
) {
839 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
841 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
842 ver
= SPA_VERSION_FEATURES
;
846 /* Save time if the version is already set. */
847 if (ver
== spa_version(spa
))
851 * In addition to the pool directory object, we might
852 * create the pool properties object, the features for
853 * read object, the features for write object, or the
854 * feature descriptions object.
856 error
= dsl_sync_task(spa
->spa_name
, NULL
,
857 spa_sync_version
, &ver
,
858 6, ZFS_SPACE_CHECK_RESERVED
);
869 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
870 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
877 * If the bootfs property value is dsobj, clear it.
880 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
882 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
883 VERIFY(zap_remove(spa
->spa_meta_objset
,
884 spa
->spa_pool_props_object
,
885 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
891 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
893 uint64_t *newguid __maybe_unused
= arg
;
894 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
895 vdev_t
*rvd
= spa
->spa_root_vdev
;
898 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
899 int error
= (spa_has_checkpoint(spa
)) ?
900 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
901 return (SET_ERROR(error
));
904 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
905 vdev_state
= rvd
->vdev_state
;
906 spa_config_exit(spa
, SCL_STATE
, FTAG
);
908 if (vdev_state
!= VDEV_STATE_HEALTHY
)
909 return (SET_ERROR(ENXIO
));
911 ASSERT3U(spa_guid(spa
), !=, *newguid
);
917 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
919 uint64_t *newguid
= arg
;
920 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
922 vdev_t
*rvd
= spa
->spa_root_vdev
;
924 oldguid
= spa_guid(spa
);
926 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
927 rvd
->vdev_guid
= *newguid
;
928 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
929 vdev_config_dirty(rvd
);
930 spa_config_exit(spa
, SCL_STATE
, FTAG
);
932 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
933 (u_longlong_t
)oldguid
, (u_longlong_t
)*newguid
);
937 * Change the GUID for the pool. This is done so that we can later
938 * re-import a pool built from a clone of our own vdevs. We will modify
939 * the root vdev's guid, our own pool guid, and then mark all of our
940 * vdevs dirty. Note that we must make sure that all our vdevs are
941 * online when we do this, or else any vdevs that weren't present
942 * would be orphaned from our pool. We are also going to issue a
943 * sysevent to update any watchers.
946 spa_change_guid(spa_t
*spa
)
951 mutex_enter(&spa
->spa_vdev_top_lock
);
952 mutex_enter(&spa_namespace_lock
);
953 guid
= spa_generate_guid(NULL
);
955 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
956 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
960 * Clear the kobj flag from all the vdevs to allow
961 * vdev_cache_process_kobj_evt() to post events to all the
962 * vdevs since GUID is updated.
964 vdev_clear_kobj_evt(spa
->spa_root_vdev
);
965 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
966 vdev_clear_kobj_evt(spa
->spa_l2cache
.sav_vdevs
[i
]);
968 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
969 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
972 mutex_exit(&spa_namespace_lock
);
973 mutex_exit(&spa
->spa_vdev_top_lock
);
979 * ==========================================================================
980 * SPA state manipulation (open/create/destroy/import/export)
981 * ==========================================================================
985 spa_error_entry_compare(const void *a
, const void *b
)
987 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
988 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
991 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
992 sizeof (zbookmark_phys_t
));
994 return (TREE_ISIGN(ret
));
998 * Utility function which retrieves copies of the current logs and
999 * re-initializes them in the process.
1002 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
1004 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
1006 memcpy(last
, &spa
->spa_errlist_last
, sizeof (avl_tree_t
));
1007 memcpy(scrub
, &spa
->spa_errlist_scrub
, sizeof (avl_tree_t
));
1009 avl_create(&spa
->spa_errlist_scrub
,
1010 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1011 offsetof(spa_error_entry_t
, se_avl
));
1012 avl_create(&spa
->spa_errlist_last
,
1013 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1014 offsetof(spa_error_entry_t
, se_avl
));
1018 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1020 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
1021 enum zti_modes mode
= ztip
->zti_mode
;
1022 uint_t value
= ztip
->zti_value
;
1023 uint_t count
= ztip
->zti_count
;
1024 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1025 uint_t cpus
, flags
= TASKQ_DYNAMIC
;
1026 boolean_t batch
= B_FALSE
;
1029 case ZTI_MODE_FIXED
:
1030 ASSERT3U(value
, >, 0);
1033 case ZTI_MODE_BATCH
:
1035 flags
|= TASKQ_THREADS_CPU_PCT
;
1036 value
= MIN(zio_taskq_batch_pct
, 100);
1039 case ZTI_MODE_SCALE
:
1040 flags
|= TASKQ_THREADS_CPU_PCT
;
1042 * We want more taskqs to reduce lock contention, but we want
1043 * less for better request ordering and CPU utilization.
1045 cpus
= MAX(1, boot_ncpus
* zio_taskq_batch_pct
/ 100);
1046 if (zio_taskq_batch_tpq
> 0) {
1047 count
= MAX(1, (cpus
+ zio_taskq_batch_tpq
/ 2) /
1048 zio_taskq_batch_tpq
);
1051 * Prefer 6 threads per taskq, but no more taskqs
1052 * than threads in them on large systems. For 80%:
1055 * cpus taskqs percent threads threads
1056 * ------- ------- ------- ------- -------
1067 count
= 1 + cpus
/ 6;
1068 while (count
* count
> cpus
)
1071 /* Limit each taskq within 100% to not trigger assertion. */
1072 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1073 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1077 tqs
->stqs_count
= 0;
1078 tqs
->stqs_taskq
= NULL
;
1082 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
1084 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
1088 ASSERT3U(count
, >, 0);
1089 tqs
->stqs_count
= count
;
1090 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
1092 for (uint_t i
= 0; i
< count
; i
++) {
1097 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
1098 zio_type_name
[t
], zio_taskq_types
[q
], i
);
1100 (void) snprintf(name
, sizeof (name
), "%s_%s",
1101 zio_type_name
[t
], zio_taskq_types
[q
]);
1103 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
1105 flags
|= TASKQ_DC_BATCH
;
1107 (void) zio_taskq_basedc
;
1108 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
1109 spa
->spa_proc
, zio_taskq_basedc
, flags
);
1111 pri_t pri
= maxclsyspri
;
1113 * The write issue taskq can be extremely CPU
1114 * intensive. Run it at slightly less important
1115 * priority than the other taskqs.
1117 * Under Linux and FreeBSD this means incrementing
1118 * the priority value as opposed to platforms like
1119 * illumos where it should be decremented.
1121 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1122 * are equal then a difference between them is
1125 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
) {
1126 #if defined(__linux__)
1128 #elif defined(__FreeBSD__)
1134 tq
= taskq_create_proc(name
, value
, pri
, 50,
1135 INT_MAX
, spa
->spa_proc
, flags
);
1138 tqs
->stqs_taskq
[i
] = tq
;
1143 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1145 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1147 if (tqs
->stqs_taskq
== NULL
) {
1148 ASSERT3U(tqs
->stqs_count
, ==, 0);
1152 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1153 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1154 taskq_destroy(tqs
->stqs_taskq
[i
]);
1157 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1158 tqs
->stqs_taskq
= NULL
;
1162 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1163 * Note that a type may have multiple discrete taskqs to avoid lock contention
1164 * on the taskq itself. In that case we choose which taskq at random by using
1165 * the low bits of gethrtime().
1168 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1169 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1171 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1174 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1175 ASSERT3U(tqs
->stqs_count
, !=, 0);
1177 if (tqs
->stqs_count
== 1) {
1178 tq
= tqs
->stqs_taskq
[0];
1180 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1183 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1187 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1190 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1191 task_func_t
*func
, void *arg
, uint_t flags
)
1193 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1197 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1198 ASSERT3U(tqs
->stqs_count
, !=, 0);
1200 if (tqs
->stqs_count
== 1) {
1201 tq
= tqs
->stqs_taskq
[0];
1203 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1206 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1208 taskq_wait_id(tq
, id
);
1212 spa_create_zio_taskqs(spa_t
*spa
)
1214 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1215 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1216 spa_taskqs_init(spa
, t
, q
);
1222 * Disabled until spa_thread() can be adapted for Linux.
1224 #undef HAVE_SPA_THREAD
1226 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1228 spa_thread(void *arg
)
1230 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1231 callb_cpr_t cprinfo
;
1234 user_t
*pu
= PTOU(curproc
);
1236 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1239 ASSERT(curproc
!= &p0
);
1240 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1241 "zpool-%s", spa
->spa_name
);
1242 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1244 /* bind this thread to the requested psrset */
1245 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1247 mutex_enter(&cpu_lock
);
1248 mutex_enter(&pidlock
);
1249 mutex_enter(&curproc
->p_lock
);
1251 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1252 0, NULL
, NULL
) == 0) {
1253 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1256 "Couldn't bind process for zfs pool \"%s\" to "
1257 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1260 mutex_exit(&curproc
->p_lock
);
1261 mutex_exit(&pidlock
);
1262 mutex_exit(&cpu_lock
);
1266 if (zio_taskq_sysdc
) {
1267 sysdc_thread_enter(curthread
, 100, 0);
1270 spa
->spa_proc
= curproc
;
1271 spa
->spa_did
= curthread
->t_did
;
1273 spa_create_zio_taskqs(spa
);
1275 mutex_enter(&spa
->spa_proc_lock
);
1276 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1278 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1279 cv_broadcast(&spa
->spa_proc_cv
);
1281 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1282 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1283 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1284 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1286 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1287 spa
->spa_proc_state
= SPA_PROC_GONE
;
1288 spa
->spa_proc
= &p0
;
1289 cv_broadcast(&spa
->spa_proc_cv
);
1290 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1292 mutex_enter(&curproc
->p_lock
);
1298 * Activate an uninitialized pool.
1301 spa_activate(spa_t
*spa
, spa_mode_t mode
)
1303 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1305 spa
->spa_state
= POOL_STATE_ACTIVE
;
1306 spa
->spa_mode
= mode
;
1307 spa
->spa_read_spacemaps
= spa_mode_readable_spacemaps
;
1309 spa
->spa_normal_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1310 spa
->spa_log_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1311 spa
->spa_embedded_log_class
=
1312 metaslab_class_create(spa
, &zfs_metaslab_ops
);
1313 spa
->spa_special_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1314 spa
->spa_dedup_class
= metaslab_class_create(spa
, &zfs_metaslab_ops
);
1316 /* Try to create a covering process */
1317 mutex_enter(&spa
->spa_proc_lock
);
1318 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1319 ASSERT(spa
->spa_proc
== &p0
);
1322 (void) spa_create_process
;
1323 #ifdef HAVE_SPA_THREAD
1324 /* Only create a process if we're going to be around a while. */
1325 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1326 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1328 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1329 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1330 cv_wait(&spa
->spa_proc_cv
,
1331 &spa
->spa_proc_lock
);
1333 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1334 ASSERT(spa
->spa_proc
!= &p0
);
1335 ASSERT(spa
->spa_did
!= 0);
1339 "Couldn't create process for zfs pool \"%s\"\n",
1344 #endif /* HAVE_SPA_THREAD */
1345 mutex_exit(&spa
->spa_proc_lock
);
1347 /* If we didn't create a process, we need to create our taskqs. */
1348 if (spa
->spa_proc
== &p0
) {
1349 spa_create_zio_taskqs(spa
);
1352 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1353 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1357 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1358 offsetof(vdev_t
, vdev_config_dirty_node
));
1359 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1360 offsetof(objset_t
, os_evicting_node
));
1361 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1362 offsetof(vdev_t
, vdev_state_dirty_node
));
1364 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1365 offsetof(struct vdev
, vdev_txg_node
));
1367 avl_create(&spa
->spa_errlist_scrub
,
1368 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1369 offsetof(spa_error_entry_t
, se_avl
));
1370 avl_create(&spa
->spa_errlist_last
,
1371 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1372 offsetof(spa_error_entry_t
, se_avl
));
1373 avl_create(&spa
->spa_errlist_healed
,
1374 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1375 offsetof(spa_error_entry_t
, se_avl
));
1377 spa_activate_os(spa
);
1379 spa_keystore_init(&spa
->spa_keystore
);
1382 * This taskq is used to perform zvol-minor-related tasks
1383 * asynchronously. This has several advantages, including easy
1384 * resolution of various deadlocks.
1386 * The taskq must be single threaded to ensure tasks are always
1387 * processed in the order in which they were dispatched.
1389 * A taskq per pool allows one to keep the pools independent.
1390 * This way if one pool is suspended, it will not impact another.
1392 * The preferred location to dispatch a zvol minor task is a sync
1393 * task. In this context, there is easy access to the spa_t and minimal
1394 * error handling is required because the sync task must succeed.
1396 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1400 * Taskq dedicated to prefetcher threads: this is used to prevent the
1401 * pool traverse code from monopolizing the global (and limited)
1402 * system_taskq by inappropriately scheduling long running tasks on it.
1404 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", 100,
1405 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1408 * The taskq to upgrade datasets in this pool. Currently used by
1409 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1411 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", 100,
1412 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1416 * Opposite of spa_activate().
1419 spa_deactivate(spa_t
*spa
)
1421 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1422 ASSERT(spa
->spa_dsl_pool
== NULL
);
1423 ASSERT(spa
->spa_root_vdev
== NULL
);
1424 ASSERT(spa
->spa_async_zio_root
== NULL
);
1425 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1427 spa_evicting_os_wait(spa
);
1429 if (spa
->spa_zvol_taskq
) {
1430 taskq_destroy(spa
->spa_zvol_taskq
);
1431 spa
->spa_zvol_taskq
= NULL
;
1434 if (spa
->spa_prefetch_taskq
) {
1435 taskq_destroy(spa
->spa_prefetch_taskq
);
1436 spa
->spa_prefetch_taskq
= NULL
;
1439 if (spa
->spa_upgrade_taskq
) {
1440 taskq_destroy(spa
->spa_upgrade_taskq
);
1441 spa
->spa_upgrade_taskq
= NULL
;
1444 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1446 list_destroy(&spa
->spa_config_dirty_list
);
1447 list_destroy(&spa
->spa_evicting_os_list
);
1448 list_destroy(&spa
->spa_state_dirty_list
);
1450 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1452 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1453 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1454 spa_taskqs_fini(spa
, t
, q
);
1458 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1459 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1460 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1461 spa
->spa_txg_zio
[i
] = NULL
;
1464 metaslab_class_destroy(spa
->spa_normal_class
);
1465 spa
->spa_normal_class
= NULL
;
1467 metaslab_class_destroy(spa
->spa_log_class
);
1468 spa
->spa_log_class
= NULL
;
1470 metaslab_class_destroy(spa
->spa_embedded_log_class
);
1471 spa
->spa_embedded_log_class
= NULL
;
1473 metaslab_class_destroy(spa
->spa_special_class
);
1474 spa
->spa_special_class
= NULL
;
1476 metaslab_class_destroy(spa
->spa_dedup_class
);
1477 spa
->spa_dedup_class
= NULL
;
1480 * If this was part of an import or the open otherwise failed, we may
1481 * still have errors left in the queues. Empty them just in case.
1483 spa_errlog_drain(spa
);
1484 avl_destroy(&spa
->spa_errlist_scrub
);
1485 avl_destroy(&spa
->spa_errlist_last
);
1486 avl_destroy(&spa
->spa_errlist_healed
);
1488 spa_keystore_fini(&spa
->spa_keystore
);
1490 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1492 mutex_enter(&spa
->spa_proc_lock
);
1493 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1494 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1495 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1496 cv_broadcast(&spa
->spa_proc_cv
);
1497 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1498 ASSERT(spa
->spa_proc
!= &p0
);
1499 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1501 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1502 spa
->spa_proc_state
= SPA_PROC_NONE
;
1504 ASSERT(spa
->spa_proc
== &p0
);
1505 mutex_exit(&spa
->spa_proc_lock
);
1508 * We want to make sure spa_thread() has actually exited the ZFS
1509 * module, so that the module can't be unloaded out from underneath
1512 if (spa
->spa_did
!= 0) {
1513 thread_join(spa
->spa_did
);
1517 spa_deactivate_os(spa
);
1522 * Verify a pool configuration, and construct the vdev tree appropriately. This
1523 * will create all the necessary vdevs in the appropriate layout, with each vdev
1524 * in the CLOSED state. This will prep the pool before open/creation/import.
1525 * All vdev validation is done by the vdev_alloc() routine.
1528 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1529 uint_t id
, int atype
)
1535 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1538 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1541 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1544 if (error
== ENOENT
)
1550 return (SET_ERROR(EINVAL
));
1553 for (int c
= 0; c
< children
; c
++) {
1555 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1563 ASSERT(*vdp
!= NULL
);
1569 spa_should_flush_logs_on_unload(spa_t
*spa
)
1571 if (!spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
))
1574 if (!spa_writeable(spa
))
1577 if (!spa
->spa_sync_on
)
1580 if (spa_state(spa
) != POOL_STATE_EXPORTED
)
1583 if (zfs_keep_log_spacemaps_at_export
)
1590 * Opens a transaction that will set the flag that will instruct
1591 * spa_sync to attempt to flush all the metaslabs for that txg.
1594 spa_unload_log_sm_flush_all(spa_t
*spa
)
1596 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
1597 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
1599 ASSERT3U(spa
->spa_log_flushall_txg
, ==, 0);
1600 spa
->spa_log_flushall_txg
= dmu_tx_get_txg(tx
);
1603 txg_wait_synced(spa_get_dsl(spa
), spa
->spa_log_flushall_txg
);
1607 spa_unload_log_sm_metadata(spa_t
*spa
)
1609 void *cookie
= NULL
;
1611 while ((sls
= avl_destroy_nodes(&spa
->spa_sm_logs_by_txg
,
1612 &cookie
)) != NULL
) {
1613 VERIFY0(sls
->sls_mscount
);
1614 kmem_free(sls
, sizeof (spa_log_sm_t
));
1617 for (log_summary_entry_t
*e
= list_head(&spa
->spa_log_summary
);
1618 e
!= NULL
; e
= list_head(&spa
->spa_log_summary
)) {
1619 VERIFY0(e
->lse_mscount
);
1620 list_remove(&spa
->spa_log_summary
, e
);
1621 kmem_free(e
, sizeof (log_summary_entry_t
));
1624 spa
->spa_unflushed_stats
.sus_nblocks
= 0;
1625 spa
->spa_unflushed_stats
.sus_memused
= 0;
1626 spa
->spa_unflushed_stats
.sus_blocklimit
= 0;
1630 spa_destroy_aux_threads(spa_t
*spa
)
1632 if (spa
->spa_condense_zthr
!= NULL
) {
1633 zthr_destroy(spa
->spa_condense_zthr
);
1634 spa
->spa_condense_zthr
= NULL
;
1636 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1637 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1638 spa
->spa_checkpoint_discard_zthr
= NULL
;
1640 if (spa
->spa_livelist_delete_zthr
!= NULL
) {
1641 zthr_destroy(spa
->spa_livelist_delete_zthr
);
1642 spa
->spa_livelist_delete_zthr
= NULL
;
1644 if (spa
->spa_livelist_condense_zthr
!= NULL
) {
1645 zthr_destroy(spa
->spa_livelist_condense_zthr
);
1646 spa
->spa_livelist_condense_zthr
= NULL
;
1651 * Opposite of spa_load().
1654 spa_unload(spa_t
*spa
)
1656 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1657 ASSERT(spa_state(spa
) != POOL_STATE_UNINITIALIZED
);
1659 spa_import_progress_remove(spa_guid(spa
));
1660 spa_load_note(spa
, "UNLOADING");
1662 spa_wake_waiters(spa
);
1665 * If we have set the spa_final_txg, we have already performed the
1666 * tasks below in spa_export_common(). We should not redo it here since
1667 * we delay the final TXGs beyond what spa_final_txg is set at.
1669 if (spa
->spa_final_txg
== UINT64_MAX
) {
1671 * If the log space map feature is enabled and the pool is
1672 * getting exported (but not destroyed), we want to spend some
1673 * time flushing as many metaslabs as we can in an attempt to
1674 * destroy log space maps and save import time.
1676 if (spa_should_flush_logs_on_unload(spa
))
1677 spa_unload_log_sm_flush_all(spa
);
1682 spa_async_suspend(spa
);
1684 if (spa
->spa_root_vdev
) {
1685 vdev_t
*root_vdev
= spa
->spa_root_vdev
;
1686 vdev_initialize_stop_all(root_vdev
,
1687 VDEV_INITIALIZE_ACTIVE
);
1688 vdev_trim_stop_all(root_vdev
, VDEV_TRIM_ACTIVE
);
1689 vdev_autotrim_stop_all(spa
);
1690 vdev_rebuild_stop_all(spa
);
1697 if (spa
->spa_sync_on
) {
1698 txg_sync_stop(spa
->spa_dsl_pool
);
1699 spa
->spa_sync_on
= B_FALSE
;
1703 * This ensures that there is no async metaslab prefetching
1704 * while we attempt to unload the spa.
1706 if (spa
->spa_root_vdev
!= NULL
) {
1707 for (int c
= 0; c
< spa
->spa_root_vdev
->vdev_children
; c
++) {
1708 vdev_t
*vc
= spa
->spa_root_vdev
->vdev_child
[c
];
1709 if (vc
->vdev_mg
!= NULL
)
1710 taskq_wait(vc
->vdev_mg
->mg_taskq
);
1714 if (spa
->spa_mmp
.mmp_thread
)
1715 mmp_thread_stop(spa
);
1718 * Wait for any outstanding async I/O to complete.
1720 if (spa
->spa_async_zio_root
!= NULL
) {
1721 for (int i
= 0; i
< max_ncpus
; i
++)
1722 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1723 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1724 spa
->spa_async_zio_root
= NULL
;
1727 if (spa
->spa_vdev_removal
!= NULL
) {
1728 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1729 spa
->spa_vdev_removal
= NULL
;
1732 spa_destroy_aux_threads(spa
);
1734 spa_condense_fini(spa
);
1736 bpobj_close(&spa
->spa_deferred_bpobj
);
1738 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1743 if (spa
->spa_root_vdev
)
1744 vdev_free(spa
->spa_root_vdev
);
1745 ASSERT(spa
->spa_root_vdev
== NULL
);
1748 * Close the dsl pool.
1750 if (spa
->spa_dsl_pool
) {
1751 dsl_pool_close(spa
->spa_dsl_pool
);
1752 spa
->spa_dsl_pool
= NULL
;
1753 spa
->spa_meta_objset
= NULL
;
1758 spa_unload_log_sm_metadata(spa
);
1761 * Drop and purge level 2 cache
1763 spa_l2cache_drop(spa
);
1765 if (spa
->spa_spares
.sav_vdevs
) {
1766 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1767 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1768 kmem_free(spa
->spa_spares
.sav_vdevs
,
1769 spa
->spa_spares
.sav_count
* sizeof (void *));
1770 spa
->spa_spares
.sav_vdevs
= NULL
;
1772 if (spa
->spa_spares
.sav_config
) {
1773 nvlist_free(spa
->spa_spares
.sav_config
);
1774 spa
->spa_spares
.sav_config
= NULL
;
1776 spa
->spa_spares
.sav_count
= 0;
1778 if (spa
->spa_l2cache
.sav_vdevs
) {
1779 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1780 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1781 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1783 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1784 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1785 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1787 if (spa
->spa_l2cache
.sav_config
) {
1788 nvlist_free(spa
->spa_l2cache
.sav_config
);
1789 spa
->spa_l2cache
.sav_config
= NULL
;
1791 spa
->spa_l2cache
.sav_count
= 0;
1793 spa
->spa_async_suspended
= 0;
1795 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1797 if (spa
->spa_comment
!= NULL
) {
1798 spa_strfree(spa
->spa_comment
);
1799 spa
->spa_comment
= NULL
;
1801 if (spa
->spa_compatibility
!= NULL
) {
1802 spa_strfree(spa
->spa_compatibility
);
1803 spa
->spa_compatibility
= NULL
;
1806 spa_config_exit(spa
, SCL_ALL
, spa
);
1810 * Load (or re-load) the current list of vdevs describing the active spares for
1811 * this pool. When this is called, we have some form of basic information in
1812 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1813 * then re-generate a more complete list including status information.
1816 spa_load_spares(spa_t
*spa
)
1825 * zdb opens both the current state of the pool and the
1826 * checkpointed state (if present), with a different spa_t.
1828 * As spare vdevs are shared among open pools, we skip loading
1829 * them when we load the checkpointed state of the pool.
1831 if (!spa_writeable(spa
))
1835 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1838 * First, close and free any existing spare vdevs.
1840 if (spa
->spa_spares
.sav_vdevs
) {
1841 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1842 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1844 /* Undo the call to spa_activate() below */
1845 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1846 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1847 spa_spare_remove(tvd
);
1852 kmem_free(spa
->spa_spares
.sav_vdevs
,
1853 spa
->spa_spares
.sav_count
* sizeof (void *));
1856 if (spa
->spa_spares
.sav_config
== NULL
)
1859 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1860 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
1862 spa
->spa_spares
.sav_count
= (int)nspares
;
1863 spa
->spa_spares
.sav_vdevs
= NULL
;
1869 * Construct the array of vdevs, opening them to get status in the
1870 * process. For each spare, there is potentially two different vdev_t
1871 * structures associated with it: one in the list of spares (used only
1872 * for basic validation purposes) and one in the active vdev
1873 * configuration (if it's spared in). During this phase we open and
1874 * validate each vdev on the spare list. If the vdev also exists in the
1875 * active configuration, then we also mark this vdev as an active spare.
1877 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1879 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1880 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1881 VDEV_ALLOC_SPARE
) == 0);
1884 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1886 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1887 B_FALSE
)) != NULL
) {
1888 if (!tvd
->vdev_isspare
)
1892 * We only mark the spare active if we were successfully
1893 * able to load the vdev. Otherwise, importing a pool
1894 * with a bad active spare would result in strange
1895 * behavior, because multiple pool would think the spare
1896 * is actively in use.
1898 * There is a vulnerability here to an equally bizarre
1899 * circumstance, where a dead active spare is later
1900 * brought back to life (onlined or otherwise). Given
1901 * the rarity of this scenario, and the extra complexity
1902 * it adds, we ignore the possibility.
1904 if (!vdev_is_dead(tvd
))
1905 spa_spare_activate(tvd
);
1909 vd
->vdev_aux
= &spa
->spa_spares
;
1911 if (vdev_open(vd
) != 0)
1914 if (vdev_validate_aux(vd
) == 0)
1919 * Recompute the stashed list of spares, with status information
1922 fnvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
);
1924 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1926 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1927 spares
[i
] = vdev_config_generate(spa
,
1928 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1929 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1930 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
1931 spa
->spa_spares
.sav_count
);
1932 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1933 nvlist_free(spares
[i
]);
1934 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1938 * Load (or re-load) the current list of vdevs describing the active l2cache for
1939 * this pool. When this is called, we have some form of basic information in
1940 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1941 * then re-generate a more complete list including status information.
1942 * Devices which are already active have their details maintained, and are
1946 spa_load_l2cache(spa_t
*spa
)
1948 nvlist_t
**l2cache
= NULL
;
1950 int i
, j
, oldnvdevs
;
1952 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1953 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1957 * zdb opens both the current state of the pool and the
1958 * checkpointed state (if present), with a different spa_t.
1960 * As L2 caches are part of the ARC which is shared among open
1961 * pools, we skip loading them when we load the checkpointed
1962 * state of the pool.
1964 if (!spa_writeable(spa
))
1968 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1970 oldvdevs
= sav
->sav_vdevs
;
1971 oldnvdevs
= sav
->sav_count
;
1972 sav
->sav_vdevs
= NULL
;
1975 if (sav
->sav_config
== NULL
) {
1981 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
,
1982 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
1983 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1986 * Process new nvlist of vdevs.
1988 for (i
= 0; i
< nl2cache
; i
++) {
1989 guid
= fnvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
);
1992 for (j
= 0; j
< oldnvdevs
; j
++) {
1994 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1996 * Retain previous vdev for add/remove ops.
2004 if (newvdevs
[i
] == NULL
) {
2008 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
2009 VDEV_ALLOC_L2CACHE
) == 0);
2014 * Commit this vdev as an l2cache device,
2015 * even if it fails to open.
2017 spa_l2cache_add(vd
);
2022 spa_l2cache_activate(vd
);
2024 if (vdev_open(vd
) != 0)
2027 (void) vdev_validate_aux(vd
);
2029 if (!vdev_is_dead(vd
))
2030 l2arc_add_vdev(spa
, vd
);
2033 * Upon cache device addition to a pool or pool
2034 * creation with a cache device or if the header
2035 * of the device is invalid we issue an async
2036 * TRIM command for the whole device which will
2037 * execute if l2arc_trim_ahead > 0.
2039 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2043 sav
->sav_vdevs
= newvdevs
;
2044 sav
->sav_count
= (int)nl2cache
;
2047 * Recompute the stashed list of l2cache devices, with status
2048 * information this time.
2050 fnvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
);
2052 if (sav
->sav_count
> 0)
2053 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
2055 for (i
= 0; i
< sav
->sav_count
; i
++)
2056 l2cache
[i
] = vdev_config_generate(spa
,
2057 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
2058 fnvlist_add_nvlist_array(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
2059 (const nvlist_t
* const *)l2cache
, sav
->sav_count
);
2063 * Purge vdevs that were dropped
2066 for (i
= 0; i
< oldnvdevs
; i
++) {
2071 ASSERT(vd
->vdev_isl2cache
);
2073 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
2074 pool
!= 0ULL && l2arc_vdev_present(vd
))
2075 l2arc_remove_vdev(vd
);
2076 vdev_clear_stats(vd
);
2081 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
2084 for (i
= 0; i
< sav
->sav_count
; i
++)
2085 nvlist_free(l2cache
[i
]);
2087 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
2091 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
2094 char *packed
= NULL
;
2099 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
2103 nvsize
= *(uint64_t *)db
->db_data
;
2104 dmu_buf_rele(db
, FTAG
);
2106 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
2107 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
2110 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
2111 vmem_free(packed
, nvsize
);
2117 * Concrete top-level vdevs that are not missing and are not logs. At every
2118 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
2121 spa_healthy_core_tvds(spa_t
*spa
)
2123 vdev_t
*rvd
= spa
->spa_root_vdev
;
2126 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
2127 vdev_t
*vd
= rvd
->vdev_child
[i
];
2130 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
2138 * Checks to see if the given vdev could not be opened, in which case we post a
2139 * sysevent to notify the autoreplace code that the device has been removed.
2142 spa_check_removed(vdev_t
*vd
)
2144 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2145 spa_check_removed(vd
->vdev_child
[c
]);
2147 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
2148 vdev_is_concrete(vd
)) {
2149 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
2150 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
2155 spa_check_for_missing_logs(spa_t
*spa
)
2157 vdev_t
*rvd
= spa
->spa_root_vdev
;
2160 * If we're doing a normal import, then build up any additional
2161 * diagnostic information about missing log devices.
2162 * We'll pass this up to the user for further processing.
2164 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
2165 nvlist_t
**child
, *nv
;
2168 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
2170 nv
= fnvlist_alloc();
2172 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2173 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2176 * We consider a device as missing only if it failed
2177 * to open (i.e. offline or faulted is not considered
2180 if (tvd
->vdev_islog
&&
2181 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2182 child
[idx
++] = vdev_config_generate(spa
, tvd
,
2183 B_FALSE
, VDEV_CONFIG_MISSING
);
2188 fnvlist_add_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2189 (const nvlist_t
* const *)child
, idx
);
2190 fnvlist_add_nvlist(spa
->spa_load_info
,
2191 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
2193 for (uint64_t i
= 0; i
< idx
; i
++)
2194 nvlist_free(child
[i
]);
2197 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
2200 spa_load_failed(spa
, "some log devices are missing");
2201 vdev_dbgmsg_print_tree(rvd
, 2);
2202 return (SET_ERROR(ENXIO
));
2205 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2206 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2208 if (tvd
->vdev_islog
&&
2209 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2210 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2211 spa_load_note(spa
, "some log devices are "
2212 "missing, ZIL is dropped.");
2213 vdev_dbgmsg_print_tree(rvd
, 2);
2223 * Check for missing log devices
2226 spa_check_logs(spa_t
*spa
)
2228 boolean_t rv
= B_FALSE
;
2229 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2231 switch (spa
->spa_log_state
) {
2234 case SPA_LOG_MISSING
:
2235 /* need to recheck in case slog has been restored */
2236 case SPA_LOG_UNKNOWN
:
2237 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2238 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
2240 spa_set_log_state(spa
, SPA_LOG_MISSING
);
2247 * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2250 spa_passivate_log(spa_t
*spa
)
2252 vdev_t
*rvd
= spa
->spa_root_vdev
;
2253 boolean_t slog_found
= B_FALSE
;
2255 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2257 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2258 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2260 if (tvd
->vdev_islog
) {
2261 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2262 metaslab_group_passivate(tvd
->vdev_mg
);
2263 slog_found
= B_TRUE
;
2267 return (slog_found
);
2271 * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2274 spa_activate_log(spa_t
*spa
)
2276 vdev_t
*rvd
= spa
->spa_root_vdev
;
2278 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2280 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2281 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2283 if (tvd
->vdev_islog
) {
2284 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2285 metaslab_group_activate(tvd
->vdev_mg
);
2291 spa_reset_logs(spa_t
*spa
)
2295 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2296 NULL
, DS_FIND_CHILDREN
);
2299 * We successfully offlined the log device, sync out the
2300 * current txg so that the "stubby" block can be removed
2303 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2309 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2311 for (int i
= 0; i
< sav
->sav_count
; i
++)
2312 spa_check_removed(sav
->sav_vdevs
[i
]);
2316 spa_claim_notify(zio_t
*zio
)
2318 spa_t
*spa
= zio
->io_spa
;
2323 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2324 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2325 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2326 mutex_exit(&spa
->spa_props_lock
);
2329 typedef struct spa_load_error
{
2330 boolean_t sle_verify_data
;
2331 uint64_t sle_meta_count
;
2332 uint64_t sle_data_count
;
2336 spa_load_verify_done(zio_t
*zio
)
2338 blkptr_t
*bp
= zio
->io_bp
;
2339 spa_load_error_t
*sle
= zio
->io_private
;
2340 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2341 int error
= zio
->io_error
;
2342 spa_t
*spa
= zio
->io_spa
;
2344 abd_free(zio
->io_abd
);
2346 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2347 type
!= DMU_OT_INTENT_LOG
)
2348 atomic_inc_64(&sle
->sle_meta_count
);
2350 atomic_inc_64(&sle
->sle_data_count
);
2353 mutex_enter(&spa
->spa_scrub_lock
);
2354 spa
->spa_load_verify_bytes
-= BP_GET_PSIZE(bp
);
2355 cv_broadcast(&spa
->spa_scrub_io_cv
);
2356 mutex_exit(&spa
->spa_scrub_lock
);
2360 * Maximum number of inflight bytes is the log2 fraction of the arc size.
2361 * By default, we set it to 1/16th of the arc.
2363 static uint_t spa_load_verify_shift
= 4;
2364 static int spa_load_verify_metadata
= B_TRUE
;
2365 static int spa_load_verify_data
= B_TRUE
;
2368 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2369 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2372 spa_load_error_t
*sle
= rio
->io_private
;
2374 (void) zilog
, (void) dnp
;
2377 * Note: normally this routine will not be called if
2378 * spa_load_verify_metadata is not set. However, it may be useful
2379 * to manually set the flag after the traversal has begun.
2381 if (!spa_load_verify_metadata
)
2385 * Sanity check the block pointer in order to detect obvious damage
2386 * before using the contents in subsequent checks or in zio_read().
2387 * When damaged consider it to be a metadata error since we cannot
2388 * trust the BP_GET_TYPE and BP_GET_LEVEL values.
2390 if (!zfs_blkptr_verify(spa
, bp
, BLK_CONFIG_NEEDED
, BLK_VERIFY_LOG
)) {
2391 atomic_inc_64(&sle
->sle_meta_count
);
2395 if (zb
->zb_level
== ZB_DNODE_LEVEL
|| BP_IS_HOLE(bp
) ||
2396 BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
2399 if (!BP_IS_METADATA(bp
) &&
2400 (!spa_load_verify_data
|| !sle
->sle_verify_data
))
2403 uint64_t maxinflight_bytes
=
2404 arc_target_bytes() >> spa_load_verify_shift
;
2405 size_t size
= BP_GET_PSIZE(bp
);
2407 mutex_enter(&spa
->spa_scrub_lock
);
2408 while (spa
->spa_load_verify_bytes
>= maxinflight_bytes
)
2409 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2410 spa
->spa_load_verify_bytes
+= size
;
2411 mutex_exit(&spa
->spa_scrub_lock
);
2413 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2414 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2415 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2416 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2421 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2423 (void) dp
, (void) arg
;
2425 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2426 return (SET_ERROR(ENAMETOOLONG
));
2432 spa_load_verify(spa_t
*spa
)
2435 spa_load_error_t sle
= { 0 };
2436 zpool_load_policy_t policy
;
2437 boolean_t verify_ok
= B_FALSE
;
2440 zpool_get_load_policy(spa
->spa_config
, &policy
);
2442 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
||
2443 policy
.zlp_maxmeta
== UINT64_MAX
)
2446 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2447 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2448 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2450 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2455 * Verify data only if we are rewinding or error limit was set.
2456 * Otherwise nothing except dbgmsg care about it to waste time.
2458 sle
.sle_verify_data
= (policy
.zlp_rewind
& ZPOOL_REWIND_MASK
) ||
2459 (policy
.zlp_maxdata
< UINT64_MAX
);
2461 rio
= zio_root(spa
, NULL
, &sle
,
2462 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2464 if (spa_load_verify_metadata
) {
2465 if (spa
->spa_extreme_rewind
) {
2466 spa_load_note(spa
, "performing a complete scan of the "
2467 "pool since extreme rewind is on. This may take "
2468 "a very long time.\n (spa_load_verify_data=%u, "
2469 "spa_load_verify_metadata=%u)",
2470 spa_load_verify_data
, spa_load_verify_metadata
);
2473 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2474 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2475 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2478 (void) zio_wait(rio
);
2479 ASSERT0(spa
->spa_load_verify_bytes
);
2481 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2482 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2484 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2485 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2486 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2487 (u_longlong_t
)sle
.sle_data_count
);
2490 if (spa_load_verify_dryrun
||
2491 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2492 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2496 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2497 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2499 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2500 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_LOAD_TIME
,
2501 spa
->spa_load_txg_ts
);
2502 fnvlist_add_int64(spa
->spa_load_info
, ZPOOL_CONFIG_REWIND_TIME
,
2504 fnvlist_add_uint64(spa
->spa_load_info
,
2505 ZPOOL_CONFIG_LOAD_META_ERRORS
, sle
.sle_meta_count
);
2506 fnvlist_add_uint64(spa
->spa_load_info
,
2507 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
);
2509 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2512 if (spa_load_verify_dryrun
)
2516 if (error
!= ENXIO
&& error
!= EIO
)
2517 error
= SET_ERROR(EIO
);
2521 return (verify_ok
? 0 : EIO
);
2525 * Find a value in the pool props object.
2528 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2530 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2531 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2535 * Find a value in the pool directory object.
2538 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2540 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2541 name
, sizeof (uint64_t), 1, val
);
2543 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2544 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2545 "[error=%d]", name
, error
);
2552 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2554 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2555 return (SET_ERROR(err
));
2559 spa_livelist_delete_check(spa_t
*spa
)
2561 return (spa
->spa_livelists_to_delete
!= 0);
2565 spa_livelist_delete_cb_check(void *arg
, zthr_t
*z
)
2569 return (spa_livelist_delete_check(spa
));
2573 delete_blkptr_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2576 zio_free(spa
, tx
->tx_txg
, bp
);
2577 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
2578 -bp_get_dsize_sync(spa
, bp
),
2579 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
2584 dsl_get_next_livelist_obj(objset_t
*os
, uint64_t zap_obj
, uint64_t *llp
)
2589 zap_cursor_init(&zc
, os
, zap_obj
);
2590 err
= zap_cursor_retrieve(&zc
, &za
);
2591 zap_cursor_fini(&zc
);
2593 *llp
= za
.za_first_integer
;
2598 * Components of livelist deletion that must be performed in syncing
2599 * context: freeing block pointers and updating the pool-wide data
2600 * structures to indicate how much work is left to do
2602 typedef struct sublist_delete_arg
{
2607 } sublist_delete_arg_t
;
2610 sublist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2612 sublist_delete_arg_t
*sda
= arg
;
2613 spa_t
*spa
= sda
->spa
;
2614 dsl_deadlist_t
*ll
= sda
->ll
;
2615 uint64_t key
= sda
->key
;
2616 bplist_t
*to_free
= sda
->to_free
;
2618 bplist_iterate(to_free
, delete_blkptr_cb
, spa
, tx
);
2619 dsl_deadlist_remove_entry(ll
, key
, tx
);
2622 typedef struct livelist_delete_arg
{
2626 } livelist_delete_arg_t
;
2629 livelist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2631 livelist_delete_arg_t
*lda
= arg
;
2632 spa_t
*spa
= lda
->spa
;
2633 uint64_t ll_obj
= lda
->ll_obj
;
2634 uint64_t zap_obj
= lda
->zap_obj
;
2635 objset_t
*mos
= spa
->spa_meta_objset
;
2638 /* free the livelist and decrement the feature count */
2639 VERIFY0(zap_remove_int(mos
, zap_obj
, ll_obj
, tx
));
2640 dsl_deadlist_free(mos
, ll_obj
, tx
);
2641 spa_feature_decr(spa
, SPA_FEATURE_LIVELIST
, tx
);
2642 VERIFY0(zap_count(mos
, zap_obj
, &count
));
2644 /* no more livelists to delete */
2645 VERIFY0(zap_remove(mos
, DMU_POOL_DIRECTORY_OBJECT
,
2646 DMU_POOL_DELETED_CLONES
, tx
));
2647 VERIFY0(zap_destroy(mos
, zap_obj
, tx
));
2648 spa
->spa_livelists_to_delete
= 0;
2649 spa_notify_waiters(spa
);
2654 * Load in the value for the livelist to be removed and open it. Then,
2655 * load its first sublist and determine which block pointers should actually
2656 * be freed. Then, call a synctask which performs the actual frees and updates
2657 * the pool-wide livelist data.
2660 spa_livelist_delete_cb(void *arg
, zthr_t
*z
)
2663 uint64_t ll_obj
= 0, count
;
2664 objset_t
*mos
= spa
->spa_meta_objset
;
2665 uint64_t zap_obj
= spa
->spa_livelists_to_delete
;
2667 * Determine the next livelist to delete. This function should only
2668 * be called if there is at least one deleted clone.
2670 VERIFY0(dsl_get_next_livelist_obj(mos
, zap_obj
, &ll_obj
));
2671 VERIFY0(zap_count(mos
, ll_obj
, &count
));
2674 dsl_deadlist_entry_t
*dle
;
2676 ll
= kmem_zalloc(sizeof (dsl_deadlist_t
), KM_SLEEP
);
2677 dsl_deadlist_open(ll
, mos
, ll_obj
);
2678 dle
= dsl_deadlist_first(ll
);
2679 ASSERT3P(dle
, !=, NULL
);
2680 bplist_create(&to_free
);
2681 int err
= dsl_process_sub_livelist(&dle
->dle_bpobj
, &to_free
,
2684 sublist_delete_arg_t sync_arg
= {
2687 .key
= dle
->dle_mintxg
,
2690 zfs_dbgmsg("deleting sublist (id %llu) from"
2691 " livelist %llu, %lld remaining",
2692 (u_longlong_t
)dle
->dle_bpobj
.bpo_object
,
2693 (u_longlong_t
)ll_obj
, (longlong_t
)count
- 1);
2694 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
2695 sublist_delete_sync
, &sync_arg
, 0,
2696 ZFS_SPACE_CHECK_DESTROY
));
2698 VERIFY3U(err
, ==, EINTR
);
2700 bplist_clear(&to_free
);
2701 bplist_destroy(&to_free
);
2702 dsl_deadlist_close(ll
);
2703 kmem_free(ll
, sizeof (dsl_deadlist_t
));
2705 livelist_delete_arg_t sync_arg
= {
2710 zfs_dbgmsg("deletion of livelist %llu completed",
2711 (u_longlong_t
)ll_obj
);
2712 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
, livelist_delete_sync
,
2713 &sync_arg
, 0, ZFS_SPACE_CHECK_DESTROY
));
2718 spa_start_livelist_destroy_thread(spa_t
*spa
)
2720 ASSERT3P(spa
->spa_livelist_delete_zthr
, ==, NULL
);
2721 spa
->spa_livelist_delete_zthr
=
2722 zthr_create("z_livelist_destroy",
2723 spa_livelist_delete_cb_check
, spa_livelist_delete_cb
, spa
,
2727 typedef struct livelist_new_arg
{
2730 } livelist_new_arg_t
;
2733 livelist_track_new_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
2737 livelist_new_arg_t
*lna
= arg
;
2739 bplist_append(lna
->frees
, bp
);
2741 bplist_append(lna
->allocs
, bp
);
2742 zfs_livelist_condense_new_alloc
++;
2747 typedef struct livelist_condense_arg
{
2750 uint64_t first_size
;
2752 } livelist_condense_arg_t
;
2755 spa_livelist_condense_sync(void *arg
, dmu_tx_t
*tx
)
2757 livelist_condense_arg_t
*lca
= arg
;
2758 spa_t
*spa
= lca
->spa
;
2760 dsl_dataset_t
*ds
= spa
->spa_to_condense
.ds
;
2762 /* Have we been cancelled? */
2763 if (spa
->spa_to_condense
.cancelled
) {
2764 zfs_livelist_condense_sync_cancel
++;
2768 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2769 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2770 dsl_deadlist_t
*ll
= &ds
->ds_dir
->dd_livelist
;
2773 * It's possible that the livelist was changed while the zthr was
2774 * running. Therefore, we need to check for new blkptrs in the two
2775 * entries being condensed and continue to track them in the livelist.
2776 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
2777 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
2778 * we need to sort them into two different bplists.
2780 uint64_t first_obj
= first
->dle_bpobj
.bpo_object
;
2781 uint64_t next_obj
= next
->dle_bpobj
.bpo_object
;
2782 uint64_t cur_first_size
= first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2783 uint64_t cur_next_size
= next
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2785 bplist_create(&new_frees
);
2786 livelist_new_arg_t new_bps
= {
2787 .allocs
= &lca
->to_keep
,
2788 .frees
= &new_frees
,
2791 if (cur_first_size
> lca
->first_size
) {
2792 VERIFY0(livelist_bpobj_iterate_from_nofree(&first
->dle_bpobj
,
2793 livelist_track_new_cb
, &new_bps
, lca
->first_size
));
2795 if (cur_next_size
> lca
->next_size
) {
2796 VERIFY0(livelist_bpobj_iterate_from_nofree(&next
->dle_bpobj
,
2797 livelist_track_new_cb
, &new_bps
, lca
->next_size
));
2800 dsl_deadlist_clear_entry(first
, ll
, tx
);
2801 ASSERT(bpobj_is_empty(&first
->dle_bpobj
));
2802 dsl_deadlist_remove_entry(ll
, next
->dle_mintxg
, tx
);
2804 bplist_iterate(&lca
->to_keep
, dsl_deadlist_insert_alloc_cb
, ll
, tx
);
2805 bplist_iterate(&new_frees
, dsl_deadlist_insert_free_cb
, ll
, tx
);
2806 bplist_destroy(&new_frees
);
2808 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
2809 dsl_dataset_name(ds
, dsname
);
2810 zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
2811 "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
2812 "(%llu blkptrs)", (u_longlong_t
)tx
->tx_txg
, dsname
,
2813 (u_longlong_t
)ds
->ds_object
, (u_longlong_t
)first_obj
,
2814 (u_longlong_t
)cur_first_size
, (u_longlong_t
)next_obj
,
2815 (u_longlong_t
)cur_next_size
,
2816 (u_longlong_t
)first
->dle_bpobj
.bpo_object
,
2817 (u_longlong_t
)first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
);
2819 dmu_buf_rele(ds
->ds_dbuf
, spa
);
2820 spa
->spa_to_condense
.ds
= NULL
;
2821 bplist_clear(&lca
->to_keep
);
2822 bplist_destroy(&lca
->to_keep
);
2823 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2824 spa
->spa_to_condense
.syncing
= B_FALSE
;
2828 spa_livelist_condense_cb(void *arg
, zthr_t
*t
)
2830 while (zfs_livelist_condense_zthr_pause
&&
2831 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2835 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2836 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2837 uint64_t first_size
, next_size
;
2839 livelist_condense_arg_t
*lca
=
2840 kmem_alloc(sizeof (livelist_condense_arg_t
), KM_SLEEP
);
2841 bplist_create(&lca
->to_keep
);
2844 * Process the livelists (matching FREEs and ALLOCs) in open context
2845 * so we have minimal work in syncing context to condense.
2847 * We save bpobj sizes (first_size and next_size) to use later in
2848 * syncing context to determine if entries were added to these sublists
2849 * while in open context. This is possible because the clone is still
2850 * active and open for normal writes and we want to make sure the new,
2851 * unprocessed blockpointers are inserted into the livelist normally.
2853 * Note that dsl_process_sub_livelist() both stores the size number of
2854 * blockpointers and iterates over them while the bpobj's lock held, so
2855 * the sizes returned to us are consistent which what was actually
2858 int err
= dsl_process_sub_livelist(&first
->dle_bpobj
, &lca
->to_keep
, t
,
2861 err
= dsl_process_sub_livelist(&next
->dle_bpobj
, &lca
->to_keep
,
2865 while (zfs_livelist_condense_sync_pause
&&
2866 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2869 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
2870 dmu_tx_mark_netfree(tx
);
2871 dmu_tx_hold_space(tx
, 1);
2872 err
= dmu_tx_assign(tx
, TXG_NOWAIT
| TXG_NOTHROTTLE
);
2875 * Prevent the condense zthr restarting before
2876 * the synctask completes.
2878 spa
->spa_to_condense
.syncing
= B_TRUE
;
2880 lca
->first_size
= first_size
;
2881 lca
->next_size
= next_size
;
2882 dsl_sync_task_nowait(spa_get_dsl(spa
),
2883 spa_livelist_condense_sync
, lca
, tx
);
2889 * Condensing can not continue: either it was externally stopped or
2890 * we were unable to assign to a tx because the pool has run out of
2891 * space. In the second case, we'll just end up trying to condense
2892 * again in a later txg.
2895 bplist_clear(&lca
->to_keep
);
2896 bplist_destroy(&lca
->to_keep
);
2897 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2898 dmu_buf_rele(spa
->spa_to_condense
.ds
->ds_dbuf
, spa
);
2899 spa
->spa_to_condense
.ds
= NULL
;
2901 zfs_livelist_condense_zthr_cancel
++;
2905 * Check that there is something to condense but that a condense is not
2906 * already in progress and that condensing has not been cancelled.
2909 spa_livelist_condense_cb_check(void *arg
, zthr_t
*z
)
2913 if ((spa
->spa_to_condense
.ds
!= NULL
) &&
2914 (spa
->spa_to_condense
.syncing
== B_FALSE
) &&
2915 (spa
->spa_to_condense
.cancelled
== B_FALSE
)) {
2922 spa_start_livelist_condensing_thread(spa_t
*spa
)
2924 spa
->spa_to_condense
.ds
= NULL
;
2925 spa
->spa_to_condense
.first
= NULL
;
2926 spa
->spa_to_condense
.next
= NULL
;
2927 spa
->spa_to_condense
.syncing
= B_FALSE
;
2928 spa
->spa_to_condense
.cancelled
= B_FALSE
;
2930 ASSERT3P(spa
->spa_livelist_condense_zthr
, ==, NULL
);
2931 spa
->spa_livelist_condense_zthr
=
2932 zthr_create("z_livelist_condense",
2933 spa_livelist_condense_cb_check
,
2934 spa_livelist_condense_cb
, spa
, minclsyspri
);
2938 spa_spawn_aux_threads(spa_t
*spa
)
2940 ASSERT(spa_writeable(spa
));
2942 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2944 spa_start_indirect_condensing_thread(spa
);
2945 spa_start_livelist_destroy_thread(spa
);
2946 spa_start_livelist_condensing_thread(spa
);
2948 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
2949 spa
->spa_checkpoint_discard_zthr
=
2950 zthr_create("z_checkpoint_discard",
2951 spa_checkpoint_discard_thread_check
,
2952 spa_checkpoint_discard_thread
, spa
, minclsyspri
);
2956 * Fix up config after a partly-completed split. This is done with the
2957 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2958 * pool have that entry in their config, but only the splitting one contains
2959 * a list of all the guids of the vdevs that are being split off.
2961 * This function determines what to do with that list: either rejoin
2962 * all the disks to the pool, or complete the splitting process. To attempt
2963 * the rejoin, each disk that is offlined is marked online again, and
2964 * we do a reopen() call. If the vdev label for every disk that was
2965 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2966 * then we call vdev_split() on each disk, and complete the split.
2968 * Otherwise we leave the config alone, with all the vdevs in place in
2969 * the original pool.
2972 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2979 boolean_t attempt_reopen
;
2981 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2984 /* check that the config is complete */
2985 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2986 &glist
, &gcount
) != 0)
2989 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2991 /* attempt to online all the vdevs & validate */
2992 attempt_reopen
= B_TRUE
;
2993 for (i
= 0; i
< gcount
; i
++) {
2994 if (glist
[i
] == 0) /* vdev is hole */
2997 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2998 if (vd
[i
] == NULL
) {
3000 * Don't bother attempting to reopen the disks;
3001 * just do the split.
3003 attempt_reopen
= B_FALSE
;
3005 /* attempt to re-online it */
3006 vd
[i
]->vdev_offline
= B_FALSE
;
3010 if (attempt_reopen
) {
3011 vdev_reopen(spa
->spa_root_vdev
);
3013 /* check each device to see what state it's in */
3014 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
3015 if (vd
[i
] != NULL
&&
3016 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
3023 * If every disk has been moved to the new pool, or if we never
3024 * even attempted to look at them, then we split them off for
3027 if (!attempt_reopen
|| gcount
== extracted
) {
3028 for (i
= 0; i
< gcount
; i
++)
3031 vdev_reopen(spa
->spa_root_vdev
);
3034 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
3038 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
3040 const char *ereport
= FM_EREPORT_ZFS_POOL
;
3043 spa
->spa_load_state
= state
;
3044 (void) spa_import_progress_set_state(spa_guid(spa
),
3045 spa_load_state(spa
));
3047 gethrestime(&spa
->spa_loaded_ts
);
3048 error
= spa_load_impl(spa
, type
, &ereport
);
3051 * Don't count references from objsets that are already closed
3052 * and are making their way through the eviction process.
3054 spa_evicting_os_wait(spa
);
3055 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
3057 if (error
!= EEXIST
) {
3058 spa
->spa_loaded_ts
.tv_sec
= 0;
3059 spa
->spa_loaded_ts
.tv_nsec
= 0;
3061 if (error
!= EBADF
) {
3062 (void) zfs_ereport_post(ereport
, spa
,
3063 NULL
, NULL
, NULL
, 0);
3066 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
3069 (void) spa_import_progress_set_state(spa_guid(spa
),
3070 spa_load_state(spa
));
3077 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
3078 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
3079 * spa's per-vdev ZAP list.
3082 vdev_count_verify_zaps(vdev_t
*vd
)
3084 spa_t
*spa
= vd
->vdev_spa
;
3087 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
) &&
3088 vd
->vdev_root_zap
!= 0) {
3090 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3091 spa
->spa_all_vdev_zaps
, vd
->vdev_root_zap
));
3093 if (vd
->vdev_top_zap
!= 0) {
3095 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3096 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
3098 if (vd
->vdev_leaf_zap
!= 0) {
3100 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3101 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
3104 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3105 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
3111 #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0)
3115 * Determine whether the activity check is required.
3118 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
3122 uint64_t hostid
= 0;
3123 uint64_t tryconfig_txg
= 0;
3124 uint64_t tryconfig_timestamp
= 0;
3125 uint16_t tryconfig_mmp_seq
= 0;
3128 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3129 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3130 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
3132 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
3133 &tryconfig_timestamp
);
3134 (void) nvlist_lookup_uint16(nvinfo
, ZPOOL_CONFIG_MMP_SEQ
,
3135 &tryconfig_mmp_seq
);
3138 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
3141 * Disable the MMP activity check - This is used by zdb which
3142 * is intended to be used on potentially active pools.
3144 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
3148 * Skip the activity check when the MMP feature is disabled.
3150 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
3154 * If the tryconfig_ values are nonzero, they are the results of an
3155 * earlier tryimport. If they all match the uberblock we just found,
3156 * then the pool has not changed and we return false so we do not test
3159 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
3160 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
&&
3161 tryconfig_mmp_seq
&& tryconfig_mmp_seq
==
3162 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0))
3166 * Allow the activity check to be skipped when importing the pool
3167 * on the same host which last imported it. Since the hostid from
3168 * configuration may be stale use the one read from the label.
3170 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
3171 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
3173 if (hostid
== spa_get_hostid(spa
))
3177 * Skip the activity test when the pool was cleanly exported.
3179 if (state
!= POOL_STATE_ACTIVE
)
3186 * Nanoseconds the activity check must watch for changes on-disk.
3189 spa_activity_check_duration(spa_t
*spa
, uberblock_t
*ub
)
3191 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
3192 uint64_t multihost_interval
= MSEC2NSEC(
3193 MMP_INTERVAL_OK(zfs_multihost_interval
));
3194 uint64_t import_delay
= MAX(NANOSEC
, import_intervals
*
3195 multihost_interval
);
3198 * Local tunables determine a minimum duration except for the case
3199 * where we know when the remote host will suspend the pool if MMP
3200 * writes do not land.
3202 * See Big Theory comment at the top of mmp.c for the reasoning behind
3203 * these cases and times.
3206 ASSERT(MMP_IMPORT_SAFETY_FACTOR
>= 100);
3208 if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3209 MMP_FAIL_INT(ub
) > 0) {
3211 /* MMP on remote host will suspend pool after failed writes */
3212 import_delay
= MMP_FAIL_INT(ub
) * MSEC2NSEC(MMP_INTERVAL(ub
)) *
3213 MMP_IMPORT_SAFETY_FACTOR
/ 100;
3215 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3216 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
3217 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3218 (u_longlong_t
)MMP_FAIL_INT(ub
),
3219 (u_longlong_t
)MMP_INTERVAL(ub
),
3220 (u_longlong_t
)import_intervals
);
3222 } else if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3223 MMP_FAIL_INT(ub
) == 0) {
3225 /* MMP on remote host will never suspend pool */
3226 import_delay
= MAX(import_delay
, (MSEC2NSEC(MMP_INTERVAL(ub
)) +
3227 ub
->ub_mmp_delay
) * import_intervals
);
3229 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3230 "mmp_interval=%llu ub_mmp_delay=%llu "
3231 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3232 (u_longlong_t
)MMP_INTERVAL(ub
),
3233 (u_longlong_t
)ub
->ub_mmp_delay
,
3234 (u_longlong_t
)import_intervals
);
3236 } else if (MMP_VALID(ub
)) {
3238 * zfs-0.7 compatibility case
3241 import_delay
= MAX(import_delay
, (multihost_interval
+
3242 ub
->ub_mmp_delay
) * import_intervals
);
3244 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
3245 "import_intervals=%llu leaves=%u",
3246 (u_longlong_t
)import_delay
,
3247 (u_longlong_t
)ub
->ub_mmp_delay
,
3248 (u_longlong_t
)import_intervals
,
3249 vdev_count_leaves(spa
));
3251 /* Using local tunings is the only reasonable option */
3252 zfs_dbgmsg("pool last imported on non-MMP aware "
3253 "host using import_delay=%llu multihost_interval=%llu "
3254 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3255 (u_longlong_t
)multihost_interval
,
3256 (u_longlong_t
)import_intervals
);
3259 return (import_delay
);
3263 * Perform the import activity check. If the user canceled the import or
3264 * we detected activity then fail.
3267 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
3269 uint64_t txg
= ub
->ub_txg
;
3270 uint64_t timestamp
= ub
->ub_timestamp
;
3271 uint64_t mmp_config
= ub
->ub_mmp_config
;
3272 uint16_t mmp_seq
= MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0;
3273 uint64_t import_delay
;
3274 hrtime_t import_expire
;
3275 nvlist_t
*mmp_label
= NULL
;
3276 vdev_t
*rvd
= spa
->spa_root_vdev
;
3281 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
3282 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3286 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3287 * during the earlier tryimport. If the txg recorded there is 0 then
3288 * the pool is known to be active on another host.
3290 * Otherwise, the pool might be in use on another host. Check for
3291 * changes in the uberblocks on disk if necessary.
3293 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3294 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
3295 ZPOOL_CONFIG_LOAD_INFO
);
3297 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
3298 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
3299 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3300 error
= SET_ERROR(EREMOTEIO
);
3305 import_delay
= spa_activity_check_duration(spa
, ub
);
3307 /* Add a small random factor in case of simultaneous imports (0-25%) */
3308 import_delay
+= import_delay
* random_in_range(250) / 1000;
3310 import_expire
= gethrtime() + import_delay
;
3312 while (gethrtime() < import_expire
) {
3313 (void) spa_import_progress_set_mmp_check(spa_guid(spa
),
3314 NSEC2SEC(import_expire
- gethrtime()));
3316 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3318 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
||
3319 mmp_seq
!= (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0)) {
3320 zfs_dbgmsg("multihost activity detected "
3321 "txg %llu ub_txg %llu "
3322 "timestamp %llu ub_timestamp %llu "
3323 "mmp_config %#llx ub_mmp_config %#llx",
3324 (u_longlong_t
)txg
, (u_longlong_t
)ub
->ub_txg
,
3325 (u_longlong_t
)timestamp
,
3326 (u_longlong_t
)ub
->ub_timestamp
,
3327 (u_longlong_t
)mmp_config
,
3328 (u_longlong_t
)ub
->ub_mmp_config
);
3330 error
= SET_ERROR(EREMOTEIO
);
3335 nvlist_free(mmp_label
);
3339 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
3341 error
= SET_ERROR(EINTR
);
3349 mutex_destroy(&mtx
);
3353 * If the pool is determined to be active store the status in the
3354 * spa->spa_load_info nvlist. If the remote hostname or hostid are
3355 * available from configuration read from disk store them as well.
3356 * This allows 'zpool import' to generate a more useful message.
3358 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
3359 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3360 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
3362 if (error
== EREMOTEIO
) {
3363 const char *hostname
= "<unknown>";
3364 uint64_t hostid
= 0;
3367 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
3368 hostname
= fnvlist_lookup_string(mmp_label
,
3369 ZPOOL_CONFIG_HOSTNAME
);
3370 fnvlist_add_string(spa
->spa_load_info
,
3371 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
3374 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
3375 hostid
= fnvlist_lookup_uint64(mmp_label
,
3376 ZPOOL_CONFIG_HOSTID
);
3377 fnvlist_add_uint64(spa
->spa_load_info
,
3378 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
3382 fnvlist_add_uint64(spa
->spa_load_info
,
3383 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
3384 fnvlist_add_uint64(spa
->spa_load_info
,
3385 ZPOOL_CONFIG_MMP_TXG
, 0);
3387 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
3391 nvlist_free(mmp_label
);
3397 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
3400 const char *hostname
;
3401 uint64_t myhostid
= 0;
3403 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
3404 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
3405 hostname
= fnvlist_lookup_string(mos_config
,
3406 ZPOOL_CONFIG_HOSTNAME
);
3408 myhostid
= zone_get_hostid(NULL
);
3410 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
3411 cmn_err(CE_WARN
, "pool '%s' could not be "
3412 "loaded as it was last accessed by "
3413 "another system (host: %s hostid: 0x%llx). "
3414 "See: https://openzfs.github.io/openzfs-docs/msg/"
3416 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
3417 spa_load_failed(spa
, "hostid verification failed: pool "
3418 "last accessed by host: %s (hostid: 0x%llx)",
3419 hostname
, (u_longlong_t
)hostid
);
3420 return (SET_ERROR(EBADF
));
3428 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
3431 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
3435 const char *comment
;
3436 const char *compatibility
;
3439 * Versioning wasn't explicitly added to the label until later, so if
3440 * it's not present treat it as the initial version.
3442 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
3443 &spa
->spa_ubsync
.ub_version
) != 0)
3444 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
3446 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
3447 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3448 ZPOOL_CONFIG_POOL_GUID
);
3449 return (SET_ERROR(EINVAL
));
3453 * If we are doing an import, ensure that the pool is not already
3454 * imported by checking if its pool guid already exists in the
3457 * The only case that we allow an already imported pool to be
3458 * imported again, is when the pool is checkpointed and we want to
3459 * look at its checkpointed state from userland tools like zdb.
3462 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3463 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3464 spa_guid_exists(pool_guid
, 0)) {
3466 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3467 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3468 spa_guid_exists(pool_guid
, 0) &&
3469 !spa_importing_readonly_checkpoint(spa
)) {
3471 spa_load_failed(spa
, "a pool with guid %llu is already open",
3472 (u_longlong_t
)pool_guid
);
3473 return (SET_ERROR(EEXIST
));
3476 spa
->spa_config_guid
= pool_guid
;
3478 nvlist_free(spa
->spa_load_info
);
3479 spa
->spa_load_info
= fnvlist_alloc();
3481 ASSERT(spa
->spa_comment
== NULL
);
3482 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3483 spa
->spa_comment
= spa_strdup(comment
);
3485 ASSERT(spa
->spa_compatibility
== NULL
);
3486 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMPATIBILITY
,
3487 &compatibility
) == 0)
3488 spa
->spa_compatibility
= spa_strdup(compatibility
);
3490 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
3491 &spa
->spa_config_txg
);
3493 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
3494 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
3496 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
3497 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3498 ZPOOL_CONFIG_VDEV_TREE
);
3499 return (SET_ERROR(EINVAL
));
3503 * Create "The Godfather" zio to hold all async IOs
3505 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3507 for (int i
= 0; i
< max_ncpus
; i
++) {
3508 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3509 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3510 ZIO_FLAG_GODFATHER
);
3514 * Parse the configuration into a vdev tree. We explicitly set the
3515 * value that will be returned by spa_version() since parsing the
3516 * configuration requires knowing the version number.
3518 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3519 parse
= (type
== SPA_IMPORT_EXISTING
?
3520 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
3521 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
3522 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3525 spa_load_failed(spa
, "unable to parse config [error=%d]",
3530 ASSERT(spa
->spa_root_vdev
== rvd
);
3531 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
3532 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
3534 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3535 ASSERT(spa_guid(spa
) == pool_guid
);
3542 * Recursively open all vdevs in the vdev tree. This function is called twice:
3543 * first with the untrusted config, then with the trusted config.
3546 spa_ld_open_vdevs(spa_t
*spa
)
3551 * spa_missing_tvds_allowed defines how many top-level vdevs can be
3552 * missing/unopenable for the root vdev to be still considered openable.
3554 if (spa
->spa_trust_config
) {
3555 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
3556 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
3557 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
3558 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
3559 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
3561 spa
->spa_missing_tvds_allowed
= 0;
3564 spa
->spa_missing_tvds_allowed
=
3565 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
3567 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3568 error
= vdev_open(spa
->spa_root_vdev
);
3569 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3571 if (spa
->spa_missing_tvds
!= 0) {
3572 spa_load_note(spa
, "vdev tree has %lld missing top-level "
3573 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
3574 if (spa
->spa_trust_config
&& (spa
->spa_mode
& SPA_MODE_WRITE
)) {
3576 * Although theoretically we could allow users to open
3577 * incomplete pools in RW mode, we'd need to add a lot
3578 * of extra logic (e.g. adjust pool space to account
3579 * for missing vdevs).
3580 * This limitation also prevents users from accidentally
3581 * opening the pool in RW mode during data recovery and
3582 * damaging it further.
3584 spa_load_note(spa
, "pools with missing top-level "
3585 "vdevs can only be opened in read-only mode.");
3586 error
= SET_ERROR(ENXIO
);
3588 spa_load_note(spa
, "current settings allow for maximum "
3589 "%lld missing top-level vdevs at this stage.",
3590 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
3594 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
3597 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
3598 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
3604 * We need to validate the vdev labels against the configuration that
3605 * we have in hand. This function is called twice: first with an untrusted
3606 * config, then with a trusted config. The validation is more strict when the
3607 * config is trusted.
3610 spa_ld_validate_vdevs(spa_t
*spa
)
3613 vdev_t
*rvd
= spa
->spa_root_vdev
;
3615 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3616 error
= vdev_validate(rvd
);
3617 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3620 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
3624 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
3625 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
3627 vdev_dbgmsg_print_tree(rvd
, 2);
3628 return (SET_ERROR(ENXIO
));
3635 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
3637 spa
->spa_state
= POOL_STATE_ACTIVE
;
3638 spa
->spa_ubsync
= spa
->spa_uberblock
;
3639 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
3640 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
3641 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
3642 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
3643 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
3644 spa
->spa_prev_software_version
= ub
->ub_software_version
;
3648 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
3650 vdev_t
*rvd
= spa
->spa_root_vdev
;
3652 uberblock_t
*ub
= &spa
->spa_uberblock
;
3653 boolean_t activity_check
= B_FALSE
;
3656 * If we are opening the checkpointed state of the pool by
3657 * rewinding to it, at this point we will have written the
3658 * checkpointed uberblock to the vdev labels, so searching
3659 * the labels will find the right uberblock. However, if
3660 * we are opening the checkpointed state read-only, we have
3661 * not modified the labels. Therefore, we must ignore the
3662 * labels and continue using the spa_uberblock that was set
3663 * by spa_ld_checkpoint_rewind.
3665 * Note that it would be fine to ignore the labels when
3666 * rewinding (opening writeable) as well. However, if we
3667 * crash just after writing the labels, we will end up
3668 * searching the labels. Doing so in the common case means
3669 * that this code path gets exercised normally, rather than
3670 * just in the edge case.
3672 if (ub
->ub_checkpoint_txg
!= 0 &&
3673 spa_importing_readonly_checkpoint(spa
)) {
3674 spa_ld_select_uberblock_done(spa
, ub
);
3679 * Find the best uberblock.
3681 vdev_uberblock_load(rvd
, ub
, &label
);
3684 * If we weren't able to find a single valid uberblock, return failure.
3686 if (ub
->ub_txg
== 0) {
3688 spa_load_failed(spa
, "no valid uberblock found");
3689 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
3692 if (spa
->spa_load_max_txg
!= UINT64_MAX
) {
3693 (void) spa_import_progress_set_max_txg(spa_guid(spa
),
3694 (u_longlong_t
)spa
->spa_load_max_txg
);
3696 spa_load_note(spa
, "using uberblock with txg=%llu",
3697 (u_longlong_t
)ub
->ub_txg
);
3701 * For pools which have the multihost property on determine if the
3702 * pool is truly inactive and can be safely imported. Prevent
3703 * hosts which don't have a hostid set from importing the pool.
3705 activity_check
= spa_activity_check_required(spa
, ub
, label
,
3707 if (activity_check
) {
3708 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
3709 spa_get_hostid(spa
) == 0) {
3711 fnvlist_add_uint64(spa
->spa_load_info
,
3712 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3713 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3716 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
3722 fnvlist_add_uint64(spa
->spa_load_info
,
3723 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
3724 fnvlist_add_uint64(spa
->spa_load_info
,
3725 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
3726 fnvlist_add_uint16(spa
->spa_load_info
,
3727 ZPOOL_CONFIG_MMP_SEQ
,
3728 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0));
3732 * If the pool has an unsupported version we can't open it.
3734 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
3736 spa_load_failed(spa
, "version %llu is not supported",
3737 (u_longlong_t
)ub
->ub_version
);
3738 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
3741 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3745 * If we weren't able to find what's necessary for reading the
3746 * MOS in the label, return failure.
3748 if (label
== NULL
) {
3749 spa_load_failed(spa
, "label config unavailable");
3750 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3754 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
3757 spa_load_failed(spa
, "invalid label: '%s' missing",
3758 ZPOOL_CONFIG_FEATURES_FOR_READ
);
3759 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3764 * Update our in-core representation with the definitive values
3767 nvlist_free(spa
->spa_label_features
);
3768 spa
->spa_label_features
= fnvlist_dup(features
);
3774 * Look through entries in the label nvlist's features_for_read. If
3775 * there is a feature listed there which we don't understand then we
3776 * cannot open a pool.
3778 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3779 nvlist_t
*unsup_feat
;
3781 unsup_feat
= fnvlist_alloc();
3783 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
3785 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
3786 if (!zfeature_is_supported(nvpair_name(nvp
))) {
3787 fnvlist_add_string(unsup_feat
,
3788 nvpair_name(nvp
), "");
3792 if (!nvlist_empty(unsup_feat
)) {
3793 fnvlist_add_nvlist(spa
->spa_load_info
,
3794 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3795 nvlist_free(unsup_feat
);
3796 spa_load_failed(spa
, "some features are unsupported");
3797 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3801 nvlist_free(unsup_feat
);
3804 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
3805 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3806 spa_try_repair(spa
, spa
->spa_config
);
3807 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3808 nvlist_free(spa
->spa_config_splitting
);
3809 spa
->spa_config_splitting
= NULL
;
3813 * Initialize internal SPA structures.
3815 spa_ld_select_uberblock_done(spa
, ub
);
3821 spa_ld_open_rootbp(spa_t
*spa
)
3824 vdev_t
*rvd
= spa
->spa_root_vdev
;
3826 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
3828 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
3829 "[error=%d]", error
);
3830 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3832 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
3838 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3839 boolean_t reloading
)
3841 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
3842 nvlist_t
*nv
, *mos_config
, *policy
;
3843 int error
= 0, copy_error
;
3844 uint64_t healthy_tvds
, healthy_tvds_mos
;
3845 uint64_t mos_config_txg
;
3847 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
3849 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3852 * If we're assembling a pool from a split, the config provided is
3853 * already trusted so there is nothing to do.
3855 if (type
== SPA_IMPORT_ASSEMBLE
)
3858 healthy_tvds
= spa_healthy_core_tvds(spa
);
3860 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
3862 spa_load_failed(spa
, "unable to retrieve MOS config");
3863 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3867 * If we are doing an open, pool owner wasn't verified yet, thus do
3868 * the verification here.
3870 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
3871 error
= spa_verify_host(spa
, mos_config
);
3873 nvlist_free(mos_config
);
3878 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
3880 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3883 * Build a new vdev tree from the trusted config
3885 error
= spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
);
3887 nvlist_free(mos_config
);
3888 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3889 spa_load_failed(spa
, "spa_config_parse failed [error=%d]",
3891 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3895 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3896 * obtained by scanning /dev/dsk, then it will have the right vdev
3897 * paths. We update the trusted MOS config with this information.
3898 * We first try to copy the paths with vdev_copy_path_strict, which
3899 * succeeds only when both configs have exactly the same vdev tree.
3900 * If that fails, we fall back to a more flexible method that has a
3901 * best effort policy.
3903 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3904 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3905 spa_load_note(spa
, "provided vdev tree:");
3906 vdev_dbgmsg_print_tree(rvd
, 2);
3907 spa_load_note(spa
, "MOS vdev tree:");
3908 vdev_dbgmsg_print_tree(mrvd
, 2);
3910 if (copy_error
!= 0) {
3911 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3912 "back to vdev_copy_path_relaxed");
3913 vdev_copy_path_relaxed(rvd
, mrvd
);
3918 spa
->spa_root_vdev
= mrvd
;
3920 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3923 * We will use spa_config if we decide to reload the spa or if spa_load
3924 * fails and we rewind. We must thus regenerate the config using the
3925 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3926 * pass settings on how to load the pool and is not stored in the MOS.
3927 * We copy it over to our new, trusted config.
3929 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3930 ZPOOL_CONFIG_POOL_TXG
);
3931 nvlist_free(mos_config
);
3932 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3933 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
3935 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
3936 spa_config_set(spa
, mos_config
);
3937 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3940 * Now that we got the config from the MOS, we should be more strict
3941 * in checking blkptrs and can make assumptions about the consistency
3942 * of the vdev tree. spa_trust_config must be set to true before opening
3943 * vdevs in order for them to be writeable.
3945 spa
->spa_trust_config
= B_TRUE
;
3948 * Open and validate the new vdev tree
3950 error
= spa_ld_open_vdevs(spa
);
3954 error
= spa_ld_validate_vdevs(spa
);
3958 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3959 spa_load_note(spa
, "final vdev tree:");
3960 vdev_dbgmsg_print_tree(rvd
, 2);
3963 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3964 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3966 * Sanity check to make sure that we are indeed loading the
3967 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
3968 * in the config provided and they happened to be the only ones
3969 * to have the latest uberblock, we could involuntarily perform
3970 * an extreme rewind.
3972 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
3973 if (healthy_tvds_mos
- healthy_tvds
>=
3974 SPA_SYNC_MIN_VDEVS
) {
3975 spa_load_note(spa
, "config provided misses too many "
3976 "top-level vdevs compared to MOS (%lld vs %lld). ",
3977 (u_longlong_t
)healthy_tvds
,
3978 (u_longlong_t
)healthy_tvds_mos
);
3979 spa_load_note(spa
, "vdev tree:");
3980 vdev_dbgmsg_print_tree(rvd
, 2);
3982 spa_load_failed(spa
, "config was already "
3983 "provided from MOS. Aborting.");
3984 return (spa_vdev_err(rvd
,
3985 VDEV_AUX_CORRUPT_DATA
, EIO
));
3987 spa_load_note(spa
, "spa must be reloaded using MOS "
3989 return (SET_ERROR(EAGAIN
));
3993 error
= spa_check_for_missing_logs(spa
);
3995 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
3997 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
3998 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
3999 "guid sum (%llu != %llu)",
4000 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
4001 (u_longlong_t
)rvd
->vdev_guid_sum
);
4002 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
4010 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
4013 vdev_t
*rvd
= spa
->spa_root_vdev
;
4016 * Everything that we read before spa_remove_init() must be stored
4017 * on concreted vdevs. Therefore we do this as early as possible.
4019 error
= spa_remove_init(spa
);
4021 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
4023 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4027 * Retrieve information needed to condense indirect vdev mappings.
4029 error
= spa_condense_init(spa
);
4031 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
4033 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4040 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
4043 vdev_t
*rvd
= spa
->spa_root_vdev
;
4045 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
4046 boolean_t missing_feat_read
= B_FALSE
;
4047 nvlist_t
*unsup_feat
, *enabled_feat
;
4049 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
4050 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
4051 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4054 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
4055 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
4056 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4059 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
4060 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
4061 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4064 enabled_feat
= fnvlist_alloc();
4065 unsup_feat
= fnvlist_alloc();
4067 if (!spa_features_check(spa
, B_FALSE
,
4068 unsup_feat
, enabled_feat
))
4069 missing_feat_read
= B_TRUE
;
4071 if (spa_writeable(spa
) ||
4072 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
4073 if (!spa_features_check(spa
, B_TRUE
,
4074 unsup_feat
, enabled_feat
)) {
4075 *missing_feat_writep
= B_TRUE
;
4079 fnvlist_add_nvlist(spa
->spa_load_info
,
4080 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
4082 if (!nvlist_empty(unsup_feat
)) {
4083 fnvlist_add_nvlist(spa
->spa_load_info
,
4084 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4087 fnvlist_free(enabled_feat
);
4088 fnvlist_free(unsup_feat
);
4090 if (!missing_feat_read
) {
4091 fnvlist_add_boolean(spa
->spa_load_info
,
4092 ZPOOL_CONFIG_CAN_RDONLY
);
4096 * If the state is SPA_LOAD_TRYIMPORT, our objective is
4097 * twofold: to determine whether the pool is available for
4098 * import in read-write mode and (if it is not) whether the
4099 * pool is available for import in read-only mode. If the pool
4100 * is available for import in read-write mode, it is displayed
4101 * as available in userland; if it is not available for import
4102 * in read-only mode, it is displayed as unavailable in
4103 * userland. If the pool is available for import in read-only
4104 * mode but not read-write mode, it is displayed as unavailable
4105 * in userland with a special note that the pool is actually
4106 * available for open in read-only mode.
4108 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
4109 * missing a feature for write, we must first determine whether
4110 * the pool can be opened read-only before returning to
4111 * userland in order to know whether to display the
4112 * abovementioned note.
4114 if (missing_feat_read
|| (*missing_feat_writep
&&
4115 spa_writeable(spa
))) {
4116 spa_load_failed(spa
, "pool uses unsupported features");
4117 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4122 * Load refcounts for ZFS features from disk into an in-memory
4123 * cache during SPA initialization.
4125 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
4128 error
= feature_get_refcount_from_disk(spa
,
4129 &spa_feature_table
[i
], &refcount
);
4131 spa
->spa_feat_refcount_cache
[i
] = refcount
;
4132 } else if (error
== ENOTSUP
) {
4133 spa
->spa_feat_refcount_cache
[i
] =
4134 SPA_FEATURE_DISABLED
;
4136 spa_load_failed(spa
, "error getting refcount "
4137 "for feature %s [error=%d]",
4138 spa_feature_table
[i
].fi_guid
, error
);
4139 return (spa_vdev_err(rvd
,
4140 VDEV_AUX_CORRUPT_DATA
, EIO
));
4145 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
4146 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
4147 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
4148 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4152 * Encryption was added before bookmark_v2, even though bookmark_v2
4153 * is now a dependency. If this pool has encryption enabled without
4154 * bookmark_v2, trigger an errata message.
4156 if (spa_feature_is_enabled(spa
, SPA_FEATURE_ENCRYPTION
) &&
4157 !spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
4158 spa
->spa_errata
= ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
4165 spa_ld_load_special_directories(spa_t
*spa
)
4168 vdev_t
*rvd
= spa
->spa_root_vdev
;
4170 spa
->spa_is_initializing
= B_TRUE
;
4171 error
= dsl_pool_open(spa
->spa_dsl_pool
);
4172 spa
->spa_is_initializing
= B_FALSE
;
4174 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
4175 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4182 spa_ld_get_props(spa_t
*spa
)
4186 vdev_t
*rvd
= spa
->spa_root_vdev
;
4188 /* Grab the checksum salt from the MOS. */
4189 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4190 DMU_POOL_CHECKSUM_SALT
, 1,
4191 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
4192 spa
->spa_cksum_salt
.zcs_bytes
);
4193 if (error
== ENOENT
) {
4194 /* Generate a new salt for subsequent use */
4195 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4196 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4197 } else if (error
!= 0) {
4198 spa_load_failed(spa
, "unable to retrieve checksum salt from "
4199 "MOS [error=%d]", error
);
4200 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4203 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
4204 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4205 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
4207 spa_load_failed(spa
, "error opening deferred-frees bpobj "
4208 "[error=%d]", error
);
4209 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4213 * Load the bit that tells us to use the new accounting function
4214 * (raid-z deflation). If we have an older pool, this will not
4217 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
4218 if (error
!= 0 && error
!= ENOENT
)
4219 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4221 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
4222 &spa
->spa_creation_version
, B_FALSE
);
4223 if (error
!= 0 && error
!= ENOENT
)
4224 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4227 * Load the persistent error log. If we have an older pool, this will
4230 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
4232 if (error
!= 0 && error
!= ENOENT
)
4233 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4235 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
4236 &spa
->spa_errlog_scrub
, B_FALSE
);
4237 if (error
!= 0 && error
!= ENOENT
)
4238 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4241 * Load the livelist deletion field. If a livelist is queued for
4242 * deletion, indicate that in the spa
4244 error
= spa_dir_prop(spa
, DMU_POOL_DELETED_CLONES
,
4245 &spa
->spa_livelists_to_delete
, B_FALSE
);
4246 if (error
!= 0 && error
!= ENOENT
)
4247 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4250 * Load the history object. If we have an older pool, this
4251 * will not be present.
4253 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
4254 if (error
!= 0 && error
!= ENOENT
)
4255 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4258 * Load the per-vdev ZAP map. If we have an older pool, this will not
4259 * be present; in this case, defer its creation to a later time to
4260 * avoid dirtying the MOS this early / out of sync context. See
4261 * spa_sync_config_object.
4264 /* The sentinel is only available in the MOS config. */
4265 nvlist_t
*mos_config
;
4266 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
4267 spa_load_failed(spa
, "unable to retrieve MOS config");
4268 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4271 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
4272 &spa
->spa_all_vdev_zaps
, B_FALSE
);
4274 if (error
== ENOENT
) {
4275 VERIFY(!nvlist_exists(mos_config
,
4276 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
4277 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
4278 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4279 } else if (error
!= 0) {
4280 nvlist_free(mos_config
);
4281 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4282 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
4284 * An older version of ZFS overwrote the sentinel value, so
4285 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4286 * destruction to later; see spa_sync_config_object.
4288 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
4290 * We're assuming that no vdevs have had their ZAPs created
4291 * before this. Better be sure of it.
4293 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4295 nvlist_free(mos_config
);
4297 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4299 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
4301 if (error
&& error
!= ENOENT
)
4302 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4305 uint64_t autoreplace
= 0;
4307 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
4308 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
4309 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
4310 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
4311 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
4312 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
4313 spa_prop_find(spa
, ZPOOL_PROP_AUTOTRIM
, &spa
->spa_autotrim
);
4314 spa
->spa_autoreplace
= (autoreplace
!= 0);
4318 * If we are importing a pool with missing top-level vdevs,
4319 * we enforce that the pool doesn't panic or get suspended on
4320 * error since the likelihood of missing data is extremely high.
4322 if (spa
->spa_missing_tvds
> 0 &&
4323 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
4324 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4325 spa_load_note(spa
, "forcing failmode to 'continue' "
4326 "as some top level vdevs are missing");
4327 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
4334 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
4337 vdev_t
*rvd
= spa
->spa_root_vdev
;
4340 * If we're assembling the pool from the split-off vdevs of
4341 * an existing pool, we don't want to attach the spares & cache
4346 * Load any hot spares for this pool.
4348 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
4350 if (error
!= 0 && error
!= ENOENT
)
4351 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4352 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4353 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
4354 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
4355 &spa
->spa_spares
.sav_config
) != 0) {
4356 spa_load_failed(spa
, "error loading spares nvlist");
4357 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4360 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4361 spa_load_spares(spa
);
4362 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4363 } else if (error
== 0) {
4364 spa
->spa_spares
.sav_sync
= B_TRUE
;
4368 * Load any level 2 ARC devices for this pool.
4370 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
4371 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
4372 if (error
!= 0 && error
!= ENOENT
)
4373 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4374 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4375 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
4376 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
4377 &spa
->spa_l2cache
.sav_config
) != 0) {
4378 spa_load_failed(spa
, "error loading l2cache nvlist");
4379 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4382 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4383 spa_load_l2cache(spa
);
4384 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4385 } else if (error
== 0) {
4386 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4393 spa_ld_load_vdev_metadata(spa_t
*spa
)
4396 vdev_t
*rvd
= spa
->spa_root_vdev
;
4399 * If the 'multihost' property is set, then never allow a pool to
4400 * be imported when the system hostid is zero. The exception to
4401 * this rule is zdb which is always allowed to access pools.
4403 if (spa_multihost(spa
) && spa_get_hostid(spa
) == 0 &&
4404 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
4405 fnvlist_add_uint64(spa
->spa_load_info
,
4406 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4407 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4411 * If the 'autoreplace' property is set, then post a resource notifying
4412 * the ZFS DE that it should not issue any faults for unopenable
4413 * devices. We also iterate over the vdevs, and post a sysevent for any
4414 * unopenable vdevs so that the normal autoreplace handler can take
4417 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4418 spa_check_removed(spa
->spa_root_vdev
);
4420 * For the import case, this is done in spa_import(), because
4421 * at this point we're using the spare definitions from
4422 * the MOS config, not necessarily from the userland config.
4424 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
4425 spa_aux_check_removed(&spa
->spa_spares
);
4426 spa_aux_check_removed(&spa
->spa_l2cache
);
4431 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
4433 error
= vdev_load(rvd
);
4435 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
4436 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4439 error
= spa_ld_log_spacemaps(spa
);
4441 spa_load_failed(spa
, "spa_ld_log_spacemaps failed [error=%d]",
4443 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4447 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
4449 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4450 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
, B_FALSE
);
4451 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4457 spa_ld_load_dedup_tables(spa_t
*spa
)
4460 vdev_t
*rvd
= spa
->spa_root_vdev
;
4462 error
= ddt_load(spa
);
4464 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
4465 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4472 spa_ld_load_brt(spa_t
*spa
)
4475 vdev_t
*rvd
= spa
->spa_root_vdev
;
4477 error
= brt_load(spa
);
4479 spa_load_failed(spa
, "brt_load failed [error=%d]", error
);
4480 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4487 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4489 vdev_t
*rvd
= spa
->spa_root_vdev
;
4491 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
4492 boolean_t missing
= spa_check_logs(spa
);
4494 if (spa
->spa_missing_tvds
!= 0) {
4495 spa_load_note(spa
, "spa_check_logs failed "
4496 "so dropping the logs");
4498 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
4499 spa_load_failed(spa
, "spa_check_logs failed");
4500 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
4510 spa_ld_verify_pool_data(spa_t
*spa
)
4513 vdev_t
*rvd
= spa
->spa_root_vdev
;
4516 * We've successfully opened the pool, verify that we're ready
4517 * to start pushing transactions.
4519 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4520 error
= spa_load_verify(spa
);
4522 spa_load_failed(spa
, "spa_load_verify failed "
4523 "[error=%d]", error
);
4524 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4533 spa_ld_claim_log_blocks(spa_t
*spa
)
4536 dsl_pool_t
*dp
= spa_get_dsl(spa
);
4539 * Claim log blocks that haven't been committed yet.
4540 * This must all happen in a single txg.
4541 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
4542 * invoked from zil_claim_log_block()'s i/o done callback.
4543 * Price of rollback is that we abandon the log.
4545 spa
->spa_claiming
= B_TRUE
;
4547 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
4548 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
4549 zil_claim
, tx
, DS_FIND_CHILDREN
);
4552 spa
->spa_claiming
= B_FALSE
;
4554 spa_set_log_state(spa
, SPA_LOG_GOOD
);
4558 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
4559 boolean_t update_config_cache
)
4561 vdev_t
*rvd
= spa
->spa_root_vdev
;
4562 int need_update
= B_FALSE
;
4565 * If the config cache is stale, or we have uninitialized
4566 * metaslabs (see spa_vdev_add()), then update the config.
4568 * If this is a verbatim import, trust the current
4569 * in-core spa_config and update the disk labels.
4571 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
4572 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
4573 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4574 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
4575 need_update
= B_TRUE
;
4577 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
4578 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
4579 need_update
= B_TRUE
;
4582 * Update the config cache asynchronously in case we're the
4583 * root pool, in which case the config cache isn't writable yet.
4586 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
4590 spa_ld_prepare_for_reload(spa_t
*spa
)
4592 spa_mode_t mode
= spa
->spa_mode
;
4593 int async_suspended
= spa
->spa_async_suspended
;
4596 spa_deactivate(spa
);
4597 spa_activate(spa
, mode
);
4600 * We save the value of spa_async_suspended as it gets reset to 0 by
4601 * spa_unload(). We want to restore it back to the original value before
4602 * returning as we might be calling spa_async_resume() later.
4604 spa
->spa_async_suspended
= async_suspended
;
4608 spa_ld_read_checkpoint_txg(spa_t
*spa
)
4610 uberblock_t checkpoint
;
4613 ASSERT0(spa
->spa_checkpoint_txg
);
4614 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4616 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4617 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4618 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4620 if (error
== ENOENT
)
4626 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
4627 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
4628 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
4629 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
4630 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
4636 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
4640 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4641 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4644 * Never trust the config that is provided unless we are assembling
4645 * a pool following a split.
4646 * This means don't trust blkptrs and the vdev tree in general. This
4647 * also effectively puts the spa in read-only mode since
4648 * spa_writeable() checks for spa_trust_config to be true.
4649 * We will later load a trusted config from the MOS.
4651 if (type
!= SPA_IMPORT_ASSEMBLE
)
4652 spa
->spa_trust_config
= B_FALSE
;
4655 * Parse the config provided to create a vdev tree.
4657 error
= spa_ld_parse_config(spa
, type
);
4661 spa_import_progress_add(spa
);
4664 * Now that we have the vdev tree, try to open each vdev. This involves
4665 * opening the underlying physical device, retrieving its geometry and
4666 * probing the vdev with a dummy I/O. The state of each vdev will be set
4667 * based on the success of those operations. After this we'll be ready
4668 * to read from the vdevs.
4670 error
= spa_ld_open_vdevs(spa
);
4675 * Read the label of each vdev and make sure that the GUIDs stored
4676 * there match the GUIDs in the config provided.
4677 * If we're assembling a new pool that's been split off from an
4678 * existing pool, the labels haven't yet been updated so we skip
4679 * validation for now.
4681 if (type
!= SPA_IMPORT_ASSEMBLE
) {
4682 error
= spa_ld_validate_vdevs(spa
);
4688 * Read all vdev labels to find the best uberblock (i.e. latest,
4689 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
4690 * get the list of features required to read blkptrs in the MOS from
4691 * the vdev label with the best uberblock and verify that our version
4692 * of zfs supports them all.
4694 error
= spa_ld_select_uberblock(spa
, type
);
4699 * Pass that uberblock to the dsl_pool layer which will open the root
4700 * blkptr. This blkptr points to the latest version of the MOS and will
4701 * allow us to read its contents.
4703 error
= spa_ld_open_rootbp(spa
);
4711 spa_ld_checkpoint_rewind(spa_t
*spa
)
4713 uberblock_t checkpoint
;
4716 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4717 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4719 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4720 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4721 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4724 spa_load_failed(spa
, "unable to retrieve checkpointed "
4725 "uberblock from the MOS config [error=%d]", error
);
4727 if (error
== ENOENT
)
4728 error
= ZFS_ERR_NO_CHECKPOINT
;
4733 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
4734 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
4737 * We need to update the txg and timestamp of the checkpointed
4738 * uberblock to be higher than the latest one. This ensures that
4739 * the checkpointed uberblock is selected if we were to close and
4740 * reopen the pool right after we've written it in the vdev labels.
4741 * (also see block comment in vdev_uberblock_compare)
4743 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
4744 checkpoint
.ub_timestamp
= gethrestime_sec();
4747 * Set current uberblock to be the checkpointed uberblock.
4749 spa
->spa_uberblock
= checkpoint
;
4752 * If we are doing a normal rewind, then the pool is open for
4753 * writing and we sync the "updated" checkpointed uberblock to
4754 * disk. Once this is done, we've basically rewound the whole
4755 * pool and there is no way back.
4757 * There are cases when we don't want to attempt and sync the
4758 * checkpointed uberblock to disk because we are opening a
4759 * pool as read-only. Specifically, verifying the checkpointed
4760 * state with zdb, and importing the checkpointed state to get
4761 * a "preview" of its content.
4763 if (spa_writeable(spa
)) {
4764 vdev_t
*rvd
= spa
->spa_root_vdev
;
4766 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4767 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
4769 int children
= rvd
->vdev_children
;
4770 int c0
= random_in_range(children
);
4772 for (int c
= 0; c
< children
; c
++) {
4773 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
4775 /* Stop when revisiting the first vdev */
4776 if (c
> 0 && svd
[0] == vd
)
4779 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
4780 !vdev_is_concrete(vd
))
4783 svd
[svdcount
++] = vd
;
4784 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
4787 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
4789 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
4790 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4793 spa_load_failed(spa
, "failed to write checkpointed "
4794 "uberblock to the vdev labels [error=%d]", error
);
4803 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
4804 boolean_t
*update_config_cache
)
4809 * Parse the config for pool, open and validate vdevs,
4810 * select an uberblock, and use that uberblock to open
4813 error
= spa_ld_mos_init(spa
, type
);
4818 * Retrieve the trusted config stored in the MOS and use it to create
4819 * a new, exact version of the vdev tree, then reopen all vdevs.
4821 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
4822 if (error
== EAGAIN
) {
4823 if (update_config_cache
!= NULL
)
4824 *update_config_cache
= B_TRUE
;
4827 * Redo the loading process with the trusted config if it is
4828 * too different from the untrusted config.
4830 spa_ld_prepare_for_reload(spa
);
4831 spa_load_note(spa
, "RELOADING");
4832 error
= spa_ld_mos_init(spa
, type
);
4836 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
4840 } else if (error
!= 0) {
4848 * Load an existing storage pool, using the config provided. This config
4849 * describes which vdevs are part of the pool and is later validated against
4850 * partial configs present in each vdev's label and an entire copy of the
4851 * config stored in the MOS.
4854 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4857 boolean_t missing_feat_write
= B_FALSE
;
4858 boolean_t checkpoint_rewind
=
4859 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4860 boolean_t update_config_cache
= B_FALSE
;
4862 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4863 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4865 spa_load_note(spa
, "LOADING");
4867 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
4872 * If we are rewinding to the checkpoint then we need to repeat
4873 * everything we've done so far in this function but this time
4874 * selecting the checkpointed uberblock and using that to open
4877 if (checkpoint_rewind
) {
4879 * If we are rewinding to the checkpoint update config cache
4882 update_config_cache
= B_TRUE
;
4885 * Extract the checkpointed uberblock from the current MOS
4886 * and use this as the pool's uberblock from now on. If the
4887 * pool is imported as writeable we also write the checkpoint
4888 * uberblock to the labels, making the rewind permanent.
4890 error
= spa_ld_checkpoint_rewind(spa
);
4895 * Redo the loading process again with the
4896 * checkpointed uberblock.
4898 spa_ld_prepare_for_reload(spa
);
4899 spa_load_note(spa
, "LOADING checkpointed uberblock");
4900 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
4906 * Retrieve the checkpoint txg if the pool has a checkpoint.
4908 error
= spa_ld_read_checkpoint_txg(spa
);
4913 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
4914 * from the pool and their contents were re-mapped to other vdevs. Note
4915 * that everything that we read before this step must have been
4916 * rewritten on concrete vdevs after the last device removal was
4917 * initiated. Otherwise we could be reading from indirect vdevs before
4918 * we have loaded their mappings.
4920 error
= spa_ld_open_indirect_vdev_metadata(spa
);
4925 * Retrieve the full list of active features from the MOS and check if
4926 * they are all supported.
4928 error
= spa_ld_check_features(spa
, &missing_feat_write
);
4933 * Load several special directories from the MOS needed by the dsl_pool
4936 error
= spa_ld_load_special_directories(spa
);
4941 * Retrieve pool properties from the MOS.
4943 error
= spa_ld_get_props(spa
);
4948 * Retrieve the list of auxiliary devices - cache devices and spares -
4951 error
= spa_ld_open_aux_vdevs(spa
, type
);
4956 * Load the metadata for all vdevs. Also check if unopenable devices
4957 * should be autoreplaced.
4959 error
= spa_ld_load_vdev_metadata(spa
);
4963 error
= spa_ld_load_dedup_tables(spa
);
4967 error
= spa_ld_load_brt(spa
);
4972 * Verify the logs now to make sure we don't have any unexpected errors
4973 * when we claim log blocks later.
4975 error
= spa_ld_verify_logs(spa
, type
, ereport
);
4979 if (missing_feat_write
) {
4980 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
4983 * At this point, we know that we can open the pool in
4984 * read-only mode but not read-write mode. We now have enough
4985 * information and can return to userland.
4987 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
4992 * Traverse the last txgs to make sure the pool was left off in a safe
4993 * state. When performing an extreme rewind, we verify the whole pool,
4994 * which can take a very long time.
4996 error
= spa_ld_verify_pool_data(spa
);
5001 * Calculate the deflated space for the pool. This must be done before
5002 * we write anything to the pool because we'd need to update the space
5003 * accounting using the deflated sizes.
5005 spa_update_dspace(spa
);
5008 * We have now retrieved all the information we needed to open the
5009 * pool. If we are importing the pool in read-write mode, a few
5010 * additional steps must be performed to finish the import.
5012 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
5013 spa
->spa_load_max_txg
== UINT64_MAX
)) {
5014 uint64_t config_cache_txg
= spa
->spa_config_txg
;
5016 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
5019 * In case of a checkpoint rewind, log the original txg
5020 * of the checkpointed uberblock.
5022 if (checkpoint_rewind
) {
5023 spa_history_log_internal(spa
, "checkpoint rewind",
5024 NULL
, "rewound state to txg=%llu",
5025 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
5029 * Traverse the ZIL and claim all blocks.
5031 spa_ld_claim_log_blocks(spa
);
5034 * Kick-off the syncing thread.
5036 spa
->spa_sync_on
= B_TRUE
;
5037 txg_sync_start(spa
->spa_dsl_pool
);
5038 mmp_thread_start(spa
);
5041 * Wait for all claims to sync. We sync up to the highest
5042 * claimed log block birth time so that claimed log blocks
5043 * don't appear to be from the future. spa_claim_max_txg
5044 * will have been set for us by ZIL traversal operations
5047 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
5050 * Check if we need to request an update of the config. On the
5051 * next sync, we would update the config stored in vdev labels
5052 * and the cachefile (by default /etc/zfs/zpool.cache).
5054 spa_ld_check_for_config_update(spa
, config_cache_txg
,
5055 update_config_cache
);
5058 * Check if a rebuild was in progress and if so resume it.
5059 * Then check all DTLs to see if anything needs resilvering.
5060 * The resilver will be deferred if a rebuild was started.
5062 if (vdev_rebuild_active(spa
->spa_root_vdev
)) {
5063 vdev_rebuild_restart(spa
);
5064 } else if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
5065 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5066 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
5070 * Log the fact that we booted up (so that we can detect if
5071 * we rebooted in the middle of an operation).
5073 spa_history_log_version(spa
, "open", NULL
);
5075 spa_restart_removal(spa
);
5076 spa_spawn_aux_threads(spa
);
5079 * Delete any inconsistent datasets.
5082 * Since we may be issuing deletes for clones here,
5083 * we make sure to do so after we've spawned all the
5084 * auxiliary threads above (from which the livelist
5085 * deletion zthr is part of).
5087 (void) dmu_objset_find(spa_name(spa
),
5088 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
5091 * Clean up any stale temporary dataset userrefs.
5093 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
5095 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5096 vdev_initialize_restart(spa
->spa_root_vdev
);
5097 vdev_trim_restart(spa
->spa_root_vdev
);
5098 vdev_autotrim_restart(spa
);
5099 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5102 spa_import_progress_remove(spa_guid(spa
));
5103 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
5105 spa_load_note(spa
, "LOADED");
5111 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
5113 spa_mode_t mode
= spa
->spa_mode
;
5116 spa_deactivate(spa
);
5118 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
5120 spa_activate(spa
, mode
);
5121 spa_async_suspend(spa
);
5123 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
5124 (u_longlong_t
)spa
->spa_load_max_txg
);
5126 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
5130 * If spa_load() fails this function will try loading prior txg's. If
5131 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
5132 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
5133 * function will not rewind the pool and will return the same error as
5137 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
5140 nvlist_t
*loadinfo
= NULL
;
5141 nvlist_t
*config
= NULL
;
5142 int load_error
, rewind_error
;
5143 uint64_t safe_rewind_txg
;
5146 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
5147 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
5148 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5150 spa
->spa_load_max_txg
= max_request
;
5151 if (max_request
!= UINT64_MAX
)
5152 spa
->spa_extreme_rewind
= B_TRUE
;
5155 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
5156 if (load_error
== 0)
5158 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
5160 * When attempting checkpoint-rewind on a pool with no
5161 * checkpoint, we should not attempt to load uberblocks
5162 * from previous txgs when spa_load fails.
5164 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5165 spa_import_progress_remove(spa_guid(spa
));
5166 return (load_error
);
5169 if (spa
->spa_root_vdev
!= NULL
)
5170 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5172 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
5173 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
5175 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
5176 nvlist_free(config
);
5177 spa_import_progress_remove(spa_guid(spa
));
5178 return (load_error
);
5181 if (state
== SPA_LOAD_RECOVER
) {
5182 /* Price of rolling back is discarding txgs, including log */
5183 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5186 * If we aren't rolling back save the load info from our first
5187 * import attempt so that we can restore it after attempting
5190 loadinfo
= spa
->spa_load_info
;
5191 spa
->spa_load_info
= fnvlist_alloc();
5194 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
5195 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
5196 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
5197 TXG_INITIAL
: safe_rewind_txg
;
5200 * Continue as long as we're finding errors, we're still within
5201 * the acceptable rewind range, and we're still finding uberblocks
5203 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
5204 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
5205 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
5206 spa
->spa_extreme_rewind
= B_TRUE
;
5207 rewind_error
= spa_load_retry(spa
, state
);
5210 spa
->spa_extreme_rewind
= B_FALSE
;
5211 spa
->spa_load_max_txg
= UINT64_MAX
;
5213 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
5214 spa_config_set(spa
, config
);
5216 nvlist_free(config
);
5218 if (state
== SPA_LOAD_RECOVER
) {
5219 ASSERT3P(loadinfo
, ==, NULL
);
5220 spa_import_progress_remove(spa_guid(spa
));
5221 return (rewind_error
);
5223 /* Store the rewind info as part of the initial load info */
5224 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
5225 spa
->spa_load_info
);
5227 /* Restore the initial load info */
5228 fnvlist_free(spa
->spa_load_info
);
5229 spa
->spa_load_info
= loadinfo
;
5231 spa_import_progress_remove(spa_guid(spa
));
5232 return (load_error
);
5239 * The import case is identical to an open except that the configuration is sent
5240 * down from userland, instead of grabbed from the configuration cache. For the
5241 * case of an open, the pool configuration will exist in the
5242 * POOL_STATE_UNINITIALIZED state.
5244 * The stats information (gen/count/ustats) is used to gather vdev statistics at
5245 * the same time open the pool, without having to keep around the spa_t in some
5249 spa_open_common(const char *pool
, spa_t
**spapp
, const void *tag
,
5250 nvlist_t
*nvpolicy
, nvlist_t
**config
)
5253 spa_load_state_t state
= SPA_LOAD_OPEN
;
5255 int locked
= B_FALSE
;
5256 int firstopen
= B_FALSE
;
5261 * As disgusting as this is, we need to support recursive calls to this
5262 * function because dsl_dir_open() is called during spa_load(), and ends
5263 * up calling spa_open() again. The real fix is to figure out how to
5264 * avoid dsl_dir_open() calling this in the first place.
5266 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
5267 mutex_enter(&spa_namespace_lock
);
5271 if ((spa
= spa_lookup(pool
)) == NULL
) {
5273 mutex_exit(&spa_namespace_lock
);
5274 return (SET_ERROR(ENOENT
));
5277 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
5278 zpool_load_policy_t policy
;
5282 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
5284 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5285 state
= SPA_LOAD_RECOVER
;
5287 spa_activate(spa
, spa_mode_global
);
5289 if (state
!= SPA_LOAD_RECOVER
)
5290 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5291 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5293 zfs_dbgmsg("spa_open_common: opening %s", pool
);
5294 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
5297 if (error
== EBADF
) {
5299 * If vdev_validate() returns failure (indicated by
5300 * EBADF), it indicates that one of the vdevs indicates
5301 * that the pool has been exported or destroyed. If
5302 * this is the case, the config cache is out of sync and
5303 * we should remove the pool from the namespace.
5306 spa_deactivate(spa
);
5307 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
5310 mutex_exit(&spa_namespace_lock
);
5311 return (SET_ERROR(ENOENT
));
5316 * We can't open the pool, but we still have useful
5317 * information: the state of each vdev after the
5318 * attempted vdev_open(). Return this to the user.
5320 if (config
!= NULL
&& spa
->spa_config
) {
5321 *config
= fnvlist_dup(spa
->spa_config
);
5322 fnvlist_add_nvlist(*config
,
5323 ZPOOL_CONFIG_LOAD_INFO
,
5324 spa
->spa_load_info
);
5327 spa_deactivate(spa
);
5328 spa
->spa_last_open_failed
= error
;
5330 mutex_exit(&spa_namespace_lock
);
5336 spa_open_ref(spa
, tag
);
5339 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5342 * If we've recovered the pool, pass back any information we
5343 * gathered while doing the load.
5345 if (state
== SPA_LOAD_RECOVER
&& config
!= NULL
) {
5346 fnvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
5347 spa
->spa_load_info
);
5351 spa
->spa_last_open_failed
= 0;
5352 spa
->spa_last_ubsync_txg
= 0;
5353 spa
->spa_load_txg
= 0;
5354 mutex_exit(&spa_namespace_lock
);
5358 zvol_create_minors_recursive(spa_name(spa
));
5366 spa_open_rewind(const char *name
, spa_t
**spapp
, const void *tag
,
5367 nvlist_t
*policy
, nvlist_t
**config
)
5369 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
5373 spa_open(const char *name
, spa_t
**spapp
, const void *tag
)
5375 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
5379 * Lookup the given spa_t, incrementing the inject count in the process,
5380 * preventing it from being exported or destroyed.
5383 spa_inject_addref(char *name
)
5387 mutex_enter(&spa_namespace_lock
);
5388 if ((spa
= spa_lookup(name
)) == NULL
) {
5389 mutex_exit(&spa_namespace_lock
);
5392 spa
->spa_inject_ref
++;
5393 mutex_exit(&spa_namespace_lock
);
5399 spa_inject_delref(spa_t
*spa
)
5401 mutex_enter(&spa_namespace_lock
);
5402 spa
->spa_inject_ref
--;
5403 mutex_exit(&spa_namespace_lock
);
5407 * Add spares device information to the nvlist.
5410 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
5420 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5422 if (spa
->spa_spares
.sav_count
== 0)
5425 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5426 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5427 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
5429 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5430 (const nvlist_t
* const *)spares
, nspares
);
5431 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5432 &spares
, &nspares
));
5435 * Go through and find any spares which have since been
5436 * repurposed as an active spare. If this is the case, update
5437 * their status appropriately.
5439 for (i
= 0; i
< nspares
; i
++) {
5440 guid
= fnvlist_lookup_uint64(spares
[i
],
5442 VERIFY0(nvlist_lookup_uint64_array(spares
[i
],
5443 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5444 if (spa_spare_exists(guid
, &pool
, NULL
) &&
5446 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
5447 vs
->vs_aux
= VDEV_AUX_SPARED
;
5450 spa
->spa_spares
.sav_vdevs
[i
]->vdev_state
;
5457 * Add l2cache device information to the nvlist, including vdev stats.
5460 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
5463 uint_t i
, j
, nl2cache
;
5470 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5472 if (spa
->spa_l2cache
.sav_count
== 0)
5475 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5476 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5477 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
5478 if (nl2cache
!= 0) {
5479 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5480 (const nvlist_t
* const *)l2cache
, nl2cache
);
5481 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5482 &l2cache
, &nl2cache
));
5485 * Update level 2 cache device stats.
5488 for (i
= 0; i
< nl2cache
; i
++) {
5489 guid
= fnvlist_lookup_uint64(l2cache
[i
],
5493 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
5495 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
5496 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
5502 VERIFY0(nvlist_lookup_uint64_array(l2cache
[i
],
5503 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5504 vdev_get_stats(vd
, vs
);
5505 vdev_config_generate_stats(vd
, l2cache
[i
]);
5512 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
5517 if (spa
->spa_feat_for_read_obj
!= 0) {
5518 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5519 spa
->spa_feat_for_read_obj
);
5520 zap_cursor_retrieve(&zc
, &za
) == 0;
5521 zap_cursor_advance(&zc
)) {
5522 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5523 za
.za_num_integers
== 1);
5524 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5525 za
.za_first_integer
));
5527 zap_cursor_fini(&zc
);
5530 if (spa
->spa_feat_for_write_obj
!= 0) {
5531 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5532 spa
->spa_feat_for_write_obj
);
5533 zap_cursor_retrieve(&zc
, &za
) == 0;
5534 zap_cursor_advance(&zc
)) {
5535 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5536 za
.za_num_integers
== 1);
5537 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5538 za
.za_first_integer
));
5540 zap_cursor_fini(&zc
);
5545 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
5549 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5550 zfeature_info_t feature
= spa_feature_table
[i
];
5553 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
5556 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
5561 * Store a list of pool features and their reference counts in the
5564 * The first time this is called on a spa, allocate a new nvlist, fetch
5565 * the pool features and reference counts from disk, then save the list
5566 * in the spa. In subsequent calls on the same spa use the saved nvlist
5567 * and refresh its values from the cached reference counts. This
5568 * ensures we don't block here on I/O on a suspended pool so 'zpool
5569 * clear' can resume the pool.
5572 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
5576 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5578 mutex_enter(&spa
->spa_feat_stats_lock
);
5579 features
= spa
->spa_feat_stats
;
5581 if (features
!= NULL
) {
5582 spa_feature_stats_from_cache(spa
, features
);
5584 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
5585 spa
->spa_feat_stats
= features
;
5586 spa_feature_stats_from_disk(spa
, features
);
5589 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
5592 mutex_exit(&spa
->spa_feat_stats_lock
);
5596 spa_get_stats(const char *name
, nvlist_t
**config
,
5597 char *altroot
, size_t buflen
)
5603 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
5607 * This still leaves a window of inconsistency where the spares
5608 * or l2cache devices could change and the config would be
5609 * self-inconsistent.
5611 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5613 if (*config
!= NULL
) {
5614 uint64_t loadtimes
[2];
5616 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
5617 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
5618 fnvlist_add_uint64_array(*config
,
5619 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2);
5621 fnvlist_add_uint64(*config
,
5622 ZPOOL_CONFIG_ERRCOUNT
,
5623 spa_approx_errlog_size(spa
));
5625 if (spa_suspended(spa
)) {
5626 fnvlist_add_uint64(*config
,
5627 ZPOOL_CONFIG_SUSPENDED
,
5629 fnvlist_add_uint64(*config
,
5630 ZPOOL_CONFIG_SUSPENDED_REASON
,
5631 spa
->spa_suspended
);
5634 spa_add_spares(spa
, *config
);
5635 spa_add_l2cache(spa
, *config
);
5636 spa_add_feature_stats(spa
, *config
);
5641 * We want to get the alternate root even for faulted pools, so we cheat
5642 * and call spa_lookup() directly.
5646 mutex_enter(&spa_namespace_lock
);
5647 spa
= spa_lookup(name
);
5649 spa_altroot(spa
, altroot
, buflen
);
5653 mutex_exit(&spa_namespace_lock
);
5655 spa_altroot(spa
, altroot
, buflen
);
5660 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5661 spa_close(spa
, FTAG
);
5668 * Validate that the auxiliary device array is well formed. We must have an
5669 * array of nvlists, each which describes a valid leaf vdev. If this is an
5670 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
5671 * specified, as long as they are well-formed.
5674 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
5675 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
5676 vdev_labeltype_t label
)
5683 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5686 * It's acceptable to have no devs specified.
5688 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
5692 return (SET_ERROR(EINVAL
));
5695 * Make sure the pool is formatted with a version that supports this
5698 if (spa_version(spa
) < version
)
5699 return (SET_ERROR(ENOTSUP
));
5702 * Set the pending device list so we correctly handle device in-use
5705 sav
->sav_pending
= dev
;
5706 sav
->sav_npending
= ndev
;
5708 for (i
= 0; i
< ndev
; i
++) {
5709 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
5713 if (!vd
->vdev_ops
->vdev_op_leaf
) {
5715 error
= SET_ERROR(EINVAL
);
5721 if ((error
= vdev_open(vd
)) == 0 &&
5722 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
5723 fnvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
5730 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
5737 sav
->sav_pending
= NULL
;
5738 sav
->sav_npending
= 0;
5743 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
5747 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5749 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5750 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
5751 VDEV_LABEL_SPARE
)) != 0) {
5755 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5756 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
5757 VDEV_LABEL_L2CACHE
));
5761 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
5766 if (sav
->sav_config
!= NULL
) {
5772 * Generate new dev list by concatenating with the
5775 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
5776 &olddevs
, &oldndevs
));
5778 newdevs
= kmem_alloc(sizeof (void *) *
5779 (ndevs
+ oldndevs
), KM_SLEEP
);
5780 for (i
= 0; i
< oldndevs
; i
++)
5781 newdevs
[i
] = fnvlist_dup(olddevs
[i
]);
5782 for (i
= 0; i
< ndevs
; i
++)
5783 newdevs
[i
+ oldndevs
] = fnvlist_dup(devs
[i
]);
5785 fnvlist_remove(sav
->sav_config
, config
);
5787 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
5788 (const nvlist_t
* const *)newdevs
, ndevs
+ oldndevs
);
5789 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
5790 nvlist_free(newdevs
[i
]);
5791 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
5794 * Generate a new dev list.
5796 sav
->sav_config
= fnvlist_alloc();
5797 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
5798 (const nvlist_t
* const *)devs
, ndevs
);
5803 * Stop and drop level 2 ARC devices
5806 spa_l2cache_drop(spa_t
*spa
)
5810 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5812 for (i
= 0; i
< sav
->sav_count
; i
++) {
5815 vd
= sav
->sav_vdevs
[i
];
5818 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
5819 pool
!= 0ULL && l2arc_vdev_present(vd
))
5820 l2arc_remove_vdev(vd
);
5825 * Verify encryption parameters for spa creation. If we are encrypting, we must
5826 * have the encryption feature flag enabled.
5829 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
5830 boolean_t has_encryption
)
5832 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
5833 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
5835 return (SET_ERROR(ENOTSUP
));
5837 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
5844 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
5845 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
5848 const char *altroot
= NULL
;
5853 uint64_t txg
= TXG_INITIAL
;
5854 nvlist_t
**spares
, **l2cache
;
5855 uint_t nspares
, nl2cache
;
5856 uint64_t version
, obj
, ndraid
= 0;
5857 boolean_t has_features
;
5858 boolean_t has_encryption
;
5859 boolean_t has_allocclass
;
5861 const char *feat_name
;
5862 const char *poolname
;
5865 if (props
== NULL
||
5866 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
5867 poolname
= (char *)pool
;
5870 * If this pool already exists, return failure.
5872 mutex_enter(&spa_namespace_lock
);
5873 if (spa_lookup(poolname
) != NULL
) {
5874 mutex_exit(&spa_namespace_lock
);
5875 return (SET_ERROR(EEXIST
));
5879 * Allocate a new spa_t structure.
5881 nvl
= fnvlist_alloc();
5882 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
5883 (void) nvlist_lookup_string(props
,
5884 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5885 spa
= spa_add(poolname
, nvl
, altroot
);
5887 spa_activate(spa
, spa_mode_global
);
5889 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
5890 spa_deactivate(spa
);
5892 mutex_exit(&spa_namespace_lock
);
5897 * Temporary pool names should never be written to disk.
5899 if (poolname
!= pool
)
5900 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
5902 has_features
= B_FALSE
;
5903 has_encryption
= B_FALSE
;
5904 has_allocclass
= B_FALSE
;
5905 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
5906 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
5907 if (zpool_prop_feature(nvpair_name(elem
))) {
5908 has_features
= B_TRUE
;
5910 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
5911 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
5912 if (feat
== SPA_FEATURE_ENCRYPTION
)
5913 has_encryption
= B_TRUE
;
5914 if (feat
== SPA_FEATURE_ALLOCATION_CLASSES
)
5915 has_allocclass
= B_TRUE
;
5919 /* verify encryption params, if they were provided */
5921 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
5923 spa_deactivate(spa
);
5925 mutex_exit(&spa_namespace_lock
);
5929 if (!has_allocclass
&& zfs_special_devs(nvroot
, NULL
)) {
5930 spa_deactivate(spa
);
5932 mutex_exit(&spa_namespace_lock
);
5936 if (has_features
|| nvlist_lookup_uint64(props
,
5937 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
5938 version
= SPA_VERSION
;
5940 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5942 spa
->spa_first_txg
= txg
;
5943 spa
->spa_uberblock
.ub_txg
= txg
- 1;
5944 spa
->spa_uberblock
.ub_version
= version
;
5945 spa
->spa_ubsync
= spa
->spa_uberblock
;
5946 spa
->spa_load_state
= SPA_LOAD_CREATE
;
5947 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
5948 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
5949 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
5950 spa
->spa_indirect_vdevs_loaded
= B_TRUE
;
5953 * Create "The Godfather" zio to hold all async IOs
5955 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
5957 for (int i
= 0; i
< max_ncpus
; i
++) {
5958 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
5959 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
5960 ZIO_FLAG_GODFATHER
);
5964 * Create the root vdev.
5966 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5968 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
5970 ASSERT(error
!= 0 || rvd
!= NULL
);
5971 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
5973 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
5974 error
= SET_ERROR(EINVAL
);
5977 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
5978 (error
= vdev_draid_spare_create(nvroot
, rvd
, &ndraid
, 0)) == 0 &&
5979 (error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) == 0) {
5981 * instantiate the metaslab groups (this will dirty the vdevs)
5982 * we can no longer error exit past this point
5984 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
5985 vdev_t
*vd
= rvd
->vdev_child
[c
];
5987 vdev_metaslab_set_size(vd
);
5988 vdev_expand(vd
, txg
);
5992 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5996 spa_deactivate(spa
);
5998 mutex_exit(&spa_namespace_lock
);
6003 * Get the list of spares, if specified.
6005 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6006 &spares
, &nspares
) == 0) {
6007 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6008 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6009 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6011 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6012 spa_load_spares(spa
);
6013 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6014 spa
->spa_spares
.sav_sync
= B_TRUE
;
6018 * Get the list of level 2 cache devices, if specified.
6020 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6021 &l2cache
, &nl2cache
) == 0) {
6022 VERIFY0(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
6023 NV_UNIQUE_NAME
, KM_SLEEP
));
6024 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6025 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6027 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6028 spa_load_l2cache(spa
);
6029 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6030 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6033 spa
->spa_is_initializing
= B_TRUE
;
6034 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
6035 spa
->spa_is_initializing
= B_FALSE
;
6038 * Create DDTs (dedup tables).
6042 * Create BRT table and BRT table object.
6046 spa_update_dspace(spa
);
6048 tx
= dmu_tx_create_assigned(dp
, txg
);
6051 * Create the pool's history object.
6053 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
6054 spa_history_create_obj(spa
, tx
);
6056 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
6057 spa_history_log_version(spa
, "create", tx
);
6060 * Create the pool config object.
6062 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6063 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
6064 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
6066 if (zap_add(spa
->spa_meta_objset
,
6067 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
6068 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
6069 cmn_err(CE_PANIC
, "failed to add pool config");
6072 if (zap_add(spa
->spa_meta_objset
,
6073 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
6074 sizeof (uint64_t), 1, &version
, tx
) != 0) {
6075 cmn_err(CE_PANIC
, "failed to add pool version");
6078 /* Newly created pools with the right version are always deflated. */
6079 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6080 spa
->spa_deflate
= TRUE
;
6081 if (zap_add(spa
->spa_meta_objset
,
6082 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6083 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
6084 cmn_err(CE_PANIC
, "failed to add deflate");
6089 * Create the deferred-free bpobj. Turn off compression
6090 * because sync-to-convergence takes longer if the blocksize
6093 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
6094 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
6095 ZIO_COMPRESS_OFF
, tx
);
6096 if (zap_add(spa
->spa_meta_objset
,
6097 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
6098 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
6099 cmn_err(CE_PANIC
, "failed to add bpobj");
6101 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
6102 spa
->spa_meta_objset
, obj
));
6105 * Generate some random noise for salted checksums to operate on.
6107 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
6108 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
6111 * Set pool properties.
6113 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
6114 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
6115 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
6116 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
6117 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
6118 spa
->spa_autotrim
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM
);
6120 if (props
!= NULL
) {
6121 spa_configfile_set(spa
, props
, B_FALSE
);
6122 spa_sync_props(props
, tx
);
6125 for (int i
= 0; i
< ndraid
; i
++)
6126 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6130 spa
->spa_sync_on
= B_TRUE
;
6132 mmp_thread_start(spa
);
6133 txg_wait_synced(dp
, txg
);
6135 spa_spawn_aux_threads(spa
);
6137 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
6140 * Don't count references from objsets that are already closed
6141 * and are making their way through the eviction process.
6143 spa_evicting_os_wait(spa
);
6144 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
6145 spa
->spa_load_state
= SPA_LOAD_NONE
;
6149 mutex_exit(&spa_namespace_lock
);
6155 * Import a non-root pool into the system.
6158 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
6161 const char *altroot
= NULL
;
6162 spa_load_state_t state
= SPA_LOAD_IMPORT
;
6163 zpool_load_policy_t policy
;
6164 spa_mode_t mode
= spa_mode_global
;
6165 uint64_t readonly
= B_FALSE
;
6168 nvlist_t
**spares
, **l2cache
;
6169 uint_t nspares
, nl2cache
;
6172 * If a pool with this name exists, return failure.
6174 mutex_enter(&spa_namespace_lock
);
6175 if (spa_lookup(pool
) != NULL
) {
6176 mutex_exit(&spa_namespace_lock
);
6177 return (SET_ERROR(EEXIST
));
6181 * Create and initialize the spa structure.
6183 (void) nvlist_lookup_string(props
,
6184 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6185 (void) nvlist_lookup_uint64(props
,
6186 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
6188 mode
= SPA_MODE_READ
;
6189 spa
= spa_add(pool
, config
, altroot
);
6190 spa
->spa_import_flags
= flags
;
6193 * Verbatim import - Take a pool and insert it into the namespace
6194 * as if it had been loaded at boot.
6196 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
6198 spa_configfile_set(spa
, props
, B_FALSE
);
6200 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_FALSE
);
6201 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6202 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
6203 mutex_exit(&spa_namespace_lock
);
6207 spa_activate(spa
, mode
);
6210 * Don't start async tasks until we know everything is healthy.
6212 spa_async_suspend(spa
);
6214 zpool_get_load_policy(config
, &policy
);
6215 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
6216 state
= SPA_LOAD_RECOVER
;
6218 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
6220 if (state
!= SPA_LOAD_RECOVER
) {
6221 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
6222 zfs_dbgmsg("spa_import: importing %s", pool
);
6224 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
6225 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
6227 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
6230 * Propagate anything learned while loading the pool and pass it
6231 * back to caller (i.e. rewind info, missing devices, etc).
6233 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, spa
->spa_load_info
);
6235 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6237 * Toss any existing sparelist, as it doesn't have any validity
6238 * anymore, and conflicts with spa_has_spare().
6240 if (spa
->spa_spares
.sav_config
) {
6241 nvlist_free(spa
->spa_spares
.sav_config
);
6242 spa
->spa_spares
.sav_config
= NULL
;
6243 spa_load_spares(spa
);
6245 if (spa
->spa_l2cache
.sav_config
) {
6246 nvlist_free(spa
->spa_l2cache
.sav_config
);
6247 spa
->spa_l2cache
.sav_config
= NULL
;
6248 spa_load_l2cache(spa
);
6251 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6252 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6255 spa_configfile_set(spa
, props
, B_FALSE
);
6257 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
6258 (error
= spa_prop_set(spa
, props
)))) {
6260 spa_deactivate(spa
);
6262 mutex_exit(&spa_namespace_lock
);
6266 spa_async_resume(spa
);
6269 * Override any spares and level 2 cache devices as specified by
6270 * the user, as these may have correct device names/devids, etc.
6272 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6273 &spares
, &nspares
) == 0) {
6274 if (spa
->spa_spares
.sav_config
)
6275 fnvlist_remove(spa
->spa_spares
.sav_config
,
6276 ZPOOL_CONFIG_SPARES
);
6278 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6279 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6280 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6282 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6283 spa_load_spares(spa
);
6284 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6285 spa
->spa_spares
.sav_sync
= B_TRUE
;
6287 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6288 &l2cache
, &nl2cache
) == 0) {
6289 if (spa
->spa_l2cache
.sav_config
)
6290 fnvlist_remove(spa
->spa_l2cache
.sav_config
,
6291 ZPOOL_CONFIG_L2CACHE
);
6293 spa
->spa_l2cache
.sav_config
= fnvlist_alloc();
6294 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6295 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6297 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6298 spa_load_l2cache(spa
);
6299 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6300 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6304 * Check for any removed devices.
6306 if (spa
->spa_autoreplace
) {
6307 spa_aux_check_removed(&spa
->spa_spares
);
6308 spa_aux_check_removed(&spa
->spa_l2cache
);
6311 if (spa_writeable(spa
)) {
6313 * Update the config cache to include the newly-imported pool.
6315 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6319 * It's possible that the pool was expanded while it was exported.
6320 * We kick off an async task to handle this for us.
6322 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
6324 spa_history_log_version(spa
, "import", NULL
);
6326 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6328 mutex_exit(&spa_namespace_lock
);
6330 zvol_create_minors_recursive(pool
);
6338 spa_tryimport(nvlist_t
*tryconfig
)
6340 nvlist_t
*config
= NULL
;
6341 const char *poolname
, *cachefile
;
6345 zpool_load_policy_t policy
;
6347 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
6350 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
6354 * Create and initialize the spa structure.
6356 mutex_enter(&spa_namespace_lock
);
6357 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
6358 spa_activate(spa
, SPA_MODE_READ
);
6361 * Rewind pool if a max txg was provided.
6363 zpool_get_load_policy(spa
->spa_config
, &policy
);
6364 if (policy
.zlp_txg
!= UINT64_MAX
) {
6365 spa
->spa_load_max_txg
= policy
.zlp_txg
;
6366 spa
->spa_extreme_rewind
= B_TRUE
;
6367 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
6368 poolname
, (longlong_t
)policy
.zlp_txg
);
6370 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
6373 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
6375 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
6376 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
6378 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
6382 * spa_import() relies on a pool config fetched by spa_try_import()
6383 * for spare/cache devices. Import flags are not passed to
6384 * spa_tryimport(), which makes it return early due to a missing log
6385 * device and missing retrieving the cache device and spare eventually.
6386 * Passing ZFS_IMPORT_MISSING_LOG to spa_tryimport() makes it fetch
6387 * the correct configuration regardless of the missing log device.
6389 spa
->spa_import_flags
|= ZFS_IMPORT_MISSING_LOG
;
6391 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
6394 * If 'tryconfig' was at least parsable, return the current config.
6396 if (spa
->spa_root_vdev
!= NULL
) {
6397 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
6398 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, poolname
);
6399 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, state
);
6400 fnvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
6401 spa
->spa_uberblock
.ub_timestamp
);
6402 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
6403 spa
->spa_load_info
);
6404 fnvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
6408 * If the bootfs property exists on this pool then we
6409 * copy it out so that external consumers can tell which
6410 * pools are bootable.
6412 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
6413 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6416 * We have to play games with the name since the
6417 * pool was opened as TRYIMPORT_NAME.
6419 if (dsl_dsobj_to_dsname(spa_name(spa
),
6420 spa
->spa_bootfs
, tmpname
) == 0) {
6424 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6426 cp
= strchr(tmpname
, '/');
6428 (void) strlcpy(dsname
, tmpname
,
6431 (void) snprintf(dsname
, MAXPATHLEN
,
6432 "%s/%s", poolname
, ++cp
);
6434 fnvlist_add_string(config
, ZPOOL_CONFIG_BOOTFS
,
6436 kmem_free(dsname
, MAXPATHLEN
);
6438 kmem_free(tmpname
, MAXPATHLEN
);
6442 * Add the list of hot spares and level 2 cache devices.
6444 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6445 spa_add_spares(spa
, config
);
6446 spa_add_l2cache(spa
, config
);
6447 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6451 spa_deactivate(spa
);
6453 mutex_exit(&spa_namespace_lock
);
6459 * Pool export/destroy
6461 * The act of destroying or exporting a pool is very simple. We make sure there
6462 * is no more pending I/O and any references to the pool are gone. Then, we
6463 * update the pool state and sync all the labels to disk, removing the
6464 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
6465 * we don't sync the labels or remove the configuration cache.
6468 spa_export_common(const char *pool
, int new_state
, nvlist_t
**oldconfig
,
6469 boolean_t force
, boolean_t hardforce
)
6477 if (!(spa_mode_global
& SPA_MODE_WRITE
))
6478 return (SET_ERROR(EROFS
));
6480 mutex_enter(&spa_namespace_lock
);
6481 if ((spa
= spa_lookup(pool
)) == NULL
) {
6482 mutex_exit(&spa_namespace_lock
);
6483 return (SET_ERROR(ENOENT
));
6486 if (spa
->spa_is_exporting
) {
6487 /* the pool is being exported by another thread */
6488 mutex_exit(&spa_namespace_lock
);
6489 return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS
));
6491 spa
->spa_is_exporting
= B_TRUE
;
6494 * Put a hold on the pool, drop the namespace lock, stop async tasks,
6495 * reacquire the namespace lock, and see if we can export.
6497 spa_open_ref(spa
, FTAG
);
6498 mutex_exit(&spa_namespace_lock
);
6499 spa_async_suspend(spa
);
6500 if (spa
->spa_zvol_taskq
) {
6501 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
6502 taskq_wait(spa
->spa_zvol_taskq
);
6504 mutex_enter(&spa_namespace_lock
);
6505 spa_close(spa
, FTAG
);
6507 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
6510 * The pool will be in core if it's openable, in which case we can
6511 * modify its state. Objsets may be open only because they're dirty,
6512 * so we have to force it to sync before checking spa_refcnt.
6514 if (spa
->spa_sync_on
) {
6515 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6516 spa_evicting_os_wait(spa
);
6520 * A pool cannot be exported or destroyed if there are active
6521 * references. If we are resetting a pool, allow references by
6522 * fault injection handlers.
6524 if (!spa_refcount_zero(spa
) || (spa
->spa_inject_ref
!= 0)) {
6525 error
= SET_ERROR(EBUSY
);
6529 if (spa
->spa_sync_on
) {
6530 vdev_t
*rvd
= spa
->spa_root_vdev
;
6532 * A pool cannot be exported if it has an active shared spare.
6533 * This is to prevent other pools stealing the active spare
6534 * from an exported pool. At user's own will, such pool can
6535 * be forcedly exported.
6537 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
6538 spa_has_active_shared_spare(spa
)) {
6539 error
= SET_ERROR(EXDEV
);
6544 * We're about to export or destroy this pool. Make sure
6545 * we stop all initialization and trim activity here before
6546 * we set the spa_final_txg. This will ensure that all
6547 * dirty data resulting from the initialization is
6548 * committed to disk before we unload the pool.
6550 vdev_initialize_stop_all(rvd
, VDEV_INITIALIZE_ACTIVE
);
6551 vdev_trim_stop_all(rvd
, VDEV_TRIM_ACTIVE
);
6552 vdev_autotrim_stop_all(spa
);
6553 vdev_rebuild_stop_all(spa
);
6556 * We want this to be reflected on every label,
6557 * so mark them all dirty. spa_unload() will do the
6558 * final sync that pushes these changes out.
6560 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6561 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6562 spa
->spa_state
= new_state
;
6563 vdev_config_dirty(rvd
);
6564 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6568 * If the log space map feature is enabled and the pool is
6569 * getting exported (but not destroyed), we want to spend some
6570 * time flushing as many metaslabs as we can in an attempt to
6571 * destroy log space maps and save import time. This has to be
6572 * done before we set the spa_final_txg, otherwise
6573 * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs.
6574 * spa_should_flush_logs_on_unload() should be called after
6575 * spa_state has been set to the new_state.
6577 if (spa_should_flush_logs_on_unload(spa
))
6578 spa_unload_log_sm_flush_all(spa
);
6580 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6581 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6582 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
6584 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6591 if (new_state
== POOL_STATE_DESTROYED
)
6592 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
6593 else if (new_state
== POOL_STATE_EXPORTED
)
6594 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
6596 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6598 spa_deactivate(spa
);
6601 if (oldconfig
&& spa
->spa_config
)
6602 *oldconfig
= fnvlist_dup(spa
->spa_config
);
6604 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
6606 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
6610 * If spa_remove() is not called for this spa_t and
6611 * there is any possibility that it can be reused,
6612 * we make sure to reset the exporting flag.
6614 spa
->spa_is_exporting
= B_FALSE
;
6617 mutex_exit(&spa_namespace_lock
);
6621 spa
->spa_is_exporting
= B_FALSE
;
6622 spa_async_resume(spa
);
6623 mutex_exit(&spa_namespace_lock
);
6628 * Destroy a storage pool.
6631 spa_destroy(const char *pool
)
6633 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
6638 * Export a storage pool.
6641 spa_export(const char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
6642 boolean_t hardforce
)
6644 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
6649 * Similar to spa_export(), this unloads the spa_t without actually removing it
6650 * from the namespace in any way.
6653 spa_reset(const char *pool
)
6655 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
6660 * ==========================================================================
6661 * Device manipulation
6662 * ==========================================================================
6666 * This is called as a synctask to increment the draid feature flag
6669 spa_draid_feature_incr(void *arg
, dmu_tx_t
*tx
)
6671 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6672 int draid
= (int)(uintptr_t)arg
;
6674 for (int c
= 0; c
< draid
; c
++)
6675 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6679 * Add a device to a storage pool.
6682 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
6684 uint64_t txg
, ndraid
= 0;
6686 vdev_t
*rvd
= spa
->spa_root_vdev
;
6688 nvlist_t
**spares
, **l2cache
;
6689 uint_t nspares
, nl2cache
;
6691 ASSERT(spa_writeable(spa
));
6693 txg
= spa_vdev_enter(spa
);
6695 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
6696 VDEV_ALLOC_ADD
)) != 0)
6697 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6699 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
6701 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
6705 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
6709 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
6710 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6712 if (vd
->vdev_children
!= 0 &&
6713 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0) {
6714 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6718 * The virtual dRAID spares must be added after vdev tree is created
6719 * and the vdev guids are generated. The guid of their associated
6720 * dRAID is stored in the config and used when opening the spare.
6722 if ((error
= vdev_draid_spare_create(nvroot
, vd
, &ndraid
,
6723 rvd
->vdev_children
)) == 0) {
6724 if (ndraid
> 0 && nvlist_lookup_nvlist_array(nvroot
,
6725 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) != 0)
6728 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6732 * We must validate the spares and l2cache devices after checking the
6733 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
6735 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
6736 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6739 * If we are in the middle of a device removal, we can only add
6740 * devices which match the existing devices in the pool.
6741 * If we are in the middle of a removal, or have some indirect
6742 * vdevs, we can not add raidz or dRAID top levels.
6744 if (spa
->spa_vdev_removal
!= NULL
||
6745 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
6746 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6747 tvd
= vd
->vdev_child
[c
];
6748 if (spa
->spa_vdev_removal
!= NULL
&&
6749 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
6750 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6752 /* Fail if top level vdev is raidz or a dRAID */
6753 if (vdev_get_nparity(tvd
) != 0)
6754 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6757 * Need the top level mirror to be
6758 * a mirror of leaf vdevs only
6760 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
6761 for (uint64_t cid
= 0;
6762 cid
< tvd
->vdev_children
; cid
++) {
6763 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
6764 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
6765 return (spa_vdev_exit(spa
, vd
,
6773 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6774 tvd
= vd
->vdev_child
[c
];
6775 vdev_remove_child(vd
, tvd
);
6776 tvd
->vdev_id
= rvd
->vdev_children
;
6777 vdev_add_child(rvd
, tvd
);
6778 vdev_config_dirty(tvd
);
6782 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
6783 ZPOOL_CONFIG_SPARES
);
6784 spa_load_spares(spa
);
6785 spa
->spa_spares
.sav_sync
= B_TRUE
;
6788 if (nl2cache
!= 0) {
6789 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
6790 ZPOOL_CONFIG_L2CACHE
);
6791 spa_load_l2cache(spa
);
6792 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6796 * We can't increment a feature while holding spa_vdev so we
6797 * have to do it in a synctask.
6802 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
6803 dsl_sync_task_nowait(spa
->spa_dsl_pool
, spa_draid_feature_incr
,
6804 (void *)(uintptr_t)ndraid
, tx
);
6809 * We have to be careful when adding new vdevs to an existing pool.
6810 * If other threads start allocating from these vdevs before we
6811 * sync the config cache, and we lose power, then upon reboot we may
6812 * fail to open the pool because there are DVAs that the config cache
6813 * can't translate. Therefore, we first add the vdevs without
6814 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
6815 * and then let spa_config_update() initialize the new metaslabs.
6817 * spa_load() checks for added-but-not-initialized vdevs, so that
6818 * if we lose power at any point in this sequence, the remaining
6819 * steps will be completed the next time we load the pool.
6821 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
6823 mutex_enter(&spa_namespace_lock
);
6824 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6825 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
6826 mutex_exit(&spa_namespace_lock
);
6832 * Attach a device to a mirror. The arguments are the path to any device
6833 * in the mirror, and the nvroot for the new device. If the path specifies
6834 * a device that is not mirrored, we automatically insert the mirror vdev.
6836 * If 'replacing' is specified, the new device is intended to replace the
6837 * existing device; in this case the two devices are made into their own
6838 * mirror using the 'replacing' vdev, which is functionally identical to
6839 * the mirror vdev (it actually reuses all the same ops) but has a few
6840 * extra rules: you can't attach to it after it's been created, and upon
6841 * completion of resilvering, the first disk (the one being replaced)
6842 * is automatically detached.
6844 * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
6845 * should be performed instead of traditional healing reconstruction. From
6846 * an administrators perspective these are both resilver operations.
6849 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
,
6852 uint64_t txg
, dtl_max_txg
;
6853 vdev_t
*rvd
= spa
->spa_root_vdev
;
6854 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
6856 char *oldvdpath
, *newvdpath
;
6860 ASSERT(spa_writeable(spa
));
6862 txg
= spa_vdev_enter(spa
);
6864 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6866 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6867 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6868 error
= (spa_has_checkpoint(spa
)) ?
6869 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6870 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6874 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_DEVICE_REBUILD
))
6875 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6877 if (dsl_scan_resilvering(spa_get_dsl(spa
)))
6878 return (spa_vdev_exit(spa
, NULL
, txg
,
6879 ZFS_ERR_RESILVER_IN_PROGRESS
));
6881 if (vdev_rebuild_active(rvd
))
6882 return (spa_vdev_exit(spa
, NULL
, txg
,
6883 ZFS_ERR_REBUILD_IN_PROGRESS
));
6886 if (spa
->spa_vdev_removal
!= NULL
)
6887 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6890 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6892 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
6893 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6895 pvd
= oldvd
->vdev_parent
;
6897 if (spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
6898 VDEV_ALLOC_ATTACH
) != 0)
6899 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6901 if (newrootvd
->vdev_children
!= 1)
6902 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6904 newvd
= newrootvd
->vdev_child
[0];
6906 if (!newvd
->vdev_ops
->vdev_op_leaf
)
6907 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6909 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
6910 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
6913 * log, dedup and special vdevs should not be replaced by spares.
6915 if ((oldvd
->vdev_top
->vdev_alloc_bias
!= VDEV_BIAS_NONE
||
6916 oldvd
->vdev_top
->vdev_islog
) && newvd
->vdev_isspare
) {
6917 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6921 * A dRAID spare can only replace a child of its parent dRAID vdev.
6923 if (newvd
->vdev_ops
== &vdev_draid_spare_ops
&&
6924 oldvd
->vdev_top
!= vdev_draid_spare_get_parent(newvd
)) {
6925 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6930 * For rebuilds, the top vdev must support reconstruction
6931 * using only space maps. This means the only allowable
6932 * vdevs types are the root vdev, a mirror, or dRAID.
6935 if (pvd
->vdev_top
!= NULL
)
6936 tvd
= pvd
->vdev_top
;
6938 if (tvd
->vdev_ops
!= &vdev_mirror_ops
&&
6939 tvd
->vdev_ops
!= &vdev_root_ops
&&
6940 tvd
->vdev_ops
!= &vdev_draid_ops
) {
6941 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6947 * For attach, the only allowable parent is a mirror or the root
6950 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6951 pvd
->vdev_ops
!= &vdev_root_ops
)
6952 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6954 pvops
= &vdev_mirror_ops
;
6957 * Active hot spares can only be replaced by inactive hot
6960 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6961 oldvd
->vdev_isspare
&&
6962 !spa_has_spare(spa
, newvd
->vdev_guid
))
6963 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6966 * If the source is a hot spare, and the parent isn't already a
6967 * spare, then we want to create a new hot spare. Otherwise, we
6968 * want to create a replacing vdev. The user is not allowed to
6969 * attach to a spared vdev child unless the 'isspare' state is
6970 * the same (spare replaces spare, non-spare replaces
6973 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
6974 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
6975 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6976 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6977 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
6978 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6981 if (newvd
->vdev_isspare
)
6982 pvops
= &vdev_spare_ops
;
6984 pvops
= &vdev_replacing_ops
;
6988 * Make sure the new device is big enough.
6990 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
6991 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
6994 * The new device cannot have a higher alignment requirement
6995 * than the top-level vdev.
6997 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
6998 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7001 * If this is an in-place replacement, update oldvd's path and devid
7002 * to make it distinguishable from newvd, and unopenable from now on.
7004 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
7005 spa_strfree(oldvd
->vdev_path
);
7006 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
7008 (void) snprintf(oldvd
->vdev_path
, strlen(newvd
->vdev_path
) + 5,
7009 "%s/%s", newvd
->vdev_path
, "old");
7010 if (oldvd
->vdev_devid
!= NULL
) {
7011 spa_strfree(oldvd
->vdev_devid
);
7012 oldvd
->vdev_devid
= NULL
;
7017 * If the parent is not a mirror, or if we're replacing, insert the new
7018 * mirror/replacing/spare vdev above oldvd.
7020 if (pvd
->vdev_ops
!= pvops
)
7021 pvd
= vdev_add_parent(oldvd
, pvops
);
7023 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
7024 ASSERT(pvd
->vdev_ops
== pvops
);
7025 ASSERT(oldvd
->vdev_parent
== pvd
);
7028 * Extract the new device from its root and add it to pvd.
7030 vdev_remove_child(newrootvd
, newvd
);
7031 newvd
->vdev_id
= pvd
->vdev_children
;
7032 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
7033 vdev_add_child(pvd
, newvd
);
7036 * Reevaluate the parent vdev state.
7038 vdev_propagate_state(pvd
);
7040 tvd
= newvd
->vdev_top
;
7041 ASSERT(pvd
->vdev_top
== tvd
);
7042 ASSERT(tvd
->vdev_parent
== rvd
);
7044 vdev_config_dirty(tvd
);
7047 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
7048 * for any dmu_sync-ed blocks. It will propagate upward when
7049 * spa_vdev_exit() calls vdev_dtl_reassess().
7051 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
7053 vdev_dtl_dirty(newvd
, DTL_MISSING
,
7054 TXG_INITIAL
, dtl_max_txg
- TXG_INITIAL
);
7056 if (newvd
->vdev_isspare
) {
7057 spa_spare_activate(newvd
);
7058 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
7061 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
7062 newvdpath
= spa_strdup(newvd
->vdev_path
);
7063 newvd_isspare
= newvd
->vdev_isspare
;
7066 * Mark newvd's DTL dirty in this txg.
7068 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
7071 * Schedule the resilver or rebuild to restart in the future. We do
7072 * this to ensure that dmu_sync-ed blocks have been stitched into the
7073 * respective datasets.
7076 newvd
->vdev_rebuild_txg
= txg
;
7080 newvd
->vdev_resilver_txg
= txg
;
7082 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
7083 spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
)) {
7084 vdev_defer_resilver(newvd
);
7086 dsl_scan_restart_resilver(spa
->spa_dsl_pool
,
7091 if (spa
->spa_bootfs
)
7092 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
7094 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
7099 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
7101 spa_history_log_internal(spa
, "vdev attach", NULL
,
7102 "%s vdev=%s %s vdev=%s",
7103 replacing
&& newvd_isspare
? "spare in" :
7104 replacing
? "replace" : "attach", newvdpath
,
7105 replacing
? "for" : "to", oldvdpath
);
7107 spa_strfree(oldvdpath
);
7108 spa_strfree(newvdpath
);
7114 * Detach a device from a mirror or replacing vdev.
7116 * If 'replace_done' is specified, only detach if the parent
7117 * is a replacing or a spare vdev.
7120 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
7124 vdev_t
*rvd __maybe_unused
= spa
->spa_root_vdev
;
7125 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
7126 boolean_t unspare
= B_FALSE
;
7127 uint64_t unspare_guid
= 0;
7130 ASSERT(spa_writeable(spa
));
7132 txg
= spa_vdev_detach_enter(spa
, guid
);
7134 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7137 * Besides being called directly from the userland through the
7138 * ioctl interface, spa_vdev_detach() can be potentially called
7139 * at the end of spa_vdev_resilver_done().
7141 * In the regular case, when we have a checkpoint this shouldn't
7142 * happen as we never empty the DTLs of a vdev during the scrub
7143 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
7144 * should never get here when we have a checkpoint.
7146 * That said, even in a case when we checkpoint the pool exactly
7147 * as spa_vdev_resilver_done() calls this function everything
7148 * should be fine as the resilver will return right away.
7150 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7151 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7152 error
= (spa_has_checkpoint(spa
)) ?
7153 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7154 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7158 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7160 if (!vd
->vdev_ops
->vdev_op_leaf
)
7161 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7163 pvd
= vd
->vdev_parent
;
7166 * If the parent/child relationship is not as expected, don't do it.
7167 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
7168 * vdev that's replacing B with C. The user's intent in replacing
7169 * is to go from M(A,B) to M(A,C). If the user decides to cancel
7170 * the replace by detaching C, the expected behavior is to end up
7171 * M(A,B). But suppose that right after deciding to detach C,
7172 * the replacement of B completes. We would have M(A,C), and then
7173 * ask to detach C, which would leave us with just A -- not what
7174 * the user wanted. To prevent this, we make sure that the
7175 * parent/child relationship hasn't changed -- in this example,
7176 * that C's parent is still the replacing vdev R.
7178 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
7179 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7182 * Only 'replacing' or 'spare' vdevs can be replaced.
7184 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7185 pvd
->vdev_ops
!= &vdev_spare_ops
)
7186 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7188 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
7189 spa_version(spa
) >= SPA_VERSION_SPARES
);
7192 * Only mirror, replacing, and spare vdevs support detach.
7194 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7195 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7196 pvd
->vdev_ops
!= &vdev_spare_ops
)
7197 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7200 * If this device has the only valid copy of some data,
7201 * we cannot safely detach it.
7203 if (vdev_dtl_required(vd
))
7204 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7206 ASSERT(pvd
->vdev_children
>= 2);
7209 * If we are detaching the second disk from a replacing vdev, then
7210 * check to see if we changed the original vdev's path to have "/old"
7211 * at the end in spa_vdev_attach(). If so, undo that change now.
7213 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
7214 vd
->vdev_path
!= NULL
) {
7215 size_t len
= strlen(vd
->vdev_path
);
7217 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
7218 cvd
= pvd
->vdev_child
[c
];
7220 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
7223 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
7224 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
7225 spa_strfree(cvd
->vdev_path
);
7226 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
7233 * If we are detaching the original disk from a normal spare, then it
7234 * implies that the spare should become a real disk, and be removed
7235 * from the active spare list for the pool. dRAID spares on the
7236 * other hand are coupled to the pool and thus should never be removed
7237 * from the spares list.
7239 if (pvd
->vdev_ops
== &vdev_spare_ops
&& vd
->vdev_id
== 0) {
7240 vdev_t
*last_cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7242 if (last_cvd
->vdev_isspare
&&
7243 last_cvd
->vdev_ops
!= &vdev_draid_spare_ops
) {
7249 * Erase the disk labels so the disk can be used for other things.
7250 * This must be done after all other error cases are handled,
7251 * but before we disembowel vd (so we can still do I/O to it).
7252 * But if we can't do it, don't treat the error as fatal --
7253 * it may be that the unwritability of the disk is the reason
7254 * it's being detached!
7256 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
7259 * Remove vd from its parent and compact the parent's children.
7261 vdev_remove_child(pvd
, vd
);
7262 vdev_compact_children(pvd
);
7265 * Remember one of the remaining children so we can get tvd below.
7267 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7270 * If we need to remove the remaining child from the list of hot spares,
7271 * do it now, marking the vdev as no longer a spare in the process.
7272 * We must do this before vdev_remove_parent(), because that can
7273 * change the GUID if it creates a new toplevel GUID. For a similar
7274 * reason, we must remove the spare now, in the same txg as the detach;
7275 * otherwise someone could attach a new sibling, change the GUID, and
7276 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
7279 ASSERT(cvd
->vdev_isspare
);
7280 spa_spare_remove(cvd
);
7281 unspare_guid
= cvd
->vdev_guid
;
7282 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
7283 cvd
->vdev_unspare
= B_TRUE
;
7287 * If the parent mirror/replacing vdev only has one child,
7288 * the parent is no longer needed. Remove it from the tree.
7290 if (pvd
->vdev_children
== 1) {
7291 if (pvd
->vdev_ops
== &vdev_spare_ops
)
7292 cvd
->vdev_unspare
= B_FALSE
;
7293 vdev_remove_parent(cvd
);
7297 * We don't set tvd until now because the parent we just removed
7298 * may have been the previous top-level vdev.
7300 tvd
= cvd
->vdev_top
;
7301 ASSERT(tvd
->vdev_parent
== rvd
);
7304 * Reevaluate the parent vdev state.
7306 vdev_propagate_state(cvd
);
7309 * If the 'autoexpand' property is set on the pool then automatically
7310 * try to expand the size of the pool. For example if the device we
7311 * just detached was smaller than the others, it may be possible to
7312 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
7313 * first so that we can obtain the updated sizes of the leaf vdevs.
7315 if (spa
->spa_autoexpand
) {
7317 vdev_expand(tvd
, txg
);
7320 vdev_config_dirty(tvd
);
7323 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
7324 * vd->vdev_detached is set and free vd's DTL object in syncing context.
7325 * But first make sure we're not on any *other* txg's DTL list, to
7326 * prevent vd from being accessed after it's freed.
7328 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
7329 for (int t
= 0; t
< TXG_SIZE
; t
++)
7330 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
7331 vd
->vdev_detached
= B_TRUE
;
7332 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
7334 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
7335 spa_notify_waiters(spa
);
7337 /* hang on to the spa before we release the lock */
7338 spa_open_ref(spa
, FTAG
);
7340 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
7342 spa_history_log_internal(spa
, "detach", NULL
,
7344 spa_strfree(vdpath
);
7347 * If this was the removal of the original device in a hot spare vdev,
7348 * then we want to go through and remove the device from the hot spare
7349 * list of every other pool.
7352 spa_t
*altspa
= NULL
;
7354 mutex_enter(&spa_namespace_lock
);
7355 while ((altspa
= spa_next(altspa
)) != NULL
) {
7356 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
7360 spa_open_ref(altspa
, FTAG
);
7361 mutex_exit(&spa_namespace_lock
);
7362 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
7363 mutex_enter(&spa_namespace_lock
);
7364 spa_close(altspa
, FTAG
);
7366 mutex_exit(&spa_namespace_lock
);
7368 /* search the rest of the vdevs for spares to remove */
7369 spa_vdev_resilver_done(spa
);
7372 /* all done with the spa; OK to release */
7373 mutex_enter(&spa_namespace_lock
);
7374 spa_close(spa
, FTAG
);
7375 mutex_exit(&spa_namespace_lock
);
7381 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7384 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7386 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7388 /* Look up vdev and ensure it's a leaf. */
7389 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7390 if (vd
== NULL
|| vd
->vdev_detached
) {
7391 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7392 return (SET_ERROR(ENODEV
));
7393 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7394 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7395 return (SET_ERROR(EINVAL
));
7396 } else if (!vdev_writeable(vd
)) {
7397 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7398 return (SET_ERROR(EROFS
));
7400 mutex_enter(&vd
->vdev_initialize_lock
);
7401 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7404 * When we activate an initialize action we check to see
7405 * if the vdev_initialize_thread is NULL. We do this instead
7406 * of using the vdev_initialize_state since there might be
7407 * a previous initialization process which has completed but
7408 * the thread is not exited.
7410 if (cmd_type
== POOL_INITIALIZE_START
&&
7411 (vd
->vdev_initialize_thread
!= NULL
||
7412 vd
->vdev_top
->vdev_removing
)) {
7413 mutex_exit(&vd
->vdev_initialize_lock
);
7414 return (SET_ERROR(EBUSY
));
7415 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
7416 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
7417 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
7418 mutex_exit(&vd
->vdev_initialize_lock
);
7419 return (SET_ERROR(ESRCH
));
7420 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
7421 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
7422 mutex_exit(&vd
->vdev_initialize_lock
);
7423 return (SET_ERROR(ESRCH
));
7424 } else if (cmd_type
== POOL_INITIALIZE_UNINIT
&&
7425 vd
->vdev_initialize_thread
!= NULL
) {
7426 mutex_exit(&vd
->vdev_initialize_lock
);
7427 return (SET_ERROR(EBUSY
));
7431 case POOL_INITIALIZE_START
:
7432 vdev_initialize(vd
);
7434 case POOL_INITIALIZE_CANCEL
:
7435 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
7437 case POOL_INITIALIZE_SUSPEND
:
7438 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
7440 case POOL_INITIALIZE_UNINIT
:
7441 vdev_uninitialize(vd
);
7444 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7446 mutex_exit(&vd
->vdev_initialize_lock
);
7452 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
7453 nvlist_t
*vdev_errlist
)
7455 int total_errors
= 0;
7458 list_create(&vd_list
, sizeof (vdev_t
),
7459 offsetof(vdev_t
, vdev_initialize_node
));
7462 * We hold the namespace lock through the whole function
7463 * to prevent any changes to the pool while we're starting or
7464 * stopping initialization. The config and state locks are held so that
7465 * we can properly assess the vdev state before we commit to
7466 * the initializing operation.
7468 mutex_enter(&spa_namespace_lock
);
7470 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7471 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7472 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7474 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
7477 char guid_as_str
[MAXNAMELEN
];
7479 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7480 "%llu", (unsigned long long)vdev_guid
);
7481 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7486 /* Wait for all initialize threads to stop. */
7487 vdev_initialize_stop_wait(spa
, &vd_list
);
7489 /* Sync out the initializing state */
7490 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7491 mutex_exit(&spa_namespace_lock
);
7493 list_destroy(&vd_list
);
7495 return (total_errors
);
7499 spa_vdev_trim_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7500 uint64_t rate
, boolean_t partial
, boolean_t secure
, list_t
*vd_list
)
7502 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7504 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7506 /* Look up vdev and ensure it's a leaf. */
7507 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7508 if (vd
== NULL
|| vd
->vdev_detached
) {
7509 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7510 return (SET_ERROR(ENODEV
));
7511 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7512 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7513 return (SET_ERROR(EINVAL
));
7514 } else if (!vdev_writeable(vd
)) {
7515 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7516 return (SET_ERROR(EROFS
));
7517 } else if (!vd
->vdev_has_trim
) {
7518 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7519 return (SET_ERROR(EOPNOTSUPP
));
7520 } else if (secure
&& !vd
->vdev_has_securetrim
) {
7521 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7522 return (SET_ERROR(EOPNOTSUPP
));
7524 mutex_enter(&vd
->vdev_trim_lock
);
7525 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7528 * When we activate a TRIM action we check to see if the
7529 * vdev_trim_thread is NULL. We do this instead of using the
7530 * vdev_trim_state since there might be a previous TRIM process
7531 * which has completed but the thread is not exited.
7533 if (cmd_type
== POOL_TRIM_START
&&
7534 (vd
->vdev_trim_thread
!= NULL
|| vd
->vdev_top
->vdev_removing
)) {
7535 mutex_exit(&vd
->vdev_trim_lock
);
7536 return (SET_ERROR(EBUSY
));
7537 } else if (cmd_type
== POOL_TRIM_CANCEL
&&
7538 (vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
&&
7539 vd
->vdev_trim_state
!= VDEV_TRIM_SUSPENDED
)) {
7540 mutex_exit(&vd
->vdev_trim_lock
);
7541 return (SET_ERROR(ESRCH
));
7542 } else if (cmd_type
== POOL_TRIM_SUSPEND
&&
7543 vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
) {
7544 mutex_exit(&vd
->vdev_trim_lock
);
7545 return (SET_ERROR(ESRCH
));
7549 case POOL_TRIM_START
:
7550 vdev_trim(vd
, rate
, partial
, secure
);
7552 case POOL_TRIM_CANCEL
:
7553 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, vd_list
);
7555 case POOL_TRIM_SUSPEND
:
7556 vdev_trim_stop(vd
, VDEV_TRIM_SUSPENDED
, vd_list
);
7559 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7561 mutex_exit(&vd
->vdev_trim_lock
);
7567 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
7568 * TRIM threads for each child vdev. These threads pass over all of the free
7569 * space in the vdev's metaslabs and issues TRIM commands for that space.
7572 spa_vdev_trim(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
, uint64_t rate
,
7573 boolean_t partial
, boolean_t secure
, nvlist_t
*vdev_errlist
)
7575 int total_errors
= 0;
7578 list_create(&vd_list
, sizeof (vdev_t
),
7579 offsetof(vdev_t
, vdev_trim_node
));
7582 * We hold the namespace lock through the whole function
7583 * to prevent any changes to the pool while we're starting or
7584 * stopping TRIM. The config and state locks are held so that
7585 * we can properly assess the vdev state before we commit to
7586 * the TRIM operation.
7588 mutex_enter(&spa_namespace_lock
);
7590 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7591 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7592 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7594 int error
= spa_vdev_trim_impl(spa
, vdev_guid
, cmd_type
,
7595 rate
, partial
, secure
, &vd_list
);
7597 char guid_as_str
[MAXNAMELEN
];
7599 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7600 "%llu", (unsigned long long)vdev_guid
);
7601 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7606 /* Wait for all TRIM threads to stop. */
7607 vdev_trim_stop_wait(spa
, &vd_list
);
7609 /* Sync out the TRIM state */
7610 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7611 mutex_exit(&spa_namespace_lock
);
7613 list_destroy(&vd_list
);
7615 return (total_errors
);
7619 * Split a set of devices from their mirrors, and create a new pool from them.
7622 spa_vdev_split_mirror(spa_t
*spa
, const char *newname
, nvlist_t
*config
,
7623 nvlist_t
*props
, boolean_t exp
)
7626 uint64_t txg
, *glist
;
7628 uint_t c
, children
, lastlog
;
7629 nvlist_t
**child
, *nvl
, *tmp
;
7631 const char *altroot
= NULL
;
7632 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
7633 boolean_t activate_slog
;
7635 ASSERT(spa_writeable(spa
));
7637 txg
= spa_vdev_enter(spa
);
7639 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7640 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7641 error
= (spa_has_checkpoint(spa
)) ?
7642 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7643 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7646 /* clear the log and flush everything up to now */
7647 activate_slog
= spa_passivate_log(spa
);
7648 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7649 error
= spa_reset_logs(spa
);
7650 txg
= spa_vdev_config_enter(spa
);
7653 spa_activate_log(spa
);
7656 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7658 /* check new spa name before going any further */
7659 if (spa_lookup(newname
) != NULL
)
7660 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
7663 * scan through all the children to ensure they're all mirrors
7665 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
7666 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
7668 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7670 /* first, check to ensure we've got the right child count */
7671 rvd
= spa
->spa_root_vdev
;
7673 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
7674 vdev_t
*vd
= rvd
->vdev_child
[c
];
7676 /* don't count the holes & logs as children */
7677 if (vd
->vdev_islog
|| (vd
->vdev_ops
!= &vdev_indirect_ops
&&
7678 !vdev_is_concrete(vd
))) {
7686 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
7687 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7689 /* next, ensure no spare or cache devices are part of the split */
7690 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
7691 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
7692 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7694 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
7695 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
7697 /* then, loop over each vdev and validate it */
7698 for (c
= 0; c
< children
; c
++) {
7699 uint64_t is_hole
= 0;
7701 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
7705 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
7706 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
7709 error
= SET_ERROR(EINVAL
);
7714 /* deal with indirect vdevs */
7715 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ops
==
7719 /* which disk is going to be split? */
7720 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
7722 error
= SET_ERROR(EINVAL
);
7726 /* look it up in the spa */
7727 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
7728 if (vml
[c
] == NULL
) {
7729 error
= SET_ERROR(ENODEV
);
7733 /* make sure there's nothing stopping the split */
7734 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
7735 vml
[c
]->vdev_islog
||
7736 !vdev_is_concrete(vml
[c
]) ||
7737 vml
[c
]->vdev_isspare
||
7738 vml
[c
]->vdev_isl2cache
||
7739 !vdev_writeable(vml
[c
]) ||
7740 vml
[c
]->vdev_children
!= 0 ||
7741 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
7742 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
7743 error
= SET_ERROR(EINVAL
);
7747 if (vdev_dtl_required(vml
[c
]) ||
7748 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
7749 error
= SET_ERROR(EBUSY
);
7753 /* we need certain info from the top level */
7754 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
7755 vml
[c
]->vdev_top
->vdev_ms_array
);
7756 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
7757 vml
[c
]->vdev_top
->vdev_ms_shift
);
7758 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
7759 vml
[c
]->vdev_top
->vdev_asize
);
7760 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
7761 vml
[c
]->vdev_top
->vdev_ashift
);
7763 /* transfer per-vdev ZAPs */
7764 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
7765 VERIFY0(nvlist_add_uint64(child
[c
],
7766 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
7768 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
7769 VERIFY0(nvlist_add_uint64(child
[c
],
7770 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
7771 vml
[c
]->vdev_parent
->vdev_top_zap
));
7775 kmem_free(vml
, children
* sizeof (vdev_t
*));
7776 kmem_free(glist
, children
* sizeof (uint64_t));
7777 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7780 /* stop writers from using the disks */
7781 for (c
= 0; c
< children
; c
++) {
7783 vml
[c
]->vdev_offline
= B_TRUE
;
7785 vdev_reopen(spa
->spa_root_vdev
);
7788 * Temporarily record the splitting vdevs in the spa config. This
7789 * will disappear once the config is regenerated.
7791 nvl
= fnvlist_alloc();
7792 fnvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
, glist
, children
);
7793 kmem_free(glist
, children
* sizeof (uint64_t));
7795 mutex_enter(&spa
->spa_props_lock
);
7796 fnvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
, nvl
);
7797 mutex_exit(&spa
->spa_props_lock
);
7798 spa
->spa_config_splitting
= nvl
;
7799 vdev_config_dirty(spa
->spa_root_vdev
);
7801 /* configure and create the new pool */
7802 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
);
7803 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
7804 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
);
7805 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
, spa_version(spa
));
7806 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, spa
->spa_config_txg
);
7807 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
7808 spa_generate_guid(NULL
));
7809 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
7810 (void) nvlist_lookup_string(props
,
7811 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
7813 /* add the new pool to the namespace */
7814 newspa
= spa_add(newname
, config
, altroot
);
7815 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7816 newspa
->spa_config_txg
= spa
->spa_config_txg
;
7817 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
7819 /* release the spa config lock, retaining the namespace lock */
7820 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7822 if (zio_injection_enabled
)
7823 zio_handle_panic_injection(spa
, FTAG
, 1);
7825 spa_activate(newspa
, spa_mode_global
);
7826 spa_async_suspend(newspa
);
7829 * Temporarily stop the initializing and TRIM activity. We set the
7830 * state to ACTIVE so that we know to resume initializing or TRIM
7831 * once the split has completed.
7833 list_t vd_initialize_list
;
7834 list_create(&vd_initialize_list
, sizeof (vdev_t
),
7835 offsetof(vdev_t
, vdev_initialize_node
));
7837 list_t vd_trim_list
;
7838 list_create(&vd_trim_list
, sizeof (vdev_t
),
7839 offsetof(vdev_t
, vdev_trim_node
));
7841 for (c
= 0; c
< children
; c
++) {
7842 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7843 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
7844 vdev_initialize_stop(vml
[c
],
7845 VDEV_INITIALIZE_ACTIVE
, &vd_initialize_list
);
7846 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
7848 mutex_enter(&vml
[c
]->vdev_trim_lock
);
7849 vdev_trim_stop(vml
[c
], VDEV_TRIM_ACTIVE
, &vd_trim_list
);
7850 mutex_exit(&vml
[c
]->vdev_trim_lock
);
7854 vdev_initialize_stop_wait(spa
, &vd_initialize_list
);
7855 vdev_trim_stop_wait(spa
, &vd_trim_list
);
7857 list_destroy(&vd_initialize_list
);
7858 list_destroy(&vd_trim_list
);
7860 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
7861 newspa
->spa_is_splitting
= B_TRUE
;
7863 /* create the new pool from the disks of the original pool */
7864 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
7868 /* if that worked, generate a real config for the new pool */
7869 if (newspa
->spa_root_vdev
!= NULL
) {
7870 newspa
->spa_config_splitting
= fnvlist_alloc();
7871 fnvlist_add_uint64(newspa
->spa_config_splitting
,
7872 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
));
7873 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
7878 if (props
!= NULL
) {
7879 spa_configfile_set(newspa
, props
, B_FALSE
);
7880 error
= spa_prop_set(newspa
, props
);
7885 /* flush everything */
7886 txg
= spa_vdev_config_enter(newspa
);
7887 vdev_config_dirty(newspa
->spa_root_vdev
);
7888 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
7890 if (zio_injection_enabled
)
7891 zio_handle_panic_injection(spa
, FTAG
, 2);
7893 spa_async_resume(newspa
);
7895 /* finally, update the original pool's config */
7896 txg
= spa_vdev_config_enter(spa
);
7897 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
7898 error
= dmu_tx_assign(tx
, TXG_WAIT
);
7901 for (c
= 0; c
< children
; c
++) {
7902 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7903 vdev_t
*tvd
= vml
[c
]->vdev_top
;
7906 * Need to be sure the detachable VDEV is not
7907 * on any *other* txg's DTL list to prevent it
7908 * from being accessed after it's freed.
7910 for (int t
= 0; t
< TXG_SIZE
; t
++) {
7911 (void) txg_list_remove_this(
7912 &tvd
->vdev_dtl_list
, vml
[c
], t
);
7917 spa_history_log_internal(spa
, "detach", tx
,
7918 "vdev=%s", vml
[c
]->vdev_path
);
7923 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7924 vdev_config_dirty(spa
->spa_root_vdev
);
7925 spa
->spa_config_splitting
= NULL
;
7929 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
7931 if (zio_injection_enabled
)
7932 zio_handle_panic_injection(spa
, FTAG
, 3);
7934 /* split is complete; log a history record */
7935 spa_history_log_internal(newspa
, "split", NULL
,
7936 "from pool %s", spa_name(spa
));
7938 newspa
->spa_is_splitting
= B_FALSE
;
7939 kmem_free(vml
, children
* sizeof (vdev_t
*));
7941 /* if we're not going to mount the filesystems in userland, export */
7943 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
7950 spa_deactivate(newspa
);
7953 txg
= spa_vdev_config_enter(spa
);
7955 /* re-online all offlined disks */
7956 for (c
= 0; c
< children
; c
++) {
7958 vml
[c
]->vdev_offline
= B_FALSE
;
7961 /* restart initializing or trimming disks as necessary */
7962 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
7963 spa_async_request(spa
, SPA_ASYNC_TRIM_RESTART
);
7964 spa_async_request(spa
, SPA_ASYNC_AUTOTRIM_RESTART
);
7966 vdev_reopen(spa
->spa_root_vdev
);
7968 nvlist_free(spa
->spa_config_splitting
);
7969 spa
->spa_config_splitting
= NULL
;
7970 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
7972 kmem_free(vml
, children
* sizeof (vdev_t
*));
7977 * Find any device that's done replacing, or a vdev marked 'unspare' that's
7978 * currently spared, so we can detach it.
7981 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
7983 vdev_t
*newvd
, *oldvd
;
7985 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7986 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
7992 * Check for a completed replacement. We always consider the first
7993 * vdev in the list to be the oldest vdev, and the last one to be
7994 * the newest (see spa_vdev_attach() for how that works). In
7995 * the case where the newest vdev is faulted, we will not automatically
7996 * remove it after a resilver completes. This is OK as it will require
7997 * user intervention to determine which disk the admin wishes to keep.
7999 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
8000 ASSERT(vd
->vdev_children
> 1);
8002 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
8003 oldvd
= vd
->vdev_child
[0];
8005 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8006 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8007 !vdev_dtl_required(oldvd
))
8012 * Check for a completed resilver with the 'unspare' flag set.
8013 * Also potentially update faulted state.
8015 if (vd
->vdev_ops
== &vdev_spare_ops
) {
8016 vdev_t
*first
= vd
->vdev_child
[0];
8017 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
8019 if (last
->vdev_unspare
) {
8022 } else if (first
->vdev_unspare
) {
8029 if (oldvd
!= NULL
&&
8030 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8031 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8032 !vdev_dtl_required(oldvd
))
8035 vdev_propagate_state(vd
);
8038 * If there are more than two spares attached to a disk,
8039 * and those spares are not required, then we want to
8040 * attempt to free them up now so that they can be used
8041 * by other pools. Once we're back down to a single
8042 * disk+spare, we stop removing them.
8044 if (vd
->vdev_children
> 2) {
8045 newvd
= vd
->vdev_child
[1];
8047 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
8048 vdev_dtl_empty(last
, DTL_MISSING
) &&
8049 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
8050 !vdev_dtl_required(newvd
))
8059 spa_vdev_resilver_done(spa_t
*spa
)
8061 vdev_t
*vd
, *pvd
, *ppvd
;
8062 uint64_t guid
, sguid
, pguid
, ppguid
;
8064 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8066 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
8067 pvd
= vd
->vdev_parent
;
8068 ppvd
= pvd
->vdev_parent
;
8069 guid
= vd
->vdev_guid
;
8070 pguid
= pvd
->vdev_guid
;
8071 ppguid
= ppvd
->vdev_guid
;
8074 * If we have just finished replacing a hot spared device, then
8075 * we need to detach the parent's first child (the original hot
8078 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
8079 ppvd
->vdev_children
== 2) {
8080 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
8081 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
8083 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
8085 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8086 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
8088 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
8090 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8093 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8096 * If a detach was not performed above replace waiters will not have
8097 * been notified. In which case we must do so now.
8099 spa_notify_waiters(spa
);
8103 * Update the stored path or FRU for this vdev.
8106 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
8110 boolean_t sync
= B_FALSE
;
8112 ASSERT(spa_writeable(spa
));
8114 spa_vdev_state_enter(spa
, SCL_ALL
);
8116 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
8117 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
8119 if (!vd
->vdev_ops
->vdev_op_leaf
)
8120 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
8123 if (strcmp(value
, vd
->vdev_path
) != 0) {
8124 spa_strfree(vd
->vdev_path
);
8125 vd
->vdev_path
= spa_strdup(value
);
8129 if (vd
->vdev_fru
== NULL
) {
8130 vd
->vdev_fru
= spa_strdup(value
);
8132 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
8133 spa_strfree(vd
->vdev_fru
);
8134 vd
->vdev_fru
= spa_strdup(value
);
8139 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
8143 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
8145 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
8149 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
8151 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
8155 * ==========================================================================
8157 * ==========================================================================
8160 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
8162 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8164 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8165 return (SET_ERROR(EBUSY
));
8167 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
8171 spa_scan_stop(spa_t
*spa
)
8173 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8174 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8175 return (SET_ERROR(EBUSY
));
8176 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
8180 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
8182 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8184 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
8185 return (SET_ERROR(ENOTSUP
));
8187 if (func
== POOL_SCAN_RESILVER
&&
8188 !spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
8189 return (SET_ERROR(ENOTSUP
));
8192 * If a resilver was requested, but there is no DTL on a
8193 * writeable leaf device, we have nothing to do.
8195 if (func
== POOL_SCAN_RESILVER
&&
8196 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
8197 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
8201 return (dsl_scan(spa
->spa_dsl_pool
, func
));
8205 * ==========================================================================
8206 * SPA async task processing
8207 * ==========================================================================
8211 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
8213 if (vd
->vdev_remove_wanted
) {
8214 vd
->vdev_remove_wanted
= B_FALSE
;
8215 vd
->vdev_delayed_close
= B_FALSE
;
8216 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
8219 * We want to clear the stats, but we don't want to do a full
8220 * vdev_clear() as that will cause us to throw away
8221 * degraded/faulted state as well as attempt to reopen the
8222 * device, all of which is a waste.
8224 vd
->vdev_stat
.vs_read_errors
= 0;
8225 vd
->vdev_stat
.vs_write_errors
= 0;
8226 vd
->vdev_stat
.vs_checksum_errors
= 0;
8228 vdev_state_dirty(vd
->vdev_top
);
8230 /* Tell userspace that the vdev is gone. */
8231 zfs_post_remove(spa
, vd
);
8234 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8235 spa_async_remove(spa
, vd
->vdev_child
[c
]);
8239 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
8241 if (vd
->vdev_probe_wanted
) {
8242 vd
->vdev_probe_wanted
= B_FALSE
;
8243 vdev_reopen(vd
); /* vdev_open() does the actual probe */
8246 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8247 spa_async_probe(spa
, vd
->vdev_child
[c
]);
8251 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
8253 if (!spa
->spa_autoexpand
)
8256 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8257 vdev_t
*cvd
= vd
->vdev_child
[c
];
8258 spa_async_autoexpand(spa
, cvd
);
8261 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
8264 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
8267 static __attribute__((noreturn
)) void
8268 spa_async_thread(void *arg
)
8270 spa_t
*spa
= (spa_t
*)arg
;
8271 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8274 ASSERT(spa
->spa_sync_on
);
8276 mutex_enter(&spa
->spa_async_lock
);
8277 tasks
= spa
->spa_async_tasks
;
8278 spa
->spa_async_tasks
= 0;
8279 mutex_exit(&spa
->spa_async_lock
);
8282 * See if the config needs to be updated.
8284 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
8285 uint64_t old_space
, new_space
;
8287 mutex_enter(&spa_namespace_lock
);
8288 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
8289 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
8290 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8291 old_space
+= metaslab_class_get_space(
8292 spa_embedded_log_class(spa
));
8294 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
8296 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
8297 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
8298 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8299 new_space
+= metaslab_class_get_space(
8300 spa_embedded_log_class(spa
));
8301 mutex_exit(&spa_namespace_lock
);
8304 * If the pool grew as a result of the config update,
8305 * then log an internal history event.
8307 if (new_space
!= old_space
) {
8308 spa_history_log_internal(spa
, "vdev online", NULL
,
8309 "pool '%s' size: %llu(+%llu)",
8310 spa_name(spa
), (u_longlong_t
)new_space
,
8311 (u_longlong_t
)(new_space
- old_space
));
8316 * See if any devices need to be marked REMOVED.
8318 if (tasks
& SPA_ASYNC_REMOVE
) {
8319 spa_vdev_state_enter(spa
, SCL_NONE
);
8320 spa_async_remove(spa
, spa
->spa_root_vdev
);
8321 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
8322 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
8323 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
8324 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
8325 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8328 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
8329 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8330 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
8331 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8335 * See if any devices need to be probed.
8337 if (tasks
& SPA_ASYNC_PROBE
) {
8338 spa_vdev_state_enter(spa
, SCL_NONE
);
8339 spa_async_probe(spa
, spa
->spa_root_vdev
);
8340 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8344 * If any devices are done replacing, detach them.
8346 if (tasks
& SPA_ASYNC_RESILVER_DONE
||
8347 tasks
& SPA_ASYNC_REBUILD_DONE
||
8348 tasks
& SPA_ASYNC_DETACH_SPARE
) {
8349 spa_vdev_resilver_done(spa
);
8353 * Kick off a resilver.
8355 if (tasks
& SPA_ASYNC_RESILVER
&&
8356 !vdev_rebuild_active(spa
->spa_root_vdev
) &&
8357 (!dsl_scan_resilvering(dp
) ||
8358 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
8359 dsl_scan_restart_resilver(dp
, 0);
8361 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
8362 mutex_enter(&spa_namespace_lock
);
8363 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8364 vdev_initialize_restart(spa
->spa_root_vdev
);
8365 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8366 mutex_exit(&spa_namespace_lock
);
8369 if (tasks
& SPA_ASYNC_TRIM_RESTART
) {
8370 mutex_enter(&spa_namespace_lock
);
8371 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8372 vdev_trim_restart(spa
->spa_root_vdev
);
8373 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8374 mutex_exit(&spa_namespace_lock
);
8377 if (tasks
& SPA_ASYNC_AUTOTRIM_RESTART
) {
8378 mutex_enter(&spa_namespace_lock
);
8379 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8380 vdev_autotrim_restart(spa
);
8381 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8382 mutex_exit(&spa_namespace_lock
);
8386 * Kick off L2 cache whole device TRIM.
8388 if (tasks
& SPA_ASYNC_L2CACHE_TRIM
) {
8389 mutex_enter(&spa_namespace_lock
);
8390 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8391 vdev_trim_l2arc(spa
);
8392 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8393 mutex_exit(&spa_namespace_lock
);
8397 * Kick off L2 cache rebuilding.
8399 if (tasks
& SPA_ASYNC_L2CACHE_REBUILD
) {
8400 mutex_enter(&spa_namespace_lock
);
8401 spa_config_enter(spa
, SCL_L2ARC
, FTAG
, RW_READER
);
8402 l2arc_spa_rebuild_start(spa
);
8403 spa_config_exit(spa
, SCL_L2ARC
, FTAG
);
8404 mutex_exit(&spa_namespace_lock
);
8408 * Let the world know that we're done.
8410 mutex_enter(&spa
->spa_async_lock
);
8411 spa
->spa_async_thread
= NULL
;
8412 cv_broadcast(&spa
->spa_async_cv
);
8413 mutex_exit(&spa
->spa_async_lock
);
8418 spa_async_suspend(spa_t
*spa
)
8420 mutex_enter(&spa
->spa_async_lock
);
8421 spa
->spa_async_suspended
++;
8422 while (spa
->spa_async_thread
!= NULL
)
8423 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
8424 mutex_exit(&spa
->spa_async_lock
);
8426 spa_vdev_remove_suspend(spa
);
8428 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8429 if (condense_thread
!= NULL
)
8430 zthr_cancel(condense_thread
);
8432 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8433 if (discard_thread
!= NULL
)
8434 zthr_cancel(discard_thread
);
8436 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8437 if (ll_delete_thread
!= NULL
)
8438 zthr_cancel(ll_delete_thread
);
8440 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8441 if (ll_condense_thread
!= NULL
)
8442 zthr_cancel(ll_condense_thread
);
8446 spa_async_resume(spa_t
*spa
)
8448 mutex_enter(&spa
->spa_async_lock
);
8449 ASSERT(spa
->spa_async_suspended
!= 0);
8450 spa
->spa_async_suspended
--;
8451 mutex_exit(&spa
->spa_async_lock
);
8452 spa_restart_removal(spa
);
8454 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8455 if (condense_thread
!= NULL
)
8456 zthr_resume(condense_thread
);
8458 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8459 if (discard_thread
!= NULL
)
8460 zthr_resume(discard_thread
);
8462 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8463 if (ll_delete_thread
!= NULL
)
8464 zthr_resume(ll_delete_thread
);
8466 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8467 if (ll_condense_thread
!= NULL
)
8468 zthr_resume(ll_condense_thread
);
8472 spa_async_tasks_pending(spa_t
*spa
)
8474 uint_t non_config_tasks
;
8476 boolean_t config_task_suspended
;
8478 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
8479 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
8480 if (spa
->spa_ccw_fail_time
== 0) {
8481 config_task_suspended
= B_FALSE
;
8483 config_task_suspended
=
8484 (gethrtime() - spa
->spa_ccw_fail_time
) <
8485 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
8488 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
8492 spa_async_dispatch(spa_t
*spa
)
8494 mutex_enter(&spa
->spa_async_lock
);
8495 if (spa_async_tasks_pending(spa
) &&
8496 !spa
->spa_async_suspended
&&
8497 spa
->spa_async_thread
== NULL
)
8498 spa
->spa_async_thread
= thread_create(NULL
, 0,
8499 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
8500 mutex_exit(&spa
->spa_async_lock
);
8504 spa_async_request(spa_t
*spa
, int task
)
8506 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
8507 mutex_enter(&spa
->spa_async_lock
);
8508 spa
->spa_async_tasks
|= task
;
8509 mutex_exit(&spa
->spa_async_lock
);
8513 spa_async_tasks(spa_t
*spa
)
8515 return (spa
->spa_async_tasks
);
8519 * ==========================================================================
8520 * SPA syncing routines
8521 * ==========================================================================
8526 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8530 bpobj_enqueue(bpo
, bp
, bp_freed
, tx
);
8535 bpobj_enqueue_alloc_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8537 return (bpobj_enqueue_cb(arg
, bp
, B_FALSE
, tx
));
8541 bpobj_enqueue_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8543 return (bpobj_enqueue_cb(arg
, bp
, B_TRUE
, tx
));
8547 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8551 zio_nowait(zio_free_sync(pio
, pio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
8557 bpobj_spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8561 return (spa_free_sync_cb(arg
, bp
, tx
));
8565 * Note: this simple function is not inlined to make it easier to dtrace the
8566 * amount of time spent syncing frees.
8569 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
8571 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8572 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
8573 VERIFY(zio_wait(zio
) == 0);
8577 * Note: this simple function is not inlined to make it easier to dtrace the
8578 * amount of time spent syncing deferred frees.
8581 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
8583 if (spa_sync_pass(spa
) != 1)
8588 * If the log space map feature is active, we stop deferring
8589 * frees to the next TXG and therefore running this function
8590 * would be considered a no-op as spa_deferred_bpobj should
8591 * not have any entries.
8593 * That said we run this function anyway (instead of returning
8594 * immediately) for the edge-case scenario where we just
8595 * activated the log space map feature in this TXG but we have
8596 * deferred frees from the previous TXG.
8598 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8599 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
8600 bpobj_spa_free_sync_cb
, zio
, tx
), ==, 0);
8601 VERIFY0(zio_wait(zio
));
8605 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
8607 char *packed
= NULL
;
8612 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
8615 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
8616 * information. This avoids the dmu_buf_will_dirty() path and
8617 * saves us a pre-read to get data we don't actually care about.
8619 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
8620 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
8622 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
8624 memset(packed
+ nvsize
, 0, bufsize
- nvsize
);
8626 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
8628 vmem_free(packed
, bufsize
);
8630 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
8631 dmu_buf_will_dirty(db
, tx
);
8632 *(uint64_t *)db
->db_data
= nvsize
;
8633 dmu_buf_rele(db
, FTAG
);
8637 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
8638 const char *config
, const char *entry
)
8648 * Update the MOS nvlist describing the list of available devices.
8649 * spa_validate_aux() will have already made sure this nvlist is
8650 * valid and the vdevs are labeled appropriately.
8652 if (sav
->sav_object
== 0) {
8653 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
8654 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
8655 sizeof (uint64_t), tx
);
8656 VERIFY(zap_update(spa
->spa_meta_objset
,
8657 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
8658 &sav
->sav_object
, tx
) == 0);
8661 nvroot
= fnvlist_alloc();
8662 if (sav
->sav_count
== 0) {
8663 fnvlist_add_nvlist_array(nvroot
, config
,
8664 (const nvlist_t
* const *)NULL
, 0);
8666 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
8667 for (i
= 0; i
< sav
->sav_count
; i
++)
8668 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
8669 B_FALSE
, VDEV_CONFIG_L2CACHE
);
8670 fnvlist_add_nvlist_array(nvroot
, config
,
8671 (const nvlist_t
* const *)list
, sav
->sav_count
);
8672 for (i
= 0; i
< sav
->sav_count
; i
++)
8673 nvlist_free(list
[i
]);
8674 kmem_free(list
, sav
->sav_count
* sizeof (void *));
8677 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
8678 nvlist_free(nvroot
);
8680 sav
->sav_sync
= B_FALSE
;
8684 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
8685 * The all-vdev ZAP must be empty.
8688 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
8690 spa_t
*spa
= vd
->vdev_spa
;
8692 if (vd
->vdev_root_zap
!= 0 &&
8693 spa_feature_is_active(spa
, SPA_FEATURE_AVZ_V2
)) {
8694 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8695 vd
->vdev_root_zap
, tx
));
8697 if (vd
->vdev_top_zap
!= 0) {
8698 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8699 vd
->vdev_top_zap
, tx
));
8701 if (vd
->vdev_leaf_zap
!= 0) {
8702 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8703 vd
->vdev_leaf_zap
, tx
));
8705 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
8706 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
8711 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
8716 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
8717 * its config may not be dirty but we still need to build per-vdev ZAPs.
8718 * Similarly, if the pool is being assembled (e.g. after a split), we
8719 * need to rebuild the AVZ although the config may not be dirty.
8721 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
8722 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
8725 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
8727 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
8728 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
8729 spa
->spa_all_vdev_zaps
!= 0);
8731 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
8732 /* Make and build the new AVZ */
8733 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
8734 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
8735 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
8737 /* Diff old AVZ with new one */
8741 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8742 spa
->spa_all_vdev_zaps
);
8743 zap_cursor_retrieve(&zc
, &za
) == 0;
8744 zap_cursor_advance(&zc
)) {
8745 uint64_t vdzap
= za
.za_first_integer
;
8746 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
8749 * ZAP is listed in old AVZ but not in new one;
8752 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
8757 zap_cursor_fini(&zc
);
8759 /* Destroy the old AVZ */
8760 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8761 spa
->spa_all_vdev_zaps
, tx
));
8763 /* Replace the old AVZ in the dir obj with the new one */
8764 VERIFY0(zap_update(spa
->spa_meta_objset
,
8765 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
8766 sizeof (new_avz
), 1, &new_avz
, tx
));
8768 spa
->spa_all_vdev_zaps
= new_avz
;
8769 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
8773 /* Walk through the AVZ and destroy all listed ZAPs */
8774 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8775 spa
->spa_all_vdev_zaps
);
8776 zap_cursor_retrieve(&zc
, &za
) == 0;
8777 zap_cursor_advance(&zc
)) {
8778 uint64_t zap
= za
.za_first_integer
;
8779 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
8782 zap_cursor_fini(&zc
);
8784 /* Destroy and unlink the AVZ itself */
8785 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8786 spa
->spa_all_vdev_zaps
, tx
));
8787 VERIFY0(zap_remove(spa
->spa_meta_objset
,
8788 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
8789 spa
->spa_all_vdev_zaps
= 0;
8792 if (spa
->spa_all_vdev_zaps
== 0) {
8793 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
8794 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
8795 DMU_POOL_VDEV_ZAP_MAP
, tx
);
8797 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
8799 /* Create ZAPs for vdevs that don't have them. */
8800 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
8802 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
8803 dmu_tx_get_txg(tx
), B_FALSE
);
8806 * If we're upgrading the spa version then make sure that
8807 * the config object gets updated with the correct version.
8809 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
8810 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
8811 spa
->spa_uberblock
.ub_version
);
8813 spa_config_exit(spa
, SCL_STATE
, FTAG
);
8815 nvlist_free(spa
->spa_config_syncing
);
8816 spa
->spa_config_syncing
= config
;
8818 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
8822 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
8824 uint64_t *versionp
= arg
;
8825 uint64_t version
= *versionp
;
8826 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8829 * Setting the version is special cased when first creating the pool.
8831 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
8833 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
8834 ASSERT(version
>= spa_version(spa
));
8836 spa
->spa_uberblock
.ub_version
= version
;
8837 vdev_config_dirty(spa
->spa_root_vdev
);
8838 spa_history_log_internal(spa
, "set", tx
, "version=%lld",
8839 (longlong_t
)version
);
8843 * Set zpool properties.
8846 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
8848 nvlist_t
*nvp
= arg
;
8849 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8850 objset_t
*mos
= spa
->spa_meta_objset
;
8851 nvpair_t
*elem
= NULL
;
8853 mutex_enter(&spa
->spa_props_lock
);
8855 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
8857 const char *strval
, *fname
;
8859 const char *propname
;
8860 const char *elemname
= nvpair_name(elem
);
8861 zprop_type_t proptype
;
8864 switch (prop
= zpool_name_to_prop(elemname
)) {
8865 case ZPOOL_PROP_VERSION
:
8866 intval
= fnvpair_value_uint64(elem
);
8868 * The version is synced separately before other
8869 * properties and should be correct by now.
8871 ASSERT3U(spa_version(spa
), >=, intval
);
8874 case ZPOOL_PROP_ALTROOT
:
8876 * 'altroot' is a non-persistent property. It should
8877 * have been set temporarily at creation or import time.
8879 ASSERT(spa
->spa_root
!= NULL
);
8882 case ZPOOL_PROP_READONLY
:
8883 case ZPOOL_PROP_CACHEFILE
:
8885 * 'readonly' and 'cachefile' are also non-persistent
8889 case ZPOOL_PROP_COMMENT
:
8890 strval
= fnvpair_value_string(elem
);
8891 if (spa
->spa_comment
!= NULL
)
8892 spa_strfree(spa
->spa_comment
);
8893 spa
->spa_comment
= spa_strdup(strval
);
8895 * We need to dirty the configuration on all the vdevs
8896 * so that their labels get updated. We also need to
8897 * update the cache file to keep it in sync with the
8898 * MOS version. It's unnecessary to do this for pool
8899 * creation since the vdev's configuration has already
8902 if (tx
->tx_txg
!= TXG_INITIAL
) {
8903 vdev_config_dirty(spa
->spa_root_vdev
);
8904 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
8906 spa_history_log_internal(spa
, "set", tx
,
8907 "%s=%s", elemname
, strval
);
8909 case ZPOOL_PROP_COMPATIBILITY
:
8910 strval
= fnvpair_value_string(elem
);
8911 if (spa
->spa_compatibility
!= NULL
)
8912 spa_strfree(spa
->spa_compatibility
);
8913 spa
->spa_compatibility
= spa_strdup(strval
);
8915 * Dirty the configuration on vdevs as above.
8917 if (tx
->tx_txg
!= TXG_INITIAL
) {
8918 vdev_config_dirty(spa
->spa_root_vdev
);
8919 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
8922 spa_history_log_internal(spa
, "set", tx
,
8923 "%s=%s", nvpair_name(elem
), strval
);
8926 case ZPOOL_PROP_INVAL
:
8927 if (zpool_prop_feature(elemname
)) {
8928 fname
= strchr(elemname
, '@') + 1;
8929 VERIFY0(zfeature_lookup_name(fname
, &fid
));
8931 spa_feature_enable(spa
, fid
, tx
);
8932 spa_history_log_internal(spa
, "set", tx
,
8933 "%s=enabled", elemname
);
8935 } else if (!zfs_prop_user(elemname
)) {
8936 ASSERT(zpool_prop_feature(elemname
));
8942 * Set pool property values in the poolprops mos object.
8944 if (spa
->spa_pool_props_object
== 0) {
8945 spa
->spa_pool_props_object
=
8946 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
8947 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
8951 /* normalize the property name */
8952 if (prop
== ZPOOL_PROP_INVAL
) {
8953 propname
= elemname
;
8954 proptype
= PROP_TYPE_STRING
;
8956 propname
= zpool_prop_to_name(prop
);
8957 proptype
= zpool_prop_get_type(prop
);
8960 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
8961 ASSERT(proptype
== PROP_TYPE_STRING
);
8962 strval
= fnvpair_value_string(elem
);
8963 VERIFY0(zap_update(mos
,
8964 spa
->spa_pool_props_object
, propname
,
8965 1, strlen(strval
) + 1, strval
, tx
));
8966 spa_history_log_internal(spa
, "set", tx
,
8967 "%s=%s", elemname
, strval
);
8968 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
8969 intval
= fnvpair_value_uint64(elem
);
8971 if (proptype
== PROP_TYPE_INDEX
) {
8973 VERIFY0(zpool_prop_index_to_string(
8974 prop
, intval
, &unused
));
8976 VERIFY0(zap_update(mos
,
8977 spa
->spa_pool_props_object
, propname
,
8978 8, 1, &intval
, tx
));
8979 spa_history_log_internal(spa
, "set", tx
,
8980 "%s=%lld", elemname
,
8981 (longlong_t
)intval
);
8984 case ZPOOL_PROP_DELEGATION
:
8985 spa
->spa_delegation
= intval
;
8987 case ZPOOL_PROP_BOOTFS
:
8988 spa
->spa_bootfs
= intval
;
8990 case ZPOOL_PROP_FAILUREMODE
:
8991 spa
->spa_failmode
= intval
;
8993 case ZPOOL_PROP_AUTOTRIM
:
8994 spa
->spa_autotrim
= intval
;
8995 spa_async_request(spa
,
8996 SPA_ASYNC_AUTOTRIM_RESTART
);
8998 case ZPOOL_PROP_AUTOEXPAND
:
8999 spa
->spa_autoexpand
= intval
;
9000 if (tx
->tx_txg
!= TXG_INITIAL
)
9001 spa_async_request(spa
,
9002 SPA_ASYNC_AUTOEXPAND
);
9004 case ZPOOL_PROP_MULTIHOST
:
9005 spa
->spa_multihost
= intval
;
9011 ASSERT(0); /* not allowed */
9017 mutex_exit(&spa
->spa_props_lock
);
9021 * Perform one-time upgrade on-disk changes. spa_version() does not
9022 * reflect the new version this txg, so there must be no changes this
9023 * txg to anything that the upgrade code depends on after it executes.
9024 * Therefore this must be called after dsl_pool_sync() does the sync
9028 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
9030 if (spa_sync_pass(spa
) != 1)
9033 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9034 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
9036 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
9037 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
9038 dsl_pool_create_origin(dp
, tx
);
9040 /* Keeping the origin open increases spa_minref */
9041 spa
->spa_minref
+= 3;
9044 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
9045 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
9046 dsl_pool_upgrade_clones(dp
, tx
);
9049 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
9050 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
9051 dsl_pool_upgrade_dir_clones(dp
, tx
);
9053 /* Keeping the freedir open increases spa_minref */
9054 spa
->spa_minref
+= 3;
9057 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
9058 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9059 spa_feature_create_zap_objects(spa
, tx
);
9063 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
9064 * when possibility to use lz4 compression for metadata was added
9065 * Old pools that have this feature enabled must be upgraded to have
9066 * this feature active
9068 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9069 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
9070 SPA_FEATURE_LZ4_COMPRESS
);
9071 boolean_t lz4_ac
= spa_feature_is_active(spa
,
9072 SPA_FEATURE_LZ4_COMPRESS
);
9074 if (lz4_en
&& !lz4_ac
)
9075 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
9079 * If we haven't written the salt, do so now. Note that the
9080 * feature may not be activated yet, but that's fine since
9081 * the presence of this ZAP entry is backwards compatible.
9083 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
9084 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
9085 VERIFY0(zap_add(spa
->spa_meta_objset
,
9086 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
9087 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
9088 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
9091 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
9095 vdev_indirect_state_sync_verify(vdev_t
*vd
)
9097 vdev_indirect_mapping_t
*vim __maybe_unused
= vd
->vdev_indirect_mapping
;
9098 vdev_indirect_births_t
*vib __maybe_unused
= vd
->vdev_indirect_births
;
9100 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
9101 ASSERT(vim
!= NULL
);
9102 ASSERT(vib
!= NULL
);
9105 uint64_t obsolete_sm_object
= 0;
9106 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
9107 if (obsolete_sm_object
!= 0) {
9108 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
9109 ASSERT(vd
->vdev_removing
||
9110 vd
->vdev_ops
== &vdev_indirect_ops
);
9111 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
9112 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
9113 ASSERT3U(obsolete_sm_object
, ==,
9114 space_map_object(vd
->vdev_obsolete_sm
));
9115 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
9116 space_map_allocated(vd
->vdev_obsolete_sm
));
9118 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
9121 * Since frees / remaps to an indirect vdev can only
9122 * happen in syncing context, the obsolete segments
9123 * tree must be empty when we start syncing.
9125 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
9129 * Set the top-level vdev's max queue depth. Evaluate each top-level's
9130 * async write queue depth in case it changed. The max queue depth will
9131 * not change in the middle of syncing out this txg.
9134 spa_sync_adjust_vdev_max_queue_depth(spa_t
*spa
)
9136 ASSERT(spa_writeable(spa
));
9138 vdev_t
*rvd
= spa
->spa_root_vdev
;
9139 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
9140 zfs_vdev_queue_depth_pct
/ 100;
9141 metaslab_class_t
*normal
= spa_normal_class(spa
);
9142 metaslab_class_t
*special
= spa_special_class(spa
);
9143 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
9145 uint64_t slots_per_allocator
= 0;
9146 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9147 vdev_t
*tvd
= rvd
->vdev_child
[c
];
9149 metaslab_group_t
*mg
= tvd
->vdev_mg
;
9150 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
9153 metaslab_class_t
*mc
= mg
->mg_class
;
9154 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
9158 * It is safe to do a lock-free check here because only async
9159 * allocations look at mg_max_alloc_queue_depth, and async
9160 * allocations all happen from spa_sync().
9162 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9163 ASSERT0(zfs_refcount_count(
9164 &(mg
->mg_allocator
[i
].mga_alloc_queue_depth
)));
9166 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
9168 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9169 mg
->mg_allocator
[i
].mga_cur_max_alloc_queue_depth
=
9170 zfs_vdev_def_queue_depth
;
9172 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
9175 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9176 ASSERT0(zfs_refcount_count(&normal
->mc_allocator
[i
].
9178 ASSERT0(zfs_refcount_count(&special
->mc_allocator
[i
].
9180 ASSERT0(zfs_refcount_count(&dedup
->mc_allocator
[i
].
9182 normal
->mc_allocator
[i
].mca_alloc_max_slots
=
9183 slots_per_allocator
;
9184 special
->mc_allocator
[i
].mca_alloc_max_slots
=
9185 slots_per_allocator
;
9186 dedup
->mc_allocator
[i
].mca_alloc_max_slots
=
9187 slots_per_allocator
;
9189 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9190 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9191 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9195 spa_sync_condense_indirect(spa_t
*spa
, dmu_tx_t
*tx
)
9197 ASSERT(spa_writeable(spa
));
9199 vdev_t
*rvd
= spa
->spa_root_vdev
;
9200 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9201 vdev_t
*vd
= rvd
->vdev_child
[c
];
9202 vdev_indirect_state_sync_verify(vd
);
9204 if (vdev_indirect_should_condense(vd
)) {
9205 spa_condense_indirect_start_sync(vd
, tx
);
9212 spa_sync_iterate_to_convergence(spa_t
*spa
, dmu_tx_t
*tx
)
9214 objset_t
*mos
= spa
->spa_meta_objset
;
9215 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9216 uint64_t txg
= tx
->tx_txg
;
9217 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
9220 int pass
= ++spa
->spa_sync_pass
;
9222 spa_sync_config_object(spa
, tx
);
9223 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
9224 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
9225 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
9226 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
9227 spa_errlog_sync(spa
, txg
);
9228 dsl_pool_sync(dp
, txg
);
9230 if (pass
< zfs_sync_pass_deferred_free
||
9231 spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
)) {
9233 * If the log space map feature is active we don't
9234 * care about deferred frees and the deferred bpobj
9235 * as the log space map should effectively have the
9236 * same results (i.e. appending only to one object).
9238 spa_sync_frees(spa
, free_bpl
, tx
);
9241 * We can not defer frees in pass 1, because
9242 * we sync the deferred frees later in pass 1.
9244 ASSERT3U(pass
, >, 1);
9245 bplist_iterate(free_bpl
, bpobj_enqueue_alloc_cb
,
9246 &spa
->spa_deferred_bpobj
, tx
);
9251 dsl_scan_sync(dp
, tx
);
9253 spa_sync_upgrades(spa
, tx
);
9255 spa_flush_metaslabs(spa
, tx
);
9258 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
9263 * Note: We need to check if the MOS is dirty because we could
9264 * have marked the MOS dirty without updating the uberblock
9265 * (e.g. if we have sync tasks but no dirty user data). We need
9266 * to check the uberblock's rootbp because it is updated if we
9267 * have synced out dirty data (though in this case the MOS will
9268 * most likely also be dirty due to second order effects, we
9269 * don't want to rely on that here).
9272 spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
9273 !dmu_objset_is_dirty(mos
, txg
)) {
9275 * Nothing changed on the first pass, therefore this
9276 * TXG is a no-op. Avoid syncing deferred frees, so
9277 * that we can keep this TXG as a no-op.
9279 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9280 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9281 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
9282 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
, txg
));
9286 spa_sync_deferred_frees(spa
, tx
);
9287 } while (dmu_objset_is_dirty(mos
, txg
));
9291 * Rewrite the vdev configuration (which includes the uberblock) to
9292 * commit the transaction group.
9294 * If there are no dirty vdevs, we sync the uberblock to a few random
9295 * top-level vdevs that are known to be visible in the config cache
9296 * (see spa_vdev_add() for a complete description). If there *are* dirty
9297 * vdevs, sync the uberblock to all vdevs.
9300 spa_sync_rewrite_vdev_config(spa_t
*spa
, dmu_tx_t
*tx
)
9302 vdev_t
*rvd
= spa
->spa_root_vdev
;
9303 uint64_t txg
= tx
->tx_txg
;
9309 * We hold SCL_STATE to prevent vdev open/close/etc.
9310 * while we're attempting to write the vdev labels.
9312 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9314 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
9315 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
9317 int children
= rvd
->vdev_children
;
9318 int c0
= random_in_range(children
);
9320 for (int c
= 0; c
< children
; c
++) {
9322 rvd
->vdev_child
[(c0
+ c
) % children
];
9324 /* Stop when revisiting the first vdev */
9325 if (c
> 0 && svd
[0] == vd
)
9328 if (vd
->vdev_ms_array
== 0 ||
9330 !vdev_is_concrete(vd
))
9333 svd
[svdcount
++] = vd
;
9334 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
9337 error
= vdev_config_sync(svd
, svdcount
, txg
);
9339 error
= vdev_config_sync(rvd
->vdev_child
,
9340 rvd
->vdev_children
, txg
);
9344 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
9346 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9350 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
9351 zio_resume_wait(spa
);
9356 * Sync the specified transaction group. New blocks may be dirtied as
9357 * part of the process, so we iterate until it converges.
9360 spa_sync(spa_t
*spa
, uint64_t txg
)
9364 VERIFY(spa_writeable(spa
));
9367 * Wait for i/os issued in open context that need to complete
9368 * before this txg syncs.
9370 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
9371 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
9375 * Now that there can be no more cloning in this transaction group,
9376 * but we are still before issuing frees, we can process pending BRT
9379 brt_pending_apply(spa
, txg
);
9382 * Lock out configuration changes.
9384 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9386 spa
->spa_syncing_txg
= txg
;
9387 spa
->spa_sync_pass
= 0;
9389 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9390 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
9391 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
9392 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
9396 * If there are any pending vdev state changes, convert them
9397 * into config changes that go out with this transaction group.
9399 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9400 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9401 /* Avoid holding the write lock unless actually necessary */
9402 if (vd
->vdev_aux
== NULL
) {
9403 vdev_state_clean(vd
);
9404 vdev_config_dirty(vd
);
9408 * We need the write lock here because, for aux vdevs,
9409 * calling vdev_config_dirty() modifies sav_config.
9410 * This is ugly and will become unnecessary when we
9411 * eliminate the aux vdev wart by integrating all vdevs
9412 * into the root vdev tree.
9414 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9415 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
9416 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9417 vdev_state_clean(vd
);
9418 vdev_config_dirty(vd
);
9420 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9421 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9423 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9425 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9426 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
9428 spa
->spa_sync_starttime
= gethrtime();
9429 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9430 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
9431 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
9432 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
9435 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
9436 * set spa_deflate if we have no raid-z vdevs.
9438 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
9439 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
9440 vdev_t
*rvd
= spa
->spa_root_vdev
;
9443 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
9444 vd
= rvd
->vdev_child
[i
];
9445 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
9448 if (i
== rvd
->vdev_children
) {
9449 spa
->spa_deflate
= TRUE
;
9450 VERIFY0(zap_add(spa
->spa_meta_objset
,
9451 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
9452 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
9456 spa_sync_adjust_vdev_max_queue_depth(spa
);
9458 spa_sync_condense_indirect(spa
, tx
);
9460 spa_sync_iterate_to_convergence(spa
, tx
);
9463 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
9465 * Make sure that the number of ZAPs for all the vdevs matches
9466 * the number of ZAPs in the per-vdev ZAP list. This only gets
9467 * called if the config is dirty; otherwise there may be
9468 * outstanding AVZ operations that weren't completed in
9469 * spa_sync_config_object.
9471 uint64_t all_vdev_zap_entry_count
;
9472 ASSERT0(zap_count(spa
->spa_meta_objset
,
9473 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
9474 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
9475 all_vdev_zap_entry_count
);
9479 if (spa
->spa_vdev_removal
!= NULL
) {
9480 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
9483 spa_sync_rewrite_vdev_config(spa
, tx
);
9486 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9487 spa
->spa_deadman_tqid
= 0;
9490 * Clear the dirty config list.
9492 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
9493 vdev_config_clean(vd
);
9496 * Now that the new config has synced transactionally,
9497 * let it become visible to the config cache.
9499 if (spa
->spa_config_syncing
!= NULL
) {
9500 spa_config_set(spa
, spa
->spa_config_syncing
);
9501 spa
->spa_config_txg
= txg
;
9502 spa
->spa_config_syncing
= NULL
;
9505 dsl_pool_sync_done(dp
, txg
);
9507 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9508 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
9509 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
9510 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
9514 * Update usable space statistics.
9516 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
9518 vdev_sync_done(vd
, txg
);
9520 metaslab_class_evict_old(spa
->spa_normal_class
, txg
);
9521 metaslab_class_evict_old(spa
->spa_log_class
, txg
);
9523 spa_sync_close_syncing_log_sm(spa
);
9525 spa_update_dspace(spa
);
9527 if (spa_get_autotrim(spa
) == SPA_AUTOTRIM_ON
)
9528 vdev_autotrim_kick(spa
);
9531 * It had better be the case that we didn't dirty anything
9532 * since vdev_config_sync().
9534 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9535 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9536 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
9538 while (zfs_pause_spa_sync
)
9541 spa
->spa_sync_pass
= 0;
9544 * Update the last synced uberblock here. We want to do this at
9545 * the end of spa_sync() so that consumers of spa_last_synced_txg()
9546 * will be guaranteed that all the processing associated with
9547 * that txg has been completed.
9549 spa
->spa_ubsync
= spa
->spa_uberblock
;
9550 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9552 spa_handle_ignored_writes(spa
);
9555 * If any async tasks have been requested, kick them off.
9557 spa_async_dispatch(spa
);
9561 * Sync all pools. We don't want to hold the namespace lock across these
9562 * operations, so we take a reference on the spa_t and drop the lock during the
9566 spa_sync_allpools(void)
9569 mutex_enter(&spa_namespace_lock
);
9570 while ((spa
= spa_next(spa
)) != NULL
) {
9571 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
9572 !spa_writeable(spa
) || spa_suspended(spa
))
9574 spa_open_ref(spa
, FTAG
);
9575 mutex_exit(&spa_namespace_lock
);
9576 txg_wait_synced(spa_get_dsl(spa
), 0);
9577 mutex_enter(&spa_namespace_lock
);
9578 spa_close(spa
, FTAG
);
9580 mutex_exit(&spa_namespace_lock
);
9584 * ==========================================================================
9585 * Miscellaneous routines
9586 * ==========================================================================
9590 * Remove all pools in the system.
9598 * Remove all cached state. All pools should be closed now,
9599 * so every spa in the AVL tree should be unreferenced.
9601 mutex_enter(&spa_namespace_lock
);
9602 while ((spa
= spa_next(NULL
)) != NULL
) {
9604 * Stop async tasks. The async thread may need to detach
9605 * a device that's been replaced, which requires grabbing
9606 * spa_namespace_lock, so we must drop it here.
9608 spa_open_ref(spa
, FTAG
);
9609 mutex_exit(&spa_namespace_lock
);
9610 spa_async_suspend(spa
);
9611 mutex_enter(&spa_namespace_lock
);
9612 spa_close(spa
, FTAG
);
9614 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
9616 spa_deactivate(spa
);
9620 mutex_exit(&spa_namespace_lock
);
9624 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
9629 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
9633 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
9634 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
9635 if (vd
->vdev_guid
== guid
)
9639 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
9640 vd
= spa
->spa_spares
.sav_vdevs
[i
];
9641 if (vd
->vdev_guid
== guid
)
9650 spa_upgrade(spa_t
*spa
, uint64_t version
)
9652 ASSERT(spa_writeable(spa
));
9654 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
9657 * This should only be called for a non-faulted pool, and since a
9658 * future version would result in an unopenable pool, this shouldn't be
9661 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
9662 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
9664 spa
->spa_uberblock
.ub_version
= version
;
9665 vdev_config_dirty(spa
->spa_root_vdev
);
9667 spa_config_exit(spa
, SCL_ALL
, FTAG
);
9669 txg_wait_synced(spa_get_dsl(spa
), 0);
9673 spa_has_aux_vdev(spa_t
*spa
, uint64_t guid
, spa_aux_vdev_t
*sav
)
9679 for (i
= 0; i
< sav
->sav_count
; i
++)
9680 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
9683 for (i
= 0; i
< sav
->sav_npending
; i
++) {
9684 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
9685 &vdev_guid
) == 0 && vdev_guid
== guid
)
9693 spa_has_l2cache(spa_t
*spa
, uint64_t guid
)
9695 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_l2cache
));
9699 spa_has_spare(spa_t
*spa
, uint64_t guid
)
9701 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_spares
));
9705 * Check if a pool has an active shared spare device.
9706 * Note: reference count of an active spare is 2, as a spare and as a replace
9709 spa_has_active_shared_spare(spa_t
*spa
)
9713 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
9715 for (i
= 0; i
< sav
->sav_count
; i
++) {
9716 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
9717 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
9726 spa_total_metaslabs(spa_t
*spa
)
9728 vdev_t
*rvd
= spa
->spa_root_vdev
;
9731 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
9732 vdev_t
*vd
= rvd
->vdev_child
[c
];
9733 if (!vdev_is_concrete(vd
))
9735 m
+= vd
->vdev_ms_count
;
9741 * Notify any waiting threads that some activity has switched from being in-
9742 * progress to not-in-progress so that the thread can wake up and determine
9743 * whether it is finished waiting.
9746 spa_notify_waiters(spa_t
*spa
)
9749 * Acquiring spa_activities_lock here prevents the cv_broadcast from
9750 * happening between the waiting thread's check and cv_wait.
9752 mutex_enter(&spa
->spa_activities_lock
);
9753 cv_broadcast(&spa
->spa_activities_cv
);
9754 mutex_exit(&spa
->spa_activities_lock
);
9758 * Notify any waiting threads that the pool is exporting, and then block until
9759 * they are finished using the spa_t.
9762 spa_wake_waiters(spa_t
*spa
)
9764 mutex_enter(&spa
->spa_activities_lock
);
9765 spa
->spa_waiters_cancel
= B_TRUE
;
9766 cv_broadcast(&spa
->spa_activities_cv
);
9767 while (spa
->spa_waiters
!= 0)
9768 cv_wait(&spa
->spa_waiters_cv
, &spa
->spa_activities_lock
);
9769 spa
->spa_waiters_cancel
= B_FALSE
;
9770 mutex_exit(&spa
->spa_activities_lock
);
9773 /* Whether the vdev or any of its descendants are being initialized/trimmed. */
9775 spa_vdev_activity_in_progress_impl(vdev_t
*vd
, zpool_wait_activity_t activity
)
9777 spa_t
*spa
= vd
->vdev_spa
;
9779 ASSERT(spa_config_held(spa
, SCL_CONFIG
| SCL_STATE
, RW_READER
));
9780 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9781 ASSERT(activity
== ZPOOL_WAIT_INITIALIZE
||
9782 activity
== ZPOOL_WAIT_TRIM
);
9784 kmutex_t
*lock
= activity
== ZPOOL_WAIT_INITIALIZE
?
9785 &vd
->vdev_initialize_lock
: &vd
->vdev_trim_lock
;
9787 mutex_exit(&spa
->spa_activities_lock
);
9789 mutex_enter(&spa
->spa_activities_lock
);
9791 boolean_t in_progress
= (activity
== ZPOOL_WAIT_INITIALIZE
) ?
9792 (vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) :
9793 (vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
);
9799 for (int i
= 0; i
< vd
->vdev_children
; i
++) {
9800 if (spa_vdev_activity_in_progress_impl(vd
->vdev_child
[i
],
9809 * If use_guid is true, this checks whether the vdev specified by guid is
9810 * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
9811 * is being initialized/trimmed. The caller must hold the config lock and
9812 * spa_activities_lock.
9815 spa_vdev_activity_in_progress(spa_t
*spa
, boolean_t use_guid
, uint64_t guid
,
9816 zpool_wait_activity_t activity
, boolean_t
*in_progress
)
9818 mutex_exit(&spa
->spa_activities_lock
);
9819 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9820 mutex_enter(&spa
->spa_activities_lock
);
9824 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
9825 if (vd
== NULL
|| !vd
->vdev_ops
->vdev_op_leaf
) {
9826 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9830 vd
= spa
->spa_root_vdev
;
9833 *in_progress
= spa_vdev_activity_in_progress_impl(vd
, activity
);
9835 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9840 * Locking for waiting threads
9841 * ---------------------------
9843 * Waiting threads need a way to check whether a given activity is in progress,
9844 * and then, if it is, wait for it to complete. Each activity will have some
9845 * in-memory representation of the relevant on-disk state which can be used to
9846 * determine whether or not the activity is in progress. The in-memory state and
9847 * the locking used to protect it will be different for each activity, and may
9848 * not be suitable for use with a cvar (e.g., some state is protected by the
9849 * config lock). To allow waiting threads to wait without any races, another
9850 * lock, spa_activities_lock, is used.
9852 * When the state is checked, both the activity-specific lock (if there is one)
9853 * and spa_activities_lock are held. In some cases, the activity-specific lock
9854 * is acquired explicitly (e.g. the config lock). In others, the locking is
9855 * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
9856 * thread releases the activity-specific lock and, if the activity is in
9857 * progress, then cv_waits using spa_activities_lock.
9859 * The waiting thread is woken when another thread, one completing some
9860 * activity, updates the state of the activity and then calls
9861 * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
9862 * needs to hold its activity-specific lock when updating the state, and this
9863 * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
9865 * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
9866 * and because it is held when the waiting thread checks the state of the
9867 * activity, it can never be the case that the completing thread both updates
9868 * the activity state and cv_broadcasts in between the waiting thread's check
9869 * and cv_wait. Thus, a waiting thread can never miss a wakeup.
9871 * In order to prevent deadlock, when the waiting thread does its check, in some
9872 * cases it will temporarily drop spa_activities_lock in order to acquire the
9873 * activity-specific lock. The order in which spa_activities_lock and the
9874 * activity specific lock are acquired in the waiting thread is determined by
9875 * the order in which they are acquired in the completing thread; if the
9876 * completing thread calls spa_notify_waiters with the activity-specific lock
9877 * held, then the waiting thread must also acquire the activity-specific lock
9882 spa_activity_in_progress(spa_t
*spa
, zpool_wait_activity_t activity
,
9883 boolean_t use_tag
, uint64_t tag
, boolean_t
*in_progress
)
9887 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9890 case ZPOOL_WAIT_CKPT_DISCARD
:
9892 (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
) &&
9893 zap_contains(spa_meta_objset(spa
),
9894 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_ZPOOL_CHECKPOINT
) ==
9897 case ZPOOL_WAIT_FREE
:
9898 *in_progress
= ((spa_version(spa
) >= SPA_VERSION_DEADLISTS
&&
9899 !bpobj_is_empty(&spa
->spa_dsl_pool
->dp_free_bpobj
)) ||
9900 spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
) ||
9901 spa_livelist_delete_check(spa
));
9903 case ZPOOL_WAIT_INITIALIZE
:
9904 case ZPOOL_WAIT_TRIM
:
9905 error
= spa_vdev_activity_in_progress(spa
, use_tag
, tag
,
9906 activity
, in_progress
);
9908 case ZPOOL_WAIT_REPLACE
:
9909 mutex_exit(&spa
->spa_activities_lock
);
9910 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9911 mutex_enter(&spa
->spa_activities_lock
);
9913 *in_progress
= vdev_replace_in_progress(spa
->spa_root_vdev
);
9914 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9916 case ZPOOL_WAIT_REMOVE
:
9917 *in_progress
= (spa
->spa_removing_phys
.sr_state
==
9920 case ZPOOL_WAIT_RESILVER
:
9921 if ((*in_progress
= vdev_rebuild_active(spa
->spa_root_vdev
)))
9924 case ZPOOL_WAIT_SCRUB
:
9926 boolean_t scanning
, paused
, is_scrub
;
9927 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
9929 is_scrub
= (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
);
9930 scanning
= (scn
->scn_phys
.scn_state
== DSS_SCANNING
);
9931 paused
= dsl_scan_is_paused_scrub(scn
);
9932 *in_progress
= (scanning
&& !paused
&&
9933 is_scrub
== (activity
== ZPOOL_WAIT_SCRUB
));
9937 panic("unrecognized value for activity %d", activity
);
9944 spa_wait_common(const char *pool
, zpool_wait_activity_t activity
,
9945 boolean_t use_tag
, uint64_t tag
, boolean_t
*waited
)
9948 * The tag is used to distinguish between instances of an activity.
9949 * 'initialize' and 'trim' are the only activities that we use this for.
9950 * The other activities can only have a single instance in progress in a
9951 * pool at one time, making the tag unnecessary.
9953 * There can be multiple devices being replaced at once, but since they
9954 * all finish once resilvering finishes, we don't bother keeping track
9955 * of them individually, we just wait for them all to finish.
9957 if (use_tag
&& activity
!= ZPOOL_WAIT_INITIALIZE
&&
9958 activity
!= ZPOOL_WAIT_TRIM
)
9961 if (activity
< 0 || activity
>= ZPOOL_WAIT_NUM_ACTIVITIES
)
9965 int error
= spa_open(pool
, &spa
, FTAG
);
9970 * Increment the spa's waiter count so that we can call spa_close and
9971 * still ensure that the spa_t doesn't get freed before this thread is
9972 * finished with it when the pool is exported. We want to call spa_close
9973 * before we start waiting because otherwise the additional ref would
9974 * prevent the pool from being exported or destroyed throughout the
9975 * potentially long wait.
9977 mutex_enter(&spa
->spa_activities_lock
);
9979 spa_close(spa
, FTAG
);
9983 boolean_t in_progress
;
9984 error
= spa_activity_in_progress(spa
, activity
, use_tag
, tag
,
9987 if (error
|| !in_progress
|| spa
->spa_waiters_cancel
)
9992 if (cv_wait_sig(&spa
->spa_activities_cv
,
9993 &spa
->spa_activities_lock
) == 0) {
10000 cv_signal(&spa
->spa_waiters_cv
);
10001 mutex_exit(&spa
->spa_activities_lock
);
10007 * Wait for a particular instance of the specified activity to complete, where
10008 * the instance is identified by 'tag'
10011 spa_wait_tag(const char *pool
, zpool_wait_activity_t activity
, uint64_t tag
,
10014 return (spa_wait_common(pool
, activity
, B_TRUE
, tag
, waited
));
10018 * Wait for all instances of the specified activity complete
10021 spa_wait(const char *pool
, zpool_wait_activity_t activity
, boolean_t
*waited
)
10024 return (spa_wait_common(pool
, activity
, B_FALSE
, 0, waited
));
10028 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10030 sysevent_t
*ev
= NULL
;
10032 nvlist_t
*resource
;
10034 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
10036 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
10037 ev
->resource
= resource
;
10040 (void) spa
, (void) vd
, (void) hist_nvl
, (void) name
;
10046 spa_event_post(sysevent_t
*ev
)
10050 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
10051 kmem_free(ev
, sizeof (*ev
));
10059 * Post a zevent corresponding to the given sysevent. The 'name' must be one
10060 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
10061 * filled in from the spa and (optionally) the vdev. This doesn't do anything
10062 * in the userland libzpool, as we don't want consumers to misinterpret ztest
10063 * or zdb as real changes.
10066 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10068 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
10071 /* state manipulation functions */
10072 EXPORT_SYMBOL(spa_open
);
10073 EXPORT_SYMBOL(spa_open_rewind
);
10074 EXPORT_SYMBOL(spa_get_stats
);
10075 EXPORT_SYMBOL(spa_create
);
10076 EXPORT_SYMBOL(spa_import
);
10077 EXPORT_SYMBOL(spa_tryimport
);
10078 EXPORT_SYMBOL(spa_destroy
);
10079 EXPORT_SYMBOL(spa_export
);
10080 EXPORT_SYMBOL(spa_reset
);
10081 EXPORT_SYMBOL(spa_async_request
);
10082 EXPORT_SYMBOL(spa_async_suspend
);
10083 EXPORT_SYMBOL(spa_async_resume
);
10084 EXPORT_SYMBOL(spa_inject_addref
);
10085 EXPORT_SYMBOL(spa_inject_delref
);
10086 EXPORT_SYMBOL(spa_scan_stat_init
);
10087 EXPORT_SYMBOL(spa_scan_get_stats
);
10089 /* device manipulation */
10090 EXPORT_SYMBOL(spa_vdev_add
);
10091 EXPORT_SYMBOL(spa_vdev_attach
);
10092 EXPORT_SYMBOL(spa_vdev_detach
);
10093 EXPORT_SYMBOL(spa_vdev_setpath
);
10094 EXPORT_SYMBOL(spa_vdev_setfru
);
10095 EXPORT_SYMBOL(spa_vdev_split_mirror
);
10097 /* spare statech is global across all pools) */
10098 EXPORT_SYMBOL(spa_spare_add
);
10099 EXPORT_SYMBOL(spa_spare_remove
);
10100 EXPORT_SYMBOL(spa_spare_exists
);
10101 EXPORT_SYMBOL(spa_spare_activate
);
10103 /* L2ARC statech is global across all pools) */
10104 EXPORT_SYMBOL(spa_l2cache_add
);
10105 EXPORT_SYMBOL(spa_l2cache_remove
);
10106 EXPORT_SYMBOL(spa_l2cache_exists
);
10107 EXPORT_SYMBOL(spa_l2cache_activate
);
10108 EXPORT_SYMBOL(spa_l2cache_drop
);
10111 EXPORT_SYMBOL(spa_scan
);
10112 EXPORT_SYMBOL(spa_scan_stop
);
10115 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
10116 EXPORT_SYMBOL(spa_sync_allpools
);
10119 EXPORT_SYMBOL(spa_prop_set
);
10120 EXPORT_SYMBOL(spa_prop_get
);
10121 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
10123 /* asynchronous event notification */
10124 EXPORT_SYMBOL(spa_event_notify
);
10126 /* BEGIN CSTYLED */
10127 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_shift
, UINT
, ZMOD_RW
,
10128 "log2 fraction of arc that can be used by inflight I/Os when "
10129 "verifying pool during import");
10132 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_metadata
, INT
, ZMOD_RW
,
10133 "Set to traverse metadata on pool import");
10135 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_data
, INT
, ZMOD_RW
,
10136 "Set to traverse data on pool import");
10138 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_print_vdev_tree
, INT
, ZMOD_RW
,
10139 "Print vdev tree to zfs_dbgmsg during pool import");
10141 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_pct
, UINT
, ZMOD_RD
,
10142 "Percentage of CPUs to run an IO worker thread");
10144 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_tpq
, UINT
, ZMOD_RD
,
10145 "Number of threads per IO worker taskqueue");
10147 /* BEGIN CSTYLED */
10148 ZFS_MODULE_PARAM(zfs
, zfs_
, max_missing_tvds
, U64
, ZMOD_RW
,
10149 "Allow importing pool with up to this number of missing top-level "
10150 "vdevs (in read-only mode)");
10153 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_pause
, INT
,
10154 ZMOD_RW
, "Set the livelist condense zthr to pause");
10156 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_pause
, INT
,
10157 ZMOD_RW
, "Set the livelist condense synctask to pause");
10159 /* BEGIN CSTYLED */
10160 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_cancel
,
10162 "Whether livelist condensing was canceled in the synctask");
10164 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_cancel
,
10166 "Whether livelist condensing was canceled in the zthr function");
10168 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, new_alloc
, INT
,
10170 "Whether extra ALLOC blkptrs were added to a livelist entry while it "
10171 "was being condensed");