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, 2024 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.
37 * Copyright (c) 2024, Klara Inc.
41 * SPA: Storage Pool Allocator
43 * This file contains all the routines used when modifying on-disk SPA state.
44 * This includes opening, importing, destroying, exporting a pool, and syncing a
48 #include <sys/zfs_context.h>
49 #include <sys/fm/fs/zfs.h>
50 #include <sys/spa_impl.h>
52 #include <sys/zio_checksum.h>
54 #include <sys/dmu_tx.h>
59 #include <sys/vdev_impl.h>
60 #include <sys/vdev_removal.h>
61 #include <sys/vdev_indirect_mapping.h>
62 #include <sys/vdev_indirect_births.h>
63 #include <sys/vdev_initialize.h>
64 #include <sys/vdev_rebuild.h>
65 #include <sys/vdev_trim.h>
66 #include <sys/vdev_disk.h>
67 #include <sys/vdev_raidz.h>
68 #include <sys/vdev_draid.h>
69 #include <sys/metaslab.h>
70 #include <sys/metaslab_impl.h>
72 #include <sys/uberblock_impl.h>
75 #include <sys/bpobj.h>
76 #include <sys/dmu_traverse.h>
77 #include <sys/dmu_objset.h>
78 #include <sys/unique.h>
79 #include <sys/dsl_pool.h>
80 #include <sys/dsl_dataset.h>
81 #include <sys/dsl_dir.h>
82 #include <sys/dsl_prop.h>
83 #include <sys/dsl_synctask.h>
84 #include <sys/fs/zfs.h>
86 #include <sys/callb.h>
87 #include <sys/systeminfo.h>
88 #include <sys/zfs_ioctl.h>
89 #include <sys/dsl_scan.h>
90 #include <sys/zfeature.h>
91 #include <sys/dsl_destroy.h>
95 #include <sys/fm/protocol.h>
96 #include <sys/fm/util.h>
97 #include <sys/callb.h>
99 #include <sys/vmsystm.h>
102 #include "zfs_prop.h"
103 #include "zfs_comutil.h"
104 #include <cityhash.h>
107 * spa_thread() existed on Illumos as a parent thread for the various worker
108 * threads that actually run the pool, as a way to both reference the entire
109 * pool work as a single object, and to share properties like scheduling
110 * options. It has not yet been adapted to Linux or FreeBSD. This define is
111 * used to mark related parts of the code to make things easier for the reader,
112 * and to compile this code out. It can be removed when someone implements it,
113 * moves it to some Illumos-specific place, or removes it entirely.
115 #undef HAVE_SPA_THREAD
118 * The "System Duty Cycle" scheduling class is an Illumos feature to help
119 * prevent CPU-intensive kernel threads from affecting latency on interactive
120 * threads. It doesn't exist on Linux or FreeBSD, so the supporting code is
121 * gated behind a define. On Illumos SDC depends on spa_thread(), but
122 * spa_thread() also has other uses, so this is a separate define.
127 * The interval, in seconds, at which failed configuration cache file writes
130 int zfs_ccw_retry_interval
= 300;
132 typedef enum zti_modes
{
133 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
134 ZTI_MODE_SCALE
, /* Taskqs scale with CPUs. */
135 ZTI_MODE_SYNC
, /* sync thread assigned */
136 ZTI_MODE_NULL
, /* don't create a taskq */
140 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
141 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
142 #define ZTI_SCALE { ZTI_MODE_SCALE, 0, 1 }
143 #define ZTI_SYNC { ZTI_MODE_SYNC, 0, 1 }
144 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
146 #define ZTI_N(n) ZTI_P(n, 1)
147 #define ZTI_ONE ZTI_N(1)
149 typedef struct zio_taskq_info
{
150 zti_modes_t zti_mode
;
155 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
156 "iss", "iss_h", "int", "int_h"
160 * This table defines the taskq settings for each ZFS I/O type. When
161 * initializing a pool, we use this table to create an appropriately sized
162 * taskq. Some operations are low volume and therefore have a small, static
163 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
164 * macros. Other operations process a large amount of data; the ZTI_SCALE
165 * macro causes us to create a taskq oriented for throughput. Some operations
166 * are so high frequency and short-lived that the taskq itself can become a
167 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
168 * additional degree of parallelism specified by the number of threads per-
169 * taskq and the number of taskqs; when dispatching an event in this case, the
170 * particular taskq is chosen at random. ZTI_SCALE uses a number of taskqs
171 * that scales with the number of CPUs.
173 * The different taskq priorities are to handle the different contexts (issue
174 * and interrupt) and then to reserve threads for high priority I/Os that
175 * need to be handled with minimum delay. Illumos taskq has unfair TQ_FRONT
176 * implementation, so separate high priority threads are used there.
178 static zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
179 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
180 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
181 { ZTI_N(8), ZTI_NULL
, ZTI_SCALE
, ZTI_NULL
}, /* READ */
183 { ZTI_SYNC
, ZTI_N(5), ZTI_SCALE
, ZTI_N(5) }, /* WRITE */
185 { ZTI_SYNC
, ZTI_NULL
, ZTI_SCALE
, ZTI_NULL
}, /* WRITE */
187 { ZTI_SCALE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
188 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
189 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FLUSH */
190 { ZTI_N(4), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* TRIM */
193 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
194 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
195 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
196 static int spa_load_impl(spa_t
*spa
, spa_import_type_t type
,
197 const char **ereport
);
198 static void spa_vdev_resilver_done(spa_t
*spa
);
201 * Percentage of all CPUs that can be used by the metaslab preload taskq.
203 static uint_t metaslab_preload_pct
= 50;
205 static uint_t zio_taskq_batch_pct
= 80; /* 1 thread per cpu in pset */
206 static uint_t zio_taskq_batch_tpq
; /* threads per taskq */
209 static const boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
210 static const uint_t zio_taskq_basedc
= 80; /* base duty cycle */
213 #ifdef HAVE_SPA_THREAD
214 static const boolean_t spa_create_process
= B_TRUE
; /* no process => no sysdc */
217 static uint_t zio_taskq_write_tpq
= 16;
220 * Report any spa_load_verify errors found, but do not fail spa_load.
221 * This is used by zdb to analyze non-idle pools.
223 boolean_t spa_load_verify_dryrun
= B_FALSE
;
226 * Allow read spacemaps in case of readonly import (spa_mode == SPA_MODE_READ).
227 * This is used by zdb for spacemaps verification.
229 boolean_t spa_mode_readable_spacemaps
= B_FALSE
;
232 * This (illegal) pool name is used when temporarily importing a spa_t in order
233 * to get the vdev stats associated with the imported devices.
235 #define TRYIMPORT_NAME "$import"
238 * For debugging purposes: print out vdev tree during pool import.
240 static int spa_load_print_vdev_tree
= B_FALSE
;
243 * A non-zero value for zfs_max_missing_tvds means that we allow importing
244 * pools with missing top-level vdevs. This is strictly intended for advanced
245 * pool recovery cases since missing data is almost inevitable. Pools with
246 * missing devices can only be imported read-only for safety reasons, and their
247 * fail-mode will be automatically set to "continue".
249 * With 1 missing vdev we should be able to import the pool and mount all
250 * datasets. User data that was not modified after the missing device has been
251 * added should be recoverable. This means that snapshots created prior to the
252 * addition of that device should be completely intact.
254 * With 2 missing vdevs, some datasets may fail to mount since there are
255 * dataset statistics that are stored as regular metadata. Some data might be
256 * recoverable if those vdevs were added recently.
258 * With 3 or more missing vdevs, the pool is severely damaged and MOS entries
259 * may be missing entirely. Chances of data recovery are very low. Note that
260 * there are also risks of performing an inadvertent rewind as we might be
261 * missing all the vdevs with the latest uberblocks.
263 uint64_t zfs_max_missing_tvds
= 0;
266 * The parameters below are similar to zfs_max_missing_tvds but are only
267 * intended for a preliminary open of the pool with an untrusted config which
268 * might be incomplete or out-dated.
270 * We are more tolerant for pools opened from a cachefile since we could have
271 * an out-dated cachefile where a device removal was not registered.
272 * We could have set the limit arbitrarily high but in the case where devices
273 * are really missing we would want to return the proper error codes; we chose
274 * SPA_DVAS_PER_BP - 1 so that some copies of the MOS would still be available
275 * and we get a chance to retrieve the trusted config.
277 uint64_t zfs_max_missing_tvds_cachefile
= SPA_DVAS_PER_BP
- 1;
280 * In the case where config was assembled by scanning device paths (/dev/dsks
281 * by default) we are less tolerant since all the existing devices should have
282 * been detected and we want spa_load to return the right error codes.
284 uint64_t zfs_max_missing_tvds_scan
= 0;
287 * Debugging aid that pauses spa_sync() towards the end.
289 static const boolean_t zfs_pause_spa_sync
= B_FALSE
;
292 * Variables to indicate the livelist condense zthr func should wait at certain
293 * points for the livelist to be removed - used to test condense/destroy races
295 static int zfs_livelist_condense_zthr_pause
= 0;
296 static int zfs_livelist_condense_sync_pause
= 0;
299 * Variables to track whether or not condense cancellation has been
300 * triggered in testing.
302 static int zfs_livelist_condense_sync_cancel
= 0;
303 static int zfs_livelist_condense_zthr_cancel
= 0;
306 * Variable to track whether or not extra ALLOC blkptrs were added to a
307 * livelist entry while it was being condensed (caused by the way we track
308 * remapped blkptrs in dbuf_remap_impl)
310 static int zfs_livelist_condense_new_alloc
= 0;
313 * ==========================================================================
314 * SPA properties routines
315 * ==========================================================================
319 * Add a (source=src, propname=propval) list to an nvlist.
322 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, const char *strval
,
323 uint64_t intval
, zprop_source_t src
)
325 const char *propname
= zpool_prop_to_name(prop
);
328 propval
= fnvlist_alloc();
329 fnvlist_add_uint64(propval
, ZPROP_SOURCE
, src
);
332 fnvlist_add_string(propval
, ZPROP_VALUE
, strval
);
334 fnvlist_add_uint64(propval
, ZPROP_VALUE
, intval
);
336 fnvlist_add_nvlist(nvl
, propname
, propval
);
337 nvlist_free(propval
);
341 * Add a user property (source=src, propname=propval) to an nvlist.
344 spa_prop_add_user(nvlist_t
*nvl
, const char *propname
, char *strval
,
349 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
350 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
351 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
352 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
353 nvlist_free(propval
);
357 * Get property values from the spa configuration.
360 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
362 vdev_t
*rvd
= spa
->spa_root_vdev
;
363 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
364 uint64_t size
, alloc
, cap
, version
;
365 const zprop_source_t src
= ZPROP_SRC_NONE
;
366 spa_config_dirent_t
*dp
;
367 metaslab_class_t
*mc
= spa_normal_class(spa
);
369 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
372 alloc
= metaslab_class_get_alloc(mc
);
373 alloc
+= metaslab_class_get_alloc(spa_special_class(spa
));
374 alloc
+= metaslab_class_get_alloc(spa_dedup_class(spa
));
375 alloc
+= metaslab_class_get_alloc(spa_embedded_log_class(spa
));
377 size
= metaslab_class_get_space(mc
);
378 size
+= metaslab_class_get_space(spa_special_class(spa
));
379 size
+= metaslab_class_get_space(spa_dedup_class(spa
));
380 size
+= metaslab_class_get_space(spa_embedded_log_class(spa
));
382 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
383 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
384 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
385 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
387 spa_prop_add_list(*nvp
, ZPOOL_PROP_CHECKPOINT
, NULL
,
388 spa
->spa_checkpoint_info
.sci_dspace
, src
);
390 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
391 metaslab_class_fragmentation(mc
), src
);
392 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
393 metaslab_class_expandable_space(mc
), src
);
394 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
395 (spa_mode(spa
) == SPA_MODE_READ
), src
);
397 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
398 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
400 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
401 ddt_get_pool_dedup_ratio(spa
), src
);
402 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONEUSED
, NULL
,
403 brt_get_used(spa
), src
);
404 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONESAVED
, NULL
,
405 brt_get_saved(spa
), src
);
406 spa_prop_add_list(*nvp
, ZPOOL_PROP_BCLONERATIO
, NULL
,
407 brt_get_ratio(spa
), src
);
409 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
410 rvd
->vdev_state
, src
);
412 version
= spa_version(spa
);
413 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
414 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
415 version
, ZPROP_SRC_DEFAULT
);
417 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
418 version
, ZPROP_SRC_LOCAL
);
420 spa_prop_add_list(*nvp
, ZPOOL_PROP_LOAD_GUID
,
421 NULL
, spa_load_guid(spa
), src
);
426 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
427 * when opening pools before this version freedir will be NULL.
429 if (pool
->dp_free_dir
!= NULL
) {
430 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
431 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
434 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
438 if (pool
->dp_leak_dir
!= NULL
) {
439 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
440 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
443 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
448 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
450 if (spa
->spa_comment
!= NULL
) {
451 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
455 if (spa
->spa_compatibility
!= NULL
) {
456 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMPATIBILITY
,
457 spa
->spa_compatibility
, 0, ZPROP_SRC_LOCAL
);
460 if (spa
->spa_root
!= NULL
)
461 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
464 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
465 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
466 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
468 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
469 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
472 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
473 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
474 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
476 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
477 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
480 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
481 if (dp
->scd_path
== NULL
) {
482 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
483 "none", 0, ZPROP_SRC_LOCAL
);
484 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
485 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
486 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
492 * Get zpool property values.
495 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
497 objset_t
*mos
= spa
->spa_meta_objset
;
503 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
507 dp
= spa_get_dsl(spa
);
508 dsl_pool_config_enter(dp
, FTAG
);
509 mutex_enter(&spa
->spa_props_lock
);
512 * Get properties from the spa config.
514 spa_prop_get_config(spa
, nvp
);
516 /* If no pool property object, no more prop to get. */
517 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0)
521 * Get properties from the MOS pool property object.
523 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
524 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
525 zap_cursor_advance(&zc
)) {
528 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
531 if ((prop
= zpool_name_to_prop(za
.za_name
)) ==
532 ZPOOL_PROP_INVAL
&& !zfs_prop_user(za
.za_name
))
535 switch (za
.za_integer_length
) {
537 /* integer property */
538 if (za
.za_first_integer
!=
539 zpool_prop_default_numeric(prop
))
540 src
= ZPROP_SRC_LOCAL
;
542 if (prop
== ZPOOL_PROP_BOOTFS
) {
543 dsl_dataset_t
*ds
= NULL
;
545 err
= dsl_dataset_hold_obj(dp
,
546 za
.za_first_integer
, FTAG
, &ds
);
550 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
552 dsl_dataset_name(ds
, strval
);
553 dsl_dataset_rele(ds
, FTAG
);
556 intval
= za
.za_first_integer
;
559 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
562 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
567 /* string property */
568 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
569 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
570 za
.za_name
, 1, za
.za_num_integers
, strval
);
572 kmem_free(strval
, za
.za_num_integers
);
575 if (prop
!= ZPOOL_PROP_INVAL
) {
576 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
578 src
= ZPROP_SRC_LOCAL
;
579 spa_prop_add_user(*nvp
, za
.za_name
, strval
,
582 kmem_free(strval
, za
.za_num_integers
);
589 zap_cursor_fini(&zc
);
591 mutex_exit(&spa
->spa_props_lock
);
592 dsl_pool_config_exit(dp
, FTAG
);
593 if (err
&& err
!= ENOENT
) {
603 * Validate the given pool properties nvlist and modify the list
604 * for the property values to be set.
607 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
610 int error
= 0, reset_bootfs
= 0;
612 boolean_t has_feature
= B_FALSE
;
615 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
617 const char *strval
, *slash
, *check
, *fname
;
618 const char *propname
= nvpair_name(elem
);
619 zpool_prop_t prop
= zpool_name_to_prop(propname
);
622 case ZPOOL_PROP_INVAL
:
624 * Sanitize the input.
626 if (zfs_prop_user(propname
)) {
627 if (strlen(propname
) >= ZAP_MAXNAMELEN
) {
628 error
= SET_ERROR(ENAMETOOLONG
);
632 if (strlen(fnvpair_value_string(elem
)) >=
634 error
= SET_ERROR(E2BIG
);
637 } else if (zpool_prop_feature(propname
)) {
638 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
639 error
= SET_ERROR(EINVAL
);
643 if (nvpair_value_uint64(elem
, &intval
) != 0) {
644 error
= SET_ERROR(EINVAL
);
649 error
= SET_ERROR(EINVAL
);
653 fname
= strchr(propname
, '@') + 1;
654 if (zfeature_lookup_name(fname
, NULL
) != 0) {
655 error
= SET_ERROR(EINVAL
);
659 has_feature
= B_TRUE
;
661 error
= SET_ERROR(EINVAL
);
666 case ZPOOL_PROP_VERSION
:
667 error
= nvpair_value_uint64(elem
, &intval
);
669 (intval
< spa_version(spa
) ||
670 intval
> SPA_VERSION_BEFORE_FEATURES
||
672 error
= SET_ERROR(EINVAL
);
675 case ZPOOL_PROP_DELEGATION
:
676 case ZPOOL_PROP_AUTOREPLACE
:
677 case ZPOOL_PROP_LISTSNAPS
:
678 case ZPOOL_PROP_AUTOEXPAND
:
679 case ZPOOL_PROP_AUTOTRIM
:
680 error
= nvpair_value_uint64(elem
, &intval
);
681 if (!error
&& intval
> 1)
682 error
= SET_ERROR(EINVAL
);
685 case ZPOOL_PROP_MULTIHOST
:
686 error
= nvpair_value_uint64(elem
, &intval
);
687 if (!error
&& intval
> 1)
688 error
= SET_ERROR(EINVAL
);
691 uint32_t hostid
= zone_get_hostid(NULL
);
693 spa
->spa_hostid
= hostid
;
695 error
= SET_ERROR(ENOTSUP
);
700 case ZPOOL_PROP_BOOTFS
:
702 * If the pool version is less than SPA_VERSION_BOOTFS,
703 * or the pool is still being created (version == 0),
704 * the bootfs property cannot be set.
706 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
707 error
= SET_ERROR(ENOTSUP
);
712 * Make sure the vdev config is bootable
714 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
715 error
= SET_ERROR(ENOTSUP
);
721 error
= nvpair_value_string(elem
, &strval
);
726 if (strval
== NULL
|| strval
[0] == '\0') {
727 objnum
= zpool_prop_default_numeric(
732 error
= dmu_objset_hold(strval
, FTAG
, &os
);
737 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
738 error
= SET_ERROR(ENOTSUP
);
740 objnum
= dmu_objset_id(os
);
742 dmu_objset_rele(os
, FTAG
);
746 case ZPOOL_PROP_FAILUREMODE
:
747 error
= nvpair_value_uint64(elem
, &intval
);
748 if (!error
&& intval
> ZIO_FAILURE_MODE_PANIC
)
749 error
= SET_ERROR(EINVAL
);
752 * This is a special case which only occurs when
753 * the pool has completely failed. This allows
754 * the user to change the in-core failmode property
755 * without syncing it out to disk (I/Os might
756 * currently be blocked). We do this by returning
757 * EIO to the caller (spa_prop_set) to trick it
758 * into thinking we encountered a property validation
761 if (!error
&& spa_suspended(spa
)) {
762 spa
->spa_failmode
= intval
;
763 error
= SET_ERROR(EIO
);
767 case ZPOOL_PROP_CACHEFILE
:
768 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
771 if (strval
[0] == '\0')
774 if (strcmp(strval
, "none") == 0)
777 if (strval
[0] != '/') {
778 error
= SET_ERROR(EINVAL
);
782 slash
= strrchr(strval
, '/');
783 ASSERT(slash
!= NULL
);
785 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
786 strcmp(slash
, "/..") == 0)
787 error
= SET_ERROR(EINVAL
);
790 case ZPOOL_PROP_COMMENT
:
791 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
793 for (check
= strval
; *check
!= '\0'; check
++) {
794 if (!isprint(*check
)) {
795 error
= SET_ERROR(EINVAL
);
799 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
800 error
= SET_ERROR(E2BIG
);
811 (void) nvlist_remove_all(props
,
812 zpool_prop_to_name(ZPOOL_PROP_DEDUPDITTO
));
814 if (!error
&& reset_bootfs
) {
815 error
= nvlist_remove(props
,
816 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
819 error
= nvlist_add_uint64(props
,
820 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
828 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
830 const char *cachefile
;
831 spa_config_dirent_t
*dp
;
833 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
837 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
840 if (cachefile
[0] == '\0')
841 dp
->scd_path
= spa_strdup(spa_config_path
);
842 else if (strcmp(cachefile
, "none") == 0)
845 dp
->scd_path
= spa_strdup(cachefile
);
847 list_insert_head(&spa
->spa_config_list
, dp
);
849 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
853 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
856 nvpair_t
*elem
= NULL
;
857 boolean_t need_sync
= B_FALSE
;
859 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
862 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
863 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
865 if (prop
== ZPOOL_PROP_CACHEFILE
||
866 prop
== ZPOOL_PROP_ALTROOT
||
867 prop
== ZPOOL_PROP_READONLY
)
870 if (prop
== ZPOOL_PROP_INVAL
&&
871 zfs_prop_user(nvpair_name(elem
))) {
876 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPOOL_PROP_INVAL
) {
879 if (prop
== ZPOOL_PROP_VERSION
) {
880 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
882 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
883 ver
= SPA_VERSION_FEATURES
;
887 /* Save time if the version is already set. */
888 if (ver
== spa_version(spa
))
892 * In addition to the pool directory object, we might
893 * create the pool properties object, the features for
894 * read object, the features for write object, or the
895 * feature descriptions object.
897 error
= dsl_sync_task(spa
->spa_name
, NULL
,
898 spa_sync_version
, &ver
,
899 6, ZFS_SPACE_CHECK_RESERVED
);
910 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
911 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
918 * If the bootfs property value is dsobj, clear it.
921 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
923 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
924 VERIFY(zap_remove(spa
->spa_meta_objset
,
925 spa
->spa_pool_props_object
,
926 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
932 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
934 uint64_t *newguid __maybe_unused
= arg
;
935 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
936 vdev_t
*rvd
= spa
->spa_root_vdev
;
939 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
940 int error
= (spa_has_checkpoint(spa
)) ?
941 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
942 return (SET_ERROR(error
));
945 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
946 vdev_state
= rvd
->vdev_state
;
947 spa_config_exit(spa
, SCL_STATE
, FTAG
);
949 if (vdev_state
!= VDEV_STATE_HEALTHY
)
950 return (SET_ERROR(ENXIO
));
952 ASSERT3U(spa_guid(spa
), !=, *newguid
);
958 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
960 uint64_t *newguid
= arg
;
961 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
963 vdev_t
*rvd
= spa
->spa_root_vdev
;
965 oldguid
= spa_guid(spa
);
967 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
968 rvd
->vdev_guid
= *newguid
;
969 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
970 vdev_config_dirty(rvd
);
971 spa_config_exit(spa
, SCL_STATE
, FTAG
);
973 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
974 (u_longlong_t
)oldguid
, (u_longlong_t
)*newguid
);
978 * Change the GUID for the pool. This is done so that we can later
979 * re-import a pool built from a clone of our own vdevs. We will modify
980 * the root vdev's guid, our own pool guid, and then mark all of our
981 * vdevs dirty. Note that we must make sure that all our vdevs are
982 * online when we do this, or else any vdevs that weren't present
983 * would be orphaned from our pool. We are also going to issue a
984 * sysevent to update any watchers.
987 spa_change_guid(spa_t
*spa
)
992 mutex_enter(&spa
->spa_vdev_top_lock
);
993 mutex_enter(&spa_namespace_lock
);
994 guid
= spa_generate_guid(NULL
);
996 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
997 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
1001 * Clear the kobj flag from all the vdevs to allow
1002 * vdev_cache_process_kobj_evt() to post events to all the
1003 * vdevs since GUID is updated.
1005 vdev_clear_kobj_evt(spa
->spa_root_vdev
);
1006 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
1007 vdev_clear_kobj_evt(spa
->spa_l2cache
.sav_vdevs
[i
]);
1009 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
1010 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_REGUID
);
1013 mutex_exit(&spa_namespace_lock
);
1014 mutex_exit(&spa
->spa_vdev_top_lock
);
1020 * ==========================================================================
1021 * SPA state manipulation (open/create/destroy/import/export)
1022 * ==========================================================================
1026 spa_error_entry_compare(const void *a
, const void *b
)
1028 const spa_error_entry_t
*sa
= (const spa_error_entry_t
*)a
;
1029 const spa_error_entry_t
*sb
= (const spa_error_entry_t
*)b
;
1032 ret
= memcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
1033 sizeof (zbookmark_phys_t
));
1035 return (TREE_ISIGN(ret
));
1039 * Utility function which retrieves copies of the current logs and
1040 * re-initializes them in the process.
1043 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
1045 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
1047 memcpy(last
, &spa
->spa_errlist_last
, sizeof (avl_tree_t
));
1048 memcpy(scrub
, &spa
->spa_errlist_scrub
, sizeof (avl_tree_t
));
1050 avl_create(&spa
->spa_errlist_scrub
,
1051 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1052 offsetof(spa_error_entry_t
, se_avl
));
1053 avl_create(&spa
->spa_errlist_last
,
1054 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1055 offsetof(spa_error_entry_t
, se_avl
));
1059 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1061 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
1062 enum zti_modes mode
= ztip
->zti_mode
;
1063 uint_t value
= ztip
->zti_value
;
1064 uint_t count
= ztip
->zti_count
;
1065 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1066 uint_t cpus
, flags
= TASKQ_DYNAMIC
;
1069 case ZTI_MODE_FIXED
:
1070 ASSERT3U(value
, >, 0);
1076 * Create one wr_iss taskq for every 'zio_taskq_write_tpq' CPUs,
1077 * not to exceed the number of spa allocators, and align to it.
1079 cpus
= MAX(1, boot_ncpus
* zio_taskq_batch_pct
/ 100);
1080 count
= MAX(1, cpus
/ MAX(1, zio_taskq_write_tpq
));
1081 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1082 count
= MIN(count
, spa
->spa_alloc_count
);
1083 while (spa
->spa_alloc_count
% count
!= 0 &&
1084 spa
->spa_alloc_count
< count
* 2)
1088 * zio_taskq_batch_pct is unbounded and may exceed 100%, but no
1089 * single taskq may have more threads than 100% of online cpus.
1091 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1092 value
= MIN(value
, 100);
1093 flags
|= TASKQ_THREADS_CPU_PCT
;
1096 case ZTI_MODE_SCALE
:
1097 flags
|= TASKQ_THREADS_CPU_PCT
;
1099 * We want more taskqs to reduce lock contention, but we want
1100 * less for better request ordering and CPU utilization.
1102 cpus
= MAX(1, boot_ncpus
* zio_taskq_batch_pct
/ 100);
1103 if (zio_taskq_batch_tpq
> 0) {
1104 count
= MAX(1, (cpus
+ zio_taskq_batch_tpq
/ 2) /
1105 zio_taskq_batch_tpq
);
1108 * Prefer 6 threads per taskq, but no more taskqs
1109 * than threads in them on large systems. For 80%:
1112 * cpus taskqs percent threads threads
1113 * ------- ------- ------- ------- -------
1124 count
= 1 + cpus
/ 6;
1125 while (count
* count
> cpus
)
1128 /* Limit each taskq within 100% to not trigger assertion. */
1129 count
= MAX(count
, (zio_taskq_batch_pct
+ 99) / 100);
1130 value
= (zio_taskq_batch_pct
+ count
/ 2) / count
;
1134 tqs
->stqs_count
= 0;
1135 tqs
->stqs_taskq
= NULL
;
1139 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
1140 "spa_taskqs_init()",
1141 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
1145 ASSERT3U(count
, >, 0);
1146 tqs
->stqs_count
= count
;
1147 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
1149 for (uint_t i
= 0; i
< count
; i
++) {
1154 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
1155 zio_type_name
[t
], zio_taskq_types
[q
], i
);
1157 (void) snprintf(name
, sizeof (name
), "%s_%s",
1158 zio_type_name
[t
], zio_taskq_types
[q
]);
1161 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
1162 (void) zio_taskq_basedc
;
1163 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
1164 spa
->spa_proc
, zio_taskq_basedc
, flags
);
1167 pri_t pri
= maxclsyspri
;
1169 * The write issue taskq can be extremely CPU
1170 * intensive. Run it at slightly less important
1171 * priority than the other taskqs.
1173 * Under Linux and FreeBSD this means incrementing
1174 * the priority value as opposed to platforms like
1175 * illumos where it should be decremented.
1177 * On FreeBSD, if priorities divided by four (RQ_PPQ)
1178 * are equal then a difference between them is
1181 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
) {
1182 #if defined(__linux__)
1184 #elif defined(__FreeBSD__)
1190 tq
= taskq_create_proc(name
, value
, pri
, 50,
1191 INT_MAX
, spa
->spa_proc
, flags
);
1196 tqs
->stqs_taskq
[i
] = tq
;
1201 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
1203 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1205 if (tqs
->stqs_taskq
== NULL
) {
1206 ASSERT3U(tqs
->stqs_count
, ==, 0);
1210 for (uint_t i
= 0; i
< tqs
->stqs_count
; i
++) {
1211 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
1212 taskq_destroy(tqs
->stqs_taskq
[i
]);
1215 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
1216 tqs
->stqs_taskq
= NULL
;
1221 * The READ and WRITE rows of zio_taskqs are configurable at module load time
1222 * by setting zio_taskq_read or zio_taskq_write.
1224 * Example (the defaults for READ and WRITE)
1225 * zio_taskq_read='fixed,1,8 null scale null'
1226 * zio_taskq_write='sync null scale null'
1228 * Each sets the entire row at a time.
1230 * 'fixed' is parameterised: fixed,Q,T where Q is number of taskqs, T is number
1231 * of threads per taskq.
1233 * 'null' can only be set on the high-priority queues (queue selection for
1234 * high-priority queues will fall back to the regular queue if the high-pri
1237 static const char *const modes
[ZTI_NMODES
] = {
1238 "fixed", "scale", "sync", "null"
1241 /* Parse the incoming config string. Modifies cfg */
1243 spa_taskq_param_set(zio_type_t t
, char *cfg
)
1247 zio_taskq_info_t row
[ZIO_TASKQ_TYPES
] = {{0}};
1249 char *next
= cfg
, *tok
, *c
;
1252 * Parse out each element from the string and fill `row`. The entire
1253 * row has to be set at once, so any errors are flagged by just
1254 * breaking out of this loop early.
1257 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1258 /* `next` is the start of the config */
1262 /* Eat up leading space */
1263 while (isspace(*next
))
1268 /* Mode ends at space or end of string */
1270 next
= strchr(tok
, ' ');
1271 if (next
!= NULL
) *next
++ = '\0';
1273 /* Parameters start after a comma */
1274 c
= strchr(tok
, ',');
1275 if (c
!= NULL
) *c
++ = '\0';
1277 /* Match mode string */
1279 for (mode
= 0; mode
< ZTI_NMODES
; mode
++)
1280 if (strcmp(tok
, modes
[mode
]) == 0)
1282 if (mode
== ZTI_NMODES
)
1285 /* Invalid canary */
1286 row
[q
].zti_mode
= ZTI_NMODES
;
1288 /* Per-mode setup */
1292 * FIXED is parameterised: number of queues, and number of
1293 * threads per queue.
1295 case ZTI_MODE_FIXED
: {
1296 /* No parameters? */
1297 if (c
== NULL
|| *c
== '\0')
1300 /* Find next parameter */
1302 c
= strchr(tok
, ',');
1306 /* Take digits and convert */
1307 unsigned long long nq
;
1308 if (!(isdigit(*tok
)))
1310 err
= ddi_strtoull(tok
, &tok
, 10, &nq
);
1311 /* Must succeed and also end at the next param sep */
1312 if (err
!= 0 || tok
!= c
)
1315 /* Move past the comma */
1317 /* Need another number */
1318 if (!(isdigit(*tok
)))
1320 /* Remember start to make sure we moved */
1324 unsigned long long ntpq
;
1325 err
= ddi_strtoull(tok
, &tok
, 10, &ntpq
);
1326 /* Must succeed, and moved forward */
1327 if (err
!= 0 || tok
== c
|| *tok
!= '\0')
1331 * sanity; zero queues/threads make no sense, and
1332 * 16K is almost certainly more than anyone will ever
1333 * need and avoids silly numbers like UINT32_MAX
1335 if (nq
== 0 || nq
>= 16384 ||
1336 ntpq
== 0 || ntpq
>= 16384)
1339 const zio_taskq_info_t zti
= ZTI_P(ntpq
, nq
);
1344 case ZTI_MODE_SCALE
: {
1345 const zio_taskq_info_t zti
= ZTI_SCALE
;
1350 case ZTI_MODE_SYNC
: {
1351 const zio_taskq_info_t zti
= ZTI_SYNC
;
1356 case ZTI_MODE_NULL
: {
1358 * Can only null the high-priority queues; the general-
1359 * purpose ones have to exist.
1361 if (q
!= ZIO_TASKQ_ISSUE_HIGH
&&
1362 q
!= ZIO_TASKQ_INTERRUPT_HIGH
)
1365 const zio_taskq_info_t zti
= ZTI_NULL
;
1374 /* Ensure we set a mode */
1375 if (row
[q
].zti_mode
== ZTI_NMODES
)
1379 /* Didn't get a full row, fail */
1380 if (q
< ZIO_TASKQ_TYPES
)
1381 return (SET_ERROR(EINVAL
));
1383 /* Eat trailing space */
1385 while (isspace(*next
))
1388 /* If there's anything left over then fail */
1389 if (next
!= NULL
&& *next
!= '\0')
1390 return (SET_ERROR(EINVAL
));
1392 /* Success! Copy it into the real config */
1393 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++)
1394 zio_taskqs
[t
][q
] = row
[q
];
1400 spa_taskq_param_get(zio_type_t t
, char *buf
, boolean_t add_newline
)
1404 /* Build paramater string from live config */
1405 const char *sep
= "";
1406 for (uint_t q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1407 const zio_taskq_info_t
*zti
= &zio_taskqs
[t
][q
];
1408 if (zti
->zti_mode
== ZTI_MODE_FIXED
)
1409 pos
+= sprintf(&buf
[pos
], "%s%s,%u,%u", sep
,
1410 modes
[zti
->zti_mode
], zti
->zti_count
,
1413 pos
+= sprintf(&buf
[pos
], "%s%s", sep
,
1414 modes
[zti
->zti_mode
]);
1427 spa_taskq_read_param_set(const char *val
, zfs_kernel_param_t
*kp
)
1429 char *cfg
= kmem_strdup(val
);
1430 int err
= spa_taskq_param_set(ZIO_TYPE_READ
, cfg
);
1431 kmem_free(cfg
, strlen(val
)+1);
1435 spa_taskq_read_param_get(char *buf
, zfs_kernel_param_t
*kp
)
1437 return (spa_taskq_param_get(ZIO_TYPE_READ
, buf
, TRUE
));
1441 spa_taskq_write_param_set(const char *val
, zfs_kernel_param_t
*kp
)
1443 char *cfg
= kmem_strdup(val
);
1444 int err
= spa_taskq_param_set(ZIO_TYPE_WRITE
, cfg
);
1445 kmem_free(cfg
, strlen(val
)+1);
1449 spa_taskq_write_param_get(char *buf
, zfs_kernel_param_t
*kp
)
1451 return (spa_taskq_param_get(ZIO_TYPE_WRITE
, buf
, TRUE
));
1455 * On FreeBSD load-time parameters can be set up before malloc() is available,
1456 * so we have to do all the parsing work on the stack.
1458 #define SPA_TASKQ_PARAM_MAX (128)
1461 spa_taskq_read_param(ZFS_MODULE_PARAM_ARGS
)
1463 char buf
[SPA_TASKQ_PARAM_MAX
];
1466 (void) spa_taskq_param_get(ZIO_TYPE_READ
, buf
, FALSE
);
1467 err
= sysctl_handle_string(oidp
, buf
, sizeof (buf
), req
);
1468 if (err
|| req
->newptr
== NULL
)
1470 return (spa_taskq_param_set(ZIO_TYPE_READ
, buf
));
1474 spa_taskq_write_param(ZFS_MODULE_PARAM_ARGS
)
1476 char buf
[SPA_TASKQ_PARAM_MAX
];
1479 (void) spa_taskq_param_get(ZIO_TYPE_WRITE
, buf
, FALSE
);
1480 err
= sysctl_handle_string(oidp
, buf
, sizeof (buf
), req
);
1481 if (err
|| req
->newptr
== NULL
)
1483 return (spa_taskq_param_set(ZIO_TYPE_WRITE
, buf
));
1486 #endif /* _KERNEL */
1489 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
1490 * Note that a type may have multiple discrete taskqs to avoid lock contention
1491 * on the taskq itself.
1494 spa_taskq_dispatch(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
1495 task_func_t
*func
, zio_t
*zio
, boolean_t cutinline
)
1497 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
1500 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
1501 ASSERT3U(tqs
->stqs_count
, !=, 0);
1504 * NB: We are assuming that the zio can only be dispatched
1505 * to a single taskq at a time. It would be a grievous error
1506 * to dispatch the zio to another taskq at the same time.
1509 ASSERT(taskq_empty_ent(&zio
->io_tqent
));
1511 if (tqs
->stqs_count
== 1) {
1512 tq
= tqs
->stqs_taskq
[0];
1513 } else if ((t
== ZIO_TYPE_WRITE
) && (q
== ZIO_TASKQ_ISSUE
) &&
1514 ZIO_HAS_ALLOCATOR(zio
)) {
1515 tq
= tqs
->stqs_taskq
[zio
->io_allocator
% tqs
->stqs_count
];
1517 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1520 taskq_dispatch_ent(tq
, func
, zio
, cutinline
? TQ_FRONT
: 0,
1525 spa_create_zio_taskqs(spa_t
*spa
)
1527 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1528 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1529 spa_taskqs_init(spa
, t
, q
);
1534 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1536 spa_thread(void *arg
)
1538 psetid_t zio_taskq_psrset_bind
= PS_NONE
;
1539 callb_cpr_t cprinfo
;
1542 user_t
*pu
= PTOU(curproc
);
1544 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1547 ASSERT(curproc
!= &p0
);
1548 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1549 "zpool-%s", spa
->spa_name
);
1550 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1552 /* bind this thread to the requested psrset */
1553 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1555 mutex_enter(&cpu_lock
);
1556 mutex_enter(&pidlock
);
1557 mutex_enter(&curproc
->p_lock
);
1559 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1560 0, NULL
, NULL
) == 0) {
1561 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1564 "Couldn't bind process for zfs pool \"%s\" to "
1565 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1568 mutex_exit(&curproc
->p_lock
);
1569 mutex_exit(&pidlock
);
1570 mutex_exit(&cpu_lock
);
1575 if (zio_taskq_sysdc
) {
1576 sysdc_thread_enter(curthread
, 100, 0);
1580 spa
->spa_proc
= curproc
;
1581 spa
->spa_did
= curthread
->t_did
;
1583 spa_create_zio_taskqs(spa
);
1585 mutex_enter(&spa
->spa_proc_lock
);
1586 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1588 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1589 cv_broadcast(&spa
->spa_proc_cv
);
1591 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1592 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1593 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1594 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1596 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1597 spa
->spa_proc_state
= SPA_PROC_GONE
;
1598 spa
->spa_proc
= &p0
;
1599 cv_broadcast(&spa
->spa_proc_cv
);
1600 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1602 mutex_enter(&curproc
->p_lock
);
1607 extern metaslab_ops_t
*metaslab_allocator(spa_t
*spa
);
1610 * Activate an uninitialized pool.
1613 spa_activate(spa_t
*spa
, spa_mode_t mode
)
1615 metaslab_ops_t
*msp
= metaslab_allocator(spa
);
1616 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1618 spa
->spa_state
= POOL_STATE_ACTIVE
;
1619 spa
->spa_mode
= mode
;
1620 spa
->spa_read_spacemaps
= spa_mode_readable_spacemaps
;
1622 spa
->spa_normal_class
= metaslab_class_create(spa
, msp
);
1623 spa
->spa_log_class
= metaslab_class_create(spa
, msp
);
1624 spa
->spa_embedded_log_class
= metaslab_class_create(spa
, msp
);
1625 spa
->spa_special_class
= metaslab_class_create(spa
, msp
);
1626 spa
->spa_dedup_class
= metaslab_class_create(spa
, msp
);
1628 /* Try to create a covering process */
1629 mutex_enter(&spa
->spa_proc_lock
);
1630 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1631 ASSERT(spa
->spa_proc
== &p0
);
1634 #ifdef HAVE_SPA_THREAD
1635 /* Only create a process if we're going to be around a while. */
1636 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1637 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1639 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1640 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1641 cv_wait(&spa
->spa_proc_cv
,
1642 &spa
->spa_proc_lock
);
1644 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1645 ASSERT(spa
->spa_proc
!= &p0
);
1646 ASSERT(spa
->spa_did
!= 0);
1650 "Couldn't create process for zfs pool \"%s\"\n",
1655 #endif /* HAVE_SPA_THREAD */
1656 mutex_exit(&spa
->spa_proc_lock
);
1658 /* If we didn't create a process, we need to create our taskqs. */
1659 if (spa
->spa_proc
== &p0
) {
1660 spa_create_zio_taskqs(spa
);
1663 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1664 spa
->spa_txg_zio
[i
] = zio_root(spa
, NULL
, NULL
,
1668 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1669 offsetof(vdev_t
, vdev_config_dirty_node
));
1670 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1671 offsetof(objset_t
, os_evicting_node
));
1672 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1673 offsetof(vdev_t
, vdev_state_dirty_node
));
1675 txg_list_create(&spa
->spa_vdev_txg_list
, spa
,
1676 offsetof(struct vdev
, vdev_txg_node
));
1678 avl_create(&spa
->spa_errlist_scrub
,
1679 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1680 offsetof(spa_error_entry_t
, se_avl
));
1681 avl_create(&spa
->spa_errlist_last
,
1682 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1683 offsetof(spa_error_entry_t
, se_avl
));
1684 avl_create(&spa
->spa_errlist_healed
,
1685 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1686 offsetof(spa_error_entry_t
, se_avl
));
1688 spa_activate_os(spa
);
1690 spa_keystore_init(&spa
->spa_keystore
);
1693 * This taskq is used to perform zvol-minor-related tasks
1694 * asynchronously. This has several advantages, including easy
1695 * resolution of various deadlocks.
1697 * The taskq must be single threaded to ensure tasks are always
1698 * processed in the order in which they were dispatched.
1700 * A taskq per pool allows one to keep the pools independent.
1701 * This way if one pool is suspended, it will not impact another.
1703 * The preferred location to dispatch a zvol minor task is a sync
1704 * task. In this context, there is easy access to the spa_t and minimal
1705 * error handling is required because the sync task must succeed.
1707 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1711 * The taskq to preload metaslabs.
1713 spa
->spa_metaslab_taskq
= taskq_create("z_metaslab",
1714 metaslab_preload_pct
, maxclsyspri
, 1, INT_MAX
,
1715 TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1718 * Taskq dedicated to prefetcher threads: this is used to prevent the
1719 * pool traverse code from monopolizing the global (and limited)
1720 * system_taskq by inappropriately scheduling long running tasks on it.
1722 spa
->spa_prefetch_taskq
= taskq_create("z_prefetch", 100,
1723 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1726 * The taskq to upgrade datasets in this pool. Currently used by
1727 * feature SPA_FEATURE_USEROBJ_ACCOUNTING/SPA_FEATURE_PROJECT_QUOTA.
1729 spa
->spa_upgrade_taskq
= taskq_create("z_upgrade", 100,
1730 defclsyspri
, 1, INT_MAX
, TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
);
1734 * Opposite of spa_activate().
1737 spa_deactivate(spa_t
*spa
)
1739 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1740 ASSERT(spa
->spa_dsl_pool
== NULL
);
1741 ASSERT(spa
->spa_root_vdev
== NULL
);
1742 ASSERT(spa
->spa_async_zio_root
== NULL
);
1743 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1745 spa_evicting_os_wait(spa
);
1747 if (spa
->spa_zvol_taskq
) {
1748 taskq_destroy(spa
->spa_zvol_taskq
);
1749 spa
->spa_zvol_taskq
= NULL
;
1752 if (spa
->spa_metaslab_taskq
) {
1753 taskq_destroy(spa
->spa_metaslab_taskq
);
1754 spa
->spa_metaslab_taskq
= NULL
;
1757 if (spa
->spa_prefetch_taskq
) {
1758 taskq_destroy(spa
->spa_prefetch_taskq
);
1759 spa
->spa_prefetch_taskq
= NULL
;
1762 if (spa
->spa_upgrade_taskq
) {
1763 taskq_destroy(spa
->spa_upgrade_taskq
);
1764 spa
->spa_upgrade_taskq
= NULL
;
1767 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1769 list_destroy(&spa
->spa_config_dirty_list
);
1770 list_destroy(&spa
->spa_evicting_os_list
);
1771 list_destroy(&spa
->spa_state_dirty_list
);
1773 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
1775 for (int t
= 0; t
< ZIO_TYPES
; t
++) {
1776 for (int q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1777 spa_taskqs_fini(spa
, t
, q
);
1781 for (size_t i
= 0; i
< TXG_SIZE
; i
++) {
1782 ASSERT3P(spa
->spa_txg_zio
[i
], !=, NULL
);
1783 VERIFY0(zio_wait(spa
->spa_txg_zio
[i
]));
1784 spa
->spa_txg_zio
[i
] = NULL
;
1787 metaslab_class_destroy(spa
->spa_normal_class
);
1788 spa
->spa_normal_class
= NULL
;
1790 metaslab_class_destroy(spa
->spa_log_class
);
1791 spa
->spa_log_class
= NULL
;
1793 metaslab_class_destroy(spa
->spa_embedded_log_class
);
1794 spa
->spa_embedded_log_class
= NULL
;
1796 metaslab_class_destroy(spa
->spa_special_class
);
1797 spa
->spa_special_class
= NULL
;
1799 metaslab_class_destroy(spa
->spa_dedup_class
);
1800 spa
->spa_dedup_class
= NULL
;
1803 * If this was part of an import or the open otherwise failed, we may
1804 * still have errors left in the queues. Empty them just in case.
1806 spa_errlog_drain(spa
);
1807 avl_destroy(&spa
->spa_errlist_scrub
);
1808 avl_destroy(&spa
->spa_errlist_last
);
1809 avl_destroy(&spa
->spa_errlist_healed
);
1811 spa_keystore_fini(&spa
->spa_keystore
);
1813 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1815 mutex_enter(&spa
->spa_proc_lock
);
1816 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1817 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1818 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1819 cv_broadcast(&spa
->spa_proc_cv
);
1820 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1821 ASSERT(spa
->spa_proc
!= &p0
);
1822 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1824 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1825 spa
->spa_proc_state
= SPA_PROC_NONE
;
1827 ASSERT(spa
->spa_proc
== &p0
);
1828 mutex_exit(&spa
->spa_proc_lock
);
1831 * We want to make sure spa_thread() has actually exited the ZFS
1832 * module, so that the module can't be unloaded out from underneath
1835 if (spa
->spa_did
!= 0) {
1836 thread_join(spa
->spa_did
);
1840 spa_deactivate_os(spa
);
1845 * Verify a pool configuration, and construct the vdev tree appropriately. This
1846 * will create all the necessary vdevs in the appropriate layout, with each vdev
1847 * in the CLOSED state. This will prep the pool before open/creation/import.
1848 * All vdev validation is done by the vdev_alloc() routine.
1851 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1852 uint_t id
, int atype
)
1858 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1861 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1864 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1867 if (error
== ENOENT
)
1873 return (SET_ERROR(EINVAL
));
1876 for (int c
= 0; c
< children
; c
++) {
1878 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1886 ASSERT(*vdp
!= NULL
);
1892 spa_should_flush_logs_on_unload(spa_t
*spa
)
1894 if (!spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
))
1897 if (!spa_writeable(spa
))
1900 if (!spa
->spa_sync_on
)
1903 if (spa_state(spa
) != POOL_STATE_EXPORTED
)
1906 if (zfs_keep_log_spacemaps_at_export
)
1913 * Opens a transaction that will set the flag that will instruct
1914 * spa_sync to attempt to flush all the metaslabs for that txg.
1917 spa_unload_log_sm_flush_all(spa_t
*spa
)
1919 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
1920 VERIFY0(dmu_tx_assign(tx
, TXG_WAIT
));
1922 ASSERT3U(spa
->spa_log_flushall_txg
, ==, 0);
1923 spa
->spa_log_flushall_txg
= dmu_tx_get_txg(tx
);
1926 txg_wait_synced(spa_get_dsl(spa
), spa
->spa_log_flushall_txg
);
1930 spa_unload_log_sm_metadata(spa_t
*spa
)
1932 void *cookie
= NULL
;
1934 log_summary_entry_t
*e
;
1936 while ((sls
= avl_destroy_nodes(&spa
->spa_sm_logs_by_txg
,
1937 &cookie
)) != NULL
) {
1938 VERIFY0(sls
->sls_mscount
);
1939 kmem_free(sls
, sizeof (spa_log_sm_t
));
1942 while ((e
= list_remove_head(&spa
->spa_log_summary
)) != NULL
) {
1943 VERIFY0(e
->lse_mscount
);
1944 kmem_free(e
, sizeof (log_summary_entry_t
));
1947 spa
->spa_unflushed_stats
.sus_nblocks
= 0;
1948 spa
->spa_unflushed_stats
.sus_memused
= 0;
1949 spa
->spa_unflushed_stats
.sus_blocklimit
= 0;
1953 spa_destroy_aux_threads(spa_t
*spa
)
1955 if (spa
->spa_condense_zthr
!= NULL
) {
1956 zthr_destroy(spa
->spa_condense_zthr
);
1957 spa
->spa_condense_zthr
= NULL
;
1959 if (spa
->spa_checkpoint_discard_zthr
!= NULL
) {
1960 zthr_destroy(spa
->spa_checkpoint_discard_zthr
);
1961 spa
->spa_checkpoint_discard_zthr
= NULL
;
1963 if (spa
->spa_livelist_delete_zthr
!= NULL
) {
1964 zthr_destroy(spa
->spa_livelist_delete_zthr
);
1965 spa
->spa_livelist_delete_zthr
= NULL
;
1967 if (spa
->spa_livelist_condense_zthr
!= NULL
) {
1968 zthr_destroy(spa
->spa_livelist_condense_zthr
);
1969 spa
->spa_livelist_condense_zthr
= NULL
;
1971 if (spa
->spa_raidz_expand_zthr
!= NULL
) {
1972 zthr_destroy(spa
->spa_raidz_expand_zthr
);
1973 spa
->spa_raidz_expand_zthr
= NULL
;
1978 * Opposite of spa_load().
1981 spa_unload(spa_t
*spa
)
1983 ASSERT(MUTEX_HELD(&spa_namespace_lock
) ||
1984 spa
->spa_export_thread
== curthread
);
1985 ASSERT(spa_state(spa
) != POOL_STATE_UNINITIALIZED
);
1987 spa_import_progress_remove(spa_guid(spa
));
1988 spa_load_note(spa
, "UNLOADING");
1990 spa_wake_waiters(spa
);
1993 * If we have set the spa_final_txg, we have already performed the
1994 * tasks below in spa_export_common(). We should not redo it here since
1995 * we delay the final TXGs beyond what spa_final_txg is set at.
1997 if (spa
->spa_final_txg
== UINT64_MAX
) {
1999 * If the log space map feature is enabled and the pool is
2000 * getting exported (but not destroyed), we want to spend some
2001 * time flushing as many metaslabs as we can in an attempt to
2002 * destroy log space maps and save import time.
2004 if (spa_should_flush_logs_on_unload(spa
))
2005 spa_unload_log_sm_flush_all(spa
);
2010 spa_async_suspend(spa
);
2012 if (spa
->spa_root_vdev
) {
2013 vdev_t
*root_vdev
= spa
->spa_root_vdev
;
2014 vdev_initialize_stop_all(root_vdev
,
2015 VDEV_INITIALIZE_ACTIVE
);
2016 vdev_trim_stop_all(root_vdev
, VDEV_TRIM_ACTIVE
);
2017 vdev_autotrim_stop_all(spa
);
2018 vdev_rebuild_stop_all(spa
);
2025 if (spa
->spa_sync_on
) {
2026 txg_sync_stop(spa
->spa_dsl_pool
);
2027 spa
->spa_sync_on
= B_FALSE
;
2031 * This ensures that there is no async metaslab prefetching
2032 * while we attempt to unload the spa.
2034 taskq_wait(spa
->spa_metaslab_taskq
);
2036 if (spa
->spa_mmp
.mmp_thread
)
2037 mmp_thread_stop(spa
);
2040 * Wait for any outstanding async I/O to complete.
2042 if (spa
->spa_async_zio_root
!= NULL
) {
2043 for (int i
= 0; i
< max_ncpus
; i
++)
2044 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
2045 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
2046 spa
->spa_async_zio_root
= NULL
;
2049 if (spa
->spa_vdev_removal
!= NULL
) {
2050 spa_vdev_removal_destroy(spa
->spa_vdev_removal
);
2051 spa
->spa_vdev_removal
= NULL
;
2054 spa_destroy_aux_threads(spa
);
2056 spa_condense_fini(spa
);
2058 bpobj_close(&spa
->spa_deferred_bpobj
);
2060 spa_config_enter(spa
, SCL_ALL
, spa
, RW_WRITER
);
2065 if (spa
->spa_root_vdev
)
2066 vdev_free(spa
->spa_root_vdev
);
2067 ASSERT(spa
->spa_root_vdev
== NULL
);
2070 * Close the dsl pool.
2072 if (spa
->spa_dsl_pool
) {
2073 dsl_pool_close(spa
->spa_dsl_pool
);
2074 spa
->spa_dsl_pool
= NULL
;
2075 spa
->spa_meta_objset
= NULL
;
2080 spa_unload_log_sm_metadata(spa
);
2083 * Drop and purge level 2 cache
2085 spa_l2cache_drop(spa
);
2087 if (spa
->spa_spares
.sav_vdevs
) {
2088 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
2089 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
2090 kmem_free(spa
->spa_spares
.sav_vdevs
,
2091 spa
->spa_spares
.sav_count
* sizeof (void *));
2092 spa
->spa_spares
.sav_vdevs
= NULL
;
2094 if (spa
->spa_spares
.sav_config
) {
2095 nvlist_free(spa
->spa_spares
.sav_config
);
2096 spa
->spa_spares
.sav_config
= NULL
;
2098 spa
->spa_spares
.sav_count
= 0;
2100 if (spa
->spa_l2cache
.sav_vdevs
) {
2101 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
2102 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
2103 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
2105 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
2106 spa
->spa_l2cache
.sav_count
* sizeof (void *));
2107 spa
->spa_l2cache
.sav_vdevs
= NULL
;
2109 if (spa
->spa_l2cache
.sav_config
) {
2110 nvlist_free(spa
->spa_l2cache
.sav_config
);
2111 spa
->spa_l2cache
.sav_config
= NULL
;
2113 spa
->spa_l2cache
.sav_count
= 0;
2115 spa
->spa_async_suspended
= 0;
2117 spa
->spa_indirect_vdevs_loaded
= B_FALSE
;
2119 if (spa
->spa_comment
!= NULL
) {
2120 spa_strfree(spa
->spa_comment
);
2121 spa
->spa_comment
= NULL
;
2123 if (spa
->spa_compatibility
!= NULL
) {
2124 spa_strfree(spa
->spa_compatibility
);
2125 spa
->spa_compatibility
= NULL
;
2128 spa
->spa_raidz_expand
= NULL
;
2130 spa_config_exit(spa
, SCL_ALL
, spa
);
2134 * Load (or re-load) the current list of vdevs describing the active spares for
2135 * this pool. When this is called, we have some form of basic information in
2136 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
2137 * then re-generate a more complete list including status information.
2140 spa_load_spares(spa_t
*spa
)
2149 * zdb opens both the current state of the pool and the
2150 * checkpointed state (if present), with a different spa_t.
2152 * As spare vdevs are shared among open pools, we skip loading
2153 * them when we load the checkpointed state of the pool.
2155 if (!spa_writeable(spa
))
2159 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2162 * First, close and free any existing spare vdevs.
2164 if (spa
->spa_spares
.sav_vdevs
) {
2165 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
2166 vd
= spa
->spa_spares
.sav_vdevs
[i
];
2168 /* Undo the call to spa_activate() below */
2169 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
2170 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
2171 spa_spare_remove(tvd
);
2176 kmem_free(spa
->spa_spares
.sav_vdevs
,
2177 spa
->spa_spares
.sav_count
* sizeof (void *));
2180 if (spa
->spa_spares
.sav_config
== NULL
)
2183 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
2184 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
2186 spa
->spa_spares
.sav_count
= (int)nspares
;
2187 spa
->spa_spares
.sav_vdevs
= NULL
;
2193 * Construct the array of vdevs, opening them to get status in the
2194 * process. For each spare, there is potentially two different vdev_t
2195 * structures associated with it: one in the list of spares (used only
2196 * for basic validation purposes) and one in the active vdev
2197 * configuration (if it's spared in). During this phase we open and
2198 * validate each vdev on the spare list. If the vdev also exists in the
2199 * active configuration, then we also mark this vdev as an active spare.
2201 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
2203 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
2204 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
2205 VDEV_ALLOC_SPARE
) == 0);
2208 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
2210 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
2211 B_FALSE
)) != NULL
) {
2212 if (!tvd
->vdev_isspare
)
2216 * We only mark the spare active if we were successfully
2217 * able to load the vdev. Otherwise, importing a pool
2218 * with a bad active spare would result in strange
2219 * behavior, because multiple pool would think the spare
2220 * is actively in use.
2222 * There is a vulnerability here to an equally bizarre
2223 * circumstance, where a dead active spare is later
2224 * brought back to life (onlined or otherwise). Given
2225 * the rarity of this scenario, and the extra complexity
2226 * it adds, we ignore the possibility.
2228 if (!vdev_is_dead(tvd
))
2229 spa_spare_activate(tvd
);
2233 vd
->vdev_aux
= &spa
->spa_spares
;
2235 if (vdev_open(vd
) != 0)
2238 if (vdev_validate_aux(vd
) == 0)
2243 * Recompute the stashed list of spares, with status information
2246 fnvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
);
2248 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
2250 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
2251 spares
[i
] = vdev_config_generate(spa
,
2252 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
2253 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
2254 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
2255 spa
->spa_spares
.sav_count
);
2256 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
2257 nvlist_free(spares
[i
]);
2258 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
2262 * Load (or re-load) the current list of vdevs describing the active l2cache for
2263 * this pool. When this is called, we have some form of basic information in
2264 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
2265 * then re-generate a more complete list including status information.
2266 * Devices which are already active have their details maintained, and are
2270 spa_load_l2cache(spa_t
*spa
)
2272 nvlist_t
**l2cache
= NULL
;
2274 int i
, j
, oldnvdevs
;
2276 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
2277 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
2281 * zdb opens both the current state of the pool and the
2282 * checkpointed state (if present), with a different spa_t.
2284 * As L2 caches are part of the ARC which is shared among open
2285 * pools, we skip loading them when we load the checkpointed
2286 * state of the pool.
2288 if (!spa_writeable(spa
))
2292 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
2294 oldvdevs
= sav
->sav_vdevs
;
2295 oldnvdevs
= sav
->sav_count
;
2296 sav
->sav_vdevs
= NULL
;
2299 if (sav
->sav_config
== NULL
) {
2305 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
,
2306 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
2307 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
2310 * Process new nvlist of vdevs.
2312 for (i
= 0; i
< nl2cache
; i
++) {
2313 guid
= fnvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
);
2316 for (j
= 0; j
< oldnvdevs
; j
++) {
2318 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
2320 * Retain previous vdev for add/remove ops.
2328 if (newvdevs
[i
] == NULL
) {
2332 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
2333 VDEV_ALLOC_L2CACHE
) == 0);
2338 * Commit this vdev as an l2cache device,
2339 * even if it fails to open.
2341 spa_l2cache_add(vd
);
2346 spa_l2cache_activate(vd
);
2348 if (vdev_open(vd
) != 0)
2351 (void) vdev_validate_aux(vd
);
2353 if (!vdev_is_dead(vd
))
2354 l2arc_add_vdev(spa
, vd
);
2357 * Upon cache device addition to a pool or pool
2358 * creation with a cache device or if the header
2359 * of the device is invalid we issue an async
2360 * TRIM command for the whole device which will
2361 * execute if l2arc_trim_ahead > 0.
2363 spa_async_request(spa
, SPA_ASYNC_L2CACHE_TRIM
);
2367 sav
->sav_vdevs
= newvdevs
;
2368 sav
->sav_count
= (int)nl2cache
;
2371 * Recompute the stashed list of l2cache devices, with status
2372 * information this time.
2374 fnvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
);
2376 if (sav
->sav_count
> 0)
2377 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *),
2379 for (i
= 0; i
< sav
->sav_count
; i
++)
2380 l2cache
[i
] = vdev_config_generate(spa
,
2381 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
2382 fnvlist_add_nvlist_array(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
2383 (const nvlist_t
* const *)l2cache
, sav
->sav_count
);
2387 * Purge vdevs that were dropped
2390 for (i
= 0; i
< oldnvdevs
; i
++) {
2395 ASSERT(vd
->vdev_isl2cache
);
2397 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
2398 pool
!= 0ULL && l2arc_vdev_present(vd
))
2399 l2arc_remove_vdev(vd
);
2400 vdev_clear_stats(vd
);
2405 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
2408 for (i
= 0; i
< sav
->sav_count
; i
++)
2409 nvlist_free(l2cache
[i
]);
2411 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
2415 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
2418 char *packed
= NULL
;
2423 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
2427 nvsize
= *(uint64_t *)db
->db_data
;
2428 dmu_buf_rele(db
, FTAG
);
2430 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
2431 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
2434 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
2435 vmem_free(packed
, nvsize
);
2441 * Concrete top-level vdevs that are not missing and are not logs. At every
2442 * spa_sync we write new uberblocks to at least SPA_SYNC_MIN_VDEVS core tvds.
2445 spa_healthy_core_tvds(spa_t
*spa
)
2447 vdev_t
*rvd
= spa
->spa_root_vdev
;
2450 for (uint64_t i
= 0; i
< rvd
->vdev_children
; i
++) {
2451 vdev_t
*vd
= rvd
->vdev_child
[i
];
2454 if (vdev_is_concrete(vd
) && !vdev_is_dead(vd
))
2462 * Checks to see if the given vdev could not be opened, in which case we post a
2463 * sysevent to notify the autoreplace code that the device has been removed.
2466 spa_check_removed(vdev_t
*vd
)
2468 for (uint64_t c
= 0; c
< vd
->vdev_children
; c
++)
2469 spa_check_removed(vd
->vdev_child
[c
]);
2471 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
2472 vdev_is_concrete(vd
)) {
2473 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
2474 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_CHECK
);
2479 spa_check_for_missing_logs(spa_t
*spa
)
2481 vdev_t
*rvd
= spa
->spa_root_vdev
;
2484 * If we're doing a normal import, then build up any additional
2485 * diagnostic information about missing log devices.
2486 * We'll pass this up to the user for further processing.
2488 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
2489 nvlist_t
**child
, *nv
;
2492 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
*),
2494 nv
= fnvlist_alloc();
2496 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2497 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2500 * We consider a device as missing only if it failed
2501 * to open (i.e. offline or faulted is not considered
2504 if (tvd
->vdev_islog
&&
2505 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2506 child
[idx
++] = vdev_config_generate(spa
, tvd
,
2507 B_FALSE
, VDEV_CONFIG_MISSING
);
2512 fnvlist_add_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2513 (const nvlist_t
* const *)child
, idx
);
2514 fnvlist_add_nvlist(spa
->spa_load_info
,
2515 ZPOOL_CONFIG_MISSING_DEVICES
, nv
);
2517 for (uint64_t i
= 0; i
< idx
; i
++)
2518 nvlist_free(child
[i
]);
2521 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
2524 spa_load_failed(spa
, "some log devices are missing");
2525 vdev_dbgmsg_print_tree(rvd
, 2);
2526 return (SET_ERROR(ENXIO
));
2529 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
2530 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2532 if (tvd
->vdev_islog
&&
2533 tvd
->vdev_state
== VDEV_STATE_CANT_OPEN
) {
2534 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2535 spa_load_note(spa
, "some log devices are "
2536 "missing, ZIL is dropped.");
2537 vdev_dbgmsg_print_tree(rvd
, 2);
2547 * Check for missing log devices
2550 spa_check_logs(spa_t
*spa
)
2552 boolean_t rv
= B_FALSE
;
2553 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2555 switch (spa
->spa_log_state
) {
2558 case SPA_LOG_MISSING
:
2559 /* need to recheck in case slog has been restored */
2560 case SPA_LOG_UNKNOWN
:
2561 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2562 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
2564 spa_set_log_state(spa
, SPA_LOG_MISSING
);
2571 * Passivate any log vdevs (note, does not apply to embedded log metaslabs).
2574 spa_passivate_log(spa_t
*spa
)
2576 vdev_t
*rvd
= spa
->spa_root_vdev
;
2577 boolean_t slog_found
= B_FALSE
;
2579 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2581 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2582 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2584 if (tvd
->vdev_islog
) {
2585 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2586 metaslab_group_passivate(tvd
->vdev_mg
);
2587 slog_found
= B_TRUE
;
2591 return (slog_found
);
2595 * Activate any log vdevs (note, does not apply to embedded log metaslabs).
2598 spa_activate_log(spa_t
*spa
)
2600 vdev_t
*rvd
= spa
->spa_root_vdev
;
2602 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
2604 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
2605 vdev_t
*tvd
= rvd
->vdev_child
[c
];
2607 if (tvd
->vdev_islog
) {
2608 ASSERT3P(tvd
->vdev_log_mg
, ==, NULL
);
2609 metaslab_group_activate(tvd
->vdev_mg
);
2615 spa_reset_logs(spa_t
*spa
)
2619 error
= dmu_objset_find(spa_name(spa
), zil_reset
,
2620 NULL
, DS_FIND_CHILDREN
);
2623 * We successfully offlined the log device, sync out the
2624 * current txg so that the "stubby" block can be removed
2627 txg_wait_synced(spa
->spa_dsl_pool
, 0);
2633 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
2635 for (int i
= 0; i
< sav
->sav_count
; i
++)
2636 spa_check_removed(sav
->sav_vdevs
[i
]);
2640 spa_claim_notify(zio_t
*zio
)
2642 spa_t
*spa
= zio
->io_spa
;
2647 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
2648 if (spa
->spa_claim_max_txg
< BP_GET_LOGICAL_BIRTH(zio
->io_bp
))
2649 spa
->spa_claim_max_txg
= BP_GET_LOGICAL_BIRTH(zio
->io_bp
);
2650 mutex_exit(&spa
->spa_props_lock
);
2653 typedef struct spa_load_error
{
2654 boolean_t sle_verify_data
;
2655 uint64_t sle_meta_count
;
2656 uint64_t sle_data_count
;
2660 spa_load_verify_done(zio_t
*zio
)
2662 blkptr_t
*bp
= zio
->io_bp
;
2663 spa_load_error_t
*sle
= zio
->io_private
;
2664 dmu_object_type_t type
= BP_GET_TYPE(bp
);
2665 int error
= zio
->io_error
;
2666 spa_t
*spa
= zio
->io_spa
;
2668 abd_free(zio
->io_abd
);
2670 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
2671 type
!= DMU_OT_INTENT_LOG
)
2672 atomic_inc_64(&sle
->sle_meta_count
);
2674 atomic_inc_64(&sle
->sle_data_count
);
2677 mutex_enter(&spa
->spa_scrub_lock
);
2678 spa
->spa_load_verify_bytes
-= BP_GET_PSIZE(bp
);
2679 cv_broadcast(&spa
->spa_scrub_io_cv
);
2680 mutex_exit(&spa
->spa_scrub_lock
);
2684 * Maximum number of inflight bytes is the log2 fraction of the arc size.
2685 * By default, we set it to 1/16th of the arc.
2687 static uint_t spa_load_verify_shift
= 4;
2688 static int spa_load_verify_metadata
= B_TRUE
;
2689 static int spa_load_verify_data
= B_TRUE
;
2692 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
2693 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
2696 spa_load_error_t
*sle
= rio
->io_private
;
2698 (void) zilog
, (void) dnp
;
2701 * Note: normally this routine will not be called if
2702 * spa_load_verify_metadata is not set. However, it may be useful
2703 * to manually set the flag after the traversal has begun.
2705 if (!spa_load_verify_metadata
)
2709 * Sanity check the block pointer in order to detect obvious damage
2710 * before using the contents in subsequent checks or in zio_read().
2711 * When damaged consider it to be a metadata error since we cannot
2712 * trust the BP_GET_TYPE and BP_GET_LEVEL values.
2714 if (!zfs_blkptr_verify(spa
, bp
, BLK_CONFIG_NEEDED
, BLK_VERIFY_LOG
)) {
2715 atomic_inc_64(&sle
->sle_meta_count
);
2719 if (zb
->zb_level
== ZB_DNODE_LEVEL
|| BP_IS_HOLE(bp
) ||
2720 BP_IS_EMBEDDED(bp
) || BP_IS_REDACTED(bp
))
2723 if (!BP_IS_METADATA(bp
) &&
2724 (!spa_load_verify_data
|| !sle
->sle_verify_data
))
2727 uint64_t maxinflight_bytes
=
2728 arc_target_bytes() >> spa_load_verify_shift
;
2729 size_t size
= BP_GET_PSIZE(bp
);
2731 mutex_enter(&spa
->spa_scrub_lock
);
2732 while (spa
->spa_load_verify_bytes
>= maxinflight_bytes
)
2733 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2734 spa
->spa_load_verify_bytes
+= size
;
2735 mutex_exit(&spa
->spa_scrub_lock
);
2737 zio_nowait(zio_read(rio
, spa
, bp
, abd_alloc_for_io(size
, B_FALSE
), size
,
2738 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2739 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2740 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2745 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2747 (void) dp
, (void) arg
;
2749 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2750 return (SET_ERROR(ENAMETOOLONG
));
2756 spa_load_verify(spa_t
*spa
)
2759 spa_load_error_t sle
= { 0 };
2760 zpool_load_policy_t policy
;
2761 boolean_t verify_ok
= B_FALSE
;
2764 zpool_get_load_policy(spa
->spa_config
, &policy
);
2766 if (policy
.zlp_rewind
& ZPOOL_NEVER_REWIND
||
2767 policy
.zlp_maxmeta
== UINT64_MAX
)
2770 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2771 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2772 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2774 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2779 * Verify data only if we are rewinding or error limit was set.
2780 * Otherwise nothing except dbgmsg care about it to waste time.
2782 sle
.sle_verify_data
= (policy
.zlp_rewind
& ZPOOL_REWIND_MASK
) ||
2783 (policy
.zlp_maxdata
< UINT64_MAX
);
2785 rio
= zio_root(spa
, NULL
, &sle
,
2786 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2788 if (spa_load_verify_metadata
) {
2789 if (spa
->spa_extreme_rewind
) {
2790 spa_load_note(spa
, "performing a complete scan of the "
2791 "pool since extreme rewind is on. This may take "
2792 "a very long time.\n (spa_load_verify_data=%u, "
2793 "spa_load_verify_metadata=%u)",
2794 spa_load_verify_data
, spa_load_verify_metadata
);
2797 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2798 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
|
2799 TRAVERSE_NO_DECRYPT
, spa_load_verify_cb
, rio
);
2802 (void) zio_wait(rio
);
2803 ASSERT0(spa
->spa_load_verify_bytes
);
2805 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2806 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2808 if (sle
.sle_meta_count
!= 0 || sle
.sle_data_count
!= 0) {
2809 spa_load_note(spa
, "spa_load_verify found %llu metadata errors "
2810 "and %llu data errors", (u_longlong_t
)sle
.sle_meta_count
,
2811 (u_longlong_t
)sle
.sle_data_count
);
2814 if (spa_load_verify_dryrun
||
2815 (!error
&& sle
.sle_meta_count
<= policy
.zlp_maxmeta
&&
2816 sle
.sle_data_count
<= policy
.zlp_maxdata
)) {
2820 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2821 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2823 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2824 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_LOAD_TIME
,
2825 spa
->spa_load_txg_ts
);
2826 fnvlist_add_int64(spa
->spa_load_info
, ZPOOL_CONFIG_REWIND_TIME
,
2828 fnvlist_add_uint64(spa
->spa_load_info
,
2829 ZPOOL_CONFIG_LOAD_META_ERRORS
, sle
.sle_meta_count
);
2830 fnvlist_add_uint64(spa
->spa_load_info
,
2831 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
);
2833 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2836 if (spa_load_verify_dryrun
)
2840 if (error
!= ENXIO
&& error
!= EIO
)
2841 error
= SET_ERROR(EIO
);
2845 return (verify_ok
? 0 : EIO
);
2849 * Find a value in the pool props object.
2852 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2854 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2855 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2859 * Find a value in the pool directory object.
2862 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
, boolean_t log_enoent
)
2864 int error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2865 name
, sizeof (uint64_t), 1, val
);
2867 if (error
!= 0 && (error
!= ENOENT
|| log_enoent
)) {
2868 spa_load_failed(spa
, "couldn't get '%s' value in MOS directory "
2869 "[error=%d]", name
, error
);
2876 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2878 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2879 return (SET_ERROR(err
));
2883 spa_livelist_delete_check(spa_t
*spa
)
2885 return (spa
->spa_livelists_to_delete
!= 0);
2889 spa_livelist_delete_cb_check(void *arg
, zthr_t
*z
)
2893 return (spa_livelist_delete_check(spa
));
2897 delete_blkptr_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
2900 zio_free(spa
, tx
->tx_txg
, bp
);
2901 dsl_dir_diduse_space(tx
->tx_pool
->dp_free_dir
, DD_USED_HEAD
,
2902 -bp_get_dsize_sync(spa
, bp
),
2903 -BP_GET_PSIZE(bp
), -BP_GET_UCSIZE(bp
), tx
);
2908 dsl_get_next_livelist_obj(objset_t
*os
, uint64_t zap_obj
, uint64_t *llp
)
2913 zap_cursor_init(&zc
, os
, zap_obj
);
2914 err
= zap_cursor_retrieve(&zc
, &za
);
2915 zap_cursor_fini(&zc
);
2917 *llp
= za
.za_first_integer
;
2922 * Components of livelist deletion that must be performed in syncing
2923 * context: freeing block pointers and updating the pool-wide data
2924 * structures to indicate how much work is left to do
2926 typedef struct sublist_delete_arg
{
2931 } sublist_delete_arg_t
;
2934 sublist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2936 sublist_delete_arg_t
*sda
= arg
;
2937 spa_t
*spa
= sda
->spa
;
2938 dsl_deadlist_t
*ll
= sda
->ll
;
2939 uint64_t key
= sda
->key
;
2940 bplist_t
*to_free
= sda
->to_free
;
2942 bplist_iterate(to_free
, delete_blkptr_cb
, spa
, tx
);
2943 dsl_deadlist_remove_entry(ll
, key
, tx
);
2946 typedef struct livelist_delete_arg
{
2950 } livelist_delete_arg_t
;
2953 livelist_delete_sync(void *arg
, dmu_tx_t
*tx
)
2955 livelist_delete_arg_t
*lda
= arg
;
2956 spa_t
*spa
= lda
->spa
;
2957 uint64_t ll_obj
= lda
->ll_obj
;
2958 uint64_t zap_obj
= lda
->zap_obj
;
2959 objset_t
*mos
= spa
->spa_meta_objset
;
2962 /* free the livelist and decrement the feature count */
2963 VERIFY0(zap_remove_int(mos
, zap_obj
, ll_obj
, tx
));
2964 dsl_deadlist_free(mos
, ll_obj
, tx
);
2965 spa_feature_decr(spa
, SPA_FEATURE_LIVELIST
, tx
);
2966 VERIFY0(zap_count(mos
, zap_obj
, &count
));
2968 /* no more livelists to delete */
2969 VERIFY0(zap_remove(mos
, DMU_POOL_DIRECTORY_OBJECT
,
2970 DMU_POOL_DELETED_CLONES
, tx
));
2971 VERIFY0(zap_destroy(mos
, zap_obj
, tx
));
2972 spa
->spa_livelists_to_delete
= 0;
2973 spa_notify_waiters(spa
);
2978 * Load in the value for the livelist to be removed and open it. Then,
2979 * load its first sublist and determine which block pointers should actually
2980 * be freed. Then, call a synctask which performs the actual frees and updates
2981 * the pool-wide livelist data.
2984 spa_livelist_delete_cb(void *arg
, zthr_t
*z
)
2987 uint64_t ll_obj
= 0, count
;
2988 objset_t
*mos
= spa
->spa_meta_objset
;
2989 uint64_t zap_obj
= spa
->spa_livelists_to_delete
;
2991 * Determine the next livelist to delete. This function should only
2992 * be called if there is at least one deleted clone.
2994 VERIFY0(dsl_get_next_livelist_obj(mos
, zap_obj
, &ll_obj
));
2995 VERIFY0(zap_count(mos
, ll_obj
, &count
));
2998 dsl_deadlist_entry_t
*dle
;
3000 ll
= kmem_zalloc(sizeof (dsl_deadlist_t
), KM_SLEEP
);
3001 dsl_deadlist_open(ll
, mos
, ll_obj
);
3002 dle
= dsl_deadlist_first(ll
);
3003 ASSERT3P(dle
, !=, NULL
);
3004 bplist_create(&to_free
);
3005 int err
= dsl_process_sub_livelist(&dle
->dle_bpobj
, &to_free
,
3008 sublist_delete_arg_t sync_arg
= {
3011 .key
= dle
->dle_mintxg
,
3014 zfs_dbgmsg("deleting sublist (id %llu) from"
3015 " livelist %llu, %lld remaining",
3016 (u_longlong_t
)dle
->dle_bpobj
.bpo_object
,
3017 (u_longlong_t
)ll_obj
, (longlong_t
)count
- 1);
3018 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
,
3019 sublist_delete_sync
, &sync_arg
, 0,
3020 ZFS_SPACE_CHECK_DESTROY
));
3022 VERIFY3U(err
, ==, EINTR
);
3024 bplist_clear(&to_free
);
3025 bplist_destroy(&to_free
);
3026 dsl_deadlist_close(ll
);
3027 kmem_free(ll
, sizeof (dsl_deadlist_t
));
3029 livelist_delete_arg_t sync_arg
= {
3034 zfs_dbgmsg("deletion of livelist %llu completed",
3035 (u_longlong_t
)ll_obj
);
3036 VERIFY0(dsl_sync_task(spa_name(spa
), NULL
, livelist_delete_sync
,
3037 &sync_arg
, 0, ZFS_SPACE_CHECK_DESTROY
));
3042 spa_start_livelist_destroy_thread(spa_t
*spa
)
3044 ASSERT3P(spa
->spa_livelist_delete_zthr
, ==, NULL
);
3045 spa
->spa_livelist_delete_zthr
=
3046 zthr_create("z_livelist_destroy",
3047 spa_livelist_delete_cb_check
, spa_livelist_delete_cb
, spa
,
3051 typedef struct livelist_new_arg
{
3054 } livelist_new_arg_t
;
3057 livelist_track_new_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
3061 livelist_new_arg_t
*lna
= arg
;
3063 bplist_append(lna
->frees
, bp
);
3065 bplist_append(lna
->allocs
, bp
);
3066 zfs_livelist_condense_new_alloc
++;
3071 typedef struct livelist_condense_arg
{
3074 uint64_t first_size
;
3076 } livelist_condense_arg_t
;
3079 spa_livelist_condense_sync(void *arg
, dmu_tx_t
*tx
)
3081 livelist_condense_arg_t
*lca
= arg
;
3082 spa_t
*spa
= lca
->spa
;
3084 dsl_dataset_t
*ds
= spa
->spa_to_condense
.ds
;
3086 /* Have we been cancelled? */
3087 if (spa
->spa_to_condense
.cancelled
) {
3088 zfs_livelist_condense_sync_cancel
++;
3092 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
3093 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
3094 dsl_deadlist_t
*ll
= &ds
->ds_dir
->dd_livelist
;
3097 * It's possible that the livelist was changed while the zthr was
3098 * running. Therefore, we need to check for new blkptrs in the two
3099 * entries being condensed and continue to track them in the livelist.
3100 * Because of the way we handle remapped blkptrs (see dbuf_remap_impl),
3101 * it's possible that the newly added blkptrs are FREEs or ALLOCs so
3102 * we need to sort them into two different bplists.
3104 uint64_t first_obj
= first
->dle_bpobj
.bpo_object
;
3105 uint64_t next_obj
= next
->dle_bpobj
.bpo_object
;
3106 uint64_t cur_first_size
= first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
3107 uint64_t cur_next_size
= next
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
;
3109 bplist_create(&new_frees
);
3110 livelist_new_arg_t new_bps
= {
3111 .allocs
= &lca
->to_keep
,
3112 .frees
= &new_frees
,
3115 if (cur_first_size
> lca
->first_size
) {
3116 VERIFY0(livelist_bpobj_iterate_from_nofree(&first
->dle_bpobj
,
3117 livelist_track_new_cb
, &new_bps
, lca
->first_size
));
3119 if (cur_next_size
> lca
->next_size
) {
3120 VERIFY0(livelist_bpobj_iterate_from_nofree(&next
->dle_bpobj
,
3121 livelist_track_new_cb
, &new_bps
, lca
->next_size
));
3124 dsl_deadlist_clear_entry(first
, ll
, tx
);
3125 ASSERT(bpobj_is_empty(&first
->dle_bpobj
));
3126 dsl_deadlist_remove_entry(ll
, next
->dle_mintxg
, tx
);
3128 bplist_iterate(&lca
->to_keep
, dsl_deadlist_insert_alloc_cb
, ll
, tx
);
3129 bplist_iterate(&new_frees
, dsl_deadlist_insert_free_cb
, ll
, tx
);
3130 bplist_destroy(&new_frees
);
3132 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
3133 dsl_dataset_name(ds
, dsname
);
3134 zfs_dbgmsg("txg %llu condensing livelist of %s (id %llu), bpobj %llu "
3135 "(%llu blkptrs) and bpobj %llu (%llu blkptrs) -> bpobj %llu "
3136 "(%llu blkptrs)", (u_longlong_t
)tx
->tx_txg
, dsname
,
3137 (u_longlong_t
)ds
->ds_object
, (u_longlong_t
)first_obj
,
3138 (u_longlong_t
)cur_first_size
, (u_longlong_t
)next_obj
,
3139 (u_longlong_t
)cur_next_size
,
3140 (u_longlong_t
)first
->dle_bpobj
.bpo_object
,
3141 (u_longlong_t
)first
->dle_bpobj
.bpo_phys
->bpo_num_blkptrs
);
3143 dmu_buf_rele(ds
->ds_dbuf
, spa
);
3144 spa
->spa_to_condense
.ds
= NULL
;
3145 bplist_clear(&lca
->to_keep
);
3146 bplist_destroy(&lca
->to_keep
);
3147 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
3148 spa
->spa_to_condense
.syncing
= B_FALSE
;
3152 spa_livelist_condense_cb(void *arg
, zthr_t
*t
)
3154 while (zfs_livelist_condense_zthr_pause
&&
3155 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
3159 dsl_deadlist_entry_t
*first
= spa
->spa_to_condense
.first
;
3160 dsl_deadlist_entry_t
*next
= spa
->spa_to_condense
.next
;
3161 uint64_t first_size
, next_size
;
3163 livelist_condense_arg_t
*lca
=
3164 kmem_alloc(sizeof (livelist_condense_arg_t
), KM_SLEEP
);
3165 bplist_create(&lca
->to_keep
);
3168 * Process the livelists (matching FREEs and ALLOCs) in open context
3169 * so we have minimal work in syncing context to condense.
3171 * We save bpobj sizes (first_size and next_size) to use later in
3172 * syncing context to determine if entries were added to these sublists
3173 * while in open context. This is possible because the clone is still
3174 * active and open for normal writes and we want to make sure the new,
3175 * unprocessed blockpointers are inserted into the livelist normally.
3177 * Note that dsl_process_sub_livelist() both stores the size number of
3178 * blockpointers and iterates over them while the bpobj's lock held, so
3179 * the sizes returned to us are consistent which what was actually
3182 int err
= dsl_process_sub_livelist(&first
->dle_bpobj
, &lca
->to_keep
, t
,
3185 err
= dsl_process_sub_livelist(&next
->dle_bpobj
, &lca
->to_keep
,
3189 while (zfs_livelist_condense_sync_pause
&&
3190 !(zthr_has_waiters(t
) || zthr_iscancelled(t
)))
3193 dmu_tx_t
*tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
3194 dmu_tx_mark_netfree(tx
);
3195 dmu_tx_hold_space(tx
, 1);
3196 err
= dmu_tx_assign(tx
, TXG_NOWAIT
| TXG_NOTHROTTLE
);
3199 * Prevent the condense zthr restarting before
3200 * the synctask completes.
3202 spa
->spa_to_condense
.syncing
= B_TRUE
;
3204 lca
->first_size
= first_size
;
3205 lca
->next_size
= next_size
;
3206 dsl_sync_task_nowait(spa_get_dsl(spa
),
3207 spa_livelist_condense_sync
, lca
, tx
);
3213 * Condensing can not continue: either it was externally stopped or
3214 * we were unable to assign to a tx because the pool has run out of
3215 * space. In the second case, we'll just end up trying to condense
3216 * again in a later txg.
3219 bplist_clear(&lca
->to_keep
);
3220 bplist_destroy(&lca
->to_keep
);
3221 kmem_free(lca
, sizeof (livelist_condense_arg_t
));
3222 dmu_buf_rele(spa
->spa_to_condense
.ds
->ds_dbuf
, spa
);
3223 spa
->spa_to_condense
.ds
= NULL
;
3225 zfs_livelist_condense_zthr_cancel
++;
3229 * Check that there is something to condense but that a condense is not
3230 * already in progress and that condensing has not been cancelled.
3233 spa_livelist_condense_cb_check(void *arg
, zthr_t
*z
)
3237 if ((spa
->spa_to_condense
.ds
!= NULL
) &&
3238 (spa
->spa_to_condense
.syncing
== B_FALSE
) &&
3239 (spa
->spa_to_condense
.cancelled
== B_FALSE
)) {
3246 spa_start_livelist_condensing_thread(spa_t
*spa
)
3248 spa
->spa_to_condense
.ds
= NULL
;
3249 spa
->spa_to_condense
.first
= NULL
;
3250 spa
->spa_to_condense
.next
= NULL
;
3251 spa
->spa_to_condense
.syncing
= B_FALSE
;
3252 spa
->spa_to_condense
.cancelled
= B_FALSE
;
3254 ASSERT3P(spa
->spa_livelist_condense_zthr
, ==, NULL
);
3255 spa
->spa_livelist_condense_zthr
=
3256 zthr_create("z_livelist_condense",
3257 spa_livelist_condense_cb_check
,
3258 spa_livelist_condense_cb
, spa
, minclsyspri
);
3262 spa_spawn_aux_threads(spa_t
*spa
)
3264 ASSERT(spa_writeable(spa
));
3266 spa_start_raidz_expansion_thread(spa
);
3267 spa_start_indirect_condensing_thread(spa
);
3268 spa_start_livelist_destroy_thread(spa
);
3269 spa_start_livelist_condensing_thread(spa
);
3271 ASSERT3P(spa
->spa_checkpoint_discard_zthr
, ==, NULL
);
3272 spa
->spa_checkpoint_discard_zthr
=
3273 zthr_create("z_checkpoint_discard",
3274 spa_checkpoint_discard_thread_check
,
3275 spa_checkpoint_discard_thread
, spa
, minclsyspri
);
3279 * Fix up config after a partly-completed split. This is done with the
3280 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
3281 * pool have that entry in their config, but only the splitting one contains
3282 * a list of all the guids of the vdevs that are being split off.
3284 * This function determines what to do with that list: either rejoin
3285 * all the disks to the pool, or complete the splitting process. To attempt
3286 * the rejoin, each disk that is offlined is marked online again, and
3287 * we do a reopen() call. If the vdev label for every disk that was
3288 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
3289 * then we call vdev_split() on each disk, and complete the split.
3291 * Otherwise we leave the config alone, with all the vdevs in place in
3292 * the original pool.
3295 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
3302 boolean_t attempt_reopen
;
3304 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
3307 /* check that the config is complete */
3308 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
3309 &glist
, &gcount
) != 0)
3312 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
3314 /* attempt to online all the vdevs & validate */
3315 attempt_reopen
= B_TRUE
;
3316 for (i
= 0; i
< gcount
; i
++) {
3317 if (glist
[i
] == 0) /* vdev is hole */
3320 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
3321 if (vd
[i
] == NULL
) {
3323 * Don't bother attempting to reopen the disks;
3324 * just do the split.
3326 attempt_reopen
= B_FALSE
;
3328 /* attempt to re-online it */
3329 vd
[i
]->vdev_offline
= B_FALSE
;
3333 if (attempt_reopen
) {
3334 vdev_reopen(spa
->spa_root_vdev
);
3336 /* check each device to see what state it's in */
3337 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
3338 if (vd
[i
] != NULL
&&
3339 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
3346 * If every disk has been moved to the new pool, or if we never
3347 * even attempted to look at them, then we split them off for
3350 if (!attempt_reopen
|| gcount
== extracted
) {
3351 for (i
= 0; i
< gcount
; i
++)
3354 vdev_reopen(spa
->spa_root_vdev
);
3357 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
3361 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
)
3363 const char *ereport
= FM_EREPORT_ZFS_POOL
;
3366 spa
->spa_load_state
= state
;
3367 (void) spa_import_progress_set_state(spa_guid(spa
),
3368 spa_load_state(spa
));
3369 spa_import_progress_set_notes(spa
, "spa_load()");
3371 gethrestime(&spa
->spa_loaded_ts
);
3372 error
= spa_load_impl(spa
, type
, &ereport
);
3375 * Don't count references from objsets that are already closed
3376 * and are making their way through the eviction process.
3378 spa_evicting_os_wait(spa
);
3379 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
3381 if (error
!= EEXIST
) {
3382 spa
->spa_loaded_ts
.tv_sec
= 0;
3383 spa
->spa_loaded_ts
.tv_nsec
= 0;
3385 if (error
!= EBADF
) {
3386 (void) zfs_ereport_post(ereport
, spa
,
3387 NULL
, NULL
, NULL
, 0);
3390 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
3393 (void) spa_import_progress_set_state(spa_guid(spa
),
3394 spa_load_state(spa
));
3401 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
3402 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
3403 * spa's per-vdev ZAP list.
3406 vdev_count_verify_zaps(vdev_t
*vd
)
3408 spa_t
*spa
= vd
->vdev_spa
;
3411 if (spa_feature_is_active(vd
->vdev_spa
, SPA_FEATURE_AVZ_V2
) &&
3412 vd
->vdev_root_zap
!= 0) {
3414 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3415 spa
->spa_all_vdev_zaps
, vd
->vdev_root_zap
));
3417 if (vd
->vdev_top_zap
!= 0) {
3419 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3420 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
3422 if (vd
->vdev_leaf_zap
!= 0) {
3424 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
3425 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
3428 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
3429 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
3435 #define vdev_count_verify_zaps(vd) ((void) sizeof (vd), 0)
3439 * Determine whether the activity check is required.
3442 spa_activity_check_required(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*label
,
3446 uint64_t hostid
= 0;
3447 uint64_t tryconfig_txg
= 0;
3448 uint64_t tryconfig_timestamp
= 0;
3449 uint16_t tryconfig_mmp_seq
= 0;
3452 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3453 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
3454 (void) nvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
,
3456 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
3457 &tryconfig_timestamp
);
3458 (void) nvlist_lookup_uint16(nvinfo
, ZPOOL_CONFIG_MMP_SEQ
,
3459 &tryconfig_mmp_seq
);
3462 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, &state
);
3465 * Disable the MMP activity check - This is used by zdb which
3466 * is intended to be used on potentially active pools.
3468 if (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
)
3472 * Skip the activity check when the MMP feature is disabled.
3474 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
== 0)
3478 * If the tryconfig_ values are nonzero, they are the results of an
3479 * earlier tryimport. If they all match the uberblock we just found,
3480 * then the pool has not changed and we return false so we do not test
3483 if (tryconfig_txg
&& tryconfig_txg
== ub
->ub_txg
&&
3484 tryconfig_timestamp
&& tryconfig_timestamp
== ub
->ub_timestamp
&&
3485 tryconfig_mmp_seq
&& tryconfig_mmp_seq
==
3486 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0))
3490 * Allow the activity check to be skipped when importing the pool
3491 * on the same host which last imported it. Since the hostid from
3492 * configuration may be stale use the one read from the label.
3494 if (nvlist_exists(label
, ZPOOL_CONFIG_HOSTID
))
3495 hostid
= fnvlist_lookup_uint64(label
, ZPOOL_CONFIG_HOSTID
);
3497 if (hostid
== spa_get_hostid(spa
))
3501 * Skip the activity test when the pool was cleanly exported.
3503 if (state
!= POOL_STATE_ACTIVE
)
3510 * Nanoseconds the activity check must watch for changes on-disk.
3513 spa_activity_check_duration(spa_t
*spa
, uberblock_t
*ub
)
3515 uint64_t import_intervals
= MAX(zfs_multihost_import_intervals
, 1);
3516 uint64_t multihost_interval
= MSEC2NSEC(
3517 MMP_INTERVAL_OK(zfs_multihost_interval
));
3518 uint64_t import_delay
= MAX(NANOSEC
, import_intervals
*
3519 multihost_interval
);
3522 * Local tunables determine a minimum duration except for the case
3523 * where we know when the remote host will suspend the pool if MMP
3524 * writes do not land.
3526 * See Big Theory comment at the top of mmp.c for the reasoning behind
3527 * these cases and times.
3530 ASSERT(MMP_IMPORT_SAFETY_FACTOR
>= 100);
3532 if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3533 MMP_FAIL_INT(ub
) > 0) {
3535 /* MMP on remote host will suspend pool after failed writes */
3536 import_delay
= MMP_FAIL_INT(ub
) * MSEC2NSEC(MMP_INTERVAL(ub
)) *
3537 MMP_IMPORT_SAFETY_FACTOR
/ 100;
3539 zfs_dbgmsg("fail_intvals>0 import_delay=%llu ub_mmp "
3540 "mmp_fails=%llu ub_mmp mmp_interval=%llu "
3541 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3542 (u_longlong_t
)MMP_FAIL_INT(ub
),
3543 (u_longlong_t
)MMP_INTERVAL(ub
),
3544 (u_longlong_t
)import_intervals
);
3546 } else if (MMP_INTERVAL_VALID(ub
) && MMP_FAIL_INT_VALID(ub
) &&
3547 MMP_FAIL_INT(ub
) == 0) {
3549 /* MMP on remote host will never suspend pool */
3550 import_delay
= MAX(import_delay
, (MSEC2NSEC(MMP_INTERVAL(ub
)) +
3551 ub
->ub_mmp_delay
) * import_intervals
);
3553 zfs_dbgmsg("fail_intvals=0 import_delay=%llu ub_mmp "
3554 "mmp_interval=%llu ub_mmp_delay=%llu "
3555 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3556 (u_longlong_t
)MMP_INTERVAL(ub
),
3557 (u_longlong_t
)ub
->ub_mmp_delay
,
3558 (u_longlong_t
)import_intervals
);
3560 } else if (MMP_VALID(ub
)) {
3562 * zfs-0.7 compatibility case
3565 import_delay
= MAX(import_delay
, (multihost_interval
+
3566 ub
->ub_mmp_delay
) * import_intervals
);
3568 zfs_dbgmsg("import_delay=%llu ub_mmp_delay=%llu "
3569 "import_intervals=%llu leaves=%u",
3570 (u_longlong_t
)import_delay
,
3571 (u_longlong_t
)ub
->ub_mmp_delay
,
3572 (u_longlong_t
)import_intervals
,
3573 vdev_count_leaves(spa
));
3575 /* Using local tunings is the only reasonable option */
3576 zfs_dbgmsg("pool last imported on non-MMP aware "
3577 "host using import_delay=%llu multihost_interval=%llu "
3578 "import_intervals=%llu", (u_longlong_t
)import_delay
,
3579 (u_longlong_t
)multihost_interval
,
3580 (u_longlong_t
)import_intervals
);
3583 return (import_delay
);
3587 * Remote host activity check.
3590 * 0 - no activity detected
3591 * EREMOTEIO - remote activity detected
3592 * EINTR - user canceled the operation
3595 spa_activity_check(spa_t
*spa
, uberblock_t
*ub
, nvlist_t
*config
,
3596 boolean_t importing
)
3598 uint64_t txg
= ub
->ub_txg
;
3599 uint64_t timestamp
= ub
->ub_timestamp
;
3600 uint64_t mmp_config
= ub
->ub_mmp_config
;
3601 uint16_t mmp_seq
= MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0;
3602 uint64_t import_delay
;
3603 hrtime_t import_expire
, now
;
3604 nvlist_t
*mmp_label
= NULL
;
3605 vdev_t
*rvd
= spa
->spa_root_vdev
;
3610 cv_init(&cv
, NULL
, CV_DEFAULT
, NULL
);
3611 mutex_init(&mtx
, NULL
, MUTEX_DEFAULT
, NULL
);
3615 * If ZPOOL_CONFIG_MMP_TXG is present an activity check was performed
3616 * during the earlier tryimport. If the txg recorded there is 0 then
3617 * the pool is known to be active on another host.
3619 * Otherwise, the pool might be in use on another host. Check for
3620 * changes in the uberblocks on disk if necessary.
3622 if (nvlist_exists(config
, ZPOOL_CONFIG_LOAD_INFO
)) {
3623 nvlist_t
*nvinfo
= fnvlist_lookup_nvlist(config
,
3624 ZPOOL_CONFIG_LOAD_INFO
);
3626 if (nvlist_exists(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) &&
3627 fnvlist_lookup_uint64(nvinfo
, ZPOOL_CONFIG_MMP_TXG
) == 0) {
3628 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3629 error
= SET_ERROR(EREMOTEIO
);
3634 import_delay
= spa_activity_check_duration(spa
, ub
);
3636 /* Add a small random factor in case of simultaneous imports (0-25%) */
3637 import_delay
+= import_delay
* random_in_range(250) / 1000;
3639 import_expire
= gethrtime() + import_delay
;
3642 spa_import_progress_set_notes(spa
, "Checking MMP activity, "
3643 "waiting %llu ms", (u_longlong_t
)NSEC2MSEC(import_delay
));
3647 while ((now
= gethrtime()) < import_expire
) {
3648 if (importing
&& iterations
++ % 30 == 0) {
3649 spa_import_progress_set_notes(spa
, "Checking MMP "
3650 "activity, %llu ms remaining",
3651 (u_longlong_t
)NSEC2MSEC(import_expire
- now
));
3655 (void) spa_import_progress_set_mmp_check(spa_guid(spa
),
3656 NSEC2SEC(import_expire
- gethrtime()));
3659 vdev_uberblock_load(rvd
, ub
, &mmp_label
);
3661 if (txg
!= ub
->ub_txg
|| timestamp
!= ub
->ub_timestamp
||
3662 mmp_seq
!= (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0)) {
3663 zfs_dbgmsg("multihost activity detected "
3664 "txg %llu ub_txg %llu "
3665 "timestamp %llu ub_timestamp %llu "
3666 "mmp_config %#llx ub_mmp_config %#llx",
3667 (u_longlong_t
)txg
, (u_longlong_t
)ub
->ub_txg
,
3668 (u_longlong_t
)timestamp
,
3669 (u_longlong_t
)ub
->ub_timestamp
,
3670 (u_longlong_t
)mmp_config
,
3671 (u_longlong_t
)ub
->ub_mmp_config
);
3673 error
= SET_ERROR(EREMOTEIO
);
3678 nvlist_free(mmp_label
);
3682 error
= cv_timedwait_sig(&cv
, &mtx
, ddi_get_lbolt() + hz
);
3684 error
= SET_ERROR(EINTR
);
3692 mutex_destroy(&mtx
);
3696 * If the pool is determined to be active store the status in the
3697 * spa->spa_load_info nvlist. If the remote hostname or hostid are
3698 * available from configuration read from disk store them as well.
3699 * This allows 'zpool import' to generate a more useful message.
3701 * ZPOOL_CONFIG_MMP_STATE - observed pool status (mandatory)
3702 * ZPOOL_CONFIG_MMP_HOSTNAME - hostname from the active pool
3703 * ZPOOL_CONFIG_MMP_HOSTID - hostid from the active pool
3705 if (error
== EREMOTEIO
) {
3706 const char *hostname
= "<unknown>";
3707 uint64_t hostid
= 0;
3710 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTNAME
)) {
3711 hostname
= fnvlist_lookup_string(mmp_label
,
3712 ZPOOL_CONFIG_HOSTNAME
);
3713 fnvlist_add_string(spa
->spa_load_info
,
3714 ZPOOL_CONFIG_MMP_HOSTNAME
, hostname
);
3717 if (nvlist_exists(mmp_label
, ZPOOL_CONFIG_HOSTID
)) {
3718 hostid
= fnvlist_lookup_uint64(mmp_label
,
3719 ZPOOL_CONFIG_HOSTID
);
3720 fnvlist_add_uint64(spa
->spa_load_info
,
3721 ZPOOL_CONFIG_MMP_HOSTID
, hostid
);
3725 fnvlist_add_uint64(spa
->spa_load_info
,
3726 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_ACTIVE
);
3727 fnvlist_add_uint64(spa
->spa_load_info
,
3728 ZPOOL_CONFIG_MMP_TXG
, 0);
3730 error
= spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
);
3734 nvlist_free(mmp_label
);
3740 * Called from zfs_ioc_clear for a pool that was suspended
3741 * after failing mmp write checks.
3744 spa_mmp_remote_host_activity(spa_t
*spa
)
3746 ASSERT(spa_multihost(spa
) && spa_suspended(spa
));
3748 nvlist_t
*best_label
;
3749 uberblock_t best_ub
;
3752 * Locate the best uberblock on disk
3754 vdev_uberblock_load(spa
->spa_root_vdev
, &best_ub
, &best_label
);
3757 * confirm that the best hostid matches our hostid
3759 if (nvlist_exists(best_label
, ZPOOL_CONFIG_HOSTID
) &&
3760 spa_get_hostid(spa
) !=
3761 fnvlist_lookup_uint64(best_label
, ZPOOL_CONFIG_HOSTID
)) {
3762 nvlist_free(best_label
);
3765 nvlist_free(best_label
);
3770 if (!MMP_VALID(&best_ub
) ||
3771 !MMP_FAIL_INT_VALID(&best_ub
) ||
3772 MMP_FAIL_INT(&best_ub
) == 0) {
3776 if (best_ub
.ub_txg
!= spa
->spa_uberblock
.ub_txg
||
3777 best_ub
.ub_timestamp
!= spa
->spa_uberblock
.ub_timestamp
) {
3778 zfs_dbgmsg("txg mismatch detected during pool clear "
3779 "txg %llu ub_txg %llu timestamp %llu ub_timestamp %llu",
3780 (u_longlong_t
)spa
->spa_uberblock
.ub_txg
,
3781 (u_longlong_t
)best_ub
.ub_txg
,
3782 (u_longlong_t
)spa
->spa_uberblock
.ub_timestamp
,
3783 (u_longlong_t
)best_ub
.ub_timestamp
);
3788 * Perform an activity check looking for any remote writer
3790 return (spa_activity_check(spa
, &spa
->spa_uberblock
, spa
->spa_config
,
3795 spa_verify_host(spa_t
*spa
, nvlist_t
*mos_config
)
3798 const char *hostname
;
3799 uint64_t myhostid
= 0;
3801 if (!spa_is_root(spa
) && nvlist_lookup_uint64(mos_config
,
3802 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
3803 hostname
= fnvlist_lookup_string(mos_config
,
3804 ZPOOL_CONFIG_HOSTNAME
);
3806 myhostid
= zone_get_hostid(NULL
);
3808 if (hostid
!= 0 && myhostid
!= 0 && hostid
!= myhostid
) {
3809 cmn_err(CE_WARN
, "pool '%s' could not be "
3810 "loaded as it was last accessed by "
3811 "another system (host: %s hostid: 0x%llx). "
3812 "See: https://openzfs.github.io/openzfs-docs/msg/"
3814 spa_name(spa
), hostname
, (u_longlong_t
)hostid
);
3815 spa_load_failed(spa
, "hostid verification failed: pool "
3816 "last accessed by host: %s (hostid: 0x%llx)",
3817 hostname
, (u_longlong_t
)hostid
);
3818 return (SET_ERROR(EBADF
));
3826 spa_ld_parse_config(spa_t
*spa
, spa_import_type_t type
)
3829 nvlist_t
*nvtree
, *nvl
, *config
= spa
->spa_config
;
3833 const char *comment
;
3834 const char *compatibility
;
3837 * Versioning wasn't explicitly added to the label until later, so if
3838 * it's not present treat it as the initial version.
3840 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
3841 &spa
->spa_ubsync
.ub_version
) != 0)
3842 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
3844 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
)) {
3845 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3846 ZPOOL_CONFIG_POOL_GUID
);
3847 return (SET_ERROR(EINVAL
));
3851 * If we are doing an import, ensure that the pool is not already
3852 * imported by checking if its pool guid already exists in the
3855 * The only case that we allow an already imported pool to be
3856 * imported again, is when the pool is checkpointed and we want to
3857 * look at its checkpointed state from userland tools like zdb.
3860 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3861 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3862 spa_guid_exists(pool_guid
, 0)) {
3864 if ((spa
->spa_load_state
== SPA_LOAD_IMPORT
||
3865 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) &&
3866 spa_guid_exists(pool_guid
, 0) &&
3867 !spa_importing_readonly_checkpoint(spa
)) {
3869 spa_load_failed(spa
, "a pool with guid %llu is already open",
3870 (u_longlong_t
)pool_guid
);
3871 return (SET_ERROR(EEXIST
));
3874 spa
->spa_config_guid
= pool_guid
;
3876 nvlist_free(spa
->spa_load_info
);
3877 spa
->spa_load_info
= fnvlist_alloc();
3879 ASSERT(spa
->spa_comment
== NULL
);
3880 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
3881 spa
->spa_comment
= spa_strdup(comment
);
3883 ASSERT(spa
->spa_compatibility
== NULL
);
3884 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMPATIBILITY
,
3885 &compatibility
) == 0)
3886 spa
->spa_compatibility
= spa_strdup(compatibility
);
3888 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
3889 &spa
->spa_config_txg
);
3891 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) == 0)
3892 spa
->spa_config_splitting
= fnvlist_dup(nvl
);
3894 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvtree
)) {
3895 spa_load_failed(spa
, "invalid config provided: '%s' missing",
3896 ZPOOL_CONFIG_VDEV_TREE
);
3897 return (SET_ERROR(EINVAL
));
3901 * Create "The Godfather" zio to hold all async IOs
3903 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3905 for (int i
= 0; i
< max_ncpus
; i
++) {
3906 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3907 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3908 ZIO_FLAG_GODFATHER
);
3912 * Parse the configuration into a vdev tree. We explicitly set the
3913 * value that will be returned by spa_version() since parsing the
3914 * configuration requires knowing the version number.
3916 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3917 parse
= (type
== SPA_IMPORT_EXISTING
?
3918 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
3919 error
= spa_config_parse(spa
, &rvd
, nvtree
, NULL
, 0, parse
);
3920 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3923 spa_load_failed(spa
, "unable to parse config [error=%d]",
3928 ASSERT(spa
->spa_root_vdev
== rvd
);
3929 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
3930 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
3932 if (type
!= SPA_IMPORT_ASSEMBLE
) {
3933 ASSERT(spa_guid(spa
) == pool_guid
);
3940 * Recursively open all vdevs in the vdev tree. This function is called twice:
3941 * first with the untrusted config, then with the trusted config.
3944 spa_ld_open_vdevs(spa_t
*spa
)
3949 * spa_missing_tvds_allowed defines how many top-level vdevs can be
3950 * missing/unopenable for the root vdev to be still considered openable.
3952 if (spa
->spa_trust_config
) {
3953 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds
;
3954 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_CACHEFILE
) {
3955 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_cachefile
;
3956 } else if (spa
->spa_config_source
== SPA_CONFIG_SRC_SCAN
) {
3957 spa
->spa_missing_tvds_allowed
= zfs_max_missing_tvds_scan
;
3959 spa
->spa_missing_tvds_allowed
= 0;
3962 spa
->spa_missing_tvds_allowed
=
3963 MAX(zfs_max_missing_tvds
, spa
->spa_missing_tvds_allowed
);
3965 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3966 error
= vdev_open(spa
->spa_root_vdev
);
3967 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3969 if (spa
->spa_missing_tvds
!= 0) {
3970 spa_load_note(spa
, "vdev tree has %lld missing top-level "
3971 "vdevs.", (u_longlong_t
)spa
->spa_missing_tvds
);
3972 if (spa
->spa_trust_config
&& (spa
->spa_mode
& SPA_MODE_WRITE
)) {
3974 * Although theoretically we could allow users to open
3975 * incomplete pools in RW mode, we'd need to add a lot
3976 * of extra logic (e.g. adjust pool space to account
3977 * for missing vdevs).
3978 * This limitation also prevents users from accidentally
3979 * opening the pool in RW mode during data recovery and
3980 * damaging it further.
3982 spa_load_note(spa
, "pools with missing top-level "
3983 "vdevs can only be opened in read-only mode.");
3984 error
= SET_ERROR(ENXIO
);
3986 spa_load_note(spa
, "current settings allow for maximum "
3987 "%lld missing top-level vdevs at this stage.",
3988 (u_longlong_t
)spa
->spa_missing_tvds_allowed
);
3992 spa_load_failed(spa
, "unable to open vdev tree [error=%d]",
3995 if (spa
->spa_missing_tvds
!= 0 || error
!= 0)
3996 vdev_dbgmsg_print_tree(spa
->spa_root_vdev
, 2);
4002 * We need to validate the vdev labels against the configuration that
4003 * we have in hand. This function is called twice: first with an untrusted
4004 * config, then with a trusted config. The validation is more strict when the
4005 * config is trusted.
4008 spa_ld_validate_vdevs(spa_t
*spa
)
4011 vdev_t
*rvd
= spa
->spa_root_vdev
;
4013 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4014 error
= vdev_validate(rvd
);
4015 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4018 spa_load_failed(spa
, "vdev_validate failed [error=%d]", error
);
4022 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
) {
4023 spa_load_failed(spa
, "cannot open vdev tree after invalidating "
4025 vdev_dbgmsg_print_tree(rvd
, 2);
4026 return (SET_ERROR(ENXIO
));
4033 spa_ld_select_uberblock_done(spa_t
*spa
, uberblock_t
*ub
)
4035 spa
->spa_state
= POOL_STATE_ACTIVE
;
4036 spa
->spa_ubsync
= spa
->spa_uberblock
;
4037 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
4038 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
4039 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
4040 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
4041 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
4042 spa
->spa_prev_software_version
= ub
->ub_software_version
;
4046 spa_ld_select_uberblock(spa_t
*spa
, spa_import_type_t type
)
4048 vdev_t
*rvd
= spa
->spa_root_vdev
;
4050 uberblock_t
*ub
= &spa
->spa_uberblock
;
4051 boolean_t activity_check
= B_FALSE
;
4054 * If we are opening the checkpointed state of the pool by
4055 * rewinding to it, at this point we will have written the
4056 * checkpointed uberblock to the vdev labels, so searching
4057 * the labels will find the right uberblock. However, if
4058 * we are opening the checkpointed state read-only, we have
4059 * not modified the labels. Therefore, we must ignore the
4060 * labels and continue using the spa_uberblock that was set
4061 * by spa_ld_checkpoint_rewind.
4063 * Note that it would be fine to ignore the labels when
4064 * rewinding (opening writeable) as well. However, if we
4065 * crash just after writing the labels, we will end up
4066 * searching the labels. Doing so in the common case means
4067 * that this code path gets exercised normally, rather than
4068 * just in the edge case.
4070 if (ub
->ub_checkpoint_txg
!= 0 &&
4071 spa_importing_readonly_checkpoint(spa
)) {
4072 spa_ld_select_uberblock_done(spa
, ub
);
4077 * Find the best uberblock.
4079 vdev_uberblock_load(rvd
, ub
, &label
);
4082 * If we weren't able to find a single valid uberblock, return failure.
4084 if (ub
->ub_txg
== 0) {
4086 spa_load_failed(spa
, "no valid uberblock found");
4087 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
4090 if (spa
->spa_load_max_txg
!= UINT64_MAX
) {
4091 (void) spa_import_progress_set_max_txg(spa_guid(spa
),
4092 (u_longlong_t
)spa
->spa_load_max_txg
);
4094 spa_load_note(spa
, "using uberblock with txg=%llu",
4095 (u_longlong_t
)ub
->ub_txg
);
4096 if (ub
->ub_raidz_reflow_info
!= 0) {
4097 spa_load_note(spa
, "uberblock raidz_reflow_info: "
4098 "state=%u offset=%llu",
4099 (int)RRSS_GET_STATE(ub
),
4100 (u_longlong_t
)RRSS_GET_OFFSET(ub
));
4105 * For pools which have the multihost property on determine if the
4106 * pool is truly inactive and can be safely imported. Prevent
4107 * hosts which don't have a hostid set from importing the pool.
4109 activity_check
= spa_activity_check_required(spa
, ub
, label
,
4111 if (activity_check
) {
4112 if (ub
->ub_mmp_magic
== MMP_MAGIC
&& ub
->ub_mmp_delay
&&
4113 spa_get_hostid(spa
) == 0) {
4115 fnvlist_add_uint64(spa
->spa_load_info
,
4116 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4117 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4121 spa_activity_check(spa
, ub
, spa
->spa_config
, B_TRUE
);
4127 fnvlist_add_uint64(spa
->spa_load_info
,
4128 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_INACTIVE
);
4129 fnvlist_add_uint64(spa
->spa_load_info
,
4130 ZPOOL_CONFIG_MMP_TXG
, ub
->ub_txg
);
4131 fnvlist_add_uint16(spa
->spa_load_info
,
4132 ZPOOL_CONFIG_MMP_SEQ
,
4133 (MMP_SEQ_VALID(ub
) ? MMP_SEQ(ub
) : 0));
4137 * If the pool has an unsupported version we can't open it.
4139 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
4141 spa_load_failed(spa
, "version %llu is not supported",
4142 (u_longlong_t
)ub
->ub_version
);
4143 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
4146 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
4150 * If we weren't able to find what's necessary for reading the
4151 * MOS in the label, return failure.
4153 if (label
== NULL
) {
4154 spa_load_failed(spa
, "label config unavailable");
4155 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4159 if (nvlist_lookup_nvlist(label
, ZPOOL_CONFIG_FEATURES_FOR_READ
,
4162 spa_load_failed(spa
, "invalid label: '%s' missing",
4163 ZPOOL_CONFIG_FEATURES_FOR_READ
);
4164 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4169 * Update our in-core representation with the definitive values
4172 nvlist_free(spa
->spa_label_features
);
4173 spa
->spa_label_features
= fnvlist_dup(features
);
4179 * Look through entries in the label nvlist's features_for_read. If
4180 * there is a feature listed there which we don't understand then we
4181 * cannot open a pool.
4183 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
4184 nvlist_t
*unsup_feat
;
4186 unsup_feat
= fnvlist_alloc();
4188 for (nvpair_t
*nvp
= nvlist_next_nvpair(spa
->spa_label_features
,
4190 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
4191 if (!zfeature_is_supported(nvpair_name(nvp
))) {
4192 fnvlist_add_string(unsup_feat
,
4193 nvpair_name(nvp
), "");
4197 if (!nvlist_empty(unsup_feat
)) {
4198 fnvlist_add_nvlist(spa
->spa_load_info
,
4199 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4200 nvlist_free(unsup_feat
);
4201 spa_load_failed(spa
, "some features are unsupported");
4202 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4206 nvlist_free(unsup_feat
);
4209 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
4210 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4211 spa_try_repair(spa
, spa
->spa_config
);
4212 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4213 nvlist_free(spa
->spa_config_splitting
);
4214 spa
->spa_config_splitting
= NULL
;
4218 * Initialize internal SPA structures.
4220 spa_ld_select_uberblock_done(spa
, ub
);
4226 spa_ld_open_rootbp(spa_t
*spa
)
4229 vdev_t
*rvd
= spa
->spa_root_vdev
;
4231 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
4233 spa_load_failed(spa
, "unable to open rootbp in dsl_pool_init "
4234 "[error=%d]", error
);
4235 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4237 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
4243 spa_ld_trusted_config(spa_t
*spa
, spa_import_type_t type
,
4244 boolean_t reloading
)
4246 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
4247 nvlist_t
*nv
, *mos_config
, *policy
;
4248 int error
= 0, copy_error
;
4249 uint64_t healthy_tvds
, healthy_tvds_mos
;
4250 uint64_t mos_config_txg
;
4252 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
, B_TRUE
)
4254 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4257 * If we're assembling a pool from a split, the config provided is
4258 * already trusted so there is nothing to do.
4260 if (type
== SPA_IMPORT_ASSEMBLE
)
4263 healthy_tvds
= spa_healthy_core_tvds(spa
);
4265 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
)
4267 spa_load_failed(spa
, "unable to retrieve MOS config");
4268 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4272 * If we are doing an open, pool owner wasn't verified yet, thus do
4273 * the verification here.
4275 if (spa
->spa_load_state
== SPA_LOAD_OPEN
) {
4276 error
= spa_verify_host(spa
, mos_config
);
4278 nvlist_free(mos_config
);
4283 nv
= fnvlist_lookup_nvlist(mos_config
, ZPOOL_CONFIG_VDEV_TREE
);
4285 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4288 * Build a new vdev tree from the trusted config
4290 error
= spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
);
4292 nvlist_free(mos_config
);
4293 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4294 spa_load_failed(spa
, "spa_config_parse failed [error=%d]",
4296 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4300 * Vdev paths in the MOS may be obsolete. If the untrusted config was
4301 * obtained by scanning /dev/dsk, then it will have the right vdev
4302 * paths. We update the trusted MOS config with this information.
4303 * We first try to copy the paths with vdev_copy_path_strict, which
4304 * succeeds only when both configs have exactly the same vdev tree.
4305 * If that fails, we fall back to a more flexible method that has a
4306 * best effort policy.
4308 copy_error
= vdev_copy_path_strict(rvd
, mrvd
);
4309 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
4310 spa_load_note(spa
, "provided vdev tree:");
4311 vdev_dbgmsg_print_tree(rvd
, 2);
4312 spa_load_note(spa
, "MOS vdev tree:");
4313 vdev_dbgmsg_print_tree(mrvd
, 2);
4315 if (copy_error
!= 0) {
4316 spa_load_note(spa
, "vdev_copy_path_strict failed, falling "
4317 "back to vdev_copy_path_relaxed");
4318 vdev_copy_path_relaxed(rvd
, mrvd
);
4323 spa
->spa_root_vdev
= mrvd
;
4325 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4328 * If 'zpool import' used a cached config, then the on-disk hostid and
4329 * hostname may be different to the cached config in ways that should
4330 * prevent import. Userspace can't discover this without a scan, but
4331 * we know, so we add these values to LOAD_INFO so the caller can know
4334 * Note that we have to do this before the config is regenerated,
4335 * because the new config will have the hostid and hostname for this
4336 * host, in readiness for import.
4338 if (nvlist_exists(mos_config
, ZPOOL_CONFIG_HOSTID
))
4339 fnvlist_add_uint64(spa
->spa_load_info
, ZPOOL_CONFIG_HOSTID
,
4340 fnvlist_lookup_uint64(mos_config
, ZPOOL_CONFIG_HOSTID
));
4341 if (nvlist_exists(mos_config
, ZPOOL_CONFIG_HOSTNAME
))
4342 fnvlist_add_string(spa
->spa_load_info
, ZPOOL_CONFIG_HOSTNAME
,
4343 fnvlist_lookup_string(mos_config
, ZPOOL_CONFIG_HOSTNAME
));
4346 * We will use spa_config if we decide to reload the spa or if spa_load
4347 * fails and we rewind. We must thus regenerate the config using the
4348 * MOS information with the updated paths. ZPOOL_LOAD_POLICY is used to
4349 * pass settings on how to load the pool and is not stored in the MOS.
4350 * We copy it over to our new, trusted config.
4352 mos_config_txg
= fnvlist_lookup_uint64(mos_config
,
4353 ZPOOL_CONFIG_POOL_TXG
);
4354 nvlist_free(mos_config
);
4355 mos_config
= spa_config_generate(spa
, NULL
, mos_config_txg
, B_FALSE
);
4356 if (nvlist_lookup_nvlist(spa
->spa_config
, ZPOOL_LOAD_POLICY
,
4358 fnvlist_add_nvlist(mos_config
, ZPOOL_LOAD_POLICY
, policy
);
4359 spa_config_set(spa
, mos_config
);
4360 spa
->spa_config_source
= SPA_CONFIG_SRC_MOS
;
4363 * Now that we got the config from the MOS, we should be more strict
4364 * in checking blkptrs and can make assumptions about the consistency
4365 * of the vdev tree. spa_trust_config must be set to true before opening
4366 * vdevs in order for them to be writeable.
4368 spa
->spa_trust_config
= B_TRUE
;
4371 * Open and validate the new vdev tree
4373 error
= spa_ld_open_vdevs(spa
);
4377 error
= spa_ld_validate_vdevs(spa
);
4381 if (copy_error
!= 0 || spa_load_print_vdev_tree
) {
4382 spa_load_note(spa
, "final vdev tree:");
4383 vdev_dbgmsg_print_tree(rvd
, 2);
4386 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
&&
4387 !spa
->spa_extreme_rewind
&& zfs_max_missing_tvds
== 0) {
4389 * Sanity check to make sure that we are indeed loading the
4390 * latest uberblock. If we missed SPA_SYNC_MIN_VDEVS tvds
4391 * in the config provided and they happened to be the only ones
4392 * to have the latest uberblock, we could involuntarily perform
4393 * an extreme rewind.
4395 healthy_tvds_mos
= spa_healthy_core_tvds(spa
);
4396 if (healthy_tvds_mos
- healthy_tvds
>=
4397 SPA_SYNC_MIN_VDEVS
) {
4398 spa_load_note(spa
, "config provided misses too many "
4399 "top-level vdevs compared to MOS (%lld vs %lld). ",
4400 (u_longlong_t
)healthy_tvds
,
4401 (u_longlong_t
)healthy_tvds_mos
);
4402 spa_load_note(spa
, "vdev tree:");
4403 vdev_dbgmsg_print_tree(rvd
, 2);
4405 spa_load_failed(spa
, "config was already "
4406 "provided from MOS. Aborting.");
4407 return (spa_vdev_err(rvd
,
4408 VDEV_AUX_CORRUPT_DATA
, EIO
));
4410 spa_load_note(spa
, "spa must be reloaded using MOS "
4412 return (SET_ERROR(EAGAIN
));
4416 error
= spa_check_for_missing_logs(spa
);
4418 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
4420 if (rvd
->vdev_guid_sum
!= spa
->spa_uberblock
.ub_guid_sum
) {
4421 spa_load_failed(spa
, "uberblock guid sum doesn't match MOS "
4422 "guid sum (%llu != %llu)",
4423 (u_longlong_t
)spa
->spa_uberblock
.ub_guid_sum
,
4424 (u_longlong_t
)rvd
->vdev_guid_sum
);
4425 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
4433 spa_ld_open_indirect_vdev_metadata(spa_t
*spa
)
4436 vdev_t
*rvd
= spa
->spa_root_vdev
;
4439 * Everything that we read before spa_remove_init() must be stored
4440 * on concreted vdevs. Therefore we do this as early as possible.
4442 error
= spa_remove_init(spa
);
4444 spa_load_failed(spa
, "spa_remove_init failed [error=%d]",
4446 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4450 * Retrieve information needed to condense indirect vdev mappings.
4452 error
= spa_condense_init(spa
);
4454 spa_load_failed(spa
, "spa_condense_init failed [error=%d]",
4456 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4463 spa_ld_check_features(spa_t
*spa
, boolean_t
*missing_feat_writep
)
4466 vdev_t
*rvd
= spa
->spa_root_vdev
;
4468 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
4469 boolean_t missing_feat_read
= B_FALSE
;
4470 nvlist_t
*unsup_feat
, *enabled_feat
;
4472 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
4473 &spa
->spa_feat_for_read_obj
, B_TRUE
) != 0) {
4474 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4477 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
4478 &spa
->spa_feat_for_write_obj
, B_TRUE
) != 0) {
4479 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4482 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
4483 &spa
->spa_feat_desc_obj
, B_TRUE
) != 0) {
4484 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4487 enabled_feat
= fnvlist_alloc();
4488 unsup_feat
= fnvlist_alloc();
4490 if (!spa_features_check(spa
, B_FALSE
,
4491 unsup_feat
, enabled_feat
))
4492 missing_feat_read
= B_TRUE
;
4494 if (spa_writeable(spa
) ||
4495 spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
) {
4496 if (!spa_features_check(spa
, B_TRUE
,
4497 unsup_feat
, enabled_feat
)) {
4498 *missing_feat_writep
= B_TRUE
;
4502 fnvlist_add_nvlist(spa
->spa_load_info
,
4503 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
4505 if (!nvlist_empty(unsup_feat
)) {
4506 fnvlist_add_nvlist(spa
->spa_load_info
,
4507 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
4510 fnvlist_free(enabled_feat
);
4511 fnvlist_free(unsup_feat
);
4513 if (!missing_feat_read
) {
4514 fnvlist_add_boolean(spa
->spa_load_info
,
4515 ZPOOL_CONFIG_CAN_RDONLY
);
4519 * If the state is SPA_LOAD_TRYIMPORT, our objective is
4520 * twofold: to determine whether the pool is available for
4521 * import in read-write mode and (if it is not) whether the
4522 * pool is available for import in read-only mode. If the pool
4523 * is available for import in read-write mode, it is displayed
4524 * as available in userland; if it is not available for import
4525 * in read-only mode, it is displayed as unavailable in
4526 * userland. If the pool is available for import in read-only
4527 * mode but not read-write mode, it is displayed as unavailable
4528 * in userland with a special note that the pool is actually
4529 * available for open in read-only mode.
4531 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
4532 * missing a feature for write, we must first determine whether
4533 * the pool can be opened read-only before returning to
4534 * userland in order to know whether to display the
4535 * abovementioned note.
4537 if (missing_feat_read
|| (*missing_feat_writep
&&
4538 spa_writeable(spa
))) {
4539 spa_load_failed(spa
, "pool uses unsupported features");
4540 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
4545 * Load refcounts for ZFS features from disk into an in-memory
4546 * cache during SPA initialization.
4548 for (spa_feature_t i
= 0; i
< SPA_FEATURES
; i
++) {
4551 error
= feature_get_refcount_from_disk(spa
,
4552 &spa_feature_table
[i
], &refcount
);
4554 spa
->spa_feat_refcount_cache
[i
] = refcount
;
4555 } else if (error
== ENOTSUP
) {
4556 spa
->spa_feat_refcount_cache
[i
] =
4557 SPA_FEATURE_DISABLED
;
4559 spa_load_failed(spa
, "error getting refcount "
4560 "for feature %s [error=%d]",
4561 spa_feature_table
[i
].fi_guid
, error
);
4562 return (spa_vdev_err(rvd
,
4563 VDEV_AUX_CORRUPT_DATA
, EIO
));
4568 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
4569 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
4570 &spa
->spa_feat_enabled_txg_obj
, B_TRUE
) != 0)
4571 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4575 * Encryption was added before bookmark_v2, even though bookmark_v2
4576 * is now a dependency. If this pool has encryption enabled without
4577 * bookmark_v2, trigger an errata message.
4579 if (spa_feature_is_enabled(spa
, SPA_FEATURE_ENCRYPTION
) &&
4580 !spa_feature_is_enabled(spa
, SPA_FEATURE_BOOKMARK_V2
)) {
4581 spa
->spa_errata
= ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
;
4588 spa_ld_load_special_directories(spa_t
*spa
)
4591 vdev_t
*rvd
= spa
->spa_root_vdev
;
4593 spa
->spa_is_initializing
= B_TRUE
;
4594 error
= dsl_pool_open(spa
->spa_dsl_pool
);
4595 spa
->spa_is_initializing
= B_FALSE
;
4597 spa_load_failed(spa
, "dsl_pool_open failed [error=%d]", error
);
4598 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4605 spa_ld_get_props(spa_t
*spa
)
4609 vdev_t
*rvd
= spa
->spa_root_vdev
;
4611 /* Grab the checksum salt from the MOS. */
4612 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
4613 DMU_POOL_CHECKSUM_SALT
, 1,
4614 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
4615 spa
->spa_cksum_salt
.zcs_bytes
);
4616 if (error
== ENOENT
) {
4617 /* Generate a new salt for subsequent use */
4618 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
4619 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
4620 } else if (error
!= 0) {
4621 spa_load_failed(spa
, "unable to retrieve checksum salt from "
4622 "MOS [error=%d]", error
);
4623 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4626 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
, B_TRUE
) != 0)
4627 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4628 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
4630 spa_load_failed(spa
, "error opening deferred-frees bpobj "
4631 "[error=%d]", error
);
4632 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4636 * Load the bit that tells us to use the new accounting function
4637 * (raid-z deflation). If we have an older pool, this will not
4640 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
, B_FALSE
);
4641 if (error
!= 0 && error
!= ENOENT
)
4642 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4644 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
4645 &spa
->spa_creation_version
, B_FALSE
);
4646 if (error
!= 0 && error
!= ENOENT
)
4647 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4650 * Load the persistent error log. If we have an older pool, this will
4653 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
,
4655 if (error
!= 0 && error
!= ENOENT
)
4656 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4658 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
4659 &spa
->spa_errlog_scrub
, B_FALSE
);
4660 if (error
!= 0 && error
!= ENOENT
)
4661 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4664 * Load the livelist deletion field. If a livelist is queued for
4665 * deletion, indicate that in the spa
4667 error
= spa_dir_prop(spa
, DMU_POOL_DELETED_CLONES
,
4668 &spa
->spa_livelists_to_delete
, B_FALSE
);
4669 if (error
!= 0 && error
!= ENOENT
)
4670 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4673 * Load the history object. If we have an older pool, this
4674 * will not be present.
4676 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
, B_FALSE
);
4677 if (error
!= 0 && error
!= ENOENT
)
4678 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4681 * Load the per-vdev ZAP map. If we have an older pool, this will not
4682 * be present; in this case, defer its creation to a later time to
4683 * avoid dirtying the MOS this early / out of sync context. See
4684 * spa_sync_config_object.
4687 /* The sentinel is only available in the MOS config. */
4688 nvlist_t
*mos_config
;
4689 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0) {
4690 spa_load_failed(spa
, "unable to retrieve MOS config");
4691 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4694 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
4695 &spa
->spa_all_vdev_zaps
, B_FALSE
);
4697 if (error
== ENOENT
) {
4698 VERIFY(!nvlist_exists(mos_config
,
4699 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
4700 spa
->spa_avz_action
= AVZ_ACTION_INITIALIZE
;
4701 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4702 } else if (error
!= 0) {
4703 nvlist_free(mos_config
);
4704 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4705 } else if (!nvlist_exists(mos_config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
4707 * An older version of ZFS overwrote the sentinel value, so
4708 * we have orphaned per-vdev ZAPs in the MOS. Defer their
4709 * destruction to later; see spa_sync_config_object.
4711 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
4713 * We're assuming that no vdevs have had their ZAPs created
4714 * before this. Better be sure of it.
4716 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
4718 nvlist_free(mos_config
);
4720 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
4722 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
,
4724 if (error
&& error
!= ENOENT
)
4725 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4728 uint64_t autoreplace
= 0;
4730 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
4731 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
4732 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
4733 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
4734 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
4735 spa_prop_find(spa
, ZPOOL_PROP_MULTIHOST
, &spa
->spa_multihost
);
4736 spa_prop_find(spa
, ZPOOL_PROP_AUTOTRIM
, &spa
->spa_autotrim
);
4737 spa
->spa_autoreplace
= (autoreplace
!= 0);
4741 * If we are importing a pool with missing top-level vdevs,
4742 * we enforce that the pool doesn't panic or get suspended on
4743 * error since the likelihood of missing data is extremely high.
4745 if (spa
->spa_missing_tvds
> 0 &&
4746 spa
->spa_failmode
!= ZIO_FAILURE_MODE_CONTINUE
&&
4747 spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4748 spa_load_note(spa
, "forcing failmode to 'continue' "
4749 "as some top level vdevs are missing");
4750 spa
->spa_failmode
= ZIO_FAILURE_MODE_CONTINUE
;
4757 spa_ld_open_aux_vdevs(spa_t
*spa
, spa_import_type_t type
)
4760 vdev_t
*rvd
= spa
->spa_root_vdev
;
4763 * If we're assembling the pool from the split-off vdevs of
4764 * an existing pool, we don't want to attach the spares & cache
4769 * Load any hot spares for this pool.
4771 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
,
4773 if (error
!= 0 && error
!= ENOENT
)
4774 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4775 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4776 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
4777 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
4778 &spa
->spa_spares
.sav_config
) != 0) {
4779 spa_load_failed(spa
, "error loading spares nvlist");
4780 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4783 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4784 spa_load_spares(spa
);
4785 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4786 } else if (error
== 0) {
4787 spa
->spa_spares
.sav_sync
= B_TRUE
;
4791 * Load any level 2 ARC devices for this pool.
4793 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
4794 &spa
->spa_l2cache
.sav_object
, B_FALSE
);
4795 if (error
!= 0 && error
!= ENOENT
)
4796 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4797 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
4798 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
4799 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
4800 &spa
->spa_l2cache
.sav_config
) != 0) {
4801 spa_load_failed(spa
, "error loading l2cache nvlist");
4802 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4805 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4806 spa_load_l2cache(spa
);
4807 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4808 } else if (error
== 0) {
4809 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4816 spa_ld_load_vdev_metadata(spa_t
*spa
)
4819 vdev_t
*rvd
= spa
->spa_root_vdev
;
4822 * If the 'multihost' property is set, then never allow a pool to
4823 * be imported when the system hostid is zero. The exception to
4824 * this rule is zdb which is always allowed to access pools.
4826 if (spa_multihost(spa
) && spa_get_hostid(spa
) == 0 &&
4827 (spa
->spa_import_flags
& ZFS_IMPORT_SKIP_MMP
) == 0) {
4828 fnvlist_add_uint64(spa
->spa_load_info
,
4829 ZPOOL_CONFIG_MMP_STATE
, MMP_STATE_NO_HOSTID
);
4830 return (spa_vdev_err(rvd
, VDEV_AUX_ACTIVE
, EREMOTEIO
));
4834 * If the 'autoreplace' property is set, then post a resource notifying
4835 * the ZFS DE that it should not issue any faults for unopenable
4836 * devices. We also iterate over the vdevs, and post a sysevent for any
4837 * unopenable vdevs so that the normal autoreplace handler can take
4840 if (spa
->spa_autoreplace
&& spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4841 spa_check_removed(spa
->spa_root_vdev
);
4843 * For the import case, this is done in spa_import(), because
4844 * at this point we're using the spare definitions from
4845 * the MOS config, not necessarily from the userland config.
4847 if (spa
->spa_load_state
!= SPA_LOAD_IMPORT
) {
4848 spa_aux_check_removed(&spa
->spa_spares
);
4849 spa_aux_check_removed(&spa
->spa_l2cache
);
4854 * Load the vdev metadata such as metaslabs, DTLs, spacemap object, etc.
4856 error
= vdev_load(rvd
);
4858 spa_load_failed(spa
, "vdev_load failed [error=%d]", error
);
4859 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4862 error
= spa_ld_log_spacemaps(spa
);
4864 spa_load_failed(spa
, "spa_ld_log_spacemaps failed [error=%d]",
4866 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, error
));
4870 * Propagate the leaf DTLs we just loaded all the way up the vdev tree.
4872 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4873 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
, B_FALSE
);
4874 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4880 spa_ld_load_dedup_tables(spa_t
*spa
)
4883 vdev_t
*rvd
= spa
->spa_root_vdev
;
4885 error
= ddt_load(spa
);
4887 spa_load_failed(spa
, "ddt_load failed [error=%d]", error
);
4888 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4895 spa_ld_load_brt(spa_t
*spa
)
4898 vdev_t
*rvd
= spa
->spa_root_vdev
;
4900 error
= brt_load(spa
);
4902 spa_load_failed(spa
, "brt_load failed [error=%d]", error
);
4903 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
4910 spa_ld_verify_logs(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
4912 vdev_t
*rvd
= spa
->spa_root_vdev
;
4914 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa_writeable(spa
)) {
4915 boolean_t missing
= spa_check_logs(spa
);
4917 if (spa
->spa_missing_tvds
!= 0) {
4918 spa_load_note(spa
, "spa_check_logs failed "
4919 "so dropping the logs");
4921 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
4922 spa_load_failed(spa
, "spa_check_logs failed");
4923 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
,
4933 spa_ld_verify_pool_data(spa_t
*spa
)
4936 vdev_t
*rvd
= spa
->spa_root_vdev
;
4939 * We've successfully opened the pool, verify that we're ready
4940 * to start pushing transactions.
4942 if (spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
) {
4943 error
= spa_load_verify(spa
);
4945 spa_load_failed(spa
, "spa_load_verify failed "
4946 "[error=%d]", error
);
4947 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
4956 spa_ld_claim_log_blocks(spa_t
*spa
)
4959 dsl_pool_t
*dp
= spa_get_dsl(spa
);
4962 * Claim log blocks that haven't been committed yet.
4963 * This must all happen in a single txg.
4964 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
4965 * invoked from zil_claim_log_block()'s i/o done callback.
4966 * Price of rollback is that we abandon the log.
4968 spa
->spa_claiming
= B_TRUE
;
4970 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
4971 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
4972 zil_claim
, tx
, DS_FIND_CHILDREN
);
4975 spa
->spa_claiming
= B_FALSE
;
4977 spa_set_log_state(spa
, SPA_LOG_GOOD
);
4981 spa_ld_check_for_config_update(spa_t
*spa
, uint64_t config_cache_txg
,
4982 boolean_t update_config_cache
)
4984 vdev_t
*rvd
= spa
->spa_root_vdev
;
4985 int need_update
= B_FALSE
;
4988 * If the config cache is stale, or we have uninitialized
4989 * metaslabs (see spa_vdev_add()), then update the config.
4991 * If this is a verbatim import, trust the current
4992 * in-core spa_config and update the disk labels.
4994 if (update_config_cache
|| config_cache_txg
!= spa
->spa_config_txg
||
4995 spa
->spa_load_state
== SPA_LOAD_IMPORT
||
4996 spa
->spa_load_state
== SPA_LOAD_RECOVER
||
4997 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
4998 need_update
= B_TRUE
;
5000 for (int c
= 0; c
< rvd
->vdev_children
; c
++)
5001 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
5002 need_update
= B_TRUE
;
5005 * Update the config cache asynchronously in case we're the
5006 * root pool, in which case the config cache isn't writable yet.
5009 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
5013 spa_ld_prepare_for_reload(spa_t
*spa
)
5015 spa_mode_t mode
= spa
->spa_mode
;
5016 int async_suspended
= spa
->spa_async_suspended
;
5019 spa_deactivate(spa
);
5020 spa_activate(spa
, mode
);
5023 * We save the value of spa_async_suspended as it gets reset to 0 by
5024 * spa_unload(). We want to restore it back to the original value before
5025 * returning as we might be calling spa_async_resume() later.
5027 spa
->spa_async_suspended
= async_suspended
;
5031 spa_ld_read_checkpoint_txg(spa_t
*spa
)
5033 uberblock_t checkpoint
;
5036 ASSERT0(spa
->spa_checkpoint_txg
);
5037 ASSERT(MUTEX_HELD(&spa_namespace_lock
) ||
5038 spa
->spa_load_thread
== curthread
);
5040 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
5041 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
5042 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
5044 if (error
== ENOENT
)
5050 ASSERT3U(checkpoint
.ub_txg
, !=, 0);
5051 ASSERT3U(checkpoint
.ub_checkpoint_txg
, !=, 0);
5052 ASSERT3U(checkpoint
.ub_timestamp
, !=, 0);
5053 spa
->spa_checkpoint_txg
= checkpoint
.ub_txg
;
5054 spa
->spa_checkpoint_info
.sci_timestamp
= checkpoint
.ub_timestamp
;
5060 spa_ld_mos_init(spa_t
*spa
, spa_import_type_t type
)
5064 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5065 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
5068 * Never trust the config that is provided unless we are assembling
5069 * a pool following a split.
5070 * This means don't trust blkptrs and the vdev tree in general. This
5071 * also effectively puts the spa in read-only mode since
5072 * spa_writeable() checks for spa_trust_config to be true.
5073 * We will later load a trusted config from the MOS.
5075 if (type
!= SPA_IMPORT_ASSEMBLE
)
5076 spa
->spa_trust_config
= B_FALSE
;
5079 * Parse the config provided to create a vdev tree.
5081 error
= spa_ld_parse_config(spa
, type
);
5085 spa_import_progress_add(spa
);
5088 * Now that we have the vdev tree, try to open each vdev. This involves
5089 * opening the underlying physical device, retrieving its geometry and
5090 * probing the vdev with a dummy I/O. The state of each vdev will be set
5091 * based on the success of those operations. After this we'll be ready
5092 * to read from the vdevs.
5094 error
= spa_ld_open_vdevs(spa
);
5099 * Read the label of each vdev and make sure that the GUIDs stored
5100 * there match the GUIDs in the config provided.
5101 * If we're assembling a new pool that's been split off from an
5102 * existing pool, the labels haven't yet been updated so we skip
5103 * validation for now.
5105 if (type
!= SPA_IMPORT_ASSEMBLE
) {
5106 error
= spa_ld_validate_vdevs(spa
);
5112 * Read all vdev labels to find the best uberblock (i.e. latest,
5113 * unless spa_load_max_txg is set) and store it in spa_uberblock. We
5114 * get the list of features required to read blkptrs in the MOS from
5115 * the vdev label with the best uberblock and verify that our version
5116 * of zfs supports them all.
5118 error
= spa_ld_select_uberblock(spa
, type
);
5123 * Pass that uberblock to the dsl_pool layer which will open the root
5124 * blkptr. This blkptr points to the latest version of the MOS and will
5125 * allow us to read its contents.
5127 error
= spa_ld_open_rootbp(spa
);
5135 spa_ld_checkpoint_rewind(spa_t
*spa
)
5137 uberblock_t checkpoint
;
5140 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5141 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5143 error
= zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
5144 DMU_POOL_ZPOOL_CHECKPOINT
, sizeof (uint64_t),
5145 sizeof (uberblock_t
) / sizeof (uint64_t), &checkpoint
);
5148 spa_load_failed(spa
, "unable to retrieve checkpointed "
5149 "uberblock from the MOS config [error=%d]", error
);
5151 if (error
== ENOENT
)
5152 error
= ZFS_ERR_NO_CHECKPOINT
;
5157 ASSERT3U(checkpoint
.ub_txg
, <, spa
->spa_uberblock
.ub_txg
);
5158 ASSERT3U(checkpoint
.ub_txg
, ==, checkpoint
.ub_checkpoint_txg
);
5161 * We need to update the txg and timestamp of the checkpointed
5162 * uberblock to be higher than the latest one. This ensures that
5163 * the checkpointed uberblock is selected if we were to close and
5164 * reopen the pool right after we've written it in the vdev labels.
5165 * (also see block comment in vdev_uberblock_compare)
5167 checkpoint
.ub_txg
= spa
->spa_uberblock
.ub_txg
+ 1;
5168 checkpoint
.ub_timestamp
= gethrestime_sec();
5171 * Set current uberblock to be the checkpointed uberblock.
5173 spa
->spa_uberblock
= checkpoint
;
5176 * If we are doing a normal rewind, then the pool is open for
5177 * writing and we sync the "updated" checkpointed uberblock to
5178 * disk. Once this is done, we've basically rewound the whole
5179 * pool and there is no way back.
5181 * There are cases when we don't want to attempt and sync the
5182 * checkpointed uberblock to disk because we are opening a
5183 * pool as read-only. Specifically, verifying the checkpointed
5184 * state with zdb, and importing the checkpointed state to get
5185 * a "preview" of its content.
5187 if (spa_writeable(spa
)) {
5188 vdev_t
*rvd
= spa
->spa_root_vdev
;
5190 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5191 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
5193 int children
= rvd
->vdev_children
;
5194 int c0
= random_in_range(children
);
5196 for (int c
= 0; c
< children
; c
++) {
5197 vdev_t
*vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
5199 /* Stop when revisiting the first vdev */
5200 if (c
> 0 && svd
[0] == vd
)
5203 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
||
5204 !vdev_is_concrete(vd
))
5207 svd
[svdcount
++] = vd
;
5208 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
5211 error
= vdev_config_sync(svd
, svdcount
, spa
->spa_first_txg
);
5213 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
5214 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5217 spa_load_failed(spa
, "failed to write checkpointed "
5218 "uberblock to the vdev labels [error=%d]", error
);
5227 spa_ld_mos_with_trusted_config(spa_t
*spa
, spa_import_type_t type
,
5228 boolean_t
*update_config_cache
)
5233 * Parse the config for pool, open and validate vdevs,
5234 * select an uberblock, and use that uberblock to open
5237 error
= spa_ld_mos_init(spa
, type
);
5242 * Retrieve the trusted config stored in the MOS and use it to create
5243 * a new, exact version of the vdev tree, then reopen all vdevs.
5245 error
= spa_ld_trusted_config(spa
, type
, B_FALSE
);
5246 if (error
== EAGAIN
) {
5247 if (update_config_cache
!= NULL
)
5248 *update_config_cache
= B_TRUE
;
5251 * Redo the loading process with the trusted config if it is
5252 * too different from the untrusted config.
5254 spa_ld_prepare_for_reload(spa
);
5255 spa_load_note(spa
, "RELOADING");
5256 error
= spa_ld_mos_init(spa
, type
);
5260 error
= spa_ld_trusted_config(spa
, type
, B_TRUE
);
5264 } else if (error
!= 0) {
5272 * Load an existing storage pool, using the config provided. This config
5273 * describes which vdevs are part of the pool and is later validated against
5274 * partial configs present in each vdev's label and an entire copy of the
5275 * config stored in the MOS.
5278 spa_load_impl(spa_t
*spa
, spa_import_type_t type
, const char **ereport
)
5281 boolean_t missing_feat_write
= B_FALSE
;
5282 boolean_t checkpoint_rewind
=
5283 (spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5284 boolean_t update_config_cache
= B_FALSE
;
5285 hrtime_t load_start
= gethrtime();
5287 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5288 ASSERT(spa
->spa_config_source
!= SPA_CONFIG_SRC_NONE
);
5290 spa_load_note(spa
, "LOADING");
5292 error
= spa_ld_mos_with_trusted_config(spa
, type
, &update_config_cache
);
5297 * If we are rewinding to the checkpoint then we need to repeat
5298 * everything we've done so far in this function but this time
5299 * selecting the checkpointed uberblock and using that to open
5302 if (checkpoint_rewind
) {
5304 * If we are rewinding to the checkpoint update config cache
5307 update_config_cache
= B_TRUE
;
5310 * Extract the checkpointed uberblock from the current MOS
5311 * and use this as the pool's uberblock from now on. If the
5312 * pool is imported as writeable we also write the checkpoint
5313 * uberblock to the labels, making the rewind permanent.
5315 error
= spa_ld_checkpoint_rewind(spa
);
5320 * Redo the loading process again with the
5321 * checkpointed uberblock.
5323 spa_ld_prepare_for_reload(spa
);
5324 spa_load_note(spa
, "LOADING checkpointed uberblock");
5325 error
= spa_ld_mos_with_trusted_config(spa
, type
, NULL
);
5331 * Drop the namespace lock for the rest of the function.
5333 spa
->spa_load_thread
= curthread
;
5334 mutex_exit(&spa_namespace_lock
);
5337 * Retrieve the checkpoint txg if the pool has a checkpoint.
5339 spa_import_progress_set_notes(spa
, "Loading checkpoint txg");
5340 error
= spa_ld_read_checkpoint_txg(spa
);
5345 * Retrieve the mapping of indirect vdevs. Those vdevs were removed
5346 * from the pool and their contents were re-mapped to other vdevs. Note
5347 * that everything that we read before this step must have been
5348 * rewritten on concrete vdevs after the last device removal was
5349 * initiated. Otherwise we could be reading from indirect vdevs before
5350 * we have loaded their mappings.
5352 spa_import_progress_set_notes(spa
, "Loading indirect vdev metadata");
5353 error
= spa_ld_open_indirect_vdev_metadata(spa
);
5358 * Retrieve the full list of active features from the MOS and check if
5359 * they are all supported.
5361 spa_import_progress_set_notes(spa
, "Checking feature flags");
5362 error
= spa_ld_check_features(spa
, &missing_feat_write
);
5367 * Load several special directories from the MOS needed by the dsl_pool
5370 spa_import_progress_set_notes(spa
, "Loading special MOS directories");
5371 error
= spa_ld_load_special_directories(spa
);
5376 * Retrieve pool properties from the MOS.
5378 spa_import_progress_set_notes(spa
, "Loading properties");
5379 error
= spa_ld_get_props(spa
);
5384 * Retrieve the list of auxiliary devices - cache devices and spares -
5387 spa_import_progress_set_notes(spa
, "Loading AUX vdevs");
5388 error
= spa_ld_open_aux_vdevs(spa
, type
);
5393 * Load the metadata for all vdevs. Also check if unopenable devices
5394 * should be autoreplaced.
5396 spa_import_progress_set_notes(spa
, "Loading vdev metadata");
5397 error
= spa_ld_load_vdev_metadata(spa
);
5401 spa_import_progress_set_notes(spa
, "Loading dedup tables");
5402 error
= spa_ld_load_dedup_tables(spa
);
5406 spa_import_progress_set_notes(spa
, "Loading BRT");
5407 error
= spa_ld_load_brt(spa
);
5412 * Verify the logs now to make sure we don't have any unexpected errors
5413 * when we claim log blocks later.
5415 spa_import_progress_set_notes(spa
, "Verifying Log Devices");
5416 error
= spa_ld_verify_logs(spa
, type
, ereport
);
5420 if (missing_feat_write
) {
5421 ASSERT(spa
->spa_load_state
== SPA_LOAD_TRYIMPORT
);
5424 * At this point, we know that we can open the pool in
5425 * read-only mode but not read-write mode. We now have enough
5426 * information and can return to userland.
5428 error
= spa_vdev_err(spa
->spa_root_vdev
, VDEV_AUX_UNSUP_FEAT
,
5434 * Traverse the last txgs to make sure the pool was left off in a safe
5435 * state. When performing an extreme rewind, we verify the whole pool,
5436 * which can take a very long time.
5438 spa_import_progress_set_notes(spa
, "Verifying pool data");
5439 error
= spa_ld_verify_pool_data(spa
);
5444 * Calculate the deflated space for the pool. This must be done before
5445 * we write anything to the pool because we'd need to update the space
5446 * accounting using the deflated sizes.
5448 spa_import_progress_set_notes(spa
, "Calculating deflated space");
5449 spa_update_dspace(spa
);
5452 * We have now retrieved all the information we needed to open the
5453 * pool. If we are importing the pool in read-write mode, a few
5454 * additional steps must be performed to finish the import.
5456 spa_import_progress_set_notes(spa
, "Starting import");
5457 if (spa_writeable(spa
) && (spa
->spa_load_state
== SPA_LOAD_RECOVER
||
5458 spa
->spa_load_max_txg
== UINT64_MAX
)) {
5459 uint64_t config_cache_txg
= spa
->spa_config_txg
;
5461 ASSERT(spa
->spa_load_state
!= SPA_LOAD_TRYIMPORT
);
5464 * Before we do any zio_write's, complete the raidz expansion
5465 * scratch space copying, if necessary.
5467 if (RRSS_GET_STATE(&spa
->spa_uberblock
) == RRSS_SCRATCH_VALID
)
5468 vdev_raidz_reflow_copy_scratch(spa
);
5471 * In case of a checkpoint rewind, log the original txg
5472 * of the checkpointed uberblock.
5474 if (checkpoint_rewind
) {
5475 spa_history_log_internal(spa
, "checkpoint rewind",
5476 NULL
, "rewound state to txg=%llu",
5477 (u_longlong_t
)spa
->spa_uberblock
.ub_checkpoint_txg
);
5480 spa_import_progress_set_notes(spa
, "Claiming ZIL blocks");
5482 * Traverse the ZIL and claim all blocks.
5484 spa_ld_claim_log_blocks(spa
);
5487 * Kick-off the syncing thread.
5489 spa
->spa_sync_on
= B_TRUE
;
5490 txg_sync_start(spa
->spa_dsl_pool
);
5491 mmp_thread_start(spa
);
5494 * Wait for all claims to sync. We sync up to the highest
5495 * claimed log block birth time so that claimed log blocks
5496 * don't appear to be from the future. spa_claim_max_txg
5497 * will have been set for us by ZIL traversal operations
5500 spa_import_progress_set_notes(spa
, "Syncing ZIL claims");
5501 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
5504 * Check if we need to request an update of the config. On the
5505 * next sync, we would update the config stored in vdev labels
5506 * and the cachefile (by default /etc/zfs/zpool.cache).
5508 spa_import_progress_set_notes(spa
, "Updating configs");
5509 spa_ld_check_for_config_update(spa
, config_cache_txg
,
5510 update_config_cache
);
5513 * Check if a rebuild was in progress and if so resume it.
5514 * Then check all DTLs to see if anything needs resilvering.
5515 * The resilver will be deferred if a rebuild was started.
5517 spa_import_progress_set_notes(spa
, "Starting resilvers");
5518 if (vdev_rebuild_active(spa
->spa_root_vdev
)) {
5519 vdev_rebuild_restart(spa
);
5520 } else if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
5521 vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5522 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
5526 * Log the fact that we booted up (so that we can detect if
5527 * we rebooted in the middle of an operation).
5529 spa_history_log_version(spa
, "open", NULL
);
5531 spa_import_progress_set_notes(spa
,
5532 "Restarting device removals");
5533 spa_restart_removal(spa
);
5534 spa_spawn_aux_threads(spa
);
5537 * Delete any inconsistent datasets.
5540 * Since we may be issuing deletes for clones here,
5541 * we make sure to do so after we've spawned all the
5542 * auxiliary threads above (from which the livelist
5543 * deletion zthr is part of).
5545 spa_import_progress_set_notes(spa
,
5546 "Cleaning up inconsistent objsets");
5547 (void) dmu_objset_find(spa_name(spa
),
5548 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
5551 * Clean up any stale temporary dataset userrefs.
5553 spa_import_progress_set_notes(spa
,
5554 "Cleaning up temporary userrefs");
5555 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
5557 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5558 spa_import_progress_set_notes(spa
, "Restarting initialize");
5559 vdev_initialize_restart(spa
->spa_root_vdev
);
5560 spa_import_progress_set_notes(spa
, "Restarting TRIM");
5561 vdev_trim_restart(spa
->spa_root_vdev
);
5562 vdev_autotrim_restart(spa
);
5563 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5564 spa_import_progress_set_notes(spa
, "Finished importing");
5566 zio_handle_import_delay(spa
, gethrtime() - load_start
);
5568 spa_import_progress_remove(spa_guid(spa
));
5569 spa_async_request(spa
, SPA_ASYNC_L2CACHE_REBUILD
);
5571 spa_load_note(spa
, "LOADED");
5573 mutex_enter(&spa_namespace_lock
);
5574 spa
->spa_load_thread
= NULL
;
5575 cv_broadcast(&spa_namespace_cv
);
5582 spa_load_retry(spa_t
*spa
, spa_load_state_t state
)
5584 spa_mode_t mode
= spa
->spa_mode
;
5587 spa_deactivate(spa
);
5589 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
5591 spa_activate(spa
, mode
);
5592 spa_async_suspend(spa
);
5594 spa_load_note(spa
, "spa_load_retry: rewind, max txg: %llu",
5595 (u_longlong_t
)spa
->spa_load_max_txg
);
5597 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
));
5601 * If spa_load() fails this function will try loading prior txg's. If
5602 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
5603 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
5604 * function will not rewind the pool and will return the same error as
5608 spa_load_best(spa_t
*spa
, spa_load_state_t state
, uint64_t max_request
,
5611 nvlist_t
*loadinfo
= NULL
;
5612 nvlist_t
*config
= NULL
;
5613 int load_error
, rewind_error
;
5614 uint64_t safe_rewind_txg
;
5617 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
5618 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
5619 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5621 spa
->spa_load_max_txg
= max_request
;
5622 if (max_request
!= UINT64_MAX
)
5623 spa
->spa_extreme_rewind
= B_TRUE
;
5626 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
);
5627 if (load_error
== 0)
5629 if (load_error
== ZFS_ERR_NO_CHECKPOINT
) {
5631 * When attempting checkpoint-rewind on a pool with no
5632 * checkpoint, we should not attempt to load uberblocks
5633 * from previous txgs when spa_load fails.
5635 ASSERT(spa
->spa_import_flags
& ZFS_IMPORT_CHECKPOINT
);
5636 spa_import_progress_remove(spa_guid(spa
));
5637 return (load_error
);
5640 if (spa
->spa_root_vdev
!= NULL
)
5641 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5643 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
5644 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
5646 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
5647 nvlist_free(config
);
5648 spa_import_progress_remove(spa_guid(spa
));
5649 return (load_error
);
5652 if (state
== SPA_LOAD_RECOVER
) {
5653 /* Price of rolling back is discarding txgs, including log */
5654 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
5657 * If we aren't rolling back save the load info from our first
5658 * import attempt so that we can restore it after attempting
5661 loadinfo
= spa
->spa_load_info
;
5662 spa
->spa_load_info
= fnvlist_alloc();
5665 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
5666 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
5667 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
5668 TXG_INITIAL
: safe_rewind_txg
;
5671 * Continue as long as we're finding errors, we're still within
5672 * the acceptable rewind range, and we're still finding uberblocks
5674 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
5675 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
5676 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
5677 spa
->spa_extreme_rewind
= B_TRUE
;
5678 rewind_error
= spa_load_retry(spa
, state
);
5681 spa
->spa_extreme_rewind
= B_FALSE
;
5682 spa
->spa_load_max_txg
= UINT64_MAX
;
5684 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
5685 spa_config_set(spa
, config
);
5687 nvlist_free(config
);
5689 if (state
== SPA_LOAD_RECOVER
) {
5690 ASSERT3P(loadinfo
, ==, NULL
);
5691 spa_import_progress_remove(spa_guid(spa
));
5692 return (rewind_error
);
5694 /* Store the rewind info as part of the initial load info */
5695 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
5696 spa
->spa_load_info
);
5698 /* Restore the initial load info */
5699 fnvlist_free(spa
->spa_load_info
);
5700 spa
->spa_load_info
= loadinfo
;
5702 spa_import_progress_remove(spa_guid(spa
));
5703 return (load_error
);
5710 * The import case is identical to an open except that the configuration is sent
5711 * down from userland, instead of grabbed from the configuration cache. For the
5712 * case of an open, the pool configuration will exist in the
5713 * POOL_STATE_UNINITIALIZED state.
5715 * The stats information (gen/count/ustats) is used to gather vdev statistics at
5716 * the same time open the pool, without having to keep around the spa_t in some
5720 spa_open_common(const char *pool
, spa_t
**spapp
, const void *tag
,
5721 nvlist_t
*nvpolicy
, nvlist_t
**config
)
5724 spa_load_state_t state
= SPA_LOAD_OPEN
;
5726 int locked
= B_FALSE
;
5727 int firstopen
= B_FALSE
;
5732 * As disgusting as this is, we need to support recursive calls to this
5733 * function because dsl_dir_open() is called during spa_load(), and ends
5734 * up calling spa_open() again. The real fix is to figure out how to
5735 * avoid dsl_dir_open() calling this in the first place.
5737 if (MUTEX_NOT_HELD(&spa_namespace_lock
)) {
5738 mutex_enter(&spa_namespace_lock
);
5742 if ((spa
= spa_lookup(pool
)) == NULL
) {
5744 mutex_exit(&spa_namespace_lock
);
5745 return (SET_ERROR(ENOENT
));
5748 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
5749 zpool_load_policy_t policy
;
5753 zpool_get_load_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
5755 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
5756 state
= SPA_LOAD_RECOVER
;
5758 spa_activate(spa
, spa_mode_global
);
5760 if (state
!= SPA_LOAD_RECOVER
)
5761 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
5762 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
5764 zfs_dbgmsg("spa_open_common: opening %s", pool
);
5765 error
= spa_load_best(spa
, state
, policy
.zlp_txg
,
5768 if (error
== EBADF
) {
5770 * If vdev_validate() returns failure (indicated by
5771 * EBADF), it indicates that one of the vdevs indicates
5772 * that the pool has been exported or destroyed. If
5773 * this is the case, the config cache is out of sync and
5774 * we should remove the pool from the namespace.
5777 spa_deactivate(spa
);
5778 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
5781 mutex_exit(&spa_namespace_lock
);
5782 return (SET_ERROR(ENOENT
));
5787 * We can't open the pool, but we still have useful
5788 * information: the state of each vdev after the
5789 * attempted vdev_open(). Return this to the user.
5791 if (config
!= NULL
&& spa
->spa_config
) {
5792 *config
= fnvlist_dup(spa
->spa_config
);
5793 fnvlist_add_nvlist(*config
,
5794 ZPOOL_CONFIG_LOAD_INFO
,
5795 spa
->spa_load_info
);
5798 spa_deactivate(spa
);
5799 spa
->spa_last_open_failed
= error
;
5801 mutex_exit(&spa_namespace_lock
);
5807 spa_open_ref(spa
, tag
);
5810 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
5813 * If we've recovered the pool, pass back any information we
5814 * gathered while doing the load.
5816 if (state
== SPA_LOAD_RECOVER
&& config
!= NULL
) {
5817 fnvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
5818 spa
->spa_load_info
);
5822 spa
->spa_last_open_failed
= 0;
5823 spa
->spa_last_ubsync_txg
= 0;
5824 spa
->spa_load_txg
= 0;
5825 mutex_exit(&spa_namespace_lock
);
5829 zvol_create_minors_recursive(spa_name(spa
));
5837 spa_open_rewind(const char *name
, spa_t
**spapp
, const void *tag
,
5838 nvlist_t
*policy
, nvlist_t
**config
)
5840 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
5844 spa_open(const char *name
, spa_t
**spapp
, const void *tag
)
5846 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
5850 * Lookup the given spa_t, incrementing the inject count in the process,
5851 * preventing it from being exported or destroyed.
5854 spa_inject_addref(char *name
)
5858 mutex_enter(&spa_namespace_lock
);
5859 if ((spa
= spa_lookup(name
)) == NULL
) {
5860 mutex_exit(&spa_namespace_lock
);
5863 spa
->spa_inject_ref
++;
5864 mutex_exit(&spa_namespace_lock
);
5870 spa_inject_delref(spa_t
*spa
)
5872 mutex_enter(&spa_namespace_lock
);
5873 spa
->spa_inject_ref
--;
5874 mutex_exit(&spa_namespace_lock
);
5878 * Add spares device information to the nvlist.
5881 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
5891 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5893 if (spa
->spa_spares
.sav_count
== 0)
5896 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5897 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5898 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
));
5900 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5901 (const nvlist_t
* const *)spares
, nspares
);
5902 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
5903 &spares
, &nspares
));
5906 * Go through and find any spares which have since been
5907 * repurposed as an active spare. If this is the case, update
5908 * their status appropriately.
5910 for (i
= 0; i
< nspares
; i
++) {
5911 guid
= fnvlist_lookup_uint64(spares
[i
],
5913 VERIFY0(nvlist_lookup_uint64_array(spares
[i
],
5914 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5915 if (spa_spare_exists(guid
, &pool
, NULL
) &&
5917 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
5918 vs
->vs_aux
= VDEV_AUX_SPARED
;
5921 spa
->spa_spares
.sav_vdevs
[i
]->vdev_state
;
5928 * Add l2cache device information to the nvlist, including vdev stats.
5931 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
5934 uint_t i
, j
, nl2cache
;
5941 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
5943 if (spa
->spa_l2cache
.sav_count
== 0)
5946 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
5947 VERIFY0(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5948 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
));
5949 if (nl2cache
!= 0) {
5950 fnvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5951 (const nvlist_t
* const *)l2cache
, nl2cache
);
5952 VERIFY0(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
5953 &l2cache
, &nl2cache
));
5956 * Update level 2 cache device stats.
5959 for (i
= 0; i
< nl2cache
; i
++) {
5960 guid
= fnvlist_lookup_uint64(l2cache
[i
],
5964 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
5966 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
5967 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
5973 VERIFY0(nvlist_lookup_uint64_array(l2cache
[i
],
5974 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
));
5975 vdev_get_stats(vd
, vs
);
5976 vdev_config_generate_stats(vd
, l2cache
[i
]);
5983 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
5988 if (spa
->spa_feat_for_read_obj
!= 0) {
5989 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
5990 spa
->spa_feat_for_read_obj
);
5991 zap_cursor_retrieve(&zc
, &za
) == 0;
5992 zap_cursor_advance(&zc
)) {
5993 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
5994 za
.za_num_integers
== 1);
5995 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
5996 za
.za_first_integer
));
5998 zap_cursor_fini(&zc
);
6001 if (spa
->spa_feat_for_write_obj
!= 0) {
6002 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6003 spa
->spa_feat_for_write_obj
);
6004 zap_cursor_retrieve(&zc
, &za
) == 0;
6005 zap_cursor_advance(&zc
)) {
6006 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
6007 za
.za_num_integers
== 1);
6008 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
6009 za
.za_first_integer
));
6011 zap_cursor_fini(&zc
);
6016 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
6020 for (i
= 0; i
< SPA_FEATURES
; i
++) {
6021 zfeature_info_t feature
= spa_feature_table
[i
];
6024 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
6027 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
6032 * Store a list of pool features and their reference counts in the
6035 * The first time this is called on a spa, allocate a new nvlist, fetch
6036 * the pool features and reference counts from disk, then save the list
6037 * in the spa. In subsequent calls on the same spa use the saved nvlist
6038 * and refresh its values from the cached reference counts. This
6039 * ensures we don't block here on I/O on a suspended pool so 'zpool
6040 * clear' can resume the pool.
6043 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
6047 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
6049 mutex_enter(&spa
->spa_feat_stats_lock
);
6050 features
= spa
->spa_feat_stats
;
6052 if (features
!= NULL
) {
6053 spa_feature_stats_from_cache(spa
, features
);
6055 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
6056 spa
->spa_feat_stats
= features
;
6057 spa_feature_stats_from_disk(spa
, features
);
6060 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
6063 mutex_exit(&spa
->spa_feat_stats_lock
);
6067 spa_get_stats(const char *name
, nvlist_t
**config
,
6068 char *altroot
, size_t buflen
)
6074 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
6078 * This still leaves a window of inconsistency where the spares
6079 * or l2cache devices could change and the config would be
6080 * self-inconsistent.
6082 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6084 if (*config
!= NULL
) {
6085 uint64_t loadtimes
[2];
6087 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
6088 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
6089 fnvlist_add_uint64_array(*config
,
6090 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2);
6092 fnvlist_add_uint64(*config
,
6093 ZPOOL_CONFIG_ERRCOUNT
,
6094 spa_approx_errlog_size(spa
));
6096 if (spa_suspended(spa
)) {
6097 fnvlist_add_uint64(*config
,
6098 ZPOOL_CONFIG_SUSPENDED
,
6100 fnvlist_add_uint64(*config
,
6101 ZPOOL_CONFIG_SUSPENDED_REASON
,
6102 spa
->spa_suspended
);
6105 spa_add_spares(spa
, *config
);
6106 spa_add_l2cache(spa
, *config
);
6107 spa_add_feature_stats(spa
, *config
);
6112 * We want to get the alternate root even for faulted pools, so we cheat
6113 * and call spa_lookup() directly.
6117 mutex_enter(&spa_namespace_lock
);
6118 spa
= spa_lookup(name
);
6120 spa_altroot(spa
, altroot
, buflen
);
6124 mutex_exit(&spa_namespace_lock
);
6126 spa_altroot(spa
, altroot
, buflen
);
6131 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6132 spa_close(spa
, FTAG
);
6139 * Validate that the auxiliary device array is well formed. We must have an
6140 * array of nvlists, each which describes a valid leaf vdev. If this is an
6141 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
6142 * specified, as long as they are well-formed.
6145 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
6146 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
6147 vdev_labeltype_t label
)
6154 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
6157 * It's acceptable to have no devs specified.
6159 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
6163 return (SET_ERROR(EINVAL
));
6166 * Make sure the pool is formatted with a version that supports this
6169 if (spa_version(spa
) < version
)
6170 return (SET_ERROR(ENOTSUP
));
6173 * Set the pending device list so we correctly handle device in-use
6176 sav
->sav_pending
= dev
;
6177 sav
->sav_npending
= ndev
;
6179 for (i
= 0; i
< ndev
; i
++) {
6180 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
6184 if (!vd
->vdev_ops
->vdev_op_leaf
) {
6186 error
= SET_ERROR(EINVAL
);
6192 if ((error
= vdev_open(vd
)) == 0 &&
6193 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
6194 fnvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
6201 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
6208 sav
->sav_pending
= NULL
;
6209 sav
->sav_npending
= 0;
6214 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
6218 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
6220 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
6221 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
6222 VDEV_LABEL_SPARE
)) != 0) {
6226 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
6227 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
6228 VDEV_LABEL_L2CACHE
));
6232 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
6237 if (sav
->sav_config
!= NULL
) {
6243 * Generate new dev list by concatenating with the
6246 VERIFY0(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
6247 &olddevs
, &oldndevs
));
6249 newdevs
= kmem_alloc(sizeof (void *) *
6250 (ndevs
+ oldndevs
), KM_SLEEP
);
6251 for (i
= 0; i
< oldndevs
; i
++)
6252 newdevs
[i
] = fnvlist_dup(olddevs
[i
]);
6253 for (i
= 0; i
< ndevs
; i
++)
6254 newdevs
[i
+ oldndevs
] = fnvlist_dup(devs
[i
]);
6256 fnvlist_remove(sav
->sav_config
, config
);
6258 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
6259 (const nvlist_t
* const *)newdevs
, ndevs
+ oldndevs
);
6260 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
6261 nvlist_free(newdevs
[i
]);
6262 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
6265 * Generate a new dev list.
6267 sav
->sav_config
= fnvlist_alloc();
6268 fnvlist_add_nvlist_array(sav
->sav_config
, config
,
6269 (const nvlist_t
* const *)devs
, ndevs
);
6274 * Stop and drop level 2 ARC devices
6277 spa_l2cache_drop(spa_t
*spa
)
6281 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
6283 for (i
= 0; i
< sav
->sav_count
; i
++) {
6286 vd
= sav
->sav_vdevs
[i
];
6289 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
6290 pool
!= 0ULL && l2arc_vdev_present(vd
))
6291 l2arc_remove_vdev(vd
);
6296 * Verify encryption parameters for spa creation. If we are encrypting, we must
6297 * have the encryption feature flag enabled.
6300 spa_create_check_encryption_params(dsl_crypto_params_t
*dcp
,
6301 boolean_t has_encryption
)
6303 if (dcp
->cp_crypt
!= ZIO_CRYPT_OFF
&&
6304 dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
&&
6306 return (SET_ERROR(ENOTSUP
));
6308 return (dmu_objset_create_crypt_check(NULL
, dcp
, NULL
));
6315 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
6316 nvlist_t
*zplprops
, dsl_crypto_params_t
*dcp
)
6319 const char *altroot
= NULL
;
6324 uint64_t txg
= TXG_INITIAL
;
6325 nvlist_t
**spares
, **l2cache
;
6326 uint_t nspares
, nl2cache
;
6327 uint64_t version
, obj
, ndraid
= 0;
6328 boolean_t has_features
;
6329 boolean_t has_encryption
;
6330 boolean_t has_allocclass
;
6332 const char *feat_name
;
6333 const char *poolname
;
6336 if (props
== NULL
||
6337 nvlist_lookup_string(props
,
6338 zpool_prop_to_name(ZPOOL_PROP_TNAME
), &poolname
) != 0)
6339 poolname
= (char *)pool
;
6342 * If this pool already exists, return failure.
6344 mutex_enter(&spa_namespace_lock
);
6345 if (spa_lookup(poolname
) != NULL
) {
6346 mutex_exit(&spa_namespace_lock
);
6347 return (SET_ERROR(EEXIST
));
6351 * Allocate a new spa_t structure.
6353 nvl
= fnvlist_alloc();
6354 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
6355 (void) nvlist_lookup_string(props
,
6356 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6357 spa
= spa_add(poolname
, nvl
, altroot
);
6359 spa_activate(spa
, spa_mode_global
);
6361 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
6362 spa_deactivate(spa
);
6364 mutex_exit(&spa_namespace_lock
);
6369 * Temporary pool names should never be written to disk.
6371 if (poolname
!= pool
)
6372 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
6374 has_features
= B_FALSE
;
6375 has_encryption
= B_FALSE
;
6376 has_allocclass
= B_FALSE
;
6377 for (nvpair_t
*elem
= nvlist_next_nvpair(props
, NULL
);
6378 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
6379 if (zpool_prop_feature(nvpair_name(elem
))) {
6380 has_features
= B_TRUE
;
6382 feat_name
= strchr(nvpair_name(elem
), '@') + 1;
6383 VERIFY0(zfeature_lookup_name(feat_name
, &feat
));
6384 if (feat
== SPA_FEATURE_ENCRYPTION
)
6385 has_encryption
= B_TRUE
;
6386 if (feat
== SPA_FEATURE_ALLOCATION_CLASSES
)
6387 has_allocclass
= B_TRUE
;
6391 /* verify encryption params, if they were provided */
6393 error
= spa_create_check_encryption_params(dcp
, has_encryption
);
6395 spa_deactivate(spa
);
6397 mutex_exit(&spa_namespace_lock
);
6401 if (!has_allocclass
&& zfs_special_devs(nvroot
, NULL
)) {
6402 spa_deactivate(spa
);
6404 mutex_exit(&spa_namespace_lock
);
6408 if (has_features
|| nvlist_lookup_uint64(props
,
6409 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
6410 version
= SPA_VERSION
;
6412 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6414 spa
->spa_first_txg
= txg
;
6415 spa
->spa_uberblock
.ub_txg
= txg
- 1;
6416 spa
->spa_uberblock
.ub_version
= version
;
6417 spa
->spa_ubsync
= spa
->spa_uberblock
;
6418 spa
->spa_load_state
= SPA_LOAD_CREATE
;
6419 spa
->spa_removing_phys
.sr_state
= DSS_NONE
;
6420 spa
->spa_removing_phys
.sr_removing_vdev
= -1;
6421 spa
->spa_removing_phys
.sr_prev_indirect_vdev
= -1;
6422 spa
->spa_indirect_vdevs_loaded
= B_TRUE
;
6425 * Create "The Godfather" zio to hold all async IOs
6427 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
6429 for (int i
= 0; i
< max_ncpus
; i
++) {
6430 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
6431 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
6432 ZIO_FLAG_GODFATHER
);
6436 * Create the root vdev.
6438 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6440 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
6442 ASSERT(error
!= 0 || rvd
!= NULL
);
6443 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
6445 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
6446 error
= SET_ERROR(EINVAL
);
6449 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
6450 (error
= vdev_draid_spare_create(nvroot
, rvd
, &ndraid
, 0)) == 0 &&
6451 (error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) == 0) {
6453 * instantiate the metaslab groups (this will dirty the vdevs)
6454 * we can no longer error exit past this point
6456 for (int c
= 0; error
== 0 && c
< rvd
->vdev_children
; c
++) {
6457 vdev_t
*vd
= rvd
->vdev_child
[c
];
6459 vdev_metaslab_set_size(vd
);
6460 vdev_expand(vd
, txg
);
6464 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6468 spa_deactivate(spa
);
6470 mutex_exit(&spa_namespace_lock
);
6475 * Get the list of spares, if specified.
6477 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6478 &spares
, &nspares
) == 0) {
6479 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6480 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6481 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6483 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6484 spa_load_spares(spa
);
6485 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6486 spa
->spa_spares
.sav_sync
= B_TRUE
;
6490 * Get the list of level 2 cache devices, if specified.
6492 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6493 &l2cache
, &nl2cache
) == 0) {
6494 VERIFY0(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
6495 NV_UNIQUE_NAME
, KM_SLEEP
));
6496 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6497 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6499 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6500 spa_load_l2cache(spa
);
6501 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6502 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6505 spa
->spa_is_initializing
= B_TRUE
;
6506 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, dcp
, txg
);
6507 spa
->spa_is_initializing
= B_FALSE
;
6510 * Create DDTs (dedup tables).
6514 * Create BRT table and BRT table object.
6518 spa_update_dspace(spa
);
6520 tx
= dmu_tx_create_assigned(dp
, txg
);
6523 * Create the pool's history object.
6525 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& !spa
->spa_history
)
6526 spa_history_create_obj(spa
, tx
);
6528 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_CREATE
);
6529 spa_history_log_version(spa
, "create", tx
);
6532 * Create the pool config object.
6534 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6535 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
6536 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
6538 if (zap_add(spa
->spa_meta_objset
,
6539 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
6540 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
6541 cmn_err(CE_PANIC
, "failed to add pool config");
6544 if (zap_add(spa
->spa_meta_objset
,
6545 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
6546 sizeof (uint64_t), 1, &version
, tx
) != 0) {
6547 cmn_err(CE_PANIC
, "failed to add pool version");
6550 /* Newly created pools with the right version are always deflated. */
6551 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6552 spa
->spa_deflate
= TRUE
;
6553 if (zap_add(spa
->spa_meta_objset
,
6554 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6555 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
6556 cmn_err(CE_PANIC
, "failed to add deflate");
6561 * Create the deferred-free bpobj. Turn off compression
6562 * because sync-to-convergence takes longer if the blocksize
6565 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
6566 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
6567 ZIO_COMPRESS_OFF
, tx
);
6568 if (zap_add(spa
->spa_meta_objset
,
6569 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
6570 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
6571 cmn_err(CE_PANIC
, "failed to add bpobj");
6573 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
6574 spa
->spa_meta_objset
, obj
));
6577 * Generate some random noise for salted checksums to operate on.
6579 (void) random_get_pseudo_bytes(spa
->spa_cksum_salt
.zcs_bytes
,
6580 sizeof (spa
->spa_cksum_salt
.zcs_bytes
));
6583 * Set pool properties.
6585 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
6586 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
6587 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
6588 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
6589 spa
->spa_multihost
= zpool_prop_default_numeric(ZPOOL_PROP_MULTIHOST
);
6590 spa
->spa_autotrim
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOTRIM
);
6592 if (props
!= NULL
) {
6593 spa_configfile_set(spa
, props
, B_FALSE
);
6594 spa_sync_props(props
, tx
);
6597 for (int i
= 0; i
< ndraid
; i
++)
6598 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
6602 spa
->spa_sync_on
= B_TRUE
;
6604 mmp_thread_start(spa
);
6605 txg_wait_synced(dp
, txg
);
6607 spa_spawn_aux_threads(spa
);
6609 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_TRUE
);
6612 * Don't count references from objsets that are already closed
6613 * and are making their way through the eviction process.
6615 spa_evicting_os_wait(spa
);
6616 spa
->spa_minref
= zfs_refcount_count(&spa
->spa_refcount
);
6617 spa
->spa_load_state
= SPA_LOAD_NONE
;
6621 mutex_exit(&spa_namespace_lock
);
6627 * Import a non-root pool into the system.
6630 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
6633 const char *altroot
= NULL
;
6634 spa_load_state_t state
= SPA_LOAD_IMPORT
;
6635 zpool_load_policy_t policy
;
6636 spa_mode_t mode
= spa_mode_global
;
6637 uint64_t readonly
= B_FALSE
;
6640 nvlist_t
**spares
, **l2cache
;
6641 uint_t nspares
, nl2cache
;
6644 * If a pool with this name exists, return failure.
6646 mutex_enter(&spa_namespace_lock
);
6647 if (spa_lookup(pool
) != NULL
) {
6648 mutex_exit(&spa_namespace_lock
);
6649 return (SET_ERROR(EEXIST
));
6653 * Create and initialize the spa structure.
6655 (void) nvlist_lookup_string(props
,
6656 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
6657 (void) nvlist_lookup_uint64(props
,
6658 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
6660 mode
= SPA_MODE_READ
;
6661 spa
= spa_add(pool
, config
, altroot
);
6662 spa
->spa_import_flags
= flags
;
6665 * Verbatim import - Take a pool and insert it into the namespace
6666 * as if it had been loaded at boot.
6668 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
6670 spa_configfile_set(spa
, props
, B_FALSE
);
6672 spa_write_cachefile(spa
, B_FALSE
, B_TRUE
, B_FALSE
);
6673 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6674 zfs_dbgmsg("spa_import: verbatim import of %s", pool
);
6675 mutex_exit(&spa_namespace_lock
);
6679 spa_activate(spa
, mode
);
6682 * Don't start async tasks until we know everything is healthy.
6684 spa_async_suspend(spa
);
6686 zpool_get_load_policy(config
, &policy
);
6687 if (policy
.zlp_rewind
& ZPOOL_DO_REWIND
)
6688 state
= SPA_LOAD_RECOVER
;
6690 spa
->spa_config_source
= SPA_CONFIG_SRC_TRYIMPORT
;
6692 if (state
!= SPA_LOAD_RECOVER
) {
6693 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
6694 zfs_dbgmsg("spa_import: importing %s", pool
);
6696 zfs_dbgmsg("spa_import: importing %s, max_txg=%lld "
6697 "(RECOVERY MODE)", pool
, (longlong_t
)policy
.zlp_txg
);
6699 error
= spa_load_best(spa
, state
, policy
.zlp_txg
, policy
.zlp_rewind
);
6702 * Propagate anything learned while loading the pool and pass it
6703 * back to caller (i.e. rewind info, missing devices, etc).
6705 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, spa
->spa_load_info
);
6707 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6709 * Toss any existing sparelist, as it doesn't have any validity
6710 * anymore, and conflicts with spa_has_spare().
6712 if (spa
->spa_spares
.sav_config
) {
6713 nvlist_free(spa
->spa_spares
.sav_config
);
6714 spa
->spa_spares
.sav_config
= NULL
;
6715 spa_load_spares(spa
);
6717 if (spa
->spa_l2cache
.sav_config
) {
6718 nvlist_free(spa
->spa_l2cache
.sav_config
);
6719 spa
->spa_l2cache
.sav_config
= NULL
;
6720 spa_load_l2cache(spa
);
6723 nvroot
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
6724 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6727 spa_configfile_set(spa
, props
, B_FALSE
);
6729 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
6730 (error
= spa_prop_set(spa
, props
)))) {
6732 spa_deactivate(spa
);
6734 mutex_exit(&spa_namespace_lock
);
6738 spa_async_resume(spa
);
6741 * Override any spares and level 2 cache devices as specified by
6742 * the user, as these may have correct device names/devids, etc.
6744 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
6745 &spares
, &nspares
) == 0) {
6746 if (spa
->spa_spares
.sav_config
)
6747 fnvlist_remove(spa
->spa_spares
.sav_config
,
6748 ZPOOL_CONFIG_SPARES
);
6750 spa
->spa_spares
.sav_config
= fnvlist_alloc();
6751 fnvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
6752 ZPOOL_CONFIG_SPARES
, (const nvlist_t
* const *)spares
,
6754 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6755 spa_load_spares(spa
);
6756 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6757 spa
->spa_spares
.sav_sync
= B_TRUE
;
6759 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
6760 &l2cache
, &nl2cache
) == 0) {
6761 if (spa
->spa_l2cache
.sav_config
)
6762 fnvlist_remove(spa
->spa_l2cache
.sav_config
,
6763 ZPOOL_CONFIG_L2CACHE
);
6765 spa
->spa_l2cache
.sav_config
= fnvlist_alloc();
6766 fnvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
6767 ZPOOL_CONFIG_L2CACHE
, (const nvlist_t
* const *)l2cache
,
6769 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6770 spa_load_l2cache(spa
);
6771 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6772 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
6776 * Check for any removed devices.
6778 if (spa
->spa_autoreplace
) {
6779 spa_aux_check_removed(&spa
->spa_spares
);
6780 spa_aux_check_removed(&spa
->spa_l2cache
);
6783 if (spa_writeable(spa
)) {
6785 * Update the config cache to include the newly-imported pool.
6787 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
6791 * It's possible that the pool was expanded while it was exported.
6792 * We kick off an async task to handle this for us.
6794 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
6796 spa_history_log_version(spa
, "import", NULL
);
6798 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_IMPORT
);
6800 mutex_exit(&spa_namespace_lock
);
6802 zvol_create_minors_recursive(pool
);
6810 spa_tryimport(nvlist_t
*tryconfig
)
6812 nvlist_t
*config
= NULL
;
6813 const char *poolname
, *cachefile
;
6817 zpool_load_policy_t policy
;
6819 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
6822 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
6826 * Create and initialize the spa structure.
6828 char *name
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6829 (void) snprintf(name
, MAXPATHLEN
, "%s-%llx-%s",
6830 TRYIMPORT_NAME
, (u_longlong_t
)curthread
, poolname
);
6832 mutex_enter(&spa_namespace_lock
);
6833 spa
= spa_add(name
, tryconfig
, NULL
);
6834 spa_activate(spa
, SPA_MODE_READ
);
6835 kmem_free(name
, MAXPATHLEN
);
6838 * Rewind pool if a max txg was provided.
6840 zpool_get_load_policy(spa
->spa_config
, &policy
);
6841 if (policy
.zlp_txg
!= UINT64_MAX
) {
6842 spa
->spa_load_max_txg
= policy
.zlp_txg
;
6843 spa
->spa_extreme_rewind
= B_TRUE
;
6844 zfs_dbgmsg("spa_tryimport: importing %s, max_txg=%lld",
6845 poolname
, (longlong_t
)policy
.zlp_txg
);
6847 zfs_dbgmsg("spa_tryimport: importing %s", poolname
);
6850 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_CACHEFILE
, &cachefile
)
6852 zfs_dbgmsg("spa_tryimport: using cachefile '%s'", cachefile
);
6853 spa
->spa_config_source
= SPA_CONFIG_SRC_CACHEFILE
;
6855 spa
->spa_config_source
= SPA_CONFIG_SRC_SCAN
;
6859 * spa_import() relies on a pool config fetched by spa_try_import()
6860 * for spare/cache devices. Import flags are not passed to
6861 * spa_tryimport(), which makes it return early due to a missing log
6862 * device and missing retrieving the cache device and spare eventually.
6863 * Passing ZFS_IMPORT_MISSING_LOG to spa_tryimport() makes it fetch
6864 * the correct configuration regardless of the missing log device.
6866 spa
->spa_import_flags
|= ZFS_IMPORT_MISSING_LOG
;
6868 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
);
6871 * If 'tryconfig' was at least parsable, return the current config.
6873 if (spa
->spa_root_vdev
!= NULL
) {
6874 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
6875 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, poolname
);
6876 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
, state
);
6877 fnvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
6878 spa
->spa_uberblock
.ub_timestamp
);
6879 fnvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
6880 spa
->spa_load_info
);
6881 fnvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
6885 * If the bootfs property exists on this pool then we
6886 * copy it out so that external consumers can tell which
6887 * pools are bootable.
6889 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
6890 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6893 * We have to play games with the name since the
6894 * pool was opened as TRYIMPORT_NAME.
6896 if (dsl_dsobj_to_dsname(spa_name(spa
),
6897 spa
->spa_bootfs
, tmpname
) == 0) {
6901 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
6903 cp
= strchr(tmpname
, '/');
6905 (void) strlcpy(dsname
, tmpname
,
6908 (void) snprintf(dsname
, MAXPATHLEN
,
6909 "%s/%s", poolname
, ++cp
);
6911 fnvlist_add_string(config
, ZPOOL_CONFIG_BOOTFS
,
6913 kmem_free(dsname
, MAXPATHLEN
);
6915 kmem_free(tmpname
, MAXPATHLEN
);
6919 * Add the list of hot spares and level 2 cache devices.
6921 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6922 spa_add_spares(spa
, config
);
6923 spa_add_l2cache(spa
, config
);
6924 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6928 spa_deactivate(spa
);
6930 mutex_exit(&spa_namespace_lock
);
6936 * Pool export/destroy
6938 * The act of destroying or exporting a pool is very simple. We make sure there
6939 * is no more pending I/O and any references to the pool are gone. Then, we
6940 * update the pool state and sync all the labels to disk, removing the
6941 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
6942 * we don't sync the labels or remove the configuration cache.
6945 spa_export_common(const char *pool
, int new_state
, nvlist_t
**oldconfig
,
6946 boolean_t force
, boolean_t hardforce
)
6950 hrtime_t export_start
= gethrtime();
6955 if (!(spa_mode_global
& SPA_MODE_WRITE
))
6956 return (SET_ERROR(EROFS
));
6958 mutex_enter(&spa_namespace_lock
);
6959 if ((spa
= spa_lookup(pool
)) == NULL
) {
6960 mutex_exit(&spa_namespace_lock
);
6961 return (SET_ERROR(ENOENT
));
6964 if (spa
->spa_is_exporting
) {
6965 /* the pool is being exported by another thread */
6966 mutex_exit(&spa_namespace_lock
);
6967 return (SET_ERROR(ZFS_ERR_EXPORT_IN_PROGRESS
));
6969 spa
->spa_is_exporting
= B_TRUE
;
6972 * Put a hold on the pool, drop the namespace lock, stop async tasks
6973 * and see if we can export.
6975 spa_open_ref(spa
, FTAG
);
6976 mutex_exit(&spa_namespace_lock
);
6977 spa_async_suspend(spa
);
6978 if (spa
->spa_zvol_taskq
) {
6979 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
6980 taskq_wait(spa
->spa_zvol_taskq
);
6982 mutex_enter(&spa_namespace_lock
);
6983 spa
->spa_export_thread
= curthread
;
6984 spa_close(spa
, FTAG
);
6986 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
6987 mutex_exit(&spa_namespace_lock
);
6992 * The pool will be in core if it's openable, in which case we can
6993 * modify its state. Objsets may be open only because they're dirty,
6994 * so we have to force it to sync before checking spa_refcnt.
6996 if (spa
->spa_sync_on
) {
6997 txg_wait_synced(spa
->spa_dsl_pool
, 0);
6998 spa_evicting_os_wait(spa
);
7002 * A pool cannot be exported or destroyed if there are active
7003 * references. If we are resetting a pool, allow references by
7004 * fault injection handlers.
7006 if (!spa_refcount_zero(spa
) || (spa
->spa_inject_ref
!= 0)) {
7007 error
= SET_ERROR(EBUSY
);
7011 mutex_exit(&spa_namespace_lock
);
7013 * At this point we no longer hold the spa_namespace_lock and
7014 * there were no references on the spa. Future spa_lookups will
7015 * notice the spa->spa_export_thread and wait until we signal
7016 * that we are finshed.
7019 if (spa
->spa_sync_on
) {
7020 vdev_t
*rvd
= spa
->spa_root_vdev
;
7022 * A pool cannot be exported if it has an active shared spare.
7023 * This is to prevent other pools stealing the active spare
7024 * from an exported pool. At user's own will, such pool can
7025 * be forcedly exported.
7027 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
7028 spa_has_active_shared_spare(spa
)) {
7029 error
= SET_ERROR(EXDEV
);
7030 mutex_enter(&spa_namespace_lock
);
7035 * We're about to export or destroy this pool. Make sure
7036 * we stop all initialization and trim activity here before
7037 * we set the spa_final_txg. This will ensure that all
7038 * dirty data resulting from the initialization is
7039 * committed to disk before we unload the pool.
7041 vdev_initialize_stop_all(rvd
, VDEV_INITIALIZE_ACTIVE
);
7042 vdev_trim_stop_all(rvd
, VDEV_TRIM_ACTIVE
);
7043 vdev_autotrim_stop_all(spa
);
7044 vdev_rebuild_stop_all(spa
);
7047 * We want this to be reflected on every label,
7048 * so mark them all dirty. spa_unload() will do the
7049 * final sync that pushes these changes out.
7051 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
7052 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7053 spa
->spa_state
= new_state
;
7054 vdev_config_dirty(rvd
);
7055 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7059 * If the log space map feature is enabled and the pool is
7060 * getting exported (but not destroyed), we want to spend some
7061 * time flushing as many metaslabs as we can in an attempt to
7062 * destroy log space maps and save import time. This has to be
7063 * done before we set the spa_final_txg, otherwise
7064 * spa_sync() -> spa_flush_metaslabs() may dirty the final TXGs.
7065 * spa_should_flush_logs_on_unload() should be called after
7066 * spa_state has been set to the new_state.
7068 if (spa_should_flush_logs_on_unload(spa
))
7069 spa_unload_log_sm_flush_all(spa
);
7071 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
7072 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
7073 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
7075 spa_config_exit(spa
, SCL_ALL
, FTAG
);
7082 if (new_state
== POOL_STATE_DESTROYED
)
7083 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_DESTROY
);
7084 else if (new_state
== POOL_STATE_EXPORTED
)
7085 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_POOL_EXPORT
);
7087 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
7089 spa_deactivate(spa
);
7092 if (oldconfig
&& spa
->spa_config
)
7093 *oldconfig
= fnvlist_dup(spa
->spa_config
);
7095 if (new_state
== POOL_STATE_EXPORTED
)
7096 zio_handle_export_delay(spa
, gethrtime() - export_start
);
7099 * Take the namespace lock for the actual spa_t removal
7101 mutex_enter(&spa_namespace_lock
);
7102 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
7104 spa_write_cachefile(spa
, B_TRUE
, B_TRUE
, B_FALSE
);
7108 * If spa_remove() is not called for this spa_t and
7109 * there is any possibility that it can be reused,
7110 * we make sure to reset the exporting flag.
7112 spa
->spa_is_exporting
= B_FALSE
;
7113 spa
->spa_export_thread
= NULL
;
7117 * Wake up any waiters in spa_lookup()
7119 cv_broadcast(&spa_namespace_cv
);
7120 mutex_exit(&spa_namespace_lock
);
7124 spa
->spa_is_exporting
= B_FALSE
;
7125 spa
->spa_export_thread
= NULL
;
7127 spa_async_resume(spa
);
7129 * Wake up any waiters in spa_lookup()
7131 cv_broadcast(&spa_namespace_cv
);
7132 mutex_exit(&spa_namespace_lock
);
7137 * Destroy a storage pool.
7140 spa_destroy(const char *pool
)
7142 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
7147 * Export a storage pool.
7150 spa_export(const char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
7151 boolean_t hardforce
)
7153 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
7158 * Similar to spa_export(), this unloads the spa_t without actually removing it
7159 * from the namespace in any way.
7162 spa_reset(const char *pool
)
7164 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
7169 * ==========================================================================
7170 * Device manipulation
7171 * ==========================================================================
7175 * This is called as a synctask to increment the draid feature flag
7178 spa_draid_feature_incr(void *arg
, dmu_tx_t
*tx
)
7180 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
7181 int draid
= (int)(uintptr_t)arg
;
7183 for (int c
= 0; c
< draid
; c
++)
7184 spa_feature_incr(spa
, SPA_FEATURE_DRAID
, tx
);
7188 * Add a device to a storage pool.
7191 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
, boolean_t check_ashift
)
7193 uint64_t txg
, ndraid
= 0;
7195 vdev_t
*rvd
= spa
->spa_root_vdev
;
7197 nvlist_t
**spares
, **l2cache
;
7198 uint_t nspares
, nl2cache
;
7200 ASSERT(spa_writeable(spa
));
7202 txg
= spa_vdev_enter(spa
);
7204 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
7205 VDEV_ALLOC_ADD
)) != 0)
7206 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7208 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
7210 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
7214 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
7218 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
7219 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
7221 if (vd
->vdev_children
!= 0 &&
7222 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0) {
7223 return (spa_vdev_exit(spa
, vd
, txg
, error
));
7227 * The virtual dRAID spares must be added after vdev tree is created
7228 * and the vdev guids are generated. The guid of their associated
7229 * dRAID is stored in the config and used when opening the spare.
7231 if ((error
= vdev_draid_spare_create(nvroot
, vd
, &ndraid
,
7232 rvd
->vdev_children
)) == 0) {
7233 if (ndraid
> 0 && nvlist_lookup_nvlist_array(nvroot
,
7234 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) != 0)
7237 return (spa_vdev_exit(spa
, vd
, txg
, error
));
7241 * We must validate the spares and l2cache devices after checking the
7242 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
7244 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
7245 return (spa_vdev_exit(spa
, vd
, txg
, error
));
7248 * If we are in the middle of a device removal, we can only add
7249 * devices which match the existing devices in the pool.
7250 * If we are in the middle of a removal, or have some indirect
7251 * vdevs, we can not add raidz or dRAID top levels.
7253 if (spa
->spa_vdev_removal
!= NULL
||
7254 spa
->spa_removing_phys
.sr_prev_indirect_vdev
!= -1) {
7255 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7256 tvd
= vd
->vdev_child
[c
];
7257 if (spa
->spa_vdev_removal
!= NULL
&&
7258 tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
7259 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
7261 /* Fail if top level vdev is raidz or a dRAID */
7262 if (vdev_get_nparity(tvd
) != 0)
7263 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
7266 * Need the top level mirror to be
7267 * a mirror of leaf vdevs only
7269 if (tvd
->vdev_ops
== &vdev_mirror_ops
) {
7270 for (uint64_t cid
= 0;
7271 cid
< tvd
->vdev_children
; cid
++) {
7272 vdev_t
*cvd
= tvd
->vdev_child
[cid
];
7273 if (!cvd
->vdev_ops
->vdev_op_leaf
) {
7274 return (spa_vdev_exit(spa
, vd
,
7282 if (check_ashift
&& spa
->spa_max_ashift
== spa
->spa_min_ashift
) {
7283 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7284 tvd
= vd
->vdev_child
[c
];
7285 if (tvd
->vdev_ashift
!= spa
->spa_max_ashift
) {
7286 return (spa_vdev_exit(spa
, vd
, txg
,
7287 ZFS_ERR_ASHIFT_MISMATCH
));
7292 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
7293 tvd
= vd
->vdev_child
[c
];
7294 vdev_remove_child(vd
, tvd
);
7295 tvd
->vdev_id
= rvd
->vdev_children
;
7296 vdev_add_child(rvd
, tvd
);
7297 vdev_config_dirty(tvd
);
7301 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
7302 ZPOOL_CONFIG_SPARES
);
7303 spa_load_spares(spa
);
7304 spa
->spa_spares
.sav_sync
= B_TRUE
;
7307 if (nl2cache
!= 0) {
7308 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
7309 ZPOOL_CONFIG_L2CACHE
);
7310 spa_load_l2cache(spa
);
7311 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
7315 * We can't increment a feature while holding spa_vdev so we
7316 * have to do it in a synctask.
7321 tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
, txg
);
7322 dsl_sync_task_nowait(spa
->spa_dsl_pool
, spa_draid_feature_incr
,
7323 (void *)(uintptr_t)ndraid
, tx
);
7328 * We have to be careful when adding new vdevs to an existing pool.
7329 * If other threads start allocating from these vdevs before we
7330 * sync the config cache, and we lose power, then upon reboot we may
7331 * fail to open the pool because there are DVAs that the config cache
7332 * can't translate. Therefore, we first add the vdevs without
7333 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
7334 * and then let spa_config_update() initialize the new metaslabs.
7336 * spa_load() checks for added-but-not-initialized vdevs, so that
7337 * if we lose power at any point in this sequence, the remaining
7338 * steps will be completed the next time we load the pool.
7340 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
7342 mutex_enter(&spa_namespace_lock
);
7343 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
7344 spa_event_notify(spa
, NULL
, NULL
, ESC_ZFS_VDEV_ADD
);
7345 mutex_exit(&spa_namespace_lock
);
7351 * Attach a device to a vdev specified by its guid. The vdev type can be
7352 * a mirror, a raidz, or a leaf device that is also a top-level (e.g. a
7353 * single device). When the vdev is a single device, a mirror vdev will be
7354 * automatically inserted.
7356 * If 'replacing' is specified, the new device is intended to replace the
7357 * existing device; in this case the two devices are made into their own
7358 * mirror using the 'replacing' vdev, which is functionally identical to
7359 * the mirror vdev (it actually reuses all the same ops) but has a few
7360 * extra rules: you can't attach to it after it's been created, and upon
7361 * completion of resilvering, the first disk (the one being replaced)
7362 * is automatically detached.
7364 * If 'rebuild' is specified, then sequential reconstruction (a.ka. rebuild)
7365 * should be performed instead of traditional healing reconstruction. From
7366 * an administrators perspective these are both resilver operations.
7369 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
,
7372 uint64_t txg
, dtl_max_txg
;
7373 vdev_t
*rvd
= spa
->spa_root_vdev
;
7374 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
7376 char *oldvdpath
, *newvdpath
;
7377 int newvd_isspare
= B_FALSE
;
7380 ASSERT(spa_writeable(spa
));
7382 txg
= spa_vdev_enter(spa
);
7384 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7386 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7387 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7388 error
= (spa_has_checkpoint(spa
)) ?
7389 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7390 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7394 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_DEVICE_REBUILD
))
7395 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7397 if (dsl_scan_resilvering(spa_get_dsl(spa
)) ||
7398 dsl_scan_resilver_scheduled(spa_get_dsl(spa
))) {
7399 return (spa_vdev_exit(spa
, NULL
, txg
,
7400 ZFS_ERR_RESILVER_IN_PROGRESS
));
7403 if (vdev_rebuild_active(rvd
))
7404 return (spa_vdev_exit(spa
, NULL
, txg
,
7405 ZFS_ERR_REBUILD_IN_PROGRESS
));
7408 if (spa
->spa_vdev_removal
!= NULL
) {
7409 return (spa_vdev_exit(spa
, NULL
, txg
,
7410 ZFS_ERR_DEVRM_IN_PROGRESS
));
7414 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7416 boolean_t raidz
= oldvd
->vdev_ops
== &vdev_raidz_ops
;
7419 if (!spa_feature_is_enabled(spa
, SPA_FEATURE_RAIDZ_EXPANSION
))
7420 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7423 * Can't expand a raidz while prior expand is in progress.
7425 if (spa
->spa_raidz_expand
!= NULL
) {
7426 return (spa_vdev_exit(spa
, NULL
, txg
,
7427 ZFS_ERR_RAIDZ_EXPAND_IN_PROGRESS
));
7429 } else if (!oldvd
->vdev_ops
->vdev_op_leaf
) {
7430 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7436 pvd
= oldvd
->vdev_parent
;
7438 if (spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
7439 VDEV_ALLOC_ATTACH
) != 0)
7440 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
7442 if (newrootvd
->vdev_children
!= 1)
7443 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
7445 newvd
= newrootvd
->vdev_child
[0];
7447 if (!newvd
->vdev_ops
->vdev_op_leaf
)
7448 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
7450 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
7451 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
7454 * log, dedup and special vdevs should not be replaced by spares.
7456 if ((oldvd
->vdev_top
->vdev_alloc_bias
!= VDEV_BIAS_NONE
||
7457 oldvd
->vdev_top
->vdev_islog
) && newvd
->vdev_isspare
) {
7458 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7462 * A dRAID spare can only replace a child of its parent dRAID vdev.
7464 if (newvd
->vdev_ops
== &vdev_draid_spare_ops
&&
7465 oldvd
->vdev_top
!= vdev_draid_spare_get_parent(newvd
)) {
7466 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7471 * For rebuilds, the top vdev must support reconstruction
7472 * using only space maps. This means the only allowable
7473 * vdevs types are the root vdev, a mirror, or dRAID.
7476 if (pvd
->vdev_top
!= NULL
)
7477 tvd
= pvd
->vdev_top
;
7479 if (tvd
->vdev_ops
!= &vdev_mirror_ops
&&
7480 tvd
->vdev_ops
!= &vdev_root_ops
&&
7481 tvd
->vdev_ops
!= &vdev_draid_ops
) {
7482 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7488 * For attach, the only allowable parent is a mirror or
7489 * the root vdev. A raidz vdev can be attached to, but
7490 * you cannot attach to a raidz child.
7492 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7493 pvd
->vdev_ops
!= &vdev_root_ops
&&
7495 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7497 pvops
= &vdev_mirror_ops
;
7500 * Active hot spares can only be replaced by inactive hot
7503 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
7504 oldvd
->vdev_isspare
&&
7505 !spa_has_spare(spa
, newvd
->vdev_guid
))
7506 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7509 * If the source is a hot spare, and the parent isn't already a
7510 * spare, then we want to create a new hot spare. Otherwise, we
7511 * want to create a replacing vdev. The user is not allowed to
7512 * attach to a spared vdev child unless the 'isspare' state is
7513 * the same (spare replaces spare, non-spare replaces
7516 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
7517 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
7518 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7519 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
7520 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
7521 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7524 if (newvd
->vdev_isspare
)
7525 pvops
= &vdev_spare_ops
;
7527 pvops
= &vdev_replacing_ops
;
7531 * Make sure the new device is big enough.
7533 vdev_t
*min_vdev
= raidz
? oldvd
->vdev_child
[0] : oldvd
;
7534 if (newvd
->vdev_asize
< vdev_get_min_asize(min_vdev
))
7535 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
7538 * The new device cannot have a higher alignment requirement
7539 * than the top-level vdev.
7541 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
7542 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7545 * RAIDZ-expansion-specific checks.
7548 if (vdev_raidz_attach_check(newvd
) != 0)
7549 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
7552 * Fail early if a child is not healthy or being replaced
7554 for (int i
= 0; i
< oldvd
->vdev_children
; i
++) {
7555 if (vdev_is_dead(oldvd
->vdev_child
[i
]) ||
7556 !oldvd
->vdev_child
[i
]->vdev_ops
->vdev_op_leaf
) {
7557 return (spa_vdev_exit(spa
, newrootvd
, txg
,
7560 /* Also fail if reserved boot area is in-use */
7561 if (vdev_check_boot_reserve(spa
, oldvd
->vdev_child
[i
])
7563 return (spa_vdev_exit(spa
, newrootvd
, txg
,
7571 * Note: oldvdpath is freed by spa_strfree(), but
7572 * kmem_asprintf() is freed by kmem_strfree(), so we have to
7573 * move it to a spa_strdup-ed string.
7575 char *tmp
= kmem_asprintf("raidz%u-%u",
7576 (uint_t
)vdev_get_nparity(oldvd
), (uint_t
)oldvd
->vdev_id
);
7577 oldvdpath
= spa_strdup(tmp
);
7580 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
7582 newvdpath
= spa_strdup(newvd
->vdev_path
);
7585 * If this is an in-place replacement, update oldvd's path and devid
7586 * to make it distinguishable from newvd, and unopenable from now on.
7588 if (strcmp(oldvdpath
, newvdpath
) == 0) {
7589 spa_strfree(oldvd
->vdev_path
);
7590 oldvd
->vdev_path
= kmem_alloc(strlen(newvdpath
) + 5,
7592 (void) sprintf(oldvd
->vdev_path
, "%s/old",
7594 if (oldvd
->vdev_devid
!= NULL
) {
7595 spa_strfree(oldvd
->vdev_devid
);
7596 oldvd
->vdev_devid
= NULL
;
7598 spa_strfree(oldvdpath
);
7599 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
7603 * If the parent is not a mirror, or if we're replacing, insert the new
7604 * mirror/replacing/spare vdev above oldvd.
7606 if (!raidz
&& pvd
->vdev_ops
!= pvops
) {
7607 pvd
= vdev_add_parent(oldvd
, pvops
);
7608 ASSERT(pvd
->vdev_ops
== pvops
);
7609 ASSERT(oldvd
->vdev_parent
== pvd
);
7612 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
7615 * Extract the new device from its root and add it to pvd.
7617 vdev_remove_child(newrootvd
, newvd
);
7618 newvd
->vdev_id
= pvd
->vdev_children
;
7619 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
7620 vdev_add_child(pvd
, newvd
);
7623 * Reevaluate the parent vdev state.
7625 vdev_propagate_state(pvd
);
7627 tvd
= newvd
->vdev_top
;
7628 ASSERT(pvd
->vdev_top
== tvd
);
7629 ASSERT(tvd
->vdev_parent
== rvd
);
7631 vdev_config_dirty(tvd
);
7634 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
7635 * for any dmu_sync-ed blocks. It will propagate upward when
7636 * spa_vdev_exit() calls vdev_dtl_reassess().
7638 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
7642 * Wait for the youngest allocations and frees to sync,
7643 * and then wait for the deferral of those frees to finish.
7645 spa_vdev_config_exit(spa
, NULL
,
7646 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
7648 vdev_initialize_stop_all(tvd
, VDEV_INITIALIZE_ACTIVE
);
7649 vdev_trim_stop_all(tvd
, VDEV_TRIM_ACTIVE
);
7650 vdev_autotrim_stop_wait(tvd
);
7652 dtl_max_txg
= spa_vdev_config_enter(spa
);
7654 tvd
->vdev_rz_expanding
= B_TRUE
;
7656 vdev_dirty_leaves(tvd
, VDD_DTL
, dtl_max_txg
);
7657 vdev_config_dirty(tvd
);
7659 dmu_tx_t
*tx
= dmu_tx_create_assigned(spa
->spa_dsl_pool
,
7661 dsl_sync_task_nowait(spa
->spa_dsl_pool
, vdev_raidz_attach_sync
,
7665 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
7666 dtl_max_txg
- TXG_INITIAL
);
7668 if (newvd
->vdev_isspare
) {
7669 spa_spare_activate(newvd
);
7670 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_SPARE
);
7673 newvd_isspare
= newvd
->vdev_isspare
;
7676 * Mark newvd's DTL dirty in this txg.
7678 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
7681 * Schedule the resilver or rebuild to restart in the future.
7682 * We do this to ensure that dmu_sync-ed blocks have been
7683 * stitched into the respective datasets.
7686 newvd
->vdev_rebuild_txg
= txg
;
7690 newvd
->vdev_resilver_txg
= txg
;
7692 if (dsl_scan_resilvering(spa_get_dsl(spa
)) &&
7693 spa_feature_is_enabled(spa
,
7694 SPA_FEATURE_RESILVER_DEFER
)) {
7695 vdev_defer_resilver(newvd
);
7697 dsl_scan_restart_resilver(spa
->spa_dsl_pool
,
7703 if (spa
->spa_bootfs
)
7704 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
7706 spa_event_notify(spa
, newvd
, NULL
, ESC_ZFS_VDEV_ATTACH
);
7711 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
7713 spa_history_log_internal(spa
, "vdev attach", NULL
,
7714 "%s vdev=%s %s vdev=%s",
7715 replacing
&& newvd_isspare
? "spare in" :
7716 replacing
? "replace" : "attach", newvdpath
,
7717 replacing
? "for" : "to", oldvdpath
);
7719 spa_strfree(oldvdpath
);
7720 spa_strfree(newvdpath
);
7726 * Detach a device from a mirror or replacing vdev.
7728 * If 'replace_done' is specified, only detach if the parent
7729 * is a replacing or a spare vdev.
7732 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
7736 vdev_t
*rvd __maybe_unused
= spa
->spa_root_vdev
;
7737 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
7738 boolean_t unspare
= B_FALSE
;
7739 uint64_t unspare_guid
= 0;
7742 ASSERT(spa_writeable(spa
));
7744 txg
= spa_vdev_detach_enter(spa
, guid
);
7746 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
7749 * Besides being called directly from the userland through the
7750 * ioctl interface, spa_vdev_detach() can be potentially called
7751 * at the end of spa_vdev_resilver_done().
7753 * In the regular case, when we have a checkpoint this shouldn't
7754 * happen as we never empty the DTLs of a vdev during the scrub
7755 * [see comment in dsl_scan_done()]. Thus spa_vdev_resilvering_done()
7756 * should never get here when we have a checkpoint.
7758 * That said, even in a case when we checkpoint the pool exactly
7759 * as spa_vdev_resilver_done() calls this function everything
7760 * should be fine as the resilver will return right away.
7762 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7763 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
7764 error
= (spa_has_checkpoint(spa
)) ?
7765 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
7766 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
7770 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
7772 if (!vd
->vdev_ops
->vdev_op_leaf
)
7773 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7775 pvd
= vd
->vdev_parent
;
7778 * If the parent/child relationship is not as expected, don't do it.
7779 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
7780 * vdev that's replacing B with C. The user's intent in replacing
7781 * is to go from M(A,B) to M(A,C). If the user decides to cancel
7782 * the replace by detaching C, the expected behavior is to end up
7783 * M(A,B). But suppose that right after deciding to detach C,
7784 * the replacement of B completes. We would have M(A,C), and then
7785 * ask to detach C, which would leave us with just A -- not what
7786 * the user wanted. To prevent this, we make sure that the
7787 * parent/child relationship hasn't changed -- in this example,
7788 * that C's parent is still the replacing vdev R.
7790 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
7791 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7794 * Only 'replacing' or 'spare' vdevs can be replaced.
7796 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7797 pvd
->vdev_ops
!= &vdev_spare_ops
)
7798 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7800 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
7801 spa_version(spa
) >= SPA_VERSION_SPARES
);
7804 * Only mirror, replacing, and spare vdevs support detach.
7806 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
7807 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
7808 pvd
->vdev_ops
!= &vdev_spare_ops
)
7809 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
7812 * If this device has the only valid copy of some data,
7813 * we cannot safely detach it.
7815 if (vdev_dtl_required(vd
))
7816 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
7818 ASSERT(pvd
->vdev_children
>= 2);
7821 * If we are detaching the second disk from a replacing vdev, then
7822 * check to see if we changed the original vdev's path to have "/old"
7823 * at the end in spa_vdev_attach(). If so, undo that change now.
7825 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
7826 vd
->vdev_path
!= NULL
) {
7827 size_t len
= strlen(vd
->vdev_path
);
7829 for (int c
= 0; c
< pvd
->vdev_children
; c
++) {
7830 cvd
= pvd
->vdev_child
[c
];
7832 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
7835 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
7836 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
7837 spa_strfree(cvd
->vdev_path
);
7838 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
7845 * If we are detaching the original disk from a normal spare, then it
7846 * implies that the spare should become a real disk, and be removed
7847 * from the active spare list for the pool. dRAID spares on the
7848 * other hand are coupled to the pool and thus should never be removed
7849 * from the spares list.
7851 if (pvd
->vdev_ops
== &vdev_spare_ops
&& vd
->vdev_id
== 0) {
7852 vdev_t
*last_cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7854 if (last_cvd
->vdev_isspare
&&
7855 last_cvd
->vdev_ops
!= &vdev_draid_spare_ops
) {
7861 * Erase the disk labels so the disk can be used for other things.
7862 * This must be done after all other error cases are handled,
7863 * but before we disembowel vd (so we can still do I/O to it).
7864 * But if we can't do it, don't treat the error as fatal --
7865 * it may be that the unwritability of the disk is the reason
7866 * it's being detached!
7868 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
7871 * Remove vd from its parent and compact the parent's children.
7873 vdev_remove_child(pvd
, vd
);
7874 vdev_compact_children(pvd
);
7877 * Remember one of the remaining children so we can get tvd below.
7879 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
7882 * If we need to remove the remaining child from the list of hot spares,
7883 * do it now, marking the vdev as no longer a spare in the process.
7884 * We must do this before vdev_remove_parent(), because that can
7885 * change the GUID if it creates a new toplevel GUID. For a similar
7886 * reason, we must remove the spare now, in the same txg as the detach;
7887 * otherwise someone could attach a new sibling, change the GUID, and
7888 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
7891 ASSERT(cvd
->vdev_isspare
);
7892 spa_spare_remove(cvd
);
7893 unspare_guid
= cvd
->vdev_guid
;
7894 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
7895 cvd
->vdev_unspare
= B_TRUE
;
7899 * If the parent mirror/replacing vdev only has one child,
7900 * the parent is no longer needed. Remove it from the tree.
7902 if (pvd
->vdev_children
== 1) {
7903 if (pvd
->vdev_ops
== &vdev_spare_ops
)
7904 cvd
->vdev_unspare
= B_FALSE
;
7905 vdev_remove_parent(cvd
);
7909 * We don't set tvd until now because the parent we just removed
7910 * may have been the previous top-level vdev.
7912 tvd
= cvd
->vdev_top
;
7913 ASSERT(tvd
->vdev_parent
== rvd
);
7916 * Reevaluate the parent vdev state.
7918 vdev_propagate_state(cvd
);
7921 * If the 'autoexpand' property is set on the pool then automatically
7922 * try to expand the size of the pool. For example if the device we
7923 * just detached was smaller than the others, it may be possible to
7924 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
7925 * first so that we can obtain the updated sizes of the leaf vdevs.
7927 if (spa
->spa_autoexpand
) {
7929 vdev_expand(tvd
, txg
);
7932 vdev_config_dirty(tvd
);
7935 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
7936 * vd->vdev_detached is set and free vd's DTL object in syncing context.
7937 * But first make sure we're not on any *other* txg's DTL list, to
7938 * prevent vd from being accessed after it's freed.
7940 vdpath
= spa_strdup(vd
->vdev_path
? vd
->vdev_path
: "none");
7941 for (int t
= 0; t
< TXG_SIZE
; t
++)
7942 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
7943 vd
->vdev_detached
= B_TRUE
;
7944 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
7946 spa_event_notify(spa
, vd
, NULL
, ESC_ZFS_VDEV_REMOVE
);
7947 spa_notify_waiters(spa
);
7949 /* hang on to the spa before we release the lock */
7950 spa_open_ref(spa
, FTAG
);
7952 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
7954 spa_history_log_internal(spa
, "detach", NULL
,
7956 spa_strfree(vdpath
);
7959 * If this was the removal of the original device in a hot spare vdev,
7960 * then we want to go through and remove the device from the hot spare
7961 * list of every other pool.
7964 spa_t
*altspa
= NULL
;
7966 mutex_enter(&spa_namespace_lock
);
7967 while ((altspa
= spa_next(altspa
)) != NULL
) {
7968 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
7972 spa_open_ref(altspa
, FTAG
);
7973 mutex_exit(&spa_namespace_lock
);
7974 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
7975 mutex_enter(&spa_namespace_lock
);
7976 spa_close(altspa
, FTAG
);
7978 mutex_exit(&spa_namespace_lock
);
7980 /* search the rest of the vdevs for spares to remove */
7981 spa_vdev_resilver_done(spa
);
7984 /* all done with the spa; OK to release */
7985 mutex_enter(&spa_namespace_lock
);
7986 spa_close(spa
, FTAG
);
7987 mutex_exit(&spa_namespace_lock
);
7993 spa_vdev_initialize_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
7996 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
7998 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
8000 /* Look up vdev and ensure it's a leaf. */
8001 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
8002 if (vd
== NULL
|| vd
->vdev_detached
) {
8003 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8004 return (SET_ERROR(ENODEV
));
8005 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
8006 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8007 return (SET_ERROR(EINVAL
));
8008 } else if (!vdev_writeable(vd
)) {
8009 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8010 return (SET_ERROR(EROFS
));
8012 mutex_enter(&vd
->vdev_initialize_lock
);
8013 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8016 * When we activate an initialize action we check to see
8017 * if the vdev_initialize_thread is NULL. We do this instead
8018 * of using the vdev_initialize_state since there might be
8019 * a previous initialization process which has completed but
8020 * the thread is not exited.
8022 if (cmd_type
== POOL_INITIALIZE_START
&&
8023 (vd
->vdev_initialize_thread
!= NULL
||
8024 vd
->vdev_top
->vdev_removing
|| vd
->vdev_top
->vdev_rz_expanding
)) {
8025 mutex_exit(&vd
->vdev_initialize_lock
);
8026 return (SET_ERROR(EBUSY
));
8027 } else if (cmd_type
== POOL_INITIALIZE_CANCEL
&&
8028 (vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
&&
8029 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_SUSPENDED
)) {
8030 mutex_exit(&vd
->vdev_initialize_lock
);
8031 return (SET_ERROR(ESRCH
));
8032 } else if (cmd_type
== POOL_INITIALIZE_SUSPEND
&&
8033 vd
->vdev_initialize_state
!= VDEV_INITIALIZE_ACTIVE
) {
8034 mutex_exit(&vd
->vdev_initialize_lock
);
8035 return (SET_ERROR(ESRCH
));
8036 } else if (cmd_type
== POOL_INITIALIZE_UNINIT
&&
8037 vd
->vdev_initialize_thread
!= NULL
) {
8038 mutex_exit(&vd
->vdev_initialize_lock
);
8039 return (SET_ERROR(EBUSY
));
8043 case POOL_INITIALIZE_START
:
8044 vdev_initialize(vd
);
8046 case POOL_INITIALIZE_CANCEL
:
8047 vdev_initialize_stop(vd
, VDEV_INITIALIZE_CANCELED
, vd_list
);
8049 case POOL_INITIALIZE_SUSPEND
:
8050 vdev_initialize_stop(vd
, VDEV_INITIALIZE_SUSPENDED
, vd_list
);
8052 case POOL_INITIALIZE_UNINIT
:
8053 vdev_uninitialize(vd
);
8056 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
8058 mutex_exit(&vd
->vdev_initialize_lock
);
8064 spa_vdev_initialize(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
,
8065 nvlist_t
*vdev_errlist
)
8067 int total_errors
= 0;
8070 list_create(&vd_list
, sizeof (vdev_t
),
8071 offsetof(vdev_t
, vdev_initialize_node
));
8074 * We hold the namespace lock through the whole function
8075 * to prevent any changes to the pool while we're starting or
8076 * stopping initialization. The config and state locks are held so that
8077 * we can properly assess the vdev state before we commit to
8078 * the initializing operation.
8080 mutex_enter(&spa_namespace_lock
);
8082 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
8083 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
8084 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
8086 int error
= spa_vdev_initialize_impl(spa
, vdev_guid
, cmd_type
,
8089 char guid_as_str
[MAXNAMELEN
];
8091 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
8092 "%llu", (unsigned long long)vdev_guid
);
8093 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
8098 /* Wait for all initialize threads to stop. */
8099 vdev_initialize_stop_wait(spa
, &vd_list
);
8101 /* Sync out the initializing state */
8102 txg_wait_synced(spa
->spa_dsl_pool
, 0);
8103 mutex_exit(&spa_namespace_lock
);
8105 list_destroy(&vd_list
);
8107 return (total_errors
);
8111 spa_vdev_trim_impl(spa_t
*spa
, uint64_t guid
, uint64_t cmd_type
,
8112 uint64_t rate
, boolean_t partial
, boolean_t secure
, list_t
*vd_list
)
8114 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
8116 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
8118 /* Look up vdev and ensure it's a leaf. */
8119 vdev_t
*vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
8120 if (vd
== NULL
|| vd
->vdev_detached
) {
8121 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8122 return (SET_ERROR(ENODEV
));
8123 } else if (!vd
->vdev_ops
->vdev_op_leaf
|| !vdev_is_concrete(vd
)) {
8124 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8125 return (SET_ERROR(EINVAL
));
8126 } else if (!vdev_writeable(vd
)) {
8127 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8128 return (SET_ERROR(EROFS
));
8129 } else if (!vd
->vdev_has_trim
) {
8130 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8131 return (SET_ERROR(EOPNOTSUPP
));
8132 } else if (secure
&& !vd
->vdev_has_securetrim
) {
8133 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8134 return (SET_ERROR(EOPNOTSUPP
));
8136 mutex_enter(&vd
->vdev_trim_lock
);
8137 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
8140 * When we activate a TRIM action we check to see if the
8141 * vdev_trim_thread is NULL. We do this instead of using the
8142 * vdev_trim_state since there might be a previous TRIM process
8143 * which has completed but the thread is not exited.
8145 if (cmd_type
== POOL_TRIM_START
&&
8146 (vd
->vdev_trim_thread
!= NULL
|| vd
->vdev_top
->vdev_removing
||
8147 vd
->vdev_top
->vdev_rz_expanding
)) {
8148 mutex_exit(&vd
->vdev_trim_lock
);
8149 return (SET_ERROR(EBUSY
));
8150 } else if (cmd_type
== POOL_TRIM_CANCEL
&&
8151 (vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
&&
8152 vd
->vdev_trim_state
!= VDEV_TRIM_SUSPENDED
)) {
8153 mutex_exit(&vd
->vdev_trim_lock
);
8154 return (SET_ERROR(ESRCH
));
8155 } else if (cmd_type
== POOL_TRIM_SUSPEND
&&
8156 vd
->vdev_trim_state
!= VDEV_TRIM_ACTIVE
) {
8157 mutex_exit(&vd
->vdev_trim_lock
);
8158 return (SET_ERROR(ESRCH
));
8162 case POOL_TRIM_START
:
8163 vdev_trim(vd
, rate
, partial
, secure
);
8165 case POOL_TRIM_CANCEL
:
8166 vdev_trim_stop(vd
, VDEV_TRIM_CANCELED
, vd_list
);
8168 case POOL_TRIM_SUSPEND
:
8169 vdev_trim_stop(vd
, VDEV_TRIM_SUSPENDED
, vd_list
);
8172 panic("invalid cmd_type %llu", (unsigned long long)cmd_type
);
8174 mutex_exit(&vd
->vdev_trim_lock
);
8180 * Initiates a manual TRIM for the requested vdevs. This kicks off individual
8181 * TRIM threads for each child vdev. These threads pass over all of the free
8182 * space in the vdev's metaslabs and issues TRIM commands for that space.
8185 spa_vdev_trim(spa_t
*spa
, nvlist_t
*nv
, uint64_t cmd_type
, uint64_t rate
,
8186 boolean_t partial
, boolean_t secure
, nvlist_t
*vdev_errlist
)
8188 int total_errors
= 0;
8191 list_create(&vd_list
, sizeof (vdev_t
),
8192 offsetof(vdev_t
, vdev_trim_node
));
8195 * We hold the namespace lock through the whole function
8196 * to prevent any changes to the pool while we're starting or
8197 * stopping TRIM. The config and state locks are held so that
8198 * we can properly assess the vdev state before we commit to
8199 * the TRIM operation.
8201 mutex_enter(&spa_namespace_lock
);
8203 for (nvpair_t
*pair
= nvlist_next_nvpair(nv
, NULL
);
8204 pair
!= NULL
; pair
= nvlist_next_nvpair(nv
, pair
)) {
8205 uint64_t vdev_guid
= fnvpair_value_uint64(pair
);
8207 int error
= spa_vdev_trim_impl(spa
, vdev_guid
, cmd_type
,
8208 rate
, partial
, secure
, &vd_list
);
8210 char guid_as_str
[MAXNAMELEN
];
8212 (void) snprintf(guid_as_str
, sizeof (guid_as_str
),
8213 "%llu", (unsigned long long)vdev_guid
);
8214 fnvlist_add_int64(vdev_errlist
, guid_as_str
, error
);
8219 /* Wait for all TRIM threads to stop. */
8220 vdev_trim_stop_wait(spa
, &vd_list
);
8222 /* Sync out the TRIM state */
8223 txg_wait_synced(spa
->spa_dsl_pool
, 0);
8224 mutex_exit(&spa_namespace_lock
);
8226 list_destroy(&vd_list
);
8228 return (total_errors
);
8232 * Split a set of devices from their mirrors, and create a new pool from them.
8235 spa_vdev_split_mirror(spa_t
*spa
, const char *newname
, nvlist_t
*config
,
8236 nvlist_t
*props
, boolean_t exp
)
8239 uint64_t txg
, *glist
;
8241 uint_t c
, children
, lastlog
;
8242 nvlist_t
**child
, *nvl
, *tmp
;
8244 const char *altroot
= NULL
;
8245 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
8246 boolean_t activate_slog
;
8248 ASSERT(spa_writeable(spa
));
8250 txg
= spa_vdev_enter(spa
);
8252 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
8253 if (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
)) {
8254 error
= (spa_has_checkpoint(spa
)) ?
8255 ZFS_ERR_CHECKPOINT_EXISTS
: ZFS_ERR_DISCARDING_CHECKPOINT
;
8256 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
8259 /* clear the log and flush everything up to now */
8260 activate_slog
= spa_passivate_log(spa
);
8261 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
8262 error
= spa_reset_logs(spa
);
8263 txg
= spa_vdev_config_enter(spa
);
8266 spa_activate_log(spa
);
8269 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
8271 /* check new spa name before going any further */
8272 if (spa_lookup(newname
) != NULL
)
8273 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
8276 * scan through all the children to ensure they're all mirrors
8278 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
8279 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
8281 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
8283 /* first, check to ensure we've got the right child count */
8284 rvd
= spa
->spa_root_vdev
;
8286 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
8287 vdev_t
*vd
= rvd
->vdev_child
[c
];
8289 /* don't count the holes & logs as children */
8290 if (vd
->vdev_islog
|| (vd
->vdev_ops
!= &vdev_indirect_ops
&&
8291 !vdev_is_concrete(vd
))) {
8299 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
8300 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
8302 /* next, ensure no spare or cache devices are part of the split */
8303 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
8304 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
8305 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
8307 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
8308 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
8310 /* then, loop over each vdev and validate it */
8311 for (c
= 0; c
< children
; c
++) {
8312 uint64_t is_hole
= 0;
8314 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
8318 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
8319 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
8322 error
= SET_ERROR(EINVAL
);
8327 /* deal with indirect vdevs */
8328 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ops
==
8332 /* which disk is going to be split? */
8333 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
8335 error
= SET_ERROR(EINVAL
);
8339 /* look it up in the spa */
8340 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
8341 if (vml
[c
] == NULL
) {
8342 error
= SET_ERROR(ENODEV
);
8346 /* make sure there's nothing stopping the split */
8347 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
8348 vml
[c
]->vdev_islog
||
8349 !vdev_is_concrete(vml
[c
]) ||
8350 vml
[c
]->vdev_isspare
||
8351 vml
[c
]->vdev_isl2cache
||
8352 !vdev_writeable(vml
[c
]) ||
8353 vml
[c
]->vdev_children
!= 0 ||
8354 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
8355 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
8356 error
= SET_ERROR(EINVAL
);
8360 if (vdev_dtl_required(vml
[c
]) ||
8361 vdev_resilver_needed(vml
[c
], NULL
, NULL
)) {
8362 error
= SET_ERROR(EBUSY
);
8366 /* we need certain info from the top level */
8367 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
8368 vml
[c
]->vdev_top
->vdev_ms_array
);
8369 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
8370 vml
[c
]->vdev_top
->vdev_ms_shift
);
8371 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
8372 vml
[c
]->vdev_top
->vdev_asize
);
8373 fnvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
8374 vml
[c
]->vdev_top
->vdev_ashift
);
8376 /* transfer per-vdev ZAPs */
8377 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
8378 VERIFY0(nvlist_add_uint64(child
[c
],
8379 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
8381 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
8382 VERIFY0(nvlist_add_uint64(child
[c
],
8383 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
8384 vml
[c
]->vdev_parent
->vdev_top_zap
));
8388 kmem_free(vml
, children
* sizeof (vdev_t
*));
8389 kmem_free(glist
, children
* sizeof (uint64_t));
8390 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
8393 /* stop writers from using the disks */
8394 for (c
= 0; c
< children
; c
++) {
8396 vml
[c
]->vdev_offline
= B_TRUE
;
8398 vdev_reopen(spa
->spa_root_vdev
);
8401 * Temporarily record the splitting vdevs in the spa config. This
8402 * will disappear once the config is regenerated.
8404 nvl
= fnvlist_alloc();
8405 fnvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
, glist
, children
);
8406 kmem_free(glist
, children
* sizeof (uint64_t));
8408 mutex_enter(&spa
->spa_props_lock
);
8409 fnvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
, nvl
);
8410 mutex_exit(&spa
->spa_props_lock
);
8411 spa
->spa_config_splitting
= nvl
;
8412 vdev_config_dirty(spa
->spa_root_vdev
);
8414 /* configure and create the new pool */
8415 fnvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
);
8416 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
8417 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
);
8418 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
, spa_version(spa
));
8419 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, spa
->spa_config_txg
);
8420 fnvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
8421 spa_generate_guid(NULL
));
8422 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
8423 (void) nvlist_lookup_string(props
,
8424 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
8426 /* add the new pool to the namespace */
8427 newspa
= spa_add(newname
, config
, altroot
);
8428 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
8429 newspa
->spa_config_txg
= spa
->spa_config_txg
;
8430 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
8432 /* release the spa config lock, retaining the namespace lock */
8433 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
8435 if (zio_injection_enabled
)
8436 zio_handle_panic_injection(spa
, FTAG
, 1);
8438 spa_activate(newspa
, spa_mode_global
);
8439 spa_async_suspend(newspa
);
8442 * Temporarily stop the initializing and TRIM activity. We set the
8443 * state to ACTIVE so that we know to resume initializing or TRIM
8444 * once the split has completed.
8446 list_t vd_initialize_list
;
8447 list_create(&vd_initialize_list
, sizeof (vdev_t
),
8448 offsetof(vdev_t
, vdev_initialize_node
));
8450 list_t vd_trim_list
;
8451 list_create(&vd_trim_list
, sizeof (vdev_t
),
8452 offsetof(vdev_t
, vdev_trim_node
));
8454 for (c
= 0; c
< children
; c
++) {
8455 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
8456 mutex_enter(&vml
[c
]->vdev_initialize_lock
);
8457 vdev_initialize_stop(vml
[c
],
8458 VDEV_INITIALIZE_ACTIVE
, &vd_initialize_list
);
8459 mutex_exit(&vml
[c
]->vdev_initialize_lock
);
8461 mutex_enter(&vml
[c
]->vdev_trim_lock
);
8462 vdev_trim_stop(vml
[c
], VDEV_TRIM_ACTIVE
, &vd_trim_list
);
8463 mutex_exit(&vml
[c
]->vdev_trim_lock
);
8467 vdev_initialize_stop_wait(spa
, &vd_initialize_list
);
8468 vdev_trim_stop_wait(spa
, &vd_trim_list
);
8470 list_destroy(&vd_initialize_list
);
8471 list_destroy(&vd_trim_list
);
8473 newspa
->spa_config_source
= SPA_CONFIG_SRC_SPLIT
;
8474 newspa
->spa_is_splitting
= B_TRUE
;
8476 /* create the new pool from the disks of the original pool */
8477 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
);
8481 /* if that worked, generate a real config for the new pool */
8482 if (newspa
->spa_root_vdev
!= NULL
) {
8483 newspa
->spa_config_splitting
= fnvlist_alloc();
8484 fnvlist_add_uint64(newspa
->spa_config_splitting
,
8485 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
));
8486 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
8491 if (props
!= NULL
) {
8492 spa_configfile_set(newspa
, props
, B_FALSE
);
8493 error
= spa_prop_set(newspa
, props
);
8498 /* flush everything */
8499 txg
= spa_vdev_config_enter(newspa
);
8500 vdev_config_dirty(newspa
->spa_root_vdev
);
8501 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
8503 if (zio_injection_enabled
)
8504 zio_handle_panic_injection(spa
, FTAG
, 2);
8506 spa_async_resume(newspa
);
8508 /* finally, update the original pool's config */
8509 txg
= spa_vdev_config_enter(spa
);
8510 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
8511 error
= dmu_tx_assign(tx
, TXG_WAIT
);
8514 for (c
= 0; c
< children
; c
++) {
8515 if (vml
[c
] != NULL
&& vml
[c
]->vdev_ops
!= &vdev_indirect_ops
) {
8516 vdev_t
*tvd
= vml
[c
]->vdev_top
;
8519 * Need to be sure the detachable VDEV is not
8520 * on any *other* txg's DTL list to prevent it
8521 * from being accessed after it's freed.
8523 for (int t
= 0; t
< TXG_SIZE
; t
++) {
8524 (void) txg_list_remove_this(
8525 &tvd
->vdev_dtl_list
, vml
[c
], t
);
8530 spa_history_log_internal(spa
, "detach", tx
,
8531 "vdev=%s", vml
[c
]->vdev_path
);
8536 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
8537 vdev_config_dirty(spa
->spa_root_vdev
);
8538 spa
->spa_config_splitting
= NULL
;
8542 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
8544 if (zio_injection_enabled
)
8545 zio_handle_panic_injection(spa
, FTAG
, 3);
8547 /* split is complete; log a history record */
8548 spa_history_log_internal(newspa
, "split", NULL
,
8549 "from pool %s", spa_name(spa
));
8551 newspa
->spa_is_splitting
= B_FALSE
;
8552 kmem_free(vml
, children
* sizeof (vdev_t
*));
8554 /* if we're not going to mount the filesystems in userland, export */
8556 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
8563 spa_deactivate(newspa
);
8566 txg
= spa_vdev_config_enter(spa
);
8568 /* re-online all offlined disks */
8569 for (c
= 0; c
< children
; c
++) {
8571 vml
[c
]->vdev_offline
= B_FALSE
;
8574 /* restart initializing or trimming disks as necessary */
8575 spa_async_request(spa
, SPA_ASYNC_INITIALIZE_RESTART
);
8576 spa_async_request(spa
, SPA_ASYNC_TRIM_RESTART
);
8577 spa_async_request(spa
, SPA_ASYNC_AUTOTRIM_RESTART
);
8579 vdev_reopen(spa
->spa_root_vdev
);
8581 nvlist_free(spa
->spa_config_splitting
);
8582 spa
->spa_config_splitting
= NULL
;
8583 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
8585 kmem_free(vml
, children
* sizeof (vdev_t
*));
8590 * Find any device that's done replacing, or a vdev marked 'unspare' that's
8591 * currently spared, so we can detach it.
8594 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
8596 vdev_t
*newvd
, *oldvd
;
8598 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8599 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
8605 * Check for a completed replacement. We always consider the first
8606 * vdev in the list to be the oldest vdev, and the last one to be
8607 * the newest (see spa_vdev_attach() for how that works). In
8608 * the case where the newest vdev is faulted, we will not automatically
8609 * remove it after a resilver completes. This is OK as it will require
8610 * user intervention to determine which disk the admin wishes to keep.
8612 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
8613 ASSERT(vd
->vdev_children
> 1);
8615 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
8616 oldvd
= vd
->vdev_child
[0];
8618 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8619 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8620 !vdev_dtl_required(oldvd
))
8625 * Check for a completed resilver with the 'unspare' flag set.
8626 * Also potentially update faulted state.
8628 if (vd
->vdev_ops
== &vdev_spare_ops
) {
8629 vdev_t
*first
= vd
->vdev_child
[0];
8630 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
8632 if (last
->vdev_unspare
) {
8635 } else if (first
->vdev_unspare
) {
8642 if (oldvd
!= NULL
&&
8643 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
8644 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
8645 !vdev_dtl_required(oldvd
))
8648 vdev_propagate_state(vd
);
8651 * If there are more than two spares attached to a disk,
8652 * and those spares are not required, then we want to
8653 * attempt to free them up now so that they can be used
8654 * by other pools. Once we're back down to a single
8655 * disk+spare, we stop removing them.
8657 if (vd
->vdev_children
> 2) {
8658 newvd
= vd
->vdev_child
[1];
8660 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
8661 vdev_dtl_empty(last
, DTL_MISSING
) &&
8662 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
8663 !vdev_dtl_required(newvd
))
8672 spa_vdev_resilver_done(spa_t
*spa
)
8674 vdev_t
*vd
, *pvd
, *ppvd
;
8675 uint64_t guid
, sguid
, pguid
, ppguid
;
8677 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8679 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
8680 pvd
= vd
->vdev_parent
;
8681 ppvd
= pvd
->vdev_parent
;
8682 guid
= vd
->vdev_guid
;
8683 pguid
= pvd
->vdev_guid
;
8684 ppguid
= ppvd
->vdev_guid
;
8687 * If we have just finished replacing a hot spared device, then
8688 * we need to detach the parent's first child (the original hot
8691 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
8692 ppvd
->vdev_children
== 2) {
8693 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
8694 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
8696 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
8698 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8699 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
8701 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
8703 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
8706 spa_config_exit(spa
, SCL_ALL
, FTAG
);
8709 * If a detach was not performed above replace waiters will not have
8710 * been notified. In which case we must do so now.
8712 spa_notify_waiters(spa
);
8716 * Update the stored path or FRU for this vdev.
8719 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
8723 boolean_t sync
= B_FALSE
;
8725 ASSERT(spa_writeable(spa
));
8727 spa_vdev_state_enter(spa
, SCL_ALL
);
8729 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
8730 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
8732 if (!vd
->vdev_ops
->vdev_op_leaf
)
8733 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
8736 if (strcmp(value
, vd
->vdev_path
) != 0) {
8737 spa_strfree(vd
->vdev_path
);
8738 vd
->vdev_path
= spa_strdup(value
);
8742 if (vd
->vdev_fru
== NULL
) {
8743 vd
->vdev_fru
= spa_strdup(value
);
8745 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
8746 spa_strfree(vd
->vdev_fru
);
8747 vd
->vdev_fru
= spa_strdup(value
);
8752 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
8756 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
8758 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
8762 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
8764 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
8768 * ==========================================================================
8770 * ==========================================================================
8773 spa_scrub_pause_resume(spa_t
*spa
, pool_scrub_cmd_t cmd
)
8775 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8777 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8778 return (SET_ERROR(EBUSY
));
8780 return (dsl_scrub_set_pause_resume(spa
->spa_dsl_pool
, cmd
));
8784 spa_scan_stop(spa_t
*spa
)
8786 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8787 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
8788 return (SET_ERROR(EBUSY
));
8790 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
8794 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
8796 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
8798 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
8799 return (SET_ERROR(ENOTSUP
));
8801 if (func
== POOL_SCAN_RESILVER
&&
8802 !spa_feature_is_enabled(spa
, SPA_FEATURE_RESILVER_DEFER
))
8803 return (SET_ERROR(ENOTSUP
));
8806 * If a resilver was requested, but there is no DTL on a
8807 * writeable leaf device, we have nothing to do.
8809 if (func
== POOL_SCAN_RESILVER
&&
8810 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
8811 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
8815 if (func
== POOL_SCAN_ERRORSCRUB
&&
8816 !spa_feature_is_enabled(spa
, SPA_FEATURE_HEAD_ERRLOG
))
8817 return (SET_ERROR(ENOTSUP
));
8819 return (dsl_scan(spa
->spa_dsl_pool
, func
));
8823 * ==========================================================================
8824 * SPA async task processing
8825 * ==========================================================================
8829 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
8831 if (vd
->vdev_remove_wanted
) {
8832 vd
->vdev_remove_wanted
= B_FALSE
;
8833 vd
->vdev_delayed_close
= B_FALSE
;
8834 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
8837 * We want to clear the stats, but we don't want to do a full
8838 * vdev_clear() as that will cause us to throw away
8839 * degraded/faulted state as well as attempt to reopen the
8840 * device, all of which is a waste.
8842 vd
->vdev_stat
.vs_read_errors
= 0;
8843 vd
->vdev_stat
.vs_write_errors
= 0;
8844 vd
->vdev_stat
.vs_checksum_errors
= 0;
8846 vdev_state_dirty(vd
->vdev_top
);
8848 /* Tell userspace that the vdev is gone. */
8849 zfs_post_remove(spa
, vd
);
8852 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8853 spa_async_remove(spa
, vd
->vdev_child
[c
]);
8857 spa_async_fault_vdev(spa_t
*spa
, vdev_t
*vd
)
8859 if (vd
->vdev_fault_wanted
) {
8860 vd
->vdev_fault_wanted
= B_FALSE
;
8861 vdev_set_state(vd
, B_TRUE
, VDEV_STATE_FAULTED
,
8862 VDEV_AUX_ERR_EXCEEDED
);
8865 for (int c
= 0; c
< vd
->vdev_children
; c
++)
8866 spa_async_fault_vdev(spa
, vd
->vdev_child
[c
]);
8870 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
8872 if (!spa
->spa_autoexpand
)
8875 for (int c
= 0; c
< vd
->vdev_children
; c
++) {
8876 vdev_t
*cvd
= vd
->vdev_child
[c
];
8877 spa_async_autoexpand(spa
, cvd
);
8880 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
8883 spa_event_notify(vd
->vdev_spa
, vd
, NULL
, ESC_ZFS_VDEV_AUTOEXPAND
);
8886 static __attribute__((noreturn
)) void
8887 spa_async_thread(void *arg
)
8889 spa_t
*spa
= (spa_t
*)arg
;
8890 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
8893 ASSERT(spa
->spa_sync_on
);
8895 mutex_enter(&spa
->spa_async_lock
);
8896 tasks
= spa
->spa_async_tasks
;
8897 spa
->spa_async_tasks
= 0;
8898 mutex_exit(&spa
->spa_async_lock
);
8901 * See if the config needs to be updated.
8903 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
8904 uint64_t old_space
, new_space
;
8906 mutex_enter(&spa_namespace_lock
);
8907 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
8908 old_space
+= metaslab_class_get_space(spa_special_class(spa
));
8909 old_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8910 old_space
+= metaslab_class_get_space(
8911 spa_embedded_log_class(spa
));
8913 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
8915 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
8916 new_space
+= metaslab_class_get_space(spa_special_class(spa
));
8917 new_space
+= metaslab_class_get_space(spa_dedup_class(spa
));
8918 new_space
+= metaslab_class_get_space(
8919 spa_embedded_log_class(spa
));
8920 mutex_exit(&spa_namespace_lock
);
8923 * If the pool grew as a result of the config update,
8924 * then log an internal history event.
8926 if (new_space
!= old_space
) {
8927 spa_history_log_internal(spa
, "vdev online", NULL
,
8928 "pool '%s' size: %llu(+%llu)",
8929 spa_name(spa
), (u_longlong_t
)new_space
,
8930 (u_longlong_t
)(new_space
- old_space
));
8935 * See if any devices need to be marked REMOVED.
8937 if (tasks
& SPA_ASYNC_REMOVE
) {
8938 spa_vdev_state_enter(spa
, SCL_NONE
);
8939 spa_async_remove(spa
, spa
->spa_root_vdev
);
8940 for (int i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
8941 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
8942 for (int i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
8943 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
8944 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8947 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
8948 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8949 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
8950 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8954 * See if any devices need to be marked faulted.
8956 if (tasks
& SPA_ASYNC_FAULT_VDEV
) {
8957 spa_vdev_state_enter(spa
, SCL_NONE
);
8958 spa_async_fault_vdev(spa
, spa
->spa_root_vdev
);
8959 (void) spa_vdev_state_exit(spa
, NULL
, 0);
8963 * If any devices are done replacing, detach them.
8965 if (tasks
& SPA_ASYNC_RESILVER_DONE
||
8966 tasks
& SPA_ASYNC_REBUILD_DONE
||
8967 tasks
& SPA_ASYNC_DETACH_SPARE
) {
8968 spa_vdev_resilver_done(spa
);
8972 * Kick off a resilver.
8974 if (tasks
& SPA_ASYNC_RESILVER
&&
8975 !vdev_rebuild_active(spa
->spa_root_vdev
) &&
8976 (!dsl_scan_resilvering(dp
) ||
8977 !spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_RESILVER_DEFER
)))
8978 dsl_scan_restart_resilver(dp
, 0);
8980 if (tasks
& SPA_ASYNC_INITIALIZE_RESTART
) {
8981 mutex_enter(&spa_namespace_lock
);
8982 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8983 vdev_initialize_restart(spa
->spa_root_vdev
);
8984 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8985 mutex_exit(&spa_namespace_lock
);
8988 if (tasks
& SPA_ASYNC_TRIM_RESTART
) {
8989 mutex_enter(&spa_namespace_lock
);
8990 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8991 vdev_trim_restart(spa
->spa_root_vdev
);
8992 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
8993 mutex_exit(&spa_namespace_lock
);
8996 if (tasks
& SPA_ASYNC_AUTOTRIM_RESTART
) {
8997 mutex_enter(&spa_namespace_lock
);
8998 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
8999 vdev_autotrim_restart(spa
);
9000 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9001 mutex_exit(&spa_namespace_lock
);
9005 * Kick off L2 cache whole device TRIM.
9007 if (tasks
& SPA_ASYNC_L2CACHE_TRIM
) {
9008 mutex_enter(&spa_namespace_lock
);
9009 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
9010 vdev_trim_l2arc(spa
);
9011 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
9012 mutex_exit(&spa_namespace_lock
);
9016 * Kick off L2 cache rebuilding.
9018 if (tasks
& SPA_ASYNC_L2CACHE_REBUILD
) {
9019 mutex_enter(&spa_namespace_lock
);
9020 spa_config_enter(spa
, SCL_L2ARC
, FTAG
, RW_READER
);
9021 l2arc_spa_rebuild_start(spa
);
9022 spa_config_exit(spa
, SCL_L2ARC
, FTAG
);
9023 mutex_exit(&spa_namespace_lock
);
9027 * Let the world know that we're done.
9029 mutex_enter(&spa
->spa_async_lock
);
9030 spa
->spa_async_thread
= NULL
;
9031 cv_broadcast(&spa
->spa_async_cv
);
9032 mutex_exit(&spa
->spa_async_lock
);
9037 spa_async_suspend(spa_t
*spa
)
9039 mutex_enter(&spa
->spa_async_lock
);
9040 spa
->spa_async_suspended
++;
9041 while (spa
->spa_async_thread
!= NULL
)
9042 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
9043 mutex_exit(&spa
->spa_async_lock
);
9045 spa_vdev_remove_suspend(spa
);
9047 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
9048 if (condense_thread
!= NULL
)
9049 zthr_cancel(condense_thread
);
9051 zthr_t
*raidz_expand_thread
= spa
->spa_raidz_expand_zthr
;
9052 if (raidz_expand_thread
!= NULL
)
9053 zthr_cancel(raidz_expand_thread
);
9055 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
9056 if (discard_thread
!= NULL
)
9057 zthr_cancel(discard_thread
);
9059 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
9060 if (ll_delete_thread
!= NULL
)
9061 zthr_cancel(ll_delete_thread
);
9063 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
9064 if (ll_condense_thread
!= NULL
)
9065 zthr_cancel(ll_condense_thread
);
9069 spa_async_resume(spa_t
*spa
)
9071 mutex_enter(&spa
->spa_async_lock
);
9072 ASSERT(spa
->spa_async_suspended
!= 0);
9073 spa
->spa_async_suspended
--;
9074 mutex_exit(&spa
->spa_async_lock
);
9075 spa_restart_removal(spa
);
9077 zthr_t
*condense_thread
= spa
->spa_condense_zthr
;
9078 if (condense_thread
!= NULL
)
9079 zthr_resume(condense_thread
);
9081 zthr_t
*raidz_expand_thread
= spa
->spa_raidz_expand_zthr
;
9082 if (raidz_expand_thread
!= NULL
)
9083 zthr_resume(raidz_expand_thread
);
9085 zthr_t
*discard_thread
= spa
->spa_checkpoint_discard_zthr
;
9086 if (discard_thread
!= NULL
)
9087 zthr_resume(discard_thread
);
9089 zthr_t
*ll_delete_thread
= spa
->spa_livelist_delete_zthr
;
9090 if (ll_delete_thread
!= NULL
)
9091 zthr_resume(ll_delete_thread
);
9093 zthr_t
*ll_condense_thread
= spa
->spa_livelist_condense_zthr
;
9094 if (ll_condense_thread
!= NULL
)
9095 zthr_resume(ll_condense_thread
);
9099 spa_async_tasks_pending(spa_t
*spa
)
9101 uint_t non_config_tasks
;
9103 boolean_t config_task_suspended
;
9105 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
9106 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
9107 if (spa
->spa_ccw_fail_time
== 0) {
9108 config_task_suspended
= B_FALSE
;
9110 config_task_suspended
=
9111 (gethrtime() - spa
->spa_ccw_fail_time
) <
9112 ((hrtime_t
)zfs_ccw_retry_interval
* NANOSEC
);
9115 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
9119 spa_async_dispatch(spa_t
*spa
)
9121 mutex_enter(&spa
->spa_async_lock
);
9122 if (spa_async_tasks_pending(spa
) &&
9123 !spa
->spa_async_suspended
&&
9124 spa
->spa_async_thread
== NULL
)
9125 spa
->spa_async_thread
= thread_create(NULL
, 0,
9126 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
9127 mutex_exit(&spa
->spa_async_lock
);
9131 spa_async_request(spa_t
*spa
, int task
)
9133 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
9134 mutex_enter(&spa
->spa_async_lock
);
9135 spa
->spa_async_tasks
|= task
;
9136 mutex_exit(&spa
->spa_async_lock
);
9140 spa_async_tasks(spa_t
*spa
)
9142 return (spa
->spa_async_tasks
);
9146 * ==========================================================================
9147 * SPA syncing routines
9148 * ==========================================================================
9153 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
9157 bpobj_enqueue(bpo
, bp
, bp_freed
, tx
);
9162 bpobj_enqueue_alloc_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
9164 return (bpobj_enqueue_cb(arg
, bp
, B_FALSE
, tx
));
9168 bpobj_enqueue_free_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
9170 return (bpobj_enqueue_cb(arg
, bp
, B_TRUE
, tx
));
9174 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
9178 zio_nowait(zio_free_sync(pio
, pio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
9184 bpobj_spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, boolean_t bp_freed
,
9188 return (spa_free_sync_cb(arg
, bp
, tx
));
9192 * Note: this simple function is not inlined to make it easier to dtrace the
9193 * amount of time spent syncing frees.
9196 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
9198 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
9199 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
9200 VERIFY(zio_wait(zio
) == 0);
9204 * Note: this simple function is not inlined to make it easier to dtrace the
9205 * amount of time spent syncing deferred frees.
9208 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
9210 if (spa_sync_pass(spa
) != 1)
9215 * If the log space map feature is active, we stop deferring
9216 * frees to the next TXG and therefore running this function
9217 * would be considered a no-op as spa_deferred_bpobj should
9218 * not have any entries.
9220 * That said we run this function anyway (instead of returning
9221 * immediately) for the edge-case scenario where we just
9222 * activated the log space map feature in this TXG but we have
9223 * deferred frees from the previous TXG.
9225 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
9226 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
9227 bpobj_spa_free_sync_cb
, zio
, tx
), ==, 0);
9228 VERIFY0(zio_wait(zio
));
9232 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
9234 char *packed
= NULL
;
9239 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
9242 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
9243 * information. This avoids the dmu_buf_will_dirty() path and
9244 * saves us a pre-read to get data we don't actually care about.
9246 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
9247 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
9249 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
9251 memset(packed
+ nvsize
, 0, bufsize
- nvsize
);
9253 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
9255 vmem_free(packed
, bufsize
);
9257 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
9258 dmu_buf_will_dirty(db
, tx
);
9259 *(uint64_t *)db
->db_data
= nvsize
;
9260 dmu_buf_rele(db
, FTAG
);
9264 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
9265 const char *config
, const char *entry
)
9275 * Update the MOS nvlist describing the list of available devices.
9276 * spa_validate_aux() will have already made sure this nvlist is
9277 * valid and the vdevs are labeled appropriately.
9279 if (sav
->sav_object
== 0) {
9280 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
9281 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
9282 sizeof (uint64_t), tx
);
9283 VERIFY(zap_update(spa
->spa_meta_objset
,
9284 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
9285 &sav
->sav_object
, tx
) == 0);
9288 nvroot
= fnvlist_alloc();
9289 if (sav
->sav_count
== 0) {
9290 fnvlist_add_nvlist_array(nvroot
, config
,
9291 (const nvlist_t
* const *)NULL
, 0);
9293 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
9294 for (i
= 0; i
< sav
->sav_count
; i
++)
9295 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
9296 B_FALSE
, VDEV_CONFIG_L2CACHE
);
9297 fnvlist_add_nvlist_array(nvroot
, config
,
9298 (const nvlist_t
* const *)list
, sav
->sav_count
);
9299 for (i
= 0; i
< sav
->sav_count
; i
++)
9300 nvlist_free(list
[i
]);
9301 kmem_free(list
, sav
->sav_count
* sizeof (void *));
9304 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
9305 nvlist_free(nvroot
);
9307 sav
->sav_sync
= B_FALSE
;
9311 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
9312 * The all-vdev ZAP must be empty.
9315 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
9317 spa_t
*spa
= vd
->vdev_spa
;
9319 if (vd
->vdev_root_zap
!= 0 &&
9320 spa_feature_is_active(spa
, SPA_FEATURE_AVZ_V2
)) {
9321 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
9322 vd
->vdev_root_zap
, tx
));
9324 if (vd
->vdev_top_zap
!= 0) {
9325 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
9326 vd
->vdev_top_zap
, tx
));
9328 if (vd
->vdev_leaf_zap
!= 0) {
9329 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
9330 vd
->vdev_leaf_zap
, tx
));
9332 for (uint64_t i
= 0; i
< vd
->vdev_children
; i
++) {
9333 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
9338 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
9343 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
9344 * its config may not be dirty but we still need to build per-vdev ZAPs.
9345 * Similarly, if the pool is being assembled (e.g. after a split), we
9346 * need to rebuild the AVZ although the config may not be dirty.
9348 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
9349 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
9352 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9354 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
9355 spa
->spa_avz_action
== AVZ_ACTION_INITIALIZE
||
9356 spa
->spa_all_vdev_zaps
!= 0);
9358 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
9359 /* Make and build the new AVZ */
9360 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
9361 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
9362 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
9364 /* Diff old AVZ with new one */
9368 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
9369 spa
->spa_all_vdev_zaps
);
9370 zap_cursor_retrieve(&zc
, &za
) == 0;
9371 zap_cursor_advance(&zc
)) {
9372 uint64_t vdzap
= za
.za_first_integer
;
9373 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
9376 * ZAP is listed in old AVZ but not in new one;
9379 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
9384 zap_cursor_fini(&zc
);
9386 /* Destroy the old AVZ */
9387 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
9388 spa
->spa_all_vdev_zaps
, tx
));
9390 /* Replace the old AVZ in the dir obj with the new one */
9391 VERIFY0(zap_update(spa
->spa_meta_objset
,
9392 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
9393 sizeof (new_avz
), 1, &new_avz
, tx
));
9395 spa
->spa_all_vdev_zaps
= new_avz
;
9396 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
9400 /* Walk through the AVZ and destroy all listed ZAPs */
9401 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
9402 spa
->spa_all_vdev_zaps
);
9403 zap_cursor_retrieve(&zc
, &za
) == 0;
9404 zap_cursor_advance(&zc
)) {
9405 uint64_t zap
= za
.za_first_integer
;
9406 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
9409 zap_cursor_fini(&zc
);
9411 /* Destroy and unlink the AVZ itself */
9412 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
9413 spa
->spa_all_vdev_zaps
, tx
));
9414 VERIFY0(zap_remove(spa
->spa_meta_objset
,
9415 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
9416 spa
->spa_all_vdev_zaps
= 0;
9419 if (spa
->spa_all_vdev_zaps
== 0) {
9420 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
9421 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
9422 DMU_POOL_VDEV_ZAP_MAP
, tx
);
9424 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
9426 /* Create ZAPs for vdevs that don't have them. */
9427 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
9429 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
9430 dmu_tx_get_txg(tx
), B_FALSE
);
9433 * If we're upgrading the spa version then make sure that
9434 * the config object gets updated with the correct version.
9436 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
9437 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
9438 spa
->spa_uberblock
.ub_version
);
9440 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9442 nvlist_free(spa
->spa_config_syncing
);
9443 spa
->spa_config_syncing
= config
;
9445 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
9449 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
9451 uint64_t *versionp
= arg
;
9452 uint64_t version
= *versionp
;
9453 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
9456 * Setting the version is special cased when first creating the pool.
9458 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
9460 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
9461 ASSERT(version
>= spa_version(spa
));
9463 spa
->spa_uberblock
.ub_version
= version
;
9464 vdev_config_dirty(spa
->spa_root_vdev
);
9465 spa_history_log_internal(spa
, "set", tx
, "version=%lld",
9466 (longlong_t
)version
);
9470 * Set zpool properties.
9473 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
9475 nvlist_t
*nvp
= arg
;
9476 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
9477 objset_t
*mos
= spa
->spa_meta_objset
;
9478 nvpair_t
*elem
= NULL
;
9480 mutex_enter(&spa
->spa_props_lock
);
9482 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
9484 const char *strval
, *fname
;
9486 const char *propname
;
9487 const char *elemname
= nvpair_name(elem
);
9488 zprop_type_t proptype
;
9491 switch (prop
= zpool_name_to_prop(elemname
)) {
9492 case ZPOOL_PROP_VERSION
:
9493 intval
= fnvpair_value_uint64(elem
);
9495 * The version is synced separately before other
9496 * properties and should be correct by now.
9498 ASSERT3U(spa_version(spa
), >=, intval
);
9501 case ZPOOL_PROP_ALTROOT
:
9503 * 'altroot' is a non-persistent property. It should
9504 * have been set temporarily at creation or import time.
9506 ASSERT(spa
->spa_root
!= NULL
);
9509 case ZPOOL_PROP_READONLY
:
9510 case ZPOOL_PROP_CACHEFILE
:
9512 * 'readonly' and 'cachefile' are also non-persistent
9516 case ZPOOL_PROP_COMMENT
:
9517 strval
= fnvpair_value_string(elem
);
9518 if (spa
->spa_comment
!= NULL
)
9519 spa_strfree(spa
->spa_comment
);
9520 spa
->spa_comment
= spa_strdup(strval
);
9522 * We need to dirty the configuration on all the vdevs
9523 * so that their labels get updated. We also need to
9524 * update the cache file to keep it in sync with the
9525 * MOS version. It's unnecessary to do this for pool
9526 * creation since the vdev's configuration has already
9529 if (tx
->tx_txg
!= TXG_INITIAL
) {
9530 vdev_config_dirty(spa
->spa_root_vdev
);
9531 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
9533 spa_history_log_internal(spa
, "set", tx
,
9534 "%s=%s", elemname
, strval
);
9536 case ZPOOL_PROP_COMPATIBILITY
:
9537 strval
= fnvpair_value_string(elem
);
9538 if (spa
->spa_compatibility
!= NULL
)
9539 spa_strfree(spa
->spa_compatibility
);
9540 spa
->spa_compatibility
= spa_strdup(strval
);
9542 * Dirty the configuration on vdevs as above.
9544 if (tx
->tx_txg
!= TXG_INITIAL
) {
9545 vdev_config_dirty(spa
->spa_root_vdev
);
9546 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
9549 spa_history_log_internal(spa
, "set", tx
,
9550 "%s=%s", nvpair_name(elem
), strval
);
9553 case ZPOOL_PROP_INVAL
:
9554 if (zpool_prop_feature(elemname
)) {
9555 fname
= strchr(elemname
, '@') + 1;
9556 VERIFY0(zfeature_lookup_name(fname
, &fid
));
9558 spa_feature_enable(spa
, fid
, tx
);
9559 spa_history_log_internal(spa
, "set", tx
,
9560 "%s=enabled", elemname
);
9562 } else if (!zfs_prop_user(elemname
)) {
9563 ASSERT(zpool_prop_feature(elemname
));
9569 * Set pool property values in the poolprops mos object.
9571 if (spa
->spa_pool_props_object
== 0) {
9572 spa
->spa_pool_props_object
=
9573 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
9574 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
9578 /* normalize the property name */
9579 if (prop
== ZPOOL_PROP_INVAL
) {
9580 propname
= elemname
;
9581 proptype
= PROP_TYPE_STRING
;
9583 propname
= zpool_prop_to_name(prop
);
9584 proptype
= zpool_prop_get_type(prop
);
9587 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
9588 ASSERT(proptype
== PROP_TYPE_STRING
);
9589 strval
= fnvpair_value_string(elem
);
9590 VERIFY0(zap_update(mos
,
9591 spa
->spa_pool_props_object
, propname
,
9592 1, strlen(strval
) + 1, strval
, tx
));
9593 spa_history_log_internal(spa
, "set", tx
,
9594 "%s=%s", elemname
, strval
);
9595 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
9596 intval
= fnvpair_value_uint64(elem
);
9598 if (proptype
== PROP_TYPE_INDEX
) {
9600 VERIFY0(zpool_prop_index_to_string(
9601 prop
, intval
, &unused
));
9603 VERIFY0(zap_update(mos
,
9604 spa
->spa_pool_props_object
, propname
,
9605 8, 1, &intval
, tx
));
9606 spa_history_log_internal(spa
, "set", tx
,
9607 "%s=%lld", elemname
,
9608 (longlong_t
)intval
);
9611 case ZPOOL_PROP_DELEGATION
:
9612 spa
->spa_delegation
= intval
;
9614 case ZPOOL_PROP_BOOTFS
:
9615 spa
->spa_bootfs
= intval
;
9617 case ZPOOL_PROP_FAILUREMODE
:
9618 spa
->spa_failmode
= intval
;
9620 case ZPOOL_PROP_AUTOTRIM
:
9621 spa
->spa_autotrim
= intval
;
9622 spa_async_request(spa
,
9623 SPA_ASYNC_AUTOTRIM_RESTART
);
9625 case ZPOOL_PROP_AUTOEXPAND
:
9626 spa
->spa_autoexpand
= intval
;
9627 if (tx
->tx_txg
!= TXG_INITIAL
)
9628 spa_async_request(spa
,
9629 SPA_ASYNC_AUTOEXPAND
);
9631 case ZPOOL_PROP_MULTIHOST
:
9632 spa
->spa_multihost
= intval
;
9638 ASSERT(0); /* not allowed */
9644 mutex_exit(&spa
->spa_props_lock
);
9648 * Perform one-time upgrade on-disk changes. spa_version() does not
9649 * reflect the new version this txg, so there must be no changes this
9650 * txg to anything that the upgrade code depends on after it executes.
9651 * Therefore this must be called after dsl_pool_sync() does the sync
9655 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
9657 if (spa_sync_pass(spa
) != 1)
9660 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9661 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
9663 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
9664 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
9665 dsl_pool_create_origin(dp
, tx
);
9667 /* Keeping the origin open increases spa_minref */
9668 spa
->spa_minref
+= 3;
9671 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
9672 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
9673 dsl_pool_upgrade_clones(dp
, tx
);
9676 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
9677 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
9678 dsl_pool_upgrade_dir_clones(dp
, tx
);
9680 /* Keeping the freedir open increases spa_minref */
9681 spa
->spa_minref
+= 3;
9684 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
9685 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9686 spa_feature_create_zap_objects(spa
, tx
);
9690 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
9691 * when possibility to use lz4 compression for metadata was added
9692 * Old pools that have this feature enabled must be upgraded to have
9693 * this feature active
9695 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
9696 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
9697 SPA_FEATURE_LZ4_COMPRESS
);
9698 boolean_t lz4_ac
= spa_feature_is_active(spa
,
9699 SPA_FEATURE_LZ4_COMPRESS
);
9701 if (lz4_en
&& !lz4_ac
)
9702 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
9706 * If we haven't written the salt, do so now. Note that the
9707 * feature may not be activated yet, but that's fine since
9708 * the presence of this ZAP entry is backwards compatible.
9710 if (zap_contains(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
9711 DMU_POOL_CHECKSUM_SALT
) == ENOENT
) {
9712 VERIFY0(zap_add(spa
->spa_meta_objset
,
9713 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CHECKSUM_SALT
, 1,
9714 sizeof (spa
->spa_cksum_salt
.zcs_bytes
),
9715 spa
->spa_cksum_salt
.zcs_bytes
, tx
));
9718 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
9722 vdev_indirect_state_sync_verify(vdev_t
*vd
)
9724 vdev_indirect_mapping_t
*vim __maybe_unused
= vd
->vdev_indirect_mapping
;
9725 vdev_indirect_births_t
*vib __maybe_unused
= vd
->vdev_indirect_births
;
9727 if (vd
->vdev_ops
== &vdev_indirect_ops
) {
9728 ASSERT(vim
!= NULL
);
9729 ASSERT(vib
!= NULL
);
9732 uint64_t obsolete_sm_object
= 0;
9733 ASSERT0(vdev_obsolete_sm_object(vd
, &obsolete_sm_object
));
9734 if (obsolete_sm_object
!= 0) {
9735 ASSERT(vd
->vdev_obsolete_sm
!= NULL
);
9736 ASSERT(vd
->vdev_removing
||
9737 vd
->vdev_ops
== &vdev_indirect_ops
);
9738 ASSERT(vdev_indirect_mapping_num_entries(vim
) > 0);
9739 ASSERT(vdev_indirect_mapping_bytes_mapped(vim
) > 0);
9740 ASSERT3U(obsolete_sm_object
, ==,
9741 space_map_object(vd
->vdev_obsolete_sm
));
9742 ASSERT3U(vdev_indirect_mapping_bytes_mapped(vim
), >=,
9743 space_map_allocated(vd
->vdev_obsolete_sm
));
9745 ASSERT(vd
->vdev_obsolete_segments
!= NULL
);
9748 * Since frees / remaps to an indirect vdev can only
9749 * happen in syncing context, the obsolete segments
9750 * tree must be empty when we start syncing.
9752 ASSERT0(range_tree_space(vd
->vdev_obsolete_segments
));
9756 * Set the top-level vdev's max queue depth. Evaluate each top-level's
9757 * async write queue depth in case it changed. The max queue depth will
9758 * not change in the middle of syncing out this txg.
9761 spa_sync_adjust_vdev_max_queue_depth(spa_t
*spa
)
9763 ASSERT(spa_writeable(spa
));
9765 vdev_t
*rvd
= spa
->spa_root_vdev
;
9766 uint32_t max_queue_depth
= zfs_vdev_async_write_max_active
*
9767 zfs_vdev_queue_depth_pct
/ 100;
9768 metaslab_class_t
*normal
= spa_normal_class(spa
);
9769 metaslab_class_t
*special
= spa_special_class(spa
);
9770 metaslab_class_t
*dedup
= spa_dedup_class(spa
);
9772 uint64_t slots_per_allocator
= 0;
9773 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9774 vdev_t
*tvd
= rvd
->vdev_child
[c
];
9776 metaslab_group_t
*mg
= tvd
->vdev_mg
;
9777 if (mg
== NULL
|| !metaslab_group_initialized(mg
))
9780 metaslab_class_t
*mc
= mg
->mg_class
;
9781 if (mc
!= normal
&& mc
!= special
&& mc
!= dedup
)
9785 * It is safe to do a lock-free check here because only async
9786 * allocations look at mg_max_alloc_queue_depth, and async
9787 * allocations all happen from spa_sync().
9789 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9790 ASSERT0(zfs_refcount_count(
9791 &(mg
->mg_allocator
[i
].mga_alloc_queue_depth
)));
9793 mg
->mg_max_alloc_queue_depth
= max_queue_depth
;
9795 for (int i
= 0; i
< mg
->mg_allocators
; i
++) {
9796 mg
->mg_allocator
[i
].mga_cur_max_alloc_queue_depth
=
9797 zfs_vdev_def_queue_depth
;
9799 slots_per_allocator
+= zfs_vdev_def_queue_depth
;
9802 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
9803 ASSERT0(zfs_refcount_count(&normal
->mc_allocator
[i
].
9805 ASSERT0(zfs_refcount_count(&special
->mc_allocator
[i
].
9807 ASSERT0(zfs_refcount_count(&dedup
->mc_allocator
[i
].
9809 normal
->mc_allocator
[i
].mca_alloc_max_slots
=
9810 slots_per_allocator
;
9811 special
->mc_allocator
[i
].mca_alloc_max_slots
=
9812 slots_per_allocator
;
9813 dedup
->mc_allocator
[i
].mca_alloc_max_slots
=
9814 slots_per_allocator
;
9816 normal
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9817 special
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9818 dedup
->mc_alloc_throttle_enabled
= zio_dva_throttle_enabled
;
9822 spa_sync_condense_indirect(spa_t
*spa
, dmu_tx_t
*tx
)
9824 ASSERT(spa_writeable(spa
));
9826 vdev_t
*rvd
= spa
->spa_root_vdev
;
9827 for (int c
= 0; c
< rvd
->vdev_children
; c
++) {
9828 vdev_t
*vd
= rvd
->vdev_child
[c
];
9829 vdev_indirect_state_sync_verify(vd
);
9831 if (vdev_indirect_should_condense(vd
)) {
9832 spa_condense_indirect_start_sync(vd
, tx
);
9839 spa_sync_iterate_to_convergence(spa_t
*spa
, dmu_tx_t
*tx
)
9841 objset_t
*mos
= spa
->spa_meta_objset
;
9842 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
9843 uint64_t txg
= tx
->tx_txg
;
9844 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
9847 int pass
= ++spa
->spa_sync_pass
;
9849 spa_sync_config_object(spa
, tx
);
9850 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
9851 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
9852 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
9853 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
9854 spa_errlog_sync(spa
, txg
);
9855 dsl_pool_sync(dp
, txg
);
9857 if (pass
< zfs_sync_pass_deferred_free
||
9858 spa_feature_is_active(spa
, SPA_FEATURE_LOG_SPACEMAP
)) {
9860 * If the log space map feature is active we don't
9861 * care about deferred frees and the deferred bpobj
9862 * as the log space map should effectively have the
9863 * same results (i.e. appending only to one object).
9865 spa_sync_frees(spa
, free_bpl
, tx
);
9868 * We can not defer frees in pass 1, because
9869 * we sync the deferred frees later in pass 1.
9871 ASSERT3U(pass
, >, 1);
9872 bplist_iterate(free_bpl
, bpobj_enqueue_alloc_cb
,
9873 &spa
->spa_deferred_bpobj
, tx
);
9878 dsl_scan_sync(dp
, tx
);
9879 dsl_errorscrub_sync(dp
, tx
);
9881 spa_sync_upgrades(spa
, tx
);
9883 spa_flush_metaslabs(spa
, tx
);
9886 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
))
9892 * dsl_pool_sync() -> dp_sync_tasks may have dirtied
9893 * the config. If that happens, this txg should not
9894 * be a no-op. So we must sync the config to the MOS
9895 * before checking for no-op.
9897 * Note that when the config is dirty, it will
9898 * be written to the MOS (i.e. the MOS will be
9899 * dirtied) every time we call spa_sync_config_object()
9900 * in this txg. Therefore we can't call this after
9901 * dsl_pool_sync() every pass, because it would
9902 * prevent us from converging, since we'd dirty
9903 * the MOS every pass.
9905 * Sync tasks can only be processed in pass 1, so
9906 * there's no need to do this in later passes.
9908 spa_sync_config_object(spa
, tx
);
9912 * Note: We need to check if the MOS is dirty because we could
9913 * have marked the MOS dirty without updating the uberblock
9914 * (e.g. if we have sync tasks but no dirty user data). We need
9915 * to check the uberblock's rootbp because it is updated if we
9916 * have synced out dirty data (though in this case the MOS will
9917 * most likely also be dirty due to second order effects, we
9918 * don't want to rely on that here).
9921 BP_GET_LOGICAL_BIRTH(&spa
->spa_uberblock
.ub_rootbp
) < txg
&&
9922 !dmu_objset_is_dirty(mos
, txg
)) {
9924 * Nothing changed on the first pass, therefore this
9925 * TXG is a no-op. Avoid syncing deferred frees, so
9926 * that we can keep this TXG as a no-op.
9928 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
9929 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
9930 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
9931 ASSERT(txg_list_empty(&dp
->dp_early_sync_tasks
, txg
));
9935 spa_sync_deferred_frees(spa
, tx
);
9936 } while (dmu_objset_is_dirty(mos
, txg
));
9940 * Rewrite the vdev configuration (which includes the uberblock) to
9941 * commit the transaction group.
9943 * If there are no dirty vdevs, we sync the uberblock to a few random
9944 * top-level vdevs that are known to be visible in the config cache
9945 * (see spa_vdev_add() for a complete description). If there *are* dirty
9946 * vdevs, sync the uberblock to all vdevs.
9949 spa_sync_rewrite_vdev_config(spa_t
*spa
, dmu_tx_t
*tx
)
9951 vdev_t
*rvd
= spa
->spa_root_vdev
;
9952 uint64_t txg
= tx
->tx_txg
;
9958 * We hold SCL_STATE to prevent vdev open/close/etc.
9959 * while we're attempting to write the vdev labels.
9961 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
9963 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
9964 vdev_t
*svd
[SPA_SYNC_MIN_VDEVS
] = { NULL
};
9966 int children
= rvd
->vdev_children
;
9967 int c0
= random_in_range(children
);
9969 for (int c
= 0; c
< children
; c
++) {
9971 rvd
->vdev_child
[(c0
+ c
) % children
];
9973 /* Stop when revisiting the first vdev */
9974 if (c
> 0 && svd
[0] == vd
)
9977 if (vd
->vdev_ms_array
== 0 ||
9979 !vdev_is_concrete(vd
))
9982 svd
[svdcount
++] = vd
;
9983 if (svdcount
== SPA_SYNC_MIN_VDEVS
)
9986 error
= vdev_config_sync(svd
, svdcount
, txg
);
9988 error
= vdev_config_sync(rvd
->vdev_child
,
9989 rvd
->vdev_children
, txg
);
9993 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
9995 spa_config_exit(spa
, SCL_STATE
, FTAG
);
9999 zio_suspend(spa
, NULL
, ZIO_SUSPEND_IOERR
);
10000 zio_resume_wait(spa
);
10005 * Sync the specified transaction group. New blocks may be dirtied as
10006 * part of the process, so we iterate until it converges.
10009 spa_sync(spa_t
*spa
, uint64_t txg
)
10013 VERIFY(spa_writeable(spa
));
10016 * Wait for i/os issued in open context that need to complete
10017 * before this txg syncs.
10019 (void) zio_wait(spa
->spa_txg_zio
[txg
& TXG_MASK
]);
10020 spa
->spa_txg_zio
[txg
& TXG_MASK
] = zio_root(spa
, NULL
, NULL
,
10024 * Now that there can be no more cloning in this transaction group,
10025 * but we are still before issuing frees, we can process pending BRT
10028 brt_pending_apply(spa
, txg
);
10031 * Lock out configuration changes.
10033 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
10035 spa
->spa_syncing_txg
= txg
;
10036 spa
->spa_sync_pass
= 0;
10038 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
10039 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
10040 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
10041 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
10045 * If there are any pending vdev state changes, convert them
10046 * into config changes that go out with this transaction group.
10048 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
10049 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
10050 /* Avoid holding the write lock unless actually necessary */
10051 if (vd
->vdev_aux
== NULL
) {
10052 vdev_state_clean(vd
);
10053 vdev_config_dirty(vd
);
10057 * We need the write lock here because, for aux vdevs,
10058 * calling vdev_config_dirty() modifies sav_config.
10059 * This is ugly and will become unnecessary when we
10060 * eliminate the aux vdev wart by integrating all vdevs
10061 * into the root vdev tree.
10063 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10064 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
10065 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
10066 vdev_state_clean(vd
);
10067 vdev_config_dirty(vd
);
10069 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10070 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
10072 spa_config_exit(spa
, SCL_STATE
, FTAG
);
10074 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
10075 dmu_tx_t
*tx
= dmu_tx_create_assigned(dp
, txg
);
10077 spa
->spa_sync_starttime
= gethrtime();
10078 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
10079 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_delay_taskq
,
10080 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
10081 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
10084 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
10085 * set spa_deflate if we have no raid-z vdevs.
10087 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
10088 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
10089 vdev_t
*rvd
= spa
->spa_root_vdev
;
10092 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
10093 vd
= rvd
->vdev_child
[i
];
10094 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
10097 if (i
== rvd
->vdev_children
) {
10098 spa
->spa_deflate
= TRUE
;
10099 VERIFY0(zap_add(spa
->spa_meta_objset
,
10100 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
10101 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
10105 spa_sync_adjust_vdev_max_queue_depth(spa
);
10107 spa_sync_condense_indirect(spa
, tx
);
10109 spa_sync_iterate_to_convergence(spa
, tx
);
10112 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
10114 * Make sure that the number of ZAPs for all the vdevs matches
10115 * the number of ZAPs in the per-vdev ZAP list. This only gets
10116 * called if the config is dirty; otherwise there may be
10117 * outstanding AVZ operations that weren't completed in
10118 * spa_sync_config_object.
10120 uint64_t all_vdev_zap_entry_count
;
10121 ASSERT0(zap_count(spa
->spa_meta_objset
,
10122 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
10123 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
10124 all_vdev_zap_entry_count
);
10128 if (spa
->spa_vdev_removal
!= NULL
) {
10129 ASSERT0(spa
->spa_vdev_removal
->svr_bytes_done
[txg
& TXG_MASK
]);
10132 spa_sync_rewrite_vdev_config(spa
, tx
);
10135 taskq_cancel_id(system_delay_taskq
, spa
->spa_deadman_tqid
);
10136 spa
->spa_deadman_tqid
= 0;
10139 * Clear the dirty config list.
10141 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
10142 vdev_config_clean(vd
);
10145 * Now that the new config has synced transactionally,
10146 * let it become visible to the config cache.
10148 if (spa
->spa_config_syncing
!= NULL
) {
10149 spa_config_set(spa
, spa
->spa_config_syncing
);
10150 spa
->spa_config_txg
= txg
;
10151 spa
->spa_config_syncing
= NULL
;
10154 dsl_pool_sync_done(dp
, txg
);
10156 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++) {
10157 mutex_enter(&spa
->spa_allocs
[i
].spaa_lock
);
10158 VERIFY0(avl_numnodes(&spa
->spa_allocs
[i
].spaa_tree
));
10159 mutex_exit(&spa
->spa_allocs
[i
].spaa_lock
);
10163 * Update usable space statistics.
10165 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
)))
10167 vdev_sync_done(vd
, txg
);
10169 metaslab_class_evict_old(spa
->spa_normal_class
, txg
);
10170 metaslab_class_evict_old(spa
->spa_log_class
, txg
);
10172 spa_sync_close_syncing_log_sm(spa
);
10174 spa_update_dspace(spa
);
10176 if (spa_get_autotrim(spa
) == SPA_AUTOTRIM_ON
)
10177 vdev_autotrim_kick(spa
);
10180 * It had better be the case that we didn't dirty anything
10181 * since vdev_config_sync().
10183 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
10184 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
10185 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
10187 while (zfs_pause_spa_sync
)
10190 spa
->spa_sync_pass
= 0;
10193 * Update the last synced uberblock here. We want to do this at
10194 * the end of spa_sync() so that consumers of spa_last_synced_txg()
10195 * will be guaranteed that all the processing associated with
10196 * that txg has been completed.
10198 spa
->spa_ubsync
= spa
->spa_uberblock
;
10199 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
10201 spa_handle_ignored_writes(spa
);
10204 * If any async tasks have been requested, kick them off.
10206 spa_async_dispatch(spa
);
10210 * Sync all pools. We don't want to hold the namespace lock across these
10211 * operations, so we take a reference on the spa_t and drop the lock during the
10215 spa_sync_allpools(void)
10218 mutex_enter(&spa_namespace_lock
);
10219 while ((spa
= spa_next(spa
)) != NULL
) {
10220 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
10221 !spa_writeable(spa
) || spa_suspended(spa
))
10223 spa_open_ref(spa
, FTAG
);
10224 mutex_exit(&spa_namespace_lock
);
10225 txg_wait_synced(spa_get_dsl(spa
), 0);
10226 mutex_enter(&spa_namespace_lock
);
10227 spa_close(spa
, FTAG
);
10229 mutex_exit(&spa_namespace_lock
);
10233 spa_sync_tq_create(spa_t
*spa
, const char *name
)
10235 kthread_t
**kthreads
;
10237 ASSERT(spa
->spa_sync_tq
== NULL
);
10238 ASSERT3S(spa
->spa_alloc_count
, <=, boot_ncpus
);
10241 * - do not allow more allocators than cpus.
10242 * - there may be more cpus than allocators.
10243 * - do not allow more sync taskq threads than allocators or cpus.
10245 int nthreads
= spa
->spa_alloc_count
;
10246 spa
->spa_syncthreads
= kmem_zalloc(sizeof (spa_syncthread_info_t
) *
10247 nthreads
, KM_SLEEP
);
10249 spa
->spa_sync_tq
= taskq_create_synced(name
, nthreads
, minclsyspri
,
10250 nthreads
, INT_MAX
, TASKQ_PREPOPULATE
, &kthreads
);
10251 VERIFY(spa
->spa_sync_tq
!= NULL
);
10252 VERIFY(kthreads
!= NULL
);
10254 spa_syncthread_info_t
*ti
= spa
->spa_syncthreads
;
10255 for (int i
= 0; i
< nthreads
; i
++, ti
++) {
10256 ti
->sti_thread
= kthreads
[i
];
10257 ti
->sti_allocator
= i
;
10260 kmem_free(kthreads
, sizeof (*kthreads
) * nthreads
);
10261 return (spa
->spa_sync_tq
);
10265 spa_sync_tq_destroy(spa_t
*spa
)
10267 ASSERT(spa
->spa_sync_tq
!= NULL
);
10269 taskq_wait(spa
->spa_sync_tq
);
10270 taskq_destroy(spa
->spa_sync_tq
);
10271 kmem_free(spa
->spa_syncthreads
,
10272 sizeof (spa_syncthread_info_t
) * spa
->spa_alloc_count
);
10273 spa
->spa_sync_tq
= NULL
;
10277 spa_acq_allocator(spa_t
*spa
)
10281 if (spa
->spa_alloc_count
== 1)
10284 mutex_enter(&spa
->spa_allocs_use
->sau_lock
);
10285 uint_t r
= spa
->spa_allocs_use
->sau_rotor
;
10287 if (++r
== spa
->spa_alloc_count
)
10289 } while (spa
->spa_allocs_use
->sau_inuse
[r
]);
10290 spa
->spa_allocs_use
->sau_inuse
[r
] = B_TRUE
;
10291 spa
->spa_allocs_use
->sau_rotor
= r
;
10292 mutex_exit(&spa
->spa_allocs_use
->sau_lock
);
10294 spa_syncthread_info_t
*ti
= spa
->spa_syncthreads
;
10295 for (i
= 0; i
< spa
->spa_alloc_count
; i
++, ti
++) {
10296 if (ti
->sti_thread
== curthread
) {
10297 ti
->sti_allocator
= r
;
10301 ASSERT3S(i
, <, spa
->spa_alloc_count
);
10306 spa_rel_allocator(spa_t
*spa
, uint_t allocator
)
10308 if (spa
->spa_alloc_count
> 1)
10309 spa
->spa_allocs_use
->sau_inuse
[allocator
] = B_FALSE
;
10313 spa_select_allocator(zio_t
*zio
)
10315 zbookmark_phys_t
*bm
= &zio
->io_bookmark
;
10316 spa_t
*spa
= zio
->io_spa
;
10318 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
10321 * A gang block (for example) may have inherited its parent's
10322 * allocator, in which case there is nothing further to do here.
10324 if (ZIO_HAS_ALLOCATOR(zio
))
10327 ASSERT(spa
!= NULL
);
10328 ASSERT(bm
!= NULL
);
10331 * First try to use an allocator assigned to the syncthread, and set
10332 * the corresponding write issue taskq for the allocator.
10333 * Note, we must have an open pool to do this.
10335 if (spa
->spa_sync_tq
!= NULL
) {
10336 spa_syncthread_info_t
*ti
= spa
->spa_syncthreads
;
10337 for (int i
= 0; i
< spa
->spa_alloc_count
; i
++, ti
++) {
10338 if (ti
->sti_thread
== curthread
) {
10339 zio
->io_allocator
= ti
->sti_allocator
;
10346 * We want to try to use as many allocators as possible to help improve
10347 * performance, but we also want logically adjacent IOs to be physically
10348 * adjacent to improve sequential read performance. We chunk each object
10349 * into 2^20 block regions, and then hash based on the objset, object,
10350 * level, and region to accomplish both of these goals.
10352 uint64_t hv
= cityhash4(bm
->zb_objset
, bm
->zb_object
, bm
->zb_level
,
10353 bm
->zb_blkid
>> 20);
10355 zio
->io_allocator
= (uint_t
)hv
% spa
->spa_alloc_count
;
10359 * ==========================================================================
10360 * Miscellaneous routines
10361 * ==========================================================================
10365 * Remove all pools in the system.
10368 spa_evict_all(void)
10373 * Remove all cached state. All pools should be closed now,
10374 * so every spa in the AVL tree should be unreferenced.
10376 mutex_enter(&spa_namespace_lock
);
10377 while ((spa
= spa_next(NULL
)) != NULL
) {
10379 * Stop async tasks. The async thread may need to detach
10380 * a device that's been replaced, which requires grabbing
10381 * spa_namespace_lock, so we must drop it here.
10383 spa_open_ref(spa
, FTAG
);
10384 mutex_exit(&spa_namespace_lock
);
10385 spa_async_suspend(spa
);
10386 mutex_enter(&spa_namespace_lock
);
10387 spa_close(spa
, FTAG
);
10389 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
10391 spa_deactivate(spa
);
10395 mutex_exit(&spa_namespace_lock
);
10399 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
10404 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
10408 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
10409 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
10410 if (vd
->vdev_guid
== guid
)
10414 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
10415 vd
= spa
->spa_spares
.sav_vdevs
[i
];
10416 if (vd
->vdev_guid
== guid
)
10425 spa_upgrade(spa_t
*spa
, uint64_t version
)
10427 ASSERT(spa_writeable(spa
));
10429 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
10432 * This should only be called for a non-faulted pool, and since a
10433 * future version would result in an unopenable pool, this shouldn't be
10436 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
10437 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
10439 spa
->spa_uberblock
.ub_version
= version
;
10440 vdev_config_dirty(spa
->spa_root_vdev
);
10442 spa_config_exit(spa
, SCL_ALL
, FTAG
);
10444 txg_wait_synced(spa_get_dsl(spa
), 0);
10448 spa_has_aux_vdev(spa_t
*spa
, uint64_t guid
, spa_aux_vdev_t
*sav
)
10452 uint64_t vdev_guid
;
10454 for (i
= 0; i
< sav
->sav_count
; i
++)
10455 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
10458 for (i
= 0; i
< sav
->sav_npending
; i
++) {
10459 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
10460 &vdev_guid
) == 0 && vdev_guid
== guid
)
10468 spa_has_l2cache(spa_t
*spa
, uint64_t guid
)
10470 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_l2cache
));
10474 spa_has_spare(spa_t
*spa
, uint64_t guid
)
10476 return (spa_has_aux_vdev(spa
, guid
, &spa
->spa_spares
));
10480 * Check if a pool has an active shared spare device.
10481 * Note: reference count of an active spare is 2, as a spare and as a replace
10484 spa_has_active_shared_spare(spa_t
*spa
)
10488 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
10490 for (i
= 0; i
< sav
->sav_count
; i
++) {
10491 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
10492 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
10501 spa_total_metaslabs(spa_t
*spa
)
10503 vdev_t
*rvd
= spa
->spa_root_vdev
;
10506 for (uint64_t c
= 0; c
< rvd
->vdev_children
; c
++) {
10507 vdev_t
*vd
= rvd
->vdev_child
[c
];
10508 if (!vdev_is_concrete(vd
))
10510 m
+= vd
->vdev_ms_count
;
10516 * Notify any waiting threads that some activity has switched from being in-
10517 * progress to not-in-progress so that the thread can wake up and determine
10518 * whether it is finished waiting.
10521 spa_notify_waiters(spa_t
*spa
)
10524 * Acquiring spa_activities_lock here prevents the cv_broadcast from
10525 * happening between the waiting thread's check and cv_wait.
10527 mutex_enter(&spa
->spa_activities_lock
);
10528 cv_broadcast(&spa
->spa_activities_cv
);
10529 mutex_exit(&spa
->spa_activities_lock
);
10533 * Notify any waiting threads that the pool is exporting, and then block until
10534 * they are finished using the spa_t.
10537 spa_wake_waiters(spa_t
*spa
)
10539 mutex_enter(&spa
->spa_activities_lock
);
10540 spa
->spa_waiters_cancel
= B_TRUE
;
10541 cv_broadcast(&spa
->spa_activities_cv
);
10542 while (spa
->spa_waiters
!= 0)
10543 cv_wait(&spa
->spa_waiters_cv
, &spa
->spa_activities_lock
);
10544 spa
->spa_waiters_cancel
= B_FALSE
;
10545 mutex_exit(&spa
->spa_activities_lock
);
10548 /* Whether the vdev or any of its descendants are being initialized/trimmed. */
10550 spa_vdev_activity_in_progress_impl(vdev_t
*vd
, zpool_wait_activity_t activity
)
10552 spa_t
*spa
= vd
->vdev_spa
;
10554 ASSERT(spa_config_held(spa
, SCL_CONFIG
| SCL_STATE
, RW_READER
));
10555 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
10556 ASSERT(activity
== ZPOOL_WAIT_INITIALIZE
||
10557 activity
== ZPOOL_WAIT_TRIM
);
10559 kmutex_t
*lock
= activity
== ZPOOL_WAIT_INITIALIZE
?
10560 &vd
->vdev_initialize_lock
: &vd
->vdev_trim_lock
;
10562 mutex_exit(&spa
->spa_activities_lock
);
10564 mutex_enter(&spa
->spa_activities_lock
);
10566 boolean_t in_progress
= (activity
== ZPOOL_WAIT_INITIALIZE
) ?
10567 (vd
->vdev_initialize_state
== VDEV_INITIALIZE_ACTIVE
) :
10568 (vd
->vdev_trim_state
== VDEV_TRIM_ACTIVE
);
10574 for (int i
= 0; i
< vd
->vdev_children
; i
++) {
10575 if (spa_vdev_activity_in_progress_impl(vd
->vdev_child
[i
],
10584 * If use_guid is true, this checks whether the vdev specified by guid is
10585 * being initialized/trimmed. Otherwise, it checks whether any vdev in the pool
10586 * is being initialized/trimmed. The caller must hold the config lock and
10587 * spa_activities_lock.
10590 spa_vdev_activity_in_progress(spa_t
*spa
, boolean_t use_guid
, uint64_t guid
,
10591 zpool_wait_activity_t activity
, boolean_t
*in_progress
)
10593 mutex_exit(&spa
->spa_activities_lock
);
10594 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
10595 mutex_enter(&spa
->spa_activities_lock
);
10599 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
10600 if (vd
== NULL
|| !vd
->vdev_ops
->vdev_op_leaf
) {
10601 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10605 vd
= spa
->spa_root_vdev
;
10608 *in_progress
= spa_vdev_activity_in_progress_impl(vd
, activity
);
10610 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10615 * Locking for waiting threads
10616 * ---------------------------
10618 * Waiting threads need a way to check whether a given activity is in progress,
10619 * and then, if it is, wait for it to complete. Each activity will have some
10620 * in-memory representation of the relevant on-disk state which can be used to
10621 * determine whether or not the activity is in progress. The in-memory state and
10622 * the locking used to protect it will be different for each activity, and may
10623 * not be suitable for use with a cvar (e.g., some state is protected by the
10624 * config lock). To allow waiting threads to wait without any races, another
10625 * lock, spa_activities_lock, is used.
10627 * When the state is checked, both the activity-specific lock (if there is one)
10628 * and spa_activities_lock are held. In some cases, the activity-specific lock
10629 * is acquired explicitly (e.g. the config lock). In others, the locking is
10630 * internal to some check (e.g. bpobj_is_empty). After checking, the waiting
10631 * thread releases the activity-specific lock and, if the activity is in
10632 * progress, then cv_waits using spa_activities_lock.
10634 * The waiting thread is woken when another thread, one completing some
10635 * activity, updates the state of the activity and then calls
10636 * spa_notify_waiters, which will cv_broadcast. This 'completing' thread only
10637 * needs to hold its activity-specific lock when updating the state, and this
10638 * lock can (but doesn't have to) be dropped before calling spa_notify_waiters.
10640 * Because spa_notify_waiters acquires spa_activities_lock before broadcasting,
10641 * and because it is held when the waiting thread checks the state of the
10642 * activity, it can never be the case that the completing thread both updates
10643 * the activity state and cv_broadcasts in between the waiting thread's check
10644 * and cv_wait. Thus, a waiting thread can never miss a wakeup.
10646 * In order to prevent deadlock, when the waiting thread does its check, in some
10647 * cases it will temporarily drop spa_activities_lock in order to acquire the
10648 * activity-specific lock. The order in which spa_activities_lock and the
10649 * activity specific lock are acquired in the waiting thread is determined by
10650 * the order in which they are acquired in the completing thread; if the
10651 * completing thread calls spa_notify_waiters with the activity-specific lock
10652 * held, then the waiting thread must also acquire the activity-specific lock
10657 spa_activity_in_progress(spa_t
*spa
, zpool_wait_activity_t activity
,
10658 boolean_t use_tag
, uint64_t tag
, boolean_t
*in_progress
)
10662 ASSERT(MUTEX_HELD(&spa
->spa_activities_lock
));
10664 switch (activity
) {
10665 case ZPOOL_WAIT_CKPT_DISCARD
:
10667 (spa_feature_is_active(spa
, SPA_FEATURE_POOL_CHECKPOINT
) &&
10668 zap_contains(spa_meta_objset(spa
),
10669 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_ZPOOL_CHECKPOINT
) ==
10672 case ZPOOL_WAIT_FREE
:
10673 *in_progress
= ((spa_version(spa
) >= SPA_VERSION_DEADLISTS
&&
10674 !bpobj_is_empty(&spa
->spa_dsl_pool
->dp_free_bpobj
)) ||
10675 spa_feature_is_active(spa
, SPA_FEATURE_ASYNC_DESTROY
) ||
10676 spa_livelist_delete_check(spa
));
10678 case ZPOOL_WAIT_INITIALIZE
:
10679 case ZPOOL_WAIT_TRIM
:
10680 error
= spa_vdev_activity_in_progress(spa
, use_tag
, tag
,
10681 activity
, in_progress
);
10683 case ZPOOL_WAIT_REPLACE
:
10684 mutex_exit(&spa
->spa_activities_lock
);
10685 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
10686 mutex_enter(&spa
->spa_activities_lock
);
10688 *in_progress
= vdev_replace_in_progress(spa
->spa_root_vdev
);
10689 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
10691 case ZPOOL_WAIT_REMOVE
:
10692 *in_progress
= (spa
->spa_removing_phys
.sr_state
==
10695 case ZPOOL_WAIT_RESILVER
:
10696 *in_progress
= vdev_rebuild_active(spa
->spa_root_vdev
);
10700 case ZPOOL_WAIT_SCRUB
:
10702 boolean_t scanning
, paused
, is_scrub
;
10703 dsl_scan_t
*scn
= spa
->spa_dsl_pool
->dp_scan
;
10705 is_scrub
= (scn
->scn_phys
.scn_func
== POOL_SCAN_SCRUB
);
10706 scanning
= (scn
->scn_phys
.scn_state
== DSS_SCANNING
);
10707 paused
= dsl_scan_is_paused_scrub(scn
);
10708 *in_progress
= (scanning
&& !paused
&&
10709 is_scrub
== (activity
== ZPOOL_WAIT_SCRUB
));
10712 case ZPOOL_WAIT_RAIDZ_EXPAND
:
10714 vdev_raidz_expand_t
*vre
= spa
->spa_raidz_expand
;
10715 *in_progress
= (vre
!= NULL
&& vre
->vre_state
== DSS_SCANNING
);
10719 panic("unrecognized value for activity %d", activity
);
10726 spa_wait_common(const char *pool
, zpool_wait_activity_t activity
,
10727 boolean_t use_tag
, uint64_t tag
, boolean_t
*waited
)
10730 * The tag is used to distinguish between instances of an activity.
10731 * 'initialize' and 'trim' are the only activities that we use this for.
10732 * The other activities can only have a single instance in progress in a
10733 * pool at one time, making the tag unnecessary.
10735 * There can be multiple devices being replaced at once, but since they
10736 * all finish once resilvering finishes, we don't bother keeping track
10737 * of them individually, we just wait for them all to finish.
10739 if (use_tag
&& activity
!= ZPOOL_WAIT_INITIALIZE
&&
10740 activity
!= ZPOOL_WAIT_TRIM
)
10743 if (activity
< 0 || activity
>= ZPOOL_WAIT_NUM_ACTIVITIES
)
10747 int error
= spa_open(pool
, &spa
, FTAG
);
10752 * Increment the spa's waiter count so that we can call spa_close and
10753 * still ensure that the spa_t doesn't get freed before this thread is
10754 * finished with it when the pool is exported. We want to call spa_close
10755 * before we start waiting because otherwise the additional ref would
10756 * prevent the pool from being exported or destroyed throughout the
10757 * potentially long wait.
10759 mutex_enter(&spa
->spa_activities_lock
);
10760 spa
->spa_waiters
++;
10761 spa_close(spa
, FTAG
);
10765 boolean_t in_progress
;
10766 error
= spa_activity_in_progress(spa
, activity
, use_tag
, tag
,
10769 if (error
|| !in_progress
|| spa
->spa_waiters_cancel
)
10774 if (cv_wait_sig(&spa
->spa_activities_cv
,
10775 &spa
->spa_activities_lock
) == 0) {
10781 spa
->spa_waiters
--;
10782 cv_signal(&spa
->spa_waiters_cv
);
10783 mutex_exit(&spa
->spa_activities_lock
);
10789 * Wait for a particular instance of the specified activity to complete, where
10790 * the instance is identified by 'tag'
10793 spa_wait_tag(const char *pool
, zpool_wait_activity_t activity
, uint64_t tag
,
10796 return (spa_wait_common(pool
, activity
, B_TRUE
, tag
, waited
));
10800 * Wait for all instances of the specified activity complete
10803 spa_wait(const char *pool
, zpool_wait_activity_t activity
, boolean_t
*waited
)
10806 return (spa_wait_common(pool
, activity
, B_FALSE
, 0, waited
));
10810 spa_event_create(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10812 sysevent_t
*ev
= NULL
;
10814 nvlist_t
*resource
;
10816 resource
= zfs_event_create(spa
, vd
, FM_SYSEVENT_CLASS
, name
, hist_nvl
);
10818 ev
= kmem_alloc(sizeof (sysevent_t
), KM_SLEEP
);
10819 ev
->resource
= resource
;
10822 (void) spa
, (void) vd
, (void) hist_nvl
, (void) name
;
10828 spa_event_post(sysevent_t
*ev
)
10832 zfs_zevent_post(ev
->resource
, NULL
, zfs_zevent_post_cb
);
10833 kmem_free(ev
, sizeof (*ev
));
10841 * Post a zevent corresponding to the given sysevent. The 'name' must be one
10842 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
10843 * filled in from the spa and (optionally) the vdev. This doesn't do anything
10844 * in the userland libzpool, as we don't want consumers to misinterpret ztest
10845 * or zdb as real changes.
10848 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, nvlist_t
*hist_nvl
, const char *name
)
10850 spa_event_post(spa_event_create(spa
, vd
, hist_nvl
, name
));
10853 /* state manipulation functions */
10854 EXPORT_SYMBOL(spa_open
);
10855 EXPORT_SYMBOL(spa_open_rewind
);
10856 EXPORT_SYMBOL(spa_get_stats
);
10857 EXPORT_SYMBOL(spa_create
);
10858 EXPORT_SYMBOL(spa_import
);
10859 EXPORT_SYMBOL(spa_tryimport
);
10860 EXPORT_SYMBOL(spa_destroy
);
10861 EXPORT_SYMBOL(spa_export
);
10862 EXPORT_SYMBOL(spa_reset
);
10863 EXPORT_SYMBOL(spa_async_request
);
10864 EXPORT_SYMBOL(spa_async_suspend
);
10865 EXPORT_SYMBOL(spa_async_resume
);
10866 EXPORT_SYMBOL(spa_inject_addref
);
10867 EXPORT_SYMBOL(spa_inject_delref
);
10868 EXPORT_SYMBOL(spa_scan_stat_init
);
10869 EXPORT_SYMBOL(spa_scan_get_stats
);
10871 /* device manipulation */
10872 EXPORT_SYMBOL(spa_vdev_add
);
10873 EXPORT_SYMBOL(spa_vdev_attach
);
10874 EXPORT_SYMBOL(spa_vdev_detach
);
10875 EXPORT_SYMBOL(spa_vdev_setpath
);
10876 EXPORT_SYMBOL(spa_vdev_setfru
);
10877 EXPORT_SYMBOL(spa_vdev_split_mirror
);
10879 /* spare statech is global across all pools) */
10880 EXPORT_SYMBOL(spa_spare_add
);
10881 EXPORT_SYMBOL(spa_spare_remove
);
10882 EXPORT_SYMBOL(spa_spare_exists
);
10883 EXPORT_SYMBOL(spa_spare_activate
);
10885 /* L2ARC statech is global across all pools) */
10886 EXPORT_SYMBOL(spa_l2cache_add
);
10887 EXPORT_SYMBOL(spa_l2cache_remove
);
10888 EXPORT_SYMBOL(spa_l2cache_exists
);
10889 EXPORT_SYMBOL(spa_l2cache_activate
);
10890 EXPORT_SYMBOL(spa_l2cache_drop
);
10893 EXPORT_SYMBOL(spa_scan
);
10894 EXPORT_SYMBOL(spa_scan_stop
);
10897 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
10898 EXPORT_SYMBOL(spa_sync_allpools
);
10901 EXPORT_SYMBOL(spa_prop_set
);
10902 EXPORT_SYMBOL(spa_prop_get
);
10903 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
10905 /* asynchronous event notification */
10906 EXPORT_SYMBOL(spa_event_notify
);
10908 ZFS_MODULE_PARAM(zfs_metaslab
, metaslab_
, preload_pct
, UINT
, ZMOD_RW
,
10909 "Percentage of CPUs to run a metaslab preload taskq");
10911 /* BEGIN CSTYLED */
10912 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_shift
, UINT
, ZMOD_RW
,
10913 "log2 fraction of arc that can be used by inflight I/Os when "
10914 "verifying pool during import");
10917 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_metadata
, INT
, ZMOD_RW
,
10918 "Set to traverse metadata on pool import");
10920 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_verify_data
, INT
, ZMOD_RW
,
10921 "Set to traverse data on pool import");
10923 ZFS_MODULE_PARAM(zfs_spa
, spa_
, load_print_vdev_tree
, INT
, ZMOD_RW
,
10924 "Print vdev tree to zfs_dbgmsg during pool import");
10926 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_pct
, UINT
, ZMOD_RW
,
10927 "Percentage of CPUs to run an IO worker thread");
10929 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_batch_tpq
, UINT
, ZMOD_RW
,
10930 "Number of threads per IO worker taskqueue");
10932 /* BEGIN CSTYLED */
10933 ZFS_MODULE_PARAM(zfs
, zfs_
, max_missing_tvds
, U64
, ZMOD_RW
,
10934 "Allow importing pool with up to this number of missing top-level "
10935 "vdevs (in read-only mode)");
10938 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_pause
, INT
,
10939 ZMOD_RW
, "Set the livelist condense zthr to pause");
10941 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_pause
, INT
,
10942 ZMOD_RW
, "Set the livelist condense synctask to pause");
10944 /* BEGIN CSTYLED */
10945 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, sync_cancel
,
10947 "Whether livelist condensing was canceled in the synctask");
10949 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, zthr_cancel
,
10951 "Whether livelist condensing was canceled in the zthr function");
10953 ZFS_MODULE_PARAM(zfs_livelist_condense
, zfs_livelist_condense_
, new_alloc
, INT
,
10955 "Whether extra ALLOC blkptrs were added to a livelist entry while it "
10956 "was being condensed");
10959 ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio
, zio_
, taskq_read
,
10960 spa_taskq_read_param_set
, spa_taskq_read_param_get
, ZMOD_RW
,
10961 "Configure IO queues for read IO");
10962 ZFS_MODULE_VIRTUAL_PARAM_CALL(zfs_zio
, zio_
, taskq_write
,
10963 spa_taskq_write_param_set
, spa_taskq_write_param_get
, ZMOD_RW
,
10964 "Configure IO queues for write IO");
10968 ZFS_MODULE_PARAM(zfs_zio
, zio_
, taskq_write_tpq
, UINT
, ZMOD_RW
,
10969 "Number of CPUs per write issue taskq");