4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25 * Copyright (c) 2018, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
27 * Copyright 2013 Saso Kiselkov. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
29 * Copyright 2016 Toomas Soome <tsoome@me.com>
30 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
31 * Copyright 2018 Joyent, Inc.
32 * Copyright (c) 2017, 2019, Datto Inc. All rights reserved.
33 * Copyright 2017 Joyent, Inc.
34 * Copyright (c) 2017, Intel Corporation.
35 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
36 * Copyright (c) 2023 Hewlett Packard Enterprise Development LP.
40 * SPA: Storage Pool Allocator
42 * This file contains all the routines used when modifying on-disk SPA state.
43 * This includes opening, importing, destroying, exporting a pool, and syncing a
47 #include <sys/zfs_context.h>
48 #include <sys/fm/fs/zfs.h>
49 #include <sys/spa_impl.h>
51 #include <sys/zio_checksum.h>
53 #include <sys/dmu_tx.h>
58 #include <sys/vdev_impl.h>
59 #include <sys/vdev_removal.h>
60 #include <sys/vdev_indirect_mapping.h>
61 #include <sys/vdev_indirect_births.h>
62 #include <sys/vdev_initialize.h>
63 #include <sys/vdev_rebuild.h>
64 #include <sys/vdev_trim.h>
65 #include <sys/vdev_disk.h>
66 #include <sys/vdev_draid.h>
67 #include <sys/metaslab.h>
68 #include <sys/metaslab_impl.h>
70 #include <sys/uberblock_impl.h>
73 #include <sys/bpobj.h>
74 #include <sys/dmu_traverse.h>
75 #include <sys/dmu_objset.h>
76 #include <sys/unique.h>
77 #include <sys/dsl_pool.h>
78 #include <sys/dsl_dataset.h>
79 #include <sys/dsl_dir.h>
80 #include <sys/dsl_prop.h>
81 #include <sys/dsl_synctask.h>
82 #include <sys/fs/zfs.h>
84 #include <sys/callb.h>
85 #include <sys/systeminfo.h>
86 #include <sys/zfs_ioctl.h>
87 #include <sys/dsl_scan.h>
88 #include <sys/zfeature.h>
89 #include <sys/dsl_destroy.h>
93 #include <sys/fm/protocol.h>
94 #include <sys/fm/util.h>
95 #include <sys/callb.h>
97 #include <sys/vmsystm.h>
100 #include "zfs_prop.h"
101 #include "zfs_comutil.h"
104 * The interval, in seconds, at which failed configuration cache file writes
107 int zfs_ccw_retry_interval
= 300;
109 typedef enum zti_modes
{
110 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
111 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
112 ZTI_MODE_SCALE
, /* Taskqs scale with CPUs. */
113 ZTI_MODE_NULL
, /* don't create a taskq */
117 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
118 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
119 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
120 #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 }
121 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
123 #define ZTI_N(n) ZTI_P(n, 1)
124 #define ZTI_ONE ZTI_N(1)
126 typedef struct zio_taskq_info
{
127 zti_modes_t zti_mode
;
132 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
133 "iss", "iss_h", "int", "int_h"
137 * This table defines the taskq settings for each ZFS I/O type. When
138 * initializing a pool, we use this table to create an appropriately sized
139 * taskq. Some operations are low volume and therefore have a small, static
140 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
141 * macros. Other operations process a large amount of data; the ZTI_BATCH
142 * macro causes us to create a taskq oriented for throughput. Some operations
143 * are so high frequency and short-lived that the taskq itself can become a
144 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
145 * additional degree of parallelism specified by the number of threads per-
146 * taskq and the number of taskqs; when dispatching an event in this case, the
147 * particular taskq is chosen at random. ZTI_SCALE is similar to ZTI_BATCH,
148 * but with number of taskqs also scaling with number of CPUs.
150 * The different taskq priorities are to handle the different contexts (issue
151 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
152 * need to be handled with minimum delay.
154 static const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
155 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
156 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
157 { ZTI_N(8), ZTI_NULL
, ZTI_SCALE
, ZTI_NULL
}, /* READ */
158 { ZTI_BATCH
, ZTI_N(5), ZTI_SCALE
, ZTI_N(5) }, /* WRITE */
159 { ZTI_SCALE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
160 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
161 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
162 { ZTI_N(4), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* TRIM */
165 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
166 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
167 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
168 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
,
169 const char **ereport
);
170 static void spa_vdev_resilver_done(spa_t
*spa
);
173 * Percentage of all CPUs that can be used by the metaslab preload taskq.
175 static uint_t metaslab_preload_pct
= 50;
177 static uint_t zio_taskq_batch_pct
= 80; /* 1 thread per cpu in pset */
178 static uint_t zio_taskq_batch_tpq
; /* threads per taskq */
179 static const boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
180 static const uint_t zio_taskq_basedc
= 80; /* base duty cycle */
182 static const boolean_t spa_create_process
= B_TRUE
; /* no process => no sysdc */
185 * Report any spa_load_verify errors found, but do not fail spa_load.
186 * This is used by zdb to analyze non-idle pools.
188 boolean_t spa_load_verify_dryrun
= B_FALSE
;
191 * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ).
192 * This is used by zdb for spacemaps verification.
194 boolean_t spa_mode_readable_spacemaps
= B_FALSE
;
197 * This (illegal) pool name is used when temporarily importing a spa_t in order
198 * to get the vdev stats associated with the imported devices.
200 #define TRYIMPORT_NAME "$import"
203 * For debugging purposes: print out vdev tree during pool import.
205 static int spa_load_print_vdev_tree
= B_FALSE
;
208 * A non-zero value for zfs_max_missing_tvds means that we allow importing
209 * pools with missing top-level vdevs. This is strictly intended for advanced
210 * pool recovery cases since missing data is almost inevitable. Pools with
211 * missing devices can only be imported read-only for safety reasons, and their
212 * fail-mode will be automatically set to "continue".
214 * With 1 missing vdev we should be able to import the pool and mount all
215 * datasets. User data that was not modified after the missing device has been
216 * added should be recoverable. This means that snapshots created prior to the
217 * addition of that device should be completely intact.
219 * With 2 missing vdevs, some datasets may fail to mount since there are
220 * dataset statistics that are stored as regular metadata. Some data might be
221 * recoverable if those vdevs were added recently.
223 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
224 * may be missing entirely. Chances of data recovery are very low. Note that
225 * there are also risks of performing an inadvertent rewind as we might be
226 * missing all the vdevs with the latest uberblocks.
228 uint64_t zfs_max_missing_tvds
= 0;
231 * The parameters below are similar to zfs_max_missing_tvds but are only
232 * intended for a preliminary open of the pool with an untrusted config which
233 * might be incomplete or out-dated.
235 * We are more tolerant for pools opened from a cachefile since we could have
236 * an out-dated cachefile where a device removal was not registered.
237 * We could have set the limit arbitrarily high but in the case where devices
238 * are really missing we would want to return the proper error codes; we chose
239 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
240 * and we get a chance to retrieve the trusted config.
242 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
245 * In the case where config was assembled by scanning device paths (/dev/dsks
246 * by default) we are less tolerant since all the existing devices should have
247 * been detected and we want spa_load to return the right error codes.
249 uint64_t zfs_max_missing_tvds_scan
= 0;
252 * Debugging aid that pauses spa_sync() towards the end.
254 static const boolean_t zfs_pause_spa_sync
= B_FALSE
;
257 * Variables to indicate the livelist condense zthr func should wait at certain
258 * points for the livelist to be removed - used to test condense/destroy races
260 static int zfs_livelist_condense_zthr_pause
= 0;
261 static int zfs_livelist_condense_sync_pause
= 0;
264 * Variables to track whether or not condense cancellation has been
265 * triggered in testing.
267 static int zfs_livelist_condense_sync_cancel
= 0;
268 static int zfs_livelist_condense_zthr_cancel
= 0;
271 * Variable to track whether or not extra ALLOC blkptrs were added to a
272 * livelist entry while it was being condensed (caused by the way we track
273 * remapped blkptrs in dbuf_remap_impl)
275 static int zfs_livelist_condense_new_alloc
= 0;
278 * ==========================================================================
279 * SPA properties routines
280 * ==========================================================================
284 * Add a (source=src, propname=propval) list to an nvlist.
287 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, const char *strval
,
288 uint64_t intval
, zprop_source_t src
)
290 const char *propname
= zpool_prop_to_name(prop
);
293 propval
= fnvlist_alloc();
294 fnvlist_add_uint64(propval
, ZPROP_SOURCE
, src
);
297 fnvlist_add_string(propval
, ZPROP_VALUE
, strval
);
299 fnvlist_add_uint64(propval
, ZPROP_VALUE
, intval
);
301 fnvlist_add_nvlist(nvl
, propname
, propval
);
302 nvlist_free(propval
);
306 * Add a user property (source=src, propname=propval) to an nvlist.
309 spa_prop_add_user(nvlist_t
*nvl
, const char *propname
, char *strval
,
314 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
315 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
316 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
317 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
318 nvlist_free(propval
);
322 * Get property values from the spa configuration.
325 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
327 vdev_t
*rvd
= spa
->spa_root_vdev
;
328 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
329 uint64_t size
, alloc
, cap
, version
;
330 const zprop_source_t src
= ZPROP_SRC_NONE
;
331 spa_config_dirent_t
*dp
;
332 metaslab_class_t
*mc
= spa_normal_class(spa
);
334 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
337 alloc
= metaslab_class_get_alloc(mc
);
338 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
339 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
340 alloc
+= metaslab_class_get_alloc(spa_embedded_log_class(spa
));
342 size
= metaslab_class_get_space(mc
);
343 size
+= metaslab_class_get_space(spa_special_class(spa
));
344 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
345 size
+= metaslab_class_get_space(spa_embedded_log_class(spa
));
347 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
348 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
349 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
350 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
352 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
353 spa
->spa_checkpoint_info
.sci_dspace
, src
);
355 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
356 metaslab_class_fragmentation(mc
), src
);
357 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
358 metaslab_class_expandable_space(mc
), src
);
359 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
360 (spa_mode(spa
) == SPA_MODE_READ
), src
);
362 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
363 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
365 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
366 ddt_get_pool_dedup_ratio(spa
), src
);
367 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONEUSED
, NULL
,
368 brt_get_used(spa
), src
);
369 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONESAVED
, NULL
,
370 brt_get_saved(spa
), src
);
371 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONERATIO
, NULL
,
372 brt_get_ratio(spa
), src
);
374 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
375 rvd
->vdev_state
, src
);
377 version
= spa_version(spa
);
378 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
379 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
380 version
, ZPROP_SRC_DEFAULT
);
382 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
383 version
, ZPROP_SRC_LOCAL
);
385 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
386 NULL
, spa_load_guid(spa
), src
);
391 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
392 * when opening pools before this version freedir will be NULL.
394 if (pool
->dp_free_dir
!= NULL
) {
395 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
396 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
399 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
403 if (pool
->dp_leak_dir
!= NULL
) {
404 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
405 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
408 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
413 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
415 if (spa
->spa_comment
!= NULL
) {
416 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
420 if (spa
->spa_compatibility
!= NULL
) {
421 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMPATIBILITY
,
422 spa
->spa_compatibility
, 0, ZPROP_SRC_LOCAL
);
425 if (spa
->spa_root
!= NULL
)
426 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
429 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
430 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
431 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
433 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
434 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
437 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
438 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
439 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
441 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
442 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
445 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
446 if (dp
->scd_path
== NULL
) {
447 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
448 "none", 0, ZPROP_SRC_LOCAL
);
449 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
450 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
451 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
457 * Get zpool property values.
460 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
462 objset_t
*mos
= spa
->spa_meta_objset
;
468 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
472 dp
= spa_get_dsl(spa
);
473 dsl_pool_config_enter(dp
, FTAG
);
474 mutex_enter(&spa
->spa_props_lock
);
477 * Get properties from the spa config.
479 spa_prop_get_config(spa
, nvp
);
481 /* If no pool property object, no more prop to get. */
482 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0)
486 * Get properties from the MOS pool property object.
488 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
489 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
490 zap_cursor_advance(&zc
)) {
493 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
496 if ((prop
= zpool_name_to_prop(za
.za_name
)) ==
497 ZPOOL_PROP_INVAL
&& !zfs_prop_user(za
.za_name
))
500 switch (za
.za_integer_length
) {
502 /* integer property */
503 if (za
.za_first_integer
!=
504 zpool_prop_default_numeric(prop
))
505 src
= ZPROP_SRC_LOCAL
;
507 if (prop
== ZPOOL_PROP_BOOTFS
) {
508 dsl_dataset_t
*ds
= NULL
;
510 err
= dsl_dataset_hold_obj(dp
,
511 za
.za_first_integer
, FTAG
, &ds
);
515 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
517 dsl_dataset_name(ds
, strval
);
518 dsl_dataset_rele(ds
, FTAG
);
521 intval
= za
.za_first_integer
;
524 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
527 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
532 /* string property */
533 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
534 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
535 za
.za_name
, 1, za
.za_num_integers
, strval
);
537 kmem_free(strval
, za
.za_num_integers
);
540 if (prop
!= ZPOOL_PROP_INVAL
) {
541 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
543 src
= ZPROP_SRC_LOCAL
;
544 spa_prop_add_user(*nvp
, za
.za_name
, strval
,
547 kmem_free(strval
, za
.za_num_integers
);
554 zap_cursor_fini(&zc
);
556 mutex_exit(&spa
->spa_props_lock
);
557 dsl_pool_config_exit(dp
, FTAG
);
558 if (err
&& err
!= ENOENT
) {
568 * Validate the given pool properties nvlist and modify the list
569 * for the property values to be set.
572 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
575 int error
= 0, reset_bootfs
= 0;
577 boolean_t has_feature
= B_FALSE
;
580 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
582 const char *strval
, *slash
, *check
, *fname
;
583 const char *propname
= nvpair_name(elem
);
584 zpool_prop_t prop
= zpool_name_to_prop(propname
);
587 case ZPOOL_PROP_INVAL
:
589 * Sanitize the input.
591 if (zfs_prop_user(propname
)) {
592 if (strlen(propname
) >= ZAP_MAXNAMELEN
) {
593 error
= SET_ERROR(ENAMETOOLONG
);
597 if (strlen(fnvpair_value_string(elem
)) >=
599 error
= SET_ERROR(E2BIG
);
602 } else if (zpool_prop_feature(propname
)) {
603 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
604 error
= SET_ERROR(EINVAL
);
608 if (nvpair_value_uint64(elem
, &intval
) != 0) {
609 error
= SET_ERROR(EINVAL
);
614 error
= SET_ERROR(EINVAL
);
618 fname
= strchr(propname
, '@') + 1;
619 if (zfeature_lookup_name(fname
, NULL
) != 0) {
620 error
= SET_ERROR(EINVAL
);
624 has_feature
= B_TRUE
;
626 error
= SET_ERROR(EINVAL
);
631 case ZPOOL_PROP_VERSION
:
632 error
= nvpair_value_uint64(elem
, &intval
);
634 (intval
< spa_version(spa
) ||
635 intval
> SPA_VERSION_BEFORE_FEATURES
||
637 error
= SET_ERROR(EINVAL
);
640 case ZPOOL_PROP_DELEGATION
:
641 case ZPOOL_PROP_AUTOREPLACE
:
642 case ZPOOL_PROP_LISTSNAPS
:
643 case ZPOOL_PROP_AUTOEXPAND
:
644 case ZPOOL_PROP_AUTOTRIM
:
645 error
= nvpair_value_uint64(elem
, &intval
);
646 if (!error
&& intval
> 1)
647 error
= SET_ERROR(EINVAL
);
650 case ZPOOL_PROP_MULTIHOST
:
651 error
= nvpair_value_uint64(elem
, &intval
);
652 if (!error
&& intval
> 1)
653 error
= SET_ERROR(EINVAL
);
656 uint32_t hostid
= zone_get_hostid(NULL
);
658 spa
->spa_hostid
= hostid
;
660 error
= SET_ERROR(ENOTSUP
);
665 case ZPOOL_PROP_BOOTFS
:
667 * If the pool version is less than SPA_VERSION_BOOTFS,
668 * or the pool is still being created (version == 0),
669 * the bootfs property cannot be set.
671 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
672 error
= SET_ERROR(ENOTSUP
);
677 * Make sure the vdev config is bootable
679 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
680 error
= SET_ERROR(ENOTSUP
);
686 error
= nvpair_value_string(elem
, &strval
);
691 if (strval
== NULL
|| strval
[0] == '\0') {
692 objnum
= zpool_prop_default_numeric(
697 error
= dmu_objset_hold(strval
, FTAG
, &os
);
702 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
703 error
= SET_ERROR(ENOTSUP
);
705 objnum
= dmu_objset_id(os
);
707 dmu_objset_rele(os
, FTAG
);
711 case ZPOOL_PROP_FAILUREMODE
:
712 error
= nvpair_value_uint64(elem
, &intval
);
713 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
714 error
= SET_ERROR(EINVAL
);
717 * This is a special case which only occurs when
718 * the pool has completely failed. This allows
719 * the user to change the in-core failmode property
720 * without syncing it out to disk (I/Os might
721 * currently be blocked). We do this by returning
722 * EIO to the caller (spa_prop_set) to trick it
723 * into thinking we encountered a property validation
726 if (!error
&& spa_suspended(spa
)) {
727 spa
->spa_failmode
= intval
;
728 error
= SET_ERROR(EIO
);
732 case ZPOOL_PROP_CACHEFILE
:
733 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
736 if (strval
[0] == '\0')
739 if (strcmp(strval
, "none") == 0)
742 if (strval
[0] != '/') {
743 error
= SET_ERROR(EINVAL
);
747 slash
= strrchr(strval
, '/');
748 ASSERT(slash
!= NULL
);
750 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
751 strcmp(slash
, "/..") == 0)
752 error
= SET_ERROR(EINVAL
);
755 case ZPOOL_PROP_COMMENT
:
756 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
758 for (check
= strval
; *check
!= '\0'; check
++) {
759 if (!isprint(*check
)) {
760 error
= SET_ERROR(EINVAL
);
764 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
765 error
= SET_ERROR(E2BIG
);
776 (void) nvlist_remove_all(props
,
777 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO
));
779 if (!error
&& reset_bootfs
) {
780 error
= nvlist_remove(props
,
781 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
784 error
= nvlist_add_uint64(props
,
785 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
793 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
795 const char *cachefile
;
796 spa_config_dirent_t
*dp
;
798 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
802 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
805 if (cachefile
[0] == '\0')
806 dp
->scd_path
= spa_strdup(spa_config_path
);
807 else if (strcmp(cachefile
, "none") == 0)
810 dp
->scd_path
= spa_strdup(cachefile
);
812 list_insert_head(&spa
->spa_config_list
, dp
);
814 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
818 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
821 nvpair_t
*elem
= NULL
;
822 boolean_t need_sync
= B_FALSE
;
824 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
827 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
828 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
830 if (prop
== ZPOOL_PROP_CACHEFILE
||
831 prop
== ZPOOL_PROP_ALTROOT
||
832 prop
== ZPOOL_PROP_READONLY
)
835 if (prop
== ZPOOL_PROP_INVAL
&&
836 zfs_prop_user(nvpair_name(elem
))) {
841 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
844 if (prop
== ZPOOL_PROP_VERSION
) {
845 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
847 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
848 ver
= SPA_VERSION_FEATURES
;
852 /* Save time if the version is already set. */
853 if (ver
== spa_version(spa
))
857 * In addition to the pool directory object, we might
858 * create the pool properties object, the features for
859 * read object, the features for write object, or the
860 * feature descriptions object.
862 error
= dsl_sync_task(spa
->spa_name
, NULL
,
863 spa_sync_version
, &ver
,
864 6, ZFS_SPACE_CHECK_RESERVED
);
875 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
876 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
883 * If the bootfs property value is dsobj, clear it.
886 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
888 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
889 VERIFY(zap_remove(spa
->spa_meta_objset
,
890 spa
->spa_pool_props_object
,
891 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
897 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
899 uint64_t *newguid __maybe_unused
= arg
;
900 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
901 vdev_t
*rvd
= spa
->spa_root_vdev
;
904 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
905 int error
= (spa_has_checkpoint(spa
)) ?
906 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
907 return (SET_ERROR(error
));
910 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
911 vdev_state
= rvd
->vdev_state
;
912 spa_config_exit(spa
, SCL_STATE
, FTAG
);
914 if (vdev_state
!= VDEV_STATE_HEALTHY
)
915 return (SET_ERROR(ENXIO
));
917 ASSERT3U(spa_guid(spa
), !=, *newguid
);
923 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
925 uint64_t *newguid
= arg
;
926 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
928 vdev_t
*rvd
= spa
->spa_root_vdev
;
930 oldguid
= spa_guid(spa
);
932 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
933 rvd
->vdev_guid
= *newguid
;
934 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
935 vdev_config_dirty(rvd
);
936 spa_config_exit(spa
, SCL_STATE
, FTAG
);
938 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
939 (u_longlong_t
)oldguid
, (u_longlong_t
)*newguid
);
943 * Change the GUID for the pool. This is done so that we can later
944 * re-import a pool built from a clone of our own vdevs. We will modify
945 * the root vdev's guid, our own pool guid, and then mark all of our
946 * vdevs dirty. Note that we must make sure that all our vdevs are
947 * online when we do this, or else any vdevs that weren't present
948 * would be orphaned from our pool. We are also going to issue a
949 * sysevent to update any watchers.
952 spa_change_guid(spa_t
*spa
)
957 mutex_enter(&spa
->spa_vdev_top_lock
);
958 mutex_enter(&spa_namespace_lock
);
959 guid
= spa_generate_guid(NULL
);
961 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
962 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
966 * Clear the kobj flag from all the vdevs to allow
967 * vdev_cache_process_kobj_evt() to post events to all the
968 * vdevs since GUID is updated.
970 vdev_clear_kobj_evt(spa
->spa_root_vdev
);
971 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
972 vdev_clear_kobj_evt(spa
->spa_l2cache
.sav_vdevs
[i
]);
974 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
975 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
978 mutex_exit(&spa_namespace_lock
);
979 mutex_exit(&spa
->spa_vdev_top_lock
);
985 * ==========================================================================
986 * SPA state manipulation (open/create/destroy/import/export)
987 * ==========================================================================
991 spa_error_entry_compare(const void *a
, const void *b
)
993 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
994 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
997 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
998 sizeof (zbookmark_phys_t
));
1000 return (TREE_ISIGN(ret
));
1004 * Utility function which retrieves copies of the current logs and
1005 * re-initializes them in the process.
1008 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
1010 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
1012 memcpy(last
, &spa
->spa_errlist_last
, sizeof (avl_tree_t
));
1013 memcpy(scrub
, &spa
->spa_errlist_scrub
, sizeof (avl_tree_t
));
1015 avl_create(&spa
->spa_errlist_scrub
,
1016 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1017 offsetof(spa_error_entry_t
, se_avl
));
1018 avl_create(&spa
->spa_errlist_last
,
1019 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1020 offsetof(spa_error_entry_t
, se_avl
));
1024 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1026 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
1027 enum zti_modes mode
= ztip
->zti_mode
;
1028 uint_t value
= ztip
->zti_value
;
1029 uint_t count
= ztip
->zti_count
;
1030 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1031 uint_t cpus
, flags
= TASKQ_DYNAMIC
;
1032 boolean_t batch
= B_FALSE
;
1035 case ZTI_MODE_FIXED
:
1036 ASSERT3U(value
, >, 0);
1039 case ZTI_MODE_BATCH
:
1041 flags
|= TASKQ_THREADS_CPU_PCT
;
1042 value
= MIN(zio_taskq_batch_pct
, 100);
1045 case ZTI_MODE_SCALE
:
1046 flags
|= TASKQ_THREADS_CPU_PCT
;
1048 * We want more taskqs to reduce lock contention, but we want
1049 * less for better request ordering and CPU utilization.
1051 cpus
= MAX(1, boot_ncpus
* zio_taskq_batch_pct
/ 100);
1052 if (zio_taskq_batch_tpq
> 0) {
1053 count
= MAX(1, (cpus
+ zio_taskq_batch_tpq
/ 2) /
1054 zio_taskq_batch_tpq
);
1057 * Prefer 6 threads per taskq, but no more taskqs
1058 * than threads in them on large systems. For 80%:
1061 * cpus taskqs percent threads threads
1062 * ------- ------- ------- ------- -------
1073 count
= 1 + cpus
/ 6;
1074 while (count
* count
> cpus
)
1077 /* Limit each taskq within 100% to not trigger assertion. */
1078 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1079 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1083 tqs
->stqs_count
= 0;
1084 tqs
->stqs_taskq
= NULL
;
1088 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
1090 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
1094 ASSERT3U(count
, >, 0);
1095 tqs
->stqs_count
= count
;
1096 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
1098 for (uint_t i
= 0; i
< count
; i
++) {
1103 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
1104 zio_type_name
[t
], zio_taskq_types
[q
], i
);
1106 (void) snprintf(name
, sizeof (name
), "%s_%s",
1107 zio_type_name
[t
], zio_taskq_types
[q
]);
1109 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
1111 flags
|= TASKQ_DC_BATCH
;
1113 (void) zio_taskq_basedc
;
1114 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
1115 spa
->spa_proc
, zio_taskq_basedc
, flags
);
1117 pri_t pri
= maxclsyspri
;
1119 * The write issue taskq can be extremely CPU
1120 * intensive. Run it at slightly less important
1121 * priority than the other taskqs.
1123 * Under Linux and FreeBSD this means incrementing
1124 * the priority value as opposed to platforms like
1125 * illumos where it should be decremented.
1127 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1128 * are equal then a difference between them is
1131 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
) {
1132 #if defined(__linux__)
1134 #elif defined(__FreeBSD__)
1140 tq
= taskq_create_proc(name
, value
, pri
, 50,
1141 INT_MAX
, spa
->spa_proc
, flags
);
1144 tqs
->stqs_taskq
[i
] = tq
;
1149 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1151 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1153 if (tqs
->stqs_taskq
== NULL
) {
1154 ASSERT3U(tqs
->stqs_count
, ==, 0);
1158 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1159 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1160 taskq_destroy(tqs
->stqs_taskq
[i
]);
1163 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1164 tqs
->stqs_taskq
= NULL
;
1168 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1169 * Note that a type may have multiple discrete taskqs to avoid lock contention
1170 * on the taskq itself. In that case we choose which taskq at random by using
1171 * the low bits of gethrtime().
1174 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1175 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
1177 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1180 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1181 ASSERT3U(tqs
->stqs_count
, !=, 0);
1183 if (tqs
->stqs_count
== 1) {
1184 tq
= tqs
->stqs_taskq
[0];
1186 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1189 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
1193 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
1196 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1197 task_func_t
*func
, void *arg
, uint_t flags
)
1199 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1203 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1204 ASSERT3U(tqs
->stqs_count
, !=, 0);
1206 if (tqs
->stqs_count
== 1) {
1207 tq
= tqs
->stqs_taskq
[0];
1209 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1212 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1214 taskq_wait_id(tq
, id
);
1218 spa_create_zio_taskqs(spa_t
*spa
)
1220 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1221 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1222 spa_taskqs_init(spa
, t
, q
);
1228 * Disabled until spa_thread() can be adapted for Linux.
1230 #undef HAVE_SPA_THREAD
1232 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1234 spa_thread(void *arg
)
1236 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1237 callb_cpr_t cprinfo
;
1240 user_t
*pu
= PTOU(curproc
);
1242 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1245 ASSERT(curproc
!= &p0
);
1246 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1247 "zpool-%s", spa
->spa_name
);
1248 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1250 /* bind this thread to the requested psrset */
1251 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1253 mutex_enter(&cpu_lock
);
1254 mutex_enter(&pidlock
);
1255 mutex_enter(&curproc
->p_lock
);
1257 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1258 0, NULL
, NULL
) == 0) {
1259 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1262 "Couldn't bind process for zfs pool \"%s\" to "
1263 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1266 mutex_exit(&curproc
->p_lock
);
1267 mutex_exit(&pidlock
);
1268 mutex_exit(&cpu_lock
);
1272 if (zio_taskq_sysdc
) {
1273 sysdc_thread_enter(curthread
, 100, 0);
1276 spa
->spa_proc
= curproc
;
1277 spa
->spa_did
= curthread
->t_did
;
1279 spa_create_zio_taskqs(spa
);
1281 mutex_enter(&spa
->spa_proc_lock
);
1282 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1284 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1285 cv_broadcast(&spa
->spa_proc_cv
);
1287 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1288 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1289 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1290 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1292 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1293 spa
->spa_proc_state
= SPA_PROC_GONE
;
1294 spa
->spa_proc
= &p0
;
1295 cv_broadcast(&spa
->spa_proc_cv
);
1296 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1298 mutex_enter(&curproc
->p_lock
);
1303 extern metaslab_ops_t
*metaslab_allocator(spa_t
*spa
);
1306 * Activate an uninitialized pool.
1309 spa_activate(spa_t
*spa
, spa_mode_t mode
)
1311 metaslab_ops_t
*msp
= metaslab_allocator(spa
);
1312 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1314 spa
->spa_state
= POOL_STATE_ACTIVE
;
1315 spa
->spa_mode
= mode
;
1316 spa
->spa_read_spacemaps
= spa_mode_readable_spacemaps
;
1318 spa
->spa_normal_class
= metaslab_class_create(spa
, msp
);
1319 spa
->spa_log_class
= metaslab_class_create(spa
, msp
);
1320 spa
->spa_embedded_log_class
= metaslab_class_create(spa
, msp
);
1321 spa
->spa_special_class
= metaslab_class_create(spa
, msp
);
1322 spa
->spa_dedup_class
= metaslab_class_create(spa
, msp
);
1324 /* Try to create a covering process */
1325 mutex_enter(&spa
->spa_proc_lock
);
1326 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1327 ASSERT(spa
->spa_proc
== &p0
);
1330 (void) spa_create_process
;
1331 #ifdef HAVE_SPA_THREAD
1332 /* Only create a process if we're going to be around a while. */
1333 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1334 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1336 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1337 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1338 cv_wait(&spa
->spa_proc_cv
,
1339 &spa
->spa_proc_lock
);
1341 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1342 ASSERT(spa
->spa_proc
!= &p0
);
1343 ASSERT(spa
->spa_did
!= 0);
1347 "Couldn't create process for zfs pool \"%s\"\n",
1352 #endif /* HAVE_SPA_THREAD */
1353 mutex_exit(&spa
->spa_proc_lock
);
1355 /* If we didn't create a process, we need to create our taskqs. */
1356 if (spa
->spa_proc
== &p0
) {
1357 spa_create_zio_taskqs(spa
);
1360 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1361 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1365 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1366 offsetof(vdev_t
, vdev_config_dirty_node
));
1367 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1368 offsetof(objset_t
, os_evicting_node
));
1369 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1370 offsetof(vdev_t
, vdev_state_dirty_node
));
1372 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1373 offsetof(struct vdev
, vdev_txg_node
));
1375 avl_create(&spa
->spa_errlist_scrub
,
1376 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1377 offsetof(spa_error_entry_t
, se_avl
));
1378 avl_create(&spa
->spa_errlist_last
,
1379 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1380 offsetof(spa_error_entry_t
, se_avl
));
1381 avl_create(&spa
->spa_errlist_healed
,
1382 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1383 offsetof(spa_error_entry_t
, se_avl
));
1385 spa_activate_os(spa
);
1387 spa_keystore_init(&spa
->spa_keystore
);
1390 * This taskq is used to perform zvol-minor-related tasks
1391 * asynchronously. This has several advantages, including easy
1392 * resolution of various deadlocks.
1394 * The taskq must be single threaded to ensure tasks are always
1395 * processed in the order in which they were dispatched.
1397 * A taskq per pool allows one to keep the pools independent.
1398 * This way if one pool is suspended, it will not impact another.
1400 * The preferred location to dispatch a zvol minor task is a sync
1401 * task. In this context, there is easy access to the spa_t and minimal
1402 * error handling is required because the sync task must succeed.
1404 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1408 * The taskq to preload metaslabs.
1410 spa
->spa_metaslab_taskq
= taskq_create("z_metaslab",
1411 metaslab_preload_pct
, maxclsyspri
, 1, INT_MAX
,
1412 TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1415 * Taskq dedicated to prefetcher threads: this is used to prevent the
1416 * pool traverse code from monopolizing the global (and limited)
1417 * system_taskq by inappropriately scheduling long running tasks on it.
1419 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", 100,
1420 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1423 * The taskq to upgrade datasets in this pool. Currently used by
1424 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1426 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", 100,
1427 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1431 * Opposite of spa_activate().
1434 spa_deactivate(spa_t
*spa
)
1436 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1437 ASSERT(spa
->spa_dsl_pool
== NULL
);
1438 ASSERT(spa
->spa_root_vdev
== NULL
);
1439 ASSERT(spa
->spa_async_zio_root
== NULL
);
1440 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1442 spa_evicting_os_wait(spa
);
1444 if (spa
->spa_zvol_taskq
) {
1445 taskq_destroy(spa
->spa_zvol_taskq
);
1446 spa
->spa_zvol_taskq
= NULL
;
1449 if (spa
->spa_metaslab_taskq
) {
1450 taskq_destroy(spa
->spa_metaslab_taskq
);
1451 spa
->spa_metaslab_taskq
= NULL
;
1454 if (spa
->spa_prefetch_taskq
) {
1455 taskq_destroy(spa
->spa_prefetch_taskq
);
1456 spa
->spa_prefetch_taskq
= NULL
;
1459 if (spa
->spa_upgrade_taskq
) {
1460 taskq_destroy(spa
->spa_upgrade_taskq
);
1461 spa
->spa_upgrade_taskq
= NULL
;
1464 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1466 list_destroy(&spa
->spa_config_dirty_list
);
1467 list_destroy(&spa
->spa_evicting_os_list
);
1468 list_destroy(&spa
->spa_state_dirty_list
);
1470 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1472 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1473 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1474 spa_taskqs_fini(spa
, t
, q
);
1478 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1479 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1480 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1481 spa
->spa_txg_zio
[i
] = NULL
;
1484 metaslab_class_destroy(spa
->spa_normal_class
);
1485 spa
->spa_normal_class
= NULL
;
1487 metaslab_class_destroy(spa
->spa_log_class
);
1488 spa
->spa_log_class
= NULL
;
1490 metaslab_class_destroy(spa
->spa_embedded_log_class
);
1491 spa
->spa_embedded_log_class
= NULL
;
1493 metaslab_class_destroy(spa
->spa_special_class
);
1494 spa
->spa_special_class
= NULL
;
1496 metaslab_class_destroy(spa
->spa_dedup_class
);
1497 spa
->spa_dedup_class
= NULL
;
1500 * If this was part of an import or the open otherwise failed, we may
1501 * still have errors left in the queues. Empty them just in case.
1503 spa_errlog_drain(spa
);
1504 avl_destroy(&spa
->spa_errlist_scrub
);
1505 avl_destroy(&spa
->spa_errlist_last
);
1506 avl_destroy(&spa
->spa_errlist_healed
);
1508 spa_keystore_fini(&spa
->spa_keystore
);
1510 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1512 mutex_enter(&spa
->spa_proc_lock
);
1513 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1514 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1515 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1516 cv_broadcast(&spa
->spa_proc_cv
);
1517 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1518 ASSERT(spa
->spa_proc
!= &p0
);
1519 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1521 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1522 spa
->spa_proc_state
= SPA_PROC_NONE
;
1524 ASSERT(spa
->spa_proc
== &p0
);
1525 mutex_exit(&spa
->spa_proc_lock
);
1528 * We want to make sure spa_thread() has actually exited the ZFS
1529 * module, so that the module can't be unloaded out from underneath
1532 if (spa
->spa_did
!= 0) {
1533 thread_join(spa
->spa_did
);
1537 spa_deactivate_os(spa
);
1542 * Verify a pool configuration, and construct the vdev tree appropriately. This
1543 * will create all the necessary vdevs in the appropriate layout, with each vdev
1544 * in the CLOSED state. This will prep the pool before open/creation/import.
1545 * All vdev validation is done by the vdev_alloc() routine.
1548 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1549 uint_t id
, int atype
)
1555 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1558 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1561 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1564 if (error
== ENOENT
)
1570 return (SET_ERROR(EINVAL
));
1573 for (int c
= 0; c
< children
; c
++) {
1575 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1583 ASSERT(*vdp
!= NULL
);
1589 spa_should_flush_logs_on_unload(spa_t
*spa
)
1591 if (!spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
))
1594 if (!spa_writeable(spa
))
1597 if (!spa
->spa_sync_on
)
1600 if (spa_state(spa
) != POOL_STATE_EXPORTED
)
1603 if (zfs_keep_log_spacemaps_at_export
)
1610 * Opens a transaction that will set the flag that will instruct
1611 * spa_sync to attempt to flush all the metaslabs for that txg.
1614 spa_unload_log_sm_flush_all(spa_t
*spa
)
1616 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
1617 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
1619 ASSERT3U(spa
->spa_log_flushall_txg
, ==, 0);
1620 spa
->spa_log_flushall_txg
= dmu_tx_get_txg(tx
);
1623 txg_wait_synced(spa_get_dsl(spa
), spa
->spa_log_flushall_txg
);
1627 spa_unload_log_sm_metadata(spa_t
*spa
)
1629 void *cookie
= NULL
;
1631 log_summary_entry_t
*e
;
1633 while ((sls
= avl_destroy_nodes(&spa
->spa_sm_logs_by_txg
,
1634 &cookie
)) != NULL
) {
1635 VERIFY0(sls
->sls_mscount
);
1636 kmem_free(sls
, sizeof (spa_log_sm_t
));
1639 while ((e
= list_remove_head(&spa
->spa_log_summary
)) != NULL
) {
1640 VERIFY0(e
->lse_mscount
);
1641 kmem_free(e
, sizeof (log_summary_entry_t
));
1644 spa
->spa_unflushed_stats
.sus_nblocks
= 0;
1645 spa
->spa_unflushed_stats
.sus_memused
= 0;
1646 spa
->spa_unflushed_stats
.sus_blocklimit
= 0;
1650 spa_destroy_aux_threads(spa_t
*spa
)
1652 if (spa
->spa_condense_zthr
!= NULL
) {
1653 zthr_destroy(spa
->spa_condense_zthr
);
1654 spa
->spa_condense_zthr
= NULL
;
1656 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1657 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1658 spa
->spa_checkpoint_discard_zthr
= NULL
;
1660 if (spa
->spa_livelist_delete_zthr
!= NULL
) {
1661 zthr_destroy(spa
->spa_livelist_delete_zthr
);
1662 spa
->spa_livelist_delete_zthr
= NULL
;
1664 if (spa
->spa_livelist_condense_zthr
!= NULL
) {
1665 zthr_destroy(spa
->spa_livelist_condense_zthr
);
1666 spa
->spa_livelist_condense_zthr
= NULL
;
1671 * Opposite of spa_load().
1674 spa_unload(spa_t
*spa
)
1676 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1677 ASSERT(spa_state(spa
) != POOL_STATE_UNINITIALIZED
);
1679 spa_import_progress_remove(spa_guid(spa
));
1680 spa_load_note(spa
, "UNLOADING");
1682 spa_wake_waiters(spa
);
1685 * If we have set the spa_final_txg, we have already performed the
1686 * tasks below in spa_export_common(). We should not redo it here since
1687 * we delay the final TXGs beyond what spa_final_txg is set at.
1689 if (spa
->spa_final_txg
== UINT64_MAX
) {
1691 * If the log space map feature is enabled and the pool is
1692 * getting exported (but not destroyed), we want to spend some
1693 * time flushing as many metaslabs as we can in an attempt to
1694 * destroy log space maps and save import time.
1696 if (spa_should_flush_logs_on_unload(spa
))
1697 spa_unload_log_sm_flush_all(spa
);
1702 spa_async_suspend(spa
);
1704 if (spa
->spa_root_vdev
) {
1705 vdev_t
*root_vdev
= spa
->spa_root_vdev
;
1706 vdev_initialize_stop_all(root_vdev
,
1707 VDEV_INITIALIZE_ACTIVE
);
1708 vdev_trim_stop_all(root_vdev
, VDEV_TRIM_ACTIVE
);
1709 vdev_autotrim_stop_all(spa
);
1710 vdev_rebuild_stop_all(spa
);
1717 if (spa
->spa_sync_on
) {
1718 txg_sync_stop(spa
->spa_dsl_pool
);
1719 spa
->spa_sync_on
= B_FALSE
;
1723 * This ensures that there is no async metaslab prefetching
1724 * while we attempt to unload the spa.
1726 taskq_wait(spa
->spa_metaslab_taskq
);
1728 if (spa
->spa_mmp
.mmp_thread
)
1729 mmp_thread_stop(spa
);
1732 * Wait for any outstanding async I/O to complete.
1734 if (spa
->spa_async_zio_root
!= NULL
) {
1735 for (int i
= 0; i
< max_ncpus
; i
++)
1736 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1737 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1738 spa
->spa_async_zio_root
= NULL
;
1741 if (spa
->spa_vdev_removal
!= NULL
) {
1742 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
1743 spa
->spa_vdev_removal
= NULL
;
1746 spa_destroy_aux_threads(spa
);
1748 spa_condense_fini(spa
);
1750 bpobj_close(&spa
->spa_deferred_bpobj
);
1752 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
1757 if (spa
->spa_root_vdev
)
1758 vdev_free(spa
->spa_root_vdev
);
1759 ASSERT(spa
->spa_root_vdev
== NULL
);
1762 * Close the dsl pool.
1764 if (spa
->spa_dsl_pool
) {
1765 dsl_pool_close(spa
->spa_dsl_pool
);
1766 spa
->spa_dsl_pool
= NULL
;
1767 spa
->spa_meta_objset
= NULL
;
1772 spa_unload_log_sm_metadata(spa
);
1775 * Drop and purge level 2 cache
1777 spa_l2cache_drop(spa
);
1779 if (spa
->spa_spares
.sav_vdevs
) {
1780 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1781 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1782 kmem_free(spa
->spa_spares
.sav_vdevs
,
1783 spa
->spa_spares
.sav_count
* sizeof (void *));
1784 spa
->spa_spares
.sav_vdevs
= NULL
;
1786 if (spa
->spa_spares
.sav_config
) {
1787 nvlist_free(spa
->spa_spares
.sav_config
);
1788 spa
->spa_spares
.sav_config
= NULL
;
1790 spa
->spa_spares
.sav_count
= 0;
1792 if (spa
->spa_l2cache
.sav_vdevs
) {
1793 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1794 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1795 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1797 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1798 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1799 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1801 if (spa
->spa_l2cache
.sav_config
) {
1802 nvlist_free(spa
->spa_l2cache
.sav_config
);
1803 spa
->spa_l2cache
.sav_config
= NULL
;
1805 spa
->spa_l2cache
.sav_count
= 0;
1807 spa
->spa_async_suspended
= 0;
1809 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
1811 if (spa
->spa_comment
!= NULL
) {
1812 spa_strfree(spa
->spa_comment
);
1813 spa
->spa_comment
= NULL
;
1815 if (spa
->spa_compatibility
!= NULL
) {
1816 spa_strfree(spa
->spa_compatibility
);
1817 spa
->spa_compatibility
= NULL
;
1820 spa_config_exit(spa
, SCL_ALL
, spa
);
1824 * Load (or re-load) the current list of vdevs describing the active spares for
1825 * this pool. When this is called, we have some form of basic information in
1826 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1827 * then re-generate a more complete list including status information.
1830 spa_load_spares(spa_t
*spa
)
1839 * zdb opens both the current state of the pool and the
1840 * checkpointed state (if present), with a different spa_t.
1842 * As spare vdevs are shared among open pools, we skip loading
1843 * them when we load the checkpointed state of the pool.
1845 if (!spa_writeable(spa
))
1849 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1852 * First, close and free any existing spare vdevs.
1854 if (spa
->spa_spares
.sav_vdevs
) {
1855 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1856 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1858 /* Undo the call to spa_activate() below */
1859 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1860 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1861 spa_spare_remove(tvd
);
1866 kmem_free(spa
->spa_spares
.sav_vdevs
,
1867 spa
->spa_spares
.sav_count
* sizeof (void *));
1870 if (spa
->spa_spares
.sav_config
== NULL
)
1873 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1874 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
1876 spa
->spa_spares
.sav_count
= (int)nspares
;
1877 spa
->spa_spares
.sav_vdevs
= NULL
;
1883 * Construct the array of vdevs, opening them to get status in the
1884 * process. For each spare, there is potentially two different vdev_t
1885 * structures associated with it: one in the list of spares (used only
1886 * for basic validation purposes) and one in the active vdev
1887 * configuration (if it's spared in). During this phase we open and
1888 * validate each vdev on the spare list. If the vdev also exists in the
1889 * active configuration, then we also mark this vdev as an active spare.
1891 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1893 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1894 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1895 VDEV_ALLOC_SPARE
) == 0);
1898 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1900 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1901 B_FALSE
)) != NULL
) {
1902 if (!tvd
->vdev_isspare
)
1906 * We only mark the spare active if we were successfully
1907 * able to load the vdev. Otherwise, importing a pool
1908 * with a bad active spare would result in strange
1909 * behavior, because multiple pool would think the spare
1910 * is actively in use.
1912 * There is a vulnerability here to an equally bizarre
1913 * circumstance, where a dead active spare is later
1914 * brought back to life (onlined or otherwise). Given
1915 * the rarity of this scenario, and the extra complexity
1916 * it adds, we ignore the possibility.
1918 if (!vdev_is_dead(tvd
))
1919 spa_spare_activate(tvd
);
1923 vd
->vdev_aux
= &spa
->spa_spares
;
1925 if (vdev_open(vd
) != 0)
1928 if (vdev_validate_aux(vd
) == 0)
1933 * Recompute the stashed list of spares, with status information
1936 fnvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
);
1938 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1940 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1941 spares
[i
] = vdev_config_generate(spa
,
1942 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1943 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1944 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
1945 spa
->spa_spares
.sav_count
);
1946 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1947 nvlist_free(spares
[i
]);
1948 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1952 * Load (or re-load) the current list of vdevs describing the active l2cache for
1953 * this pool. When this is called, we have some form of basic information in
1954 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1955 * then re-generate a more complete list including status information.
1956 * Devices which are already active have their details maintained, and are
1960 spa_load_l2cache(spa_t
*spa
)
1962 nvlist_t
**l2cache
= NULL
;
1964 int i
, j
, oldnvdevs
;
1966 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1967 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1971 * zdb opens both the current state of the pool and the
1972 * checkpointed state (if present), with a different spa_t.
1974 * As L2 caches are part of the ARC which is shared among open
1975 * pools, we skip loading them when we load the checkpointed
1976 * state of the pool.
1978 if (!spa_writeable(spa
))
1982 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1984 oldvdevs
= sav
->sav_vdevs
;
1985 oldnvdevs
= sav
->sav_count
;
1986 sav
->sav_vdevs
= NULL
;
1989 if (sav
->sav_config
== NULL
) {
1995 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
,
1996 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
1997 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
2000 * Process new nvlist of vdevs.
2002 for (i
= 0; i
< nl2cache
; i
++) {
2003 guid
= fnvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
);
2006 for (j
= 0; j
< oldnvdevs
; j
++) {
2008 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
2010 * Retain previous vdev for add/remove ops.
2018 if (newvdevs
[i
] == NULL
) {
2022 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
2023 VDEV_ALLOC_L2CACHE
) == 0);
2028 * Commit this vdev as an l2cache device,
2029 * even if it fails to open.
2031 spa_l2cache_add(vd
);
2036 spa_l2cache_activate(vd
);
2038 if (vdev_open(vd
) != 0)
2041 (void) vdev_validate_aux(vd
);
2043 if (!vdev_is_dead(vd
))
2044 l2arc_add_vdev(spa
, vd
);
2047 * Upon cache device addition to a pool or pool
2048 * creation with a cache device or if the header
2049 * of the device is invalid we issue an async
2050 * TRIM command for the whole device which will
2051 * execute if l2arc_trim_ahead > 0.
2053 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2057 sav
->sav_vdevs
= newvdevs
;
2058 sav
->sav_count
= (int)nl2cache
;
2061 * Recompute the stashed list of l2cache devices, with status
2062 * information this time.
2064 fnvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
);
2066 if (sav
->sav_count
> 0)
2067 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
2069 for (i
= 0; i
< sav
->sav_count
; i
++)
2070 l2cache
[i
] = vdev_config_generate(spa
,
2071 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
2072 fnvlist_add_nvlist_array(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
2073 (const nvlist_t
* const *)l2cache
, sav
->sav_count
);
2077 * Purge vdevs that were dropped
2080 for (i
= 0; i
< oldnvdevs
; i
++) {
2085 ASSERT(vd
->vdev_isl2cache
);
2087 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
2088 pool
!= 0ULL && l2arc_vdev_present(vd
))
2089 l2arc_remove_vdev(vd
);
2090 vdev_clear_stats(vd
);
2095 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
2098 for (i
= 0; i
< sav
->sav_count
; i
++)
2099 nvlist_free(l2cache
[i
]);
2101 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
2105 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
2108 char *packed
= NULL
;
2113 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
2117 nvsize
= *(uint64_t *)db
->db_data
;
2118 dmu_buf_rele(db
, FTAG
);
2120 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
2121 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
2124 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
2125 vmem_free(packed
, nvsize
);
2131 * Concrete top-level vdevs that are not missing and are not logs. At every
2132 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
2135 spa_healthy_core_tvds(spa_t
*spa
)
2137 vdev_t
*rvd
= spa
->spa_root_vdev
;
2140 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
2141 vdev_t
*vd
= rvd
->vdev_child
[i
];
2144 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
2152 * Checks to see if the given vdev could not be opened, in which case we post a
2153 * sysevent to notify the autoreplace code that the device has been removed.
2156 spa_check_removed(vdev_t
*vd
)
2158 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2159 spa_check_removed(vd
->vdev_child
[c
]);
2161 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
2162 vdev_is_concrete(vd
)) {
2163 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
2164 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
2169 spa_check_for_missing_logs(spa_t
*spa
)
2171 vdev_t
*rvd
= spa
->spa_root_vdev
;
2174 * If we're doing a normal import, then build up any additional
2175 * diagnostic information about missing log devices.
2176 * We'll pass this up to the user for further processing.
2178 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
2179 nvlist_t
**child
, *nv
;
2182 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
2184 nv
= fnvlist_alloc();
2186 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2187 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2190 * We consider a device as missing only if it failed
2191 * to open (i.e. offline or faulted is not considered
2194 if (tvd
->vdev_islog
&&
2195 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2196 child
[idx
++] = vdev_config_generate(spa
, tvd
,
2197 B_FALSE
, VDEV_CONFIG_MISSING
);
2202 fnvlist_add_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2203 (const nvlist_t
* const *)child
, idx
);
2204 fnvlist_add_nvlist(spa
->spa_load_info
,
2205 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
2207 for (uint64_t i
= 0; i
< idx
; i
++)
2208 nvlist_free(child
[i
]);
2211 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
2214 spa_load_failed(spa
, "some log devices are missing");
2215 vdev_dbgmsg_print_tree(rvd
, 2);
2216 return (SET_ERROR(ENXIO
));
2219 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2220 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2222 if (tvd
->vdev_islog
&&
2223 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2224 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2225 spa_load_note(spa
, "some log devices are "
2226 "missing, ZIL is dropped.");
2227 vdev_dbgmsg_print_tree(rvd
, 2);
2237 * Check for missing log devices
2240 spa_check_logs(spa_t
*spa
)
2242 boolean_t rv
= B_FALSE
;
2243 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2245 switch (spa
->spa_log_state
) {
2248 case SPA_LOG_MISSING
:
2249 /* need to recheck in case slog has been restored */
2250 case SPA_LOG_UNKNOWN
:
2251 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2252 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
2254 spa_set_log_state(spa
, SPA_LOG_MISSING
);
2261 * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2264 spa_passivate_log(spa_t
*spa
)
2266 vdev_t
*rvd
= spa
->spa_root_vdev
;
2267 boolean_t slog_found
= B_FALSE
;
2269 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2271 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2272 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2274 if (tvd
->vdev_islog
) {
2275 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2276 metaslab_group_passivate(tvd
->vdev_mg
);
2277 slog_found
= B_TRUE
;
2281 return (slog_found
);
2285 * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2288 spa_activate_log(spa_t
*spa
)
2290 vdev_t
*rvd
= spa
->spa_root_vdev
;
2292 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2294 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2295 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2297 if (tvd
->vdev_islog
) {
2298 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2299 metaslab_group_activate(tvd
->vdev_mg
);
2305 spa_reset_logs(spa_t
*spa
)
2309 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2310 NULL
, DS_FIND_CHILDREN
);
2313 * We successfully offlined the log device, sync out the
2314 * current txg so that the "stubby" block can be removed
2317 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2323 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2325 for (int i
= 0; i
< sav
->sav_count
; i
++)
2326 spa_check_removed(sav
->sav_vdevs
[i
]);
2330 spa_claim_notify(zio_t
*zio
)
2332 spa_t
*spa
= zio
->io_spa
;
2337 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2338 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
2339 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
2340 mutex_exit(&spa
->spa_props_lock
);
2343 typedef struct spa_load_error
{
2344 boolean_t sle_verify_data
;
2345 uint64_t sle_meta_count
;
2346 uint64_t sle_data_count
;
2350 spa_load_verify_done(zio_t
*zio
)
2352 blkptr_t
*bp
= zio
->io_bp
;
2353 spa_load_error_t
*sle
= zio
->io_private
;
2354 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2355 int error
= zio
->io_error
;
2356 spa_t
*spa
= zio
->io_spa
;
2358 abd_free(zio
->io_abd
);
2360 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2361 type
!= DMU_OT_INTENT_LOG
)
2362 atomic_inc_64(&sle
->sle_meta_count
);
2364 atomic_inc_64(&sle
->sle_data_count
);
2367 mutex_enter(&spa
->spa_scrub_lock
);
2368 spa
->spa_load_verify_bytes
-= BP_GET_PSIZE(bp
);
2369 cv_broadcast(&spa
->spa_scrub_io_cv
);
2370 mutex_exit(&spa
->spa_scrub_lock
);
2374 * Maximum number of inflight bytes is the log2 fraction of the arc size.
2375 * By default, we set it to 1/16th of the arc.
2377 static uint_t spa_load_verify_shift
= 4;
2378 static int spa_load_verify_metadata
= B_TRUE
;
2379 static int spa_load_verify_data
= B_TRUE
;
2382 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2383 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2386 spa_load_error_t
*sle
= rio
->io_private
;
2388 (void) zilog
, (void) dnp
;
2391 * Note: normally this routine will not be called if
2392 * spa_load_verify_metadata is not set. However, it may be useful
2393 * to manually set the flag after the traversal has begun.
2395 if (!spa_load_verify_metadata
)
2399 * Sanity check the block pointer in order to detect obvious damage
2400 * before using the contents in subsequent checks or in zio_read().
2401 * When damaged consider it to be a metadata error since we cannot
2402 * trust the BP_GET_TYPE and BP_GET_LEVEL values.
2404 if (!zfs_blkptr_verify(spa
, bp
, BLK_CONFIG_NEEDED
, BLK_VERIFY_LOG
)) {
2405 atomic_inc_64(&sle
->sle_meta_count
);
2409 if (zb
->zb_level
== ZB_DNODE_LEVEL
|| BP_IS_HOLE(bp
) ||
2410 BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
2413 if (!BP_IS_METADATA(bp
) &&
2414 (!spa_load_verify_data
|| !sle
->sle_verify_data
))
2417 uint64_t maxinflight_bytes
=
2418 arc_target_bytes() >> spa_load_verify_shift
;
2419 size_t size
= BP_GET_PSIZE(bp
);
2421 mutex_enter(&spa
->spa_scrub_lock
);
2422 while (spa
->spa_load_verify_bytes
>= maxinflight_bytes
)
2423 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2424 spa
->spa_load_verify_bytes
+= size
;
2425 mutex_exit(&spa
->spa_scrub_lock
);
2427 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2428 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2429 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2430 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2435 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2437 (void) dp
, (void) arg
;
2439 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2440 return (SET_ERROR(ENAMETOOLONG
));
2446 spa_load_verify(spa_t
*spa
)
2449 spa_load_error_t sle
= { 0 };
2450 zpool_load_policy_t policy
;
2451 boolean_t verify_ok
= B_FALSE
;
2454 zpool_get_load_policy(spa
->spa_config
, &policy
);
2456 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
||
2457 policy
.zlp_maxmeta
== UINT64_MAX
)
2460 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2461 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2462 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2464 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2469 * Verify data only if we are rewinding or error limit was set.
2470 * Otherwise nothing except dbgmsg care about it to waste time.
2472 sle
.sle_verify_data
= (policy
.zlp_rewind
& ZPOOL_REWIND_MASK
) ||
2473 (policy
.zlp_maxdata
< UINT64_MAX
);
2475 rio
= zio_root(spa
, NULL
, &sle
,
2476 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2478 if (spa_load_verify_metadata
) {
2479 if (spa
->spa_extreme_rewind
) {
2480 spa_load_note(spa
, "performing a complete scan of the "
2481 "pool since extreme rewind is on. This may take "
2482 "a very long time.\n (spa_load_verify_data=%u, "
2483 "spa_load_verify_metadata=%u)",
2484 spa_load_verify_data
, spa_load_verify_metadata
);
2487 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2488 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2489 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2492 (void) zio_wait(rio
);
2493 ASSERT0(spa
->spa_load_verify_bytes
);
2495 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2496 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2498 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2499 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2500 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2501 (u_longlong_t
)sle
.sle_data_count
);
2504 if (spa_load_verify_dryrun
||
2505 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2506 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2510 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2511 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2513 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2514 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_LOAD_TIME
,
2515 spa
->spa_load_txg_ts
);
2516 fnvlist_add_int64(spa
->spa_load_info
, ZPOOL_CONFIG_REWIND_TIME
,
2518 fnvlist_add_uint64(spa
->spa_load_info
,
2519 ZPOOL_CONFIG_LOAD_META_ERRORS
, sle
.sle_meta_count
);
2520 fnvlist_add_uint64(spa
->spa_load_info
,
2521 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
);
2523 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2526 if (spa_load_verify_dryrun
)
2530 if (error
!= ENXIO
&& error
!= EIO
)
2531 error
= SET_ERROR(EIO
);
2535 return (verify_ok
? 0 : EIO
);
2539 * Find a value in the pool props object.
2542 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2544 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2545 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2549 * Find a value in the pool directory object.
2552 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2554 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2555 name
, sizeof (uint64_t), 1, val
);
2557 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2558 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2559 "[error=%d]", name
, error
);
2566 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2568 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2569 return (SET_ERROR(err
));
2573 spa_livelist_delete_check(spa_t
*spa
)
2575 return (spa
->spa_livelists_to_delete
!= 0);
2579 spa_livelist_delete_cb_check(void *arg
, zthr_t
*z
)
2583 return (spa_livelist_delete_check(spa
));
2587 delete_blkptr_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2590 zio_free(spa
, tx
->tx_txg
, bp
);
2591 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
2592 -bp_get_dsize_sync(spa
, bp
),
2593 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
2598 dsl_get_next_livelist_obj(objset_t
*os
, uint64_t zap_obj
, uint64_t *llp
)
2603 zap_cursor_init(&zc
, os
, zap_obj
);
2604 err
= zap_cursor_retrieve(&zc
, &za
);
2605 zap_cursor_fini(&zc
);
2607 *llp
= za
.za_first_integer
;
2612 * Components of livelist deletion that must be performed in syncing
2613 * context: freeing block pointers and updating the pool-wide data
2614 * structures to indicate how much work is left to do
2616 typedef struct sublist_delete_arg
{
2621 } sublist_delete_arg_t
;
2624 sublist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2626 sublist_delete_arg_t
*sda
= arg
;
2627 spa_t
*spa
= sda
->spa
;
2628 dsl_deadlist_t
*ll
= sda
->ll
;
2629 uint64_t key
= sda
->key
;
2630 bplist_t
*to_free
= sda
->to_free
;
2632 bplist_iterate(to_free
, delete_blkptr_cb
, spa
, tx
);
2633 dsl_deadlist_remove_entry(ll
, key
, tx
);
2636 typedef struct livelist_delete_arg
{
2640 } livelist_delete_arg_t
;
2643 livelist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2645 livelist_delete_arg_t
*lda
= arg
;
2646 spa_t
*spa
= lda
->spa
;
2647 uint64_t ll_obj
= lda
->ll_obj
;
2648 uint64_t zap_obj
= lda
->zap_obj
;
2649 objset_t
*mos
= spa
->spa_meta_objset
;
2652 /* free the livelist and decrement the feature count */
2653 VERIFY0(zap_remove_int(mos
, zap_obj
, ll_obj
, tx
));
2654 dsl_deadlist_free(mos
, ll_obj
, tx
);
2655 spa_feature_decr(spa
, SPA_FEATURE_LIVELIST
, tx
);
2656 VERIFY0(zap_count(mos
, zap_obj
, &count
));
2658 /* no more livelists to delete */
2659 VERIFY0(zap_remove(mos
, DMU_POOL_DIRECTORY_OBJECT
,
2660 DMU_POOL_DELETED_CLONES
, tx
));
2661 VERIFY0(zap_destroy(mos
, zap_obj
, tx
));
2662 spa
->spa_livelists_to_delete
= 0;
2663 spa_notify_waiters(spa
);
2668 * Load in the value for the livelist to be removed and open it. Then,
2669 * load its first sublist and determine which block pointers should actually
2670 * be freed. Then, call a synctask which performs the actual frees and updates
2671 * the pool-wide livelist data.
2674 spa_livelist_delete_cb(void *arg
, zthr_t
*z
)
2677 uint64_t ll_obj
= 0, count
;
2678 objset_t
*mos
= spa
->spa_meta_objset
;
2679 uint64_t zap_obj
= spa
->spa_livelists_to_delete
;
2681 * Determine the next livelist to delete. This function should only
2682 * be called if there is at least one deleted clone.
2684 VERIFY0(dsl_get_next_livelist_obj(mos
, zap_obj
, &ll_obj
));
2685 VERIFY0(zap_count(mos
, ll_obj
, &count
));
2688 dsl_deadlist_entry_t
*dle
;
2690 ll
= kmem_zalloc(sizeof (dsl_deadlist_t
), KM_SLEEP
);
2691 dsl_deadlist_open(ll
, mos
, ll_obj
);
2692 dle
= dsl_deadlist_first(ll
);
2693 ASSERT3P(dle
, !=, NULL
);
2694 bplist_create(&to_free
);
2695 int err
= dsl_process_sub_livelist(&dle
->dle_bpobj
, &to_free
,
2698 sublist_delete_arg_t sync_arg
= {
2701 .key
= dle
->dle_mintxg
,
2704 zfs_dbgmsg("deleting sublist (id %llu) from"
2705 " livelist %llu, %lld remaining",
2706 (u_longlong_t
)dle
->dle_bpobj
.bpo_object
,
2707 (u_longlong_t
)ll_obj
, (longlong_t
)count
- 1);
2708 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
2709 sublist_delete_sync
, &sync_arg
, 0,
2710 ZFS_SPACE_CHECK_DESTROY
));
2712 VERIFY3U(err
, ==, EINTR
);
2714 bplist_clear(&to_free
);
2715 bplist_destroy(&to_free
);
2716 dsl_deadlist_close(ll
);
2717 kmem_free(ll
, sizeof (dsl_deadlist_t
));
2719 livelist_delete_arg_t sync_arg
= {
2724 zfs_dbgmsg("deletion of livelist %llu completed",
2725 (u_longlong_t
)ll_obj
);
2726 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
, livelist_delete_sync
,
2727 &sync_arg
, 0, ZFS_SPACE_CHECK_DESTROY
));
2732 spa_start_livelist_destroy_thread(spa_t
*spa
)
2734 ASSERT3P(spa
->spa_livelist_delete_zthr
, ==, NULL
);
2735 spa
->spa_livelist_delete_zthr
=
2736 zthr_create("z_livelist_destroy",
2737 spa_livelist_delete_cb_check
, spa_livelist_delete_cb
, spa
,
2741 typedef struct livelist_new_arg
{
2744 } livelist_new_arg_t
;
2747 livelist_track_new_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
2751 livelist_new_arg_t
*lna
= arg
;
2753 bplist_append(lna
->frees
, bp
);
2755 bplist_append(lna
->allocs
, bp
);
2756 zfs_livelist_condense_new_alloc
++;
2761 typedef struct livelist_condense_arg
{
2764 uint64_t first_size
;
2766 } livelist_condense_arg_t
;
2769 spa_livelist_condense_sync(void *arg
, dmu_tx_t
*tx
)
2771 livelist_condense_arg_t
*lca
= arg
;
2772 spa_t
*spa
= lca
->spa
;
2774 dsl_dataset_t
*ds
= spa
->spa_to_condense
.ds
;
2776 /* Have we been cancelled? */
2777 if (spa
->spa_to_condense
.cancelled
) {
2778 zfs_livelist_condense_sync_cancel
++;
2782 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2783 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2784 dsl_deadlist_t
*ll
= &ds
->ds_dir
->dd_livelist
;
2787 * It's possible that the livelist was changed while the zthr was
2788 * running. Therefore, we need to check for new blkptrs in the two
2789 * entries being condensed and continue to track them in the livelist.
2790 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
2791 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
2792 * we need to sort them into two different bplists.
2794 uint64_t first_obj
= first
->dle_bpobj
.bpo_object
;
2795 uint64_t next_obj
= next
->dle_bpobj
.bpo_object
;
2796 uint64_t cur_first_size
= first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2797 uint64_t cur_next_size
= next
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
2799 bplist_create(&new_frees
);
2800 livelist_new_arg_t new_bps
= {
2801 .allocs
= &lca
->to_keep
,
2802 .frees
= &new_frees
,
2805 if (cur_first_size
> lca
->first_size
) {
2806 VERIFY0(livelist_bpobj_iterate_from_nofree(&first
->dle_bpobj
,
2807 livelist_track_new_cb
, &new_bps
, lca
->first_size
));
2809 if (cur_next_size
> lca
->next_size
) {
2810 VERIFY0(livelist_bpobj_iterate_from_nofree(&next
->dle_bpobj
,
2811 livelist_track_new_cb
, &new_bps
, lca
->next_size
));
2814 dsl_deadlist_clear_entry(first
, ll
, tx
);
2815 ASSERT(bpobj_is_empty(&first
->dle_bpobj
));
2816 dsl_deadlist_remove_entry(ll
, next
->dle_mintxg
, tx
);
2818 bplist_iterate(&lca
->to_keep
, dsl_deadlist_insert_alloc_cb
, ll
, tx
);
2819 bplist_iterate(&new_frees
, dsl_deadlist_insert_free_cb
, ll
, tx
);
2820 bplist_destroy(&new_frees
);
2822 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
2823 dsl_dataset_name(ds
, dsname
);
2824 zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
2825 "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
2826 "(%llu blkptrs)", (u_longlong_t
)tx
->tx_txg
, dsname
,
2827 (u_longlong_t
)ds
->ds_object
, (u_longlong_t
)first_obj
,
2828 (u_longlong_t
)cur_first_size
, (u_longlong_t
)next_obj
,
2829 (u_longlong_t
)cur_next_size
,
2830 (u_longlong_t
)first
->dle_bpobj
.bpo_object
,
2831 (u_longlong_t
)first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
);
2833 dmu_buf_rele(ds
->ds_dbuf
, spa
);
2834 spa
->spa_to_condense
.ds
= NULL
;
2835 bplist_clear(&lca
->to_keep
);
2836 bplist_destroy(&lca
->to_keep
);
2837 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2838 spa
->spa_to_condense
.syncing
= B_FALSE
;
2842 spa_livelist_condense_cb(void *arg
, zthr_t
*t
)
2844 while (zfs_livelist_condense_zthr_pause
&&
2845 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2849 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
2850 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
2851 uint64_t first_size
, next_size
;
2853 livelist_condense_arg_t
*lca
=
2854 kmem_alloc(sizeof (livelist_condense_arg_t
), KM_SLEEP
);
2855 bplist_create(&lca
->to_keep
);
2858 * Process the livelists (matching FREEs and ALLOCs) in open context
2859 * so we have minimal work in syncing context to condense.
2861 * We save bpobj sizes (first_size and next_size) to use later in
2862 * syncing context to determine if entries were added to these sublists
2863 * while in open context. This is possible because the clone is still
2864 * active and open for normal writes and we want to make sure the new,
2865 * unprocessed blockpointers are inserted into the livelist normally.
2867 * Note that dsl_process_sub_livelist() both stores the size number of
2868 * blockpointers and iterates over them while the bpobj's lock held, so
2869 * the sizes returned to us are consistent which what was actually
2872 int err
= dsl_process_sub_livelist(&first
->dle_bpobj
, &lca
->to_keep
, t
,
2875 err
= dsl_process_sub_livelist(&next
->dle_bpobj
, &lca
->to_keep
,
2879 while (zfs_livelist_condense_sync_pause
&&
2880 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
2883 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
2884 dmu_tx_mark_netfree(tx
);
2885 dmu_tx_hold_space(tx
, 1);
2886 err
= dmu_tx_assign(tx
, TXG_NOWAIT
| TXG_NOTHROTTLE
);
2889 * Prevent the condense zthr restarting before
2890 * the synctask completes.
2892 spa
->spa_to_condense
.syncing
= B_TRUE
;
2894 lca
->first_size
= first_size
;
2895 lca
->next_size
= next_size
;
2896 dsl_sync_task_nowait(spa_get_dsl(spa
),
2897 spa_livelist_condense_sync
, lca
, tx
);
2903 * Condensing can not continue: either it was externally stopped or
2904 * we were unable to assign to a tx because the pool has run out of
2905 * space. In the second case, we'll just end up trying to condense
2906 * again in a later txg.
2909 bplist_clear(&lca
->to_keep
);
2910 bplist_destroy(&lca
->to_keep
);
2911 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
2912 dmu_buf_rele(spa
->spa_to_condense
.ds
->ds_dbuf
, spa
);
2913 spa
->spa_to_condense
.ds
= NULL
;
2915 zfs_livelist_condense_zthr_cancel
++;
2919 * Check that there is something to condense but that a condense is not
2920 * already in progress and that condensing has not been cancelled.
2923 spa_livelist_condense_cb_check(void *arg
, zthr_t
*z
)
2927 if ((spa
->spa_to_condense
.ds
!= NULL
) &&
2928 (spa
->spa_to_condense
.syncing
== B_FALSE
) &&
2929 (spa
->spa_to_condense
.cancelled
== B_FALSE
)) {
2936 spa_start_livelist_condensing_thread(spa_t
*spa
)
2938 spa
->spa_to_condense
.ds
= NULL
;
2939 spa
->spa_to_condense
.first
= NULL
;
2940 spa
->spa_to_condense
.next
= NULL
;
2941 spa
->spa_to_condense
.syncing
= B_FALSE
;
2942 spa
->spa_to_condense
.cancelled
= B_FALSE
;
2944 ASSERT3P(spa
->spa_livelist_condense_zthr
, ==, NULL
);
2945 spa
->spa_livelist_condense_zthr
=
2946 zthr_create("z_livelist_condense",
2947 spa_livelist_condense_cb_check
,
2948 spa_livelist_condense_cb
, spa
, minclsyspri
);
2952 spa_spawn_aux_threads(spa_t
*spa
)
2954 ASSERT(spa_writeable(spa
));
2956 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2958 spa_start_indirect_condensing_thread(spa
);
2959 spa_start_livelist_destroy_thread(spa
);
2960 spa_start_livelist_condensing_thread(spa
);
2962 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
2963 spa
->spa_checkpoint_discard_zthr
=
2964 zthr_create("z_checkpoint_discard",
2965 spa_checkpoint_discard_thread_check
,
2966 spa_checkpoint_discard_thread
, spa
, minclsyspri
);
2970 * Fix up config after a partly-completed split. This is done with the
2971 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2972 * pool have that entry in their config, but only the splitting one contains
2973 * a list of all the guids of the vdevs that are being split off.
2975 * This function determines what to do with that list: either rejoin
2976 * all the disks to the pool, or complete the splitting process. To attempt
2977 * the rejoin, each disk that is offlined is marked online again, and
2978 * we do a reopen() call. If the vdev label for every disk that was
2979 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2980 * then we call vdev_split() on each disk, and complete the split.
2982 * Otherwise we leave the config alone, with all the vdevs in place in
2983 * the original pool.
2986 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2993 boolean_t attempt_reopen
;
2995 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2998 /* check that the config is complete */
2999 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
3000 &glist
, &gcount
) != 0)
3003 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
3005 /* attempt to online all the vdevs & validate */
3006 attempt_reopen
= B_TRUE
;
3007 for (i
= 0; i
< gcount
; i
++) {
3008 if (glist
[i
] == 0) /* vdev is hole */
3011 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
3012 if (vd
[i
] == NULL
) {
3014 * Don't bother attempting to reopen the disks;
3015 * just do the split.
3017 attempt_reopen
= B_FALSE
;
3019 /* attempt to re-online it */
3020 vd
[i
]->vdev_offline
= B_FALSE
;
3024 if (attempt_reopen
) {
3025 vdev_reopen(spa
->spa_root_vdev
);
3027 /* check each device to see what state it's in */
3028 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
3029 if (vd
[i
] != NULL
&&
3030 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
3037 * If every disk has been moved to the new pool, or if we never
3038 * even attempted to look at them, then we split them off for
3041 if (!attempt_reopen
|| gcount
== extracted
) {
3042 for (i
= 0; i
< gcount
; i
++)
3045 vdev_reopen(spa
->spa_root_vdev
);
3048 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
3052 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
3054 const char *ereport
= FM_EREPORT_ZFS_POOL
;
3057 spa
->spa_load_state
= state
;
3058 (void) spa_import_progress_set_state(spa_guid(spa
),
3059 spa_load_state(spa
));
3061 gethrestime(&spa
->spa_loaded_ts
);
3062 error
= spa_load_impl(spa
, type
, &ereport
);
3065 * Don't count references from objsets that are already closed
3066 * and are making their way through the eviction process.
3068 spa_evicting_os_wait(spa
);
3069 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
3071 if (error
!= EEXIST
) {
3072 spa
->spa_loaded_ts
.tv_sec
= 0;
3073 spa
->spa_loaded_ts
.tv_nsec
= 0;
3075 if (error
!= EBADF
) {
3076 (void) zfs_ereport_post(ereport
, spa
,
3077 NULL
, NULL
, NULL
, 0);
3080 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
3083 (void) spa_import_progress_set_state(spa_guid(spa
),
3084 spa_load_state(spa
));
3091 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
3092 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
3093 * spa's per-vdev ZAP list.
3096 vdev_count_verify_zaps(vdev_t
*vd
)
3098 spa_t
*spa
= vd
->vdev_spa
;
3101 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
) &&
3102 vd
->vdev_root_zap
!= 0) {
3104 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3105 spa
->spa_all_vdev_zaps
, vd
->vdev_root_zap
));
3107 if (vd
->vdev_top_zap
!= 0) {
3109 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3110 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
3112 if (vd
->vdev_leaf_zap
!= 0) {
3114 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3115 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
3118 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3119 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
3125 #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0)
3129 * Determine whether the activity check is required.
3132 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
3136 uint64_t hostid
= 0;
3137 uint64_t tryconfig_txg
= 0;
3138 uint64_t tryconfig_timestamp
= 0;
3139 uint16_t tryconfig_mmp_seq
= 0;
3142 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3143 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3144 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
3146 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
3147 &tryconfig_timestamp
);
3148 (void) nvlist_lookup_uint16(nvinfo
, ZPOOL_CONFIG_MMP_SEQ
,
3149 &tryconfig_mmp_seq
);
3152 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
3155 * Disable the MMP activity check - This is used by zdb which
3156 * is intended to be used on potentially active pools.
3158 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
3162 * Skip the activity check when the MMP feature is disabled.
3164 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
3168 * If the tryconfig_ values are nonzero, they are the results of an
3169 * earlier tryimport. If they all match the uberblock we just found,
3170 * then the pool has not changed and we return false so we do not test
3173 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
3174 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
&&
3175 tryconfig_mmp_seq
&& tryconfig_mmp_seq
==
3176 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0))
3180 * Allow the activity check to be skipped when importing the pool
3181 * on the same host which last imported it. Since the hostid from
3182 * configuration may be stale use the one read from the label.
3184 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
3185 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
3187 if (hostid
== spa_get_hostid(spa
))
3191 * Skip the activity test when the pool was cleanly exported.
3193 if (state
!= POOL_STATE_ACTIVE
)
3200 * Nanoseconds the activity check must watch for changes on-disk.
3203 spa_activity_check_duration(spa_t
*spa
, uberblock_t
*ub
)
3205 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
3206 uint64_t multihost_interval
= MSEC2NSEC(
3207 MMP_INTERVAL_OK(zfs_multihost_interval
));
3208 uint64_t import_delay
= MAX(NANOSEC
, import_intervals
*
3209 multihost_interval
);
3212 * Local tunables determine a minimum duration except for the case
3213 * where we know when the remote host will suspend the pool if MMP
3214 * writes do not land.
3216 * See Big Theory comment at the top of mmp.c for the reasoning behind
3217 * these cases and times.
3220 ASSERT(MMP_IMPORT_SAFETY_FACTOR
>= 100);
3222 if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3223 MMP_FAIL_INT(ub
) > 0) {
3225 /* MMP on remote host will suspend pool after failed writes */
3226 import_delay
= MMP_FAIL_INT(ub
) * MSEC2NSEC(MMP_INTERVAL(ub
)) *
3227 MMP_IMPORT_SAFETY_FACTOR
/ 100;
3229 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3230 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
3231 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3232 (u_longlong_t
)MMP_FAIL_INT(ub
),
3233 (u_longlong_t
)MMP_INTERVAL(ub
),
3234 (u_longlong_t
)import_intervals
);
3236 } else if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3237 MMP_FAIL_INT(ub
) == 0) {
3239 /* MMP on remote host will never suspend pool */
3240 import_delay
= MAX(import_delay
, (MSEC2NSEC(MMP_INTERVAL(ub
)) +
3241 ub
->ub_mmp_delay
) * import_intervals
);
3243 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3244 "mmp_interval=%llu ub_mmp_delay=%llu "
3245 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3246 (u_longlong_t
)MMP_INTERVAL(ub
),
3247 (u_longlong_t
)ub
->ub_mmp_delay
,
3248 (u_longlong_t
)import_intervals
);
3250 } else if (MMP_VALID(ub
)) {
3252 * zfs-0.7 compatibility case
3255 import_delay
= MAX(import_delay
, (multihost_interval
+
3256 ub
->ub_mmp_delay
) * import_intervals
);
3258 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
3259 "import_intervals=%llu leaves=%u",
3260 (u_longlong_t
)import_delay
,
3261 (u_longlong_t
)ub
->ub_mmp_delay
,
3262 (u_longlong_t
)import_intervals
,
3263 vdev_count_leaves(spa
));
3265 /* Using local tunings is the only reasonable option */
3266 zfs_dbgmsg("pool last imported on non-MMP aware "
3267 "host using import_delay=%llu multihost_interval=%llu "
3268 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3269 (u_longlong_t
)multihost_interval
,
3270 (u_longlong_t
)import_intervals
);
3273 return (import_delay
);
3277 * Perform the import activity check. If the user canceled the import or
3278 * we detected activity then fail.
3281 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
)
3283 uint64_t txg
= ub
->ub_txg
;
3284 uint64_t timestamp
= ub
->ub_timestamp
;
3285 uint64_t mmp_config
= ub
->ub_mmp_config
;
3286 uint16_t mmp_seq
= MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0;
3287 uint64_t import_delay
;
3288 hrtime_t import_expire
;
3289 nvlist_t
*mmp_label
= NULL
;
3290 vdev_t
*rvd
= spa
->spa_root_vdev
;
3295 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
3296 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3300 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3301 * during the earlier tryimport. If the txg recorded there is 0 then
3302 * the pool is known to be active on another host.
3304 * Otherwise, the pool might be in use on another host. Check for
3305 * changes in the uberblocks on disk if necessary.
3307 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3308 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
3309 ZPOOL_CONFIG_LOAD_INFO
);
3311 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
3312 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
3313 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3314 error
= SET_ERROR(EREMOTEIO
);
3319 import_delay
= spa_activity_check_duration(spa
, ub
);
3321 /* Add a small random factor in case of simultaneous imports (0-25%) */
3322 import_delay
+= import_delay
* random_in_range(250) / 1000;
3324 import_expire
= gethrtime() + import_delay
;
3326 while (gethrtime() < import_expire
) {
3327 (void) spa_import_progress_set_mmp_check(spa_guid(spa
),
3328 NSEC2SEC(import_expire
- gethrtime()));
3330 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3332 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
||
3333 mmp_seq
!= (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0)) {
3334 zfs_dbgmsg("multihost activity detected "
3335 "txg %llu ub_txg %llu "
3336 "timestamp %llu ub_timestamp %llu "
3337 "mmp_config %#llx ub_mmp_config %#llx",
3338 (u_longlong_t
)txg
, (u_longlong_t
)ub
->ub_txg
,
3339 (u_longlong_t
)timestamp
,
3340 (u_longlong_t
)ub
->ub_timestamp
,
3341 (u_longlong_t
)mmp_config
,
3342 (u_longlong_t
)ub
->ub_mmp_config
);
3344 error
= SET_ERROR(EREMOTEIO
);
3349 nvlist_free(mmp_label
);
3353 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
3355 error
= SET_ERROR(EINTR
);
3363 mutex_destroy(&mtx
);
3367 * If the pool is determined to be active store the status in the
3368 * spa->spa_load_info nvlist. If the remote hostname or hostid are
3369 * available from configuration read from disk store them as well.
3370 * This allows 'zpool import' to generate a more useful message.
3372 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
3373 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3374 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
3376 if (error
== EREMOTEIO
) {
3377 const char *hostname
= "<unknown>";
3378 uint64_t hostid
= 0;
3381 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
3382 hostname
= fnvlist_lookup_string(mmp_label
,
3383 ZPOOL_CONFIG_HOSTNAME
);
3384 fnvlist_add_string(spa
->spa_load_info
,
3385 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
3388 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
3389 hostid
= fnvlist_lookup_uint64(mmp_label
,
3390 ZPOOL_CONFIG_HOSTID
);
3391 fnvlist_add_uint64(spa
->spa_load_info
,
3392 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
3396 fnvlist_add_uint64(spa
->spa_load_info
,
3397 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
3398 fnvlist_add_uint64(spa
->spa_load_info
,
3399 ZPOOL_CONFIG_MMP_TXG
, 0);
3401 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
3405 nvlist_free(mmp_label
);
3411 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
3414 const char *hostname
;
3415 uint64_t myhostid
= 0;
3417 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
3418 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
3419 hostname
= fnvlist_lookup_string(mos_config
,
3420 ZPOOL_CONFIG_HOSTNAME
);
3422 myhostid
= zone_get_hostid(NULL
);
3424 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
3425 cmn_err(CE_WARN
, "pool '%s' could not be "
3426 "loaded as it was last accessed by "
3427 "another system (host: %s hostid: 0x%llx). "
3428 "See: https://openzfs.github.io/openzfs-docs/msg/"
3430 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
3431 spa_load_failed(spa
, "hostid verification failed: pool "
3432 "last accessed by host: %s (hostid: 0x%llx)",
3433 hostname
, (u_longlong_t
)hostid
);
3434 return (SET_ERROR(EBADF
));
3442 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
3445 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
3449 const char *comment
;
3450 const char *compatibility
;
3453 * Versioning wasn't explicitly added to the label until later, so if
3454 * it's not present treat it as the initial version.
3456 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
3457 &spa
->spa_ubsync
.ub_version
) != 0)
3458 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
3460 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
3461 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3462 ZPOOL_CONFIG_POOL_GUID
);
3463 return (SET_ERROR(EINVAL
));
3467 * If we are doing an import, ensure that the pool is not already
3468 * imported by checking if its pool guid already exists in the
3471 * The only case that we allow an already imported pool to be
3472 * imported again, is when the pool is checkpointed and we want to
3473 * look at its checkpointed state from userland tools like zdb.
3476 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3477 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3478 spa_guid_exists(pool_guid
, 0)) {
3480 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3481 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3482 spa_guid_exists(pool_guid
, 0) &&
3483 !spa_importing_readonly_checkpoint(spa
)) {
3485 spa_load_failed(spa
, "a pool with guid %llu is already open",
3486 (u_longlong_t
)pool_guid
);
3487 return (SET_ERROR(EEXIST
));
3490 spa
->spa_config_guid
= pool_guid
;
3492 nvlist_free(spa
->spa_load_info
);
3493 spa
->spa_load_info
= fnvlist_alloc();
3495 ASSERT(spa
->spa_comment
== NULL
);
3496 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3497 spa
->spa_comment
= spa_strdup(comment
);
3499 ASSERT(spa
->spa_compatibility
== NULL
);
3500 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMPATIBILITY
,
3501 &compatibility
) == 0)
3502 spa
->spa_compatibility
= spa_strdup(compatibility
);
3504 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
3505 &spa
->spa_config_txg
);
3507 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
3508 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
3510 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
3511 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3512 ZPOOL_CONFIG_VDEV_TREE
);
3513 return (SET_ERROR(EINVAL
));
3517 * Create "The Godfather" zio to hold all async IOs
3519 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3521 for (int i
= 0; i
< max_ncpus
; i
++) {
3522 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3523 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3524 ZIO_FLAG_GODFATHER
);
3528 * Parse the configuration into a vdev tree. We explicitly set the
3529 * value that will be returned by spa_version() since parsing the
3530 * configuration requires knowing the version number.
3532 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3533 parse
= (type
== SPA_IMPORT_EXISTING
?
3534 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
3535 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
3536 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3539 spa_load_failed(spa
, "unable to parse config [error=%d]",
3544 ASSERT(spa
->spa_root_vdev
== rvd
);
3545 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
3546 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
3548 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3549 ASSERT(spa_guid(spa
) == pool_guid
);
3556 * Recursively open all vdevs in the vdev tree. This function is called twice:
3557 * first with the untrusted config, then with the trusted config.
3560 spa_ld_open_vdevs(spa_t
*spa
)
3565 * spa_missing_tvds_allowed defines how many top-level vdevs can be
3566 * missing/unopenable for the root vdev to be still considered openable.
3568 if (spa
->spa_trust_config
) {
3569 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
3570 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
3571 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
3572 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
3573 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
3575 spa
->spa_missing_tvds_allowed
= 0;
3578 spa
->spa_missing_tvds_allowed
=
3579 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
3581 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3582 error
= vdev_open(spa
->spa_root_vdev
);
3583 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3585 if (spa
->spa_missing_tvds
!= 0) {
3586 spa_load_note(spa
, "vdev tree has %lld missing top-level "
3587 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
3588 if (spa
->spa_trust_config
&& (spa
->spa_mode
& SPA_MODE_WRITE
)) {
3590 * Although theoretically we could allow users to open
3591 * incomplete pools in RW mode, we'd need to add a lot
3592 * of extra logic (e.g. adjust pool space to account
3593 * for missing vdevs).
3594 * This limitation also prevents users from accidentally
3595 * opening the pool in RW mode during data recovery and
3596 * damaging it further.
3598 spa_load_note(spa
, "pools with missing top-level "
3599 "vdevs can only be opened in read-only mode.");
3600 error
= SET_ERROR(ENXIO
);
3602 spa_load_note(spa
, "current settings allow for maximum "
3603 "%lld missing top-level vdevs at this stage.",
3604 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
3608 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
3611 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
3612 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
3618 * We need to validate the vdev labels against the configuration that
3619 * we have in hand. This function is called twice: first with an untrusted
3620 * config, then with a trusted config. The validation is more strict when the
3621 * config is trusted.
3624 spa_ld_validate_vdevs(spa_t
*spa
)
3627 vdev_t
*rvd
= spa
->spa_root_vdev
;
3629 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3630 error
= vdev_validate(rvd
);
3631 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3634 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
3638 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
3639 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
3641 vdev_dbgmsg_print_tree(rvd
, 2);
3642 return (SET_ERROR(ENXIO
));
3649 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
3651 spa
->spa_state
= POOL_STATE_ACTIVE
;
3652 spa
->spa_ubsync
= spa
->spa_uberblock
;
3653 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
3654 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
3655 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
3656 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
3657 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
3658 spa
->spa_prev_software_version
= ub
->ub_software_version
;
3662 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
3664 vdev_t
*rvd
= spa
->spa_root_vdev
;
3666 uberblock_t
*ub
= &spa
->spa_uberblock
;
3667 boolean_t activity_check
= B_FALSE
;
3670 * If we are opening the checkpointed state of the pool by
3671 * rewinding to it, at this point we will have written the
3672 * checkpointed uberblock to the vdev labels, so searching
3673 * the labels will find the right uberblock. However, if
3674 * we are opening the checkpointed state read-only, we have
3675 * not modified the labels. Therefore, we must ignore the
3676 * labels and continue using the spa_uberblock that was set
3677 * by spa_ld_checkpoint_rewind.
3679 * Note that it would be fine to ignore the labels when
3680 * rewinding (opening writeable) as well. However, if we
3681 * crash just after writing the labels, we will end up
3682 * searching the labels. Doing so in the common case means
3683 * that this code path gets exercised normally, rather than
3684 * just in the edge case.
3686 if (ub
->ub_checkpoint_txg
!= 0 &&
3687 spa_importing_readonly_checkpoint(spa
)) {
3688 spa_ld_select_uberblock_done(spa
, ub
);
3693 * Find the best uberblock.
3695 vdev_uberblock_load(rvd
, ub
, &label
);
3698 * If we weren't able to find a single valid uberblock, return failure.
3700 if (ub
->ub_txg
== 0) {
3702 spa_load_failed(spa
, "no valid uberblock found");
3703 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
3706 if (spa
->spa_load_max_txg
!= UINT64_MAX
) {
3707 (void) spa_import_progress_set_max_txg(spa_guid(spa
),
3708 (u_longlong_t
)spa
->spa_load_max_txg
);
3710 spa_load_note(spa
, "using uberblock with txg=%llu",
3711 (u_longlong_t
)ub
->ub_txg
);
3715 * For pools which have the multihost property on determine if the
3716 * pool is truly inactive and can be safely imported. Prevent
3717 * hosts which don't have a hostid set from importing the pool.
3719 activity_check
= spa_activity_check_required(spa
, ub
, label
,
3721 if (activity_check
) {
3722 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
3723 spa_get_hostid(spa
) == 0) {
3725 fnvlist_add_uint64(spa
->spa_load_info
,
3726 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
3727 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
3730 int error
= spa_activity_check(spa
, ub
, spa
->spa_config
);
3736 fnvlist_add_uint64(spa
->spa_load_info
,
3737 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
3738 fnvlist_add_uint64(spa
->spa_load_info
,
3739 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
3740 fnvlist_add_uint16(spa
->spa_load_info
,
3741 ZPOOL_CONFIG_MMP_SEQ
,
3742 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0));
3746 * If the pool has an unsupported version we can't open it.
3748 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
3750 spa_load_failed(spa
, "version %llu is not supported",
3751 (u_longlong_t
)ub
->ub_version
);
3752 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
3755 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3759 * If we weren't able to find what's necessary for reading the
3760 * MOS in the label, return failure.
3762 if (label
== NULL
) {
3763 spa_load_failed(spa
, "label config unavailable");
3764 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3768 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
3771 spa_load_failed(spa
, "invalid label: '%s' missing",
3772 ZPOOL_CONFIG_FEATURES_FOR_READ
);
3773 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
3778 * Update our in-core representation with the definitive values
3781 nvlist_free(spa
->spa_label_features
);
3782 spa
->spa_label_features
= fnvlist_dup(features
);
3788 * Look through entries in the label nvlist's features_for_read. If
3789 * there is a feature listed there which we don't understand then we
3790 * cannot open a pool.
3792 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
3793 nvlist_t
*unsup_feat
;
3795 unsup_feat
= fnvlist_alloc();
3797 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
3799 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
3800 if (!zfeature_is_supported(nvpair_name(nvp
))) {
3801 fnvlist_add_string(unsup_feat
,
3802 nvpair_name(nvp
), "");
3806 if (!nvlist_empty(unsup_feat
)) {
3807 fnvlist_add_nvlist(spa
->spa_load_info
,
3808 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
3809 nvlist_free(unsup_feat
);
3810 spa_load_failed(spa
, "some features are unsupported");
3811 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
3815 nvlist_free(unsup_feat
);
3818 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
3819 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3820 spa_try_repair(spa
, spa
->spa_config
);
3821 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3822 nvlist_free(spa
->spa_config_splitting
);
3823 spa
->spa_config_splitting
= NULL
;
3827 * Initialize internal SPA structures.
3829 spa_ld_select_uberblock_done(spa
, ub
);
3835 spa_ld_open_rootbp(spa_t
*spa
)
3838 vdev_t
*rvd
= spa
->spa_root_vdev
;
3840 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
3842 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
3843 "[error=%d]", error
);
3844 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3846 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
3852 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
3853 boolean_t reloading
)
3855 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
3856 nvlist_t
*nv
, *mos_config
, *policy
;
3857 int error
= 0, copy_error
;
3858 uint64_t healthy_tvds
, healthy_tvds_mos
;
3859 uint64_t mos_config_txg
;
3861 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
3863 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3866 * If we're assembling a pool from a split, the config provided is
3867 * already trusted so there is nothing to do.
3869 if (type
== SPA_IMPORT_ASSEMBLE
)
3872 healthy_tvds
= spa_healthy_core_tvds(spa
);
3874 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
3876 spa_load_failed(spa
, "unable to retrieve MOS config");
3877 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
3881 * If we are doing an open, pool owner wasn't verified yet, thus do
3882 * the verification here.
3884 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
3885 error
= spa_verify_host(spa
, mos_config
);
3887 nvlist_free(mos_config
);
3892 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
3894 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3897 * Build a new vdev tree from the trusted config
3899 error
= spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
);
3901 nvlist_free(mos_config
);
3902 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3903 spa_load_failed(spa
, "spa_config_parse failed [error=%d]",
3905 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
3909 * Vdev paths in the MOS may be obsolete. If the untrusted config was
3910 * obtained by scanning /dev/dsk, then it will have the right vdev
3911 * paths. We update the trusted MOS config with this information.
3912 * We first try to copy the paths with vdev_copy_path_strict, which
3913 * succeeds only when both configs have exactly the same vdev tree.
3914 * If that fails, we fall back to a more flexible method that has a
3915 * best effort policy.
3917 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
3918 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3919 spa_load_note(spa
, "provided vdev tree:");
3920 vdev_dbgmsg_print_tree(rvd
, 2);
3921 spa_load_note(spa
, "MOS vdev tree:");
3922 vdev_dbgmsg_print_tree(mrvd
, 2);
3924 if (copy_error
!= 0) {
3925 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
3926 "back to vdev_copy_path_relaxed");
3927 vdev_copy_path_relaxed(rvd
, mrvd
);
3932 spa
->spa_root_vdev
= mrvd
;
3934 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3937 * If 'zpool import' used a cached config, then the on-disk hostid and
3938 * hostname may be different to the cached config in ways that should
3939 * prevent import. Userspace can't discover this without a scan, but
3940 * we know, so we add these values to LOAD_INFO so the caller can know
3943 * Note that we have to do this before the config is regenerated,
3944 * because the new config will have the hostid and hostname for this
3945 * host, in readiness for import.
3947 if (nvlist_exists(mos_config
, ZPOOL_CONFIG_HOSTID
))
3948 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_HOSTID
,
3949 fnvlist_lookup_uint64(mos_config
, ZPOOL_CONFIG_HOSTID
));
3950 if (nvlist_exists(mos_config
, ZPOOL_CONFIG_HOSTNAME
))
3951 fnvlist_add_string(spa
->spa_load_info
, ZPOOL_CONFIG_HOSTNAME
,
3952 fnvlist_lookup_string(mos_config
, ZPOOL_CONFIG_HOSTNAME
));
3955 * We will use spa_config if we decide to reload the spa or if spa_load
3956 * fails and we rewind. We must thus regenerate the config using the
3957 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
3958 * pass settings on how to load the pool and is not stored in the MOS.
3959 * We copy it over to our new, trusted config.
3961 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
3962 ZPOOL_CONFIG_POOL_TXG
);
3963 nvlist_free(mos_config
);
3964 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
3965 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
3967 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
3968 spa_config_set(spa
, mos_config
);
3969 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
3972 * Now that we got the config from the MOS, we should be more strict
3973 * in checking blkptrs and can make assumptions about the consistency
3974 * of the vdev tree. spa_trust_config must be set to true before opening
3975 * vdevs in order for them to be writeable.
3977 spa
->spa_trust_config
= B_TRUE
;
3980 * Open and validate the new vdev tree
3982 error
= spa_ld_open_vdevs(spa
);
3986 error
= spa_ld_validate_vdevs(spa
);
3990 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
3991 spa_load_note(spa
, "final vdev tree:");
3992 vdev_dbgmsg_print_tree(rvd
, 2);
3995 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
3996 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
3998 * Sanity check to make sure that we are indeed loading the
3999 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
4000 * in the config provided and they happened to be the only ones
4001 * to have the latest uberblock, we could involuntarily perform
4002 * an extreme rewind.
4004 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
4005 if (healthy_tvds_mos
- healthy_tvds
>=
4006 SPA_SYNC_MIN_VDEVS
) {
4007 spa_load_note(spa
, "config provided misses too many "
4008 "top-level vdevs compared to MOS (%lld vs %lld). ",
4009 (u_longlong_t
)healthy_tvds
,
4010 (u_longlong_t
)healthy_tvds_mos
);
4011 spa_load_note(spa
, "vdev tree:");
4012 vdev_dbgmsg_print_tree(rvd
, 2);
4014 spa_load_failed(spa
, "config was already "
4015 "provided from MOS. Aborting.");
4016 return (spa_vdev_err(rvd
,
4017 VDEV_AUX_CORRUPT_DATA
, EIO
));
4019 spa_load_note(spa
, "spa must be reloaded using MOS "
4021 return (SET_ERROR(EAGAIN
));
4025 error
= spa_check_for_missing_logs(spa
);
4027 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
4029 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
4030 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
4031 "guid sum (%llu != %llu)",
4032 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
4033 (u_longlong_t
)rvd
->vdev_guid_sum
);
4034 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
4042 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
4045 vdev_t
*rvd
= spa
->spa_root_vdev
;
4048 * Everything that we read before spa_remove_init() must be stored
4049 * on concreted vdevs. Therefore we do this as early as possible.
4051 error
= spa_remove_init(spa
);
4053 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
4055 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4059 * Retrieve information needed to condense indirect vdev mappings.
4061 error
= spa_condense_init(spa
);
4063 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
4065 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4072 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
4075 vdev_t
*rvd
= spa
->spa_root_vdev
;
4077 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
4078 boolean_t missing_feat_read
= B_FALSE
;
4079 nvlist_t
*unsup_feat
, *enabled_feat
;
4081 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
4082 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
4083 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4086 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
4087 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
4088 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4091 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
4092 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
4093 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4096 enabled_feat
= fnvlist_alloc();
4097 unsup_feat
= fnvlist_alloc();
4099 if (!spa_features_check(spa
, B_FALSE
,
4100 unsup_feat
, enabled_feat
))
4101 missing_feat_read
= B_TRUE
;
4103 if (spa_writeable(spa
) ||
4104 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
4105 if (!spa_features_check(spa
, B_TRUE
,
4106 unsup_feat
, enabled_feat
)) {
4107 *missing_feat_writep
= B_TRUE
;
4111 fnvlist_add_nvlist(spa
->spa_load_info
,
4112 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
4114 if (!nvlist_empty(unsup_feat
)) {
4115 fnvlist_add_nvlist(spa
->spa_load_info
,
4116 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4119 fnvlist_free(enabled_feat
);
4120 fnvlist_free(unsup_feat
);
4122 if (!missing_feat_read
) {
4123 fnvlist_add_boolean(spa
->spa_load_info
,
4124 ZPOOL_CONFIG_CAN_RDONLY
);
4128 * If the state is SPA_LOAD_TRYIMPORT, our objective is
4129 * twofold: to determine whether the pool is available for
4130 * import in read-write mode and (if it is not) whether the
4131 * pool is available for import in read-only mode. If the pool
4132 * is available for import in read-write mode, it is displayed
4133 * as available in userland; if it is not available for import
4134 * in read-only mode, it is displayed as unavailable in
4135 * userland. If the pool is available for import in read-only
4136 * mode but not read-write mode, it is displayed as unavailable
4137 * in userland with a special note that the pool is actually
4138 * available for open in read-only mode.
4140 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
4141 * missing a feature for write, we must first determine whether
4142 * the pool can be opened read-only before returning to
4143 * userland in order to know whether to display the
4144 * abovementioned note.
4146 if (missing_feat_read
|| (*missing_feat_writep
&&
4147 spa_writeable(spa
))) {
4148 spa_load_failed(spa
, "pool uses unsupported features");
4149 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4154 * Load refcounts for ZFS features from disk into an in-memory
4155 * cache during SPA initialization.
4157 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
4160 error
= feature_get_refcount_from_disk(spa
,
4161 &spa_feature_table
[i
], &refcount
);
4163 spa
->spa_feat_refcount_cache
[i
] = refcount
;
4164 } else if (error
== ENOTSUP
) {
4165 spa
->spa_feat_refcount_cache
[i
] =
4166 SPA_FEATURE_DISABLED
;
4168 spa_load_failed(spa
, "error getting refcount "
4169 "for feature %s [error=%d]",
4170 spa_feature_table
[i
].fi_guid
, error
);
4171 return (spa_vdev_err(rvd
,
4172 VDEV_AUX_CORRUPT_DATA
, EIO
));
4177 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
4178 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
4179 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
4180 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4184 * Encryption was added before bookmark_v2, even though bookmark_v2
4185 * is now a dependency. If this pool has encryption enabled without
4186 * bookmark_v2, trigger an errata message.
4188 if (spa_feature_is_enabled(spa
, SPA_FEATURE_ENCRYPTION
) &&
4189 !spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
4190 spa
->spa_errata
= ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
4197 spa_ld_load_special_directories(spa_t
*spa
)
4200 vdev_t
*rvd
= spa
->spa_root_vdev
;
4202 spa
->spa_is_initializing
= B_TRUE
;
4203 error
= dsl_pool_open(spa
->spa_dsl_pool
);
4204 spa
->spa_is_initializing
= B_FALSE
;
4206 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
4207 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4214 spa_ld_get_props(spa_t
*spa
)
4218 vdev_t
*rvd
= spa
->spa_root_vdev
;
4220 /* Grab the checksum salt from the MOS. */
4221 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4222 DMU_POOL_CHECKSUM_SALT
, 1,
4223 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
4224 spa
->spa_cksum_salt
.zcs_bytes
);
4225 if (error
== ENOENT
) {
4226 /* Generate a new salt for subsequent use */
4227 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4228 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4229 } else if (error
!= 0) {
4230 spa_load_failed(spa
, "unable to retrieve checksum salt from "
4231 "MOS [error=%d]", error
);
4232 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4235 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
4236 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4237 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
4239 spa_load_failed(spa
, "error opening deferred-frees bpobj "
4240 "[error=%d]", error
);
4241 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4245 * Load the bit that tells us to use the new accounting function
4246 * (raid-z deflation). If we have an older pool, this will not
4249 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
4250 if (error
!= 0 && error
!= ENOENT
)
4251 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4253 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
4254 &spa
->spa_creation_version
, B_FALSE
);
4255 if (error
!= 0 && error
!= ENOENT
)
4256 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4259 * Load the persistent error log. If we have an older pool, this will
4262 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
4264 if (error
!= 0 && error
!= ENOENT
)
4265 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4267 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
4268 &spa
->spa_errlog_scrub
, B_FALSE
);
4269 if (error
!= 0 && error
!= ENOENT
)
4270 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4273 * Load the livelist deletion field. If a livelist is queued for
4274 * deletion, indicate that in the spa
4276 error
= spa_dir_prop(spa
, DMU_POOL_DELETED_CLONES
,
4277 &spa
->spa_livelists_to_delete
, B_FALSE
);
4278 if (error
!= 0 && error
!= ENOENT
)
4279 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4282 * Load the history object. If we have an older pool, this
4283 * will not be present.
4285 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
4286 if (error
!= 0 && error
!= ENOENT
)
4287 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4290 * Load the per-vdev ZAP map. If we have an older pool, this will not
4291 * be present; in this case, defer its creation to a later time to
4292 * avoid dirtying the MOS this early / out of sync context. See
4293 * spa_sync_config_object.
4296 /* The sentinel is only available in the MOS config. */
4297 nvlist_t
*mos_config
;
4298 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
4299 spa_load_failed(spa
, "unable to retrieve MOS config");
4300 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4303 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
4304 &spa
->spa_all_vdev_zaps
, B_FALSE
);
4306 if (error
== ENOENT
) {
4307 VERIFY(!nvlist_exists(mos_config
,
4308 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
4309 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
4310 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4311 } else if (error
!= 0) {
4312 nvlist_free(mos_config
);
4313 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4314 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
4316 * An older version of ZFS overwrote the sentinel value, so
4317 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4318 * destruction to later; see spa_sync_config_object.
4320 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
4322 * We're assuming that no vdevs have had their ZAPs created
4323 * before this. Better be sure of it.
4325 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4327 nvlist_free(mos_config
);
4329 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4331 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
4333 if (error
&& error
!= ENOENT
)
4334 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4337 uint64_t autoreplace
= 0;
4339 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
4340 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
4341 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
4342 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
4343 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
4344 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
4345 spa_prop_find(spa
, ZPOOL_PROP_AUTOTRIM
, &spa
->spa_autotrim
);
4346 spa
->spa_autoreplace
= (autoreplace
!= 0);
4350 * If we are importing a pool with missing top-level vdevs,
4351 * we enforce that the pool doesn't panic or get suspended on
4352 * error since the likelihood of missing data is extremely high.
4354 if (spa
->spa_missing_tvds
> 0 &&
4355 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
4356 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4357 spa_load_note(spa
, "forcing failmode to 'continue' "
4358 "as some top level vdevs are missing");
4359 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
4366 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
4369 vdev_t
*rvd
= spa
->spa_root_vdev
;
4372 * If we're assembling the pool from the split-off vdevs of
4373 * an existing pool, we don't want to attach the spares & cache
4378 * Load any hot spares for this pool.
4380 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
4382 if (error
!= 0 && error
!= ENOENT
)
4383 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4384 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4385 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
4386 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
4387 &spa
->spa_spares
.sav_config
) != 0) {
4388 spa_load_failed(spa
, "error loading spares nvlist");
4389 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4392 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4393 spa_load_spares(spa
);
4394 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4395 } else if (error
== 0) {
4396 spa
->spa_spares
.sav_sync
= B_TRUE
;
4400 * Load any level 2 ARC devices for this pool.
4402 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
4403 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
4404 if (error
!= 0 && error
!= ENOENT
)
4405 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4406 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4407 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
4408 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
4409 &spa
->spa_l2cache
.sav_config
) != 0) {
4410 spa_load_failed(spa
, "error loading l2cache nvlist");
4411 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4414 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4415 spa_load_l2cache(spa
);
4416 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4417 } else if (error
== 0) {
4418 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4425 spa_ld_load_vdev_metadata(spa_t
*spa
)
4428 vdev_t
*rvd
= spa
->spa_root_vdev
;
4431 * If the 'multihost' property is set, then never allow a pool to
4432 * be imported when the system hostid is zero. The exception to
4433 * this rule is zdb which is always allowed to access pools.
4435 if (spa_multihost(spa
) && spa_get_hostid(spa
) == 0 &&
4436 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
4437 fnvlist_add_uint64(spa
->spa_load_info
,
4438 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4439 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4443 * If the 'autoreplace' property is set, then post a resource notifying
4444 * the ZFS DE that it should not issue any faults for unopenable
4445 * devices. We also iterate over the vdevs, and post a sysevent for any
4446 * unopenable vdevs so that the normal autoreplace handler can take
4449 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4450 spa_check_removed(spa
->spa_root_vdev
);
4452 * For the import case, this is done in spa_import(), because
4453 * at this point we're using the spare definitions from
4454 * the MOS config, not necessarily from the userland config.
4456 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
4457 spa_aux_check_removed(&spa
->spa_spares
);
4458 spa_aux_check_removed(&spa
->spa_l2cache
);
4463 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
4465 error
= vdev_load(rvd
);
4467 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
4468 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4471 error
= spa_ld_log_spacemaps(spa
);
4473 spa_load_failed(spa
, "spa_ld_log_spacemaps failed [error=%d]",
4475 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4479 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
4481 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4482 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
, B_FALSE
);
4483 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4489 spa_ld_load_dedup_tables(spa_t
*spa
)
4492 vdev_t
*rvd
= spa
->spa_root_vdev
;
4494 error
= ddt_load(spa
);
4496 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
4497 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4504 spa_ld_load_brt(spa_t
*spa
)
4507 vdev_t
*rvd
= spa
->spa_root_vdev
;
4509 error
= brt_load(spa
);
4511 spa_load_failed(spa
, "brt_load failed [error=%d]", error
);
4512 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4519 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4521 vdev_t
*rvd
= spa
->spa_root_vdev
;
4523 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
4524 boolean_t missing
= spa_check_logs(spa
);
4526 if (spa
->spa_missing_tvds
!= 0) {
4527 spa_load_note(spa
, "spa_check_logs failed "
4528 "so dropping the logs");
4530 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
4531 spa_load_failed(spa
, "spa_check_logs failed");
4532 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
4542 spa_ld_verify_pool_data(spa_t
*spa
)
4545 vdev_t
*rvd
= spa
->spa_root_vdev
;
4548 * We've successfully opened the pool, verify that we're ready
4549 * to start pushing transactions.
4551 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4552 error
= spa_load_verify(spa
);
4554 spa_load_failed(spa
, "spa_load_verify failed "
4555 "[error=%d]", error
);
4556 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4565 spa_ld_claim_log_blocks(spa_t
*spa
)
4568 dsl_pool_t
*dp
= spa_get_dsl(spa
);
4571 * Claim log blocks that haven't been committed yet.
4572 * This must all happen in a single txg.
4573 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
4574 * invoked from zil_claim_log_block()'s i/o done callback.
4575 * Price of rollback is that we abandon the log.
4577 spa
->spa_claiming
= B_TRUE
;
4579 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
4580 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
4581 zil_claim
, tx
, DS_FIND_CHILDREN
);
4584 spa
->spa_claiming
= B_FALSE
;
4586 spa_set_log_state(spa
, SPA_LOG_GOOD
);
4590 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
4591 boolean_t update_config_cache
)
4593 vdev_t
*rvd
= spa
->spa_root_vdev
;
4594 int need_update
= B_FALSE
;
4597 * If the config cache is stale, or we have uninitialized
4598 * metaslabs (see spa_vdev_add()), then update the config.
4600 * If this is a verbatim import, trust the current
4601 * in-core spa_config and update the disk labels.
4603 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
4604 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
4605 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4606 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
4607 need_update
= B_TRUE
;
4609 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
4610 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
4611 need_update
= B_TRUE
;
4614 * Update the config cache asynchronously in case we're the
4615 * root pool, in which case the config cache isn't writable yet.
4618 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
4622 spa_ld_prepare_for_reload(spa_t
*spa
)
4624 spa_mode_t mode
= spa
->spa_mode
;
4625 int async_suspended
= spa
->spa_async_suspended
;
4628 spa_deactivate(spa
);
4629 spa_activate(spa
, mode
);
4632 * We save the value of spa_async_suspended as it gets reset to 0 by
4633 * spa_unload(). We want to restore it back to the original value before
4634 * returning as we might be calling spa_async_resume() later.
4636 spa
->spa_async_suspended
= async_suspended
;
4640 spa_ld_read_checkpoint_txg(spa_t
*spa
)
4642 uberblock_t checkpoint
;
4645 ASSERT0(spa
->spa_checkpoint_txg
);
4646 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4648 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4649 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4650 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4652 if (error
== ENOENT
)
4658 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
4659 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
4660 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
4661 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
4662 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
4668 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
4672 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4673 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4676 * Never trust the config that is provided unless we are assembling
4677 * a pool following a split.
4678 * This means don't trust blkptrs and the vdev tree in general. This
4679 * also effectively puts the spa in read-only mode since
4680 * spa_writeable() checks for spa_trust_config to be true.
4681 * We will later load a trusted config from the MOS.
4683 if (type
!= SPA_IMPORT_ASSEMBLE
)
4684 spa
->spa_trust_config
= B_FALSE
;
4687 * Parse the config provided to create a vdev tree.
4689 error
= spa_ld_parse_config(spa
, type
);
4693 spa_import_progress_add(spa
);
4696 * Now that we have the vdev tree, try to open each vdev. This involves
4697 * opening the underlying physical device, retrieving its geometry and
4698 * probing the vdev with a dummy I/O. The state of each vdev will be set
4699 * based on the success of those operations. After this we'll be ready
4700 * to read from the vdevs.
4702 error
= spa_ld_open_vdevs(spa
);
4707 * Read the label of each vdev and make sure that the GUIDs stored
4708 * there match the GUIDs in the config provided.
4709 * If we're assembling a new pool that's been split off from an
4710 * existing pool, the labels haven't yet been updated so we skip
4711 * validation for now.
4713 if (type
!= SPA_IMPORT_ASSEMBLE
) {
4714 error
= spa_ld_validate_vdevs(spa
);
4720 * Read all vdev labels to find the best uberblock (i.e. latest,
4721 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
4722 * get the list of features required to read blkptrs in the MOS from
4723 * the vdev label with the best uberblock and verify that our version
4724 * of zfs supports them all.
4726 error
= spa_ld_select_uberblock(spa
, type
);
4731 * Pass that uberblock to the dsl_pool layer which will open the root
4732 * blkptr. This blkptr points to the latest version of the MOS and will
4733 * allow us to read its contents.
4735 error
= spa_ld_open_rootbp(spa
);
4743 spa_ld_checkpoint_rewind(spa_t
*spa
)
4745 uberblock_t checkpoint
;
4748 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4749 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4751 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4752 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
4753 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
4756 spa_load_failed(spa
, "unable to retrieve checkpointed "
4757 "uberblock from the MOS config [error=%d]", error
);
4759 if (error
== ENOENT
)
4760 error
= ZFS_ERR_NO_CHECKPOINT
;
4765 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
4766 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
4769 * We need to update the txg and timestamp of the checkpointed
4770 * uberblock to be higher than the latest one. This ensures that
4771 * the checkpointed uberblock is selected if we were to close and
4772 * reopen the pool right after we've written it in the vdev labels.
4773 * (also see block comment in vdev_uberblock_compare)
4775 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
4776 checkpoint
.ub_timestamp
= gethrestime_sec();
4779 * Set current uberblock to be the checkpointed uberblock.
4781 spa
->spa_uberblock
= checkpoint
;
4784 * If we are doing a normal rewind, then the pool is open for
4785 * writing and we sync the "updated" checkpointed uberblock to
4786 * disk. Once this is done, we've basically rewound the whole
4787 * pool and there is no way back.
4789 * There are cases when we don't want to attempt and sync the
4790 * checkpointed uberblock to disk because we are opening a
4791 * pool as read-only. Specifically, verifying the checkpointed
4792 * state with zdb, and importing the checkpointed state to get
4793 * a "preview" of its content.
4795 if (spa_writeable(spa
)) {
4796 vdev_t
*rvd
= spa
->spa_root_vdev
;
4798 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4799 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
4801 int children
= rvd
->vdev_children
;
4802 int c0
= random_in_range(children
);
4804 for (int c
= 0; c
< children
; c
++) {
4805 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
4807 /* Stop when revisiting the first vdev */
4808 if (c
> 0 && svd
[0] == vd
)
4811 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
4812 !vdev_is_concrete(vd
))
4815 svd
[svdcount
++] = vd
;
4816 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
4819 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
4821 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
4822 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4825 spa_load_failed(spa
, "failed to write checkpointed "
4826 "uberblock to the vdev labels [error=%d]", error
);
4835 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
4836 boolean_t
*update_config_cache
)
4841 * Parse the config for pool, open and validate vdevs,
4842 * select an uberblock, and use that uberblock to open
4845 error
= spa_ld_mos_init(spa
, type
);
4850 * Retrieve the trusted config stored in the MOS and use it to create
4851 * a new, exact version of the vdev tree, then reopen all vdevs.
4853 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
4854 if (error
== EAGAIN
) {
4855 if (update_config_cache
!= NULL
)
4856 *update_config_cache
= B_TRUE
;
4859 * Redo the loading process with the trusted config if it is
4860 * too different from the untrusted config.
4862 spa_ld_prepare_for_reload(spa
);
4863 spa_load_note(spa
, "RELOADING");
4864 error
= spa_ld_mos_init(spa
, type
);
4868 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
4872 } else if (error
!= 0) {
4880 * Load an existing storage pool, using the config provided. This config
4881 * describes which vdevs are part of the pool and is later validated against
4882 * partial configs present in each vdev's label and an entire copy of the
4883 * config stored in the MOS.
4886 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4889 boolean_t missing_feat_write
= B_FALSE
;
4890 boolean_t checkpoint_rewind
=
4891 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
4892 boolean_t update_config_cache
= B_FALSE
;
4894 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
4895 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
4897 spa_load_note(spa
, "LOADING");
4899 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
4904 * If we are rewinding to the checkpoint then we need to repeat
4905 * everything we've done so far in this function but this time
4906 * selecting the checkpointed uberblock and using that to open
4909 if (checkpoint_rewind
) {
4911 * If we are rewinding to the checkpoint update config cache
4914 update_config_cache
= B_TRUE
;
4917 * Extract the checkpointed uberblock from the current MOS
4918 * and use this as the pool's uberblock from now on. If the
4919 * pool is imported as writeable we also write the checkpoint
4920 * uberblock to the labels, making the rewind permanent.
4922 error
= spa_ld_checkpoint_rewind(spa
);
4927 * Redo the loading process again with the
4928 * checkpointed uberblock.
4930 spa_ld_prepare_for_reload(spa
);
4931 spa_load_note(spa
, "LOADING checkpointed uberblock");
4932 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
4938 * Retrieve the checkpoint txg if the pool has a checkpoint.
4940 error
= spa_ld_read_checkpoint_txg(spa
);
4945 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
4946 * from the pool and their contents were re-mapped to other vdevs. Note
4947 * that everything that we read before this step must have been
4948 * rewritten on concrete vdevs after the last device removal was
4949 * initiated. Otherwise we could be reading from indirect vdevs before
4950 * we have loaded their mappings.
4952 error
= spa_ld_open_indirect_vdev_metadata(spa
);
4957 * Retrieve the full list of active features from the MOS and check if
4958 * they are all supported.
4960 error
= spa_ld_check_features(spa
, &missing_feat_write
);
4965 * Load several special directories from the MOS needed by the dsl_pool
4968 error
= spa_ld_load_special_directories(spa
);
4973 * Retrieve pool properties from the MOS.
4975 error
= spa_ld_get_props(spa
);
4980 * Retrieve the list of auxiliary devices - cache devices and spares -
4983 error
= spa_ld_open_aux_vdevs(spa
, type
);
4988 * Load the metadata for all vdevs. Also check if unopenable devices
4989 * should be autoreplaced.
4991 error
= spa_ld_load_vdev_metadata(spa
);
4995 error
= spa_ld_load_dedup_tables(spa
);
4999 error
= spa_ld_load_brt(spa
);
5004 * Verify the logs now to make sure we don't have any unexpected errors
5005 * when we claim log blocks later.
5007 error
= spa_ld_verify_logs(spa
, type
, ereport
);
5011 if (missing_feat_write
) {
5012 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
5015 * At this point, we know that we can open the pool in
5016 * read-only mode but not read-write mode. We now have enough
5017 * information and can return to userland.
5019 return (spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
5024 * Traverse the last txgs to make sure the pool was left off in a safe
5025 * state. When performing an extreme rewind, we verify the whole pool,
5026 * which can take a very long time.
5028 error
= spa_ld_verify_pool_data(spa
);
5033 * Calculate the deflated space for the pool. This must be done before
5034 * we write anything to the pool because we'd need to update the space
5035 * accounting using the deflated sizes.
5037 spa_update_dspace(spa
);
5040 * We have now retrieved all the information we needed to open the
5041 * pool. If we are importing the pool in read-write mode, a few
5042 * additional steps must be performed to finish the import.
5044 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
5045 spa
->spa_load_max_txg
== UINT64_MAX
)) {
5046 uint64_t config_cache_txg
= spa
->spa_config_txg
;
5048 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
5051 * In case of a checkpoint rewind, log the original txg
5052 * of the checkpointed uberblock.
5054 if (checkpoint_rewind
) {
5055 spa_history_log_internal(spa
, "checkpoint rewind",
5056 NULL
, "rewound state to txg=%llu",
5057 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
5061 * Traverse the ZIL and claim all blocks.
5063 spa_ld_claim_log_blocks(spa
);
5066 * Kick-off the syncing thread.
5068 spa
->spa_sync_on
= B_TRUE
;
5069 txg_sync_start(spa
->spa_dsl_pool
);
5070 mmp_thread_start(spa
);
5073 * Wait for all claims to sync. We sync up to the highest
5074 * claimed log block birth time so that claimed log blocks
5075 * don't appear to be from the future. spa_claim_max_txg
5076 * will have been set for us by ZIL traversal operations
5079 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
5082 * Check if we need to request an update of the config. On the
5083 * next sync, we would update the config stored in vdev labels
5084 * and the cachefile (by default /etc/zfs/zpool.cache).
5086 spa_ld_check_for_config_update(spa
, config_cache_txg
,
5087 update_config_cache
);
5090 * Check if a rebuild was in progress and if so resume it.
5091 * Then check all DTLs to see if anything needs resilvering.
5092 * The resilver will be deferred if a rebuild was started.
5094 if (vdev_rebuild_active(spa
->spa_root_vdev
)) {
5095 vdev_rebuild_restart(spa
);
5096 } else if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
5097 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5098 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
5102 * Log the fact that we booted up (so that we can detect if
5103 * we rebooted in the middle of an operation).
5105 spa_history_log_version(spa
, "open", NULL
);
5107 spa_restart_removal(spa
);
5108 spa_spawn_aux_threads(spa
);
5111 * Delete any inconsistent datasets.
5114 * Since we may be issuing deletes for clones here,
5115 * we make sure to do so after we've spawned all the
5116 * auxiliary threads above (from which the livelist
5117 * deletion zthr is part of).
5119 (void) dmu_objset_find(spa_name(spa
),
5120 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
5123 * Clean up any stale temporary dataset userrefs.
5125 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
5127 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5128 vdev_initialize_restart(spa
->spa_root_vdev
);
5129 vdev_trim_restart(spa
->spa_root_vdev
);
5130 vdev_autotrim_restart(spa
);
5131 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5134 spa_import_progress_remove(spa_guid(spa
));
5135 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
5137 spa_load_note(spa
, "LOADED");
5143 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
5145 spa_mode_t mode
= spa
->spa_mode
;
5148 spa_deactivate(spa
);
5150 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
5152 spa_activate(spa
, mode
);
5153 spa_async_suspend(spa
);
5155 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
5156 (u_longlong_t
)spa
->spa_load_max_txg
);
5158 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
5162 * If spa_load() fails this function will try loading prior txg's. If
5163 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
5164 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
5165 * function will not rewind the pool and will return the same error as
5169 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
5172 nvlist_t
*loadinfo
= NULL
;
5173 nvlist_t
*config
= NULL
;
5174 int load_error
, rewind_error
;
5175 uint64_t safe_rewind_txg
;
5178 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
5179 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
5180 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5182 spa
->spa_load_max_txg
= max_request
;
5183 if (max_request
!= UINT64_MAX
)
5184 spa
->spa_extreme_rewind
= B_TRUE
;
5187 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
5188 if (load_error
== 0)
5190 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
5192 * When attempting checkpoint-rewind on a pool with no
5193 * checkpoint, we should not attempt to load uberblocks
5194 * from previous txgs when spa_load fails.
5196 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5197 spa_import_progress_remove(spa_guid(spa
));
5198 return (load_error
);
5201 if (spa
->spa_root_vdev
!= NULL
)
5202 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5204 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
5205 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
5207 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
5208 nvlist_free(config
);
5209 spa_import_progress_remove(spa_guid(spa
));
5210 return (load_error
);
5213 if (state
== SPA_LOAD_RECOVER
) {
5214 /* Price of rolling back is discarding txgs, including log */
5215 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5218 * If we aren't rolling back save the load info from our first
5219 * import attempt so that we can restore it after attempting
5222 loadinfo
= spa
->spa_load_info
;
5223 spa
->spa_load_info
= fnvlist_alloc();
5226 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
5227 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
5228 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
5229 TXG_INITIAL
: safe_rewind_txg
;
5232 * Continue as long as we're finding errors, we're still within
5233 * the acceptable rewind range, and we're still finding uberblocks
5235 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
5236 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
5237 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
5238 spa
->spa_extreme_rewind
= B_TRUE
;
5239 rewind_error
= spa_load_retry(spa
, state
);
5242 spa
->spa_extreme_rewind
= B_FALSE
;
5243 spa
->spa_load_max_txg
= UINT64_MAX
;
5245 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
5246 spa_config_set(spa
, config
);
5248 nvlist_free(config
);
5250 if (state
== SPA_LOAD_RECOVER
) {
5251 ASSERT3P(loadinfo
, ==, NULL
);
5252 spa_import_progress_remove(spa_guid(spa
));
5253 return (rewind_error
);
5255 /* Store the rewind info as part of the initial load info */
5256 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
5257 spa
->spa_load_info
);
5259 /* Restore the initial load info */
5260 fnvlist_free(spa
->spa_load_info
);
5261 spa
->spa_load_info
= loadinfo
;
5263 spa_import_progress_remove(spa_guid(spa
));
5264 return (load_error
);
5271 * The import case is identical to an open except that the configuration is sent
5272 * down from userland, instead of grabbed from the configuration cache. For the
5273 * case of an open, the pool configuration will exist in the
5274 * POOL_STATE_UNINITIALIZED state.
5276 * The stats information (gen/count/ustats) is used to gather vdev statistics at
5277 * the same time open the pool, without having to keep around the spa_t in some
5281 spa_open_common(const char *pool
, spa_t
**spapp
, const void *tag
,
5282 nvlist_t
*nvpolicy
, nvlist_t
**config
)
5285 spa_load_state_t state
= SPA_LOAD_OPEN
;
5287 int locked
= B_FALSE
;
5288 int firstopen
= B_FALSE
;
5293 * As disgusting as this is, we need to support recursive calls to this
5294 * function because dsl_dir_open() is called during spa_load(), and ends
5295 * up calling spa_open() again. The real fix is to figure out how to
5296 * avoid dsl_dir_open() calling this in the first place.
5298 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
5299 mutex_enter(&spa_namespace_lock
);
5303 if ((spa
= spa_lookup(pool
)) == NULL
) {
5305 mutex_exit(&spa_namespace_lock
);
5306 return (SET_ERROR(ENOENT
));
5309 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
5310 zpool_load_policy_t policy
;
5314 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
5316 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5317 state
= SPA_LOAD_RECOVER
;
5319 spa_activate(spa
, spa_mode_global
);
5321 if (state
!= SPA_LOAD_RECOVER
)
5322 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5323 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5325 zfs_dbgmsg("spa_open_common: opening %s", pool
);
5326 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
5329 if (error
== EBADF
) {
5331 * If vdev_validate() returns failure (indicated by
5332 * EBADF), it indicates that one of the vdevs indicates
5333 * that the pool has been exported or destroyed. If
5334 * this is the case, the config cache is out of sync and
5335 * we should remove the pool from the namespace.
5338 spa_deactivate(spa
);
5339 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
5342 mutex_exit(&spa_namespace_lock
);
5343 return (SET_ERROR(ENOENT
));
5348 * We can't open the pool, but we still have useful
5349 * information: the state of each vdev after the
5350 * attempted vdev_open(). Return this to the user.
5352 if (config
!= NULL
&& spa
->spa_config
) {
5353 *config
= fnvlist_dup(spa
->spa_config
);
5354 fnvlist_add_nvlist(*config
,
5355 ZPOOL_CONFIG_LOAD_INFO
,
5356 spa
->spa_load_info
);
5359 spa_deactivate(spa
);
5360 spa
->spa_last_open_failed
= error
;
5362 mutex_exit(&spa_namespace_lock
);
5368 spa_open_ref(spa
, tag
);
5371 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5374 * If we've recovered the pool, pass back any information we
5375 * gathered while doing the load.
5377 if (state
== SPA_LOAD_RECOVER
&& config
!= NULL
) {
5378 fnvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
5379 spa
->spa_load_info
);
5383 spa
->spa_last_open_failed
= 0;
5384 spa
->spa_last_ubsync_txg
= 0;
5385 spa
->spa_load_txg
= 0;
5386 mutex_exit(&spa_namespace_lock
);
5390 zvol_create_minors_recursive(spa_name(spa
));
5398 spa_open_rewind(const char *name
, spa_t
**spapp
, const void *tag
,
5399 nvlist_t
*policy
, nvlist_t
**config
)
5401 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
5405 spa_open(const char *name
, spa_t
**spapp
, const void *tag
)
5407 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
5411 * Lookup the given spa_t, incrementing the inject count in the process,
5412 * preventing it from being exported or destroyed.
5415 spa_inject_addref(char *name
)
5419 mutex_enter(&spa_namespace_lock
);
5420 if ((spa
= spa_lookup(name
)) == NULL
) {
5421 mutex_exit(&spa_namespace_lock
);
5424 spa
->spa_inject_ref
++;
5425 mutex_exit(&spa_namespace_lock
);
5431 spa_inject_delref(spa_t
*spa
)
5433 mutex_enter(&spa_namespace_lock
);
5434 spa
->spa_inject_ref
--;
5435 mutex_exit(&spa_namespace_lock
);
5439 * Add spares device information to the nvlist.
5442 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
5452 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5454 if (spa
->spa_spares
.sav_count
== 0)
5457 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5458 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5459 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
5461 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5462 (const nvlist_t
* const *)spares
, nspares
);
5463 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5464 &spares
, &nspares
));
5467 * Go through and find any spares which have since been
5468 * repurposed as an active spare. If this is the case, update
5469 * their status appropriately.
5471 for (i
= 0; i
< nspares
; i
++) {
5472 guid
= fnvlist_lookup_uint64(spares
[i
],
5474 VERIFY0(nvlist_lookup_uint64_array(spares
[i
],
5475 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5476 if (spa_spare_exists(guid
, &pool
, NULL
) &&
5478 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
5479 vs
->vs_aux
= VDEV_AUX_SPARED
;
5482 spa
->spa_spares
.sav_vdevs
[i
]->vdev_state
;
5489 * Add l2cache device information to the nvlist, including vdev stats.
5492 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
5495 uint_t i
, j
, nl2cache
;
5502 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5504 if (spa
->spa_l2cache
.sav_count
== 0)
5507 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5508 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5509 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
5510 if (nl2cache
!= 0) {
5511 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5512 (const nvlist_t
* const *)l2cache
, nl2cache
);
5513 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5514 &l2cache
, &nl2cache
));
5517 * Update level 2 cache device stats.
5520 for (i
= 0; i
< nl2cache
; i
++) {
5521 guid
= fnvlist_lookup_uint64(l2cache
[i
],
5525 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
5527 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
5528 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
5534 VERIFY0(nvlist_lookup_uint64_array(l2cache
[i
],
5535 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5536 vdev_get_stats(vd
, vs
);
5537 vdev_config_generate_stats(vd
, l2cache
[i
]);
5544 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
5549 if (spa
->spa_feat_for_read_obj
!= 0) {
5550 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5551 spa
->spa_feat_for_read_obj
);
5552 zap_cursor_retrieve(&zc
, &za
) == 0;
5553 zap_cursor_advance(&zc
)) {
5554 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5555 za
.za_num_integers
== 1);
5556 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5557 za
.za_first_integer
));
5559 zap_cursor_fini(&zc
);
5562 if (spa
->spa_feat_for_write_obj
!= 0) {
5563 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5564 spa
->spa_feat_for_write_obj
);
5565 zap_cursor_retrieve(&zc
, &za
) == 0;
5566 zap_cursor_advance(&zc
)) {
5567 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5568 za
.za_num_integers
== 1);
5569 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5570 za
.za_first_integer
));
5572 zap_cursor_fini(&zc
);
5577 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
5581 for (i
= 0; i
< SPA_FEATURES
; i
++) {
5582 zfeature_info_t feature
= spa_feature_table
[i
];
5585 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
5588 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
5593 * Store a list of pool features and their reference counts in the
5596 * The first time this is called on a spa, allocate a new nvlist, fetch
5597 * the pool features and reference counts from disk, then save the list
5598 * in the spa. In subsequent calls on the same spa use the saved nvlist
5599 * and refresh its values from the cached reference counts. This
5600 * ensures we don't block here on I/O on a suspended pool so 'zpool
5601 * clear' can resume the pool.
5604 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
5608 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5610 mutex_enter(&spa
->spa_feat_stats_lock
);
5611 features
= spa
->spa_feat_stats
;
5613 if (features
!= NULL
) {
5614 spa_feature_stats_from_cache(spa
, features
);
5616 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
5617 spa
->spa_feat_stats
= features
;
5618 spa_feature_stats_from_disk(spa
, features
);
5621 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
5624 mutex_exit(&spa
->spa_feat_stats_lock
);
5628 spa_get_stats(const char *name
, nvlist_t
**config
,
5629 char *altroot
, size_t buflen
)
5635 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
5639 * This still leaves a window of inconsistency where the spares
5640 * or l2cache devices could change and the config would be
5641 * self-inconsistent.
5643 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5645 if (*config
!= NULL
) {
5646 uint64_t loadtimes
[2];
5648 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
5649 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
5650 fnvlist_add_uint64_array(*config
,
5651 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2);
5653 fnvlist_add_uint64(*config
,
5654 ZPOOL_CONFIG_ERRCOUNT
,
5655 spa_approx_errlog_size(spa
));
5657 if (spa_suspended(spa
)) {
5658 fnvlist_add_uint64(*config
,
5659 ZPOOL_CONFIG_SUSPENDED
,
5661 fnvlist_add_uint64(*config
,
5662 ZPOOL_CONFIG_SUSPENDED_REASON
,
5663 spa
->spa_suspended
);
5666 spa_add_spares(spa
, *config
);
5667 spa_add_l2cache(spa
, *config
);
5668 spa_add_feature_stats(spa
, *config
);
5673 * We want to get the alternate root even for faulted pools, so we cheat
5674 * and call spa_lookup() directly.
5678 mutex_enter(&spa_namespace_lock
);
5679 spa
= spa_lookup(name
);
5681 spa_altroot(spa
, altroot
, buflen
);
5685 mutex_exit(&spa_namespace_lock
);
5687 spa_altroot(spa
, altroot
, buflen
);
5692 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5693 spa_close(spa
, FTAG
);
5700 * Validate that the auxiliary device array is well formed. We must have an
5701 * array of nvlists, each which describes a valid leaf vdev. If this is an
5702 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
5703 * specified, as long as they are well-formed.
5706 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
5707 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
5708 vdev_labeltype_t label
)
5715 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5718 * It's acceptable to have no devs specified.
5720 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
5724 return (SET_ERROR(EINVAL
));
5727 * Make sure the pool is formatted with a version that supports this
5730 if (spa_version(spa
) < version
)
5731 return (SET_ERROR(ENOTSUP
));
5734 * Set the pending device list so we correctly handle device in-use
5737 sav
->sav_pending
= dev
;
5738 sav
->sav_npending
= ndev
;
5740 for (i
= 0; i
< ndev
; i
++) {
5741 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
5745 if (!vd
->vdev_ops
->vdev_op_leaf
) {
5747 error
= SET_ERROR(EINVAL
);
5753 if ((error
= vdev_open(vd
)) == 0 &&
5754 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
5755 fnvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
5762 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
5769 sav
->sav_pending
= NULL
;
5770 sav
->sav_npending
= 0;
5775 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
5779 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5781 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5782 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
5783 VDEV_LABEL_SPARE
)) != 0) {
5787 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
5788 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
5789 VDEV_LABEL_L2CACHE
));
5793 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
5798 if (sav
->sav_config
!= NULL
) {
5804 * Generate new dev list by concatenating with the
5807 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
5808 &olddevs
, &oldndevs
));
5810 newdevs
= kmem_alloc(sizeof (void *) *
5811 (ndevs
+ oldndevs
), KM_SLEEP
);
5812 for (i
= 0; i
< oldndevs
; i
++)
5813 newdevs
[i
] = fnvlist_dup(olddevs
[i
]);
5814 for (i
= 0; i
< ndevs
; i
++)
5815 newdevs
[i
+ oldndevs
] = fnvlist_dup(devs
[i
]);
5817 fnvlist_remove(sav
->sav_config
, config
);
5819 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
5820 (const nvlist_t
* const *)newdevs
, ndevs
+ oldndevs
);
5821 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
5822 nvlist_free(newdevs
[i
]);
5823 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
5826 * Generate a new dev list.
5828 sav
->sav_config
= fnvlist_alloc();
5829 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
5830 (const nvlist_t
* const *)devs
, ndevs
);
5835 * Stop and drop level 2 ARC devices
5838 spa_l2cache_drop(spa_t
*spa
)
5842 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
5844 for (i
= 0; i
< sav
->sav_count
; i
++) {
5847 vd
= sav
->sav_vdevs
[i
];
5850 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
5851 pool
!= 0ULL && l2arc_vdev_present(vd
))
5852 l2arc_remove_vdev(vd
);
5857 * Verify encryption parameters for spa creation. If we are encrypting, we must
5858 * have the encryption feature flag enabled.
5861 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
5862 boolean_t has_encryption
)
5864 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
5865 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
5867 return (SET_ERROR(ENOTSUP
));
5869 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
5876 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
5877 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
5880 const char *altroot
= NULL
;
5885 uint64_t txg
= TXG_INITIAL
;
5886 nvlist_t
**spares
, **l2cache
;
5887 uint_t nspares
, nl2cache
;
5888 uint64_t version
, obj
, ndraid
= 0;
5889 boolean_t has_features
;
5890 boolean_t has_encryption
;
5891 boolean_t has_allocclass
;
5893 const char *feat_name
;
5894 const char *poolname
;
5897 if (props
== NULL
||
5898 nvlist_lookup_string(props
, "tname", &poolname
) != 0)
5899 poolname
= (char *)pool
;
5902 * If this pool already exists, return failure.
5904 mutex_enter(&spa_namespace_lock
);
5905 if (spa_lookup(poolname
) != NULL
) {
5906 mutex_exit(&spa_namespace_lock
);
5907 return (SET_ERROR(EEXIST
));
5911 * Allocate a new spa_t structure.
5913 nvl
= fnvlist_alloc();
5914 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
5915 (void) nvlist_lookup_string(props
,
5916 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5917 spa
= spa_add(poolname
, nvl
, altroot
);
5919 spa_activate(spa
, spa_mode_global
);
5921 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
5922 spa_deactivate(spa
);
5924 mutex_exit(&spa_namespace_lock
);
5929 * Temporary pool names should never be written to disk.
5931 if (poolname
!= pool
)
5932 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
5934 has_features
= B_FALSE
;
5935 has_encryption
= B_FALSE
;
5936 has_allocclass
= B_FALSE
;
5937 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
5938 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
5939 if (zpool_prop_feature(nvpair_name(elem
))) {
5940 has_features
= B_TRUE
;
5942 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
5943 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
5944 if (feat
== SPA_FEATURE_ENCRYPTION
)
5945 has_encryption
= B_TRUE
;
5946 if (feat
== SPA_FEATURE_ALLOCATION_CLASSES
)
5947 has_allocclass
= B_TRUE
;
5951 /* verify encryption params, if they were provided */
5953 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
5955 spa_deactivate(spa
);
5957 mutex_exit(&spa_namespace_lock
);
5961 if (!has_allocclass
&& zfs_special_devs(nvroot
, NULL
)) {
5962 spa_deactivate(spa
);
5964 mutex_exit(&spa_namespace_lock
);
5968 if (has_features
|| nvlist_lookup_uint64(props
,
5969 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
5970 version
= SPA_VERSION
;
5972 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5974 spa
->spa_first_txg
= txg
;
5975 spa
->spa_uberblock
.ub_txg
= txg
- 1;
5976 spa
->spa_uberblock
.ub_version
= version
;
5977 spa
->spa_ubsync
= spa
->spa_uberblock
;
5978 spa
->spa_load_state
= SPA_LOAD_CREATE
;
5979 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
5980 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
5981 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
5982 spa
->spa_indirect_vdevs_loaded
= B_TRUE
;
5985 * Create "The Godfather" zio to hold all async IOs
5987 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
5989 for (int i
= 0; i
< max_ncpus
; i
++) {
5990 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
5991 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
5992 ZIO_FLAG_GODFATHER
);
5996 * Create the root vdev.
5998 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6000 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
6002 ASSERT(error
!= 0 || rvd
!= NULL
);
6003 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
6005 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
6006 error
= SET_ERROR(EINVAL
);
6009 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
6010 (error
= vdev_draid_spare_create(nvroot
, rvd
, &ndraid
, 0)) == 0 &&
6011 (error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) == 0) {
6013 * instantiate the metaslab groups (this will dirty the vdevs)
6014 * we can no longer error exit past this point
6016 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
6017 vdev_t
*vd
= rvd
->vdev_child
[c
];
6019 vdev_metaslab_set_size(vd
);
6020 vdev_expand(vd
, txg
);
6024 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6028 spa_deactivate(spa
);
6030 mutex_exit(&spa_namespace_lock
);
6035 * Get the list of spares, if specified.
6037 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6038 &spares
, &nspares
) == 0) {
6039 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6040 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6041 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6043 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6044 spa_load_spares(spa
);
6045 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6046 spa
->spa_spares
.sav_sync
= B_TRUE
;
6050 * Get the list of level 2 cache devices, if specified.
6052 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6053 &l2cache
, &nl2cache
) == 0) {
6054 VERIFY0(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
6055 NV_UNIQUE_NAME
, KM_SLEEP
));
6056 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6057 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6059 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6060 spa_load_l2cache(spa
);
6061 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6062 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6065 spa
->spa_is_initializing
= B_TRUE
;
6066 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
6067 spa
->spa_is_initializing
= B_FALSE
;
6070 * Create DDTs (dedup tables).
6074 * Create BRT table and BRT table object.
6078 spa_update_dspace(spa
);
6080 tx
= dmu_tx_create_assigned(dp
, txg
);
6083 * Create the pool's history object.
6085 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
6086 spa_history_create_obj(spa
, tx
);
6088 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
6089 spa_history_log_version(spa
, "create", tx
);
6092 * Create the pool config object.
6094 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6095 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
6096 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
6098 if (zap_add(spa
->spa_meta_objset
,
6099 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
6100 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
6101 cmn_err(CE_PANIC
, "failed to add pool config");
6104 if (zap_add(spa
->spa_meta_objset
,
6105 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
6106 sizeof (uint64_t), 1, &version
, tx
) != 0) {
6107 cmn_err(CE_PANIC
, "failed to add pool version");
6110 /* Newly created pools with the right version are always deflated. */
6111 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6112 spa
->spa_deflate
= TRUE
;
6113 if (zap_add(spa
->spa_meta_objset
,
6114 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6115 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
6116 cmn_err(CE_PANIC
, "failed to add deflate");
6121 * Create the deferred-free bpobj. Turn off compression
6122 * because sync-to-convergence takes longer if the blocksize
6125 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
6126 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
6127 ZIO_COMPRESS_OFF
, tx
);
6128 if (zap_add(spa
->spa_meta_objset
,
6129 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
6130 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
6131 cmn_err(CE_PANIC
, "failed to add bpobj");
6133 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
6134 spa
->spa_meta_objset
, obj
));
6137 * Generate some random noise for salted checksums to operate on.
6139 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
6140 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
6143 * Set pool properties.
6145 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
6146 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
6147 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
6148 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
6149 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
6150 spa
->spa_autotrim
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM
);
6152 if (props
!= NULL
) {
6153 spa_configfile_set(spa
, props
, B_FALSE
);
6154 spa_sync_props(props
, tx
);
6157 for (int i
= 0; i
< ndraid
; i
++)
6158 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6162 spa
->spa_sync_on
= B_TRUE
;
6164 mmp_thread_start(spa
);
6165 txg_wait_synced(dp
, txg
);
6167 spa_spawn_aux_threads(spa
);
6169 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
6172 * Don't count references from objsets that are already closed
6173 * and are making their way through the eviction process.
6175 spa_evicting_os_wait(spa
);
6176 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
6177 spa
->spa_load_state
= SPA_LOAD_NONE
;
6181 mutex_exit(&spa_namespace_lock
);
6187 * Import a non-root pool into the system.
6190 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
6193 const char *altroot
= NULL
;
6194 spa_load_state_t state
= SPA_LOAD_IMPORT
;
6195 zpool_load_policy_t policy
;
6196 spa_mode_t mode
= spa_mode_global
;
6197 uint64_t readonly
= B_FALSE
;
6200 nvlist_t
**spares
, **l2cache
;
6201 uint_t nspares
, nl2cache
;
6204 * If a pool with this name exists, return failure.
6206 mutex_enter(&spa_namespace_lock
);
6207 if (spa_lookup(pool
) != NULL
) {
6208 mutex_exit(&spa_namespace_lock
);
6209 return (SET_ERROR(EEXIST
));
6213 * Create and initialize the spa structure.
6215 (void) nvlist_lookup_string(props
,
6216 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6217 (void) nvlist_lookup_uint64(props
,
6218 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
6220 mode
= SPA_MODE_READ
;
6221 spa
= spa_add(pool
, config
, altroot
);
6222 spa
->spa_import_flags
= flags
;
6225 * Verbatim import - Take a pool and insert it into the namespace
6226 * as if it had been loaded at boot.
6228 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
6230 spa_configfile_set(spa
, props
, B_FALSE
);
6232 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_FALSE
);
6233 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6234 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
6235 mutex_exit(&spa_namespace_lock
);
6239 spa_activate(spa
, mode
);
6242 * Don't start async tasks until we know everything is healthy.
6244 spa_async_suspend(spa
);
6246 zpool_get_load_policy(config
, &policy
);
6247 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
6248 state
= SPA_LOAD_RECOVER
;
6250 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
6252 if (state
!= SPA_LOAD_RECOVER
) {
6253 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
6254 zfs_dbgmsg("spa_import: importing %s", pool
);
6256 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
6257 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
6259 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
6262 * Propagate anything learned while loading the pool and pass it
6263 * back to caller (i.e. rewind info, missing devices, etc).
6265 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, spa
->spa_load_info
);
6267 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6269 * Toss any existing sparelist, as it doesn't have any validity
6270 * anymore, and conflicts with spa_has_spare().
6272 if (spa
->spa_spares
.sav_config
) {
6273 nvlist_free(spa
->spa_spares
.sav_config
);
6274 spa
->spa_spares
.sav_config
= NULL
;
6275 spa_load_spares(spa
);
6277 if (spa
->spa_l2cache
.sav_config
) {
6278 nvlist_free(spa
->spa_l2cache
.sav_config
);
6279 spa
->spa_l2cache
.sav_config
= NULL
;
6280 spa_load_l2cache(spa
);
6283 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6284 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6287 spa_configfile_set(spa
, props
, B_FALSE
);
6289 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
6290 (error
= spa_prop_set(spa
, props
)))) {
6292 spa_deactivate(spa
);
6294 mutex_exit(&spa_namespace_lock
);
6298 spa_async_resume(spa
);
6301 * Override any spares and level 2 cache devices as specified by
6302 * the user, as these may have correct device names/devids, etc.
6304 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6305 &spares
, &nspares
) == 0) {
6306 if (spa
->spa_spares
.sav_config
)
6307 fnvlist_remove(spa
->spa_spares
.sav_config
,
6308 ZPOOL_CONFIG_SPARES
);
6310 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6311 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6312 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6314 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6315 spa_load_spares(spa
);
6316 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6317 spa
->spa_spares
.sav_sync
= B_TRUE
;
6319 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6320 &l2cache
, &nl2cache
) == 0) {
6321 if (spa
->spa_l2cache
.sav_config
)
6322 fnvlist_remove(spa
->spa_l2cache
.sav_config
,
6323 ZPOOL_CONFIG_L2CACHE
);
6325 spa
->spa_l2cache
.sav_config
= fnvlist_alloc();
6326 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6327 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6329 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6330 spa_load_l2cache(spa
);
6331 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6332 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6336 * Check for any removed devices.
6338 if (spa
->spa_autoreplace
) {
6339 spa_aux_check_removed(&spa
->spa_spares
);
6340 spa_aux_check_removed(&spa
->spa_l2cache
);
6343 if (spa_writeable(spa
)) {
6345 * Update the config cache to include the newly-imported pool.
6347 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6351 * It's possible that the pool was expanded while it was exported.
6352 * We kick off an async task to handle this for us.
6354 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
6356 spa_history_log_version(spa
, "import", NULL
);
6358 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6360 mutex_exit(&spa_namespace_lock
);
6362 zvol_create_minors_recursive(pool
);
6370 spa_tryimport(nvlist_t
*tryconfig
)
6372 nvlist_t
*config
= NULL
;
6373 const char *poolname
, *cachefile
;
6377 zpool_load_policy_t policy
;
6379 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
6382 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
6386 * Create and initialize the spa structure.
6388 mutex_enter(&spa_namespace_lock
);
6389 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
6390 spa_activate(spa
, SPA_MODE_READ
);
6393 * Rewind pool if a max txg was provided.
6395 zpool_get_load_policy(spa
->spa_config
, &policy
);
6396 if (policy
.zlp_txg
!= UINT64_MAX
) {
6397 spa
->spa_load_max_txg
= policy
.zlp_txg
;
6398 spa
->spa_extreme_rewind
= B_TRUE
;
6399 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
6400 poolname
, (longlong_t
)policy
.zlp_txg
);
6402 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
6405 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
6407 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
6408 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
6410 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
6414 * spa_import() relies on a pool config fetched by spa_try_import()
6415 * for spare/cache devices. Import flags are not passed to
6416 * spa_tryimport(), which makes it return early due to a missing log
6417 * device and missing retrieving the cache device and spare eventually.
6418 * Passing ZFS_IMPORT_MISSING_LOG to spa_tryimport() makes it fetch
6419 * the correct configuration regardless of the missing log device.
6421 spa
->spa_import_flags
|= ZFS_IMPORT_MISSING_LOG
;
6423 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
6426 * If 'tryconfig' was at least parsable, return the current config.
6428 if (spa
->spa_root_vdev
!= NULL
) {
6429 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
6430 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, poolname
);
6431 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, state
);
6432 fnvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
6433 spa
->spa_uberblock
.ub_timestamp
);
6434 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
6435 spa
->spa_load_info
);
6436 fnvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
6440 * If the bootfs property exists on this pool then we
6441 * copy it out so that external consumers can tell which
6442 * pools are bootable.
6444 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
6445 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6448 * We have to play games with the name since the
6449 * pool was opened as TRYIMPORT_NAME.
6451 if (dsl_dsobj_to_dsname(spa_name(spa
),
6452 spa
->spa_bootfs
, tmpname
) == 0) {
6456 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6458 cp
= strchr(tmpname
, '/');
6460 (void) strlcpy(dsname
, tmpname
,
6463 (void) snprintf(dsname
, MAXPATHLEN
,
6464 "%s/%s", poolname
, ++cp
);
6466 fnvlist_add_string(config
, ZPOOL_CONFIG_BOOTFS
,
6468 kmem_free(dsname
, MAXPATHLEN
);
6470 kmem_free(tmpname
, MAXPATHLEN
);
6474 * Add the list of hot spares and level 2 cache devices.
6476 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6477 spa_add_spares(spa
, config
);
6478 spa_add_l2cache(spa
, config
);
6479 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6483 spa_deactivate(spa
);
6485 mutex_exit(&spa_namespace_lock
);
6491 * Pool export/destroy
6493 * The act of destroying or exporting a pool is very simple. We make sure there
6494 * is no more pending I/O and any references to the pool are gone. Then, we
6495 * update the pool state and sync all the labels to disk, removing the
6496 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
6497 * we don't sync the labels or remove the configuration cache.
6500 spa_export_common(const char *pool
, int new_state
, nvlist_t
**oldconfig
,
6501 boolean_t force
, boolean_t hardforce
)
6509 if (!(spa_mode_global
& SPA_MODE_WRITE
))
6510 return (SET_ERROR(EROFS
));
6512 mutex_enter(&spa_namespace_lock
);
6513 if ((spa
= spa_lookup(pool
)) == NULL
) {
6514 mutex_exit(&spa_namespace_lock
);
6515 return (SET_ERROR(ENOENT
));
6518 if (spa
->spa_is_exporting
) {
6519 /* the pool is being exported by another thread */
6520 mutex_exit(&spa_namespace_lock
);
6521 return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS
));
6523 spa
->spa_is_exporting
= B_TRUE
;
6526 * Put a hold on the pool, drop the namespace lock, stop async tasks,
6527 * reacquire the namespace lock, and see if we can export.
6529 spa_open_ref(spa
, FTAG
);
6530 mutex_exit(&spa_namespace_lock
);
6531 spa_async_suspend(spa
);
6532 if (spa
->spa_zvol_taskq
) {
6533 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
6534 taskq_wait(spa
->spa_zvol_taskq
);
6536 mutex_enter(&spa_namespace_lock
);
6537 spa_close(spa
, FTAG
);
6539 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
6542 * The pool will be in core if it's openable, in which case we can
6543 * modify its state. Objsets may be open only because they're dirty,
6544 * so we have to force it to sync before checking spa_refcnt.
6546 if (spa
->spa_sync_on
) {
6547 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6548 spa_evicting_os_wait(spa
);
6552 * A pool cannot be exported or destroyed if there are active
6553 * references. If we are resetting a pool, allow references by
6554 * fault injection handlers.
6556 if (!spa_refcount_zero(spa
) || (spa
->spa_inject_ref
!= 0)) {
6557 error
= SET_ERROR(EBUSY
);
6561 if (spa
->spa_sync_on
) {
6562 vdev_t
*rvd
= spa
->spa_root_vdev
;
6564 * A pool cannot be exported if it has an active shared spare.
6565 * This is to prevent other pools stealing the active spare
6566 * from an exported pool. At user's own will, such pool can
6567 * be forcedly exported.
6569 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
6570 spa_has_active_shared_spare(spa
)) {
6571 error
= SET_ERROR(EXDEV
);
6576 * We're about to export or destroy this pool. Make sure
6577 * we stop all initialization and trim activity here before
6578 * we set the spa_final_txg. This will ensure that all
6579 * dirty data resulting from the initialization is
6580 * committed to disk before we unload the pool.
6582 vdev_initialize_stop_all(rvd
, VDEV_INITIALIZE_ACTIVE
);
6583 vdev_trim_stop_all(rvd
, VDEV_TRIM_ACTIVE
);
6584 vdev_autotrim_stop_all(spa
);
6585 vdev_rebuild_stop_all(spa
);
6588 * We want this to be reflected on every label,
6589 * so mark them all dirty. spa_unload() will do the
6590 * final sync that pushes these changes out.
6592 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6593 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6594 spa
->spa_state
= new_state
;
6595 vdev_config_dirty(rvd
);
6596 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6600 * If the log space map feature is enabled and the pool is
6601 * getting exported (but not destroyed), we want to spend some
6602 * time flushing as many metaslabs as we can in an attempt to
6603 * destroy log space maps and save import time. This has to be
6604 * done before we set the spa_final_txg, otherwise
6605 * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs.
6606 * spa_should_flush_logs_on_unload() should be called after
6607 * spa_state has been set to the new_state.
6609 if (spa_should_flush_logs_on_unload(spa
))
6610 spa_unload_log_sm_flush_all(spa
);
6612 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
6613 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6614 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
6616 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6623 if (new_state
== POOL_STATE_DESTROYED
)
6624 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
6625 else if (new_state
== POOL_STATE_EXPORTED
)
6626 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
6628 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6630 spa_deactivate(spa
);
6633 if (oldconfig
&& spa
->spa_config
)
6634 *oldconfig
= fnvlist_dup(spa
->spa_config
);
6636 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
6638 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
6642 * If spa_remove() is not called for this spa_t and
6643 * there is any possibility that it can be reused,
6644 * we make sure to reset the exporting flag.
6646 spa
->spa_is_exporting
= B_FALSE
;
6649 mutex_exit(&spa_namespace_lock
);
6653 spa
->spa_is_exporting
= B_FALSE
;
6654 spa_async_resume(spa
);
6655 mutex_exit(&spa_namespace_lock
);
6660 * Destroy a storage pool.
6663 spa_destroy(const char *pool
)
6665 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
6670 * Export a storage pool.
6673 spa_export(const char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
6674 boolean_t hardforce
)
6676 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
6681 * Similar to spa_export(), this unloads the spa_t without actually removing it
6682 * from the namespace in any way.
6685 spa_reset(const char *pool
)
6687 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
6692 * ==========================================================================
6693 * Device manipulation
6694 * ==========================================================================
6698 * This is called as a synctask to increment the draid feature flag
6701 spa_draid_feature_incr(void *arg
, dmu_tx_t
*tx
)
6703 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6704 int draid
= (int)(uintptr_t)arg
;
6706 for (int c
= 0; c
< draid
; c
++)
6707 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6711 * Add a device to a storage pool.
6714 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
6716 uint64_t txg
, ndraid
= 0;
6718 vdev_t
*rvd
= spa
->spa_root_vdev
;
6720 nvlist_t
**spares
, **l2cache
;
6721 uint_t nspares
, nl2cache
;
6723 ASSERT(spa_writeable(spa
));
6725 txg
= spa_vdev_enter(spa
);
6727 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
6728 VDEV_ALLOC_ADD
)) != 0)
6729 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6731 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
6733 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
6737 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
6741 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
6742 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6744 if (vd
->vdev_children
!= 0 &&
6745 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0) {
6746 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6750 * The virtual dRAID spares must be added after vdev tree is created
6751 * and the vdev guids are generated. The guid of their associated
6752 * dRAID is stored in the config and used when opening the spare.
6754 if ((error
= vdev_draid_spare_create(nvroot
, vd
, &ndraid
,
6755 rvd
->vdev_children
)) == 0) {
6756 if (ndraid
> 0 && nvlist_lookup_nvlist_array(nvroot
,
6757 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) != 0)
6760 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6764 * We must validate the spares and l2cache devices after checking the
6765 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
6767 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
6768 return (spa_vdev_exit(spa
, vd
, txg
, error
));
6771 * If we are in the middle of a device removal, we can only add
6772 * devices which match the existing devices in the pool.
6773 * If we are in the middle of a removal, or have some indirect
6774 * vdevs, we can not add raidz or dRAID top levels.
6776 if (spa
->spa_vdev_removal
!= NULL
||
6777 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
6778 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6779 tvd
= vd
->vdev_child
[c
];
6780 if (spa
->spa_vdev_removal
!= NULL
&&
6781 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
6782 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6784 /* Fail if top level vdev is raidz or a dRAID */
6785 if (vdev_get_nparity(tvd
) != 0)
6786 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
6789 * Need the top level mirror to be
6790 * a mirror of leaf vdevs only
6792 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
6793 for (uint64_t cid
= 0;
6794 cid
< tvd
->vdev_children
; cid
++) {
6795 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
6796 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
6797 return (spa_vdev_exit(spa
, vd
,
6805 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
6806 tvd
= vd
->vdev_child
[c
];
6807 vdev_remove_child(vd
, tvd
);
6808 tvd
->vdev_id
= rvd
->vdev_children
;
6809 vdev_add_child(rvd
, tvd
);
6810 vdev_config_dirty(tvd
);
6814 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
6815 ZPOOL_CONFIG_SPARES
);
6816 spa_load_spares(spa
);
6817 spa
->spa_spares
.sav_sync
= B_TRUE
;
6820 if (nl2cache
!= 0) {
6821 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
6822 ZPOOL_CONFIG_L2CACHE
);
6823 spa_load_l2cache(spa
);
6824 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6828 * We can't increment a feature while holding spa_vdev so we
6829 * have to do it in a synctask.
6834 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
6835 dsl_sync_task_nowait(spa
->spa_dsl_pool
, spa_draid_feature_incr
,
6836 (void *)(uintptr_t)ndraid
, tx
);
6841 * We have to be careful when adding new vdevs to an existing pool.
6842 * If other threads start allocating from these vdevs before we
6843 * sync the config cache, and we lose power, then upon reboot we may
6844 * fail to open the pool because there are DVAs that the config cache
6845 * can't translate. Therefore, we first add the vdevs without
6846 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
6847 * and then let spa_config_update() initialize the new metaslabs.
6849 * spa_load() checks for added-but-not-initialized vdevs, so that
6850 * if we lose power at any point in this sequence, the remaining
6851 * steps will be completed the next time we load the pool.
6853 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
6855 mutex_enter(&spa_namespace_lock
);
6856 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6857 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
6858 mutex_exit(&spa_namespace_lock
);
6864 * Attach a device to a mirror. The arguments are the path to any device
6865 * in the mirror, and the nvroot for the new device. If the path specifies
6866 * a device that is not mirrored, we automatically insert the mirror vdev.
6868 * If 'replacing' is specified, the new device is intended to replace the
6869 * existing device; in this case the two devices are made into their own
6870 * mirror using the 'replacing' vdev, which is functionally identical to
6871 * the mirror vdev (it actually reuses all the same ops) but has a few
6872 * extra rules: you can't attach to it after it's been created, and upon
6873 * completion of resilvering, the first disk (the one being replaced)
6874 * is automatically detached.
6876 * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
6877 * should be performed instead of traditional healing reconstruction. From
6878 * an administrators perspective these are both resilver operations.
6881 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
,
6884 uint64_t txg
, dtl_max_txg
;
6885 vdev_t
*rvd
= spa
->spa_root_vdev
;
6886 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
6888 char *oldvdpath
, *newvdpath
;
6892 ASSERT(spa_writeable(spa
));
6894 txg
= spa_vdev_enter(spa
);
6896 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
6898 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
6899 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
6900 error
= (spa_has_checkpoint(spa
)) ?
6901 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
6902 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
6906 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_DEVICE_REBUILD
))
6907 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6909 if (dsl_scan_resilvering(spa_get_dsl(spa
)) ||
6910 dsl_scan_resilver_scheduled(spa_get_dsl(spa
))) {
6911 return (spa_vdev_exit(spa
, NULL
, txg
,
6912 ZFS_ERR_RESILVER_IN_PROGRESS
));
6915 if (vdev_rebuild_active(rvd
))
6916 return (spa_vdev_exit(spa
, NULL
, txg
,
6917 ZFS_ERR_REBUILD_IN_PROGRESS
));
6920 if (spa
->spa_vdev_removal
!= NULL
)
6921 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
6924 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
6926 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
6927 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
6929 pvd
= oldvd
->vdev_parent
;
6931 if (spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
6932 VDEV_ALLOC_ATTACH
) != 0)
6933 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
6935 if (newrootvd
->vdev_children
!= 1)
6936 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6938 newvd
= newrootvd
->vdev_child
[0];
6940 if (!newvd
->vdev_ops
->vdev_op_leaf
)
6941 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
6943 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
6944 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
6947 * log, dedup and special vdevs should not be replaced by spares.
6949 if ((oldvd
->vdev_top
->vdev_alloc_bias
!= VDEV_BIAS_NONE
||
6950 oldvd
->vdev_top
->vdev_islog
) && newvd
->vdev_isspare
) {
6951 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6955 * A dRAID spare can only replace a child of its parent dRAID vdev.
6957 if (newvd
->vdev_ops
== &vdev_draid_spare_ops
&&
6958 oldvd
->vdev_top
!= vdev_draid_spare_get_parent(newvd
)) {
6959 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6964 * For rebuilds, the top vdev must support reconstruction
6965 * using only space maps. This means the only allowable
6966 * vdevs types are the root vdev, a mirror, or dRAID.
6969 if (pvd
->vdev_top
!= NULL
)
6970 tvd
= pvd
->vdev_top
;
6972 if (tvd
->vdev_ops
!= &vdev_mirror_ops
&&
6973 tvd
->vdev_ops
!= &vdev_root_ops
&&
6974 tvd
->vdev_ops
!= &vdev_draid_ops
) {
6975 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6981 * For attach, the only allowable parent is a mirror or the root
6984 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
6985 pvd
->vdev_ops
!= &vdev_root_ops
)
6986 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
6988 pvops
= &vdev_mirror_ops
;
6991 * Active hot spares can only be replaced by inactive hot
6994 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
6995 oldvd
->vdev_isspare
&&
6996 !spa_has_spare(spa
, newvd
->vdev_guid
))
6997 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7000 * If the source is a hot spare, and the parent isn't already a
7001 * spare, then we want to create a new hot spare. Otherwise, we
7002 * want to create a replacing vdev. The user is not allowed to
7003 * attach to a spared vdev child unless the 'isspare' state is
7004 * the same (spare replaces spare, non-spare replaces
7007 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
7008 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
7009 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7010 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
7011 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
7012 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7015 if (newvd
->vdev_isspare
)
7016 pvops
= &vdev_spare_ops
;
7018 pvops
= &vdev_replacing_ops
;
7022 * Make sure the new device is big enough.
7024 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
7025 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
7028 * The new device cannot have a higher alignment requirement
7029 * than the top-level vdev.
7031 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
7032 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7035 * If this is an in-place replacement, update oldvd's path and devid
7036 * to make it distinguishable from newvd, and unopenable from now on.
7038 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
7039 spa_strfree(oldvd
->vdev_path
);
7040 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
7042 (void) snprintf(oldvd
->vdev_path
, strlen(newvd
->vdev_path
) + 5,
7043 "%s/%s", newvd
->vdev_path
, "old");
7044 if (oldvd
->vdev_devid
!= NULL
) {
7045 spa_strfree(oldvd
->vdev_devid
);
7046 oldvd
->vdev_devid
= NULL
;
7051 * If the parent is not a mirror, or if we're replacing, insert the new
7052 * mirror/replacing/spare vdev above oldvd.
7054 if (pvd
->vdev_ops
!= pvops
)
7055 pvd
= vdev_add_parent(oldvd
, pvops
);
7057 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
7058 ASSERT(pvd
->vdev_ops
== pvops
);
7059 ASSERT(oldvd
->vdev_parent
== pvd
);
7062 * Extract the new device from its root and add it to pvd.
7064 vdev_remove_child(newrootvd
, newvd
);
7065 newvd
->vdev_id
= pvd
->vdev_children
;
7066 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
7067 vdev_add_child(pvd
, newvd
);
7070 * Reevaluate the parent vdev state.
7072 vdev_propagate_state(pvd
);
7074 tvd
= newvd
->vdev_top
;
7075 ASSERT(pvd
->vdev_top
== tvd
);
7076 ASSERT(tvd
->vdev_parent
== rvd
);
7078 vdev_config_dirty(tvd
);
7081 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
7082 * for any dmu_sync-ed blocks. It will propagate upward when
7083 * spa_vdev_exit() calls vdev_dtl_reassess().
7085 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
7087 vdev_dtl_dirty(newvd
, DTL_MISSING
,
7088 TXG_INITIAL
, dtl_max_txg
- TXG_INITIAL
);
7090 if (newvd
->vdev_isspare
) {
7091 spa_spare_activate(newvd
);
7092 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
7095 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
7096 newvdpath
= spa_strdup(newvd
->vdev_path
);
7097 newvd_isspare
= newvd
->vdev_isspare
;
7100 * Mark newvd's DTL dirty in this txg.
7102 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
7105 * Schedule the resilver or rebuild to restart in the future. We do
7106 * this to ensure that dmu_sync-ed blocks have been stitched into the
7107 * respective datasets.
7110 newvd
->vdev_rebuild_txg
= txg
;
7114 newvd
->vdev_resilver_txg
= txg
;
7116 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
7117 spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
)) {
7118 vdev_defer_resilver(newvd
);
7120 dsl_scan_restart_resilver(spa
->spa_dsl_pool
,
7125 if (spa
->spa_bootfs
)
7126 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
7128 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
7133 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
7135 spa_history_log_internal(spa
, "vdev attach", NULL
,
7136 "%s vdev=%s %s vdev=%s",
7137 replacing
&& newvd_isspare
? "spare in" :
7138 replacing
? "replace" : "attach", newvdpath
,
7139 replacing
? "for" : "to", oldvdpath
);
7141 spa_strfree(oldvdpath
);
7142 spa_strfree(newvdpath
);
7148 * Detach a device from a mirror or replacing vdev.
7150 * If 'replace_done' is specified, only detach if the parent
7151 * is a replacing or a spare vdev.
7154 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
7158 vdev_t
*rvd __maybe_unused
= spa
->spa_root_vdev
;
7159 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
7160 boolean_t unspare
= B_FALSE
;
7161 uint64_t unspare_guid
= 0;
7164 ASSERT(spa_writeable(spa
));
7166 txg
= spa_vdev_detach_enter(spa
, guid
);
7168 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7171 * Besides being called directly from the userland through the
7172 * ioctl interface, spa_vdev_detach() can be potentially called
7173 * at the end of spa_vdev_resilver_done().
7175 * In the regular case, when we have a checkpoint this shouldn't
7176 * happen as we never empty the DTLs of a vdev during the scrub
7177 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
7178 * should never get here when we have a checkpoint.
7180 * That said, even in a case when we checkpoint the pool exactly
7181 * as spa_vdev_resilver_done() calls this function everything
7182 * should be fine as the resilver will return right away.
7184 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7185 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7186 error
= (spa_has_checkpoint(spa
)) ?
7187 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7188 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7192 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7194 if (!vd
->vdev_ops
->vdev_op_leaf
)
7195 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7197 pvd
= vd
->vdev_parent
;
7200 * If the parent/child relationship is not as expected, don't do it.
7201 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
7202 * vdev that's replacing B with C. The user's intent in replacing
7203 * is to go from M(A,B) to M(A,C). If the user decides to cancel
7204 * the replace by detaching C, the expected behavior is to end up
7205 * M(A,B). But suppose that right after deciding to detach C,
7206 * the replacement of B completes. We would have M(A,C), and then
7207 * ask to detach C, which would leave us with just A -- not what
7208 * the user wanted. To prevent this, we make sure that the
7209 * parent/child relationship hasn't changed -- in this example,
7210 * that C's parent is still the replacing vdev R.
7212 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
7213 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7216 * Only 'replacing' or 'spare' vdevs can be replaced.
7218 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7219 pvd
->vdev_ops
!= &vdev_spare_ops
)
7220 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7222 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
7223 spa_version(spa
) >= SPA_VERSION_SPARES
);
7226 * Only mirror, replacing, and spare vdevs support detach.
7228 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7229 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7230 pvd
->vdev_ops
!= &vdev_spare_ops
)
7231 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7234 * If this device has the only valid copy of some data,
7235 * we cannot safely detach it.
7237 if (vdev_dtl_required(vd
))
7238 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7240 ASSERT(pvd
->vdev_children
>= 2);
7243 * If we are detaching the second disk from a replacing vdev, then
7244 * check to see if we changed the original vdev's path to have "/old"
7245 * at the end in spa_vdev_attach(). If so, undo that change now.
7247 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
7248 vd
->vdev_path
!= NULL
) {
7249 size_t len
= strlen(vd
->vdev_path
);
7251 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
7252 cvd
= pvd
->vdev_child
[c
];
7254 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
7257 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
7258 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
7259 spa_strfree(cvd
->vdev_path
);
7260 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
7267 * If we are detaching the original disk from a normal spare, then it
7268 * implies that the spare should become a real disk, and be removed
7269 * from the active spare list for the pool. dRAID spares on the
7270 * other hand are coupled to the pool and thus should never be removed
7271 * from the spares list.
7273 if (pvd
->vdev_ops
== &vdev_spare_ops
&& vd
->vdev_id
== 0) {
7274 vdev_t
*last_cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7276 if (last_cvd
->vdev_isspare
&&
7277 last_cvd
->vdev_ops
!= &vdev_draid_spare_ops
) {
7283 * Erase the disk labels so the disk can be used for other things.
7284 * This must be done after all other error cases are handled,
7285 * but before we disembowel vd (so we can still do I/O to it).
7286 * But if we can't do it, don't treat the error as fatal --
7287 * it may be that the unwritability of the disk is the reason
7288 * it's being detached!
7290 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
7293 * Remove vd from its parent and compact the parent's children.
7295 vdev_remove_child(pvd
, vd
);
7296 vdev_compact_children(pvd
);
7299 * Remember one of the remaining children so we can get tvd below.
7301 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7304 * If we need to remove the remaining child from the list of hot spares,
7305 * do it now, marking the vdev as no longer a spare in the process.
7306 * We must do this before vdev_remove_parent(), because that can
7307 * change the GUID if it creates a new toplevel GUID. For a similar
7308 * reason, we must remove the spare now, in the same txg as the detach;
7309 * otherwise someone could attach a new sibling, change the GUID, and
7310 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
7313 ASSERT(cvd
->vdev_isspare
);
7314 spa_spare_remove(cvd
);
7315 unspare_guid
= cvd
->vdev_guid
;
7316 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
7317 cvd
->vdev_unspare
= B_TRUE
;
7321 * If the parent mirror/replacing vdev only has one child,
7322 * the parent is no longer needed. Remove it from the tree.
7324 if (pvd
->vdev_children
== 1) {
7325 if (pvd
->vdev_ops
== &vdev_spare_ops
)
7326 cvd
->vdev_unspare
= B_FALSE
;
7327 vdev_remove_parent(cvd
);
7331 * We don't set tvd until now because the parent we just removed
7332 * may have been the previous top-level vdev.
7334 tvd
= cvd
->vdev_top
;
7335 ASSERT(tvd
->vdev_parent
== rvd
);
7338 * Reevaluate the parent vdev state.
7340 vdev_propagate_state(cvd
);
7343 * If the 'autoexpand' property is set on the pool then automatically
7344 * try to expand the size of the pool. For example if the device we
7345 * just detached was smaller than the others, it may be possible to
7346 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
7347 * first so that we can obtain the updated sizes of the leaf vdevs.
7349 if (spa
->spa_autoexpand
) {
7351 vdev_expand(tvd
, txg
);
7354 vdev_config_dirty(tvd
);
7357 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
7358 * vd->vdev_detached is set and free vd's DTL object in syncing context.
7359 * But first make sure we're not on any *other* txg's DTL list, to
7360 * prevent vd from being accessed after it's freed.
7362 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
7363 for (int t
= 0; t
< TXG_SIZE
; t
++)
7364 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
7365 vd
->vdev_detached
= B_TRUE
;
7366 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
7368 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
7369 spa_notify_waiters(spa
);
7371 /* hang on to the spa before we release the lock */
7372 spa_open_ref(spa
, FTAG
);
7374 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
7376 spa_history_log_internal(spa
, "detach", NULL
,
7378 spa_strfree(vdpath
);
7381 * If this was the removal of the original device in a hot spare vdev,
7382 * then we want to go through and remove the device from the hot spare
7383 * list of every other pool.
7386 spa_t
*altspa
= NULL
;
7388 mutex_enter(&spa_namespace_lock
);
7389 while ((altspa
= spa_next(altspa
)) != NULL
) {
7390 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
7394 spa_open_ref(altspa
, FTAG
);
7395 mutex_exit(&spa_namespace_lock
);
7396 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
7397 mutex_enter(&spa_namespace_lock
);
7398 spa_close(altspa
, FTAG
);
7400 mutex_exit(&spa_namespace_lock
);
7402 /* search the rest of the vdevs for spares to remove */
7403 spa_vdev_resilver_done(spa
);
7406 /* all done with the spa; OK to release */
7407 mutex_enter(&spa_namespace_lock
);
7408 spa_close(spa
, FTAG
);
7409 mutex_exit(&spa_namespace_lock
);
7415 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7418 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7420 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7422 /* Look up vdev and ensure it's a leaf. */
7423 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7424 if (vd
== NULL
|| vd
->vdev_detached
) {
7425 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7426 return (SET_ERROR(ENODEV
));
7427 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7428 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7429 return (SET_ERROR(EINVAL
));
7430 } else if (!vdev_writeable(vd
)) {
7431 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7432 return (SET_ERROR(EROFS
));
7434 mutex_enter(&vd
->vdev_initialize_lock
);
7435 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7438 * When we activate an initialize action we check to see
7439 * if the vdev_initialize_thread is NULL. We do this instead
7440 * of using the vdev_initialize_state since there might be
7441 * a previous initialization process which has completed but
7442 * the thread is not exited.
7444 if (cmd_type
== POOL_INITIALIZE_START
&&
7445 (vd
->vdev_initialize_thread
!= NULL
||
7446 vd
->vdev_top
->vdev_removing
)) {
7447 mutex_exit(&vd
->vdev_initialize_lock
);
7448 return (SET_ERROR(EBUSY
));
7449 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
7450 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
7451 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
7452 mutex_exit(&vd
->vdev_initialize_lock
);
7453 return (SET_ERROR(ESRCH
));
7454 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
7455 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
7456 mutex_exit(&vd
->vdev_initialize_lock
);
7457 return (SET_ERROR(ESRCH
));
7458 } else if (cmd_type
== POOL_INITIALIZE_UNINIT
&&
7459 vd
->vdev_initialize_thread
!= NULL
) {
7460 mutex_exit(&vd
->vdev_initialize_lock
);
7461 return (SET_ERROR(EBUSY
));
7465 case POOL_INITIALIZE_START
:
7466 vdev_initialize(vd
);
7468 case POOL_INITIALIZE_CANCEL
:
7469 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
7471 case POOL_INITIALIZE_SUSPEND
:
7472 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
7474 case POOL_INITIALIZE_UNINIT
:
7475 vdev_uninitialize(vd
);
7478 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7480 mutex_exit(&vd
->vdev_initialize_lock
);
7486 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
7487 nvlist_t
*vdev_errlist
)
7489 int total_errors
= 0;
7492 list_create(&vd_list
, sizeof (vdev_t
),
7493 offsetof(vdev_t
, vdev_initialize_node
));
7496 * We hold the namespace lock through the whole function
7497 * to prevent any changes to the pool while we're starting or
7498 * stopping initialization. The config and state locks are held so that
7499 * we can properly assess the vdev state before we commit to
7500 * the initializing operation.
7502 mutex_enter(&spa_namespace_lock
);
7504 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7505 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7506 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7508 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
7511 char guid_as_str
[MAXNAMELEN
];
7513 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7514 "%llu", (unsigned long long)vdev_guid
);
7515 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7520 /* Wait for all initialize threads to stop. */
7521 vdev_initialize_stop_wait(spa
, &vd_list
);
7523 /* Sync out the initializing state */
7524 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7525 mutex_exit(&spa_namespace_lock
);
7527 list_destroy(&vd_list
);
7529 return (total_errors
);
7533 spa_vdev_trim_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7534 uint64_t rate
, boolean_t partial
, boolean_t secure
, list_t
*vd_list
)
7536 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7538 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
7540 /* Look up vdev and ensure it's a leaf. */
7541 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7542 if (vd
== NULL
|| vd
->vdev_detached
) {
7543 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7544 return (SET_ERROR(ENODEV
));
7545 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
7546 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7547 return (SET_ERROR(EINVAL
));
7548 } else if (!vdev_writeable(vd
)) {
7549 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7550 return (SET_ERROR(EROFS
));
7551 } else if (!vd
->vdev_has_trim
) {
7552 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7553 return (SET_ERROR(EOPNOTSUPP
));
7554 } else if (secure
&& !vd
->vdev_has_securetrim
) {
7555 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7556 return (SET_ERROR(EOPNOTSUPP
));
7558 mutex_enter(&vd
->vdev_trim_lock
);
7559 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
7562 * When we activate a TRIM action we check to see if the
7563 * vdev_trim_thread is NULL. We do this instead of using the
7564 * vdev_trim_state since there might be a previous TRIM process
7565 * which has completed but the thread is not exited.
7567 if (cmd_type
== POOL_TRIM_START
&&
7568 (vd
->vdev_trim_thread
!= NULL
|| vd
->vdev_top
->vdev_removing
)) {
7569 mutex_exit(&vd
->vdev_trim_lock
);
7570 return (SET_ERROR(EBUSY
));
7571 } else if (cmd_type
== POOL_TRIM_CANCEL
&&
7572 (vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
&&
7573 vd
->vdev_trim_state
!= VDEV_TRIM_SUSPENDED
)) {
7574 mutex_exit(&vd
->vdev_trim_lock
);
7575 return (SET_ERROR(ESRCH
));
7576 } else if (cmd_type
== POOL_TRIM_SUSPEND
&&
7577 vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
) {
7578 mutex_exit(&vd
->vdev_trim_lock
);
7579 return (SET_ERROR(ESRCH
));
7583 case POOL_TRIM_START
:
7584 vdev_trim(vd
, rate
, partial
, secure
);
7586 case POOL_TRIM_CANCEL
:
7587 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, vd_list
);
7589 case POOL_TRIM_SUSPEND
:
7590 vdev_trim_stop(vd
, VDEV_TRIM_SUSPENDED
, vd_list
);
7593 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
7595 mutex_exit(&vd
->vdev_trim_lock
);
7601 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
7602 * TRIM threads for each child vdev. These threads pass over all of the free
7603 * space in the vdev's metaslabs and issues TRIM commands for that space.
7606 spa_vdev_trim(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
, uint64_t rate
,
7607 boolean_t partial
, boolean_t secure
, nvlist_t
*vdev_errlist
)
7609 int total_errors
= 0;
7612 list_create(&vd_list
, sizeof (vdev_t
),
7613 offsetof(vdev_t
, vdev_trim_node
));
7616 * We hold the namespace lock through the whole function
7617 * to prevent any changes to the pool while we're starting or
7618 * stopping TRIM. The config and state locks are held so that
7619 * we can properly assess the vdev state before we commit to
7620 * the TRIM operation.
7622 mutex_enter(&spa_namespace_lock
);
7624 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
7625 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
7626 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
7628 int error
= spa_vdev_trim_impl(spa
, vdev_guid
, cmd_type
,
7629 rate
, partial
, secure
, &vd_list
);
7631 char guid_as_str
[MAXNAMELEN
];
7633 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
7634 "%llu", (unsigned long long)vdev_guid
);
7635 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
7640 /* Wait for all TRIM threads to stop. */
7641 vdev_trim_stop_wait(spa
, &vd_list
);
7643 /* Sync out the TRIM state */
7644 txg_wait_synced(spa
->spa_dsl_pool
, 0);
7645 mutex_exit(&spa_namespace_lock
);
7647 list_destroy(&vd_list
);
7649 return (total_errors
);
7653 * Split a set of devices from their mirrors, and create a new pool from them.
7656 spa_vdev_split_mirror(spa_t
*spa
, const char *newname
, nvlist_t
*config
,
7657 nvlist_t
*props
, boolean_t exp
)
7660 uint64_t txg
, *glist
;
7662 uint_t c
, children
, lastlog
;
7663 nvlist_t
**child
, *nvl
, *tmp
;
7665 const char *altroot
= NULL
;
7666 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
7667 boolean_t activate_slog
;
7669 ASSERT(spa_writeable(spa
));
7671 txg
= spa_vdev_enter(spa
);
7673 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7674 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7675 error
= (spa_has_checkpoint(spa
)) ?
7676 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7677 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7680 /* clear the log and flush everything up to now */
7681 activate_slog
= spa_passivate_log(spa
);
7682 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7683 error
= spa_reset_logs(spa
);
7684 txg
= spa_vdev_config_enter(spa
);
7687 spa_activate_log(spa
);
7690 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7692 /* check new spa name before going any further */
7693 if (spa_lookup(newname
) != NULL
)
7694 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
7697 * scan through all the children to ensure they're all mirrors
7699 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
7700 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
7702 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7704 /* first, check to ensure we've got the right child count */
7705 rvd
= spa
->spa_root_vdev
;
7707 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
7708 vdev_t
*vd
= rvd
->vdev_child
[c
];
7710 /* don't count the holes & logs as children */
7711 if (vd
->vdev_islog
|| (vd
->vdev_ops
!= &vdev_indirect_ops
&&
7712 !vdev_is_concrete(vd
))) {
7720 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
7721 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7723 /* next, ensure no spare or cache devices are part of the split */
7724 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
7725 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
7726 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7728 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
7729 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
7731 /* then, loop over each vdev and validate it */
7732 for (c
= 0; c
< children
; c
++) {
7733 uint64_t is_hole
= 0;
7735 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
7739 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
7740 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
7743 error
= SET_ERROR(EINVAL
);
7748 /* deal with indirect vdevs */
7749 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ops
==
7753 /* which disk is going to be split? */
7754 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
7756 error
= SET_ERROR(EINVAL
);
7760 /* look it up in the spa */
7761 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
7762 if (vml
[c
] == NULL
) {
7763 error
= SET_ERROR(ENODEV
);
7767 /* make sure there's nothing stopping the split */
7768 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
7769 vml
[c
]->vdev_islog
||
7770 !vdev_is_concrete(vml
[c
]) ||
7771 vml
[c
]->vdev_isspare
||
7772 vml
[c
]->vdev_isl2cache
||
7773 !vdev_writeable(vml
[c
]) ||
7774 vml
[c
]->vdev_children
!= 0 ||
7775 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
7776 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
7777 error
= SET_ERROR(EINVAL
);
7781 if (vdev_dtl_required(vml
[c
]) ||
7782 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
7783 error
= SET_ERROR(EBUSY
);
7787 /* we need certain info from the top level */
7788 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
7789 vml
[c
]->vdev_top
->vdev_ms_array
);
7790 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
7791 vml
[c
]->vdev_top
->vdev_ms_shift
);
7792 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
7793 vml
[c
]->vdev_top
->vdev_asize
);
7794 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
7795 vml
[c
]->vdev_top
->vdev_ashift
);
7797 /* transfer per-vdev ZAPs */
7798 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
7799 VERIFY0(nvlist_add_uint64(child
[c
],
7800 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
7802 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
7803 VERIFY0(nvlist_add_uint64(child
[c
],
7804 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
7805 vml
[c
]->vdev_parent
->vdev_top_zap
));
7809 kmem_free(vml
, children
* sizeof (vdev_t
*));
7810 kmem_free(glist
, children
* sizeof (uint64_t));
7811 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7814 /* stop writers from using the disks */
7815 for (c
= 0; c
< children
; c
++) {
7817 vml
[c
]->vdev_offline
= B_TRUE
;
7819 vdev_reopen(spa
->spa_root_vdev
);
7822 * Temporarily record the splitting vdevs in the spa config. This
7823 * will disappear once the config is regenerated.
7825 nvl
= fnvlist_alloc();
7826 fnvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
, glist
, children
);
7827 kmem_free(glist
, children
* sizeof (uint64_t));
7829 mutex_enter(&spa
->spa_props_lock
);
7830 fnvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
, nvl
);
7831 mutex_exit(&spa
->spa_props_lock
);
7832 spa
->spa_config_splitting
= nvl
;
7833 vdev_config_dirty(spa
->spa_root_vdev
);
7835 /* configure and create the new pool */
7836 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
);
7837 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
7838 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
);
7839 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
, spa_version(spa
));
7840 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, spa
->spa_config_txg
);
7841 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
7842 spa_generate_guid(NULL
));
7843 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
7844 (void) nvlist_lookup_string(props
,
7845 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
7847 /* add the new pool to the namespace */
7848 newspa
= spa_add(newname
, config
, altroot
);
7849 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7850 newspa
->spa_config_txg
= spa
->spa_config_txg
;
7851 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
7853 /* release the spa config lock, retaining the namespace lock */
7854 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
7856 if (zio_injection_enabled
)
7857 zio_handle_panic_injection(spa
, FTAG
, 1);
7859 spa_activate(newspa
, spa_mode_global
);
7860 spa_async_suspend(newspa
);
7863 * Temporarily stop the initializing and TRIM activity. We set the
7864 * state to ACTIVE so that we know to resume initializing or TRIM
7865 * once the split has completed.
7867 list_t vd_initialize_list
;
7868 list_create(&vd_initialize_list
, sizeof (vdev_t
),
7869 offsetof(vdev_t
, vdev_initialize_node
));
7871 list_t vd_trim_list
;
7872 list_create(&vd_trim_list
, sizeof (vdev_t
),
7873 offsetof(vdev_t
, vdev_trim_node
));
7875 for (c
= 0; c
< children
; c
++) {
7876 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7877 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
7878 vdev_initialize_stop(vml
[c
],
7879 VDEV_INITIALIZE_ACTIVE
, &vd_initialize_list
);
7880 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
7882 mutex_enter(&vml
[c
]->vdev_trim_lock
);
7883 vdev_trim_stop(vml
[c
], VDEV_TRIM_ACTIVE
, &vd_trim_list
);
7884 mutex_exit(&vml
[c
]->vdev_trim_lock
);
7888 vdev_initialize_stop_wait(spa
, &vd_initialize_list
);
7889 vdev_trim_stop_wait(spa
, &vd_trim_list
);
7891 list_destroy(&vd_initialize_list
);
7892 list_destroy(&vd_trim_list
);
7894 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
7895 newspa
->spa_is_splitting
= B_TRUE
;
7897 /* create the new pool from the disks of the original pool */
7898 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
7902 /* if that worked, generate a real config for the new pool */
7903 if (newspa
->spa_root_vdev
!= NULL
) {
7904 newspa
->spa_config_splitting
= fnvlist_alloc();
7905 fnvlist_add_uint64(newspa
->spa_config_splitting
,
7906 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
));
7907 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
7912 if (props
!= NULL
) {
7913 spa_configfile_set(newspa
, props
, B_FALSE
);
7914 error
= spa_prop_set(newspa
, props
);
7919 /* flush everything */
7920 txg
= spa_vdev_config_enter(newspa
);
7921 vdev_config_dirty(newspa
->spa_root_vdev
);
7922 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
7924 if (zio_injection_enabled
)
7925 zio_handle_panic_injection(spa
, FTAG
, 2);
7927 spa_async_resume(newspa
);
7929 /* finally, update the original pool's config */
7930 txg
= spa_vdev_config_enter(spa
);
7931 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
7932 error
= dmu_tx_assign(tx
, TXG_WAIT
);
7935 for (c
= 0; c
< children
; c
++) {
7936 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
7937 vdev_t
*tvd
= vml
[c
]->vdev_top
;
7940 * Need to be sure the detachable VDEV is not
7941 * on any *other* txg's DTL list to prevent it
7942 * from being accessed after it's freed.
7944 for (int t
= 0; t
< TXG_SIZE
; t
++) {
7945 (void) txg_list_remove_this(
7946 &tvd
->vdev_dtl_list
, vml
[c
], t
);
7951 spa_history_log_internal(spa
, "detach", tx
,
7952 "vdev=%s", vml
[c
]->vdev_path
);
7957 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
7958 vdev_config_dirty(spa
->spa_root_vdev
);
7959 spa
->spa_config_splitting
= NULL
;
7963 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
7965 if (zio_injection_enabled
)
7966 zio_handle_panic_injection(spa
, FTAG
, 3);
7968 /* split is complete; log a history record */
7969 spa_history_log_internal(newspa
, "split", NULL
,
7970 "from pool %s", spa_name(spa
));
7972 newspa
->spa_is_splitting
= B_FALSE
;
7973 kmem_free(vml
, children
* sizeof (vdev_t
*));
7975 /* if we're not going to mount the filesystems in userland, export */
7977 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
7984 spa_deactivate(newspa
);
7987 txg
= spa_vdev_config_enter(spa
);
7989 /* re-online all offlined disks */
7990 for (c
= 0; c
< children
; c
++) {
7992 vml
[c
]->vdev_offline
= B_FALSE
;
7995 /* restart initializing or trimming disks as necessary */
7996 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
7997 spa_async_request(spa
, SPA_ASYNC_TRIM_RESTART
);
7998 spa_async_request(spa
, SPA_ASYNC_AUTOTRIM_RESTART
);
8000 vdev_reopen(spa
->spa_root_vdev
);
8002 nvlist_free(spa
->spa_config_splitting
);
8003 spa
->spa_config_splitting
= NULL
;
8004 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
8006 kmem_free(vml
, children
* sizeof (vdev_t
*));
8011 * Find any device that's done replacing, or a vdev marked 'unspare' that's
8012 * currently spared, so we can detach it.
8015 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
8017 vdev_t
*newvd
, *oldvd
;
8019 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8020 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
8026 * Check for a completed replacement. We always consider the first
8027 * vdev in the list to be the oldest vdev, and the last one to be
8028 * the newest (see spa_vdev_attach() for how that works). In
8029 * the case where the newest vdev is faulted, we will not automatically
8030 * remove it after a resilver completes. This is OK as it will require
8031 * user intervention to determine which disk the admin wishes to keep.
8033 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
8034 ASSERT(vd
->vdev_children
> 1);
8036 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
8037 oldvd
= vd
->vdev_child
[0];
8039 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8040 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8041 !vdev_dtl_required(oldvd
))
8046 * Check for a completed resilver with the 'unspare' flag set.
8047 * Also potentially update faulted state.
8049 if (vd
->vdev_ops
== &vdev_spare_ops
) {
8050 vdev_t
*first
= vd
->vdev_child
[0];
8051 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
8053 if (last
->vdev_unspare
) {
8056 } else if (first
->vdev_unspare
) {
8063 if (oldvd
!= NULL
&&
8064 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8065 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8066 !vdev_dtl_required(oldvd
))
8069 vdev_propagate_state(vd
);
8072 * If there are more than two spares attached to a disk,
8073 * and those spares are not required, then we want to
8074 * attempt to free them up now so that they can be used
8075 * by other pools. Once we're back down to a single
8076 * disk+spare, we stop removing them.
8078 if (vd
->vdev_children
> 2) {
8079 newvd
= vd
->vdev_child
[1];
8081 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
8082 vdev_dtl_empty(last
, DTL_MISSING
) &&
8083 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
8084 !vdev_dtl_required(newvd
))
8093 spa_vdev_resilver_done(spa_t
*spa
)
8095 vdev_t
*vd
, *pvd
, *ppvd
;
8096 uint64_t guid
, sguid
, pguid
, ppguid
;
8098 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8100 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
8101 pvd
= vd
->vdev_parent
;
8102 ppvd
= pvd
->vdev_parent
;
8103 guid
= vd
->vdev_guid
;
8104 pguid
= pvd
->vdev_guid
;
8105 ppguid
= ppvd
->vdev_guid
;
8108 * If we have just finished replacing a hot spared device, then
8109 * we need to detach the parent's first child (the original hot
8112 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
8113 ppvd
->vdev_children
== 2) {
8114 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
8115 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
8117 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
8119 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8120 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
8122 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
8124 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8127 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8130 * If a detach was not performed above replace waiters will not have
8131 * been notified. In which case we must do so now.
8133 spa_notify_waiters(spa
);
8137 * Update the stored path or FRU for this vdev.
8140 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
8144 boolean_t sync
= B_FALSE
;
8146 ASSERT(spa_writeable(spa
));
8148 spa_vdev_state_enter(spa
, SCL_ALL
);
8150 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
8151 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
8153 if (!vd
->vdev_ops
->vdev_op_leaf
)
8154 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
8157 if (strcmp(value
, vd
->vdev_path
) != 0) {
8158 spa_strfree(vd
->vdev_path
);
8159 vd
->vdev_path
= spa_strdup(value
);
8163 if (vd
->vdev_fru
== NULL
) {
8164 vd
->vdev_fru
= spa_strdup(value
);
8166 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
8167 spa_strfree(vd
->vdev_fru
);
8168 vd
->vdev_fru
= spa_strdup(value
);
8173 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
8177 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
8179 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
8183 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
8185 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
8189 * ==========================================================================
8191 * ==========================================================================
8194 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
8196 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8198 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8199 return (SET_ERROR(EBUSY
));
8201 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
8205 spa_scan_stop(spa_t
*spa
)
8207 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8208 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8209 return (SET_ERROR(EBUSY
));
8211 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
8215 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
8217 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8219 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
8220 return (SET_ERROR(ENOTSUP
));
8222 if (func
== POOL_SCAN_RESILVER
&&
8223 !spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
8224 return (SET_ERROR(ENOTSUP
));
8227 * If a resilver was requested, but there is no DTL on a
8228 * writeable leaf device, we have nothing to do.
8230 if (func
== POOL_SCAN_RESILVER
&&
8231 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
8232 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
8236 if (func
== POOL_SCAN_ERRORSCRUB
&&
8237 !spa_feature_is_enabled(spa
, SPA_FEATURE_HEAD_ERRLOG
))
8238 return (SET_ERROR(ENOTSUP
));
8240 return (dsl_scan(spa
->spa_dsl_pool
, func
));
8244 * ==========================================================================
8245 * SPA async task processing
8246 * ==========================================================================
8250 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
8252 if (vd
->vdev_remove_wanted
) {
8253 vd
->vdev_remove_wanted
= B_FALSE
;
8254 vd
->vdev_delayed_close
= B_FALSE
;
8255 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
8258 * We want to clear the stats, but we don't want to do a full
8259 * vdev_clear() as that will cause us to throw away
8260 * degraded/faulted state as well as attempt to reopen the
8261 * device, all of which is a waste.
8263 vd
->vdev_stat
.vs_read_errors
= 0;
8264 vd
->vdev_stat
.vs_write_errors
= 0;
8265 vd
->vdev_stat
.vs_checksum_errors
= 0;
8267 vdev_state_dirty(vd
->vdev_top
);
8269 /* Tell userspace that the vdev is gone. */
8270 zfs_post_remove(spa
, vd
);
8273 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8274 spa_async_remove(spa
, vd
->vdev_child
[c
]);
8278 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
8280 if (vd
->vdev_probe_wanted
) {
8281 vd
->vdev_probe_wanted
= B_FALSE
;
8282 vdev_reopen(vd
); /* vdev_open() does the actual probe */
8285 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8286 spa_async_probe(spa
, vd
->vdev_child
[c
]);
8290 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
8292 if (!spa
->spa_autoexpand
)
8295 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8296 vdev_t
*cvd
= vd
->vdev_child
[c
];
8297 spa_async_autoexpand(spa
, cvd
);
8300 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
8303 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
8306 static __attribute__((noreturn
)) void
8307 spa_async_thread(void *arg
)
8309 spa_t
*spa
= (spa_t
*)arg
;
8310 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8313 ASSERT(spa
->spa_sync_on
);
8315 mutex_enter(&spa
->spa_async_lock
);
8316 tasks
= spa
->spa_async_tasks
;
8317 spa
->spa_async_tasks
= 0;
8318 mutex_exit(&spa
->spa_async_lock
);
8321 * See if the config needs to be updated.
8323 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
8324 uint64_t old_space
, new_space
;
8326 mutex_enter(&spa_namespace_lock
);
8327 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
8328 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
8329 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8330 old_space
+= metaslab_class_get_space(
8331 spa_embedded_log_class(spa
));
8333 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
8335 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
8336 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
8337 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8338 new_space
+= metaslab_class_get_space(
8339 spa_embedded_log_class(spa
));
8340 mutex_exit(&spa_namespace_lock
);
8343 * If the pool grew as a result of the config update,
8344 * then log an internal history event.
8346 if (new_space
!= old_space
) {
8347 spa_history_log_internal(spa
, "vdev online", NULL
,
8348 "pool '%s' size: %llu(+%llu)",
8349 spa_name(spa
), (u_longlong_t
)new_space
,
8350 (u_longlong_t
)(new_space
- old_space
));
8355 * See if any devices need to be marked REMOVED.
8357 if (tasks
& SPA_ASYNC_REMOVE
) {
8358 spa_vdev_state_enter(spa
, SCL_NONE
);
8359 spa_async_remove(spa
, spa
->spa_root_vdev
);
8360 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
8361 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
8362 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
8363 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
8364 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8367 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
8368 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8369 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
8370 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8374 * See if any devices need to be probed.
8376 if (tasks
& SPA_ASYNC_PROBE
) {
8377 spa_vdev_state_enter(spa
, SCL_NONE
);
8378 spa_async_probe(spa
, spa
->spa_root_vdev
);
8379 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8383 * If any devices are done replacing, detach them.
8385 if (tasks
& SPA_ASYNC_RESILVER_DONE
||
8386 tasks
& SPA_ASYNC_REBUILD_DONE
||
8387 tasks
& SPA_ASYNC_DETACH_SPARE
) {
8388 spa_vdev_resilver_done(spa
);
8392 * Kick off a resilver.
8394 if (tasks
& SPA_ASYNC_RESILVER
&&
8395 !vdev_rebuild_active(spa
->spa_root_vdev
) &&
8396 (!dsl_scan_resilvering(dp
) ||
8397 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
8398 dsl_scan_restart_resilver(dp
, 0);
8400 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
8401 mutex_enter(&spa_namespace_lock
);
8402 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8403 vdev_initialize_restart(spa
->spa_root_vdev
);
8404 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8405 mutex_exit(&spa_namespace_lock
);
8408 if (tasks
& SPA_ASYNC_TRIM_RESTART
) {
8409 mutex_enter(&spa_namespace_lock
);
8410 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8411 vdev_trim_restart(spa
->spa_root_vdev
);
8412 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8413 mutex_exit(&spa_namespace_lock
);
8416 if (tasks
& SPA_ASYNC_AUTOTRIM_RESTART
) {
8417 mutex_enter(&spa_namespace_lock
);
8418 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8419 vdev_autotrim_restart(spa
);
8420 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8421 mutex_exit(&spa_namespace_lock
);
8425 * Kick off L2 cache whole device TRIM.
8427 if (tasks
& SPA_ASYNC_L2CACHE_TRIM
) {
8428 mutex_enter(&spa_namespace_lock
);
8429 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8430 vdev_trim_l2arc(spa
);
8431 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8432 mutex_exit(&spa_namespace_lock
);
8436 * Kick off L2 cache rebuilding.
8438 if (tasks
& SPA_ASYNC_L2CACHE_REBUILD
) {
8439 mutex_enter(&spa_namespace_lock
);
8440 spa_config_enter(spa
, SCL_L2ARC
, FTAG
, RW_READER
);
8441 l2arc_spa_rebuild_start(spa
);
8442 spa_config_exit(spa
, SCL_L2ARC
, FTAG
);
8443 mutex_exit(&spa_namespace_lock
);
8447 * Let the world know that we're done.
8449 mutex_enter(&spa
->spa_async_lock
);
8450 spa
->spa_async_thread
= NULL
;
8451 cv_broadcast(&spa
->spa_async_cv
);
8452 mutex_exit(&spa
->spa_async_lock
);
8457 spa_async_suspend(spa_t
*spa
)
8459 mutex_enter(&spa
->spa_async_lock
);
8460 spa
->spa_async_suspended
++;
8461 while (spa
->spa_async_thread
!= NULL
)
8462 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
8463 mutex_exit(&spa
->spa_async_lock
);
8465 spa_vdev_remove_suspend(spa
);
8467 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8468 if (condense_thread
!= NULL
)
8469 zthr_cancel(condense_thread
);
8471 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8472 if (discard_thread
!= NULL
)
8473 zthr_cancel(discard_thread
);
8475 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8476 if (ll_delete_thread
!= NULL
)
8477 zthr_cancel(ll_delete_thread
);
8479 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8480 if (ll_condense_thread
!= NULL
)
8481 zthr_cancel(ll_condense_thread
);
8485 spa_async_resume(spa_t
*spa
)
8487 mutex_enter(&spa
->spa_async_lock
);
8488 ASSERT(spa
->spa_async_suspended
!= 0);
8489 spa
->spa_async_suspended
--;
8490 mutex_exit(&spa
->spa_async_lock
);
8491 spa_restart_removal(spa
);
8493 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
8494 if (condense_thread
!= NULL
)
8495 zthr_resume(condense_thread
);
8497 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
8498 if (discard_thread
!= NULL
)
8499 zthr_resume(discard_thread
);
8501 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
8502 if (ll_delete_thread
!= NULL
)
8503 zthr_resume(ll_delete_thread
);
8505 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
8506 if (ll_condense_thread
!= NULL
)
8507 zthr_resume(ll_condense_thread
);
8511 spa_async_tasks_pending(spa_t
*spa
)
8513 uint_t non_config_tasks
;
8515 boolean_t config_task_suspended
;
8517 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
8518 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
8519 if (spa
->spa_ccw_fail_time
== 0) {
8520 config_task_suspended
= B_FALSE
;
8522 config_task_suspended
=
8523 (gethrtime() - spa
->spa_ccw_fail_time
) <
8524 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
8527 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
8531 spa_async_dispatch(spa_t
*spa
)
8533 mutex_enter(&spa
->spa_async_lock
);
8534 if (spa_async_tasks_pending(spa
) &&
8535 !spa
->spa_async_suspended
&&
8536 spa
->spa_async_thread
== NULL
)
8537 spa
->spa_async_thread
= thread_create(NULL
, 0,
8538 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
8539 mutex_exit(&spa
->spa_async_lock
);
8543 spa_async_request(spa_t
*spa
, int task
)
8545 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
8546 mutex_enter(&spa
->spa_async_lock
);
8547 spa
->spa_async_tasks
|= task
;
8548 mutex_exit(&spa
->spa_async_lock
);
8552 spa_async_tasks(spa_t
*spa
)
8554 return (spa
->spa_async_tasks
);
8558 * ==========================================================================
8559 * SPA syncing routines
8560 * ==========================================================================
8565 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8569 bpobj_enqueue(bpo
, bp
, bp_freed
, tx
);
8574 bpobj_enqueue_alloc_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8576 return (bpobj_enqueue_cb(arg
, bp
, B_FALSE
, tx
));
8580 bpobj_enqueue_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8582 return (bpobj_enqueue_cb(arg
, bp
, B_TRUE
, tx
));
8586 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
8590 zio_nowait(zio_free_sync(pio
, pio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
8596 bpobj_spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
8600 return (spa_free_sync_cb(arg
, bp
, tx
));
8604 * Note: this simple function is not inlined to make it easier to dtrace the
8605 * amount of time spent syncing frees.
8608 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
8610 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8611 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
8612 VERIFY(zio_wait(zio
) == 0);
8616 * Note: this simple function is not inlined to make it easier to dtrace the
8617 * amount of time spent syncing deferred frees.
8620 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
8622 if (spa_sync_pass(spa
) != 1)
8627 * If the log space map feature is active, we stop deferring
8628 * frees to the next TXG and therefore running this function
8629 * would be considered a no-op as spa_deferred_bpobj should
8630 * not have any entries.
8632 * That said we run this function anyway (instead of returning
8633 * immediately) for the edge-case scenario where we just
8634 * activated the log space map feature in this TXG but we have
8635 * deferred frees from the previous TXG.
8637 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
8638 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
8639 bpobj_spa_free_sync_cb
, zio
, tx
), ==, 0);
8640 VERIFY0(zio_wait(zio
));
8644 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
8646 char *packed
= NULL
;
8651 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
8654 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
8655 * information. This avoids the dmu_buf_will_dirty() path and
8656 * saves us a pre-read to get data we don't actually care about.
8658 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
8659 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
8661 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
8663 memset(packed
+ nvsize
, 0, bufsize
- nvsize
);
8665 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
8667 vmem_free(packed
, bufsize
);
8669 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
8670 dmu_buf_will_dirty(db
, tx
);
8671 *(uint64_t *)db
->db_data
= nvsize
;
8672 dmu_buf_rele(db
, FTAG
);
8676 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
8677 const char *config
, const char *entry
)
8687 * Update the MOS nvlist describing the list of available devices.
8688 * spa_validate_aux() will have already made sure this nvlist is
8689 * valid and the vdevs are labeled appropriately.
8691 if (sav
->sav_object
== 0) {
8692 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
8693 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
8694 sizeof (uint64_t), tx
);
8695 VERIFY(zap_update(spa
->spa_meta_objset
,
8696 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
8697 &sav
->sav_object
, tx
) == 0);
8700 nvroot
= fnvlist_alloc();
8701 if (sav
->sav_count
== 0) {
8702 fnvlist_add_nvlist_array(nvroot
, config
,
8703 (const nvlist_t
* const *)NULL
, 0);
8705 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
8706 for (i
= 0; i
< sav
->sav_count
; i
++)
8707 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
8708 B_FALSE
, VDEV_CONFIG_L2CACHE
);
8709 fnvlist_add_nvlist_array(nvroot
, config
,
8710 (const nvlist_t
* const *)list
, sav
->sav_count
);
8711 for (i
= 0; i
< sav
->sav_count
; i
++)
8712 nvlist_free(list
[i
]);
8713 kmem_free(list
, sav
->sav_count
* sizeof (void *));
8716 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
8717 nvlist_free(nvroot
);
8719 sav
->sav_sync
= B_FALSE
;
8723 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
8724 * The all-vdev ZAP must be empty.
8727 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
8729 spa_t
*spa
= vd
->vdev_spa
;
8731 if (vd
->vdev_root_zap
!= 0 &&
8732 spa_feature_is_active(spa
, SPA_FEATURE_AVZ_V2
)) {
8733 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8734 vd
->vdev_root_zap
, tx
));
8736 if (vd
->vdev_top_zap
!= 0) {
8737 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8738 vd
->vdev_top_zap
, tx
));
8740 if (vd
->vdev_leaf_zap
!= 0) {
8741 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
8742 vd
->vdev_leaf_zap
, tx
));
8744 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
8745 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
8750 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
8755 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
8756 * its config may not be dirty but we still need to build per-vdev ZAPs.
8757 * Similarly, if the pool is being assembled (e.g. after a split), we
8758 * need to rebuild the AVZ although the config may not be dirty.
8760 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
8761 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
8764 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
8766 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
8767 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
8768 spa
->spa_all_vdev_zaps
!= 0);
8770 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
8771 /* Make and build the new AVZ */
8772 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
8773 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
8774 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
8776 /* Diff old AVZ with new one */
8780 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8781 spa
->spa_all_vdev_zaps
);
8782 zap_cursor_retrieve(&zc
, &za
) == 0;
8783 zap_cursor_advance(&zc
)) {
8784 uint64_t vdzap
= za
.za_first_integer
;
8785 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
8788 * ZAP is listed in old AVZ but not in new one;
8791 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
8796 zap_cursor_fini(&zc
);
8798 /* Destroy the old AVZ */
8799 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8800 spa
->spa_all_vdev_zaps
, tx
));
8802 /* Replace the old AVZ in the dir obj with the new one */
8803 VERIFY0(zap_update(spa
->spa_meta_objset
,
8804 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
8805 sizeof (new_avz
), 1, &new_avz
, tx
));
8807 spa
->spa_all_vdev_zaps
= new_avz
;
8808 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
8812 /* Walk through the AVZ and destroy all listed ZAPs */
8813 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
8814 spa
->spa_all_vdev_zaps
);
8815 zap_cursor_retrieve(&zc
, &za
) == 0;
8816 zap_cursor_advance(&zc
)) {
8817 uint64_t zap
= za
.za_first_integer
;
8818 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
8821 zap_cursor_fini(&zc
);
8823 /* Destroy and unlink the AVZ itself */
8824 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
8825 spa
->spa_all_vdev_zaps
, tx
));
8826 VERIFY0(zap_remove(spa
->spa_meta_objset
,
8827 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
8828 spa
->spa_all_vdev_zaps
= 0;
8831 if (spa
->spa_all_vdev_zaps
== 0) {
8832 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
8833 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
8834 DMU_POOL_VDEV_ZAP_MAP
, tx
);
8836 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
8838 /* Create ZAPs for vdevs that don't have them. */
8839 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
8841 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
8842 dmu_tx_get_txg(tx
), B_FALSE
);
8845 * If we're upgrading the spa version then make sure that
8846 * the config object gets updated with the correct version.
8848 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
8849 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
8850 spa
->spa_uberblock
.ub_version
);
8852 spa_config_exit(spa
, SCL_STATE
, FTAG
);
8854 nvlist_free(spa
->spa_config_syncing
);
8855 spa
->spa_config_syncing
= config
;
8857 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
8861 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
8863 uint64_t *versionp
= arg
;
8864 uint64_t version
= *versionp
;
8865 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8868 * Setting the version is special cased when first creating the pool.
8870 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
8872 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
8873 ASSERT(version
>= spa_version(spa
));
8875 spa
->spa_uberblock
.ub_version
= version
;
8876 vdev_config_dirty(spa
->spa_root_vdev
);
8877 spa_history_log_internal(spa
, "set", tx
, "version=%lld",
8878 (longlong_t
)version
);
8882 * Set zpool properties.
8885 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
8887 nvlist_t
*nvp
= arg
;
8888 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
8889 objset_t
*mos
= spa
->spa_meta_objset
;
8890 nvpair_t
*elem
= NULL
;
8892 mutex_enter(&spa
->spa_props_lock
);
8894 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
8896 const char *strval
, *fname
;
8898 const char *propname
;
8899 const char *elemname
= nvpair_name(elem
);
8900 zprop_type_t proptype
;
8903 switch (prop
= zpool_name_to_prop(elemname
)) {
8904 case ZPOOL_PROP_VERSION
:
8905 intval
= fnvpair_value_uint64(elem
);
8907 * The version is synced separately before other
8908 * properties and should be correct by now.
8910 ASSERT3U(spa_version(spa
), >=, intval
);
8913 case ZPOOL_PROP_ALTROOT
:
8915 * 'altroot' is a non-persistent property. It should
8916 * have been set temporarily at creation or import time.
8918 ASSERT(spa
->spa_root
!= NULL
);
8921 case ZPOOL_PROP_READONLY
:
8922 case ZPOOL_PROP_CACHEFILE
:
8924 * 'readonly' and 'cachefile' are also non-persistent
8928 case ZPOOL_PROP_COMMENT
:
8929 strval
= fnvpair_value_string(elem
);
8930 if (spa
->spa_comment
!= NULL
)
8931 spa_strfree(spa
->spa_comment
);
8932 spa
->spa_comment
= spa_strdup(strval
);
8934 * We need to dirty the configuration on all the vdevs
8935 * so that their labels get updated. We also need to
8936 * update the cache file to keep it in sync with the
8937 * MOS version. It's unnecessary to do this for pool
8938 * creation since the vdev's configuration has already
8941 if (tx
->tx_txg
!= TXG_INITIAL
) {
8942 vdev_config_dirty(spa
->spa_root_vdev
);
8943 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
8945 spa_history_log_internal(spa
, "set", tx
,
8946 "%s=%s", elemname
, strval
);
8948 case ZPOOL_PROP_COMPATIBILITY
:
8949 strval
= fnvpair_value_string(elem
);
8950 if (spa
->spa_compatibility
!= NULL
)
8951 spa_strfree(spa
->spa_compatibility
);
8952 spa
->spa_compatibility
= spa_strdup(strval
);
8954 * Dirty the configuration on vdevs as above.
8956 if (tx
->tx_txg
!= TXG_INITIAL
) {
8957 vdev_config_dirty(spa
->spa_root_vdev
);
8958 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
8961 spa_history_log_internal(spa
, "set", tx
,
8962 "%s=%s", nvpair_name(elem
), strval
);
8965 case ZPOOL_PROP_INVAL
:
8966 if (zpool_prop_feature(elemname
)) {
8967 fname
= strchr(elemname
, '@') + 1;
8968 VERIFY0(zfeature_lookup_name(fname
, &fid
));
8970 spa_feature_enable(spa
, fid
, tx
);
8971 spa_history_log_internal(spa
, "set", tx
,
8972 "%s=enabled", elemname
);
8974 } else if (!zfs_prop_user(elemname
)) {
8975 ASSERT(zpool_prop_feature(elemname
));
8981 * Set pool property values in the poolprops mos object.
8983 if (spa
->spa_pool_props_object
== 0) {
8984 spa
->spa_pool_props_object
=
8985 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
8986 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
8990 /* normalize the property name */
8991 if (prop
== ZPOOL_PROP_INVAL
) {
8992 propname
= elemname
;
8993 proptype
= PROP_TYPE_STRING
;
8995 propname
= zpool_prop_to_name(prop
);
8996 proptype
= zpool_prop_get_type(prop
);
8999 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
9000 ASSERT(proptype
== PROP_TYPE_STRING
);
9001 strval
= fnvpair_value_string(elem
);
9002 VERIFY0(zap_update(mos
,
9003 spa
->spa_pool_props_object
, propname
,
9004 1, strlen(strval
) + 1, strval
, tx
));
9005 spa_history_log_internal(spa
, "set", tx
,
9006 "%s=%s", elemname
, strval
);
9007 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
9008 intval
= fnvpair_value_uint64(elem
);
9010 if (proptype
== PROP_TYPE_INDEX
) {
9012 VERIFY0(zpool_prop_index_to_string(
9013 prop
, intval
, &unused
));
9015 VERIFY0(zap_update(mos
,
9016 spa
->spa_pool_props_object
, propname
,
9017 8, 1, &intval
, tx
));
9018 spa_history_log_internal(spa
, "set", tx
,
9019 "%s=%lld", elemname
,
9020 (longlong_t
)intval
);
9023 case ZPOOL_PROP_DELEGATION
:
9024 spa
->spa_delegation
= intval
;
9026 case ZPOOL_PROP_BOOTFS
:
9027 spa
->spa_bootfs
= intval
;
9029 case ZPOOL_PROP_FAILUREMODE
:
9030 spa
->spa_failmode
= intval
;
9032 case ZPOOL_PROP_AUTOTRIM
:
9033 spa
->spa_autotrim
= intval
;
9034 spa_async_request(spa
,
9035 SPA_ASYNC_AUTOTRIM_RESTART
);
9037 case ZPOOL_PROP_AUTOEXPAND
:
9038 spa
->spa_autoexpand
= intval
;
9039 if (tx
->tx_txg
!= TXG_INITIAL
)
9040 spa_async_request(spa
,
9041 SPA_ASYNC_AUTOEXPAND
);
9043 case ZPOOL_PROP_MULTIHOST
:
9044 spa
->spa_multihost
= intval
;
9050 ASSERT(0); /* not allowed */
9056 mutex_exit(&spa
->spa_props_lock
);
9060 * Perform one-time upgrade on-disk changes. spa_version() does not
9061 * reflect the new version this txg, so there must be no changes this
9062 * txg to anything that the upgrade code depends on after it executes.
9063 * Therefore this must be called after dsl_pool_sync() does the sync
9067 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
9069 if (spa_sync_pass(spa
) != 1)
9072 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9073 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
9075 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
9076 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
9077 dsl_pool_create_origin(dp
, tx
);
9079 /* Keeping the origin open increases spa_minref */
9080 spa
->spa_minref
+= 3;
9083 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
9084 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
9085 dsl_pool_upgrade_clones(dp
, tx
);
9088 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
9089 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
9090 dsl_pool_upgrade_dir_clones(dp
, tx
);
9092 /* Keeping the freedir open increases spa_minref */
9093 spa
->spa_minref
+= 3;
9096 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
9097 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9098 spa_feature_create_zap_objects(spa
, tx
);
9102 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
9103 * when possibility to use lz4 compression for metadata was added
9104 * Old pools that have this feature enabled must be upgraded to have
9105 * this feature active
9107 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9108 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
9109 SPA_FEATURE_LZ4_COMPRESS
);
9110 boolean_t lz4_ac
= spa_feature_is_active(spa
,
9111 SPA_FEATURE_LZ4_COMPRESS
);
9113 if (lz4_en
&& !lz4_ac
)
9114 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
9118 * If we haven't written the salt, do so now. Note that the
9119 * feature may not be activated yet, but that's fine since
9120 * the presence of this ZAP entry is backwards compatible.
9122 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
9123 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
9124 VERIFY0(zap_add(spa
->spa_meta_objset
,
9125 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
9126 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
9127 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
9130 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
9134 vdev_indirect_state_sync_verify(vdev_t
*vd
)
9136 vdev_indirect_mapping_t
*vim __maybe_unused
= vd
->vdev_indirect_mapping
;
9137 vdev_indirect_births_t
*vib __maybe_unused
= vd
->vdev_indirect_births
;
9139 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
9140 ASSERT(vim
!= NULL
);
9141 ASSERT(vib
!= NULL
);
9144 uint64_t obsolete_sm_object
= 0;
9145 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
9146 if (obsolete_sm_object
!= 0) {
9147 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
9148 ASSERT(vd
->vdev_removing
||
9149 vd
->vdev_ops
== &vdev_indirect_ops
);
9150 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
9151 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
9152 ASSERT3U(obsolete_sm_object
, ==,
9153 space_map_object(vd
->vdev_obsolete_sm
));
9154 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
9155 space_map_allocated(vd
->vdev_obsolete_sm
));
9157 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
9160 * Since frees / remaps to an indirect vdev can only
9161 * happen in syncing context, the obsolete segments
9162 * tree must be empty when we start syncing.
9164 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
9168 * Set the top-level vdev's max queue depth. Evaluate each top-level's
9169 * async write queue depth in case it changed. The max queue depth will
9170 * not change in the middle of syncing out this txg.
9173 spa_sync_adjust_vdev_max_queue_depth(spa_t
*spa
)
9175 ASSERT(spa_writeable(spa
));
9177 vdev_t
*rvd
= spa
->spa_root_vdev
;
9178 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
9179 zfs_vdev_queue_depth_pct
/ 100;
9180 metaslab_class_t
*normal
= spa_normal_class(spa
);
9181 metaslab_class_t
*special
= spa_special_class(spa
);
9182 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
9184 uint64_t slots_per_allocator
= 0;
9185 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9186 vdev_t
*tvd
= rvd
->vdev_child
[c
];
9188 metaslab_group_t
*mg
= tvd
->vdev_mg
;
9189 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
9192 metaslab_class_t
*mc
= mg
->mg_class
;
9193 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
9197 * It is safe to do a lock-free check here because only async
9198 * allocations look at mg_max_alloc_queue_depth, and async
9199 * allocations all happen from spa_sync().
9201 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9202 ASSERT0(zfs_refcount_count(
9203 &(mg
->mg_allocator
[i
].mga_alloc_queue_depth
)));
9205 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
9207 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9208 mg
->mg_allocator
[i
].mga_cur_max_alloc_queue_depth
=
9209 zfs_vdev_def_queue_depth
;
9211 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
9214 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9215 ASSERT0(zfs_refcount_count(&normal
->mc_allocator
[i
].
9217 ASSERT0(zfs_refcount_count(&special
->mc_allocator
[i
].
9219 ASSERT0(zfs_refcount_count(&dedup
->mc_allocator
[i
].
9221 normal
->mc_allocator
[i
].mca_alloc_max_slots
=
9222 slots_per_allocator
;
9223 special
->mc_allocator
[i
].mca_alloc_max_slots
=
9224 slots_per_allocator
;
9225 dedup
->mc_allocator
[i
].mca_alloc_max_slots
=
9226 slots_per_allocator
;
9228 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9229 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9230 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9234 spa_sync_condense_indirect(spa_t
*spa
, dmu_tx_t
*tx
)
9236 ASSERT(spa_writeable(spa
));
9238 vdev_t
*rvd
= spa
->spa_root_vdev
;
9239 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9240 vdev_t
*vd
= rvd
->vdev_child
[c
];
9241 vdev_indirect_state_sync_verify(vd
);
9243 if (vdev_indirect_should_condense(vd
)) {
9244 spa_condense_indirect_start_sync(vd
, tx
);
9251 spa_sync_iterate_to_convergence(spa_t
*spa
, dmu_tx_t
*tx
)
9253 objset_t
*mos
= spa
->spa_meta_objset
;
9254 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9255 uint64_t txg
= tx
->tx_txg
;
9256 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
9259 int pass
= ++spa
->spa_sync_pass
;
9261 spa_sync_config_object(spa
, tx
);
9262 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
9263 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
9264 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
9265 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
9266 spa_errlog_sync(spa
, txg
);
9267 dsl_pool_sync(dp
, txg
);
9269 if (pass
< zfs_sync_pass_deferred_free
||
9270 spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
)) {
9272 * If the log space map feature is active we don't
9273 * care about deferred frees and the deferred bpobj
9274 * as the log space map should effectively have the
9275 * same results (i.e. appending only to one object).
9277 spa_sync_frees(spa
, free_bpl
, tx
);
9280 * We can not defer frees in pass 1, because
9281 * we sync the deferred frees later in pass 1.
9283 ASSERT3U(pass
, >, 1);
9284 bplist_iterate(free_bpl
, bpobj_enqueue_alloc_cb
,
9285 &spa
->spa_deferred_bpobj
, tx
);
9290 dsl_scan_sync(dp
, tx
);
9291 dsl_errorscrub_sync(dp
, tx
);
9293 spa_sync_upgrades(spa
, tx
);
9295 spa_flush_metaslabs(spa
, tx
);
9298 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
9303 * Note: We need to check if the MOS is dirty because we could
9304 * have marked the MOS dirty without updating the uberblock
9305 * (e.g. if we have sync tasks but no dirty user data). We need
9306 * to check the uberblock's rootbp because it is updated if we
9307 * have synced out dirty data (though in this case the MOS will
9308 * most likely also be dirty due to second order effects, we
9309 * don't want to rely on that here).
9312 spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
9313 !dmu_objset_is_dirty(mos
, txg
)) {
9315 * Nothing changed on the first pass, therefore this
9316 * TXG is a no-op. Avoid syncing deferred frees, so
9317 * that we can keep this TXG as a no-op.
9319 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9320 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9321 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
9322 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
, txg
));
9326 spa_sync_deferred_frees(spa
, tx
);
9327 } while (dmu_objset_is_dirty(mos
, txg
));
9331 * Rewrite the vdev configuration (which includes the uberblock) to
9332 * commit the transaction group.
9334 * If there are no dirty vdevs, we sync the uberblock to a few random
9335 * top-level vdevs that are known to be visible in the config cache
9336 * (see spa_vdev_add() for a complete description). If there *are* dirty
9337 * vdevs, sync the uberblock to all vdevs.
9340 spa_sync_rewrite_vdev_config(spa_t
*spa
, dmu_tx_t
*tx
)
9342 vdev_t
*rvd
= spa
->spa_root_vdev
;
9343 uint64_t txg
= tx
->tx_txg
;
9349 * We hold SCL_STATE to prevent vdev open/close/etc.
9350 * while we're attempting to write the vdev labels.
9352 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9354 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
9355 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
9357 int children
= rvd
->vdev_children
;
9358 int c0
= random_in_range(children
);
9360 for (int c
= 0; c
< children
; c
++) {
9362 rvd
->vdev_child
[(c0
+ c
) % children
];
9364 /* Stop when revisiting the first vdev */
9365 if (c
> 0 && svd
[0] == vd
)
9368 if (vd
->vdev_ms_array
== 0 ||
9370 !vdev_is_concrete(vd
))
9373 svd
[svdcount
++] = vd
;
9374 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
9377 error
= vdev_config_sync(svd
, svdcount
, txg
);
9379 error
= vdev_config_sync(rvd
->vdev_child
,
9380 rvd
->vdev_children
, txg
);
9384 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
9386 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9390 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
9391 zio_resume_wait(spa
);
9396 * Sync the specified transaction group. New blocks may be dirtied as
9397 * part of the process, so we iterate until it converges.
9400 spa_sync(spa_t
*spa
, uint64_t txg
)
9404 VERIFY(spa_writeable(spa
));
9407 * Wait for i/os issued in open context that need to complete
9408 * before this txg syncs.
9410 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
9411 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
9415 * Now that there can be no more cloning in this transaction group,
9416 * but we are still before issuing frees, we can process pending BRT
9419 brt_pending_apply(spa
, txg
);
9422 * Lock out configuration changes.
9424 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9426 spa
->spa_syncing_txg
= txg
;
9427 spa
->spa_sync_pass
= 0;
9429 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9430 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
9431 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
9432 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
9436 * If there are any pending vdev state changes, convert them
9437 * into config changes that go out with this transaction group.
9439 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9440 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9441 /* Avoid holding the write lock unless actually necessary */
9442 if (vd
->vdev_aux
== NULL
) {
9443 vdev_state_clean(vd
);
9444 vdev_config_dirty(vd
);
9448 * We need the write lock here because, for aux vdevs,
9449 * calling vdev_config_dirty() modifies sav_config.
9450 * This is ugly and will become unnecessary when we
9451 * eliminate the aux vdev wart by integrating all vdevs
9452 * into the root vdev tree.
9454 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9455 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
9456 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
9457 vdev_state_clean(vd
);
9458 vdev_config_dirty(vd
);
9460 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9461 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9463 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9465 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9466 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
9468 spa
->spa_sync_starttime
= gethrtime();
9469 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9470 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
9471 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
9472 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
9475 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
9476 * set spa_deflate if we have no raid-z vdevs.
9478 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
9479 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
9480 vdev_t
*rvd
= spa
->spa_root_vdev
;
9483 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
9484 vd
= rvd
->vdev_child
[i
];
9485 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
9488 if (i
== rvd
->vdev_children
) {
9489 spa
->spa_deflate
= TRUE
;
9490 VERIFY0(zap_add(spa
->spa_meta_objset
,
9491 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
9492 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
9496 spa_sync_adjust_vdev_max_queue_depth(spa
);
9498 spa_sync_condense_indirect(spa
, tx
);
9500 spa_sync_iterate_to_convergence(spa
, tx
);
9503 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
9505 * Make sure that the number of ZAPs for all the vdevs matches
9506 * the number of ZAPs in the per-vdev ZAP list. This only gets
9507 * called if the config is dirty; otherwise there may be
9508 * outstanding AVZ operations that weren't completed in
9509 * spa_sync_config_object.
9511 uint64_t all_vdev_zap_entry_count
;
9512 ASSERT0(zap_count(spa
->spa_meta_objset
,
9513 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
9514 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
9515 all_vdev_zap_entry_count
);
9519 if (spa
->spa_vdev_removal
!= NULL
) {
9520 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
9523 spa_sync_rewrite_vdev_config(spa
, tx
);
9526 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
9527 spa
->spa_deadman_tqid
= 0;
9530 * Clear the dirty config list.
9532 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
9533 vdev_config_clean(vd
);
9536 * Now that the new config has synced transactionally,
9537 * let it become visible to the config cache.
9539 if (spa
->spa_config_syncing
!= NULL
) {
9540 spa_config_set(spa
, spa
->spa_config_syncing
);
9541 spa
->spa_config_txg
= txg
;
9542 spa
->spa_config_syncing
= NULL
;
9545 dsl_pool_sync_done(dp
, txg
);
9547 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9548 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
9549 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
9550 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
9554 * Update usable space statistics.
9556 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
9558 vdev_sync_done(vd
, txg
);
9560 metaslab_class_evict_old(spa
->spa_normal_class
, txg
);
9561 metaslab_class_evict_old(spa
->spa_log_class
, txg
);
9563 spa_sync_close_syncing_log_sm(spa
);
9565 spa_update_dspace(spa
);
9567 if (spa_get_autotrim(spa
) == SPA_AUTOTRIM_ON
)
9568 vdev_autotrim_kick(spa
);
9571 * It had better be the case that we didn't dirty anything
9572 * since vdev_config_sync().
9574 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9575 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9576 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
9578 while (zfs_pause_spa_sync
)
9581 spa
->spa_sync_pass
= 0;
9584 * Update the last synced uberblock here. We want to do this at
9585 * the end of spa_sync() so that consumers of spa_last_synced_txg()
9586 * will be guaranteed that all the processing associated with
9587 * that txg has been completed.
9589 spa
->spa_ubsync
= spa
->spa_uberblock
;
9590 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9592 spa_handle_ignored_writes(spa
);
9595 * If any async tasks have been requested, kick them off.
9597 spa_async_dispatch(spa
);
9601 * Sync all pools. We don't want to hold the namespace lock across these
9602 * operations, so we take a reference on the spa_t and drop the lock during the
9606 spa_sync_allpools(void)
9609 mutex_enter(&spa_namespace_lock
);
9610 while ((spa
= spa_next(spa
)) != NULL
) {
9611 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
9612 !spa_writeable(spa
) || spa_suspended(spa
))
9614 spa_open_ref(spa
, FTAG
);
9615 mutex_exit(&spa_namespace_lock
);
9616 txg_wait_synced(spa_get_dsl(spa
), 0);
9617 mutex_enter(&spa_namespace_lock
);
9618 spa_close(spa
, FTAG
);
9620 mutex_exit(&spa_namespace_lock
);
9624 * ==========================================================================
9625 * Miscellaneous routines
9626 * ==========================================================================
9630 * Remove all pools in the system.
9638 * Remove all cached state. All pools should be closed now,
9639 * so every spa in the AVL tree should be unreferenced.
9641 mutex_enter(&spa_namespace_lock
);
9642 while ((spa
= spa_next(NULL
)) != NULL
) {
9644 * Stop async tasks. The async thread may need to detach
9645 * a device that's been replaced, which requires grabbing
9646 * spa_namespace_lock, so we must drop it here.
9648 spa_open_ref(spa
, FTAG
);
9649 mutex_exit(&spa_namespace_lock
);
9650 spa_async_suspend(spa
);
9651 mutex_enter(&spa_namespace_lock
);
9652 spa_close(spa
, FTAG
);
9654 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
9656 spa_deactivate(spa
);
9660 mutex_exit(&spa_namespace_lock
);
9664 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
9669 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
9673 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
9674 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
9675 if (vd
->vdev_guid
== guid
)
9679 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
9680 vd
= spa
->spa_spares
.sav_vdevs
[i
];
9681 if (vd
->vdev_guid
== guid
)
9690 spa_upgrade(spa_t
*spa
, uint64_t version
)
9692 ASSERT(spa_writeable(spa
));
9694 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
9697 * This should only be called for a non-faulted pool, and since a
9698 * future version would result in an unopenable pool, this shouldn't be
9701 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
9702 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
9704 spa
->spa_uberblock
.ub_version
= version
;
9705 vdev_config_dirty(spa
->spa_root_vdev
);
9707 spa_config_exit(spa
, SCL_ALL
, FTAG
);
9709 txg_wait_synced(spa_get_dsl(spa
), 0);
9713 spa_has_aux_vdev(spa_t
*spa
, uint64_t guid
, spa_aux_vdev_t
*sav
)
9719 for (i
= 0; i
< sav
->sav_count
; i
++)
9720 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
9723 for (i
= 0; i
< sav
->sav_npending
; i
++) {
9724 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
9725 &vdev_guid
) == 0 && vdev_guid
== guid
)
9733 spa_has_l2cache(spa_t
*spa
, uint64_t guid
)
9735 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_l2cache
));
9739 spa_has_spare(spa_t
*spa
, uint64_t guid
)
9741 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_spares
));
9745 * Check if a pool has an active shared spare device.
9746 * Note: reference count of an active spare is 2, as a spare and as a replace
9749 spa_has_active_shared_spare(spa_t
*spa
)
9753 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
9755 for (i
= 0; i
< sav
->sav_count
; i
++) {
9756 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
9757 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
9766 spa_total_metaslabs(spa_t
*spa
)
9768 vdev_t
*rvd
= spa
->spa_root_vdev
;
9771 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
9772 vdev_t
*vd
= rvd
->vdev_child
[c
];
9773 if (!vdev_is_concrete(vd
))
9775 m
+= vd
->vdev_ms_count
;
9781 * Notify any waiting threads that some activity has switched from being in-
9782 * progress to not-in-progress so that the thread can wake up and determine
9783 * whether it is finished waiting.
9786 spa_notify_waiters(spa_t
*spa
)
9789 * Acquiring spa_activities_lock here prevents the cv_broadcast from
9790 * happening between the waiting thread's check and cv_wait.
9792 mutex_enter(&spa
->spa_activities_lock
);
9793 cv_broadcast(&spa
->spa_activities_cv
);
9794 mutex_exit(&spa
->spa_activities_lock
);
9798 * Notify any waiting threads that the pool is exporting, and then block until
9799 * they are finished using the spa_t.
9802 spa_wake_waiters(spa_t
*spa
)
9804 mutex_enter(&spa
->spa_activities_lock
);
9805 spa
->spa_waiters_cancel
= B_TRUE
;
9806 cv_broadcast(&spa
->spa_activities_cv
);
9807 while (spa
->spa_waiters
!= 0)
9808 cv_wait(&spa
->spa_waiters_cv
, &spa
->spa_activities_lock
);
9809 spa
->spa_waiters_cancel
= B_FALSE
;
9810 mutex_exit(&spa
->spa_activities_lock
);
9813 /* Whether the vdev or any of its descendants are being initialized/trimmed. */
9815 spa_vdev_activity_in_progress_impl(vdev_t
*vd
, zpool_wait_activity_t activity
)
9817 spa_t
*spa
= vd
->vdev_spa
;
9819 ASSERT(spa_config_held(spa
, SCL_CONFIG
| SCL_STATE
, RW_READER
));
9820 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9821 ASSERT(activity
== ZPOOL_WAIT_INITIALIZE
||
9822 activity
== ZPOOL_WAIT_TRIM
);
9824 kmutex_t
*lock
= activity
== ZPOOL_WAIT_INITIALIZE
?
9825 &vd
->vdev_initialize_lock
: &vd
->vdev_trim_lock
;
9827 mutex_exit(&spa
->spa_activities_lock
);
9829 mutex_enter(&spa
->spa_activities_lock
);
9831 boolean_t in_progress
= (activity
== ZPOOL_WAIT_INITIALIZE
) ?
9832 (vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) :
9833 (vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
);
9839 for (int i
= 0; i
< vd
->vdev_children
; i
++) {
9840 if (spa_vdev_activity_in_progress_impl(vd
->vdev_child
[i
],
9849 * If use_guid is true, this checks whether the vdev specified by guid is
9850 * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
9851 * is being initialized/trimmed. The caller must hold the config lock and
9852 * spa_activities_lock.
9855 spa_vdev_activity_in_progress(spa_t
*spa
, boolean_t use_guid
, uint64_t guid
,
9856 zpool_wait_activity_t activity
, boolean_t
*in_progress
)
9858 mutex_exit(&spa
->spa_activities_lock
);
9859 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9860 mutex_enter(&spa
->spa_activities_lock
);
9864 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
9865 if (vd
== NULL
|| !vd
->vdev_ops
->vdev_op_leaf
) {
9866 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9870 vd
= spa
->spa_root_vdev
;
9873 *in_progress
= spa_vdev_activity_in_progress_impl(vd
, activity
);
9875 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9880 * Locking for waiting threads
9881 * ---------------------------
9883 * Waiting threads need a way to check whether a given activity is in progress,
9884 * and then, if it is, wait for it to complete. Each activity will have some
9885 * in-memory representation of the relevant on-disk state which can be used to
9886 * determine whether or not the activity is in progress. The in-memory state and
9887 * the locking used to protect it will be different for each activity, and may
9888 * not be suitable for use with a cvar (e.g., some state is protected by the
9889 * config lock). To allow waiting threads to wait without any races, another
9890 * lock, spa_activities_lock, is used.
9892 * When the state is checked, both the activity-specific lock (if there is one)
9893 * and spa_activities_lock are held. In some cases, the activity-specific lock
9894 * is acquired explicitly (e.g. the config lock). In others, the locking is
9895 * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
9896 * thread releases the activity-specific lock and, if the activity is in
9897 * progress, then cv_waits using spa_activities_lock.
9899 * The waiting thread is woken when another thread, one completing some
9900 * activity, updates the state of the activity and then calls
9901 * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
9902 * needs to hold its activity-specific lock when updating the state, and this
9903 * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
9905 * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
9906 * and because it is held when the waiting thread checks the state of the
9907 * activity, it can never be the case that the completing thread both updates
9908 * the activity state and cv_broadcasts in between the waiting thread's check
9909 * and cv_wait. Thus, a waiting thread can never miss a wakeup.
9911 * In order to prevent deadlock, when the waiting thread does its check, in some
9912 * cases it will temporarily drop spa_activities_lock in order to acquire the
9913 * activity-specific lock. The order in which spa_activities_lock and the
9914 * activity specific lock are acquired in the waiting thread is determined by
9915 * the order in which they are acquired in the completing thread; if the
9916 * completing thread calls spa_notify_waiters with the activity-specific lock
9917 * held, then the waiting thread must also acquire the activity-specific lock
9922 spa_activity_in_progress(spa_t
*spa
, zpool_wait_activity_t activity
,
9923 boolean_t use_tag
, uint64_t tag
, boolean_t
*in_progress
)
9927 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
9930 case ZPOOL_WAIT_CKPT_DISCARD
:
9932 (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
) &&
9933 zap_contains(spa_meta_objset(spa
),
9934 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_ZPOOL_CHECKPOINT
) ==
9937 case ZPOOL_WAIT_FREE
:
9938 *in_progress
= ((spa_version(spa
) >= SPA_VERSION_DEADLISTS
&&
9939 !bpobj_is_empty(&spa
->spa_dsl_pool
->dp_free_bpobj
)) ||
9940 spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
) ||
9941 spa_livelist_delete_check(spa
));
9943 case ZPOOL_WAIT_INITIALIZE
:
9944 case ZPOOL_WAIT_TRIM
:
9945 error
= spa_vdev_activity_in_progress(spa
, use_tag
, tag
,
9946 activity
, in_progress
);
9948 case ZPOOL_WAIT_REPLACE
:
9949 mutex_exit(&spa
->spa_activities_lock
);
9950 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
9951 mutex_enter(&spa
->spa_activities_lock
);
9953 *in_progress
= vdev_replace_in_progress(spa
->spa_root_vdev
);
9954 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
9956 case ZPOOL_WAIT_REMOVE
:
9957 *in_progress
= (spa
->spa_removing_phys
.sr_state
==
9960 case ZPOOL_WAIT_RESILVER
:
9961 if ((*in_progress
= vdev_rebuild_active(spa
->spa_root_vdev
)))
9964 case ZPOOL_WAIT_SCRUB
:
9966 boolean_t scanning
, paused
, is_scrub
;
9967 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
9969 is_scrub
= (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
);
9970 scanning
= (scn
->scn_phys
.scn_state
== DSS_SCANNING
);
9971 paused
= dsl_scan_is_paused_scrub(scn
);
9972 *in_progress
= (scanning
&& !paused
&&
9973 is_scrub
== (activity
== ZPOOL_WAIT_SCRUB
));
9977 panic("unrecognized value for activity %d", activity
);
9984 spa_wait_common(const char *pool
, zpool_wait_activity_t activity
,
9985 boolean_t use_tag
, uint64_t tag
, boolean_t
*waited
)
9988 * The tag is used to distinguish between instances of an activity.
9989 * 'initialize' and 'trim' are the only activities that we use this for.
9990 * The other activities can only have a single instance in progress in a
9991 * pool at one time, making the tag unnecessary.
9993 * There can be multiple devices being replaced at once, but since they
9994 * all finish once resilvering finishes, we don't bother keeping track
9995 * of them individually, we just wait for them all to finish.
9997 if (use_tag
&& activity
!= ZPOOL_WAIT_INITIALIZE
&&
9998 activity
!= ZPOOL_WAIT_TRIM
)
10001 if (activity
< 0 || activity
>= ZPOOL_WAIT_NUM_ACTIVITIES
)
10005 int error
= spa_open(pool
, &spa
, FTAG
);
10010 * Increment the spa's waiter count so that we can call spa_close and
10011 * still ensure that the spa_t doesn't get freed before this thread is
10012 * finished with it when the pool is exported. We want to call spa_close
10013 * before we start waiting because otherwise the additional ref would
10014 * prevent the pool from being exported or destroyed throughout the
10015 * potentially long wait.
10017 mutex_enter(&spa
->spa_activities_lock
);
10018 spa
->spa_waiters
++;
10019 spa_close(spa
, FTAG
);
10023 boolean_t in_progress
;
10024 error
= spa_activity_in_progress(spa
, activity
, use_tag
, tag
,
10027 if (error
|| !in_progress
|| spa
->spa_waiters_cancel
)
10032 if (cv_wait_sig(&spa
->spa_activities_cv
,
10033 &spa
->spa_activities_lock
) == 0) {
10039 spa
->spa_waiters
--;
10040 cv_signal(&spa
->spa_waiters_cv
);
10041 mutex_exit(&spa
->spa_activities_lock
);
10047 * Wait for a particular instance of the specified activity to complete, where
10048 * the instance is identified by 'tag'
10051 spa_wait_tag(const char *pool
, zpool_wait_activity_t activity
, uint64_t tag
,
10054 return (spa_wait_common(pool
, activity
, B_TRUE
, tag
, waited
));
10058 * Wait for all instances of the specified activity complete
10061 spa_wait(const char *pool
, zpool_wait_activity_t activity
, boolean_t
*waited
)
10064 return (spa_wait_common(pool
, activity
, B_FALSE
, 0, waited
));
10068 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10070 sysevent_t
*ev
= NULL
;
10072 nvlist_t
*resource
;
10074 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
10076 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
10077 ev
->resource
= resource
;
10080 (void) spa
, (void) vd
, (void) hist_nvl
, (void) name
;
10086 spa_event_post(sysevent_t
*ev
)
10090 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
10091 kmem_free(ev
, sizeof (*ev
));
10099 * Post a zevent corresponding to the given sysevent. The 'name' must be one
10100 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
10101 * filled in from the spa and (optionally) the vdev. This doesn't do anything
10102 * in the userland libzpool, as we don't want consumers to misinterpret ztest
10103 * or zdb as real changes.
10106 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10108 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
10111 /* state manipulation functions */
10112 EXPORT_SYMBOL(spa_open
);
10113 EXPORT_SYMBOL(spa_open_rewind
);
10114 EXPORT_SYMBOL(spa_get_stats
);
10115 EXPORT_SYMBOL(spa_create
);
10116 EXPORT_SYMBOL(spa_import
);
10117 EXPORT_SYMBOL(spa_tryimport
);
10118 EXPORT_SYMBOL(spa_destroy
);
10119 EXPORT_SYMBOL(spa_export
);
10120 EXPORT_SYMBOL(spa_reset
);
10121 EXPORT_SYMBOL(spa_async_request
);
10122 EXPORT_SYMBOL(spa_async_suspend
);
10123 EXPORT_SYMBOL(spa_async_resume
);
10124 EXPORT_SYMBOL(spa_inject_addref
);
10125 EXPORT_SYMBOL(spa_inject_delref
);
10126 EXPORT_SYMBOL(spa_scan_stat_init
);
10127 EXPORT_SYMBOL(spa_scan_get_stats
);
10129 /* device manipulation */
10130 EXPORT_SYMBOL(spa_vdev_add
);
10131 EXPORT_SYMBOL(spa_vdev_attach
);
10132 EXPORT_SYMBOL(spa_vdev_detach
);
10133 EXPORT_SYMBOL(spa_vdev_setpath
);
10134 EXPORT_SYMBOL(spa_vdev_setfru
);
10135 EXPORT_SYMBOL(spa_vdev_split_mirror
);
10137 /* spare statech is global across all pools) */
10138 EXPORT_SYMBOL(spa_spare_add
);
10139 EXPORT_SYMBOL(spa_spare_remove
);
10140 EXPORT_SYMBOL(spa_spare_exists
);
10141 EXPORT_SYMBOL(spa_spare_activate
);
10143 /* L2ARC statech is global across all pools) */
10144 EXPORT_SYMBOL(spa_l2cache_add
);
10145 EXPORT_SYMBOL(spa_l2cache_remove
);
10146 EXPORT_SYMBOL(spa_l2cache_exists
);
10147 EXPORT_SYMBOL(spa_l2cache_activate
);
10148 EXPORT_SYMBOL(spa_l2cache_drop
);
10151 EXPORT_SYMBOL(spa_scan
);
10152 EXPORT_SYMBOL(spa_scan_stop
);
10155 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
10156 EXPORT_SYMBOL(spa_sync_allpools
);
10159 EXPORT_SYMBOL(spa_prop_set
);
10160 EXPORT_SYMBOL(spa_prop_get
);
10161 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
10163 /* asynchronous event notification */
10164 EXPORT_SYMBOL(spa_event_notify
);
10166 ZFS_MODULE_PARAM(zfs_metaslab
, metaslab_
, preload_pct
, UINT
, ZMOD_RW
,
10167 "Percentage of CPUs to run a metaslab preload taskq");
10169 /* BEGIN CSTYLED */
10170 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_shift
, UINT
, ZMOD_RW
,
10171 "log2 fraction of arc that can be used by inflight I/Os when "
10172 "verifying pool during import");
10175 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_metadata
, INT
, ZMOD_RW
,
10176 "Set to traverse metadata on pool import");
10178 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_data
, INT
, ZMOD_RW
,
10179 "Set to traverse data on pool import");
10181 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_print_vdev_tree
, INT
, ZMOD_RW
,
10182 "Print vdev tree to zfs_dbgmsg during pool import");
10184 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_pct
, UINT
, ZMOD_RD
,
10185 "Percentage of CPUs to run an IO worker thread");
10187 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_tpq
, UINT
, ZMOD_RD
,
10188 "Number of threads per IO worker taskqueue");
10190 /* BEGIN CSTYLED */
10191 ZFS_MODULE_PARAM(zfs
, zfs_
, max_missing_tvds
, U64
, ZMOD_RW
,
10192 "Allow importing pool with up to this number of missing top-level "
10193 "vdevs (in read-only mode)");
10196 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_pause
, INT
,
10197 ZMOD_RW
, "Set the livelist condense zthr to pause");
10199 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_pause
, INT
,
10200 ZMOD_RW
, "Set the livelist condense synctask to pause");
10202 /* BEGIN CSTYLED */
10203 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_cancel
,
10205 "Whether livelist condensing was canceled in the synctask");
10207 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_cancel
,
10209 "Whether livelist condensing was canceled in the zthr function");
10211 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, new_alloc
, INT
,
10213 "Whether extra ALLOC blkptrs were added to a livelist entry while it "
10214 "was being condensed");